outlayer.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /*!
  2. * Outlayer v2.1.1
  3. * the brains and guts of a layout library
  4. * MIT license
  5. */
  6. ( function( window, factory ) {
  7. 'use strict';
  8. // universal module definition
  9. /* jshint strict: false */ /* globals define, module, require */
  10. if ( typeof define == 'function' && define.amd ) {
  11. // AMD - RequireJS
  12. define( [
  13. 'ev-emitter/ev-emitter',
  14. 'get-size/get-size',
  15. 'fizzy-ui-utils/utils',
  16. './item'
  17. ],
  18. function( EvEmitter, getSize, utils, Item ) {
  19. return factory( window, EvEmitter, getSize, utils, Item);
  20. }
  21. );
  22. } else if ( typeof module == 'object' && module.exports ) {
  23. // CommonJS - Browserify, Webpack
  24. module.exports = factory(
  25. window,
  26. require('ev-emitter'),
  27. require('get-size'),
  28. require('fizzy-ui-utils'),
  29. require('./item')
  30. );
  31. } else {
  32. // browser global
  33. window.Outlayer = factory(
  34. window,
  35. window.EvEmitter,
  36. window.getSize,
  37. window.fizzyUIUtils,
  38. window.Outlayer.Item
  39. );
  40. }
  41. }( window, function factory( window, EvEmitter, getSize, utils, Item ) {
  42. 'use strict';
  43. // ----- vars ----- //
  44. var console = window.console;
  45. var jQuery = window.jQuery;
  46. var noop = function() {};
  47. // -------------------------- Outlayer -------------------------- //
  48. // globally unique identifiers
  49. var GUID = 0;
  50. // internal store of all Outlayer intances
  51. var instances = {};
  52. /**
  53. * @param {Element, String} element
  54. * @param {Object} options
  55. * @constructor
  56. */
  57. function Outlayer( element, options ) {
  58. var queryElement = utils.getQueryElement( element );
  59. if ( !queryElement ) {
  60. if ( console ) {
  61. console.error( 'Bad element for ' + this.constructor.namespace +
  62. ': ' + ( queryElement || element ) );
  63. }
  64. return;
  65. }
  66. this.element = queryElement;
  67. // add jQuery
  68. if ( jQuery ) {
  69. this.$element = jQuery( this.element );
  70. }
  71. // options
  72. this.options = utils.extend( {}, this.constructor.defaults );
  73. this.option( options );
  74. // add id for Outlayer.getFromElement
  75. var id = ++GUID;
  76. this.element.outlayerGUID = id; // expando
  77. instances[ id ] = this; // associate via id
  78. // kick it off
  79. this._create();
  80. var isInitLayout = this._getOption('initLayout');
  81. if ( isInitLayout ) {
  82. this.layout();
  83. }
  84. }
  85. // settings are for internal use only
  86. Outlayer.namespace = 'outlayer';
  87. Outlayer.Item = Item;
  88. // default options
  89. Outlayer.defaults = {
  90. containerStyle: {
  91. position: 'relative'
  92. },
  93. initLayout: true,
  94. originLeft: true,
  95. originTop: true,
  96. resize: true,
  97. resizeContainer: true,
  98. // item options
  99. transitionDuration: '0.4s',
  100. hiddenStyle: {
  101. opacity: 0,
  102. transform: 'scale(0.001)'
  103. },
  104. visibleStyle: {
  105. opacity: 1,
  106. transform: 'scale(1)'
  107. }
  108. };
  109. var proto = Outlayer.prototype;
  110. // inherit EvEmitter
  111. utils.extend( proto, EvEmitter.prototype );
  112. /**
  113. * set options
  114. * @param {Object} opts
  115. */
  116. proto.option = function( opts ) {
  117. utils.extend( this.options, opts );
  118. };
  119. /**
  120. * get backwards compatible option value, check old name
  121. */
  122. proto._getOption = function( option ) {
  123. var oldOption = this.constructor.compatOptions[ option ];
  124. return oldOption && this.options[ oldOption ] !== undefined ?
  125. this.options[ oldOption ] : this.options[ option ];
  126. };
  127. Outlayer.compatOptions = {
  128. // currentName: oldName
  129. initLayout: 'isInitLayout',
  130. horizontal: 'isHorizontal',
  131. layoutInstant: 'isLayoutInstant',
  132. originLeft: 'isOriginLeft',
  133. originTop: 'isOriginTop',
  134. resize: 'isResizeBound',
  135. resizeContainer: 'isResizingContainer'
  136. };
  137. proto._create = function() {
  138. // get items from children
  139. this.reloadItems();
  140. // elements that affect layout, but are not laid out
  141. this.stamps = [];
  142. this.stamp( this.options.stamp );
  143. // set container style
  144. utils.extend( this.element.style, this.options.containerStyle );
  145. // bind resize method
  146. var canBindResize = this._getOption('resize');
  147. if ( canBindResize ) {
  148. this.bindResize();
  149. }
  150. };
  151. // goes through all children again and gets bricks in proper order
  152. proto.reloadItems = function() {
  153. // collection of item elements
  154. this.items = this._itemize( this.element.children );
  155. };
  156. /**
  157. * turn elements into Outlayer.Items to be used in layout
  158. * @param {Array or NodeList or HTMLElement} elems
  159. * @returns {Array} items - collection of new Outlayer Items
  160. */
  161. proto._itemize = function( elems ) {
  162. var itemElems = this._filterFindItemElements( elems );
  163. var Item = this.constructor.Item;
  164. // create new Outlayer Items for collection
  165. var items = [];
  166. for ( var i=0; i < itemElems.length; i++ ) {
  167. var elem = itemElems[i];
  168. var item = new Item( elem, this );
  169. items.push( item );
  170. }
  171. return items;
  172. };
  173. /**
  174. * get item elements to be used in layout
  175. * @param {Array or NodeList or HTMLElement} elems
  176. * @returns {Array} items - item elements
  177. */
  178. proto._filterFindItemElements = function( elems ) {
  179. return utils.filterFindElements( elems, this.options.itemSelector );
  180. };
  181. /**
  182. * getter method for getting item elements
  183. * @returns {Array} elems - collection of item elements
  184. */
  185. proto.getItemElements = function() {
  186. return this.items.map( function( item ) {
  187. return item.element;
  188. });
  189. };
  190. // ----- init & layout ----- //
  191. /**
  192. * lays out all items
  193. */
  194. proto.layout = function() {
  195. this._resetLayout();
  196. this._manageStamps();
  197. // don't animate first layout
  198. var layoutInstant = this._getOption('layoutInstant');
  199. var isInstant = layoutInstant !== undefined ?
  200. layoutInstant : !this._isLayoutInited;
  201. this.layoutItems( this.items, isInstant );
  202. // flag for initalized
  203. this._isLayoutInited = true;
  204. };
  205. // _init is alias for layout
  206. proto._init = proto.layout;
  207. /**
  208. * logic before any new layout
  209. */
  210. proto._resetLayout = function() {
  211. this.getSize();
  212. };
  213. proto.getSize = function() {
  214. this.size = getSize( this.element );
  215. };
  216. /**
  217. * get measurement from option, for columnWidth, rowHeight, gutter
  218. * if option is String -> get element from selector string, & get size of element
  219. * if option is Element -> get size of element
  220. * else use option as a number
  221. *
  222. * @param {String} measurement
  223. * @param {String} size - width or height
  224. * @private
  225. */
  226. proto._getMeasurement = function( measurement, size ) {
  227. var option = this.options[ measurement ];
  228. var elem;
  229. if ( !option ) {
  230. // default to 0
  231. this[ measurement ] = 0;
  232. } else {
  233. // use option as an element
  234. if ( typeof option == 'string' ) {
  235. elem = this.element.querySelector( option );
  236. } else if ( option instanceof HTMLElement ) {
  237. elem = option;
  238. }
  239. // use size of element, if element
  240. this[ measurement ] = elem ? getSize( elem )[ size ] : option;
  241. }
  242. };
  243. /**
  244. * layout a collection of item elements
  245. * @api public
  246. */
  247. proto.layoutItems = function( items, isInstant ) {
  248. items = this._getItemsForLayout( items );
  249. this._layoutItems( items, isInstant );
  250. this._postLayout();
  251. };
  252. /**
  253. * get the items to be laid out
  254. * you may want to skip over some items
  255. * @param {Array} items
  256. * @returns {Array} items
  257. */
  258. proto._getItemsForLayout = function( items ) {
  259. return items.filter( function( item ) {
  260. return !item.isIgnored;
  261. });
  262. };
  263. /**
  264. * layout items
  265. * @param {Array} items
  266. * @param {Boolean} isInstant
  267. */
  268. proto._layoutItems = function( items, isInstant ) {
  269. this._emitCompleteOnItems( 'layout', items );
  270. if ( !items || !items.length ) {
  271. // no items, emit event with empty array
  272. return;
  273. }
  274. var queue = [];
  275. items.forEach( function( item ) {
  276. // get x/y object from method
  277. var position = this._getItemLayoutPosition( item );
  278. // enqueue
  279. position.item = item;
  280. position.isInstant = isInstant || item.isLayoutInstant;
  281. queue.push( position );
  282. }, this );
  283. this._processLayoutQueue( queue );
  284. };
  285. /**
  286. * get item layout position
  287. * @param {Outlayer.Item} item
  288. * @returns {Object} x and y position
  289. */
  290. proto._getItemLayoutPosition = function( /* item */ ) {
  291. return {
  292. x: 0,
  293. y: 0
  294. };
  295. };
  296. /**
  297. * iterate over array and position each item
  298. * Reason being - separating this logic prevents 'layout invalidation'
  299. * thx @paul_irish
  300. * @param {Array} queue
  301. */
  302. proto._processLayoutQueue = function( queue ) {
  303. this.updateStagger();
  304. queue.forEach( function( obj, i ) {
  305. this._positionItem( obj.item, obj.x, obj.y, obj.isInstant, i );
  306. }, this );
  307. };
  308. // set stagger from option in milliseconds number
  309. proto.updateStagger = function() {
  310. var stagger = this.options.stagger;
  311. if ( stagger === null || stagger === undefined ) {
  312. this.stagger = 0;
  313. return;
  314. }
  315. this.stagger = getMilliseconds( stagger );
  316. return this.stagger;
  317. };
  318. /**
  319. * Sets position of item in DOM
  320. * @param {Outlayer.Item} item
  321. * @param {Number} x - horizontal position
  322. * @param {Number} y - vertical position
  323. * @param {Boolean} isInstant - disables transitions
  324. */
  325. proto._positionItem = function( item, x, y, isInstant, i ) {
  326. if ( isInstant ) {
  327. // if not transition, just set CSS
  328. item.goTo( x, y );
  329. } else {
  330. item.stagger( i * this.stagger );
  331. item.moveTo( x, y );
  332. }
  333. };
  334. /**
  335. * Any logic you want to do after each layout,
  336. * i.e. size the container
  337. */
  338. proto._postLayout = function() {
  339. this.resizeContainer();
  340. };
  341. proto.resizeContainer = function() {
  342. var isResizingContainer = this._getOption('resizeContainer');
  343. if ( !isResizingContainer ) {
  344. return;
  345. }
  346. var size = this._getContainerSize();
  347. if ( size ) {
  348. this._setContainerMeasure( size.width, true );
  349. this._setContainerMeasure( size.height, false );
  350. }
  351. };
  352. /**
  353. * Sets width or height of container if returned
  354. * @returns {Object} size
  355. * @param {Number} width
  356. * @param {Number} height
  357. */
  358. proto._getContainerSize = noop;
  359. /**
  360. * @param {Number} measure - size of width or height
  361. * @param {Boolean} isWidth
  362. */
  363. proto._setContainerMeasure = function( measure, isWidth ) {
  364. if ( measure === undefined ) {
  365. return;
  366. }
  367. var elemSize = this.size;
  368. // add padding and border width if border box
  369. if ( elemSize.isBorderBox ) {
  370. measure += isWidth ? elemSize.paddingLeft + elemSize.paddingRight +
  371. elemSize.borderLeftWidth + elemSize.borderRightWidth :
  372. elemSize.paddingBottom + elemSize.paddingTop +
  373. elemSize.borderTopWidth + elemSize.borderBottomWidth;
  374. }
  375. measure = Math.max( measure, 0 );
  376. this.element.style[ isWidth ? 'width' : 'height' ] = measure + 'px';
  377. };
  378. /**
  379. * emit eventComplete on a collection of items events
  380. * @param {String} eventName
  381. * @param {Array} items - Outlayer.Items
  382. */
  383. proto._emitCompleteOnItems = function( eventName, items ) {
  384. var _this = this;
  385. function onComplete() {
  386. _this.dispatchEvent( eventName + 'Complete', null, [ items ] );
  387. }
  388. var count = items.length;
  389. if ( !items || !count ) {
  390. onComplete();
  391. return;
  392. }
  393. var doneCount = 0;
  394. function tick() {
  395. doneCount++;
  396. if ( doneCount == count ) {
  397. onComplete();
  398. }
  399. }
  400. // bind callback
  401. items.forEach( function( item ) {
  402. item.once( eventName, tick );
  403. });
  404. };
  405. /**
  406. * emits events via EvEmitter and jQuery events
  407. * @param {String} type - name of event
  408. * @param {Event} event - original event
  409. * @param {Array} args - extra arguments
  410. */
  411. proto.dispatchEvent = function( type, event, args ) {
  412. // add original event to arguments
  413. var emitArgs = event ? [ event ].concat( args ) : args;
  414. this.emitEvent( type, emitArgs );
  415. if ( jQuery ) {
  416. // set this.$element
  417. this.$element = this.$element || jQuery( this.element );
  418. if ( event ) {
  419. // create jQuery event
  420. var $event = jQuery.Event( event );
  421. $event.type = type;
  422. this.$element.trigger( $event, args );
  423. } else {
  424. // just trigger with type if no event available
  425. this.$element.trigger( type, args );
  426. }
  427. }
  428. };
  429. // -------------------------- ignore & stamps -------------------------- //
  430. /**
  431. * keep item in collection, but do not lay it out
  432. * ignored items do not get skipped in layout
  433. * @param {Element} elem
  434. */
  435. proto.ignore = function( elem ) {
  436. var item = this.getItem( elem );
  437. if ( item ) {
  438. item.isIgnored = true;
  439. }
  440. };
  441. /**
  442. * return item to layout collection
  443. * @param {Element} elem
  444. */
  445. proto.unignore = function( elem ) {
  446. var item = this.getItem( elem );
  447. if ( item ) {
  448. delete item.isIgnored;
  449. }
  450. };
  451. /**
  452. * adds elements to stamps
  453. * @param {NodeList, Array, Element, or String} elems
  454. */
  455. proto.stamp = function( elems ) {
  456. elems = this._find( elems );
  457. if ( !elems ) {
  458. return;
  459. }
  460. this.stamps = this.stamps.concat( elems );
  461. // ignore
  462. elems.forEach( this.ignore, this );
  463. };
  464. /**
  465. * removes elements to stamps
  466. * @param {NodeList, Array, or Element} elems
  467. */
  468. proto.unstamp = function( elems ) {
  469. elems = this._find( elems );
  470. if ( !elems ){
  471. return;
  472. }
  473. elems.forEach( function( elem ) {
  474. // filter out removed stamp elements
  475. utils.removeFrom( this.stamps, elem );
  476. this.unignore( elem );
  477. }, this );
  478. };
  479. /**
  480. * finds child elements
  481. * @param {NodeList, Array, Element, or String} elems
  482. * @returns {Array} elems
  483. */
  484. proto._find = function( elems ) {
  485. if ( !elems ) {
  486. return;
  487. }
  488. // if string, use argument as selector string
  489. if ( typeof elems == 'string' ) {
  490. elems = this.element.querySelectorAll( elems );
  491. }
  492. elems = utils.makeArray( elems );
  493. return elems;
  494. };
  495. proto._manageStamps = function() {
  496. if ( !this.stamps || !this.stamps.length ) {
  497. return;
  498. }
  499. this._getBoundingRect();
  500. this.stamps.forEach( this._manageStamp, this );
  501. };
  502. // update boundingLeft / Top
  503. proto._getBoundingRect = function() {
  504. // get bounding rect for container element
  505. var boundingRect = this.element.getBoundingClientRect();
  506. var size = this.size;
  507. this._boundingRect = {
  508. left: boundingRect.left + size.paddingLeft + size.borderLeftWidth,
  509. top: boundingRect.top + size.paddingTop + size.borderTopWidth,
  510. right: boundingRect.right - ( size.paddingRight + size.borderRightWidth ),
  511. bottom: boundingRect.bottom - ( size.paddingBottom + size.borderBottomWidth )
  512. };
  513. };
  514. /**
  515. * @param {Element} stamp
  516. **/
  517. proto._manageStamp = noop;
  518. /**
  519. * get x/y position of element relative to container element
  520. * @param {Element} elem
  521. * @returns {Object} offset - has left, top, right, bottom
  522. */
  523. proto._getElementOffset = function( elem ) {
  524. var boundingRect = elem.getBoundingClientRect();
  525. var thisRect = this._boundingRect;
  526. var size = getSize( elem );
  527. var offset = {
  528. left: boundingRect.left - thisRect.left - size.marginLeft,
  529. top: boundingRect.top - thisRect.top - size.marginTop,
  530. right: thisRect.right - boundingRect.right - size.marginRight,
  531. bottom: thisRect.bottom - boundingRect.bottom - size.marginBottom
  532. };
  533. return offset;
  534. };
  535. // -------------------------- resize -------------------------- //
  536. // enable event handlers for listeners
  537. // i.e. resize -> onresize
  538. proto.handleEvent = utils.handleEvent;
  539. /**
  540. * Bind layout to window resizing
  541. */
  542. proto.bindResize = function() {
  543. window.addEventListener( 'resize', this );
  544. this.isResizeBound = true;
  545. };
  546. /**
  547. * Unbind layout to window resizing
  548. */
  549. proto.unbindResize = function() {
  550. window.removeEventListener( 'resize', this );
  551. this.isResizeBound = false;
  552. };
  553. proto.onresize = function() {
  554. this.resize();
  555. };
  556. utils.debounceMethod( Outlayer, 'onresize', 100 );
  557. proto.resize = function() {
  558. // don't trigger if size did not change
  559. // or if resize was unbound. See #9
  560. if ( !this.isResizeBound || !this.needsResizeLayout() ) {
  561. return;
  562. }
  563. this.layout();
  564. };
  565. /**
  566. * check if layout is needed post layout
  567. * @returns Boolean
  568. */
  569. proto.needsResizeLayout = function() {
  570. var size = getSize( this.element );
  571. // check that this.size and size are there
  572. // IE8 triggers resize on body size change, so they might not be
  573. var hasSizes = this.size && size;
  574. return hasSizes && size.innerWidth !== this.size.innerWidth;
  575. };
  576. // -------------------------- methods -------------------------- //
  577. /**
  578. * add items to Outlayer instance
  579. * @param {Array or NodeList or Element} elems
  580. * @returns {Array} items - Outlayer.Items
  581. **/
  582. proto.addItems = function( elems ) {
  583. var items = this._itemize( elems );
  584. // add items to collection
  585. if ( items.length ) {
  586. this.items = this.items.concat( items );
  587. }
  588. return items;
  589. };
  590. /**
  591. * Layout newly-appended item elements
  592. * @param {Array or NodeList or Element} elems
  593. */
  594. proto.appended = function( elems ) {
  595. var items = this.addItems( elems );
  596. if ( !items.length ) {
  597. return;
  598. }
  599. // layout and reveal just the new items
  600. this.layoutItems( items, true );
  601. this.reveal( items );
  602. };
  603. /**
  604. * Layout prepended elements
  605. * @param {Array or NodeList or Element} elems
  606. */
  607. proto.prepended = function( elems ) {
  608. var items = this._itemize( elems );
  609. if ( !items.length ) {
  610. return;
  611. }
  612. // add items to beginning of collection
  613. var previousItems = this.items.slice(0);
  614. this.items = items.concat( previousItems );
  615. // start new layout
  616. this._resetLayout();
  617. this._manageStamps();
  618. // layout new stuff without transition
  619. this.layoutItems( items, true );
  620. this.reveal( items );
  621. // layout previous items
  622. this.layoutItems( previousItems );
  623. };
  624. /**
  625. * reveal a collection of items
  626. * @param {Array of Outlayer.Items} items
  627. */
  628. proto.reveal = function( items ) {
  629. this._emitCompleteOnItems( 'reveal', items );
  630. if ( !items || !items.length ) {
  631. return;
  632. }
  633. var stagger = this.updateStagger();
  634. items.forEach( function( item, i ) {
  635. item.stagger( i * stagger );
  636. item.reveal();
  637. });
  638. };
  639. /**
  640. * hide a collection of items
  641. * @param {Array of Outlayer.Items} items
  642. */
  643. proto.hide = function( items ) {
  644. this._emitCompleteOnItems( 'hide', items );
  645. if ( !items || !items.length ) {
  646. return;
  647. }
  648. var stagger = this.updateStagger();
  649. items.forEach( function( item, i ) {
  650. item.stagger( i * stagger );
  651. item.hide();
  652. });
  653. };
  654. /**
  655. * reveal item elements
  656. * @param {Array}, {Element}, {NodeList} items
  657. */
  658. proto.revealItemElements = function( elems ) {
  659. var items = this.getItems( elems );
  660. this.reveal( items );
  661. };
  662. /**
  663. * hide item elements
  664. * @param {Array}, {Element}, {NodeList} items
  665. */
  666. proto.hideItemElements = function( elems ) {
  667. var items = this.getItems( elems );
  668. this.hide( items );
  669. };
  670. /**
  671. * get Outlayer.Item, given an Element
  672. * @param {Element} elem
  673. * @param {Function} callback
  674. * @returns {Outlayer.Item} item
  675. */
  676. proto.getItem = function( elem ) {
  677. // loop through items to get the one that matches
  678. for ( var i=0; i < this.items.length; i++ ) {
  679. var item = this.items[i];
  680. if ( item.element == elem ) {
  681. // return item
  682. return item;
  683. }
  684. }
  685. };
  686. /**
  687. * get collection of Outlayer.Items, given Elements
  688. * @param {Array} elems
  689. * @returns {Array} items - Outlayer.Items
  690. */
  691. proto.getItems = function( elems ) {
  692. elems = utils.makeArray( elems );
  693. var items = [];
  694. elems.forEach( function( elem ) {
  695. var item = this.getItem( elem );
  696. if ( item ) {
  697. items.push( item );
  698. }
  699. }, this );
  700. return items;
  701. };
  702. /**
  703. * remove element(s) from instance and DOM
  704. * @param {Array or NodeList or Element} elems
  705. */
  706. proto.remove = function( elems ) {
  707. var removeItems = this.getItems( elems );
  708. this._emitCompleteOnItems( 'remove', removeItems );
  709. // bail if no items to remove
  710. if ( !removeItems || !removeItems.length ) {
  711. return;
  712. }
  713. removeItems.forEach( function( item ) {
  714. item.remove();
  715. // remove item from collection
  716. utils.removeFrom( this.items, item );
  717. }, this );
  718. };
  719. // ----- destroy ----- //
  720. // remove and disable Outlayer instance
  721. proto.destroy = function() {
  722. // clean up dynamic styles
  723. var style = this.element.style;
  724. style.height = '';
  725. style.position = '';
  726. style.width = '';
  727. // destroy items
  728. this.items.forEach( function( item ) {
  729. item.destroy();
  730. });
  731. this.unbindResize();
  732. var id = this.element.outlayerGUID;
  733. delete instances[ id ]; // remove reference to instance by id
  734. delete this.element.outlayerGUID;
  735. // remove data for jQuery
  736. if ( jQuery ) {
  737. jQuery.removeData( this.element, this.constructor.namespace );
  738. }
  739. };
  740. // -------------------------- data -------------------------- //
  741. /**
  742. * get Outlayer instance from element
  743. * @param {Element} elem
  744. * @returns {Outlayer}
  745. */
  746. Outlayer.data = function( elem ) {
  747. elem = utils.getQueryElement( elem );
  748. var id = elem && elem.outlayerGUID;
  749. return id && instances[ id ];
  750. };
  751. // -------------------------- create Outlayer class -------------------------- //
  752. /**
  753. * create a layout class
  754. * @param {String} namespace
  755. */
  756. Outlayer.create = function( namespace, options ) {
  757. // sub-class Outlayer
  758. var Layout = subclass( Outlayer );
  759. // apply new options and compatOptions
  760. Layout.defaults = utils.extend( {}, Outlayer.defaults );
  761. utils.extend( Layout.defaults, options );
  762. Layout.compatOptions = utils.extend( {}, Outlayer.compatOptions );
  763. Layout.namespace = namespace;
  764. Layout.data = Outlayer.data;
  765. // sub-class Item
  766. Layout.Item = subclass( Item );
  767. // -------------------------- declarative -------------------------- //
  768. utils.htmlInit( Layout, namespace );
  769. // -------------------------- jQuery bridge -------------------------- //
  770. // make into jQuery plugin
  771. if ( jQuery && jQuery.bridget ) {
  772. jQuery.bridget( namespace, Layout );
  773. }
  774. return Layout;
  775. };
  776. function subclass( Parent ) {
  777. function SubClass() {
  778. Parent.apply( this, arguments );
  779. }
  780. SubClass.prototype = Object.create( Parent.prototype );
  781. SubClass.prototype.constructor = SubClass;
  782. return SubClass;
  783. }
  784. // ----- helpers ----- //
  785. // how many milliseconds are in each unit
  786. var msUnits = {
  787. ms: 1,
  788. s: 1000
  789. };
  790. // munge time-like parameter into millisecond number
  791. // '0.4s' -> 40
  792. function getMilliseconds( time ) {
  793. if ( typeof time == 'number' ) {
  794. return time;
  795. }
  796. var matches = time.match( /(^\d*\.?\d*)(\w*)/ );
  797. var num = matches && matches[1];
  798. var unit = matches && matches[2];
  799. if ( !num.length ) {
  800. return 0;
  801. }
  802. num = parseFloat( num );
  803. var mult = msUnits[ unit ] || 1;
  804. return num * mult;
  805. }
  806. // ----- fin ----- //
  807. // back in global
  808. Outlayer.Item = Item;
  809. return Outlayer;
  810. }));