stitches
Version:
Stitches is an HTML5 sprite sheet generator.
92 lines (78 loc) • 2.36 kB
JavaScript
/**
* # util/util
*
* This is the home for wayward methods who have lost their way.
*
* > http://draeton.github.com/stitches<br/>
* > Copyright 2013 Matthew Cobbs<br/>
* > Licensed under the MIT license.
*/
define([
"wrap/jquery"
],
function ($) {
"use strict";
// **Module definition**
return {
/**
* ### @inherit
* Set up prototypical inheritance
*
* @param {function} Child Constructor
* @param {function} Parent Constructor
* @param {object} methods To add to Child.prototype
*/
inherit: function (Child, Parent, methods) {
Child.prototype = new Parent();
Child.prototype.constructor = Parent;
$.each(methods, function (name, method) {
Child.prototype[name] = method;
});
Child.prototype._super = function (name, context, args) {
var method = Parent.prototype[name];
return method.apply(context, args);
};
},
/**
* ### @debounce
* Prevent a function from being called more than once within
* a certain threshold
*
* @param {function} func Function to modify
* @param {number} threshold In ms
* @param {boolean} execAsap If true, run function on first call
* @return function
*/
debounce: function (func, threshold, execAsap) {
var timeout;
return function () {
var context = this;
var args = arguments;
var delayed = function () {
if (!execAsap) {
func.apply(context, args);
}
timeout = null;
};
if (timeout) {
window.clearTimeout(timeout);
} else if (execAsap) {
func.apply(context, args);
}
timeout = setTimeout(delayed, threshold || 50);
};
},
/**
* ### @noop
* No operation
*
* @param {event} e Optional
*/
noop: function (e) {
if (e) {
e.preventDefault();
e.stopPropagation();
}
}
};
});