Skip to content

Commit d0ef234

Browse files
committed
update Outlayer v1.4.1; add jQuery events; build v1.4.2
README: add cdn link; more Initialize
1 parent 9553bd0 commit d0ef234

9 files changed

+159
-69
lines changed

README.md

+37-11
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,30 @@
22

33
_Bin-packing layout library_
44

5-
See http://packery.metafizzy.co for complete docs and demos
5+
See [packery.metafizzy.co](http://packery.metafizzy.co) for complete docs and demos
66

77
## Install
88

9-
Download a packaged source file:
9+
### Download
1010

11-
+ [packery.pkgd.js](http://packeryjs.com/packery.pkgd.js)
12-
+ [packery.pkgd.min.js](http://packeryjs.com/packery.pkgd.min.js)
11+
+ [packery.pkgd.js](https://github.com/metafizzy/packery/raw/master/dist/packery.pkgd.js) un-minified, or
12+
+ [packery.pkgd.min.js](https://github.com/metafizzy/packery/raw/master/dist/packery.pkgd.min.js) minified
1313

14-
Install with [Bower :bird:](http://bower.io): `bower install packery --save`
14+
### CDN
1515

16-
[Install with npm](https://www.npmjs.com/package/packery): `npm install packery`
16+
Link directly to [Packery files on cdnjs](https://cdnjs.com/libraries/packery).
17+
18+
``` html
19+
<script src="https://cdnjs.cloudflare.com/ajax/libs/packery/1.4.2/packery.pkgd.js"></script>
20+
<!-- or -->
21+
<script src="https://cdnjs.cloudflare.com/ajax/libs/packery/1.4.2/packery.pkgd.min.js"></script>
22+
```
23+
24+
### Package managers
25+
26+
Bower: `bower install packery --save`
27+
28+
[npm](https://www.npmjs.com/package/packery): `npm install packery --save`
1729

1830
## License
1931

@@ -29,21 +41,35 @@ If you are creating an open source application under a license compatible with t
2941

3042
## Initialize
3143

32-
### in JavaScript
44+
With jQuery
45+
46+
``` js
47+
$('.grid').packery({
48+
// options...
49+
itemSelector: '.grid-item'
50+
});
51+
```
52+
53+
With vanilla JavaScript
3354

3455
``` js
35-
var container = document.querySelector('#container');
36-
var myPackery = new Packery( container, {
56+
// vanilla JS
57+
var grid = document.querySelector('.grid');
58+
var pckry = new Packery( grid, {
3759
// options...
60+
itemSelector: '.grid-item'
3861
});
3962
```
4063

41-
### in HTML
64+
With HTML
4265

4366
Add a class of `js-packery` to your element. Options can be set in JSON in `data-packery-options`.
4467

4568
``` html
46-
<div class="js-packery" data-packery-options='{ "itemSelector": ".item" }'>
69+
<div class="grid js-packery"
70+
data-packery-options='{ "itemSelector": ".grid-item" }'>
71+
<div class="grid-item"></div>
72+
<div class="grid-item"></div>
4773
...
4874
</div>
4975
```

bower.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "packery",
3-
"version": "1.4.1",
3+
"version": "1.4.2",
44
"author": "Metafizzy",
55
"description": "bin-packing layout library",
66
"main": "js/packery.js",
77
"dependencies": {
88
"classie": "1.x",
99
"get-style-property": "1.x",
1010
"get-size": "1.2.x",
11-
"outlayer": "1.4.x"
11+
"outlayer": "~1.4.1"
1212
},
1313
"devDependencies": {
1414
"draggabilly": "1.x",

changelog.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
### v1.4.2
4+
5+
+ Updated Outlayer to v1.4.1.
6+
- Added jQuery event triggering.
7+
- Safari layout and transition fixes
38
+ Fixed bug with HTML5 drag & drop. Fixed [#206](https://github.com/metafizzy/packery/issues/206)
49

510
### v1.4.1

dist/packery.pkgd.js

+101-43
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Packery PACKAGED v1.4.1
2+
* Packery PACKAGED v1.4.2
33
* bin-packing layout library
44
*
55
* Licensed GPLv3 for open source use
@@ -1722,14 +1722,19 @@ Item.prototype.getPosition = function() {
17221722
var layoutOptions = this.layout.options;
17231723
var isOriginLeft = layoutOptions.isOriginLeft;
17241724
var isOriginTop = layoutOptions.isOriginTop;
1725-
var x = parseInt( style[ isOriginLeft ? 'left' : 'right' ], 10 );
1726-
var y = parseInt( style[ isOriginTop ? 'top' : 'bottom' ], 10 );
1725+
var xValue = style[ isOriginLeft ? 'left' : 'right' ];
1726+
var yValue = style[ isOriginTop ? 'top' : 'bottom' ];
1727+
var x = parseInt( xValue, 10 );
1728+
var y = parseInt( yValue, 10 );
1729+
// convert percent to pixels
1730+
var layoutSize = this.layout.size;
1731+
x = xValue.indexOf('%') != -1 ? ( x / 100 ) * layoutSize.width : x;
1732+
y = yValue.indexOf('%') != -1 ? ( y / 100 ) * layoutSize.height : y;
17271733

17281734
// clean up 'auto' or other non-integer values
17291735
x = isNaN( x ) ? 0 : x;
17301736
y = isNaN( y ) ? 0 : y;
17311737
// remove padding from measurement
1732-
var layoutSize = this.layout.size;
17331738
x -= isOriginLeft ? layoutSize.paddingLeft : layoutSize.paddingRight;
17341739
y -= isOriginTop ? layoutSize.paddingTop : layoutSize.paddingBottom;
17351740

@@ -1749,10 +1754,8 @@ Item.prototype.layoutPosition = function() {
17491754
var xResetProperty = layoutOptions.isOriginLeft ? 'right' : 'left';
17501755

17511756
var x = this.position.x + layoutSize[ xPadding ];
1752-
// set in percentage
1753-
x = layoutOptions.percentPosition && !layoutOptions.isHorizontal ?
1754-
( ( x / layoutSize.width ) * 100 ) + '%' : x + 'px';
1755-
style[ xProperty ] = x;
1757+
// set in percentage or pixels
1758+
style[ xProperty ] = this.getXValue( x );
17561759
// reset other property
17571760
style[ xResetProperty ] = '';
17581761

@@ -1762,26 +1765,26 @@ Item.prototype.layoutPosition = function() {
17621765
var yResetProperty = layoutOptions.isOriginTop ? 'bottom' : 'top';
17631766

17641767
var y = this.position.y + layoutSize[ yPadding ];
1765-
// set in percentage
1766-
y = layoutOptions.percentPosition && layoutOptions.isHorizontal ?
1767-
( ( y / layoutSize.height ) * 100 ) + '%' : y + 'px';
1768-
style[ yProperty ] = y;
1768+
// set in percentage or pixels
1769+
style[ yProperty ] = this.getYValue( y );
17691770
// reset other property
17701771
style[ yResetProperty ] = '';
17711772

17721773
this.css( style );
17731774
this.emitEvent( 'layout', [ this ] );
17741775
};
17751776

1777+
Item.prototype.getXValue = function( x ) {
1778+
var layoutOptions = this.layout.options;
1779+
return layoutOptions.percentPosition && !layoutOptions.isHorizontal ?
1780+
( ( x / this.layout.size.width ) * 100 ) + '%' : x + 'px';
1781+
};
17761782

1777-
// transform translate function
1778-
var translate = is3d ?
1779-
function( x, y ) {
1780-
return 'translate3d(' + x + 'px, ' + y + 'px, 0)';
1781-
} :
1782-
function( x, y ) {
1783-
return 'translate(' + x + 'px, ' + y + 'px)';
1784-
};
1783+
Item.prototype.getYValue = function( y ) {
1784+
var layoutOptions = this.layout.options;
1785+
return layoutOptions.percentPosition && layoutOptions.isHorizontal ?
1786+
( ( y / this.layout.size.height ) * 100 ) + '%' : y + 'px';
1787+
};
17851788

17861789

17871790
Item.prototype._transitionTo = function( x, y ) {
@@ -1806,11 +1809,7 @@ Item.prototype._transitionTo = function( x, y ) {
18061809
var transX = x - curX;
18071810
var transY = y - curY;
18081811
var transitionStyle = {};
1809-
// flip cooridinates if origin on right or bottom
1810-
var layoutOptions = this.layout.options;
1811-
transX = layoutOptions.isOriginLeft ? transX : -transX;
1812-
transY = layoutOptions.isOriginTop ? transY : -transY;
1813-
transitionStyle.transform = translate( transX, transY );
1812+
transitionStyle.transform = this.getTranslate( transX, transY );
18141813

18151814
this.transition({
18161815
to: transitionStyle,
@@ -1821,6 +1820,21 @@ Item.prototype._transitionTo = function( x, y ) {
18211820
});
18221821
};
18231822

1823+
Item.prototype.getTranslate = function( x, y ) {
1824+
// flip cooridinates if origin on right or bottom
1825+
var layoutOptions = this.layout.options;
1826+
x = layoutOptions.isOriginLeft ? x : -x;
1827+
y = layoutOptions.isOriginTop ? y : -y;
1828+
x = this.getXValue( x );
1829+
y = this.getYValue( y );
1830+
1831+
if ( is3d ) {
1832+
return 'translate3d(' + x + ', ' + y + ', 0)';
1833+
}
1834+
1835+
return 'translate(' + x + ', ' + y + ')';
1836+
};
1837+
18241838
// non transition + transform support
18251839
Item.prototype.goTo = function( x, y ) {
18261840
this.setPosition( x, y );
@@ -1900,28 +1914,36 @@ Item.prototype._transition = function( args ) {
19001914

19011915
};
19021916

1903-
var itemTransitionProperties = transformProperty && ( utils.toDashed( transformProperty ) +
1904-
',opacity' );
1917+
// dash before all cap letters, including first for
1918+
// WebkitTransform => -webkit-transform
1919+
function toDashedAll( str ) {
1920+
return str.replace( /([A-Z])/g, function( $1 ) {
1921+
return '-' + $1.toLowerCase();
1922+
});
1923+
}
1924+
1925+
var transitionProps = 'opacity,' +
1926+
toDashedAll( vendorProperties.transform || 'transform' );
19051927

19061928
Item.prototype.enableTransition = function(/* style */) {
1907-
// only enable if not already transitioning
1908-
// bug in IE10 were re-setting transition style will prevent
1909-
// transitionend event from triggering
1929+
// HACK changing transitionProperty during a transition
1930+
// will cause transition to jump
19101931
if ( this.isTransitioning ) {
19111932
return;
19121933
}
19131934

1914-
// make transition: foo, bar, baz from style object
1915-
// TODO uncomment this bit when IE10 bug is resolved
1916-
// var transitionValue = [];
1935+
// make `transition: foo, bar, baz` from style object
1936+
// HACK un-comment this when enableTransition can work
1937+
// while a transition is happening
1938+
// var transitionValues = [];
19171939
// for ( var prop in style ) {
19181940
// // dash-ify camelCased properties like WebkitTransition
1919-
// transitionValue.push( toDash( prop ) );
1941+
// prop = vendorProperties[ prop ] || prop;
1942+
// transitionValues.push( toDashedAll( prop ) );
19201943
// }
19211944
// enable transition styles
1922-
// HACK always enable transform,opacity for IE10
19231945
this.css({
1924-
transitionProperty: itemTransitionProperties,
1946+
transitionProperty: transitionProps,
19251947
transitionDuration: this.layout.options.transitionDuration
19261948
});
19271949
// listen for transition end event
@@ -2124,7 +2146,7 @@ return Item;
21242146
}));
21252147

21262148
/*!
2127-
* Outlayer v1.4.0
2149+
* Outlayer v1.4.1
21282150
* the brains and guts of a layout library
21292151
* MIT license
21302152
*/
@@ -2539,7 +2561,7 @@ Outlayer.prototype._setContainerMeasure = function( measure, isWidth ) {
25392561
Outlayer.prototype._emitCompleteOnItems = function( eventName, items ) {
25402562
var _this = this;
25412563
function onComplete() {
2542-
_this.emitEvent( eventName + 'Complete', [ items ] );
2564+
_this.dispatchEvent( eventName + 'Complete', null, [ items ] );
25432565
}
25442566

25452567
var count = items.length;
@@ -2563,6 +2585,32 @@ Outlayer.prototype._emitCompleteOnItems = function( eventName, items ) {
25632585
}
25642586
};
25652587

2588+
/**
2589+
* emits events via eventEmitter and jQuery events
2590+
* @param {String} type - name of event
2591+
* @param {Event} event - original event
2592+
* @param {Array} args - extra arguments
2593+
*/
2594+
Outlayer.prototype.dispatchEvent = function( type, event, args ) {
2595+
// add original event to arguments
2596+
var emitArgs = event ? [ event ].concat( args ) : args;
2597+
this.emitEvent( type, emitArgs );
2598+
2599+
if ( jQuery ) {
2600+
// set this.$element
2601+
this.$element = this.$element || jQuery( this.element );
2602+
if ( event ) {
2603+
// create jQuery event
2604+
var $event = jQuery.Event( event );
2605+
$event.type = type;
2606+
this.$element.trigger( $event, args );
2607+
} else {
2608+
// just trigger with type if no event available
2609+
this.$element.trigger( type, args );
2610+
}
2611+
}
2612+
};
2613+
25662614
// -------------------------- ignore & stamps -------------------------- //
25672615

25682616

@@ -3532,7 +3580,7 @@ return Item;
35323580
}));
35333581

35343582
/*!
3535-
* Packery v1.4.1
3583+
* Packery v1.4.2
35363584
* bin-packing layout library
35373585
*
35383586
* Licensed GPLv3 for open source use
@@ -3619,13 +3667,23 @@ Packery.prototype._create = function() {
36193667
};
36203668

36213669
this.handleUIDraggable = {
3622-
start: function handleUIDraggableStart( event ) {
3670+
start: function handleUIDraggableStart( event, ui ) {
3671+
// HTML5 may trigger dragstart, dismiss HTML5 dragging
3672+
if ( !ui ) {
3673+
return;
3674+
}
36233675
_this.itemDragStart( event.currentTarget );
36243676
},
36253677
drag: function handleUIDraggableDrag( event, ui ) {
3678+
if ( !ui ) {
3679+
return;
3680+
}
36263681
_this.itemDragMove( event.currentTarget, ui.position.left, ui.position.top );
36273682
},
3628-
stop: function handleUIDraggableStop( event ) {
3683+
stop: function handleUIDraggableStop( event, ui ) {
3684+
if ( !ui ) {
3685+
return;
3686+
}
36293687
_this.itemDragEnd( event.currentTarget );
36303688
}
36313689
};
@@ -3851,7 +3909,7 @@ Packery.prototype._bindFitEvents = function( item ) {
38513909
if ( ticks != 2 ) {
38523910
return;
38533911
}
3854-
_this.emitEvent( 'fitComplete', [ item ] );
3912+
_this.dispatchEvent( 'fitComplete', null, [ item ] );
38553913
}
38563914
// when item is laid out
38573915
item.on( 'layout', function() {
@@ -3996,7 +4054,7 @@ Packery.prototype._getDragEndLayoutComplete = function( elem, item ) {
39964054

39974055
// emit item drag event now that everything is done
39984056
if ( itemNeedsPositioning ) {
3999-
_this.emitEvent( 'dragItemPositioned', [ item ] );
4057+
_this.dispatchEvent( 'dragItemPositioned', null, [ item ] );
40004058
}
40014059
// listen once
40024060
return true;

dist/packery.pkgd.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/packery.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Packery v1.4.1
2+
* Packery v1.4.2
33
* bin-packing layout library
44
*
55
* Licensed GPLv3 for open source use
@@ -328,7 +328,7 @@ Packery.prototype._bindFitEvents = function( item ) {
328328
if ( ticks != 2 ) {
329329
return;
330330
}
331-
_this.emitEvent( 'fitComplete', [ item ] );
331+
_this.dispatchEvent( 'fitComplete', null, [ item ] );
332332
}
333333
// when item is laid out
334334
item.on( 'layout', function() {
@@ -473,7 +473,7 @@ Packery.prototype._getDragEndLayoutComplete = function( elem, item ) {
473473

474474
// emit item drag event now that everything is done
475475
if ( itemNeedsPositioning ) {
476-
_this.emitEvent( 'dragItemPositioned', [ item ] );
476+
_this.dispatchEvent( 'dragItemPositioned', null, [ item ] );
477477
}
478478
// listen once
479479
return true;

0 commit comments

Comments
 (0)