UNPKG

4.22 kBJavaScriptView Raw
1// Required for Meteor package, the use of window prevents export by Meteor
2(function(window){
3 if(window.Package){
4 Materialize = {};
5 } else {
6 window.Materialize = {};
7 }
8})(window);
9
10
11/*
12 * raf.js
13 * https://github.com/ngryman/raf.js
14 *
15 * original requestAnimationFrame polyfill by Erik Möller
16 * inspired from paul_irish gist and post
17 *
18 * Copyright (c) 2013 ngryman
19 * Licensed under the MIT license.
20 */
21(function(window) {
22 var lastTime = 0,
23 vendors = ['webkit', 'moz'],
24 requestAnimationFrame = window.requestAnimationFrame,
25 cancelAnimationFrame = window.cancelAnimationFrame,
26 i = vendors.length;
27
28 // try to un-prefix existing raf
29 while (--i >= 0 && !requestAnimationFrame) {
30 requestAnimationFrame = window[vendors[i] + 'RequestAnimationFrame'];
31 cancelAnimationFrame = window[vendors[i] + 'CancelRequestAnimationFrame'];
32 }
33
34 // polyfill with setTimeout fallback
35 // heavily inspired from @darius gist mod: https://gist.github.com/paulirish/1579671#comment-837945
36 if (!requestAnimationFrame || !cancelAnimationFrame) {
37 requestAnimationFrame = function(callback) {
38 var now = +Date.now(),
39 nextTime = Math.max(lastTime + 16, now);
40 return setTimeout(function() {
41 callback(lastTime = nextTime);
42 }, nextTime - now);
43 };
44
45 cancelAnimationFrame = clearTimeout;
46 }
47
48 // export to window
49 window.requestAnimationFrame = requestAnimationFrame;
50 window.cancelAnimationFrame = cancelAnimationFrame;
51}(window));
52
53
54// Unique ID
55Materialize.guid = (function() {
56 function s4() {
57 return Math.floor((1 + Math.random()) * 0x10000)
58 .toString(16)
59 .substring(1);
60 }
61 return function() {
62 return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
63 s4() + '-' + s4() + s4() + s4();
64 };
65})();
66
67/**
68 * Escapes hash from special characters
69 * @param {string} hash String returned from this.hash
70 * @returns {string}
71 */
72Materialize.escapeHash = function(hash) {
73 return hash.replace( /(:|\.|\[|\]|,|=)/g, "\\$1" );
74};
75
76Materialize.elementOrParentIsFixed = function(element) {
77 var $element = $(element);
78 var $checkElements = $element.add($element.parents());
79 var isFixed = false;
80 $checkElements.each(function(){
81 if ($(this).css("position") === "fixed") {
82 isFixed = true;
83 return false;
84 }
85 });
86 return isFixed;
87};
88
89
90/**
91 * Get time in ms
92 * @license https://raw.github.com/jashkenas/underscore/master/LICENSE
93 * @type {function}
94 * @return {number}
95 */
96var getTime = (Date.now || function () {
97 return new Date().getTime();
98});
99
100
101/**
102 * Returns a function, that, when invoked, will only be triggered at most once
103 * during a given window of time. Normally, the throttled function will run
104 * as much as it can, without ever going more than once per `wait` duration;
105 * but if you'd like to disable the execution on the leading edge, pass
106 * `{leading: false}`. To disable execution on the trailing edge, ditto.
107 * @license https://raw.github.com/jashkenas/underscore/master/LICENSE
108 * @param {function} func
109 * @param {number} wait
110 * @param {Object=} options
111 * @returns {Function}
112 */
113Materialize.throttle = function(func, wait, options) {
114 var context, args, result;
115 var timeout = null;
116 var previous = 0;
117 options || (options = {});
118 var later = function () {
119 previous = options.leading === false ? 0 : getTime();
120 timeout = null;
121 result = func.apply(context, args);
122 context = args = null;
123 };
124 return function () {
125 var now = getTime();
126 if (!previous && options.leading === false) previous = now;
127 var remaining = wait - (now - previous);
128 context = this;
129 args = arguments;
130 if (remaining <= 0) {
131 clearTimeout(timeout);
132 timeout = null;
133 previous = now;
134 result = func.apply(context, args);
135 context = args = null;
136 } else if (!timeout && options.trailing !== false) {
137 timeout = setTimeout(later, remaining);
138 }
139 return result;
140 };
141};
142
143
144// Velocity has conflicts when loaded with jQuery, this will check for it
145// First, check if in noConflict mode
146var Vel;
147if (jQuery) {
148 Vel = jQuery.Velocity;
149} else if ($) {
150 Vel = $.Velocity;
151} else {
152 Vel = Velocity;
153}