masonry.pkgd.js 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504
  1. /*!
  2. * Masonry PACKAGED v4.2.2
  3. * Cascading grid layout library
  4. * https://masonry.desandro.com
  5. * MIT License
  6. * by David DeSandro
  7. */
  8. /**
  9. * Bridget makes jQuery widgets
  10. * v2.0.1
  11. * MIT license
  12. */
  13. /* jshint browser: true, strict: true, undef: true, unused: true */
  14. ( function( window, factory ) {
  15. // universal module definition
  16. /*jshint strict: false */ /* globals define, module, require */
  17. if ( typeof define == 'function' && define.amd ) {
  18. // AMD
  19. define( 'jquery-bridget/jquery-bridget',[ 'jquery' ], function( jQuery ) {
  20. return factory( window, jQuery );
  21. });
  22. } else if ( typeof module == 'object' && module.exports ) {
  23. // CommonJS
  24. module.exports = factory(
  25. window,
  26. require('jquery')
  27. );
  28. } else {
  29. // browser global
  30. window.jQueryBridget = factory(
  31. window,
  32. window.jQuery
  33. );
  34. }
  35. }( window, function factory( window, jQuery ) {
  36. 'use strict';
  37. // ----- utils ----- //
  38. var arraySlice = Array.prototype.slice;
  39. // helper function for logging errors
  40. // $.error breaks jQuery chaining
  41. var console = window.console;
  42. var logError = typeof console == 'undefined' ? function() {} :
  43. function( message ) {
  44. console.error( message );
  45. };
  46. // ----- jQueryBridget ----- //
  47. function jQueryBridget( namespace, PluginClass, $ ) {
  48. $ = $ || jQuery || window.jQuery;
  49. if ( !$ ) {
  50. return;
  51. }
  52. // add option method -> $().plugin('option', {...})
  53. if ( !PluginClass.prototype.option ) {
  54. // option setter
  55. PluginClass.prototype.option = function( opts ) {
  56. // bail out if not an object
  57. if ( !$.isPlainObject( opts ) ){
  58. return;
  59. }
  60. this.options = $.extend( true, this.options, opts );
  61. };
  62. }
  63. // make jQuery plugin
  64. $.fn[ namespace ] = function( arg0 /*, arg1 */ ) {
  65. if ( typeof arg0 == 'string' ) {
  66. // method call $().plugin( 'methodName', { options } )
  67. // shift arguments by 1
  68. var args = arraySlice.call( arguments, 1 );
  69. return methodCall( this, arg0, args );
  70. }
  71. // just $().plugin({ options })
  72. plainCall( this, arg0 );
  73. return this;
  74. };
  75. // $().plugin('methodName')
  76. function methodCall( $elems, methodName, args ) {
  77. var returnValue;
  78. var pluginMethodStr = '$().' + namespace + '("' + methodName + '")';
  79. $elems.each( function( i, elem ) {
  80. // get instance
  81. var instance = $.data( elem, namespace );
  82. if ( !instance ) {
  83. logError( namespace + ' not initialized. Cannot call methods, i.e. ' +
  84. pluginMethodStr );
  85. return;
  86. }
  87. var method = instance[ methodName ];
  88. if ( !method || methodName.charAt(0) == '_' ) {
  89. logError( pluginMethodStr + ' is not a valid method' );
  90. return;
  91. }
  92. // apply method, get return value
  93. var value = method.apply( instance, args );
  94. // set return value if value is returned, use only first value
  95. returnValue = returnValue === undefined ? value : returnValue;
  96. });
  97. return returnValue !== undefined ? returnValue : $elems;
  98. }
  99. function plainCall( $elems, options ) {
  100. $elems.each( function( i, elem ) {
  101. var instance = $.data( elem, namespace );
  102. if ( instance ) {
  103. // set options & init
  104. instance.option( options );
  105. instance._init();
  106. } else {
  107. // initialize new instance
  108. instance = new PluginClass( elem, options );
  109. $.data( elem, namespace, instance );
  110. }
  111. });
  112. }
  113. updateJQuery( $ );
  114. }
  115. // ----- updateJQuery ----- //
  116. // set $.bridget for v1 backwards compatibility
  117. function updateJQuery( $ ) {
  118. if ( !$ || ( $ && $.bridget ) ) {
  119. return;
  120. }
  121. $.bridget = jQueryBridget;
  122. }
  123. updateJQuery( jQuery || window.jQuery );
  124. // ----- ----- //
  125. return jQueryBridget;
  126. }));
  127. /**
  128. * EvEmitter v1.1.0
  129. * Lil' event emitter
  130. * MIT License
  131. */
  132. /* jshint unused: true, undef: true, strict: true */
  133. ( function( global, factory ) {
  134. // universal module definition
  135. /* jshint strict: false */ /* globals define, module, window */
  136. if ( typeof define == 'function' && define.amd ) {
  137. // AMD - RequireJS
  138. define( 'ev-emitter/ev-emitter',factory );
  139. } else if ( typeof module == 'object' && module.exports ) {
  140. // CommonJS - Browserify, Webpack
  141. module.exports = factory();
  142. } else {
  143. // Browser globals
  144. global.EvEmitter = factory();
  145. }
  146. }( typeof window != 'undefined' ? window : this, function() {
  147. function EvEmitter() {}
  148. var proto = EvEmitter.prototype;
  149. proto.on = function( eventName, listener ) {
  150. if ( !eventName || !listener ) {
  151. return;
  152. }
  153. // set events hash
  154. var events = this._events = this._events || {};
  155. // set listeners array
  156. var listeners = events[ eventName ] = events[ eventName ] || [];
  157. // only add once
  158. if ( listeners.indexOf( listener ) == -1 ) {
  159. listeners.push( listener );
  160. }
  161. return this;
  162. };
  163. proto.once = function( eventName, listener ) {
  164. if ( !eventName || !listener ) {
  165. return;
  166. }
  167. // add event
  168. this.on( eventName, listener );
  169. // set once flag
  170. // set onceEvents hash
  171. var onceEvents = this._onceEvents = this._onceEvents || {};
  172. // set onceListeners object
  173. var onceListeners = onceEvents[ eventName ] = onceEvents[ eventName ] || {};
  174. // set flag
  175. onceListeners[ listener ] = true;
  176. return this;
  177. };
  178. proto.off = function( eventName, listener ) {
  179. var listeners = this._events && this._events[ eventName ];
  180. if ( !listeners || !listeners.length ) {
  181. return;
  182. }
  183. var index = listeners.indexOf( listener );
  184. if ( index != -1 ) {
  185. listeners.splice( index, 1 );
  186. }
  187. return this;
  188. };
  189. proto.emitEvent = function( eventName, args ) {
  190. var listeners = this._events && this._events[ eventName ];
  191. if ( !listeners || !listeners.length ) {
  192. return;
  193. }
  194. // copy over to avoid interference if .off() in listener
  195. listeners = listeners.slice(0);
  196. args = args || [];
  197. // once stuff
  198. var onceListeners = this._onceEvents && this._onceEvents[ eventName ];
  199. for ( var i=0; i < listeners.length; i++ ) {
  200. var listener = listeners[i]
  201. var isOnce = onceListeners && onceListeners[ listener ];
  202. if ( isOnce ) {
  203. // remove listener
  204. // remove before trigger to prevent recursion
  205. this.off( eventName, listener );
  206. // unset once flag
  207. delete onceListeners[ listener ];
  208. }
  209. // trigger listener
  210. listener.apply( this, args );
  211. }
  212. return this;
  213. };
  214. proto.allOff = function() {
  215. delete this._events;
  216. delete this._onceEvents;
  217. };
  218. return EvEmitter;
  219. }));
  220. /*!
  221. * getSize v2.0.3
  222. * measure size of elements
  223. * MIT license
  224. */
  225. /* jshint browser: true, strict: true, undef: true, unused: true */
  226. /* globals console: false */
  227. ( function( window, factory ) {
  228. /* jshint strict: false */ /* globals define, module */
  229. if ( typeof define == 'function' && define.amd ) {
  230. // AMD
  231. define( 'get-size/get-size',factory );
  232. } else if ( typeof module == 'object' && module.exports ) {
  233. // CommonJS
  234. module.exports = factory();
  235. } else {
  236. // browser global
  237. window.getSize = factory();
  238. }
  239. })( window, function factory() {
  240. 'use strict';
  241. // -------------------------- helpers -------------------------- //
  242. // get a number from a string, not a percentage
  243. function getStyleSize( value ) {
  244. var num = parseFloat( value );
  245. // not a percent like '100%', and a number
  246. var isValid = value.indexOf('%') == -1 && !isNaN( num );
  247. return isValid && num;
  248. }
  249. function noop() {}
  250. var logError = typeof console == 'undefined' ? noop :
  251. function( message ) {
  252. console.error( message );
  253. };
  254. // -------------------------- measurements -------------------------- //
  255. var measurements = [
  256. 'paddingLeft',
  257. 'paddingRight',
  258. 'paddingTop',
  259. 'paddingBottom',
  260. 'marginLeft',
  261. 'marginRight',
  262. 'marginTop',
  263. 'marginBottom',
  264. 'borderLeftWidth',
  265. 'borderRightWidth',
  266. 'borderTopWidth',
  267. 'borderBottomWidth'
  268. ];
  269. var measurementsLength = measurements.length;
  270. function getZeroSize() {
  271. var size = {
  272. width: 0,
  273. height: 0,
  274. innerWidth: 0,
  275. innerHeight: 0,
  276. outerWidth: 0,
  277. outerHeight: 0
  278. };
  279. for ( var i=0; i < measurementsLength; i++ ) {
  280. var measurement = measurements[i];
  281. size[ measurement ] = 0;
  282. }
  283. return size;
  284. }
  285. // -------------------------- getStyle -------------------------- //
  286. /**
  287. * getStyle, get style of element, check for Firefox bug
  288. * https://bugzilla.mozilla.org/show_bug.cgi?id=548397
  289. */
  290. function getStyle( elem ) {
  291. var style = getComputedStyle( elem );
  292. if ( !style ) {
  293. logError( 'Style returned ' + style +
  294. '. Are you running this code in a hidden iframe on Firefox? ' +
  295. 'See https://bit.ly/getsizebug1' );
  296. }
  297. return style;
  298. }
  299. // -------------------------- setup -------------------------- //
  300. var isSetup = false;
  301. var isBoxSizeOuter;
  302. /**
  303. * setup
  304. * check isBoxSizerOuter
  305. * do on first getSize() rather than on page load for Firefox bug
  306. */
  307. function setup() {
  308. // setup once
  309. if ( isSetup ) {
  310. return;
  311. }
  312. isSetup = true;
  313. // -------------------------- box sizing -------------------------- //
  314. /**
  315. * Chrome & Safari measure the outer-width on style.width on border-box elems
  316. * IE11 & Firefox<29 measures the inner-width
  317. */
  318. var div = document.createElement('div');
  319. div.style.width = '200px';
  320. div.style.padding = '1px 2px 3px 4px';
  321. div.style.borderStyle = 'solid';
  322. div.style.borderWidth = '1px 2px 3px 4px';
  323. div.style.boxSizing = 'border-box';
  324. var body = document.body || document.documentElement;
  325. body.appendChild( div );
  326. var style = getStyle( div );
  327. // round value for browser zoom. desandro/masonry#928
  328. isBoxSizeOuter = Math.round( getStyleSize( style.width ) ) == 200;
  329. getSize.isBoxSizeOuter = isBoxSizeOuter;
  330. body.removeChild( div );
  331. }
  332. // -------------------------- getSize -------------------------- //
  333. function getSize( elem ) {
  334. setup();
  335. // use querySeletor if elem is string
  336. if ( typeof elem == 'string' ) {
  337. elem = document.querySelector( elem );
  338. }
  339. // do not proceed on non-objects
  340. if ( !elem || typeof elem != 'object' || !elem.nodeType ) {
  341. return;
  342. }
  343. var style = getStyle( elem );
  344. // if hidden, everything is 0
  345. if ( style.display == 'none' ) {
  346. return getZeroSize();
  347. }
  348. var size = {};
  349. size.width = elem.offsetWidth;
  350. size.height = elem.offsetHeight;
  351. var isBorderBox = size.isBorderBox = style.boxSizing == 'border-box';
  352. // get all measurements
  353. for ( var i=0; i < measurementsLength; i++ ) {
  354. var measurement = measurements[i];
  355. var value = style[ measurement ];
  356. var num = parseFloat( value );
  357. // any 'auto', 'medium' value will be 0
  358. size[ measurement ] = !isNaN( num ) ? num : 0;
  359. }
  360. var paddingWidth = size.paddingLeft + size.paddingRight;
  361. var paddingHeight = size.paddingTop + size.paddingBottom;
  362. var marginWidth = size.marginLeft + size.marginRight;
  363. var marginHeight = size.marginTop + size.marginBottom;
  364. var borderWidth = size.borderLeftWidth + size.borderRightWidth;
  365. var borderHeight = size.borderTopWidth + size.borderBottomWidth;
  366. var isBorderBoxSizeOuter = isBorderBox && isBoxSizeOuter;
  367. // overwrite width and height if we can get it from style
  368. var styleWidth = getStyleSize( style.width );
  369. if ( styleWidth !== false ) {
  370. size.width = styleWidth +
  371. // add padding and border unless it's already including it
  372. ( isBorderBoxSizeOuter ? 0 : paddingWidth + borderWidth );
  373. }
  374. var styleHeight = getStyleSize( style.height );
  375. if ( styleHeight !== false ) {
  376. size.height = styleHeight +
  377. // add padding and border unless it's already including it
  378. ( isBorderBoxSizeOuter ? 0 : paddingHeight + borderHeight );
  379. }
  380. size.innerWidth = size.width - ( paddingWidth + borderWidth );
  381. size.innerHeight = size.height - ( paddingHeight + borderHeight );
  382. size.outerWidth = size.width + marginWidth;
  383. size.outerHeight = size.height + marginHeight;
  384. return size;
  385. }
  386. return getSize;
  387. });
  388. /**
  389. * matchesSelector v2.0.2
  390. * matchesSelector( element, '.selector' )
  391. * MIT license
  392. */
  393. /*jshint browser: true, strict: true, undef: true, unused: true */
  394. ( function( window, factory ) {
  395. /*global define: false, module: false */
  396. 'use strict';
  397. // universal module definition
  398. if ( typeof define == 'function' && define.amd ) {
  399. // AMD
  400. define( 'desandro-matches-selector/matches-selector',factory );
  401. } else if ( typeof module == 'object' && module.exports ) {
  402. // CommonJS
  403. module.exports = factory();
  404. } else {
  405. // browser global
  406. window.matchesSelector = factory();
  407. }
  408. }( window, function factory() {
  409. 'use strict';
  410. var matchesMethod = ( function() {
  411. var ElemProto = window.Element.prototype;
  412. // check for the standard method name first
  413. if ( ElemProto.matches ) {
  414. return 'matches';
  415. }
  416. // check un-prefixed
  417. if ( ElemProto.matchesSelector ) {
  418. return 'matchesSelector';
  419. }
  420. // check vendor prefixes
  421. var prefixes = [ 'webkit', 'moz', 'ms', 'o' ];
  422. for ( var i=0; i < prefixes.length; i++ ) {
  423. var prefix = prefixes[i];
  424. var method = prefix + 'MatchesSelector';
  425. if ( ElemProto[ method ] ) {
  426. return method;
  427. }
  428. }
  429. })();
  430. return function matchesSelector( elem, selector ) {
  431. return elem[ matchesMethod ]( selector );
  432. };
  433. }));
  434. /**
  435. * Fizzy UI utils v2.0.7
  436. * MIT license
  437. */
  438. /*jshint browser: true, undef: true, unused: true, strict: true */
  439. ( function( window, factory ) {
  440. // universal module definition
  441. /*jshint strict: false */ /*globals define, module, require */
  442. if ( typeof define == 'function' && define.amd ) {
  443. // AMD
  444. define( 'fizzy-ui-utils/utils',[
  445. 'desandro-matches-selector/matches-selector'
  446. ], function( matchesSelector ) {
  447. return factory( window, matchesSelector );
  448. });
  449. } else if ( typeof module == 'object' && module.exports ) {
  450. // CommonJS
  451. module.exports = factory(
  452. window,
  453. require('desandro-matches-selector')
  454. );
  455. } else {
  456. // browser global
  457. window.fizzyUIUtils = factory(
  458. window,
  459. window.matchesSelector
  460. );
  461. }
  462. }( window, function factory( window, matchesSelector ) {
  463. var utils = {};
  464. // ----- extend ----- //
  465. // extends objects
  466. utils.extend = function( a, b ) {
  467. for ( var prop in b ) {
  468. a[ prop ] = b[ prop ];
  469. }
  470. return a;
  471. };
  472. // ----- modulo ----- //
  473. utils.modulo = function( num, div ) {
  474. return ( ( num % div ) + div ) % div;
  475. };
  476. // ----- makeArray ----- //
  477. var arraySlice = Array.prototype.slice;
  478. // turn element or nodeList into an array
  479. utils.makeArray = function( obj ) {
  480. if ( Array.isArray( obj ) ) {
  481. // use object if already an array
  482. return obj;
  483. }
  484. // return empty array if undefined or null. #6
  485. if ( obj === null || obj === undefined ) {
  486. return [];
  487. }
  488. var isArrayLike = typeof obj == 'object' && typeof obj.length == 'number';
  489. if ( isArrayLike ) {
  490. // convert nodeList to array
  491. return arraySlice.call( obj );
  492. }
  493. // array of single index
  494. return [ obj ];
  495. };
  496. // ----- removeFrom ----- //
  497. utils.removeFrom = function( ary, obj ) {
  498. var index = ary.indexOf( obj );
  499. if ( index != -1 ) {
  500. ary.splice( index, 1 );
  501. }
  502. };
  503. // ----- getParent ----- //
  504. utils.getParent = function( elem, selector ) {
  505. while ( elem.parentNode && elem != document.body ) {
  506. elem = elem.parentNode;
  507. if ( matchesSelector( elem, selector ) ) {
  508. return elem;
  509. }
  510. }
  511. };
  512. // ----- getQueryElement ----- //
  513. // use element as selector string
  514. utils.getQueryElement = function( elem ) {
  515. if ( typeof elem == 'string' ) {
  516. return document.querySelector( elem );
  517. }
  518. return elem;
  519. };
  520. // ----- handleEvent ----- //
  521. // enable .ontype to trigger from .addEventListener( elem, 'type' )
  522. utils.handleEvent = function( event ) {
  523. var method = 'on' + event.type;
  524. if ( this[ method ] ) {
  525. this[ method ]( event );
  526. }
  527. };
  528. // ----- filterFindElements ----- //
  529. utils.filterFindElements = function( elems, selector ) {
  530. // make array of elems
  531. elems = utils.makeArray( elems );
  532. var ffElems = [];
  533. elems.forEach( function( elem ) {
  534. // check that elem is an actual element
  535. if ( !( elem instanceof HTMLElement ) ) {
  536. return;
  537. }
  538. // add elem if no selector
  539. if ( !selector ) {
  540. ffElems.push( elem );
  541. return;
  542. }
  543. // filter & find items if we have a selector
  544. // filter
  545. if ( matchesSelector( elem, selector ) ) {
  546. ffElems.push( elem );
  547. }
  548. // find children
  549. var childElems = elem.querySelectorAll( selector );
  550. // concat childElems to filterFound array
  551. for ( var i=0; i < childElems.length; i++ ) {
  552. ffElems.push( childElems[i] );
  553. }
  554. });
  555. return ffElems;
  556. };
  557. // ----- debounceMethod ----- //
  558. utils.debounceMethod = function( _class, methodName, threshold ) {
  559. threshold = threshold || 100;
  560. // original method
  561. var method = _class.prototype[ methodName ];
  562. var timeoutName = methodName + 'Timeout';
  563. _class.prototype[ methodName ] = function() {
  564. var timeout = this[ timeoutName ];
  565. clearTimeout( timeout );
  566. var args = arguments;
  567. var _this = this;
  568. this[ timeoutName ] = setTimeout( function() {
  569. method.apply( _this, args );
  570. delete _this[ timeoutName ];
  571. }, threshold );
  572. };
  573. };
  574. // ----- docReady ----- //
  575. utils.docReady = function( callback ) {
  576. var readyState = document.readyState;
  577. if ( readyState == 'complete' || readyState == 'interactive' ) {
  578. // do async to allow for other scripts to run. metafizzy/flickity#441
  579. setTimeout( callback );
  580. } else {
  581. document.addEventListener( 'DOMContentLoaded', callback );
  582. }
  583. };
  584. // ----- htmlInit ----- //
  585. // http://jamesroberts.name/blog/2010/02/22/string-functions-for-javascript-trim-to-camel-case-to-dashed-and-to-underscore/
  586. utils.toDashed = function( str ) {
  587. return str.replace( /(.)([A-Z])/g, function( match, $1, $2 ) {
  588. return $1 + '-' + $2;
  589. }).toLowerCase();
  590. };
  591. var console = window.console;
  592. /**
  593. * allow user to initialize classes via [data-namespace] or .js-namespace class
  594. * htmlInit( Widget, 'widgetName' )
  595. * options are parsed from data-namespace-options
  596. */
  597. utils.htmlInit = function( WidgetClass, namespace ) {
  598. utils.docReady( function() {
  599. var dashedNamespace = utils.toDashed( namespace );
  600. var dataAttr = 'data-' + dashedNamespace;
  601. var dataAttrElems = document.querySelectorAll( '[' + dataAttr + ']' );
  602. var jsDashElems = document.querySelectorAll( '.js-' + dashedNamespace );
  603. var elems = utils.makeArray( dataAttrElems )
  604. .concat( utils.makeArray( jsDashElems ) );
  605. var dataOptionsAttr = dataAttr + '-options';
  606. var jQuery = window.jQuery;
  607. elems.forEach( function( elem ) {
  608. var attr = elem.getAttribute( dataAttr ) ||
  609. elem.getAttribute( dataOptionsAttr );
  610. var options;
  611. try {
  612. options = attr && JSON.parse( attr );
  613. } catch ( error ) {
  614. // log error, do not initialize
  615. if ( console ) {
  616. console.error( 'Error parsing ' + dataAttr + ' on ' + elem.className +
  617. ': ' + error );
  618. }
  619. return;
  620. }
  621. // initialize
  622. var instance = new WidgetClass( elem, options );
  623. // make available via $().data('namespace')
  624. if ( jQuery ) {
  625. jQuery.data( elem, namespace, instance );
  626. }
  627. });
  628. });
  629. };
  630. // ----- ----- //
  631. return utils;
  632. }));
  633. /**
  634. * Outlayer Item
  635. */
  636. ( function( window, factory ) {
  637. // universal module definition
  638. /* jshint strict: false */ /* globals define, module, require */
  639. if ( typeof define == 'function' && define.amd ) {
  640. // AMD - RequireJS
  641. define( 'outlayer/item',[
  642. 'ev-emitter/ev-emitter',
  643. 'get-size/get-size'
  644. ],
  645. factory
  646. );
  647. } else if ( typeof module == 'object' && module.exports ) {
  648. // CommonJS - Browserify, Webpack
  649. module.exports = factory(
  650. require('ev-emitter'),
  651. require('get-size')
  652. );
  653. } else {
  654. // browser global
  655. window.Outlayer = {};
  656. window.Outlayer.Item = factory(
  657. window.EvEmitter,
  658. window.getSize
  659. );
  660. }
  661. }( window, function factory( EvEmitter, getSize ) {
  662. 'use strict';
  663. // ----- helpers ----- //
  664. function isEmptyObj( obj ) {
  665. for ( var prop in obj ) {
  666. return false;
  667. }
  668. prop = null;
  669. return true;
  670. }
  671. // -------------------------- CSS3 support -------------------------- //
  672. var docElemStyle = document.documentElement.style;
  673. var transitionProperty = typeof docElemStyle.transition == 'string' ?
  674. 'transition' : 'WebkitTransition';
  675. var transformProperty = typeof docElemStyle.transform == 'string' ?
  676. 'transform' : 'WebkitTransform';
  677. var transitionEndEvent = {
  678. WebkitTransition: 'webkitTransitionEnd',
  679. transition: 'transitionend'
  680. }[ transitionProperty ];
  681. // cache all vendor properties that could have vendor prefix
  682. var vendorProperties = {
  683. transform: transformProperty,
  684. transition: transitionProperty,
  685. transitionDuration: transitionProperty + 'Duration',
  686. transitionProperty: transitionProperty + 'Property',
  687. transitionDelay: transitionProperty + 'Delay'
  688. };
  689. // -------------------------- Item -------------------------- //
  690. function Item( element, layout ) {
  691. if ( !element ) {
  692. return;
  693. }
  694. this.element = element;
  695. // parent layout class, i.e. Masonry, Isotope, or Packery
  696. this.layout = layout;
  697. this.position = {
  698. x: 0,
  699. y: 0
  700. };
  701. this._create();
  702. }
  703. // inherit EvEmitter
  704. var proto = Item.prototype = Object.create( EvEmitter.prototype );
  705. proto.constructor = Item;
  706. proto._create = function() {
  707. // transition objects
  708. this._transn = {
  709. ingProperties: {},
  710. clean: {},
  711. onEnd: {}
  712. };
  713. this.css({
  714. position: 'absolute'
  715. });
  716. };
  717. // trigger specified handler for event type
  718. proto.handleEvent = function( event ) {
  719. var method = 'on' + event.type;
  720. if ( this[ method ] ) {
  721. this[ method ]( event );
  722. }
  723. };
  724. proto.getSize = function() {
  725. this.size = getSize( this.element );
  726. };
  727. /**
  728. * apply CSS styles to element
  729. * @param {Object} style
  730. */
  731. proto.css = function( style ) {
  732. var elemStyle = this.element.style;
  733. for ( var prop in style ) {
  734. // use vendor property if available
  735. var supportedProp = vendorProperties[ prop ] || prop;
  736. elemStyle[ supportedProp ] = style[ prop ];
  737. }
  738. };
  739. // measure position, and sets it
  740. proto.getPosition = function() {
  741. var style = getComputedStyle( this.element );
  742. var isOriginLeft = this.layout._getOption('originLeft');
  743. var isOriginTop = this.layout._getOption('originTop');
  744. var xValue = style[ isOriginLeft ? 'left' : 'right' ];
  745. var yValue = style[ isOriginTop ? 'top' : 'bottom' ];
  746. var x = parseFloat( xValue );
  747. var y = parseFloat( yValue );
  748. // convert percent to pixels
  749. var layoutSize = this.layout.size;
  750. if ( xValue.indexOf('%') != -1 ) {
  751. x = ( x / 100 ) * layoutSize.width;
  752. }
  753. if ( yValue.indexOf('%') != -1 ) {
  754. y = ( y / 100 ) * layoutSize.height;
  755. }
  756. // clean up 'auto' or other non-integer values
  757. x = isNaN( x ) ? 0 : x;
  758. y = isNaN( y ) ? 0 : y;
  759. // remove padding from measurement
  760. x -= isOriginLeft ? layoutSize.paddingLeft : layoutSize.paddingRight;
  761. y -= isOriginTop ? layoutSize.paddingTop : layoutSize.paddingBottom;
  762. this.position.x = x;
  763. this.position.y = y;
  764. };
  765. // set settled position, apply padding
  766. proto.layoutPosition = function() {
  767. var layoutSize = this.layout.size;
  768. var style = {};
  769. var isOriginLeft = this.layout._getOption('originLeft');
  770. var isOriginTop = this.layout._getOption('originTop');
  771. // x
  772. var xPadding = isOriginLeft ? 'paddingLeft' : 'paddingRight';
  773. var xProperty = isOriginLeft ? 'left' : 'right';
  774. var xResetProperty = isOriginLeft ? 'right' : 'left';
  775. var x = this.position.x + layoutSize[ xPadding ];
  776. // set in percentage or pixels
  777. style[ xProperty ] = this.getXValue( x );
  778. // reset other property
  779. style[ xResetProperty ] = '';
  780. // y
  781. var yPadding = isOriginTop ? 'paddingTop' : 'paddingBottom';
  782. var yProperty = isOriginTop ? 'top' : 'bottom';
  783. var yResetProperty = isOriginTop ? 'bottom' : 'top';
  784. var y = this.position.y + layoutSize[ yPadding ];
  785. // set in percentage or pixels
  786. style[ yProperty ] = this.getYValue( y );
  787. // reset other property
  788. style[ yResetProperty ] = '';
  789. this.css( style );
  790. this.emitEvent( 'layout', [ this ] );
  791. };
  792. proto.getXValue = function( x ) {
  793. var isHorizontal = this.layout._getOption('horizontal');
  794. return this.layout.options.percentPosition && !isHorizontal ?
  795. ( ( x / this.layout.size.width ) * 100 ) + '%' : x + 'px';
  796. };
  797. proto.getYValue = function( y ) {
  798. var isHorizontal = this.layout._getOption('horizontal');
  799. return this.layout.options.percentPosition && isHorizontal ?
  800. ( ( y / this.layout.size.height ) * 100 ) + '%' : y + 'px';
  801. };
  802. proto._transitionTo = function( x, y ) {
  803. this.getPosition();
  804. // get current x & y from top/left
  805. var curX = this.position.x;
  806. var curY = this.position.y;
  807. var didNotMove = x == this.position.x && y == this.position.y;
  808. // save end position
  809. this.setPosition( x, y );
  810. // if did not move and not transitioning, just go to layout
  811. if ( didNotMove && !this.isTransitioning ) {
  812. this.layoutPosition();
  813. return;
  814. }
  815. var transX = x - curX;
  816. var transY = y - curY;
  817. var transitionStyle = {};
  818. transitionStyle.transform = this.getTranslate( transX, transY );
  819. this.transition({
  820. to: transitionStyle,
  821. onTransitionEnd: {
  822. transform: this.layoutPosition
  823. },
  824. isCleaning: true
  825. });
  826. };
  827. proto.getTranslate = function( x, y ) {
  828. // flip cooridinates if origin on right or bottom
  829. var isOriginLeft = this.layout._getOption('originLeft');
  830. var isOriginTop = this.layout._getOption('originTop');
  831. x = isOriginLeft ? x : -x;
  832. y = isOriginTop ? y : -y;
  833. return 'translate3d(' + x + 'px, ' + y + 'px, 0)';
  834. };
  835. // non transition + transform support
  836. proto.goTo = function( x, y ) {
  837. this.setPosition( x, y );
  838. this.layoutPosition();
  839. };
  840. proto.moveTo = proto._transitionTo;
  841. proto.setPosition = function( x, y ) {
  842. this.position.x = parseFloat( x );
  843. this.position.y = parseFloat( y );
  844. };
  845. // ----- transition ----- //
  846. /**
  847. * @param {Object} style - CSS
  848. * @param {Function} onTransitionEnd
  849. */
  850. // non transition, just trigger callback
  851. proto._nonTransition = function( args ) {
  852. this.css( args.to );
  853. if ( args.isCleaning ) {
  854. this._removeStyles( args.to );
  855. }
  856. for ( var prop in args.onTransitionEnd ) {
  857. args.onTransitionEnd[ prop ].call( this );
  858. }
  859. };
  860. /**
  861. * proper transition
  862. * @param {Object} args - arguments
  863. * @param {Object} to - style to transition to
  864. * @param {Object} from - style to start transition from
  865. * @param {Boolean} isCleaning - removes transition styles after transition
  866. * @param {Function} onTransitionEnd - callback
  867. */
  868. proto.transition = function( args ) {
  869. // redirect to nonTransition if no transition duration
  870. if ( !parseFloat( this.layout.options.transitionDuration ) ) {
  871. this._nonTransition( args );
  872. return;
  873. }
  874. var _transition = this._transn;
  875. // keep track of onTransitionEnd callback by css property
  876. for ( var prop in args.onTransitionEnd ) {
  877. _transition.onEnd[ prop ] = args.onTransitionEnd[ prop ];
  878. }
  879. // keep track of properties that are transitioning
  880. for ( prop in args.to ) {
  881. _transition.ingProperties[ prop ] = true;
  882. // keep track of properties to clean up when transition is done
  883. if ( args.isCleaning ) {
  884. _transition.clean[ prop ] = true;
  885. }
  886. }
  887. // set from styles
  888. if ( args.from ) {
  889. this.css( args.from );
  890. // force redraw. http://blog.alexmaccaw.com/css-transitions
  891. var h = this.element.offsetHeight;
  892. // hack for JSHint to hush about unused var
  893. h = null;
  894. }
  895. // enable transition
  896. this.enableTransition( args.to );
  897. // set styles that are transitioning
  898. this.css( args.to );
  899. this.isTransitioning = true;
  900. };
  901. // dash before all cap letters, including first for
  902. // WebkitTransform => -webkit-transform
  903. function toDashedAll( str ) {
  904. return str.replace( /([A-Z])/g, function( $1 ) {
  905. return '-' + $1.toLowerCase();
  906. });
  907. }
  908. var transitionProps = 'opacity,' + toDashedAll( transformProperty );
  909. proto.enableTransition = function(/* style */) {
  910. // HACK changing transitionProperty during a transition
  911. // will cause transition to jump
  912. if ( this.isTransitioning ) {
  913. return;
  914. }
  915. // make `transition: foo, bar, baz` from style object
  916. // HACK un-comment this when enableTransition can work
  917. // while a transition is happening
  918. // var transitionValues = [];
  919. // for ( var prop in style ) {
  920. // // dash-ify camelCased properties like WebkitTransition
  921. // prop = vendorProperties[ prop ] || prop;
  922. // transitionValues.push( toDashedAll( prop ) );
  923. // }
  924. // munge number to millisecond, to match stagger
  925. var duration = this.layout.options.transitionDuration;
  926. duration = typeof duration == 'number' ? duration + 'ms' : duration;
  927. // enable transition styles
  928. this.css({
  929. transitionProperty: transitionProps,
  930. transitionDuration: duration,
  931. transitionDelay: this.staggerDelay || 0
  932. });
  933. // listen for transition end event
  934. this.element.addEventListener( transitionEndEvent, this, false );
  935. };
  936. // ----- events ----- //
  937. proto.onwebkitTransitionEnd = function( event ) {
  938. this.ontransitionend( event );
  939. };
  940. proto.onotransitionend = function( event ) {
  941. this.ontransitionend( event );
  942. };
  943. // properties that I munge to make my life easier
  944. var dashedVendorProperties = {
  945. '-webkit-transform': 'transform'
  946. };
  947. proto.ontransitionend = function( event ) {
  948. // disregard bubbled events from children
  949. if ( event.target !== this.element ) {
  950. return;
  951. }
  952. var _transition = this._transn;
  953. // get property name of transitioned property, convert to prefix-free
  954. var propertyName = dashedVendorProperties[ event.propertyName ] || event.propertyName;
  955. // remove property that has completed transitioning
  956. delete _transition.ingProperties[ propertyName ];
  957. // check if any properties are still transitioning
  958. if ( isEmptyObj( _transition.ingProperties ) ) {
  959. // all properties have completed transitioning
  960. this.disableTransition();
  961. }
  962. // clean style
  963. if ( propertyName in _transition.clean ) {
  964. // clean up style
  965. this.element.style[ event.propertyName ] = '';
  966. delete _transition.clean[ propertyName ];
  967. }
  968. // trigger onTransitionEnd callback
  969. if ( propertyName in _transition.onEnd ) {
  970. var onTransitionEnd = _transition.onEnd[ propertyName ];
  971. onTransitionEnd.call( this );
  972. delete _transition.onEnd[ propertyName ];
  973. }
  974. this.emitEvent( 'transitionEnd', [ this ] );
  975. };
  976. proto.disableTransition = function() {
  977. this.removeTransitionStyles();
  978. this.element.removeEventListener( transitionEndEvent, this, false );
  979. this.isTransitioning = false;
  980. };
  981. /**
  982. * removes style property from element
  983. * @param {Object} style
  984. **/
  985. proto._removeStyles = function( style ) {
  986. // clean up transition styles
  987. var cleanStyle = {};
  988. for ( var prop in style ) {
  989. cleanStyle[ prop ] = '';
  990. }
  991. this.css( cleanStyle );
  992. };
  993. var cleanTransitionStyle = {
  994. transitionProperty: '',
  995. transitionDuration: '',
  996. transitionDelay: ''
  997. };
  998. proto.removeTransitionStyles = function() {
  999. // remove transition
  1000. this.css( cleanTransitionStyle );
  1001. };
  1002. // ----- stagger ----- //
  1003. proto.stagger = function( delay ) {
  1004. delay = isNaN( delay ) ? 0 : delay;
  1005. this.staggerDelay = delay + 'ms';
  1006. };
  1007. // ----- show/hide/remove ----- //
  1008. // remove element from DOM
  1009. proto.removeElem = function() {
  1010. this.element.parentNode.removeChild( this.element );
  1011. // remove display: none
  1012. this.css({ display: '' });
  1013. this.emitEvent( 'remove', [ this ] );
  1014. };
  1015. proto.remove = function() {
  1016. // just remove element if no transition support or no transition
  1017. if ( !transitionProperty || !parseFloat( this.layout.options.transitionDuration ) ) {
  1018. this.removeElem();
  1019. return;
  1020. }
  1021. // start transition
  1022. this.once( 'transitionEnd', function() {
  1023. this.removeElem();
  1024. });
  1025. this.hide();
  1026. };
  1027. proto.reveal = function() {
  1028. delete this.isHidden;
  1029. // remove display: none
  1030. this.css({ display: '' });
  1031. var options = this.layout.options;
  1032. var onTransitionEnd = {};
  1033. var transitionEndProperty = this.getHideRevealTransitionEndProperty('visibleStyle');
  1034. onTransitionEnd[ transitionEndProperty ] = this.onRevealTransitionEnd;
  1035. this.transition({
  1036. from: options.hiddenStyle,
  1037. to: options.visibleStyle,
  1038. isCleaning: true,
  1039. onTransitionEnd: onTransitionEnd
  1040. });
  1041. };
  1042. proto.onRevealTransitionEnd = function() {
  1043. // check if still visible
  1044. // during transition, item may have been hidden
  1045. if ( !this.isHidden ) {
  1046. this.emitEvent('reveal');
  1047. }
  1048. };
  1049. /**
  1050. * get style property use for hide/reveal transition end
  1051. * @param {String} styleProperty - hiddenStyle/visibleStyle
  1052. * @returns {String}
  1053. */
  1054. proto.getHideRevealTransitionEndProperty = function( styleProperty ) {
  1055. var optionStyle = this.layout.options[ styleProperty ];
  1056. // use opacity
  1057. if ( optionStyle.opacity ) {
  1058. return 'opacity';
  1059. }
  1060. // get first property
  1061. for ( var prop in optionStyle ) {
  1062. return prop;
  1063. }
  1064. };
  1065. proto.hide = function() {
  1066. // set flag
  1067. this.isHidden = true;
  1068. // remove display: none
  1069. this.css({ display: '' });
  1070. var options = this.layout.options;
  1071. var onTransitionEnd = {};
  1072. var transitionEndProperty = this.getHideRevealTransitionEndProperty('hiddenStyle');
  1073. onTransitionEnd[ transitionEndProperty ] = this.onHideTransitionEnd;
  1074. this.transition({
  1075. from: options.visibleStyle,
  1076. to: options.hiddenStyle,
  1077. // keep hidden stuff hidden
  1078. isCleaning: true,
  1079. onTransitionEnd: onTransitionEnd
  1080. });
  1081. };
  1082. proto.onHideTransitionEnd = function() {
  1083. // check if still hidden
  1084. // during transition, item may have been un-hidden
  1085. if ( this.isHidden ) {
  1086. this.css({ display: 'none' });
  1087. this.emitEvent('hide');
  1088. }
  1089. };
  1090. proto.destroy = function() {
  1091. this.css({
  1092. position: '',
  1093. left: '',
  1094. right: '',
  1095. top: '',
  1096. bottom: '',
  1097. transition: '',
  1098. transform: ''
  1099. });
  1100. };
  1101. return Item;
  1102. }));
  1103. /*!
  1104. * Outlayer v2.1.1
  1105. * the brains and guts of a layout library
  1106. * MIT license
  1107. */
  1108. ( function( window, factory ) {
  1109. 'use strict';
  1110. // universal module definition
  1111. /* jshint strict: false */ /* globals define, module, require */
  1112. if ( typeof define == 'function' && define.amd ) {
  1113. // AMD - RequireJS
  1114. define( 'outlayer/outlayer',[
  1115. 'ev-emitter/ev-emitter',
  1116. 'get-size/get-size',
  1117. 'fizzy-ui-utils/utils',
  1118. './item'
  1119. ],
  1120. function( EvEmitter, getSize, utils, Item ) {
  1121. return factory( window, EvEmitter, getSize, utils, Item);
  1122. }
  1123. );
  1124. } else if ( typeof module == 'object' && module.exports ) {
  1125. // CommonJS - Browserify, Webpack
  1126. module.exports = factory(
  1127. window,
  1128. require('ev-emitter'),
  1129. require('get-size'),
  1130. require('fizzy-ui-utils'),
  1131. require('./item')
  1132. );
  1133. } else {
  1134. // browser global
  1135. window.Outlayer = factory(
  1136. window,
  1137. window.EvEmitter,
  1138. window.getSize,
  1139. window.fizzyUIUtils,
  1140. window.Outlayer.Item
  1141. );
  1142. }
  1143. }( window, function factory( window, EvEmitter, getSize, utils, Item ) {
  1144. 'use strict';
  1145. // ----- vars ----- //
  1146. var console = window.console;
  1147. var jQuery = window.jQuery;
  1148. var noop = function() {};
  1149. // -------------------------- Outlayer -------------------------- //
  1150. // globally unique identifiers
  1151. var GUID = 0;
  1152. // internal store of all Outlayer intances
  1153. var instances = {};
  1154. /**
  1155. * @param {Element, String} element
  1156. * @param {Object} options
  1157. * @constructor
  1158. */
  1159. function Outlayer( element, options ) {
  1160. var queryElement = utils.getQueryElement( element );
  1161. if ( !queryElement ) {
  1162. if ( console ) {
  1163. console.error( 'Bad element for ' + this.constructor.namespace +
  1164. ': ' + ( queryElement || element ) );
  1165. }
  1166. return;
  1167. }
  1168. this.element = queryElement;
  1169. // add jQuery
  1170. if ( jQuery ) {
  1171. this.$element = jQuery( this.element );
  1172. }
  1173. // options
  1174. this.options = utils.extend( {}, this.constructor.defaults );
  1175. this.option( options );
  1176. // add id for Outlayer.getFromElement
  1177. var id = ++GUID;
  1178. this.element.outlayerGUID = id; // expando
  1179. instances[ id ] = this; // associate via id
  1180. // kick it off
  1181. this._create();
  1182. var isInitLayout = this._getOption('initLayout');
  1183. if ( isInitLayout ) {
  1184. this.layout();
  1185. }
  1186. }
  1187. // settings are for internal use only
  1188. Outlayer.namespace = 'outlayer';
  1189. Outlayer.Item = Item;
  1190. // default options
  1191. Outlayer.defaults = {
  1192. containerStyle: {
  1193. position: 'relative'
  1194. },
  1195. initLayout: true,
  1196. originLeft: true,
  1197. originTop: true,
  1198. resize: true,
  1199. resizeContainer: true,
  1200. // item options
  1201. transitionDuration: '0.4s',
  1202. hiddenStyle: {
  1203. opacity: 0,
  1204. transform: 'scale(0.001)'
  1205. },
  1206. visibleStyle: {
  1207. opacity: 1,
  1208. transform: 'scale(1)'
  1209. }
  1210. };
  1211. var proto = Outlayer.prototype;
  1212. // inherit EvEmitter
  1213. utils.extend( proto, EvEmitter.prototype );
  1214. /**
  1215. * set options
  1216. * @param {Object} opts
  1217. */
  1218. proto.option = function( opts ) {
  1219. utils.extend( this.options, opts );
  1220. };
  1221. /**
  1222. * get backwards compatible option value, check old name
  1223. */
  1224. proto._getOption = function( option ) {
  1225. var oldOption = this.constructor.compatOptions[ option ];
  1226. return oldOption && this.options[ oldOption ] !== undefined ?
  1227. this.options[ oldOption ] : this.options[ option ];
  1228. };
  1229. Outlayer.compatOptions = {
  1230. // currentName: oldName
  1231. initLayout: 'isInitLayout',
  1232. horizontal: 'isHorizontal',
  1233. layoutInstant: 'isLayoutInstant',
  1234. originLeft: 'isOriginLeft',
  1235. originTop: 'isOriginTop',
  1236. resize: 'isResizeBound',
  1237. resizeContainer: 'isResizingContainer'
  1238. };
  1239. proto._create = function() {
  1240. // get items from children
  1241. this.reloadItems();
  1242. // elements that affect layout, but are not laid out
  1243. this.stamps = [];
  1244. this.stamp( this.options.stamp );
  1245. // set container style
  1246. utils.extend( this.element.style, this.options.containerStyle );
  1247. // bind resize method
  1248. var canBindResize = this._getOption('resize');
  1249. if ( canBindResize ) {
  1250. this.bindResize();
  1251. }
  1252. };
  1253. // goes through all children again and gets bricks in proper order
  1254. proto.reloadItems = function() {
  1255. // collection of item elements
  1256. this.items = this._itemize( this.element.children );
  1257. };
  1258. /**
  1259. * turn elements into Outlayer.Items to be used in layout
  1260. * @param {Array or NodeList or HTMLElement} elems
  1261. * @returns {Array} items - collection of new Outlayer Items
  1262. */
  1263. proto._itemize = function( elems ) {
  1264. var itemElems = this._filterFindItemElements( elems );
  1265. var Item = this.constructor.Item;
  1266. // create new Outlayer Items for collection
  1267. var items = [];
  1268. for ( var i=0; i < itemElems.length; i++ ) {
  1269. var elem = itemElems[i];
  1270. var item = new Item( elem, this );
  1271. items.push( item );
  1272. }
  1273. return items;
  1274. };
  1275. /**
  1276. * get item elements to be used in layout
  1277. * @param {Array or NodeList or HTMLElement} elems
  1278. * @returns {Array} items - item elements
  1279. */
  1280. proto._filterFindItemElements = function( elems ) {
  1281. return utils.filterFindElements( elems, this.options.itemSelector );
  1282. };
  1283. /**
  1284. * getter method for getting item elements
  1285. * @returns {Array} elems - collection of item elements
  1286. */
  1287. proto.getItemElements = function() {
  1288. return this.items.map( function( item ) {
  1289. return item.element;
  1290. });
  1291. };
  1292. // ----- init & layout ----- //
  1293. /**
  1294. * lays out all items
  1295. */
  1296. proto.layout = function() {
  1297. this._resetLayout();
  1298. this._manageStamps();
  1299. // don't animate first layout
  1300. var layoutInstant = this._getOption('layoutInstant');
  1301. var isInstant = layoutInstant !== undefined ?
  1302. layoutInstant : !this._isLayoutInited;
  1303. this.layoutItems( this.items, isInstant );
  1304. // flag for initalized
  1305. this._isLayoutInited = true;
  1306. };
  1307. // _init is alias for layout
  1308. proto._init = proto.layout;
  1309. /**
  1310. * logic before any new layout
  1311. */
  1312. proto._resetLayout = function() {
  1313. this.getSize();
  1314. };
  1315. proto.getSize = function() {
  1316. this.size = getSize( this.element );
  1317. };
  1318. /**
  1319. * get measurement from option, for columnWidth, rowHeight, gutter
  1320. * if option is String -> get element from selector string, & get size of element
  1321. * if option is Element -> get size of element
  1322. * else use option as a number
  1323. *
  1324. * @param {String} measurement
  1325. * @param {String} size - width or height
  1326. * @private
  1327. */
  1328. proto._getMeasurement = function( measurement, size ) {
  1329. var option = this.options[ measurement ];
  1330. var elem;
  1331. if ( !option ) {
  1332. // default to 0
  1333. this[ measurement ] = 0;
  1334. } else {
  1335. // use option as an element
  1336. if ( typeof option == 'string' ) {
  1337. elem = this.element.querySelector( option );
  1338. } else if ( option instanceof HTMLElement ) {
  1339. elem = option;
  1340. }
  1341. // use size of element, if element
  1342. this[ measurement ] = elem ? getSize( elem )[ size ] : option;
  1343. }
  1344. };
  1345. /**
  1346. * layout a collection of item elements
  1347. * @api public
  1348. */
  1349. proto.layoutItems = function( items, isInstant ) {
  1350. items = this._getItemsForLayout( items );
  1351. this._layoutItems( items, isInstant );
  1352. this._postLayout();
  1353. };
  1354. /**
  1355. * get the items to be laid out
  1356. * you may want to skip over some items
  1357. * @param {Array} items
  1358. * @returns {Array} items
  1359. */
  1360. proto._getItemsForLayout = function( items ) {
  1361. return items.filter( function( item ) {
  1362. return !item.isIgnored;
  1363. });
  1364. };
  1365. /**
  1366. * layout items
  1367. * @param {Array} items
  1368. * @param {Boolean} isInstant
  1369. */
  1370. proto._layoutItems = function( items, isInstant ) {
  1371. this._emitCompleteOnItems( 'layout', items );
  1372. if ( !items || !items.length ) {
  1373. // no items, emit event with empty array
  1374. return;
  1375. }
  1376. var queue = [];
  1377. items.forEach( function( item ) {
  1378. // get x/y object from method
  1379. var position = this._getItemLayoutPosition( item );
  1380. // enqueue
  1381. position.item = item;
  1382. position.isInstant = isInstant || item.isLayoutInstant;
  1383. queue.push( position );
  1384. }, this );
  1385. this._processLayoutQueue( queue );
  1386. };
  1387. /**
  1388. * get item layout position
  1389. * @param {Outlayer.Item} item
  1390. * @returns {Object} x and y position
  1391. */
  1392. proto._getItemLayoutPosition = function( /* item */ ) {
  1393. return {
  1394. x: 0,
  1395. y: 0
  1396. };
  1397. };
  1398. /**
  1399. * iterate over array and position each item
  1400. * Reason being - separating this logic prevents 'layout invalidation'
  1401. * thx @paul_irish
  1402. * @param {Array} queue
  1403. */
  1404. proto._processLayoutQueue = function( queue ) {
  1405. this.updateStagger();
  1406. queue.forEach( function( obj, i ) {
  1407. this._positionItem( obj.item, obj.x, obj.y, obj.isInstant, i );
  1408. }, this );
  1409. };
  1410. // set stagger from option in milliseconds number
  1411. proto.updateStagger = function() {
  1412. var stagger = this.options.stagger;
  1413. if ( stagger === null || stagger === undefined ) {
  1414. this.stagger = 0;
  1415. return;
  1416. }
  1417. this.stagger = getMilliseconds( stagger );
  1418. return this.stagger;
  1419. };
  1420. /**
  1421. * Sets position of item in DOM
  1422. * @param {Outlayer.Item} item
  1423. * @param {Number} x - horizontal position
  1424. * @param {Number} y - vertical position
  1425. * @param {Boolean} isInstant - disables transitions
  1426. */
  1427. proto._positionItem = function( item, x, y, isInstant, i ) {
  1428. if ( isInstant ) {
  1429. // if not transition, just set CSS
  1430. item.goTo( x, y );
  1431. } else {
  1432. item.stagger( i * this.stagger );
  1433. item.moveTo( x, y );
  1434. }
  1435. };
  1436. /**
  1437. * Any logic you want to do after each layout,
  1438. * i.e. size the container
  1439. */
  1440. proto._postLayout = function() {
  1441. this.resizeContainer();
  1442. };
  1443. proto.resizeContainer = function() {
  1444. var isResizingContainer = this._getOption('resizeContainer');
  1445. if ( !isResizingContainer ) {
  1446. return;
  1447. }
  1448. var size = this._getContainerSize();
  1449. if ( size ) {
  1450. this._setContainerMeasure( size.width, true );
  1451. this._setContainerMeasure( size.height, false );
  1452. }
  1453. };
  1454. /**
  1455. * Sets width or height of container if returned
  1456. * @returns {Object} size
  1457. * @param {Number} width
  1458. * @param {Number} height
  1459. */
  1460. proto._getContainerSize = noop;
  1461. /**
  1462. * @param {Number} measure - size of width or height
  1463. * @param {Boolean} isWidth
  1464. */
  1465. proto._setContainerMeasure = function( measure, isWidth ) {
  1466. if ( measure === undefined ) {
  1467. return;
  1468. }
  1469. var elemSize = this.size;
  1470. // add padding and border width if border box
  1471. if ( elemSize.isBorderBox ) {
  1472. measure += isWidth ? elemSize.paddingLeft + elemSize.paddingRight +
  1473. elemSize.borderLeftWidth + elemSize.borderRightWidth :
  1474. elemSize.paddingBottom + elemSize.paddingTop +
  1475. elemSize.borderTopWidth + elemSize.borderBottomWidth;
  1476. }
  1477. measure = Math.max( measure, 0 );
  1478. this.element.style[ isWidth ? 'width' : 'height' ] = measure + 'px';
  1479. };
  1480. /**
  1481. * emit eventComplete on a collection of items events
  1482. * @param {String} eventName
  1483. * @param {Array} items - Outlayer.Items
  1484. */
  1485. proto._emitCompleteOnItems = function( eventName, items ) {
  1486. var _this = this;
  1487. function onComplete() {
  1488. _this.dispatchEvent( eventName + 'Complete', null, [ items ] );
  1489. }
  1490. var count = items.length;
  1491. if ( !items || !count ) {
  1492. onComplete();
  1493. return;
  1494. }
  1495. var doneCount = 0;
  1496. function tick() {
  1497. doneCount++;
  1498. if ( doneCount == count ) {
  1499. onComplete();
  1500. }
  1501. }
  1502. // bind callback
  1503. items.forEach( function( item ) {
  1504. item.once( eventName, tick );
  1505. });
  1506. };
  1507. /**
  1508. * emits events via EvEmitter and jQuery events
  1509. * @param {String} type - name of event
  1510. * @param {Event} event - original event
  1511. * @param {Array} args - extra arguments
  1512. */
  1513. proto.dispatchEvent = function( type, event, args ) {
  1514. // add original event to arguments
  1515. var emitArgs = event ? [ event ].concat( args ) : args;
  1516. this.emitEvent( type, emitArgs );
  1517. if ( jQuery ) {
  1518. // set this.$element
  1519. this.$element = this.$element || jQuery( this.element );
  1520. if ( event ) {
  1521. // create jQuery event
  1522. var $event = jQuery.Event( event );
  1523. $event.type = type;
  1524. this.$element.trigger( $event, args );
  1525. } else {
  1526. // just trigger with type if no event available
  1527. this.$element.trigger( type, args );
  1528. }
  1529. }
  1530. };
  1531. // -------------------------- ignore & stamps -------------------------- //
  1532. /**
  1533. * keep item in collection, but do not lay it out
  1534. * ignored items do not get skipped in layout
  1535. * @param {Element} elem
  1536. */
  1537. proto.ignore = function( elem ) {
  1538. var item = this.getItem( elem );
  1539. if ( item ) {
  1540. item.isIgnored = true;
  1541. }
  1542. };
  1543. /**
  1544. * return item to layout collection
  1545. * @param {Element} elem
  1546. */
  1547. proto.unignore = function( elem ) {
  1548. var item = this.getItem( elem );
  1549. if ( item ) {
  1550. delete item.isIgnored;
  1551. }
  1552. };
  1553. /**
  1554. * adds elements to stamps
  1555. * @param {NodeList, Array, Element, or String} elems
  1556. */
  1557. proto.stamp = function( elems ) {
  1558. elems = this._find( elems );
  1559. if ( !elems ) {
  1560. return;
  1561. }
  1562. this.stamps = this.stamps.concat( elems );
  1563. // ignore
  1564. elems.forEach( this.ignore, this );
  1565. };
  1566. /**
  1567. * removes elements to stamps
  1568. * @param {NodeList, Array, or Element} elems
  1569. */
  1570. proto.unstamp = function( elems ) {
  1571. elems = this._find( elems );
  1572. if ( !elems ){
  1573. return;
  1574. }
  1575. elems.forEach( function( elem ) {
  1576. // filter out removed stamp elements
  1577. utils.removeFrom( this.stamps, elem );
  1578. this.unignore( elem );
  1579. }, this );
  1580. };
  1581. /**
  1582. * finds child elements
  1583. * @param {NodeList, Array, Element, or String} elems
  1584. * @returns {Array} elems
  1585. */
  1586. proto._find = function( elems ) {
  1587. if ( !elems ) {
  1588. return;
  1589. }
  1590. // if string, use argument as selector string
  1591. if ( typeof elems == 'string' ) {
  1592. elems = this.element.querySelectorAll( elems );
  1593. }
  1594. elems = utils.makeArray( elems );
  1595. return elems;
  1596. };
  1597. proto._manageStamps = function() {
  1598. if ( !this.stamps || !this.stamps.length ) {
  1599. return;
  1600. }
  1601. this._getBoundingRect();
  1602. this.stamps.forEach( this._manageStamp, this );
  1603. };
  1604. // update boundingLeft / Top
  1605. proto._getBoundingRect = function() {
  1606. // get bounding rect for container element
  1607. var boundingRect = this.element.getBoundingClientRect();
  1608. var size = this.size;
  1609. this._boundingRect = {
  1610. left: boundingRect.left + size.paddingLeft + size.borderLeftWidth,
  1611. top: boundingRect.top + size.paddingTop + size.borderTopWidth,
  1612. right: boundingRect.right - ( size.paddingRight + size.borderRightWidth ),
  1613. bottom: boundingRect.bottom - ( size.paddingBottom + size.borderBottomWidth )
  1614. };
  1615. };
  1616. /**
  1617. * @param {Element} stamp
  1618. **/
  1619. proto._manageStamp = noop;
  1620. /**
  1621. * get x/y position of element relative to container element
  1622. * @param {Element} elem
  1623. * @returns {Object} offset - has left, top, right, bottom
  1624. */
  1625. proto._getElementOffset = function( elem ) {
  1626. var boundingRect = elem.getBoundingClientRect();
  1627. var thisRect = this._boundingRect;
  1628. var size = getSize( elem );
  1629. var offset = {
  1630. left: boundingRect.left - thisRect.left - size.marginLeft,
  1631. top: boundingRect.top - thisRect.top - size.marginTop,
  1632. right: thisRect.right - boundingRect.right - size.marginRight,
  1633. bottom: thisRect.bottom - boundingRect.bottom - size.marginBottom
  1634. };
  1635. return offset;
  1636. };
  1637. // -------------------------- resize -------------------------- //
  1638. // enable event handlers for listeners
  1639. // i.e. resize -> onresize
  1640. proto.handleEvent = utils.handleEvent;
  1641. /**
  1642. * Bind layout to window resizing
  1643. */
  1644. proto.bindResize = function() {
  1645. window.addEventListener( 'resize', this );
  1646. this.isResizeBound = true;
  1647. };
  1648. /**
  1649. * Unbind layout to window resizing
  1650. */
  1651. proto.unbindResize = function() {
  1652. window.removeEventListener( 'resize', this );
  1653. this.isResizeBound = false;
  1654. };
  1655. proto.onresize = function() {
  1656. this.resize();
  1657. };
  1658. utils.debounceMethod( Outlayer, 'onresize', 100 );
  1659. proto.resize = function() {
  1660. // don't trigger if size did not change
  1661. // or if resize was unbound. See #9
  1662. if ( !this.isResizeBound || !this.needsResizeLayout() ) {
  1663. return;
  1664. }
  1665. this.layout();
  1666. };
  1667. /**
  1668. * check if layout is needed post layout
  1669. * @returns Boolean
  1670. */
  1671. proto.needsResizeLayout = function() {
  1672. var size = getSize( this.element );
  1673. // check that this.size and size are there
  1674. // IE8 triggers resize on body size change, so they might not be
  1675. var hasSizes = this.size && size;
  1676. return hasSizes && size.innerWidth !== this.size.innerWidth;
  1677. };
  1678. // -------------------------- methods -------------------------- //
  1679. /**
  1680. * add items to Outlayer instance
  1681. * @param {Array or NodeList or Element} elems
  1682. * @returns {Array} items - Outlayer.Items
  1683. **/
  1684. proto.addItems = function( elems ) {
  1685. var items = this._itemize( elems );
  1686. // add items to collection
  1687. if ( items.length ) {
  1688. this.items = this.items.concat( items );
  1689. }
  1690. return items;
  1691. };
  1692. /**
  1693. * Layout newly-appended item elements
  1694. * @param {Array or NodeList or Element} elems
  1695. */
  1696. proto.appended = function( elems ) {
  1697. var items = this.addItems( elems );
  1698. if ( !items.length ) {
  1699. return;
  1700. }
  1701. // layout and reveal just the new items
  1702. this.layoutItems( items, true );
  1703. this.reveal( items );
  1704. };
  1705. /**
  1706. * Layout prepended elements
  1707. * @param {Array or NodeList or Element} elems
  1708. */
  1709. proto.prepended = function( elems ) {
  1710. var items = this._itemize( elems );
  1711. if ( !items.length ) {
  1712. return;
  1713. }
  1714. // add items to beginning of collection
  1715. var previousItems = this.items.slice(0);
  1716. this.items = items.concat( previousItems );
  1717. // start new layout
  1718. this._resetLayout();
  1719. this._manageStamps();
  1720. // layout new stuff without transition
  1721. this.layoutItems( items, true );
  1722. this.reveal( items );
  1723. // layout previous items
  1724. this.layoutItems( previousItems );
  1725. };
  1726. /**
  1727. * reveal a collection of items
  1728. * @param {Array of Outlayer.Items} items
  1729. */
  1730. proto.reveal = function( items ) {
  1731. this._emitCompleteOnItems( 'reveal', items );
  1732. if ( !items || !items.length ) {
  1733. return;
  1734. }
  1735. var stagger = this.updateStagger();
  1736. items.forEach( function( item, i ) {
  1737. item.stagger( i * stagger );
  1738. item.reveal();
  1739. });
  1740. };
  1741. /**
  1742. * hide a collection of items
  1743. * @param {Array of Outlayer.Items} items
  1744. */
  1745. proto.hide = function( items ) {
  1746. this._emitCompleteOnItems( 'hide', items );
  1747. if ( !items || !items.length ) {
  1748. return;
  1749. }
  1750. var stagger = this.updateStagger();
  1751. items.forEach( function( item, i ) {
  1752. item.stagger( i * stagger );
  1753. item.hide();
  1754. });
  1755. };
  1756. /**
  1757. * reveal item elements
  1758. * @param {Array}, {Element}, {NodeList} items
  1759. */
  1760. proto.revealItemElements = function( elems ) {
  1761. var items = this.getItems( elems );
  1762. this.reveal( items );
  1763. };
  1764. /**
  1765. * hide item elements
  1766. * @param {Array}, {Element}, {NodeList} items
  1767. */
  1768. proto.hideItemElements = function( elems ) {
  1769. var items = this.getItems( elems );
  1770. this.hide( items );
  1771. };
  1772. /**
  1773. * get Outlayer.Item, given an Element
  1774. * @param {Element} elem
  1775. * @param {Function} callback
  1776. * @returns {Outlayer.Item} item
  1777. */
  1778. proto.getItem = function( elem ) {
  1779. // loop through items to get the one that matches
  1780. for ( var i=0; i < this.items.length; i++ ) {
  1781. var item = this.items[i];
  1782. if ( item.element == elem ) {
  1783. // return item
  1784. return item;
  1785. }
  1786. }
  1787. };
  1788. /**
  1789. * get collection of Outlayer.Items, given Elements
  1790. * @param {Array} elems
  1791. * @returns {Array} items - Outlayer.Items
  1792. */
  1793. proto.getItems = function( elems ) {
  1794. elems = utils.makeArray( elems );
  1795. var items = [];
  1796. elems.forEach( function( elem ) {
  1797. var item = this.getItem( elem );
  1798. if ( item ) {
  1799. items.push( item );
  1800. }
  1801. }, this );
  1802. return items;
  1803. };
  1804. /**
  1805. * remove element(s) from instance and DOM
  1806. * @param {Array or NodeList or Element} elems
  1807. */
  1808. proto.remove = function( elems ) {
  1809. var removeItems = this.getItems( elems );
  1810. this._emitCompleteOnItems( 'remove', removeItems );
  1811. // bail if no items to remove
  1812. if ( !removeItems || !removeItems.length ) {
  1813. return;
  1814. }
  1815. removeItems.forEach( function( item ) {
  1816. item.remove();
  1817. // remove item from collection
  1818. utils.removeFrom( this.items, item );
  1819. }, this );
  1820. };
  1821. // ----- destroy ----- //
  1822. // remove and disable Outlayer instance
  1823. proto.destroy = function() {
  1824. // clean up dynamic styles
  1825. var style = this.element.style;
  1826. style.height = '';
  1827. style.position = '';
  1828. style.width = '';
  1829. // destroy items
  1830. this.items.forEach( function( item ) {
  1831. item.destroy();
  1832. });
  1833. this.unbindResize();
  1834. var id = this.element.outlayerGUID;
  1835. delete instances[ id ]; // remove reference to instance by id
  1836. delete this.element.outlayerGUID;
  1837. // remove data for jQuery
  1838. if ( jQuery ) {
  1839. jQuery.removeData( this.element, this.constructor.namespace );
  1840. }
  1841. };
  1842. // -------------------------- data -------------------------- //
  1843. /**
  1844. * get Outlayer instance from element
  1845. * @param {Element} elem
  1846. * @returns {Outlayer}
  1847. */
  1848. Outlayer.data = function( elem ) {
  1849. elem = utils.getQueryElement( elem );
  1850. var id = elem && elem.outlayerGUID;
  1851. return id && instances[ id ];
  1852. };
  1853. // -------------------------- create Outlayer class -------------------------- //
  1854. /**
  1855. * create a layout class
  1856. * @param {String} namespace
  1857. */
  1858. Outlayer.create = function( namespace, options ) {
  1859. // sub-class Outlayer
  1860. var Layout = subclass( Outlayer );
  1861. // apply new options and compatOptions
  1862. Layout.defaults = utils.extend( {}, Outlayer.defaults );
  1863. utils.extend( Layout.defaults, options );
  1864. Layout.compatOptions = utils.extend( {}, Outlayer.compatOptions );
  1865. Layout.namespace = namespace;
  1866. Layout.data = Outlayer.data;
  1867. // sub-class Item
  1868. Layout.Item = subclass( Item );
  1869. // -------------------------- declarative -------------------------- //
  1870. utils.htmlInit( Layout, namespace );
  1871. // -------------------------- jQuery bridge -------------------------- //
  1872. // make into jQuery plugin
  1873. if ( jQuery && jQuery.bridget ) {
  1874. jQuery.bridget( namespace, Layout );
  1875. }
  1876. return Layout;
  1877. };
  1878. function subclass( Parent ) {
  1879. function SubClass() {
  1880. Parent.apply( this, arguments );
  1881. }
  1882. SubClass.prototype = Object.create( Parent.prototype );
  1883. SubClass.prototype.constructor = SubClass;
  1884. return SubClass;
  1885. }
  1886. // ----- helpers ----- //
  1887. // how many milliseconds are in each unit
  1888. var msUnits = {
  1889. ms: 1,
  1890. s: 1000
  1891. };
  1892. // munge time-like parameter into millisecond number
  1893. // '0.4s' -> 40
  1894. function getMilliseconds( time ) {
  1895. if ( typeof time == 'number' ) {
  1896. return time;
  1897. }
  1898. var matches = time.match( /(^\d*\.?\d*)(\w*)/ );
  1899. var num = matches && matches[1];
  1900. var unit = matches && matches[2];
  1901. if ( !num.length ) {
  1902. return 0;
  1903. }
  1904. num = parseFloat( num );
  1905. var mult = msUnits[ unit ] || 1;
  1906. return num * mult;
  1907. }
  1908. // ----- fin ----- //
  1909. // back in global
  1910. Outlayer.Item = Item;
  1911. return Outlayer;
  1912. }));
  1913. /*!
  1914. * Masonry v4.2.2
  1915. * Cascading grid layout library
  1916. * https://masonry.desandro.com
  1917. * MIT License
  1918. * by David DeSandro
  1919. */
  1920. ( function( window, factory ) {
  1921. // universal module definition
  1922. /* jshint strict: false */ /*globals define, module, require */
  1923. if ( typeof define == 'function' && define.amd ) {
  1924. // AMD
  1925. define( [
  1926. 'outlayer/outlayer',
  1927. 'get-size/get-size'
  1928. ],
  1929. factory );
  1930. } else if ( typeof module == 'object' && module.exports ) {
  1931. // CommonJS
  1932. module.exports = factory(
  1933. require('outlayer'),
  1934. require('get-size')
  1935. );
  1936. } else {
  1937. // browser global
  1938. window.Masonry = factory(
  1939. window.Outlayer,
  1940. window.getSize
  1941. );
  1942. }
  1943. }( window, function factory( Outlayer, getSize ) {
  1944. // -------------------------- masonryDefinition -------------------------- //
  1945. // create an Outlayer layout class
  1946. var Masonry = Outlayer.create('masonry');
  1947. // isFitWidth -> fitWidth
  1948. Masonry.compatOptions.fitWidth = 'isFitWidth';
  1949. var proto = Masonry.prototype;
  1950. proto._resetLayout = function() {
  1951. this.getSize();
  1952. this._getMeasurement( 'columnWidth', 'outerWidth' );
  1953. this._getMeasurement( 'gutter', 'outerWidth' );
  1954. this.measureColumns();
  1955. // reset column Y
  1956. this.colYs = [];
  1957. for ( var i=0; i < this.cols; i++ ) {
  1958. this.colYs.push( 0 );
  1959. }
  1960. this.maxY = 0;
  1961. this.horizontalColIndex = 0;
  1962. };
  1963. proto.measureColumns = function() {
  1964. this.getContainerWidth();
  1965. // if columnWidth is 0, default to outerWidth of first item
  1966. if ( !this.columnWidth ) {
  1967. var firstItem = this.items[0];
  1968. var firstItemElem = firstItem && firstItem.element;
  1969. // columnWidth fall back to item of first element
  1970. this.columnWidth = firstItemElem && getSize( firstItemElem ).outerWidth ||
  1971. // if first elem has no width, default to size of container
  1972. this.containerWidth;
  1973. }
  1974. var columnWidth = this.columnWidth += this.gutter;
  1975. // calculate columns
  1976. var containerWidth = this.containerWidth + this.gutter;
  1977. var cols = containerWidth / columnWidth;
  1978. // fix rounding errors, typically with gutters
  1979. var excess = columnWidth - containerWidth % columnWidth;
  1980. // if overshoot is less than a pixel, round up, otherwise floor it
  1981. var mathMethod = excess && excess < 1 ? 'round' : 'floor';
  1982. cols = Math[ mathMethod ]( cols );
  1983. this.cols = Math.max( cols, 1 );
  1984. };
  1985. proto.getContainerWidth = function() {
  1986. // container is parent if fit width
  1987. var isFitWidth = this._getOption('fitWidth');
  1988. var container = isFitWidth ? this.element.parentNode : this.element;
  1989. // check that this.size and size are there
  1990. // IE8 triggers resize on body size change, so they might not be
  1991. var size = getSize( container );
  1992. this.containerWidth = size && size.innerWidth;
  1993. };
  1994. proto._getItemLayoutPosition = function( item ) {
  1995. item.getSize();
  1996. // how many columns does this brick span
  1997. var remainder = item.size.outerWidth % this.columnWidth;
  1998. var mathMethod = remainder && remainder < 1 ? 'round' : 'ceil';
  1999. // round if off by 1 pixel, otherwise use ceil
  2000. var colSpan = Math[ mathMethod ]( item.size.outerWidth / this.columnWidth );
  2001. colSpan = Math.min( colSpan, this.cols );
  2002. // use horizontal or top column position
  2003. var colPosMethod = this.options.horizontalOrder ?
  2004. '_getHorizontalColPosition' : '_getTopColPosition';
  2005. var colPosition = this[ colPosMethod ]( colSpan, item );
  2006. // position the brick
  2007. var position = {
  2008. x: this.columnWidth * colPosition.col,
  2009. y: colPosition.y
  2010. };
  2011. // apply setHeight to necessary columns
  2012. var setHeight = colPosition.y + item.size.outerHeight;
  2013. var setMax = colSpan + colPosition.col;
  2014. for ( var i = colPosition.col; i < setMax; i++ ) {
  2015. this.colYs[i] = setHeight;
  2016. }
  2017. return position;
  2018. };
  2019. proto._getTopColPosition = function( colSpan ) {
  2020. var colGroup = this._getTopColGroup( colSpan );
  2021. // get the minimum Y value from the columns
  2022. var minimumY = Math.min.apply( Math, colGroup );
  2023. return {
  2024. col: colGroup.indexOf( minimumY ),
  2025. y: minimumY,
  2026. };
  2027. };
  2028. /**
  2029. * @param {Number} colSpan - number of columns the element spans
  2030. * @returns {Array} colGroup
  2031. */
  2032. proto._getTopColGroup = function( colSpan ) {
  2033. if ( colSpan < 2 ) {
  2034. // if brick spans only one column, use all the column Ys
  2035. return this.colYs;
  2036. }
  2037. var colGroup = [];
  2038. // how many different places could this brick fit horizontally
  2039. var groupCount = this.cols + 1 - colSpan;
  2040. // for each group potential horizontal position
  2041. for ( var i = 0; i < groupCount; i++ ) {
  2042. colGroup[i] = this._getColGroupY( i, colSpan );
  2043. }
  2044. return colGroup;
  2045. };
  2046. proto._getColGroupY = function( col, colSpan ) {
  2047. if ( colSpan < 2 ) {
  2048. return this.colYs[ col ];
  2049. }
  2050. // make an array of colY values for that one group
  2051. var groupColYs = this.colYs.slice( col, col + colSpan );
  2052. // and get the max value of the array
  2053. return Math.max.apply( Math, groupColYs );
  2054. };
  2055. // get column position based on horizontal index. #873
  2056. proto._getHorizontalColPosition = function( colSpan, item ) {
  2057. var col = this.horizontalColIndex % this.cols;
  2058. var isOver = colSpan > 1 && col + colSpan > this.cols;
  2059. // shift to next row if item can't fit on current row
  2060. col = isOver ? 0 : col;
  2061. // don't let zero-size items take up space
  2062. var hasSize = item.size.outerWidth && item.size.outerHeight;
  2063. this.horizontalColIndex = hasSize ? col + colSpan : this.horizontalColIndex;
  2064. return {
  2065. col: col,
  2066. y: this._getColGroupY( col, colSpan ),
  2067. };
  2068. };
  2069. proto._manageStamp = function( stamp ) {
  2070. var stampSize = getSize( stamp );
  2071. var offset = this._getElementOffset( stamp );
  2072. // get the columns that this stamp affects
  2073. var isOriginLeft = this._getOption('originLeft');
  2074. var firstX = isOriginLeft ? offset.left : offset.right;
  2075. var lastX = firstX + stampSize.outerWidth;
  2076. var firstCol = Math.floor( firstX / this.columnWidth );
  2077. firstCol = Math.max( 0, firstCol );
  2078. var lastCol = Math.floor( lastX / this.columnWidth );
  2079. // lastCol should not go over if multiple of columnWidth #425
  2080. lastCol -= lastX % this.columnWidth ? 0 : 1;
  2081. lastCol = Math.min( this.cols - 1, lastCol );
  2082. // set colYs to bottom of the stamp
  2083. var isOriginTop = this._getOption('originTop');
  2084. var stampMaxY = ( isOriginTop ? offset.top : offset.bottom ) +
  2085. stampSize.outerHeight;
  2086. for ( var i = firstCol; i <= lastCol; i++ ) {
  2087. this.colYs[i] = Math.max( stampMaxY, this.colYs[i] );
  2088. }
  2089. };
  2090. proto._getContainerSize = function() {
  2091. this.maxY = Math.max.apply( Math, this.colYs );
  2092. var size = {
  2093. height: this.maxY
  2094. };
  2095. if ( this._getOption('fitWidth') ) {
  2096. size.width = this._getContainerFitWidth();
  2097. }
  2098. return size;
  2099. };
  2100. proto._getContainerFitWidth = function() {
  2101. var unusedCols = 0;
  2102. // count unused columns
  2103. var i = this.cols;
  2104. while ( --i ) {
  2105. if ( this.colYs[i] !== 0 ) {
  2106. break;
  2107. }
  2108. unusedCols++;
  2109. }
  2110. // fit container to columns that have been used
  2111. return ( this.cols - unusedCols ) * this.columnWidth - this.gutter;
  2112. };
  2113. proto.needsResizeLayout = function() {
  2114. var previousWidth = this.containerWidth;
  2115. this.getContainerWidth();
  2116. return previousWidth != this.containerWidth;
  2117. };
  2118. return Masonry;
  2119. }));