From 8c6f9dfb14d23970ef715f2f785db8ef90d8ccfe Mon Sep 17 00:00:00 2001 From: pliablepixels Date: Sun, 6 Mar 2016 16:38:03 -0500 Subject: #179, made packery drag and drop mode evident, added ability to save and restore Former-commit-id: f4335f3beab8f47d5b91af929e5053a03a2d1df2 --- www/external/packery.pkgd.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'www/external') diff --git a/www/external/packery.pkgd.js b/www/external/packery.pkgd.js index c8353909..cd5b2a2c 100644 --- a/www/external/packery.pkgd.js +++ b/www/external/packery.pkgd.js @@ -2743,6 +2743,48 @@ Packery.Item = Item; var proto = Packery.prototype; +// get JSON-friendly data for items positions +Packery.prototype.getShiftPositions = function( attrName ) { + attrName = attrName || 'id'; + var _this = this; + return this.items.map( function( item ) { + return { + attr: item.element.getAttribute( attrName ), + x: item.rect.x / _this.packer.width + } + }); +}; + +Packery.prototype.initShiftLayout = function( positions, attr ) { + if ( !positions ) { + // if no initial positions, run packery layout + this.layout(); + return; + } + // parse string to JSON + if ( typeof positions == 'string' ) { + try { + positions = JSON.parse( positions ); + } catch( error ) { + console.error( 'JSON parse error: ' + error ); + this.layout(); + return; + } + } + + attr = attr || 'id'; // default to id attribute + this._resetLayout(); + // set item order and horizontal position from saved positions + this.items = positions.map( function( itemPosition ) { + var selector = '[' + attr + '="' + itemPosition.attr + '"]' + var itemElem = this.element.querySelector( selector ); + var item = this.getItem( itemElem ); + item.rect.x = itemPosition.x * this.packer.width; + return item; + }, this ); + this.shiftLayout(); +}; + proto._create = function() { // call super Outlayer.prototype._create.call( this ); -- cgit v1.2.3