PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/files/foundation/4.0.9/js/foundation/foundation.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 372 lines | 274 code | 69 blank | 29 comment | 66 complexity | a0749c6a34c67f1a27b61d2e5bc616cb MD5 | raw file
  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. /*jslint unparam: true, browser: true, indent: 2 */
  9. // Accommodate running jQuery or Zepto in noConflict() mode by
  10. // using an anonymous function to redefine the $ shorthand name.
  11. // See http://docs.jquery.com/Using_jQuery_with_Other_Libraries
  12. // and http://zeptojs.com/
  13. var libFuncName = null;
  14. if (typeof jQuery === "undefined" &&
  15. typeof Zepto === "undefined" &&
  16. typeof $ === "function") {
  17. libFuncName = $;
  18. } else if (typeof jQuery === "function") {
  19. libFuncName = jQuery;
  20. } else if (typeof Zepto === "function") {
  21. libFuncName = Zepto;
  22. } else {
  23. throw new TypeError();
  24. }
  25. (function ($) {
  26. (function () {
  27. // add dusty browser stuff
  28. if (!Array.prototype.filter) {
  29. Array.prototype.filter = function(fun /*, thisp */) {
  30. "use strict";
  31. if (this == null) {
  32. throw new TypeError();
  33. }
  34. var t = Object(this),
  35. len = t.length >>> 0;
  36. if (typeof fun != "function") {
  37. try {
  38. throw new TypeError();
  39. } catch (e) {
  40. return;
  41. }
  42. }
  43. var res = [],
  44. thisp = arguments[1];
  45. for (var i = 0; i < len; i++) {
  46. if (i in t) {
  47. var val = t[i]; // in case fun mutates this
  48. if (fun && fun.call(thisp, val, i, t)) {
  49. res.push(val);
  50. }
  51. }
  52. }
  53. return res;
  54. };
  55. if (!Function.prototype.bind) {
  56. Function.prototype.bind = function (oThis) {
  57. if (typeof this !== "function") {
  58. // closest thing possible to the ECMAScript 5 internal IsCallable function
  59. throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
  60. }
  61. var aArgs = Array.prototype.slice.call(arguments, 1),
  62. fToBind = this,
  63. fNOP = function () {},
  64. fBound = function () {
  65. return fToBind.apply(this instanceof fNOP && oThis
  66. ? this
  67. : oThis,
  68. aArgs.concat(Array.prototype.slice.call(arguments)));
  69. };
  70. fNOP.prototype = this.prototype;
  71. fBound.prototype = new fNOP();
  72. return fBound;
  73. };
  74. }
  75. }
  76. // fake stop() for zepto.
  77. $.fn.stop = $.fn.stop || function() {
  78. return this;
  79. };
  80. }());
  81. ;(function (window, document, undefined) {
  82. 'use strict';
  83. window.Foundation = {
  84. name : 'Foundation',
  85. version : '4.0.8',
  86. // global Foundation cache object
  87. cache : {},
  88. init : function (scope, libraries, method, options, response, /* internal */ nc) {
  89. var library_arr,
  90. args = [scope, method, options, response],
  91. responses = [],
  92. nc = nc || false;
  93. // disable library error catching,
  94. // used for development only
  95. if (nc) this.nc = nc;
  96. // set foundation global scope
  97. this.scope = scope || this.scope;
  98. if (libraries && typeof libraries === 'string') {
  99. if (/off/i.test(libraries)) return this.off();
  100. library_arr = libraries.split(' ');
  101. if (library_arr.length > 0) {
  102. for (var i = library_arr.length - 1; i >= 0; i--) {
  103. responses.push(this.init_lib(library_arr[i], args));
  104. }
  105. }
  106. } else {
  107. for (var lib in this.libs) {
  108. responses.push(this.init_lib(lib, args));
  109. }
  110. }
  111. // if first argument is callback, add to args
  112. if (typeof libraries === 'function') {
  113. args.unshift(libraries);
  114. }
  115. return this.response_obj(responses, args);
  116. },
  117. response_obj : function (response_arr, args) {
  118. for (var callback in args) {
  119. if (typeof args[callback] === 'function') {
  120. return args[callback]({
  121. errors: response_arr.filter(function (s) {
  122. if (typeof s === 'string') return s;
  123. })
  124. });
  125. }
  126. }
  127. return response_arr;
  128. },
  129. init_lib : function (lib, args) {
  130. return this.trap(function () {
  131. if (this.libs.hasOwnProperty(lib)) {
  132. this.patch(this.libs[lib]);
  133. return this.libs[lib].init.apply(this.libs[lib], args);
  134. }
  135. }.bind(this), lib);
  136. },
  137. trap : function (fun, lib) {
  138. if (!this.nc) {
  139. try {
  140. return fun();
  141. } catch (e) {
  142. return this.error({name: lib, message: 'could not be initialized', more: e.name + ' ' + e.message});
  143. }
  144. }
  145. return fun();
  146. },
  147. patch : function (lib) {
  148. this.fix_outer(lib);
  149. },
  150. inherit : function (scope, methods) {
  151. var methods_arr = methods.split(' ');
  152. for (var i = methods_arr.length - 1; i >= 0; i--) {
  153. if (this.lib_methods.hasOwnProperty(methods_arr[i])) {
  154. this.libs[scope.name][methods_arr[i]] = this.lib_methods[methods_arr[i]];
  155. }
  156. }
  157. },
  158. random_str : function (length) {
  159. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split('');
  160. if (!length) {
  161. length = Math.floor(Math.random() * chars.length);
  162. }
  163. var str = '';
  164. for (var i = 0; i < length; i++) {
  165. str += chars[Math.floor(Math.random() * chars.length)];
  166. }
  167. return str;
  168. },
  169. libs : {},
  170. // methods that can be inherited in libraries
  171. lib_methods : {
  172. set_data : function (node, data) {
  173. // this.name references the name of the library calling this method
  174. var id = [this.name,+new Date(),Foundation.random_str(5)].join('-');
  175. Foundation.cache[id] = data;
  176. node.attr('data-' + this.name + '-id', id);
  177. return data;
  178. },
  179. get_data : function (node) {
  180. return Foundation.cache[node.attr('data-' + this.name + '-id')];
  181. },
  182. remove_data : function (node) {
  183. if (node) {
  184. delete Foundation.cache[node.attr('data-' + this.name + '-id')];
  185. node.attr('data-' + this.name + '-id', '');
  186. } else {
  187. $('[data-' + this.name + '-id]').each(function () {
  188. delete Foundation.cache[$(this).attr('data-' + this.name + '-id')];
  189. $(this).attr('data-' + this.name + '-id', '');
  190. });
  191. }
  192. },
  193. throttle : function(fun, delay) {
  194. var timer = null;
  195. return function () {
  196. var context = this, args = arguments;
  197. clearTimeout(timer);
  198. timer = setTimeout(function () {
  199. fun.apply(context, args);
  200. }, delay);
  201. };
  202. },
  203. // parses data-options attribute on nodes and turns
  204. // them into an object
  205. data_options : function (el) {
  206. var opts = {}, ii, p,
  207. opts_arr = (el.attr('data-options') || ':').split(';'),
  208. opts_len = opts_arr.length;
  209. function isNumber (o) {
  210. return ! isNaN (o-0) && o !== null && o !== "" && o !== false && o !== true;
  211. }
  212. function trim(str) {
  213. if (typeof str === 'string') return $.trim(str);
  214. return str;
  215. }
  216. // parse options
  217. for (ii = opts_len - 1; ii >= 0; ii--) {
  218. p = opts_arr[ii].split(':');
  219. if (/true/i.test(p[1])) p[1] = true;
  220. if (/false/i.test(p[1])) p[1] = false;
  221. if (isNumber(p[1])) p[1] = parseInt(p[1], 10);
  222. if (p.length === 2 && p[0].length > 0) {
  223. opts[trim(p[0])] = trim(p[1]);
  224. }
  225. }
  226. return opts;
  227. },
  228. delay : function (fun, delay) {
  229. return setTimeout(fun, delay);
  230. },
  231. // animated scrolling
  232. scrollTo : function (el, to, duration) {
  233. if (duration < 0) return;
  234. var difference = to - $(window).scrollTop();
  235. var perTick = difference / duration * 10;
  236. this.scrollToTimerCache = setTimeout(function() {
  237. if (!isNaN(parseInt(perTick, 10))) {
  238. window.scrollTo(0, $(window).scrollTop() + perTick);
  239. this.scrollTo(el, to, duration - 10);
  240. }
  241. }.bind(this), 10);
  242. },
  243. // not supported in core Zepto
  244. scrollLeft : function (el) {
  245. if (!el.length) return;
  246. return ('scrollLeft' in el[0]) ? el[0].scrollLeft : el[0].pageXOffset;
  247. },
  248. // test for empty object or array
  249. empty : function (obj) {
  250. if (obj.length && obj.length > 0) return false;
  251. if (obj.length && obj.length === 0) return true;
  252. for (var key in obj) {
  253. if (hasOwnProperty.call(obj, key)) return false;
  254. }
  255. return true;
  256. }
  257. },
  258. fix_outer : function (lib) {
  259. lib.outerHeight = function (el, bool) {
  260. if (typeof Zepto === 'function') {
  261. return el.height();
  262. }
  263. if (typeof bool !== 'undefined') {
  264. return el.outerHeight(bool);
  265. }
  266. return el.outerHeight();
  267. };
  268. lib.outerWidth = function (el) {
  269. if (typeof Zepto === 'function') {
  270. return el.width();
  271. }
  272. if (typeof bool !== 'undefined') {
  273. return el.outerWidth(bool);
  274. }
  275. return el.outerWidth();
  276. };
  277. },
  278. error : function (error) {
  279. return error.name + ' ' + error.message + '; ' + error.more;
  280. },
  281. // remove all foundation events.
  282. off: function () {
  283. $(this.scope).off('.fndtn');
  284. $(window).off('.fndtn');
  285. return true;
  286. },
  287. zj : function () {
  288. try {
  289. return Zepto;
  290. } catch (e) {
  291. return jQuery;
  292. }
  293. }()
  294. },
  295. $.fn.foundation = function () {
  296. var args = Array.prototype.slice.call(arguments, 0);
  297. return this.each(function () {
  298. Foundation.init.apply(Foundation, [this].concat(args));
  299. return this;
  300. });
  301. };
  302. }(this, this.document));
  303. })(libFuncName);