forked from magento/adobe-stock-integration
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
magento#4: Basic component implementation
- Loading branch information
Stanislav Idolov
committed
Jun 6, 2019
1 parent
636fe8c
commit 50b399d
Showing
11 changed files
with
273 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
AdobeStockImageAdminUi/view/adminhtml/web/js/components/masonry.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
define([ | ||
'uiElement', | ||
'jquery' | ||
], function (Element, $) { | ||
'use strict'; | ||
|
||
return Element.extend({ | ||
defaults: { | ||
template: 'Magento_AdobeStockImageAdminUi/masonry', | ||
imports: { | ||
rows: '${ $.provider }:data.items' | ||
}, | ||
listens: { | ||
'rows': 'initComponent' | ||
}, | ||
|
||
images: [], | ||
|
||
containerId: '', | ||
|
||
minRatio: null, | ||
containerWidth: window.innerWidth, | ||
|
||
/** | ||
* Type: Number | ||
* Description: Size in pixels of the gap between images in the grid. | ||
*/ | ||
imageMargin: 20, | ||
|
||
containerStyles: {}, | ||
}, | ||
|
||
initObservable: function () { | ||
this._super() | ||
.observe([ | ||
'rows', | ||
'images', | ||
'containerStyles' | ||
]); | ||
|
||
return this; | ||
}, | ||
|
||
/** | ||
* @param rows | ||
* @return {exports} | ||
*/ | ||
initComponent: function (rows) { | ||
if (!rows.length) { | ||
return; | ||
} | ||
|
||
this.imageMargin = parseInt(this.imageMargin); | ||
this.container = $('[data-id="' + this.containerId + '"]')[0]; | ||
|
||
this.prepareImages(rows); | ||
this.setLayoutStyles(); | ||
this.setContainerHeight(); | ||
this.setEventListener(); | ||
return this; | ||
}, | ||
|
||
prepareImages: function (rows) { | ||
this.images(rows.map( (asset) => { | ||
return { | ||
src: asset.url, | ||
ratio: (asset.width / asset.height).toFixed(2), | ||
id: asset.id | ||
}; | ||
})); | ||
}, | ||
|
||
setEventListener: function () { | ||
var running = false, | ||
handler = function() { | ||
this.containerWidth = window.innerWidth; | ||
this.setLayoutStyles(); | ||
this.setContainerHeight(); | ||
}.bind(this); | ||
|
||
window.addEventListener('resize', function () { | ||
if (!running) { | ||
running = true; | ||
if (window.requestAnimationFrame) { | ||
window.requestAnimationFrame(function () { | ||
handler(); | ||
running = false; | ||
}); | ||
} else { | ||
setTimeout(function () { | ||
handler(); | ||
running = false; | ||
}, 66); | ||
} | ||
} | ||
}); | ||
}, | ||
|
||
setContainerHeight: function() { | ||
var styles = this.containerStyles(); | ||
|
||
styles['height'] = this.totalHeight + 'px'; | ||
this.containerStyles(styles); | ||
}, | ||
|
||
setLayoutStyles: function() { | ||
var containerWidth = parseInt(this.container.clientWidth), | ||
row = [], | ||
translateX = 0, | ||
translateY = 0, | ||
ratio = 0, | ||
imageWidth = 0, | ||
rowHeight = 0; | ||
|
||
this.setMinRatio(); | ||
|
||
this.images().forEach(function(image, index) { | ||
ratio += parseFloat(image.ratio); | ||
row.push(image); | ||
|
||
if (ratio >= this.minRatio || index + 1 === this.images().length) { | ||
ratio = Math.max(ratio, this.minRatio); | ||
rowHeight = (containerWidth - this.imageMargin * (row.length - 1)) / ratio; | ||
|
||
row.forEach(function(img) { | ||
imageWidth = rowHeight * img.ratio; | ||
this.setImageStyles(img.id, imageWidth, rowHeight, translateX, translateY); | ||
translateX += imageWidth + this.imageMargin; | ||
}.bind(this)); | ||
|
||
row = []; | ||
ratio = 0; | ||
translateY += parseInt(rowHeight) + this.imageMargin; | ||
translateX = 0; | ||
} | ||
}.bind(this)); | ||
this.totalHeight = translateY - this.imageMargin; | ||
}, | ||
|
||
setImageStyles: function (imageId, imageWidth, rowHeight, translateX, translateY) { | ||
$('[data-id="' + imageId + '"]') | ||
.css('width', parseInt(imageWidth) + 'px') | ||
.css('height', parseInt(rowHeight) + 'px') | ||
.css('transform', ('translate3d(' + translateX + 'px,' + translateY + 'px, 0)')); | ||
}, | ||
|
||
setMinRatio: function() { | ||
if (this.containerWidth <= 640) { | ||
this.minRatio = 2; | ||
} else if (this.containerWidth <= 1280) { | ||
this.minRatio = 4; | ||
} else if (this.containerWidth <= 1920) { | ||
this.minRatio = 5; | ||
} else { | ||
this.minRatio = 6; | ||
} | ||
} | ||
}); | ||
}); |
9 changes: 0 additions & 9 deletions
9
AdobeStockImageAdminUi/view/adminhtml/web/template/grid/cells/image.html
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
AdobeStockImageAdminUi/view/adminhtml/web/template/masonry.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<div data-role="grid-wrapper" ko-style="containerStyles" attr="'data-id': containerId"> | ||
<div class="masonry-image-column"> | ||
<!-- ko foreach: { data: images, as: 'row' } --> | ||
<div class="masonry-image-block" ko-style="row.styles" attr="'data-id': row.id"> | ||
<img attr="src: row.src"> | ||
</div> | ||
<!-- /ko --> | ||
</div> | ||
</div> |