Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
|
|
|
/**
|
2015-06-08 14:04:39 +00:00
|
|
|
* @file
|
2013-05-25 19:28:28 +00:00
|
|
|
* Adapted from underscore.js with the addition Drupal namespace.
|
2015-06-08 14:04:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Limits the invocations of a function in a given time frame.
|
2013-05-25 19:28:28 +00:00
|
|
|
*
|
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
|
|
|
* The debounce function wrapper should be used sparingly. One clear use case
|
|
|
|
* is limiting the invocation of a callback attached to the window resize event.
|
|
|
|
*
|
|
|
|
* Before using the debounce function wrapper, consider first whether the
|
2014-01-08 08:32:03 +00:00
|
|
|
* callback could be attached to an event that fires less frequently or if the
|
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
|
|
|
* function can be written in such a way that it is only invoked under specific
|
|
|
|
* conditions.
|
|
|
|
*
|
2015-06-08 14:04:39 +00:00
|
|
|
* @param {function} func
|
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
|
|
|
* The function to be invoked.
|
2015-06-08 14:04:39 +00:00
|
|
|
* @param {number} wait
|
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
|
|
|
* The time period within which the callback function should only be
|
|
|
|
* invoked once. For example if the wait period is 250ms, then the callback
|
|
|
|
* will only be called at most 4 times per second.
|
2015-06-08 14:04:39 +00:00
|
|
|
* @param {bool} immediate
|
|
|
|
* Whether we wait at the beginning or end to execute the function.
|
|
|
|
*
|
|
|
|
* @return {function}
|
|
|
|
* The debounced function.
|
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
|
|
|
*/
|
2013-05-25 19:28:28 +00:00
|
|
|
Drupal.debounce = function (func, wait, immediate) {
|
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
|
|
|
|
2015-10-13 22:37:56 +00:00
|
|
|
'use strict';
|
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
|
|
|
|
2015-04-19 15:30:43 +00:00
|
|
|
var timeout;
|
|
|
|
var result;
|
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
|
|
|
return function () {
|
|
|
|
var context = this;
|
|
|
|
var args = arguments;
|
|
|
|
var later = function () {
|
|
|
|
timeout = null;
|
2013-05-25 19:28:28 +00:00
|
|
|
if (!immediate) {
|
|
|
|
result = func.apply(context, args);
|
|
|
|
}
|
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
|
|
|
};
|
2013-05-25 19:28:28 +00:00
|
|
|
var callNow = immediate && !timeout;
|
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = setTimeout(later, wait);
|
|
|
|
if (callNow) {
|
|
|
|
result = func.apply(context, args);
|
|
|
|
}
|
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
|
|
|
return result;
|
|
|
|
};
|
|
|
|
};
|