release candidate

This commit is contained in:
Bachir Soussi Chiadmi
2017-12-30 17:48:05 +01:00
parent 13bd34d2b3
commit 3983d1fdaf
232 changed files with 24427 additions and 11080 deletions
+13 -17
View File
@@ -1,26 +1,21 @@
{
"name": "outlayer",
"version": "1.4.2",
"description": "the brains and guts of a layout library",
"main": "outlayer.js",
"dependencies": {
"doc-ready": "1.0.x",
"eventEmitter": ">=4.2 <5",
"eventie": "~1.0.3",
"get-size": "~1.2.2",
"get-style-property": "~1.0.4",
"matches-selector": "~1.0.2",
"fizzy-ui-utils": "~1.0.1"
"ev-emitter": "^1.0.0",
"get-size": "^2.0.2",
"fizzy-ui-utils": "^2.0.0"
},
"devDependencies": {
"jquery-bridget": "1.x",
"jquery": ">=1.4.3 <2",
"qunit": "^1.12.0"
"jquery-bridget": "2.x",
"jquery": ">=1.4.3 <4",
"qunit": "^1.17.0"
},
"ignore": [
"test/",
"docs/",
"examples",
"sandbox/",
".*",
"notes.md",
"**/.*",
@@ -45,13 +40,14 @@
"isotope"
],
"license": "MIT",
"_release": "1.4.2",
"version": "2.1.1",
"_release": "2.1.1",
"_resolution": {
"type": "version",
"tag": "v1.4.2",
"commit": "c2d5e67fa6b4e716591ac11fa2d27494bdfc1733"
"tag": "v2.1.1",
"commit": "07ca54fd55ff232aec814768716a5d245a450c41"
},
"_source": "git://github.com/metafizzy/outlayer.git",
"_target": "~1.4.0",
"_source": "https://github.com/metafizzy/outlayer.git",
"_target": "^2.1.0",
"_originalSource": "outlayer"
}
+9 -13
View File
@@ -7,7 +7,7 @@ Outlayer is a base layout class for layout libraries like [Isotope](http://isoto
Outlayer layouts work with a container element and children item elements.
``` html
<div class="container">
<div class="grid">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
@@ -39,7 +39,7 @@ Create a new layout class. `namespace` is used for jQuery plugin, and for declar
The `Layout` inherits from [`Outlayer.prototype`](docs/outlayer.md).
```
var elem = document.querySelector('#selector');
var elem = document.querySelector('.selector');
var msnry = new Masonry( elem, {
// set options...
columnWidth: 200
@@ -52,24 +52,23 @@ Layouts work with Items, accessible as `Layout.Item`. See [Item API](docs/item.m
## Declarative
An Outlayer layout class can be initialized via HTML, by setting a class of `.js-namespace` on the element. Options can be set via a `data-namespace-options` attribution. For example:
An Outlayer layout class can be initialized via HTML, by setting an attribute of `data-namespace` on the element. Options are set in JSON. For example:
``` html
<!-- var Masonry = Outlayer.create('masonry') -->
<div id="container" class="js-masonry"
data-masonry-options='{ "itemSelector": ".item", "columnWidth": 200 }'>
<div class="grid" data-masonry='{ "itemSelector": ".item", "columnWidth": 200 }'>
...
</div>
```
The declarative attributes and class will be dashed. i.e. `Outlayer.create('myNiceLayout')` will use `js-my-nice-layout` the class and `data-my-nice-layout-options` as the options attribute.
The declarative attributes and class will be dashed. i.e. `Outlayer.create('myNiceLayout')` will use `data-my-nice-layout` as the attribute.
## .data()
Get a layout instance from an element.
```
var myMasonry = Masonry.data( document.querySelector('#container') );
var myMasonry = Masonry.data( document.querySelector('.grid') );
```
## jQuery plugin
@@ -82,11 +81,11 @@ var Masonry = Outlayer.create('masonry');
// rock some jQuery
$( function() {
// .masonry() to initialize
var $container = $('#container').masonry({
var $grid = $('.grid').masonry({
// options...
});
// methods are available by passing a string as first parameter
$container.masonry( 'reveal', elems );
$grid.masonry( 'reveal', elems );
});
```
@@ -114,10 +113,7 @@ Or set a path config for all Outlayer dependencies.
``` js
requirejs.config({
paths: {
eventie: 'bower_components/eventie',
'doc-ready': 'bower_components/doc-ready',
eventEmitter: 'bower_components/eventEmitter',
'get-style-property': 'bower_components/get-style-property',
'ev-emitter': 'bower_components/ev-emitter',
'get-size': 'bower_components/get-size',
'matches-selector': 'bower_components/matches-selector'
}
+7 -12
View File
@@ -1,26 +1,21 @@
{
"name": "outlayer",
"version": "1.4.2",
"description": "the brains and guts of a layout library",
"main": "outlayer.js",
"dependencies": {
"doc-ready": "1.0.x",
"eventEmitter": ">=4.2 <5",
"eventie": "~1.0.3",
"get-size": "~1.2.2",
"get-style-property": "~1.0.4",
"matches-selector": "~1.0.2",
"fizzy-ui-utils": "~1.0.1"
"ev-emitter": "^1.0.0",
"get-size": "^2.0.2",
"fizzy-ui-utils": "^2.0.0"
},
"devDependencies": {
"jquery-bridget": "1.x",
"jquery": ">=1.4.3 <2",
"qunit": "^1.12.0"
"jquery-bridget": "2.x",
"jquery": ">=1.4.3 <4",
"qunit": "^1.17.0"
},
"ignore": [
"test/",
"docs/",
"examples",
"sandbox/",
".*",
"notes.md",
"**/.*",
+109 -139
View File
@@ -3,56 +3,36 @@
*/
( function( window, factory ) {
'use strict';
// universal module definition
if ( typeof define === 'function' && define.amd ) {
// AMD
/* jshint strict: false */ /* globals define, module, require */
if ( typeof define == 'function' && define.amd ) {
// AMD - RequireJS
define( [
'eventEmitter/EventEmitter',
'get-size/get-size',
'get-style-property/get-style-property',
'fizzy-ui-utils/utils'
'ev-emitter/ev-emitter',
'get-size/get-size'
],
function( EventEmitter, getSize, getStyleProperty, utils ) {
return factory( window, EventEmitter, getSize, getStyleProperty, utils );
}
factory
);
} else if (typeof exports === 'object') {
// CommonJS
} else if ( typeof module == 'object' && module.exports ) {
// CommonJS - Browserify, Webpack
module.exports = factory(
window,
require('wolfy87-eventemitter'),
require('get-size'),
require('desandro-get-style-property'),
require('fizzy-ui-utils')
require('ev-emitter'),
require('get-size')
);
} else {
// browser global
window.Outlayer = {};
window.Outlayer.Item = factory(
window,
window.EventEmitter,
window.getSize,
window.getStyleProperty,
window.fizzyUIUtils
window.EvEmitter,
window.getSize
);
}
}( window, function factory( window, EventEmitter, getSize, getStyleProperty, utils ) {
}( window, function factory( EvEmitter, getSize ) {
'use strict';
// ----- helpers ----- //
var getComputedStyle = window.getComputedStyle;
var getStyle = getComputedStyle ?
function( elem ) {
return getComputedStyle( elem, null );
} :
function( elem ) {
return elem.currentStyle;
};
function isEmptyObj( obj ) {
for ( var prop in obj ) {
return false;
@@ -63,38 +43,27 @@ function isEmptyObj( obj ) {
// -------------------------- CSS3 support -------------------------- //
var transitionProperty = getStyleProperty('transition');
var transformProperty = getStyleProperty('transform');
var supportsCSS3 = transitionProperty && transformProperty;
var is3d = !!getStyleProperty('perspective');
var docElemStyle = document.documentElement.style;
var transitionProperty = typeof docElemStyle.transition == 'string' ?
'transition' : 'WebkitTransition';
var transformProperty = typeof docElemStyle.transform == 'string' ?
'transform' : 'WebkitTransform';
var transitionEndEvent = {
WebkitTransition: 'webkitTransitionEnd',
MozTransition: 'transitionend',
OTransition: 'otransitionend',
transition: 'transitionend'
}[ transitionProperty ];
// properties that could have vendor prefix
var prefixableProperties = [
'transform',
'transition',
'transitionDuration',
'transitionProperty'
];
// cache all vendor properties
var vendorProperties = ( function() {
var cache = {};
for ( var i=0, len = prefixableProperties.length; i < len; i++ ) {
var prop = prefixableProperties[i];
var supportedProp = getStyleProperty( prop );
if ( supportedProp && supportedProp !== prop ) {
cache[ prop ] = supportedProp;
}
}
return cache;
})();
// cache all vendor properties that could have vendor prefix
var vendorProperties = {
transform: transformProperty,
transition: transitionProperty,
transitionDuration: transitionProperty + 'Duration',
transitionProperty: transitionProperty + 'Property',
transitionDelay: transitionProperty + 'Delay'
};
// -------------------------- Item -------------------------- //
@@ -114,10 +83,11 @@ function Item( element, layout ) {
this._create();
}
// inherit EventEmitter
utils.extend( Item.prototype, EventEmitter.prototype );
// inherit EvEmitter
var proto = Item.prototype = Object.create( EvEmitter.prototype );
proto.constructor = Item;
Item.prototype._create = function() {
proto._create = function() {
// transition objects
this._transn = {
ingProperties: {},
@@ -131,14 +101,14 @@ Item.prototype._create = function() {
};
// trigger specified handler for event type
Item.prototype.handleEvent = function( event ) {
proto.handleEvent = function( event ) {
var method = 'on' + event.type;
if ( this[ method ] ) {
this[ method ]( event );
}
};
Item.prototype.getSize = function() {
proto.getSize = function() {
this.size = getSize( this.element );
};
@@ -146,7 +116,7 @@ Item.prototype.getSize = function() {
* apply CSS styles to element
* @param {Object} style
*/
Item.prototype.css = function( style ) {
proto.css = function( style ) {
var elemStyle = this.element.style;
for ( var prop in style ) {
@@ -157,20 +127,22 @@ Item.prototype.css = function( style ) {
};
// measure position, and sets it
Item.prototype.getPosition = function() {
var style = getStyle( this.element );
var layoutOptions = this.layout.options;
var isOriginLeft = layoutOptions.isOriginLeft;
var isOriginTop = layoutOptions.isOriginTop;
proto.getPosition = function() {
var style = getComputedStyle( this.element );
var isOriginLeft = this.layout._getOption('originLeft');
var isOriginTop = this.layout._getOption('originTop');
var xValue = style[ isOriginLeft ? 'left' : 'right' ];
var yValue = style[ isOriginTop ? 'top' : 'bottom' ];
var x = parseFloat( xValue );
var y = parseFloat( yValue );
// convert percent to pixels
var layoutSize = this.layout.size;
var x = xValue.indexOf('%') != -1 ?
( parseFloat( xValue ) / 100 ) * layoutSize.width : parseInt( xValue, 10 );
var y = yValue.indexOf('%') != -1 ?
( parseFloat( yValue ) / 100 ) * layoutSize.height : parseInt( yValue, 10 );
if ( xValue.indexOf('%') != -1 ) {
x = ( x / 100 ) * layoutSize.width;
}
if ( yValue.indexOf('%') != -1 ) {
y = ( y / 100 ) * layoutSize.height;
}
// clean up 'auto' or other non-integer values
x = isNaN( x ) ? 0 : x;
y = isNaN( y ) ? 0 : y;
@@ -183,15 +155,16 @@ Item.prototype.getPosition = function() {
};
// set settled position, apply padding
Item.prototype.layoutPosition = function() {
proto.layoutPosition = function() {
var layoutSize = this.layout.size;
var layoutOptions = this.layout.options;
var style = {};
var isOriginLeft = this.layout._getOption('originLeft');
var isOriginTop = this.layout._getOption('originTop');
// x
var xPadding = layoutOptions.isOriginLeft ? 'paddingLeft' : 'paddingRight';
var xProperty = layoutOptions.isOriginLeft ? 'left' : 'right';
var xResetProperty = layoutOptions.isOriginLeft ? 'right' : 'left';
var xPadding = isOriginLeft ? 'paddingLeft' : 'paddingRight';
var xProperty = isOriginLeft ? 'left' : 'right';
var xResetProperty = isOriginLeft ? 'right' : 'left';
var x = this.position.x + layoutSize[ xPadding ];
// set in percentage or pixels
@@ -200,9 +173,9 @@ Item.prototype.layoutPosition = function() {
style[ xResetProperty ] = '';
// y
var yPadding = layoutOptions.isOriginTop ? 'paddingTop' : 'paddingBottom';
var yProperty = layoutOptions.isOriginTop ? 'top' : 'bottom';
var yResetProperty = layoutOptions.isOriginTop ? 'bottom' : 'top';
var yPadding = isOriginTop ? 'paddingTop' : 'paddingBottom';
var yProperty = isOriginTop ? 'top' : 'bottom';
var yResetProperty = isOriginTop ? 'bottom' : 'top';
var y = this.position.y + layoutSize[ yPadding ];
// set in percentage or pixels
@@ -214,28 +187,25 @@ Item.prototype.layoutPosition = function() {
this.emitEvent( 'layout', [ this ] );
};
Item.prototype.getXValue = function( x ) {
var layoutOptions = this.layout.options;
return layoutOptions.percentPosition && !layoutOptions.isHorizontal ?
proto.getXValue = function( x ) {
var isHorizontal = this.layout._getOption('horizontal');
return this.layout.options.percentPosition && !isHorizontal ?
( ( x / this.layout.size.width ) * 100 ) + '%' : x + 'px';
};
Item.prototype.getYValue = function( y ) {
var layoutOptions = this.layout.options;
return layoutOptions.percentPosition && layoutOptions.isHorizontal ?
proto.getYValue = function( y ) {
var isHorizontal = this.layout._getOption('horizontal');
return this.layout.options.percentPosition && isHorizontal ?
( ( y / this.layout.size.height ) * 100 ) + '%' : y + 'px';
};
Item.prototype._transitionTo = function( x, y ) {
proto._transitionTo = function( x, y ) {
this.getPosition();
// get current x & y from top/left
var curX = this.position.x;
var curY = this.position.y;
var compareX = parseInt( x, 10 );
var compareY = parseInt( y, 10 );
var didNotMove = compareX === this.position.x && compareY === this.position.y;
var didNotMove = x == this.position.x && y == this.position.y;
// save end position
this.setPosition( x, y );
@@ -260,32 +230,26 @@ Item.prototype._transitionTo = function( x, y ) {
});
};
Item.prototype.getTranslate = function( x, y ) {
proto.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;
if ( is3d ) {
return 'translate3d(' + x + 'px, ' + y + 'px, 0)';
}
return 'translate(' + x + 'px, ' + y + 'px)';
var isOriginLeft = this.layout._getOption('originLeft');
var isOriginTop = this.layout._getOption('originTop');
x = isOriginLeft ? x : -x;
y = isOriginTop ? y : -y;
return 'translate3d(' + x + 'px, ' + y + 'px, 0)';
};
// non transition + transform support
Item.prototype.goTo = function( x, y ) {
proto.goTo = function( x, y ) {
this.setPosition( x, y );
this.layoutPosition();
};
// use transition and transforms if supported
Item.prototype.moveTo = supportsCSS3 ?
Item.prototype._transitionTo : Item.prototype.goTo;
proto.moveTo = proto._transitionTo;
Item.prototype.setPosition = function( x, y ) {
this.position.x = parseInt( x, 10 );
this.position.y = parseInt( y, 10 );
proto.setPosition = function( x, y ) {
this.position.x = parseFloat( x );
this.position.y = parseFloat( y );
};
// ----- transition ----- //
@@ -296,7 +260,7 @@ Item.prototype.setPosition = function( x, y ) {
*/
// non transition, just trigger callback
Item.prototype._nonTransition = function( args ) {
proto._nonTransition = function( args ) {
this.css( args.to );
if ( args.isCleaning ) {
this._removeStyles( args.to );
@@ -314,7 +278,7 @@ Item.prototype._nonTransition = function( args ) {
* @param {Boolean} isCleaning - removes transition styles after transition
* @param {Function} onTransitionEnd - callback
*/
Item.prototype._transition = function( args ) {
proto.transition = function( args ) {
// redirect to nonTransition if no transition duration
if ( !parseFloat( this.layout.options.transitionDuration ) ) {
this._nonTransition( args );
@@ -360,10 +324,9 @@ function toDashedAll( str ) {
});
}
var transitionProps = 'opacity,' +
toDashedAll( vendorProperties.transform || 'transform' );
var transitionProps = 'opacity,' + toDashedAll( transformProperty );
Item.prototype.enableTransition = function(/* style */) {
proto.enableTransition = function(/* style */) {
// HACK changing transitionProperty during a transition
// will cause transition to jump
if ( this.isTransitioning ) {
@@ -379,35 +342,35 @@ Item.prototype.enableTransition = function(/* style */) {
// prop = vendorProperties[ prop ] || prop;
// transitionValues.push( toDashedAll( prop ) );
// }
// munge number to millisecond, to match stagger
var duration = this.layout.options.transitionDuration;
duration = typeof duration == 'number' ? duration + 'ms' : duration;
// enable transition styles
this.css({
transitionProperty: transitionProps,
transitionDuration: this.layout.options.transitionDuration
transitionDuration: duration,
transitionDelay: this.staggerDelay || 0
});
// listen for transition end event
this.element.addEventListener( transitionEndEvent, this, false );
};
Item.prototype.transition = Item.prototype[ transitionProperty ? '_transition' : '_nonTransition' ];
// ----- events ----- //
Item.prototype.onwebkitTransitionEnd = function( event ) {
proto.onwebkitTransitionEnd = function( event ) {
this.ontransitionend( event );
};
Item.prototype.onotransitionend = function( event ) {
proto.onotransitionend = function( event ) {
this.ontransitionend( event );
};
// properties that I munge to make my life easier
var dashedVendorProperties = {
'-webkit-transform': 'transform',
'-moz-transform': 'transform',
'-o-transform': 'transform'
'-webkit-transform': 'transform'
};
Item.prototype.ontransitionend = function( event ) {
proto.ontransitionend = function( event ) {
// disregard bubbled events from children
if ( event.target !== this.element ) {
return;
@@ -439,7 +402,7 @@ Item.prototype.ontransitionend = function( event ) {
this.emitEvent( 'transitionEnd', [ this ] );
};
Item.prototype.disableTransition = function() {
proto.disableTransition = function() {
this.removeTransitionStyles();
this.element.removeEventListener( transitionEndEvent, this, false );
this.isTransitioning = false;
@@ -449,7 +412,7 @@ Item.prototype.disableTransition = function() {
* removes style property from element
* @param {Object} style
**/
Item.prototype._removeStyles = function( style ) {
proto._removeStyles = function( style ) {
// clean up transition styles
var cleanStyle = {};
for ( var prop in style ) {
@@ -460,25 +423,33 @@ Item.prototype._removeStyles = function( style ) {
var cleanTransitionStyle = {
transitionProperty: '',
transitionDuration: ''
transitionDuration: '',
transitionDelay: ''
};
Item.prototype.removeTransitionStyles = function() {
proto.removeTransitionStyles = function() {
// remove transition
this.css( cleanTransitionStyle );
};
// ----- stagger ----- //
proto.stagger = function( delay ) {
delay = isNaN( delay ) ? 0 : delay;
this.staggerDelay = delay + 'ms';
};
// ----- show/hide/remove ----- //
// remove element from DOM
Item.prototype.removeElem = function() {
proto.removeElem = function() {
this.element.parentNode.removeChild( this.element );
// remove display: none
this.css({ display: '' });
this.emitEvent( 'remove', [ this ] );
};
Item.prototype.remove = function() {
proto.remove = function() {
// just remove element if no transition support or no transition
if ( !transitionProperty || !parseFloat( this.layout.options.transitionDuration ) ) {
this.removeElem();
@@ -486,14 +457,13 @@ Item.prototype.remove = function() {
}
// start transition
var _this = this;
this.once( 'transitionEnd', function() {
_this.removeElem();
this.removeElem();
});
this.hide();
};
Item.prototype.reveal = function() {
proto.reveal = function() {
delete this.isHidden;
// remove display: none
this.css({ display: '' });
@@ -512,7 +482,7 @@ Item.prototype.reveal = function() {
});
};
Item.prototype.onRevealTransitionEnd = function() {
proto.onRevealTransitionEnd = function() {
// check if still visible
// during transition, item may have been hidden
if ( !this.isHidden ) {
@@ -525,7 +495,7 @@ Item.prototype.onRevealTransitionEnd = function() {
* @param {String} styleProperty - hiddenStyle/visibleStyle
* @returns {String}
*/
Item.prototype.getHideRevealTransitionEndProperty = function( styleProperty ) {
proto.getHideRevealTransitionEndProperty = function( styleProperty ) {
var optionStyle = this.layout.options[ styleProperty ];
// use opacity
if ( optionStyle.opacity ) {
@@ -537,7 +507,7 @@ Item.prototype.getHideRevealTransitionEndProperty = function( styleProperty ) {
}
};
Item.prototype.hide = function() {
proto.hide = function() {
// set flag
this.isHidden = true;
// remove display: none
@@ -558,7 +528,7 @@ Item.prototype.hide = function() {
});
};
Item.prototype.onHideTransitionEnd = function() {
proto.onHideTransitionEnd = function() {
// check if still hidden
// during transition, item may have been un-hidden
if ( this.isHidden ) {
@@ -567,7 +537,7 @@ Item.prototype.onHideTransitionEnd = function() {
}
};
Item.prototype.destroy = function() {
proto.destroy = function() {
this.css({
position: '',
left: '',
+199 -186
View File
@@ -1,5 +1,5 @@
/*!
* Outlayer v1.4.2
* Outlayer v2.1.1
* the brains and guts of a layout library
* MIT license
*/
@@ -7,26 +7,24 @@
( function( window, factory ) {
'use strict';
// universal module definition
/* jshint strict: false */ /* globals define, module, require */
if ( typeof define == 'function' && define.amd ) {
// AMD
// AMD - RequireJS
define( [
'eventie/eventie',
'eventEmitter/EventEmitter',
'ev-emitter/ev-emitter',
'get-size/get-size',
'fizzy-ui-utils/utils',
'./item'
],
function( eventie, EventEmitter, getSize, utils, Item ) {
return factory( window, eventie, EventEmitter, getSize, utils, Item);
function( EvEmitter, getSize, utils, Item ) {
return factory( window, EvEmitter, getSize, utils, Item);
}
);
} else if ( typeof exports == 'object' ) {
// CommonJS
} else if ( typeof module == 'object' && module.exports ) {
// CommonJS - Browserify, Webpack
module.exports = factory(
window,
require('eventie'),
require('wolfy87-eventemitter'),
require('ev-emitter'),
require('get-size'),
require('fizzy-ui-utils'),
require('./item')
@@ -35,15 +33,14 @@
// browser global
window.Outlayer = factory(
window,
window.eventie,
window.EventEmitter,
window.EvEmitter,
window.getSize,
window.fizzyUIUtils,
window.Outlayer.Item
);
}
}( window, function factory( window, eventie, EventEmitter, getSize, utils, Item ) {
}( window, function factory( window, EvEmitter, getSize, utils, Item ) {
'use strict';
// ----- vars ----- //
@@ -92,7 +89,8 @@ function Outlayer( element, options ) {
// kick it off
this._create();
if ( this.options.isInitLayout ) {
var isInitLayout = this._getOption('initLayout');
if ( isInitLayout ) {
this.layout();
}
}
@@ -106,11 +104,11 @@ Outlayer.defaults = {
containerStyle: {
position: 'relative'
},
isInitLayout: true,
isOriginLeft: true,
isOriginTop: true,
isResizeBound: true,
isResizingContainer: true,
initLayout: true,
originLeft: true,
originTop: true,
resize: true,
resizeContainer: true,
// item options
transitionDuration: '0.4s',
hiddenStyle: {
@@ -123,18 +121,39 @@ Outlayer.defaults = {
}
};
// inherit EventEmitter
utils.extend( Outlayer.prototype, EventEmitter.prototype );
var proto = Outlayer.prototype;
// inherit EvEmitter
utils.extend( proto, EvEmitter.prototype );
/**
* set options
* @param {Object} opts
*/
Outlayer.prototype.option = function( opts ) {
proto.option = function( opts ) {
utils.extend( this.options, opts );
};
Outlayer.prototype._create = function() {
/**
* get backwards compatible option value, check old name
*/
proto._getOption = function( option ) {
var oldOption = this.constructor.compatOptions[ option ];
return oldOption && this.options[ oldOption ] !== undefined ?
this.options[ oldOption ] : this.options[ option ];
};
Outlayer.compatOptions = {
// currentName: oldName
initLayout: 'isInitLayout',
horizontal: 'isHorizontal',
layoutInstant: 'isLayoutInstant',
originLeft: 'isOriginLeft',
originTop: 'isOriginTop',
resize: 'isResizeBound',
resizeContainer: 'isResizingContainer'
};
proto._create = function() {
// get items from children
this.reloadItems();
// elements that affect layout, but are not laid out
@@ -144,13 +163,14 @@ Outlayer.prototype._create = function() {
utils.extend( this.element.style, this.options.containerStyle );
// bind resize method
if ( this.options.isResizeBound ) {
var canBindResize = this._getOption('resize');
if ( canBindResize ) {
this.bindResize();
}
};
// goes through all children again and gets bricks in proper order
Outlayer.prototype.reloadItems = function() {
proto.reloadItems = function() {
// collection of item elements
this.items = this._itemize( this.element.children );
};
@@ -161,14 +181,14 @@ Outlayer.prototype.reloadItems = function() {
* @param {Array or NodeList or HTMLElement} elems
* @returns {Array} items - collection of new Outlayer Items
*/
Outlayer.prototype._itemize = function( elems ) {
proto._itemize = function( elems ) {
var itemElems = this._filterFindItemElements( elems );
var Item = this.constructor.Item;
// create new Outlayer Items for collection
var items = [];
for ( var i=0, len = itemElems.length; i < len; i++ ) {
for ( var i=0; i < itemElems.length; i++ ) {
var elem = itemElems[i];
var item = new Item( elem, this );
items.push( item );
@@ -182,7 +202,7 @@ Outlayer.prototype._itemize = function( elems ) {
* @param {Array or NodeList or HTMLElement} elems
* @returns {Array} items - item elements
*/
Outlayer.prototype._filterFindItemElements = function( elems ) {
proto._filterFindItemElements = function( elems ) {
return utils.filterFindElements( elems, this.options.itemSelector );
};
@@ -190,12 +210,10 @@ Outlayer.prototype._filterFindItemElements = function( elems ) {
* getter method for getting item elements
* @returns {Array} elems - collection of item elements
*/
Outlayer.prototype.getItemElements = function() {
var elems = [];
for ( var i=0, len = this.items.length; i < len; i++ ) {
elems.push( this.items[i].element );
}
return elems;
proto.getItemElements = function() {
return this.items.map( function( item ) {
return item.element;
});
};
// ----- init & layout ----- //
@@ -203,13 +221,14 @@ Outlayer.prototype.getItemElements = function() {
/**
* lays out all items
*/
Outlayer.prototype.layout = function() {
proto.layout = function() {
this._resetLayout();
this._manageStamps();
// don't animate first layout
var isInstant = this.options.isLayoutInstant !== undefined ?
this.options.isLayoutInstant : !this._isLayoutInited;
var layoutInstant = this._getOption('layoutInstant');
var isInstant = layoutInstant !== undefined ?
layoutInstant : !this._isLayoutInited;
this.layoutItems( this.items, isInstant );
// flag for initalized
@@ -217,17 +236,17 @@ Outlayer.prototype.layout = function() {
};
// _init is alias for layout
Outlayer.prototype._init = Outlayer.prototype.layout;
proto._init = proto.layout;
/**
* logic before any new layout
*/
Outlayer.prototype._resetLayout = function() {
proto._resetLayout = function() {
this.getSize();
};
Outlayer.prototype.getSize = function() {
proto.getSize = function() {
this.size = getSize( this.element );
};
@@ -241,7 +260,7 @@ Outlayer.prototype.getSize = function() {
* @param {String} size - width or height
* @private
*/
Outlayer.prototype._getMeasurement = function( measurement, size ) {
proto._getMeasurement = function( measurement, size ) {
var option = this.options[ measurement ];
var elem;
if ( !option ) {
@@ -249,9 +268,9 @@ Outlayer.prototype._getMeasurement = function( measurement, size ) {
this[ measurement ] = 0;
} else {
// use option as an element
if ( typeof option === 'string' ) {
if ( typeof option == 'string' ) {
elem = this.element.querySelector( option );
} else if ( utils.isElement( option ) ) {
} else if ( option instanceof HTMLElement ) {
elem = option;
}
// use size of element, if element
@@ -263,7 +282,7 @@ Outlayer.prototype._getMeasurement = function( measurement, size ) {
* layout a collection of item elements
* @api public
*/
Outlayer.prototype.layoutItems = function( items, isInstant ) {
proto.layoutItems = function( items, isInstant ) {
items = this._getItemsForLayout( items );
this._layoutItems( items, isInstant );
@@ -277,15 +296,10 @@ Outlayer.prototype.layoutItems = function( items, isInstant ) {
* @param {Array} items
* @returns {Array} items
*/
Outlayer.prototype._getItemsForLayout = function( items ) {
var layoutItems = [];
for ( var i=0, len = items.length; i < len; i++ ) {
var item = items[i];
if ( !item.isIgnored ) {
layoutItems.push( item );
}
}
return layoutItems;
proto._getItemsForLayout = function( items ) {
return items.filter( function( item ) {
return !item.isIgnored;
});
};
/**
@@ -293,7 +307,7 @@ Outlayer.prototype._getItemsForLayout = function( items ) {
* @param {Array} items
* @param {Boolean} isInstant
*/
Outlayer.prototype._layoutItems = function( items, isInstant ) {
proto._layoutItems = function( items, isInstant ) {
this._emitCompleteOnItems( 'layout', items );
if ( !items || !items.length ) {
@@ -303,15 +317,14 @@ Outlayer.prototype._layoutItems = function( items, isInstant ) {
var queue = [];
for ( var i=0, len = items.length; i < len; i++ ) {
var item = items[i];
items.forEach( function( item ) {
// get x/y object from method
var position = this._getItemLayoutPosition( item );
// enqueue
position.item = item;
position.isInstant = isInstant || item.isLayoutInstant;
queue.push( position );
}
}, this );
this._processLayoutQueue( queue );
};
@@ -321,7 +334,7 @@ Outlayer.prototype._layoutItems = function( items, isInstant ) {
* @param {Outlayer.Item} item
* @returns {Object} x and y position
*/
Outlayer.prototype._getItemLayoutPosition = function( /* item */ ) {
proto._getItemLayoutPosition = function( /* item */ ) {
return {
x: 0,
y: 0
@@ -334,11 +347,22 @@ Outlayer.prototype._getItemLayoutPosition = function( /* item */ ) {
* thx @paul_irish
* @param {Array} queue
*/
Outlayer.prototype._processLayoutQueue = function( queue ) {
for ( var i=0, len = queue.length; i < len; i++ ) {
var obj = queue[i];
this._positionItem( obj.item, obj.x, obj.y, obj.isInstant );
proto._processLayoutQueue = function( queue ) {
this.updateStagger();
queue.forEach( function( obj, i ) {
this._positionItem( obj.item, obj.x, obj.y, obj.isInstant, i );
}, this );
};
// set stagger from option in milliseconds number
proto.updateStagger = function() {
var stagger = this.options.stagger;
if ( stagger === null || stagger === undefined ) {
this.stagger = 0;
return;
}
this.stagger = getMilliseconds( stagger );
return this.stagger;
};
/**
@@ -348,11 +372,12 @@ Outlayer.prototype._processLayoutQueue = function( queue ) {
* @param {Number} y - vertical position
* @param {Boolean} isInstant - disables transitions
*/
Outlayer.prototype._positionItem = function( item, x, y, isInstant ) {
proto._positionItem = function( item, x, y, isInstant, i ) {
if ( isInstant ) {
// if not transition, just set CSS
item.goTo( x, y );
} else {
item.stagger( i * this.stagger );
item.moveTo( x, y );
}
};
@@ -361,12 +386,13 @@ Outlayer.prototype._positionItem = function( item, x, y, isInstant ) {
* Any logic you want to do after each layout,
* i.e. size the container
*/
Outlayer.prototype._postLayout = function() {
proto._postLayout = function() {
this.resizeContainer();
};
Outlayer.prototype.resizeContainer = function() {
if ( !this.options.isResizingContainer ) {
proto.resizeContainer = function() {
var isResizingContainer = this._getOption('resizeContainer');
if ( !isResizingContainer ) {
return;
}
var size = this._getContainerSize();
@@ -382,13 +408,13 @@ Outlayer.prototype.resizeContainer = function() {
* @param {Number} width
* @param {Number} height
*/
Outlayer.prototype._getContainerSize = noop;
proto._getContainerSize = noop;
/**
* @param {Number} measure - size of width or height
* @param {Boolean} isWidth
*/
Outlayer.prototype._setContainerMeasure = function( measure, isWidth ) {
proto._setContainerMeasure = function( measure, isWidth ) {
if ( measure === undefined ) {
return;
}
@@ -411,7 +437,7 @@ Outlayer.prototype._setContainerMeasure = function( measure, isWidth ) {
* @param {String} eventName
* @param {Array} items - Outlayer.Items
*/
Outlayer.prototype._emitCompleteOnItems = function( eventName, items ) {
proto._emitCompleteOnItems = function( eventName, items ) {
var _this = this;
function onComplete() {
_this.dispatchEvent( eventName + 'Complete', null, [ items ] );
@@ -426,25 +452,24 @@ Outlayer.prototype._emitCompleteOnItems = function( eventName, items ) {
var doneCount = 0;
function tick() {
doneCount++;
if ( doneCount === count ) {
if ( doneCount == count ) {
onComplete();
}
}
// bind callback
for ( var i=0, len = items.length; i < len; i++ ) {
var item = items[i];
items.forEach( function( item ) {
item.once( eventName, tick );
}
});
};
/**
* emits events via eventEmitter and jQuery events
* emits events via EvEmitter 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 ) {
proto.dispatchEvent = function( type, event, args ) {
// add original event to arguments
var emitArgs = event ? [ event ].concat( args ) : args;
this.emitEvent( type, emitArgs );
@@ -472,7 +497,7 @@ Outlayer.prototype.dispatchEvent = function( type, event, args ) {
* ignored items do not get skipped in layout
* @param {Element} elem
*/
Outlayer.prototype.ignore = function( elem ) {
proto.ignore = function( elem ) {
var item = this.getItem( elem );
if ( item ) {
item.isIgnored = true;
@@ -483,7 +508,7 @@ Outlayer.prototype.ignore = function( elem ) {
* return item to layout collection
* @param {Element} elem
*/
Outlayer.prototype.unignore = function( elem ) {
proto.unignore = function( elem ) {
var item = this.getItem( elem );
if ( item ) {
delete item.isIgnored;
@@ -494,7 +519,7 @@ Outlayer.prototype.unignore = function( elem ) {
* adds elements to stamps
* @param {NodeList, Array, Element, or String} elems
*/
Outlayer.prototype.stamp = function( elems ) {
proto.stamp = function( elems ) {
elems = this._find( elems );
if ( !elems ) {
return;
@@ -502,29 +527,24 @@ Outlayer.prototype.stamp = function( elems ) {
this.stamps = this.stamps.concat( elems );
// ignore
for ( var i=0, len = elems.length; i < len; i++ ) {
var elem = elems[i];
this.ignore( elem );
}
elems.forEach( this.ignore, this );
};
/**
* removes elements to stamps
* @param {NodeList, Array, or Element} elems
*/
Outlayer.prototype.unstamp = function( elems ) {
proto.unstamp = function( elems ) {
elems = this._find( elems );
if ( !elems ){
return;
}
for ( var i=0, len = elems.length; i < len; i++ ) {
var elem = elems[i];
elems.forEach( function( elem ) {
// filter out removed stamp elements
utils.removeFrom( this.stamps, elem );
this.unignore( elem );
}
}, this );
};
/**
@@ -532,33 +552,30 @@ Outlayer.prototype.unstamp = function( elems ) {
* @param {NodeList, Array, Element, or String} elems
* @returns {Array} elems
*/
Outlayer.prototype._find = function( elems ) {
proto._find = function( elems ) {
if ( !elems ) {
return;
}
// if string, use argument as selector string
if ( typeof elems === 'string' ) {
if ( typeof elems == 'string' ) {
elems = this.element.querySelectorAll( elems );
}
elems = utils.makeArray( elems );
return elems;
};
Outlayer.prototype._manageStamps = function() {
proto._manageStamps = function() {
if ( !this.stamps || !this.stamps.length ) {
return;
}
this._getBoundingRect();
for ( var i=0, len = this.stamps.length; i < len; i++ ) {
var stamp = this.stamps[i];
this._manageStamp( stamp );
}
this.stamps.forEach( this._manageStamp, this );
};
// update boundingLeft / Top
Outlayer.prototype._getBoundingRect = function() {
proto._getBoundingRect = function() {
// get bounding rect for container element
var boundingRect = this.element.getBoundingClientRect();
var size = this.size;
@@ -573,14 +590,14 @@ Outlayer.prototype._getBoundingRect = function() {
/**
* @param {Element} stamp
**/
Outlayer.prototype._manageStamp = noop;
proto._manageStamp = noop;
/**
* get x/y position of element relative to container element
* @param {Element} elem
* @returns {Object} offset - has left, top, right, bottom
*/
Outlayer.prototype._getElementOffset = function( elem ) {
proto._getElementOffset = function( elem ) {
var boundingRect = elem.getBoundingClientRect();
var thisRect = this._boundingRect;
var size = getSize( elem );
@@ -597,55 +614,31 @@ Outlayer.prototype._getElementOffset = function( elem ) {
// enable event handlers for listeners
// i.e. resize -> onresize
Outlayer.prototype.handleEvent = function( event ) {
var method = 'on' + event.type;
if ( this[ method ] ) {
this[ method ]( event );
}
};
proto.handleEvent = utils.handleEvent;
/**
* Bind layout to window resizing
*/
Outlayer.prototype.bindResize = function() {
// bind just one listener
if ( this.isResizeBound ) {
return;
}
eventie.bind( window, 'resize', this );
proto.bindResize = function() {
window.addEventListener( 'resize', this );
this.isResizeBound = true;
};
/**
* Unbind layout to window resizing
*/
Outlayer.prototype.unbindResize = function() {
if ( this.isResizeBound ) {
eventie.unbind( window, 'resize', this );
}
proto.unbindResize = function() {
window.removeEventListener( 'resize', this );
this.isResizeBound = false;
};
// original debounce by John Hann
// http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
// this fires every resize
Outlayer.prototype.onresize = function() {
if ( this.resizeTimeout ) {
clearTimeout( this.resizeTimeout );
}
var _this = this;
function delayed() {
_this.resize();
delete _this.resizeTimeout;
}
this.resizeTimeout = setTimeout( delayed, 100 );
proto.onresize = function() {
this.resize();
};
// debounced, layout on resize
Outlayer.prototype.resize = function() {
utils.debounceMethod( Outlayer, 'onresize', 100 );
proto.resize = function() {
// don't trigger if size did not change
// or if resize was unbound. See #9
if ( !this.isResizeBound || !this.needsResizeLayout() ) {
@@ -659,7 +652,7 @@ Outlayer.prototype.resize = function() {
* check if layout is needed post layout
* @returns Boolean
*/
Outlayer.prototype.needsResizeLayout = function() {
proto.needsResizeLayout = function() {
var size = getSize( this.element );
// check that this.size and size are there
// IE8 triggers resize on body size change, so they might not be
@@ -674,7 +667,7 @@ Outlayer.prototype.needsResizeLayout = function() {
* @param {Array or NodeList or Element} elems
* @returns {Array} items - Outlayer.Items
**/
Outlayer.prototype.addItems = function( elems ) {
proto.addItems = function( elems ) {
var items = this._itemize( elems );
// add items to collection
if ( items.length ) {
@@ -687,7 +680,7 @@ Outlayer.prototype.addItems = function( elems ) {
* Layout newly-appended item elements
* @param {Array or NodeList or Element} elems
*/
Outlayer.prototype.appended = function( elems ) {
proto.appended = function( elems ) {
var items = this.addItems( elems );
if ( !items.length ) {
return;
@@ -701,7 +694,7 @@ Outlayer.prototype.appended = function( elems ) {
* Layout prepended elements
* @param {Array or NodeList or Element} elems
*/
Outlayer.prototype.prepended = function( elems ) {
proto.prepended = function( elems ) {
var items = this._itemize( elems );
if ( !items.length ) {
return;
@@ -723,35 +716,39 @@ Outlayer.prototype.prepended = function( elems ) {
* reveal a collection of items
* @param {Array of Outlayer.Items} items
*/
Outlayer.prototype.reveal = function( items ) {
proto.reveal = function( items ) {
this._emitCompleteOnItems( 'reveal', items );
var len = items && items.length;
for ( var i=0; len && i < len; i++ ) {
var item = items[i];
item.reveal();
if ( !items || !items.length ) {
return;
}
var stagger = this.updateStagger();
items.forEach( function( item, i ) {
item.stagger( i * stagger );
item.reveal();
});
};
/**
* hide a collection of items
* @param {Array of Outlayer.Items} items
*/
Outlayer.prototype.hide = function( items ) {
proto.hide = function( items ) {
this._emitCompleteOnItems( 'hide', items );
var len = items && items.length;
for ( var i=0; len && i < len; i++ ) {
var item = items[i];
item.hide();
if ( !items || !items.length ) {
return;
}
var stagger = this.updateStagger();
items.forEach( function( item, i ) {
item.stagger( i * stagger );
item.hide();
});
};
/**
* reveal item elements
* @param {Array}, {Element}, {NodeList} items
*/
Outlayer.prototype.revealItemElements = function( elems ) {
proto.revealItemElements = function( elems ) {
var items = this.getItems( elems );
this.reveal( items );
};
@@ -760,7 +757,7 @@ Outlayer.prototype.revealItemElements = function( elems ) {
* hide item elements
* @param {Array}, {Element}, {NodeList} items
*/
Outlayer.prototype.hideItemElements = function( elems ) {
proto.hideItemElements = function( elems ) {
var items = this.getItems( elems );
this.hide( items );
};
@@ -771,11 +768,11 @@ Outlayer.prototype.hideItemElements = function( elems ) {
* @param {Function} callback
* @returns {Outlayer.Item} item
*/
Outlayer.prototype.getItem = function( elem ) {
proto.getItem = function( elem ) {
// loop through items to get the one that matches
for ( var i=0, len = this.items.length; i < len; i++ ) {
for ( var i=0; i < this.items.length; i++ ) {
var item = this.items[i];
if ( item.element === elem ) {
if ( item.element == elem ) {
// return item
return item;
}
@@ -787,16 +784,15 @@ Outlayer.prototype.getItem = function( elem ) {
* @param {Array} elems
* @returns {Array} items - Outlayer.Items
*/
Outlayer.prototype.getItems = function( elems ) {
proto.getItems = function( elems ) {
elems = utils.makeArray( elems );
var items = [];
for ( var i=0, len = elems.length; i < len; i++ ) {
var elem = elems[i];
elems.forEach( function( elem ) {
var item = this.getItem( elem );
if ( item ) {
items.push( item );
}
}
}, this );
return items;
};
@@ -805,7 +801,7 @@ Outlayer.prototype.getItems = function( elems ) {
* remove element(s) from instance and DOM
* @param {Array or NodeList or Element} elems
*/
Outlayer.prototype.remove = function( elems ) {
proto.remove = function( elems ) {
var removeItems = this.getItems( elems );
this._emitCompleteOnItems( 'remove', removeItems );
@@ -815,28 +811,26 @@ Outlayer.prototype.remove = function( elems ) {
return;
}
for ( var i=0, len = removeItems.length; i < len; i++ ) {
var item = removeItems[i];
removeItems.forEach( function( item ) {
item.remove();
// remove item from collection
utils.removeFrom( this.items, item );
}
}, this );
};
// ----- destroy ----- //
// remove and disable Outlayer instance
Outlayer.prototype.destroy = function() {
proto.destroy = function() {
// clean up dynamic styles
var style = this.element.style;
style.height = '';
style.position = '';
style.width = '';
// destroy items
for ( var i=0, len = this.items.length; i < len; i++ ) {
var item = this.items[i];
this.items.forEach( function( item ) {
item.destroy();
}
});
this.unbindResize();
@@ -872,34 +866,18 @@ Outlayer.data = function( elem ) {
*/
Outlayer.create = function( namespace, options ) {
// sub-class Outlayer
function Layout() {
Outlayer.apply( this, arguments );
}
// inherit Outlayer prototype, use Object.create if there
if ( Object.create ) {
Layout.prototype = Object.create( Outlayer.prototype );
} else {
utils.extend( Layout.prototype, Outlayer.prototype );
}
// set contructor, used for namespace and Item
Layout.prototype.constructor = Layout;
var Layout = subclass( Outlayer );
// apply new options and compatOptions
Layout.defaults = utils.extend( {}, Outlayer.defaults );
// apply new options
utils.extend( Layout.defaults, options );
// keep prototype.settings for backwards compatibility (Packery v1.2.0)
Layout.prototype.settings = {};
Layout.compatOptions = utils.extend( {}, Outlayer.compatOptions );
Layout.namespace = namespace;
Layout.data = Outlayer.data;
// sub-class Item
Layout.Item = function LayoutItem() {
Item.apply( this, arguments );
};
Layout.Item.prototype = new Item();
Layout.Item = subclass( Item );
// -------------------------- declarative -------------------------- //
@@ -915,6 +893,42 @@ Outlayer.create = function( namespace, options ) {
return Layout;
};
function subclass( Parent ) {
function SubClass() {
Parent.apply( this, arguments );
}
SubClass.prototype = Object.create( Parent.prototype );
SubClass.prototype.constructor = SubClass;
return SubClass;
}
// ----- helpers ----- //
// how many milliseconds are in each unit
var msUnits = {
ms: 1,
s: 1000
};
// munge time-like parameter into millisecond number
// '0.4s' -> 40
function getMilliseconds( time ) {
if ( typeof time == 'number' ) {
return time;
}
var matches = time.match( /(^\d*\.?\d*)(\w*)/ );
var num = matches && matches[1];
var unit = matches && matches[2];
if ( !num.length ) {
return 0;
}
num = parseFloat( num );
var mult = msUnits[ unit ] || 1;
return num * mult;
}
// ----- fin ----- //
// back in global
@@ -923,4 +937,3 @@ Outlayer.Item = Item;
return Outlayer;
}));