Created
April 13, 2015 16:01
-
-
Save DevJMD/890a4235bfe9085826e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* PlumpJS Utility | |
* Version: 0.1.0 | |
*/ | |
/* global define: false */ | |
;(function(root, factory) { | |
'use strict'; | |
if (typeof define === 'function' && define.amd) { | |
define([], function() { | |
return (root.Utility = factory()); | |
}); | |
} else if (typeof exports === 'object') { | |
module.exports = factory(); | |
} else { | |
/** | |
* Declare `$` as a shorthand for `Utility`. | |
*/ | |
root.Utility = root.$ = factory(); | |
} | |
}(this, function() { | |
'use strict'; | |
/** | |
* Utility Functions | |
* Returns an object of usable functions. | |
* | |
* 1. deepExtend : In order to merge Object properties, we need to extend | |
* one in to the other. This preserves the state of one object declaration | |
* unless its properties are overridden by an extended object property. | |
* 2. hasClass : Checks if an element has a specified class selector. | |
* 3. addClass : Adds a class to a specified element. | |
* 4. removeClass : Removes a class from a specified element. | |
* 5. addEvent : Allows the use of addEventListener by calling the element | |
* first then the event handler and finally the function to call. | |
* 6. removeEvent : Remove any event attached to a specified DOM node. | |
*/ | |
/** | |
* Regular expression to catch a specific class selector string. | |
* @param {String} expression | |
* @return {String} | |
*/ | |
function classRegex(expression) { | |
return new RegExp('(\\s|^)' + expression + '(\\s|$)'); | |
} | |
return { | |
/* [1] */ | |
deepExtend: function(defaults, options) { | |
var extended = {}; | |
var prop; | |
for (prop in defaults) { | |
if (Object.prototype.hasOwnProperty.call(defaults, prop)) { | |
extended[prop] = defaults[prop]; | |
} | |
} | |
for (prop in options) { | |
if (Object.prototype.hasOwnProperty.call(options, prop)) { | |
extended[prop] = options[prop]; | |
} | |
} | |
return extended; | |
}, | |
/* [2] */ | |
hasClass: function(el, selector) { | |
var className = ' ' + selector + ' '; | |
if (el.nodeType === 1 && (' ' + el.className + ' ') | |
.replace(/[\t\r\n\f]/g, ' ') | |
.indexOf(className) >= 0) { | |
return true; | |
} | |
return false; | |
}, | |
/* [3] */ | |
addClass: function(el, selector) { | |
if (!this.hasClass(el, selector)) { | |
el.className += ' ' + selector; | |
} | |
}, | |
/* [4] */ | |
removeClass: function(el, selector) { | |
if (this.hasClass(el, selector)) { | |
el.className = el.className.replace(classRegex(selector), ' '); | |
} | |
}, | |
/* [5] */ | |
addEvent: function(el, type, fn) { | |
return el.addEventListener(type, fn, false); | |
}, | |
/* [6] */ | |
removeEvent: function(el, type, fn) { | |
return el.removeEventListener(type, fn, false); | |
} | |
}; | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment