diff --git a/app/code/Magento/Ui/view/base/web/js/grid/columns/image-preview.js b/app/code/Magento/Ui/view/base/web/js/grid/columns/image-preview.js index 1ef2ebf6594fa..fec9fd969d61e 100644 --- a/app/code/Magento/Ui/view/base/web/js/grid/columns/image-preview.js +++ b/app/code/Magento/Ui/view/base/web/js/grid/columns/image-preview.js @@ -4,8 +4,9 @@ */ define([ 'jquery', - 'Magento_Ui/js/grid/columns/column' -], function ($, Column) { + 'Magento_Ui/js/grid/columns/column', + 'Magento_Ui/js/lib/key-codes' +], function ($, Column, keyCodes) { 'use strict'; return Column.extend({ @@ -32,6 +33,18 @@ define([ } }, + /** + * Initialize image preview component + * + * @returns {Object} + */ + initialize: function () { + this._super(); + $(document).on('keydown', this.handleKeyDown.bind(this)); + + return this; + }, + /** * Init observable variables * @return {Object} @@ -54,8 +67,13 @@ define([ * @param {Object} record */ next: function (record) { - var recordToShow = this.getRecord(record._rowIndex + 1); + var recordToShow; + + if (record._rowIndex + 1 === this.masonry().rows().length) { + return; + } + recordToShow = this.getRecord(record._rowIndex + 1); recordToShow.rowNumber = record.lastInRow ? record.rowNumber + 1 : record.rowNumber; this.show(recordToShow); }, @@ -66,7 +84,12 @@ define([ * @param {Object} record */ prev: function (record) { - var recordToShow = this.getRecord(record._rowIndex - 1); + var recordToShow; + + if (record._rowIndex === 0) { + return; + } + recordToShow = this.getRecord(record._rowIndex - 1); recordToShow.rowNumber = record.firstInRow ? record.rowNumber - 1 : record.rowNumber; this.show(recordToShow); @@ -174,6 +197,23 @@ define([ block: 'center', inline: 'nearest' }); + }, + + /** + * Handle keyboard navigation for image preview + * + * @param {Object} e + */ + handleKeyDown: function (e) { + var key = keyCodes[e.keyCode]; + + if (this.visibleRecord() !== null) { + if (key === 'pageLeftKey') { + this.prev(this.displayedRecord()); + } else if (key === 'pageRightKey') { + this.next(this.displayedRecord()); + } + } } }); });