Skip to content

Added masonry grid and columns components #25464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 179 additions & 0 deletions app/code/Magento/Ui/view/base/web/js/grid/columns/image-preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
define([
'jquery',
'Magento_Ui/js/grid/columns/column'
], function ($, Column) {
'use strict';

return Column.extend({
defaults: {
previewImageSelector: '[data-image-preview]',
visibleRecord: null,
height: 0,
displayedRecord: {},
lastOpenedImage: null,
modules: {
masonry: '${ $.parentName }',
thumbnailComponent: '${ $.parentName }.thumbnail_url'
},
statefull: {
sorting: true,
lastOpenedImage: true
},
listens: {
'${ $.provider }:params.filters': 'hide',
'${ $.provider }:params.search': 'hide'
},
exports: {
height: '${ $.parentName }.thumbnail_url:previewHeight'
}
},

/**
* Init observable variables
* @return {Object}
*/
initObservable: function () {
this._super()
.observe([
'visibleRecord',
'height',
'displayedRecord',
'lastOpenedImage'
]);

return this;
},

/**
* Next image preview
*
* @param {Object} record
*/
next: function (record) {
var recordToShow = this.getRecord(record._rowIndex + 1);

recordToShow.rowNumber = record.lastInRow ? record.rowNumber + 1 : record.rowNumber;
this.show(recordToShow);
},

/**
* Previous image preview
*
* @param {Object} record
*/
prev: function (record) {
var recordToShow = this.getRecord(record._rowIndex - 1);

recordToShow.rowNumber = record.firstInRow ? record.rowNumber - 1 : record.rowNumber;
this.show(recordToShow);
},

/**
* Get record
*
* @param {Integer} recordIndex
*
* @return {Object}
*/
getRecord: function (recordIndex) {
return this.masonry().rows()[recordIndex];
},

/**
* Set selected row id
*
* @param {Number} rowId
* @private
*/
_selectRow: function (rowId) {
this.thumbnailComponent().previewRowId(rowId);
},

/**
* Show image preview
*
* @param {Object} record
*/
show: function (record) {
var img;

this.hide();
this.displayedRecord(record);
this._selectRow(record.rowNumber || null);
this.visibleRecord(record._rowIndex);

img = $(this.previewImageSelector + ' img');

if (img.get(0).complete) {
this.updateHeight();
this.scrollToPreview();
} else {
img.load(function () {
this.updateHeight();
this.scrollToPreview();
}.bind(this));
}

this.lastOpenedImage(record._rowIndex);
},

/**
* Update image preview section height
*/
updateHeight: function () {
this.height($(this.previewImageSelector).height() + 'px');
},

/**
* Close image preview
*/
hide: function () {
this.lastOpenedImage(null);
this.visibleRecord(null);
this.height(0);
this._selectRow(null);
},

/**
* Returns visibility for given record.
*
* @param {Object} record
* @return {*|bool}
*/
isVisible: function (record) {
if (this.lastOpenedImage() === record._rowIndex &&
this.visibleRecord() === null
) {
this.show(record);
}

return this.visibleRecord() === record._rowIndex || false;
},

/**
* Get styles for preview
*
* @returns {Object}
*/
getStyles: function () {
return {
'margin-top': '-' + this.height()
};
},

/**
* Scroll to preview window
*/
scrollToPreview: function () {
$(this.previewImageSelector).get(0).scrollIntoView({
behavior: 'smooth',
block: 'center',
inline: 'nearest'
});
}
});
});
89 changes: 89 additions & 0 deletions app/code/Magento/Ui/view/base/web/js/grid/columns/image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
define([
'Magento_Ui/js/grid/columns/column'
], function (Column) {
'use strict';

return Column.extend({
defaults: {
modules: {
previewComponent: '${ $.parentName }.preview'
},
previewRowId: null,
previewHeight: 0,
fields: {
id: 'id',
url: 'url'
}
},

/**
* Init observable variables
* @return {Object}
*/
initObservable: function () {
this._super()
.observe([
'previewRowId',
'previewHeight'
]);

return this;
},

/**
* Returns url to given record.
*
* @param {Object} record - Data to be preprocessed.
* @returns {String}
*/
getUrl: function (record) {
return record[this.fields.url];
},

/**
* Returns id to given record.
*
* @param {Object} record - Data to be preprocessed.
* @returns {Number}
*/
getId: function (record) {
return record[this.fields.id];
},

/**
* Returns container styles to given record.
*
* @param {Object} record - Data to be preprocessed.
* @returns {Object}
*/
getStyles: function (record) {
var styles = record.styles();

styles['margin-bottom'] = this.previewRowId() === record.rowNumber ? this.previewHeight : 0;
record.styles(styles);

return record.styles;
},

/**
* Returns class list to given record.
*
* @param {Object} record - Data to be preprocessed.
* @returns {Object}
*/
getClasses: function (record) {
return record.css || {};
},

/**
* Expand image preview
*/
expandPreview: function (record) {
this.previewComponent().show(record);
}
});
});
31 changes: 31 additions & 0 deletions app/code/Magento/Ui/view/base/web/js/grid/columns/overlay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
define([
'Magento_Ui/js/grid/columns/column'
], function (Column) {
'use strict';

return Column.extend({
/**
* If overlay should be visible
*
* @param {Object} row
* @returns {Boolean}
*/
isVisible: function (row) {
return !!row[this.index];
},

/**
* Get overlay label
*
* @param {Object} row
* @returns {String}
*/
getLabel: function (row) {
return row[this.index];
}
});
});
Loading