foundation.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Foundation Responsive Library
  3. * http://foundation.zurb.com
  4. * Copyright 2013, 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. // Used to retrieve Foundation media queries from CSS.
  11. if($('head').has('.foundation-mq-small').length === 0) {
  12. $('head').append('<meta class="foundation-mq-small">');
  13. }
  14. if($('head').has('.foundation-mq-medium').length === 0) {
  15. $('head').append('<meta class="foundation-mq-medium">');
  16. }
  17. if($('head').has('.foundation-mq-large').length === 0) {
  18. $('head').append('<meta class="foundation-mq-large">');
  19. }
  20. if($('head').has('.foundation-mq-xlarge').length === 0) {
  21. $('head').append('<meta class="foundation-mq-xlarge">');
  22. }
  23. if($('head').has('.foundation-mq-xxlarge').length === 0) {
  24. $('head').append('<meta class="foundation-mq-xxlarge">');
  25. }
  26. // Embed FastClick (this should be removed later)
  27. function FastClick(layer){'use strict';var oldOnClick,self=this;this.trackingClick=false;this.trackingClickStart=0;this.targetElement=null;this.touchStartX=0;this.touchStartY=0;this.lastTouchIdentifier=0;this.touchBoundary=10;this.layer=layer;if(!layer||!layer.nodeType){throw new TypeError('Layer must be a document node');}this.onClick=function(){return FastClick.prototype.onClick.apply(self,arguments)};this.onMouse=function(){return FastClick.prototype.onMouse.apply(self,arguments)};this.onTouchStart=function(){return FastClick.prototype.onTouchStart.apply(self,arguments)};this.onTouchMove=function(){return FastClick.prototype.onTouchMove.apply(self,arguments)};this.onTouchEnd=function(){return FastClick.prototype.onTouchEnd.apply(self,arguments)};this.onTouchCancel=function(){return FastClick.prototype.onTouchCancel.apply(self,arguments)};if(FastClick.notNeeded(layer)){return}if(this.deviceIsAndroid){layer.addEventListener('mouseover',this.onMouse,true);layer.addEventListener('mousedown',this.onMouse,true);layer.addEventListener('mouseup',this.onMouse,true)}layer.addEventListener('click',this.onClick,true);layer.addEventListener('touchstart',this.onTouchStart,false);layer.addEventListener('touchmove',this.onTouchMove,false);layer.addEventListener('touchend',this.onTouchEnd,false);layer.addEventListener('touchcancel',this.onTouchCancel,false);if(!Event.prototype.stopImmediatePropagation){layer.removeEventListener=function(type,callback,capture){var rmv=Node.prototype.removeEventListener;if(type==='click'){rmv.call(layer,type,callback.hijacked||callback,capture)}else{rmv.call(layer,type,callback,capture)}};layer.addEventListener=function(type,callback,capture){var adv=Node.prototype.addEventListener;if(type==='click'){adv.call(layer,type,callback.hijacked||(callback.hijacked=function(event){if(!event.propagationStopped){callback(event)}}),capture)}else{adv.call(layer,type,callback,capture)}}}if(typeof layer.onclick==='function'){oldOnClick=layer.onclick;layer.addEventListener('click',function(event){oldOnClick(event)},false);layer.onclick=null}}FastClick.prototype.deviceIsAndroid=navigator.userAgent.indexOf('Android')>0;FastClick.prototype.deviceIsIOS=/iP(ad|hone|od)/.test(navigator.userAgent);FastClick.prototype.deviceIsIOS4=FastClick.prototype.deviceIsIOS&&(/OS 4_\d(_\d)?/).test(navigator.userAgent);FastClick.prototype.deviceIsIOSWithBadTarget=FastClick.prototype.deviceIsIOS&&(/OS ([6-9]|\d{2})_\d/).test(navigator.userAgent);FastClick.prototype.needsClick=function(target){'use strict';switch(target.nodeName.toLowerCase()){case'button':case'select':case'textarea':if(target.disabled){return true}break;case'input':if((this.deviceIsIOS&&target.type==='file')||target.disabled){return true}break;case'label':case'video':return true}return(/\bneedsclick\b/).test(target.className)};FastClick.prototype.needsFocus=function(target){'use strict';switch(target.nodeName.toLowerCase()){case'textarea':case'select':return true;case'input':switch(target.type){case'button':case'checkbox':case'file':case'image':case'radio':case'submit':return false}return!target.disabled&&!target.readOnly;default:return(/\bneedsfocus\b/).test(target.className)}};FastClick.prototype.sendClick=function(targetElement,event){'use strict';var clickEvent,touch;if(document.activeElement&&document.activeElement!==targetElement){document.activeElement.blur()}touch=event.changedTouches[0];clickEvent=document.createEvent('MouseEvents');clickEvent.initMouseEvent('click',true,true,window,1,touch.screenX,touch.screenY,touch.clientX,touch.clientY,false,false,false,false,0,null);clickEvent.forwardedTouchEvent=true;targetElement.dispatchEvent(clickEvent)};FastClick.prototype.focus=function(targetElement){'use strict';var length;if(this.deviceIsIOS&&targetElement.setSelectionRange){length=targetElement.value.length;targetElement.setSelectionRange(length,length)}else{targetElement.focus()}};FastClick.prototype.updateScrollParent=function(targetElement){'use strict';var scrollParent,parentElement;scrollParent=targetElement.fastClickScrollParent;if(!scrollParent||!scrollParent.contains(targetElement)){parentElement=targetElement;do{if(parentElement.scrollHeight>parentElement.offsetHeight){scrollParent=parentElement;targetElement.fastClickScrollParent=parentElement;break}parentElement=parentElement.parentElement}while(parentElement)}if(scrollParent){scrollParent.fastClickLastScrollTop=scrollParent.scrollTop}};FastClick.prototype.getTargetElementFromEventTarget=function(eventTarget){'use strict';if(eventTarget.nodeType===Node.TEXT_NODE){return eventTarget.parentNode}return eventTarget};FastClick.prototype.onTouchStart=function(event){'use strict';var targetElement,touch,selection;if(event.targetTouches.length>1){return true}targetElement=this.getTargetElementFromEventTarget(event.target);touch=event.targetTouches[0];if(this.deviceIsIOS){selection=window.getSelection();if(selection.rangeCount&&!selection.isCollapsed){return true}if(!this.deviceIsIOS4){if(touch.identifier===this.lastTouchIdentifier){event.preventDefault();return false}this.lastTouchIdentifier=touch.identifier;this.updateScrollParent(targetElement)}}this.trackingClick=true;this.trackingClickStart=event.timeStamp;this.targetElement=targetElement;this.touchStartX=touch.pageX;this.touchStartY=touch.pageY;if((event.timeStamp-this.lastClickTime)<200){event.preventDefault()}return true};FastClick.prototype.touchHasMoved=function(event){'use strict';var touch=event.changedTouches[0],boundary=this.touchBoundary;if(Math.abs(touch.pageX-this.touchStartX)>boundary||Math.abs(touch.pageY-this.touchStartY)>boundary){return true}return false};FastClick.prototype.onTouchMove=function(event){'use strict';if(!this.trackingClick){return true}if(this.targetElement!==this.getTargetElementFromEventTarget(event.target)||this.touchHasMoved(event)){this.trackingClick=false;this.targetElement=null}return true};FastClick.prototype.findControl=function(labelElement){'use strict';if(labelElement.control!==undefined){return labelElement.control}if(labelElement.htmlFor){return document.getElementById(labelElement.htmlFor)}return labelElement.querySelector('button, input:not([type=hidden]), keygen, meter, output, progress, select, textarea')};FastClick.prototype.onTouchEnd=function(event){'use strict';var forElement,trackingClickStart,targetTagName,scrollParent,touch,targetElement=this.targetElement;if(!this.trackingClick){return true}if((event.timeStamp-this.lastClickTime)<200){this.cancelNextClick=true;return true}this.lastClickTime=event.timeStamp;trackingClickStart=this.trackingClickStart;this.trackingClick=false;this.trackingClickStart=0;if(this.deviceIsIOSWithBadTarget){touch=event.changedTouches[0];targetElement=document.elementFromPoint(touch.pageX-window.pageXOffset,touch.pageY-window.pageYOffset)||targetElement;targetElement.fastClickScrollParent=this.targetElement.fastClickScrollParent}targetTagName=targetElement.tagName.toLowerCase();if(targetTagName==='label'){forElement=this.findControl(targetElement);if(forElement){this.focus(targetElement);if(this.deviceIsAndroid){return false}targetElement=forElement}}else if(this.needsFocus(targetElement)){if((event.timeStamp-trackingClickStart)>100||(this.deviceIsIOS&&window.top!==window&&targetTagName==='input')){this.targetElement=null;return false}this.focus(targetElement);if(!this.deviceIsIOS4||targetTagName!=='select'){this.targetElement=null;event.preventDefault()}return false}if(this.deviceIsIOS&&!this.deviceIsIOS4){scrollParent=targetElement.fastClickScrollParent;if(scrollParent&&scrollParent.fastClickLastScrollTop!==scrollParent.scrollTop){return true}}if(!this.needsClick(targetElement)){event.preventDefault();this.sendClick(targetElement,event)}return false};FastClick.prototype.onTouchCancel=function(){'use strict';this.trackingClick=false;this.targetElement=null};FastClick.prototype.onMouse=function(event){'use strict';if(!this.targetElement){return true}if(event.forwardedTouchEvent){return true}if(!event.cancelable){return true}if(!this.needsClick(this.targetElement)||this.cancelNextClick){if(event.stopImmediatePropagation){event.stopImmediatePropagation()}else{event.propagationStopped=true}event.stopPropagation();event.preventDefault();return false}return true};FastClick.prototype.onClick=function(event){'use strict';var permitted;if(this.trackingClick){this.targetElement=null;this.trackingClick=false;return true}if(event.target.type==='submit'&&event.detail===0){return true}permitted=this.onMouse(event);if(!permitted){this.targetElement=null}return permitted};FastClick.prototype.destroy=function(){'use strict';var layer=this.layer;if(this.deviceIsAndroid){layer.removeEventListener('mouseover',this.onMouse,true);layer.removeEventListener('mousedown',this.onMouse,true);layer.removeEventListener('mouseup',this.onMouse,true)}layer.removeEventListener('click',this.onClick,true);layer.removeEventListener('touchstart',this.onTouchStart,false);layer.removeEventListener('touchmove',this.onTouchMove,false);layer.removeEventListener('touchend',this.onTouchEnd,false);layer.removeEventListener('touchcancel',this.onTouchCancel,false)};FastClick.notNeeded=function(layer){'use strict';var metaViewport;if(typeof window.ontouchstart==='undefined'){return true}if((/Chrome\/[0-9]+/).test(navigator.userAgent)){if(FastClick.prototype.deviceIsAndroid){metaViewport=document.querySelector('meta[name=viewport]');if(metaViewport&&metaViewport.content.indexOf('user-scalable=no')!==-1){return true}}else{return true}}if(layer.style.msTouchAction==='none'){return true}return false};FastClick.attach=function(layer){'use strict';return new FastClick(layer)};if(typeof define!=='undefined'&&define.amd){define(function(){'use strict';return FastClick})}else if(typeof module!=='undefined'&&module.exports){module.exports=FastClick.attach;module.exports.FastClick=FastClick}else{window.FastClick=FastClick}
  28. // Enable FastClick
  29. if(typeof FastClick !== 'undefined') {
  30. FastClick.attach(document.body);
  31. }
  32. // private Fast Selector wrapper,
  33. // returns jQuery object. Only use where
  34. // getElementById is not available.
  35. var S = function (selector, context) {
  36. if (typeof selector === 'string') {
  37. if (context) {
  38. return $(context.querySelectorAll(selector));
  39. }
  40. return $(document.querySelectorAll(selector));
  41. }
  42. return $(selector, context);
  43. };
  44. /*
  45. https://github.com/paulirish/matchMedia.js
  46. */
  47. window.matchMedia = window.matchMedia || (function( doc, undefined ) {
  48. "use strict";
  49. var bool,
  50. docElem = doc.documentElement,
  51. refNode = docElem.firstElementChild || docElem.firstChild,
  52. // fakeBody required for <FF4 when executed in <head>
  53. fakeBody = doc.createElement( "body" ),
  54. div = doc.createElement( "div" );
  55. div.id = "mq-test-1";
  56. div.style.cssText = "position:absolute;top:-100em";
  57. fakeBody.style.background = "none";
  58. fakeBody.appendChild(div);
  59. return function(q){
  60. div.innerHTML = "&shy;<style media=\"" + q + "\"> #mq-test-1 { width: 42px; }</style>";
  61. docElem.insertBefore( fakeBody, refNode );
  62. bool = div.offsetWidth === 42;
  63. docElem.removeChild( fakeBody );
  64. return {
  65. matches: bool,
  66. media: q
  67. };
  68. };
  69. }( document ));
  70. /*
  71. * jquery.requestAnimationFrame
  72. * https://github.com/gnarf37/jquery-requestAnimationFrame
  73. * Requires jQuery 1.8+
  74. *
  75. * Copyright (c) 2012 Corey Frang
  76. * Licensed under the MIT license.
  77. */
  78. (function( $ ) {
  79. // requestAnimationFrame polyfill adapted from Erik Möller
  80. // fixes from Paul Irish and Tino Zijdel
  81. // http://paulirish.com/2011/requestanimationframe-for-smart-animating/
  82. // http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
  83. var animating,
  84. lastTime = 0,
  85. vendors = ['webkit', 'moz'],
  86. requestAnimationFrame = window.requestAnimationFrame,
  87. cancelAnimationFrame = window.cancelAnimationFrame;
  88. for(; lastTime < vendors.length && !requestAnimationFrame; lastTime++) {
  89. requestAnimationFrame = window[ vendors[lastTime] + "RequestAnimationFrame" ];
  90. cancelAnimationFrame = cancelAnimationFrame ||
  91. window[ vendors[lastTime] + "CancelAnimationFrame" ] ||
  92. window[ vendors[lastTime] + "CancelRequestAnimationFrame" ];
  93. }
  94. function raf() {
  95. if ( animating ) {
  96. requestAnimationFrame( raf );
  97. jQuery.fx.tick();
  98. }
  99. }
  100. if ( requestAnimationFrame ) {
  101. // use rAF
  102. window.requestAnimationFrame = requestAnimationFrame;
  103. window.cancelAnimationFrame = cancelAnimationFrame;
  104. jQuery.fx.timer = function( timer ) {
  105. if ( timer() && jQuery.timers.push( timer ) && !animating ) {
  106. animating = true;
  107. raf();
  108. }
  109. };
  110. jQuery.fx.stop = function() {
  111. animating = false;
  112. };
  113. } else {
  114. // polyfill
  115. window.requestAnimationFrame = function( callback, element ) {
  116. var currTime = new Date().getTime(),
  117. timeToCall = Math.max( 0, 16 - ( currTime - lastTime ) ),
  118. id = window.setTimeout( function() {
  119. callback( currTime + timeToCall );
  120. }, timeToCall );
  121. lastTime = currTime + timeToCall;
  122. return id;
  123. };
  124. window.cancelAnimationFrame = function(id) {
  125. clearTimeout(id);
  126. };
  127. }
  128. }( jQuery ));
  129. function removeQuotes (string) {
  130. if (typeof string === 'string' || string instanceof String) {
  131. string = string.replace(/^[\\/'"]+|(;\s?})+|[\\/'"]+$/g, '');
  132. }
  133. return string;
  134. }
  135. window.Foundation = {
  136. name : 'Foundation',
  137. version : '5.0.0',
  138. media_queries : {
  139. small : S('.foundation-mq-small').css('font-family').replace(/^[\/\\'"]+|(;\s?})+|[\/\\'"]+$/g, ''),
  140. medium : S('.foundation-mq-medium').css('font-family').replace(/^[\/\\'"]+|(;\s?})+|[\/\\'"]+$/g, ''),
  141. large : S('.foundation-mq-large').css('font-family').replace(/^[\/\\'"]+|(;\s?})+|[\/\\'"]+$/g, ''),
  142. xlarge: S('.foundation-mq-xlarge').css('font-family').replace(/^[\/\\'"]+|(;\s?})+|[\/\\'"]+$/g, ''),
  143. xxlarge: S('.foundation-mq-xxlarge').css('font-family').replace(/^[\/\\'"]+|(;\s?})+|[\/\\'"]+$/g, '')
  144. },
  145. stylesheet : $('<style></style>').appendTo('head')[0].sheet,
  146. init : function (scope, libraries, method, options, response) {
  147. var library_arr,
  148. args = [scope, method, options, response],
  149. responses = [];
  150. // check RTL
  151. this.rtl = /rtl/i.test(S('html').attr('dir'));
  152. // set foundation global scope
  153. this.scope = scope || this.scope;
  154. if (libraries && typeof libraries === 'string' && !/reflow/i.test(libraries)) {
  155. if (this.libs.hasOwnProperty(libraries)) {
  156. responses.push(this.init_lib(libraries, args));
  157. }
  158. } else {
  159. for (var lib in this.libs) {
  160. responses.push(this.init_lib(lib, libraries));
  161. }
  162. }
  163. return scope;
  164. },
  165. init_lib : function (lib, args) {
  166. if (this.libs.hasOwnProperty(lib)) {
  167. this.patch(this.libs[lib]);
  168. if (args && args.hasOwnProperty(lib)) {
  169. return this.libs[lib].init.apply(this.libs[lib], [this.scope, args[lib]]);
  170. }
  171. return this.libs[lib].init.apply(this.libs[lib], args);
  172. }
  173. return function () {};
  174. },
  175. patch : function (lib) {
  176. lib.scope = this.scope;
  177. lib['data_options'] = this.lib_methods.data_options;
  178. lib['bindings'] = this.lib_methods.bindings;
  179. lib['S'] = S;
  180. lib.rtl = this.rtl;
  181. },
  182. inherit : function (scope, methods) {
  183. var methods_arr = methods.split(' ');
  184. for (var i = methods_arr.length - 1; i >= 0; i--) {
  185. if (this.lib_methods.hasOwnProperty(methods_arr[i])) {
  186. this.libs[scope.name][methods_arr[i]] = this.lib_methods[methods_arr[i]];
  187. }
  188. }
  189. },
  190. random_str : function (length) {
  191. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  192. if (!length) {
  193. length = Math.floor(Math.random() * chars.length);
  194. }
  195. var str = '';
  196. for (var i = 0; i < length; i++) {
  197. str += chars[Math.floor(Math.random() * chars.length)];
  198. }
  199. return str;
  200. },
  201. libs : {},
  202. // methods that can be inherited in libraries
  203. lib_methods : {
  204. throttle : function(fun, delay) {
  205. var timer = null;
  206. return function () {
  207. var context = this, args = arguments;
  208. clearTimeout(timer);
  209. timer = setTimeout(function () {
  210. fun.apply(context, args);
  211. }, delay);
  212. };
  213. },
  214. // parses data-options attribute
  215. data_options : function (el) {
  216. var opts = {}, ii, p, opts_arr, opts_len,
  217. data_options = el.data('options');
  218. if (typeof data_options === 'object') {
  219. return data_options;
  220. }
  221. opts_arr = (data_options || ':').split(';'),
  222. opts_len = opts_arr.length;
  223. function isNumber (o) {
  224. return ! isNaN (o-0) && o !== null && o !== "" && o !== false && o !== true;
  225. }
  226. function trim(str) {
  227. if (typeof str === 'string') return $.trim(str);
  228. return str;
  229. }
  230. // parse options
  231. for (ii = opts_len - 1; ii >= 0; ii--) {
  232. p = opts_arr[ii].split(':');
  233. if (/true/i.test(p[1])) p[1] = true;
  234. if (/false/i.test(p[1])) p[1] = false;
  235. if (isNumber(p[1])) p[1] = parseInt(p[1], 10);
  236. if (p.length === 2 && p[0].length > 0) {
  237. opts[trim(p[0])] = trim(p[1]);
  238. }
  239. }
  240. return opts;
  241. },
  242. delay : function (fun, delay) {
  243. return setTimeout(fun, delay);
  244. },
  245. // test for empty object or array
  246. empty : function (obj) {
  247. if (obj.length && obj.length > 0) return false;
  248. if (obj.length && obj.length === 0) return true;
  249. for (var key in obj) {
  250. if (hasOwnProperty.call(obj, key)) return false;
  251. }
  252. return true;
  253. },
  254. register_media : function(media, media_class) {
  255. if(Foundation.media_queries[media] === undefined) {
  256. $('head').append('<meta class="' + media_class + '">');
  257. Foundation.media_queries[media] = removeQuotes($('.' + media_class).css('font-family'));
  258. }
  259. },
  260. addCustomRule : function(rule, media) {
  261. if(media === undefined) {
  262. Foundation.stylesheet.insertRule(rule, Foundation.stylesheet.cssRules.length);
  263. } else {
  264. var query = Foundation.media_queries[media];
  265. if(query !== undefined) {
  266. Foundation.stylesheet.insertRule('@media ' +
  267. Foundation.media_queries[media] + '{ ' + rule + ' }');
  268. }
  269. }
  270. },
  271. loaded : function (image, callback) {
  272. function loaded () {
  273. callback(image[0]);
  274. }
  275. function bindLoad () {
  276. this.one('load', loaded);
  277. if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
  278. var src = this.attr( 'src' ),
  279. param = src.match( /\?/ ) ? '&' : '?';
  280. param += 'random=' + (new Date()).getTime();
  281. this.attr('src', src + param);
  282. }
  283. }
  284. if (!image.attr('src')) {
  285. loaded();
  286. return;
  287. }
  288. if (image[0].complete || image[0].readyState === 4) {
  289. loaded();
  290. } else {
  291. bindLoad.call(image);
  292. }
  293. },
  294. bindings : function (method, options) {
  295. var self = this,
  296. should_bind_events = !S(this).data(this.name + '-init');
  297. if (typeof method === 'string') {
  298. return this[method].call(this);
  299. }
  300. if (S(this.scope).is('[data-' + this.name +']')) {
  301. S(this.scope).data(this.name + '-init', $.extend({}, this.settings, (options || method), this.data_options(S(this.scope))));
  302. if (should_bind_events) {
  303. this.events(this.scope);
  304. }
  305. } else {
  306. S('[data-' + this.name + ']', this.scope).each(function () {
  307. var should_bind_events = !S(this).data(self.name + '-init');
  308. S(this).data(self.name + '-init', $.extend({}, self.settings, (options || method), self.data_options(S(this))));
  309. if (should_bind_events) {
  310. self.events(this);
  311. }
  312. });
  313. }
  314. }
  315. }
  316. };
  317. $.fn.foundation = function () {
  318. var args = Array.prototype.slice.call(arguments, 0);
  319. return this.each(function () {
  320. Foundation.init.apply(Foundation, [this].concat(args));
  321. return this;
  322. });
  323. };
  324. }(jQuery, this, this.document));