foundation.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. * Foundation Responsive Library
  3. * http://foundation.zurb.com
  4. * Copyright 2014, ZURB
  5. * Free to use under the MIT license.
  6. * http://www.opensource.org/licenses/mit-license.php
  7. */
  8. (function ($, window, document, undefined) {
  9. 'use strict';
  10. var header_helpers = function (class_array) {
  11. var i = class_array.length;
  12. var head = $('head');
  13. while (i--) {
  14. if(head.has('.' + class_array[i]).length === 0) {
  15. head.append('<meta class="' + class_array[i] + '" />');
  16. }
  17. }
  18. };
  19. header_helpers([
  20. 'foundation-mq-small',
  21. 'foundation-mq-medium',
  22. 'foundation-mq-large',
  23. 'foundation-mq-xlarge',
  24. 'foundation-mq-xxlarge',
  25. 'foundation-data-attribute-namespace']);
  26. // Enable FastClick if present
  27. $(function() {
  28. if (typeof FastClick !== 'undefined') {
  29. // Don't attach to body if undefined
  30. if (typeof document.body !== 'undefined') {
  31. FastClick.attach(document.body);
  32. }
  33. }
  34. });
  35. // private Fast Selector wrapper,
  36. // returns jQuery object. Only use where
  37. // getElementById is not available.
  38. var S = function (selector, context) {
  39. if (typeof selector === 'string') {
  40. if (context) {
  41. var cont;
  42. if (context.jquery) {
  43. cont = context[0];
  44. if (!cont) return context;
  45. } else {
  46. cont = context;
  47. }
  48. return $(cont.querySelectorAll(selector));
  49. }
  50. return $(document.querySelectorAll(selector));
  51. }
  52. return $(selector, context);
  53. };
  54. // Namespace functions.
  55. var attr_name = function (init) {
  56. var arr = [];
  57. if (!init) arr.push('data');
  58. if (this.namespace.length > 0) arr.push(this.namespace);
  59. arr.push(this.name);
  60. return arr.join('-');
  61. };
  62. var add_namespace = function (str) {
  63. var parts = str.split('-'),
  64. i = parts.length,
  65. arr = [];
  66. while (i--) {
  67. if (i !== 0) {
  68. arr.push(parts[i]);
  69. } else {
  70. if (this.namespace.length > 0) {
  71. arr.push(this.namespace, parts[i]);
  72. } else {
  73. arr.push(parts[i]);
  74. }
  75. }
  76. }
  77. return arr.reverse().join('-');
  78. };
  79. // Event binding and data-options updating.
  80. var bindings = function (method, options) {
  81. var self = this,
  82. should_bind_events = !S(this).data(this.attr_name(true));
  83. if (typeof method === 'string') {
  84. return this[method].call(this, options);
  85. }
  86. if (S(this.scope).is('[' + this.attr_name() +']')) {
  87. S(this.scope).data(this.attr_name(true) + '-init', $.extend({}, this.settings, (options || method), this.data_options(S(this.scope))));
  88. if (should_bind_events) {
  89. this.events(this.scope);
  90. }
  91. } else {
  92. S('[' + this.attr_name() +']', this.scope).each(function () {
  93. var should_bind_events = !S(this).data(self.attr_name(true) + '-init');
  94. S(this).data(self.attr_name(true) + '-init', $.extend({}, self.settings, (options || method), self.data_options(S(this))));
  95. if (should_bind_events) {
  96. self.events(this);
  97. }
  98. });
  99. }
  100. };
  101. var single_image_loaded = function (image, callback) {
  102. function loaded () {
  103. callback(image[0]);
  104. }
  105. function bindLoad () {
  106. this.one('load', loaded);
  107. if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
  108. var src = this.attr( 'src' ),
  109. param = src.match( /\?/ ) ? '&' : '?';
  110. param += 'random=' + (new Date()).getTime();
  111. this.attr('src', src + param);
  112. }
  113. }
  114. if (!image.attr('src')) {
  115. loaded();
  116. return;
  117. }
  118. if (image[0].complete || image[0].readyState === 4) {
  119. loaded();
  120. } else {
  121. bindLoad.call(image);
  122. }
  123. };
  124. /*
  125. https://github.com/paulirish/matchMedia.js
  126. */
  127. window.matchMedia = window.matchMedia || (function( doc ) {
  128. "use strict";
  129. var bool,
  130. docElem = doc.documentElement,
  131. refNode = docElem.firstElementChild || docElem.firstChild,
  132. // fakeBody required for <FF4 when executed in <head>
  133. fakeBody = doc.createElement( "body" ),
  134. div = doc.createElement( "div" );
  135. div.id = "mq-test-1";
  136. div.style.cssText = "position:absolute;top:-100em";
  137. fakeBody.style.background = "none";
  138. fakeBody.appendChild(div);
  139. return function (q) {
  140. div.innerHTML = "&shy;<style media=\"" + q + "\"> #mq-test-1 { width: 42px; }</style>";
  141. docElem.insertBefore( fakeBody, refNode );
  142. bool = div.offsetWidth === 42;
  143. docElem.removeChild( fakeBody );
  144. return {
  145. matches: bool,
  146. media: q
  147. };
  148. };
  149. }( document ));
  150. /*
  151. * jquery.requestAnimationFrame
  152. * https://github.com/gnarf37/jquery-requestAnimationFrame
  153. * Requires jQuery 1.8+
  154. *
  155. * Copyright (c) 2012 Corey Frang
  156. * Licensed under the MIT license.
  157. */
  158. (function($) {
  159. // requestAnimationFrame polyfill adapted from Erik Möller
  160. // fixes from Paul Irish and Tino Zijdel
  161. // http://paulirish.com/2011/requestanimationframe-for-smart-animating/
  162. // http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
  163. var animating,
  164. lastTime = 0,
  165. vendors = ['webkit', 'moz'],
  166. requestAnimationFrame = window.requestAnimationFrame,
  167. cancelAnimationFrame = window.cancelAnimationFrame,
  168. jqueryFxAvailable = 'undefined' !== typeof jQuery.fx;
  169. for (; lastTime < vendors.length && !requestAnimationFrame; lastTime++) {
  170. requestAnimationFrame = window[ vendors[lastTime] + "RequestAnimationFrame" ];
  171. cancelAnimationFrame = cancelAnimationFrame ||
  172. window[ vendors[lastTime] + "CancelAnimationFrame" ] ||
  173. window[ vendors[lastTime] + "CancelRequestAnimationFrame" ];
  174. }
  175. function raf() {
  176. if (animating) {
  177. requestAnimationFrame(raf);
  178. if (jqueryFxAvailable) {
  179. jQuery.fx.tick();
  180. }
  181. }
  182. }
  183. if (requestAnimationFrame) {
  184. // use rAF
  185. window.requestAnimationFrame = requestAnimationFrame;
  186. window.cancelAnimationFrame = cancelAnimationFrame;
  187. if (jqueryFxAvailable) {
  188. jQuery.fx.timer = function (timer) {
  189. if (timer() && jQuery.timers.push(timer) && !animating) {
  190. animating = true;
  191. raf();
  192. }
  193. };
  194. jQuery.fx.stop = function () {
  195. animating = false;
  196. };
  197. }
  198. } else {
  199. // polyfill
  200. window.requestAnimationFrame = function (callback) {
  201. var currTime = new Date().getTime(),
  202. timeToCall = Math.max(0, 16 - (currTime - lastTime)),
  203. id = window.setTimeout(function () {
  204. callback(currTime + timeToCall);
  205. }, timeToCall);
  206. lastTime = currTime + timeToCall;
  207. return id;
  208. };
  209. window.cancelAnimationFrame = function (id) {
  210. clearTimeout(id);
  211. };
  212. }
  213. }( jQuery ));
  214. function removeQuotes (string) {
  215. if (typeof string === 'string' || string instanceof String) {
  216. string = string.replace(/^['\\/"]+|(;\s?})+|['\\/"]+$/g, '');
  217. }
  218. return string;
  219. }
  220. window.Foundation = {
  221. name : 'Foundation',
  222. version : '5.2.2',
  223. media_queries : {
  224. small : S('.foundation-mq-small').css('font-family').replace(/^[\/\\'"]+|(;\s?})+|[\/\\'"]+$/g, ''),
  225. medium : S('.foundation-mq-medium').css('font-family').replace(/^[\/\\'"]+|(;\s?})+|[\/\\'"]+$/g, ''),
  226. large : S('.foundation-mq-large').css('font-family').replace(/^[\/\\'"]+|(;\s?})+|[\/\\'"]+$/g, ''),
  227. xlarge: S('.foundation-mq-xlarge').css('font-family').replace(/^[\/\\'"]+|(;\s?})+|[\/\\'"]+$/g, ''),
  228. xxlarge: S('.foundation-mq-xxlarge').css('font-family').replace(/^[\/\\'"]+|(;\s?})+|[\/\\'"]+$/g, '')
  229. },
  230. stylesheet : $('<style></style>').appendTo('head')[0].sheet,
  231. global: {
  232. namespace: undefined
  233. },
  234. init : function (scope, libraries, method, options, response) {
  235. var args = [scope, method, options, response],
  236. responses = [];
  237. // check RTL
  238. this.rtl = /rtl/i.test(S('html').attr('dir'));
  239. // set foundation global scope
  240. this.scope = scope || this.scope;
  241. this.set_namespace();
  242. if (libraries && typeof libraries === 'string' && !/reflow/i.test(libraries)) {
  243. if (this.libs.hasOwnProperty(libraries)) {
  244. responses.push(this.init_lib(libraries, args));
  245. }
  246. } else {
  247. for (var lib in this.libs) {
  248. responses.push(this.init_lib(lib, libraries));
  249. }
  250. }
  251. return scope;
  252. },
  253. init_lib : function (lib, args) {
  254. if (this.libs.hasOwnProperty(lib)) {
  255. this.patch(this.libs[lib]);
  256. if (args && args.hasOwnProperty(lib)) {
  257. if (typeof this.libs[lib].settings !== 'undefined') {
  258. $.extend(true, this.libs[lib].settings, args[lib]);
  259. }
  260. else if (typeof this.libs[lib].defaults !== 'undefined') {
  261. $.extend(true, this.libs[lib].defaults, args[lib]);
  262. }
  263. return this.libs[lib].init.apply(this.libs[lib], [this.scope, args[lib]]);
  264. }
  265. args = args instanceof Array ? args : new Array(args); // PATCH: added this line
  266. return this.libs[lib].init.apply(this.libs[lib], args);
  267. }
  268. return function () {};
  269. },
  270. patch : function (lib) {
  271. lib.scope = this.scope;
  272. lib.namespace = this.global.namespace;
  273. lib.rtl = this.rtl;
  274. lib['data_options'] = this.utils.data_options;
  275. lib['attr_name'] = attr_name;
  276. lib['add_namespace'] = add_namespace;
  277. lib['bindings'] = bindings;
  278. lib['S'] = this.utils.S;
  279. },
  280. inherit : function (scope, methods) {
  281. var methods_arr = methods.split(' '),
  282. i = methods_arr.length;
  283. while (i--) {
  284. if (this.utils.hasOwnProperty(methods_arr[i])) {
  285. scope[methods_arr[i]] = this.utils[methods_arr[i]];
  286. }
  287. }
  288. },
  289. set_namespace: function () {
  290. // Description:
  291. // Don't bother reading the namespace out of the meta tag
  292. // if the namespace has been set globally in javascript
  293. //
  294. // Example:
  295. // Foundation.global.namespace = 'my-namespace';
  296. // or make it an empty string:
  297. // Foundation.global.namespace = '';
  298. //
  299. //
  300. // If the namespace has not been set (is undefined), try to read it out of the meta element.
  301. // Otherwise use the globally defined namespace, even if it's empty ('')
  302. var namespace = ( this.global.namespace === undefined ) ? $('.foundation-data-attribute-namespace').css('font-family') : this.global.namespace;
  303. // Finally, if the namsepace is either undefined or false, set it to an empty string.
  304. // Otherwise use the namespace value.
  305. this.global.namespace = ( namespace === undefined || /false/i.test(namespace) ) ? '' : namespace;
  306. },
  307. libs : {},
  308. // methods that can be inherited in libraries
  309. utils : {
  310. // Description:
  311. // Fast Selector wrapper returns jQuery object. Only use where getElementById
  312. // is not available.
  313. //
  314. // Arguments:
  315. // Selector (String): CSS selector describing the element(s) to be
  316. // returned as a jQuery object.
  317. //
  318. // Scope (String): CSS selector describing the area to be searched. Default
  319. // is document.
  320. //
  321. // Returns:
  322. // Element (jQuery Object): jQuery object containing elements matching the
  323. // selector within the scope.
  324. S : S,
  325. // Description:
  326. // Executes a function a max of once every n milliseconds
  327. //
  328. // Arguments:
  329. // Func (Function): Function to be throttled.
  330. //
  331. // Delay (Integer): Function execution threshold in milliseconds.
  332. //
  333. // Returns:
  334. // Lazy_function (Function): Function with throttling applied.
  335. throttle : function (func, delay) {
  336. var timer = null;
  337. return function () {
  338. var context = this, args = arguments;
  339. if (timer == null) {
  340. timer = setTimeout(function () {
  341. func.apply(context, args);
  342. timer = null;
  343. }, delay);
  344. }
  345. };
  346. },
  347. // Description:
  348. // Executes a function when it stops being invoked for n seconds
  349. // Modified version of _.debounce() http://underscorejs.org
  350. //
  351. // Arguments:
  352. // Func (Function): Function to be debounced.
  353. //
  354. // Delay (Integer): Function execution threshold in milliseconds.
  355. //
  356. // Immediate (Bool): Whether the function should be called at the beginning
  357. // of the delay instead of the end. Default is false.
  358. //
  359. // Returns:
  360. // Lazy_function (Function): Function with debouncing applied.
  361. debounce : function (func, delay, immediate) {
  362. var timeout, result;
  363. return function () {
  364. var context = this, args = arguments;
  365. var later = function () {
  366. timeout = null;
  367. if (!immediate) result = func.apply(context, args);
  368. };
  369. var callNow = immediate && !timeout;
  370. clearTimeout(timeout);
  371. timeout = setTimeout(later, delay);
  372. if (callNow) result = func.apply(context, args);
  373. return result;
  374. };
  375. },
  376. // Description:
  377. // Parses data-options attribute
  378. //
  379. // Arguments:
  380. // El (jQuery Object): Element to be parsed.
  381. //
  382. // Returns:
  383. // Options (Javascript Object): Contents of the element's data-options
  384. // attribute.
  385. data_options : function (el) {
  386. var opts = {}, ii, p, opts_arr,
  387. data_options = function (el) {
  388. var namespace = Foundation.global.namespace;
  389. if (namespace.length > 0) {
  390. return el.data(namespace + '-options');
  391. }
  392. return el.data('options');
  393. };
  394. var cached_options = data_options(el);
  395. if (typeof cached_options === 'object') {
  396. return cached_options;
  397. }
  398. opts_arr = (cached_options || ':').split(';');
  399. ii = opts_arr.length;
  400. function isNumber (o) {
  401. return ! isNaN (o-0) && o !== null && o !== "" && o !== false && o !== true;
  402. }
  403. function trim (str) {
  404. if (typeof str === 'string') return $.trim(str);
  405. return str;
  406. }
  407. while (ii--) {
  408. p = opts_arr[ii].split(':');
  409. if (/true/i.test(p[1])) p[1] = true;
  410. if (/false/i.test(p[1])) p[1] = false;
  411. if (isNumber(p[1])) {
  412. if (p[1].indexOf('.') === -1) {
  413. p[1] = parseInt(p[1], 10);
  414. } else {
  415. p[1] = parseFloat(p[1]);
  416. }
  417. }
  418. if (p.length === 2 && p[0].length > 0) {
  419. opts[trim(p[0])] = trim(p[1]);
  420. }
  421. }
  422. return opts;
  423. },
  424. // Description:
  425. // Adds JS-recognizable media queries
  426. //
  427. // Arguments:
  428. // Media (String): Key string for the media query to be stored as in
  429. // Foundation.media_queries
  430. //
  431. // Class (String): Class name for the generated <meta> tag
  432. register_media : function (media, media_class) {
  433. if(Foundation.media_queries[media] === undefined) {
  434. $('head').append('<meta class="' + media_class + '">');
  435. Foundation.media_queries[media] = removeQuotes($('.' + media_class).css('font-family'));
  436. }
  437. },
  438. // Description:
  439. // Add custom CSS within a JS-defined media query
  440. //
  441. // Arguments:
  442. // Rule (String): CSS rule to be appended to the document.
  443. //
  444. // Media (String): Optional media query string for the CSS rule to be
  445. // nested under.
  446. add_custom_rule : function (rule, media) {
  447. if (media === undefined && Foundation.stylesheet) {
  448. Foundation.stylesheet.insertRule(rule, Foundation.stylesheet.cssRules.length);
  449. } else {
  450. var query = Foundation.media_queries[media];
  451. if (query !== undefined) {
  452. Foundation.stylesheet.insertRule('@media ' +
  453. Foundation.media_queries[media] + '{ ' + rule + ' }');
  454. }
  455. }
  456. },
  457. // Description:
  458. // Performs a callback function when an image is fully loaded
  459. //
  460. // Arguments:
  461. // Image (jQuery Object): Image(s) to check if loaded.
  462. //
  463. // Callback (Function): Fundation to execute when image is fully loaded.
  464. image_loaded : function (images, callback) {
  465. var self = this,
  466. unloaded = images.length;
  467. if (unloaded === 0) {
  468. callback(images);
  469. }
  470. images.each(function () {
  471. single_image_loaded(self.S(this), function () {
  472. unloaded -= 1;
  473. if (unloaded === 0) {
  474. callback(images);
  475. }
  476. });
  477. });
  478. },
  479. // Description:
  480. // Returns a random, alphanumeric string
  481. //
  482. // Arguments:
  483. // Length (Integer): Length of string to be generated. Defaults to random
  484. // integer.
  485. //
  486. // Returns:
  487. // Rand (String): Pseudo-random, alphanumeric string.
  488. random_str : function () {
  489. if (!this.fidx) this.fidx = 0;
  490. this.prefix = this.prefix || [(this.name || 'F'), (+new Date).toString(36)].join('-');
  491. return this.prefix + (this.fidx++).toString(36);
  492. }
  493. }
  494. };
  495. $.fn.foundation = function () {
  496. var args = Array.prototype.slice.call(arguments, 0);
  497. return this.each(function () {
  498. Foundation.init.apply(Foundation, [this].concat(args));
  499. return this;
  500. });
  501. };
  502. }(jQuery, this, this.document));