Skip to content

Commit

Permalink
update Outlayer v1.4.1; add jQuery events; build v1.4.2
Browse files Browse the repository at this point in the history
README: add cdn link; more Initialize
  • Loading branch information
desandro committed Jul 2, 2015
1 parent 9553bd0 commit d0ef234
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 69 deletions.
48 changes: 37 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@

_Bin-packing layout library_

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

## Install

Download a packaged source file:
### Download

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

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

[Install with npm](https://www.npmjs.com/package/packery): `npm install packery`
Link directly to [Packery files on cdnjs](https://cdnjs.com/libraries/packery).

``` html
<script src="https://cdnjs.cloudflare.com/ajax/libs/packery/1.4.2/packery.pkgd.js"></script>
<!-- or -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/packery/1.4.2/packery.pkgd.min.js"></script>
```

### Package managers

Bower: `bower install packery --save`

[npm](https://www.npmjs.com/package/packery): `npm install packery --save`

## License

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

## Initialize

### in JavaScript
With jQuery

``` js
$('.grid').packery({
// options...
itemSelector: '.grid-item'
});
```

With vanilla JavaScript

``` js
var container = document.querySelector('#container');
var myPackery = new Packery( container, {
// vanilla JS
var grid = document.querySelector('.grid');
var pckry = new Packery( grid, {
// options...
itemSelector: '.grid-item'
});
```

### in HTML
With HTML

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

``` html
<div class="js-packery" data-packery-options='{ "itemSelector": ".item" }'>
<div class="grid js-packery"
data-packery-options='{ "itemSelector": ".grid-item" }'>
<div class="grid-item"></div>
<div class="grid-item"></div>
...
</div>
```
Expand Down
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "packery",
"version": "1.4.1",
"version": "1.4.2",
"author": "Metafizzy",
"description": "bin-packing layout library",
"main": "js/packery.js",
"dependencies": {
"classie": "1.x",
"get-style-property": "1.x",
"get-size": "1.2.x",
"outlayer": "1.4.x"
"outlayer": "~1.4.1"
},
"devDependencies": {
"draggabilly": "1.x",
Expand Down
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

### v1.4.2

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

### v1.4.1
Expand Down
144 changes: 101 additions & 43 deletions dist/packery.pkgd.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Packery PACKAGED v1.4.1
* Packery PACKAGED v1.4.2
* bin-packing layout library
*
* Licensed GPLv3 for open source use
Expand Down Expand Up @@ -1722,14 +1722,19 @@ Item.prototype.getPosition = function() {
var layoutOptions = this.layout.options;
var isOriginLeft = layoutOptions.isOriginLeft;
var isOriginTop = layoutOptions.isOriginTop;
var x = parseInt( style[ isOriginLeft ? 'left' : 'right' ], 10 );
var y = parseInt( style[ isOriginTop ? 'top' : 'bottom' ], 10 );
var xValue = style[ isOriginLeft ? 'left' : 'right' ];
var yValue = style[ isOriginTop ? 'top' : 'bottom' ];
var x = parseInt( xValue, 10 );
var y = parseInt( yValue, 10 );
// convert percent to pixels
var layoutSize = this.layout.size;
x = xValue.indexOf('%') != -1 ? ( x / 100 ) * layoutSize.width : x;
y = yValue.indexOf('%') != -1 ? ( y / 100 ) * layoutSize.height : y;

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

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

var x = this.position.x + layoutSize[ xPadding ];
// set in percentage
x = layoutOptions.percentPosition && !layoutOptions.isHorizontal ?
( ( x / layoutSize.width ) * 100 ) + '%' : x + 'px';
style[ xProperty ] = x;
// set in percentage or pixels
style[ xProperty ] = this.getXValue( x );
// reset other property
style[ xResetProperty ] = '';

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

var y = this.position.y + layoutSize[ yPadding ];
// set in percentage
y = layoutOptions.percentPosition && layoutOptions.isHorizontal ?
( ( y / layoutSize.height ) * 100 ) + '%' : y + 'px';
style[ yProperty ] = y;
// set in percentage or pixels
style[ yProperty ] = this.getYValue( y );
// reset other property
style[ yResetProperty ] = '';

this.css( style );
this.emitEvent( 'layout', [ this ] );
};

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

// transform translate function
var translate = is3d ?
function( x, y ) {
return 'translate3d(' + x + 'px, ' + y + 'px, 0)';
} :
function( x, y ) {
return 'translate(' + x + 'px, ' + y + 'px)';
};
Item.prototype.getYValue = function( y ) {
var layoutOptions = this.layout.options;
return layoutOptions.percentPosition && layoutOptions.isHorizontal ?
( ( y / this.layout.size.height ) * 100 ) + '%' : y + 'px';
};


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

this.transition({
to: transitionStyle,
Expand All @@ -1821,6 +1820,21 @@ Item.prototype._transitionTo = function( x, y ) {
});
};

Item.prototype.getTranslate = function( x, y ) {
// flip cooridinates if origin on right or bottom
var layoutOptions = this.layout.options;
x = layoutOptions.isOriginLeft ? x : -x;
y = layoutOptions.isOriginTop ? y : -y;
x = this.getXValue( x );
y = this.getYValue( y );

if ( is3d ) {
return 'translate3d(' + x + ', ' + y + ', 0)';
}

return 'translate(' + x + ', ' + y + ')';
};

// non transition + transform support
Item.prototype.goTo = function( x, y ) {
this.setPosition( x, y );
Expand Down Expand Up @@ -1900,28 +1914,36 @@ Item.prototype._transition = function( args ) {

};

var itemTransitionProperties = transformProperty && ( utils.toDashed( transformProperty ) +
',opacity' );
// dash before all cap letters, including first for
// WebkitTransform => -webkit-transform
function toDashedAll( str ) {
return str.replace( /([A-Z])/g, function( $1 ) {
return '-' + $1.toLowerCase();
});
}

var transitionProps = 'opacity,' +
toDashedAll( vendorProperties.transform || 'transform' );

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

// make transition: foo, bar, baz from style object
// TODO uncomment this bit when IE10 bug is resolved
// var transitionValue = [];
// make `transition: foo, bar, baz` from style object
// HACK un-comment this when enableTransition can work
// while a transition is happening
// var transitionValues = [];
// for ( var prop in style ) {
// // dash-ify camelCased properties like WebkitTransition
// transitionValue.push( toDash( prop ) );
// prop = vendorProperties[ prop ] || prop;
// transitionValues.push( toDashedAll( prop ) );
// }
// enable transition styles
// HACK always enable transform,opacity for IE10
this.css({
transitionProperty: itemTransitionProperties,
transitionProperty: transitionProps,
transitionDuration: this.layout.options.transitionDuration
});
// listen for transition end event
Expand Down Expand Up @@ -2124,7 +2146,7 @@ return Item;
}));

/*!
* Outlayer v1.4.0
* Outlayer v1.4.1
* the brains and guts of a layout library
* MIT license
*/
Expand Down Expand Up @@ -2539,7 +2561,7 @@ Outlayer.prototype._setContainerMeasure = function( measure, isWidth ) {
Outlayer.prototype._emitCompleteOnItems = function( eventName, items ) {
var _this = this;
function onComplete() {
_this.emitEvent( eventName + 'Complete', [ items ] );
_this.dispatchEvent( eventName + 'Complete', null, [ items ] );
}

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

/**
* emits events via eventEmitter and jQuery events
* @param {String} type - name of event
* @param {Event} event - original event
* @param {Array} args - extra arguments
*/
Outlayer.prototype.dispatchEvent = function( type, event, args ) {
// add original event to arguments
var emitArgs = event ? [ event ].concat( args ) : args;
this.emitEvent( type, emitArgs );

if ( jQuery ) {
// set this.$element
this.$element = this.$element || jQuery( this.element );
if ( event ) {
// create jQuery event
var $event = jQuery.Event( event );
$event.type = type;
this.$element.trigger( $event, args );
} else {
// just trigger with type if no event available
this.$element.trigger( type, args );
}
}
};

// -------------------------- ignore & stamps -------------------------- //


Expand Down Expand Up @@ -3532,7 +3580,7 @@ return Item;
}));

/*!
* Packery v1.4.1
* Packery v1.4.2
* bin-packing layout library
*
* Licensed GPLv3 for open source use
Expand Down Expand Up @@ -3619,13 +3667,23 @@ Packery.prototype._create = function() {
};

this.handleUIDraggable = {
start: function handleUIDraggableStart( event ) {
start: function handleUIDraggableStart( event, ui ) {
// HTML5 may trigger dragstart, dismiss HTML5 dragging
if ( !ui ) {
return;
}
_this.itemDragStart( event.currentTarget );
},
drag: function handleUIDraggableDrag( event, ui ) {
if ( !ui ) {
return;
}
_this.itemDragMove( event.currentTarget, ui.position.left, ui.position.top );
},
stop: function handleUIDraggableStop( event ) {
stop: function handleUIDraggableStop( event, ui ) {
if ( !ui ) {
return;
}
_this.itemDragEnd( event.currentTarget );
}
};
Expand Down Expand Up @@ -3851,7 +3909,7 @@ Packery.prototype._bindFitEvents = function( item ) {
if ( ticks != 2 ) {
return;
}
_this.emitEvent( 'fitComplete', [ item ] );
_this.dispatchEvent( 'fitComplete', null, [ item ] );
}
// when item is laid out
item.on( 'layout', function() {
Expand Down Expand Up @@ -3996,7 +4054,7 @@ Packery.prototype._getDragEndLayoutComplete = function( elem, item ) {

// emit item drag event now that everything is done
if ( itemNeedsPositioning ) {
_this.emitEvent( 'dragItemPositioned', [ item ] );
_this.dispatchEvent( 'dragItemPositioned', null, [ item ] );
}
// listen once
return true;
Expand Down
6 changes: 3 additions & 3 deletions dist/packery.pkgd.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions js/packery.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Packery v1.4.1
* Packery v1.4.2
* bin-packing layout library
*
* Licensed GPLv3 for open source use
Expand Down Expand Up @@ -328,7 +328,7 @@ Packery.prototype._bindFitEvents = function( item ) {
if ( ticks != 2 ) {
return;
}
_this.emitEvent( 'fitComplete', [ item ] );
_this.dispatchEvent( 'fitComplete', null, [ item ] );
}
// when item is laid out
item.on( 'layout', function() {
Expand Down Expand Up @@ -473,7 +473,7 @@ Packery.prototype._getDragEndLayoutComplete = function( elem, item ) {

// emit item drag event now that everything is done
if ( itemNeedsPositioning ) {
_this.emitEvent( 'dragItemPositioned', [ item ] );
_this.dispatchEvent( 'dragItemPositioned', null, [ item ] );
}
// listen once
return true;
Expand Down
Loading

0 comments on commit d0ef234

Please sign in to comment.