@materializecss/materialize 1.2.2 → 2.0.1-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Gruntfile.js +68 -313
- package/README.md +26 -14
- package/dist/css/materialize.css +1009 -1822
- package/dist/css/materialize.min.css +2 -8
- package/dist/js/materialize.js +8414 -12299
- package/dist/js/materialize.min.js +8968 -2
- package/dist/js/materialize.min.js.map +1 -0
- package/package.json +13 -9
- package/sass/components/_badges.scss +12 -2
- package/sass/components/_buttons.scss +16 -11
- package/sass/components/_cards.scss +14 -9
- package/sass/components/_carousel.scss +5 -2
- package/sass/components/_chips.scss +3 -3
- package/sass/components/_collapsible.scss +22 -8
- package/sass/components/_collection.scss +14 -6
- package/sass/components/_datepicker.scss +30 -11
- package/sass/components/_dropdown.scss +6 -4
- package/sass/components/_global.scss +132 -111
- package/sass/components/_grid.scss +119 -98
- package/sass/components/_modal.scss +3 -3
- package/sass/components/_navbar.scss +31 -17
- package/sass/components/_normalize.scss +26 -124
- package/sass/components/_sidenav.scss +21 -20
- package/sass/components/_slider.scss +27 -7
- package/sass/components/_table_of_contents.scss +12 -12
- package/sass/components/_tabs.scss +47 -16
- package/sass/components/_tapTarget.scss +6 -6
- package/sass/components/_theme_variables.scss +98 -0
- package/sass/components/_timepicker.scss +54 -46
- package/sass/components/_toast.scss +3 -3
- package/sass/components/_tooltip.scss +4 -5
- package/sass/components/_typography.scss +1 -1
- package/sass/components/_variables.scss +185 -120
- package/sass/components/forms/_checkboxes.scss +9 -9
- package/sass/components/forms/_file-input.scss +9 -7
- package/sass/components/forms/_input-fields.scss +173 -234
- package/sass/components/forms/_radio-buttons.scss +1 -1
- package/sass/components/forms/_range.scss +11 -11
- package/sass/components/forms/_select.scss +29 -19
- package/sass/components/forms/_switches.scss +22 -18
- package/sass/materialize.scss +1 -1
- package/src/autocomplete.ts +459 -0
- package/src/bounding.ts +6 -0
- package/{js/buttons.js → src/buttons.ts} +103 -162
- package/src/cards.ts +54 -0
- package/{js/carousel.js → src/carousel.ts} +137 -262
- package/src/characterCounter.ts +88 -0
- package/src/chips.ts +350 -0
- package/src/collapsible.ts +184 -0
- package/{js/component.js → src/component.ts} +6 -19
- package/{js/datepicker.js → src/datepicker.ts} +213 -299
- package/{js/dropdown.js → src/dropdown.ts} +140 -254
- package/src/edges.ts +6 -0
- package/src/forms.ts +120 -0
- package/src/global.ts +385 -0
- package/src/materialbox.ts +348 -0
- package/src/modal.ts +256 -0
- package/{js/parallax.js → src/parallax.ts} +47 -60
- package/{js/pushpin.js → src/pushpin.ts} +19 -47
- package/{js/range.js → src/range.ts} +58 -139
- package/{js/scrollspy.js → src/scrollspy.ts} +81 -153
- package/src/select.ts +448 -0
- package/{js/sidenav.js → src/sidenav.ts} +96 -202
- package/src/slider.ts +415 -0
- package/src/tabs.ts +293 -0
- package/src/tapTarget.ts +240 -0
- package/{js/timepicker.js → src/timepicker.ts} +268 -272
- package/{js/toasts.js → src/toasts.ts} +75 -134
- package/{js/tooltip.js → src/tooltip.ts} +59 -96
- package/src/waves.ts +70 -0
- package/extras/noUiSlider/nouislider.css +0 -404
- package/extras/noUiSlider/nouislider.js +0 -2147
- package/extras/noUiSlider/nouislider.min.js +0 -1
- package/js/anime.min.js +0 -34
- package/js/autocomplete.js +0 -479
- package/js/cards.js +0 -40
- package/js/cash.js +0 -960
- package/js/characterCounter.js +0 -136
- package/js/chips.js +0 -486
- package/js/collapsible.js +0 -275
- package/js/forms.js +0 -285
- package/js/global.js +0 -428
- package/js/materialbox.js +0 -453
- package/js/modal.js +0 -382
- package/js/select.js +0 -391
- package/js/slider.js +0 -497
- package/js/tabs.js +0 -402
- package/js/tapTarget.js +0 -315
- package/js/waves.js +0 -615
- package/sass/components/_waves.scss +0 -187
package/js/global.js
DELETED
|
@@ -1,428 +0,0 @@
|
|
|
1
|
-
// Required for Meteor package, the use of window prevents export by Meteor
|
|
2
|
-
(function(window) {
|
|
3
|
-
if (window.Package) {
|
|
4
|
-
M = {};
|
|
5
|
-
} else {
|
|
6
|
-
window.M = {};
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
// Check for jQuery
|
|
10
|
-
M.jQueryLoaded = !!window.jQuery;
|
|
11
|
-
})(window);
|
|
12
|
-
|
|
13
|
-
// AMD
|
|
14
|
-
if (typeof define === 'function' && define.amd) {
|
|
15
|
-
define('M', [], function() {
|
|
16
|
-
return M;
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
// Common JS
|
|
20
|
-
} else if (typeof exports !== 'undefined' && !exports.nodeType) {
|
|
21
|
-
if (typeof module !== 'undefined' && !module.nodeType && module.exports) {
|
|
22
|
-
exports = module.exports = M;
|
|
23
|
-
}
|
|
24
|
-
exports.default = M;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
M.version = '1.2.2';
|
|
28
|
-
|
|
29
|
-
M.keys = {
|
|
30
|
-
TAB: 9,
|
|
31
|
-
ENTER: 13,
|
|
32
|
-
ESC: 27,
|
|
33
|
-
ARROW_UP: 38,
|
|
34
|
-
ARROW_DOWN: 40
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* TabPress Keydown handler
|
|
39
|
-
*/
|
|
40
|
-
M.tabPressed = false;
|
|
41
|
-
M.keyDown = false;
|
|
42
|
-
let docHandleKeydown = function(e) {
|
|
43
|
-
M.keyDown = true;
|
|
44
|
-
if (e.which === M.keys.TAB || e.which === M.keys.ARROW_DOWN || e.which === M.keys.ARROW_UP) {
|
|
45
|
-
M.tabPressed = true;
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
let docHandleKeyup = function(e) {
|
|
49
|
-
M.keyDown = false;
|
|
50
|
-
if (e.which === M.keys.TAB || e.which === M.keys.ARROW_DOWN || e.which === M.keys.ARROW_UP) {
|
|
51
|
-
M.tabPressed = false;
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
let docHandleFocus = function(e) {
|
|
55
|
-
if (M.keyDown) {
|
|
56
|
-
document.body.classList.add('keyboard-focused');
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
let docHandleBlur = function(e) {
|
|
60
|
-
document.body.classList.remove('keyboard-focused');
|
|
61
|
-
};
|
|
62
|
-
document.addEventListener('keydown', docHandleKeydown, true);
|
|
63
|
-
document.addEventListener('keyup', docHandleKeyup, true);
|
|
64
|
-
document.addEventListener('focus', docHandleFocus, true);
|
|
65
|
-
document.addEventListener('blur', docHandleBlur, true);
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Initialize jQuery wrapper for plugin
|
|
69
|
-
* @param {Class} plugin javascript class
|
|
70
|
-
* @param {string} pluginName jQuery plugin name
|
|
71
|
-
* @param {string} classRef Class reference name
|
|
72
|
-
*/
|
|
73
|
-
M.initializeJqueryWrapper = function(plugin, pluginName, classRef) {
|
|
74
|
-
jQuery.fn[pluginName] = function(methodOrOptions) {
|
|
75
|
-
// Call plugin method if valid method name is passed in
|
|
76
|
-
if (plugin.prototype[methodOrOptions]) {
|
|
77
|
-
let params = Array.prototype.slice.call(arguments, 1);
|
|
78
|
-
|
|
79
|
-
// Getter methods
|
|
80
|
-
if (methodOrOptions.slice(0, 3) === 'get') {
|
|
81
|
-
let instance = this.first()[0][classRef];
|
|
82
|
-
return instance[methodOrOptions].apply(instance, params);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// Void methods
|
|
86
|
-
return this.each(function() {
|
|
87
|
-
let instance = this[classRef];
|
|
88
|
-
instance[methodOrOptions].apply(instance, params);
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
// Initialize plugin if options or no argument is passed in
|
|
92
|
-
} else if (typeof methodOrOptions === 'object' || !methodOrOptions) {
|
|
93
|
-
plugin.init(this, arguments[0]);
|
|
94
|
-
return this;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// Return error if an unrecognized method name is passed in
|
|
98
|
-
jQuery.error(`Method ${methodOrOptions} does not exist on jQuery.${pluginName}`);
|
|
99
|
-
};
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Automatically initialize components
|
|
104
|
-
* @param {Element} context DOM Element to search within for components
|
|
105
|
-
*/
|
|
106
|
-
M.AutoInit = function(context) {
|
|
107
|
-
// Use document.body if no context is given
|
|
108
|
-
let root = !!context ? context : document.body;
|
|
109
|
-
|
|
110
|
-
let registry = {
|
|
111
|
-
Autocomplete: root.querySelectorAll('.autocomplete:not(.no-autoinit)'),
|
|
112
|
-
Carousel: root.querySelectorAll('.carousel:not(.no-autoinit)'),
|
|
113
|
-
Chips: root.querySelectorAll('.chips:not(.no-autoinit)'),
|
|
114
|
-
Collapsible: root.querySelectorAll('.collapsible:not(.no-autoinit)'),
|
|
115
|
-
Datepicker: root.querySelectorAll('.datepicker:not(.no-autoinit)'),
|
|
116
|
-
Dropdown: root.querySelectorAll('.dropdown-trigger:not(.no-autoinit)'),
|
|
117
|
-
Materialbox: root.querySelectorAll('.materialboxed:not(.no-autoinit)'),
|
|
118
|
-
Modal: root.querySelectorAll('.modal:not(.no-autoinit)'),
|
|
119
|
-
Parallax: root.querySelectorAll('.parallax:not(.no-autoinit)'),
|
|
120
|
-
Pushpin: root.querySelectorAll('.pushpin:not(.no-autoinit)'),
|
|
121
|
-
ScrollSpy: root.querySelectorAll('.scrollspy:not(.no-autoinit)'),
|
|
122
|
-
FormSelect: root.querySelectorAll('select:not(.no-autoinit)'),
|
|
123
|
-
Sidenav: root.querySelectorAll('.sidenav:not(.no-autoinit)'),
|
|
124
|
-
Tabs: root.querySelectorAll('.tabs:not(.no-autoinit)'),
|
|
125
|
-
TapTarget: root.querySelectorAll('.tap-target:not(.no-autoinit)'),
|
|
126
|
-
Timepicker: root.querySelectorAll('.timepicker:not(.no-autoinit)'),
|
|
127
|
-
Tooltip: root.querySelectorAll('.tooltipped:not(.no-autoinit)'),
|
|
128
|
-
FloatingActionButton: root.querySelectorAll('.fixed-action-btn:not(.no-autoinit)')
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
for (let pluginName in registry) {
|
|
132
|
-
let plugin = M[pluginName];
|
|
133
|
-
plugin.init(registry[pluginName]);
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Generate approximated selector string for a jQuery object
|
|
139
|
-
* @param {jQuery} obj jQuery object to be parsed
|
|
140
|
-
* @returns {string}
|
|
141
|
-
*/
|
|
142
|
-
M.objectSelectorString = function(obj) {
|
|
143
|
-
let tagStr = obj.prop('tagName') || '';
|
|
144
|
-
let idStr = obj.attr('id') || '';
|
|
145
|
-
let classStr = obj.attr('class') || '';
|
|
146
|
-
return (tagStr + idStr + classStr).replace(/\s/g, '');
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
// Unique Random ID
|
|
150
|
-
M.guid = (function() {
|
|
151
|
-
function s4() {
|
|
152
|
-
return Math.floor((1 + Math.random()) * 0x10000)
|
|
153
|
-
.toString(16)
|
|
154
|
-
.substring(1);
|
|
155
|
-
}
|
|
156
|
-
return function() {
|
|
157
|
-
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
|
|
158
|
-
};
|
|
159
|
-
})();
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Escapes hash from special characters
|
|
163
|
-
* @param {string} hash String returned from this.hash
|
|
164
|
-
* @returns {string}
|
|
165
|
-
*/
|
|
166
|
-
M.escapeHash = function(hash) {
|
|
167
|
-
return hash.replace(/(:|\.|\[|\]|,|=|\/)/g, '\\$1');
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* @typedef {Object} Edges
|
|
172
|
-
* @property {Boolean} top If the top edge was exceeded
|
|
173
|
-
* @property {Boolean} right If the right edge was exceeded
|
|
174
|
-
* @property {Boolean} bottom If the bottom edge was exceeded
|
|
175
|
-
* @property {Boolean} left If the left edge was exceeded
|
|
176
|
-
*/
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* @typedef {Object} Bounding
|
|
180
|
-
* @property {Number} left left offset coordinate
|
|
181
|
-
* @property {Number} top top offset coordinate
|
|
182
|
-
* @property {Number} width
|
|
183
|
-
* @property {Number} height
|
|
184
|
-
*/
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* Escapes hash from special characters
|
|
188
|
-
* @param {Element} container Container element that acts as the boundary
|
|
189
|
-
* @param {Bounding} bounding element bounding that is being checked
|
|
190
|
-
* @param {Number} offset offset from edge that counts as exceeding
|
|
191
|
-
* @returns {Edges}
|
|
192
|
-
*/
|
|
193
|
-
M.checkWithinContainer = function(container, bounding, offset) {
|
|
194
|
-
let edges = {
|
|
195
|
-
top: false,
|
|
196
|
-
right: false,
|
|
197
|
-
bottom: false,
|
|
198
|
-
left: false
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
let containerRect = container.getBoundingClientRect();
|
|
202
|
-
// If body element is smaller than viewport, use viewport height instead.
|
|
203
|
-
let containerBottom =
|
|
204
|
-
container === document.body
|
|
205
|
-
? Math.max(containerRect.bottom, window.innerHeight)
|
|
206
|
-
: containerRect.bottom;
|
|
207
|
-
|
|
208
|
-
let scrollLeft = container.scrollLeft;
|
|
209
|
-
let scrollTop = container.scrollTop;
|
|
210
|
-
|
|
211
|
-
let scrolledX = bounding.left - scrollLeft;
|
|
212
|
-
let scrolledY = bounding.top - scrollTop;
|
|
213
|
-
|
|
214
|
-
// Check for container and viewport for each edge
|
|
215
|
-
if (scrolledX < containerRect.left + offset || scrolledX < offset) {
|
|
216
|
-
edges.left = true;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
if (
|
|
220
|
-
scrolledX + bounding.width > containerRect.right - offset ||
|
|
221
|
-
scrolledX + bounding.width > window.innerWidth - offset
|
|
222
|
-
) {
|
|
223
|
-
edges.right = true;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
if (scrolledY < containerRect.top + offset || scrolledY < offset) {
|
|
227
|
-
edges.top = true;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
if (
|
|
231
|
-
scrolledY + bounding.height > containerBottom - offset ||
|
|
232
|
-
scrolledY + bounding.height > window.innerHeight - offset
|
|
233
|
-
) {
|
|
234
|
-
edges.bottom = true;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
return edges;
|
|
238
|
-
};
|
|
239
|
-
|
|
240
|
-
M.checkPossibleAlignments = function(el, container, bounding, offset) {
|
|
241
|
-
let canAlign = {
|
|
242
|
-
top: true,
|
|
243
|
-
right: true,
|
|
244
|
-
bottom: true,
|
|
245
|
-
left: true,
|
|
246
|
-
spaceOnTop: null,
|
|
247
|
-
spaceOnRight: null,
|
|
248
|
-
spaceOnBottom: null,
|
|
249
|
-
spaceOnLeft: null
|
|
250
|
-
};
|
|
251
|
-
|
|
252
|
-
let containerAllowsOverflow = getComputedStyle(container).overflow === 'visible';
|
|
253
|
-
let containerRect = container.getBoundingClientRect();
|
|
254
|
-
let containerHeight = Math.min(containerRect.height, window.innerHeight);
|
|
255
|
-
let containerWidth = Math.min(containerRect.width, window.innerWidth);
|
|
256
|
-
let elOffsetRect = el.getBoundingClientRect();
|
|
257
|
-
|
|
258
|
-
let scrollLeft = container.scrollLeft;
|
|
259
|
-
let scrollTop = container.scrollTop;
|
|
260
|
-
|
|
261
|
-
let scrolledX = bounding.left - scrollLeft;
|
|
262
|
-
let scrolledYTopEdge = bounding.top - scrollTop;
|
|
263
|
-
let scrolledYBottomEdge = bounding.top + elOffsetRect.height - scrollTop;
|
|
264
|
-
|
|
265
|
-
// Check for container and viewport for left
|
|
266
|
-
canAlign.spaceOnRight = !containerAllowsOverflow
|
|
267
|
-
? containerWidth - (scrolledX + bounding.width)
|
|
268
|
-
: window.innerWidth - (elOffsetRect.left + bounding.width);
|
|
269
|
-
if (canAlign.spaceOnRight < 0) {
|
|
270
|
-
canAlign.left = false;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
// Check for container and viewport for Right
|
|
274
|
-
canAlign.spaceOnLeft = !containerAllowsOverflow
|
|
275
|
-
? scrolledX - bounding.width + elOffsetRect.width
|
|
276
|
-
: elOffsetRect.right - bounding.width;
|
|
277
|
-
if (canAlign.spaceOnLeft < 0) {
|
|
278
|
-
canAlign.right = false;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
// Check for container and viewport for Top
|
|
282
|
-
canAlign.spaceOnBottom = !containerAllowsOverflow
|
|
283
|
-
? containerHeight - (scrolledYTopEdge + bounding.height + offset)
|
|
284
|
-
: window.innerHeight - (elOffsetRect.top + bounding.height + offset);
|
|
285
|
-
if (canAlign.spaceOnBottom < 0) {
|
|
286
|
-
canAlign.top = false;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
// Check for container and viewport for Bottom
|
|
290
|
-
canAlign.spaceOnTop = !containerAllowsOverflow
|
|
291
|
-
? scrolledYBottomEdge - (bounding.height - offset)
|
|
292
|
-
: elOffsetRect.bottom - (bounding.height + offset);
|
|
293
|
-
if (canAlign.spaceOnTop < 0) {
|
|
294
|
-
canAlign.bottom = false;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
return canAlign;
|
|
298
|
-
};
|
|
299
|
-
|
|
300
|
-
M.getOverflowParent = function(element) {
|
|
301
|
-
if (element == null) {
|
|
302
|
-
return null;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
if (element === document.body || getComputedStyle(element).overflow !== 'visible') {
|
|
306
|
-
return element;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
return M.getOverflowParent(element.parentElement);
|
|
310
|
-
};
|
|
311
|
-
|
|
312
|
-
/**
|
|
313
|
-
* Gets id of component from a trigger
|
|
314
|
-
* @param {Element} trigger trigger
|
|
315
|
-
* @returns {string}
|
|
316
|
-
*/
|
|
317
|
-
M.getIdFromTrigger = function(trigger) {
|
|
318
|
-
let id = trigger.getAttribute('data-target');
|
|
319
|
-
if (!id) {
|
|
320
|
-
id = trigger.getAttribute('href');
|
|
321
|
-
if (id) {
|
|
322
|
-
id = id.slice(1);
|
|
323
|
-
} else {
|
|
324
|
-
id = '';
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
return id;
|
|
328
|
-
};
|
|
329
|
-
|
|
330
|
-
/**
|
|
331
|
-
* Multi browser support for document scroll top
|
|
332
|
-
* @returns {Number}
|
|
333
|
-
*/
|
|
334
|
-
M.getDocumentScrollTop = function() {
|
|
335
|
-
return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
|
|
336
|
-
};
|
|
337
|
-
|
|
338
|
-
/**
|
|
339
|
-
* Multi browser support for document scroll left
|
|
340
|
-
* @returns {Number}
|
|
341
|
-
*/
|
|
342
|
-
M.getDocumentScrollLeft = function() {
|
|
343
|
-
return window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
|
|
344
|
-
};
|
|
345
|
-
|
|
346
|
-
/**
|
|
347
|
-
* @typedef {Object} Edges
|
|
348
|
-
* @property {Boolean} top If the top edge was exceeded
|
|
349
|
-
* @property {Boolean} right If the right edge was exceeded
|
|
350
|
-
* @property {Boolean} bottom If the bottom edge was exceeded
|
|
351
|
-
* @property {Boolean} left If the left edge was exceeded
|
|
352
|
-
*/
|
|
353
|
-
|
|
354
|
-
/**
|
|
355
|
-
* @typedef {Object} Bounding
|
|
356
|
-
* @property {Number} left left offset coordinate
|
|
357
|
-
* @property {Number} top top offset coordinate
|
|
358
|
-
* @property {Number} width
|
|
359
|
-
* @property {Number} height
|
|
360
|
-
*/
|
|
361
|
-
|
|
362
|
-
/**
|
|
363
|
-
* Get time in ms
|
|
364
|
-
* @license https://raw.github.com/jashkenas/underscore/master/LICENSE
|
|
365
|
-
* @type {function}
|
|
366
|
-
* @return {number}
|
|
367
|
-
*/
|
|
368
|
-
let getTime =
|
|
369
|
-
Date.now ||
|
|
370
|
-
function() {
|
|
371
|
-
return new Date().getTime();
|
|
372
|
-
};
|
|
373
|
-
|
|
374
|
-
/**
|
|
375
|
-
* Returns a function, that, when invoked, will only be triggered at most once
|
|
376
|
-
* during a given window of time. Normally, the throttled function will run
|
|
377
|
-
* as much as it can, without ever going more than once per `wait` duration;
|
|
378
|
-
* but if you'd like to disable the execution on the leading edge, pass
|
|
379
|
-
* `{leading: false}`. To disable execution on the trailing edge, ditto.
|
|
380
|
-
* @license https://raw.github.com/jashkenas/underscore/master/LICENSE
|
|
381
|
-
* @param {function} func
|
|
382
|
-
* @param {number} wait
|
|
383
|
-
* @param {Object=} options
|
|
384
|
-
* @returns {Function}
|
|
385
|
-
*/
|
|
386
|
-
M.throttle = function(func, wait, options) {
|
|
387
|
-
let context, args, result;
|
|
388
|
-
let timeout = null;
|
|
389
|
-
let previous = 0;
|
|
390
|
-
options || (options = {});
|
|
391
|
-
let later = function() {
|
|
392
|
-
previous = options.leading === false ? 0 : getTime();
|
|
393
|
-
timeout = null;
|
|
394
|
-
result = func.apply(context, args);
|
|
395
|
-
context = args = null;
|
|
396
|
-
};
|
|
397
|
-
return function() {
|
|
398
|
-
let now = getTime();
|
|
399
|
-
if (!previous && options.leading === false) previous = now;
|
|
400
|
-
let remaining = wait - (now - previous);
|
|
401
|
-
context = this;
|
|
402
|
-
args = arguments;
|
|
403
|
-
if (remaining <= 0) {
|
|
404
|
-
clearTimeout(timeout);
|
|
405
|
-
timeout = null;
|
|
406
|
-
previous = now;
|
|
407
|
-
result = func.apply(context, args);
|
|
408
|
-
context = args = null;
|
|
409
|
-
} else if (!timeout && options.trailing !== false) {
|
|
410
|
-
timeout = setTimeout(later, remaining);
|
|
411
|
-
}
|
|
412
|
-
return result;
|
|
413
|
-
};
|
|
414
|
-
};
|
|
415
|
-
|
|
416
|
-
/* Feature detection */
|
|
417
|
-
var passiveIfSupported = false;
|
|
418
|
-
try {
|
|
419
|
-
window.addEventListener(
|
|
420
|
-
'test',
|
|
421
|
-
null,
|
|
422
|
-
Object.defineProperty({}, 'passive', {
|
|
423
|
-
get: function() {
|
|
424
|
-
passiveIfSupported = { passive: false };
|
|
425
|
-
}
|
|
426
|
-
})
|
|
427
|
-
);
|
|
428
|
-
} catch (err) {}
|