Created
January 12, 2016 15:35
-
-
Save sdempsey/a2d4b0e362140ed9c933 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
var helpersController = (function() { | |
'use strict'; | |
var ret = {}; | |
//asynchronously load JavaScript | |
function loadJS(url) { | |
var s = document.createElement('script'); | |
s.type = 'text/javascript'; | |
s.src = url; | |
s.async = true; | |
document.head.appendChild(s); | |
} | |
//asynchronously loads css | |
function loadCss(url) { | |
var ss = document.styleSheets; | |
for (var i = 0, max = ss.length; i < max; i++) { | |
if (ss[i].href === url) { | |
return; | |
} | |
} | |
var l = document.createElement('link'); | |
l.type = 'text/css'; | |
l.rel = 'stylesheet'; | |
l.href = url; | |
document.head.appendChild(l); | |
} | |
var triggerOnce = (function() { | |
var timers = {}; | |
return function(callback, ms, uniqueId) { | |
if (!uniqueId) { | |
uniqueId = 'Don\'t call this twice without a uniqueId'; | |
} | |
if (timers[uniqueId]) { | |
clearTimeout (timers[uniqueId]); | |
} | |
timers[uniqueId] = setTimeout(callback, ms); | |
}; | |
})(); | |
Element.prototype.hasClass = function(selector) { | |
if (this.classList) { | |
return this.classList.contains(selector); | |
} else { | |
return new RegExp('(^| )' + selector + '( |$)', 'gi').test(this.className); | |
} | |
}; | |
Element.prototype.addClass = function(selector) { | |
if (this.classList) { | |
this.classList.add(selector); | |
} else { | |
this.className += ' ' + selector; | |
} | |
}; | |
Element.prototype.removeClass = function(selector) { | |
if (this.classList) { | |
this.classList.remove(selector); | |
} else { | |
this.className = this.className.replace(new RegExp('(^|\\b)' + selector.split(' ').join('|') + '(\\b|$)', 'gi'), ' '); | |
} | |
}; | |
Element.prototype.closest = function(selector) { | |
//var parent = foo.closest('.selector'); | |
var el = this, | |
matchesSelector = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector; | |
while (el) { | |
if (matchesSelector.bind(el)(selector)) { | |
return el; | |
} else { | |
el = el.parentElement; | |
} | |
} | |
return false; | |
}; | |
ret = { | |
loadJS: loadJS, | |
loadCss: loadCss, | |
triggerOnce: triggerOnce | |
}; | |
return ret; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment