Last active
February 12, 2019 10:43
-
-
Save bainternet/7690772 to your computer and use it in GitHub Desktop.
core js
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
/** | |
* The MIT License (MIT) | |
* | |
* Copyright (c) 2013 Andreas Gruber <andreas@webbackstube.at> | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
* THE SOFTWARE. | |
* | |
* | |
* A tiny javascript library | |
* Download: https://gist.github.com/coretracker/5661264 | |
* | |
* @class corejs | |
* @constructor | |
*/ | |
var cjs = (function() { | |
var corejs = { | |
/** | |
* Checks if DOM content is loaded | |
* | |
* @method ready | |
* @param {Function} callback Will be executed after dom is loaded | |
* @return {Object} | |
*/ | |
ready: function (callback) { | |
// for all browsers except IE | |
if (window.addEventListener) { | |
window.document.addEventListener('DOMContentLoaded', function () { | |
callback(); | |
}, false); | |
} else { | |
// for IE | |
// code taken from http://ajaxian.com/archives/iecontentloaded-yet-another-domcontentloaded | |
(function () { | |
// check IE's proprietary DOM members | |
if (!window.document.uniqueID && window.document.expando) { | |
return; | |
} | |
// you can create any tagName, even customTag like <document :ready /> | |
var tempNode = window.document.createElement('document:ready'); | |
try { | |
// see if it throws errors until after ondocumentready | |
tempNode.doScroll('left'); | |
// call ready | |
callback(); | |
} catch (err) { | |
setTimeout(callback.callee, 0); | |
} | |
})(); | |
} | |
return this; | |
}, | |
/** | |
* Various information about the browser | |
* | |
* @method browser | |
* @param {String} [all,version,name] | |
* @return {Mixed} | |
*/ | |
browser: function (args) { | |
if (args === "all") { | |
return navigator; | |
} | |
if (args === "version") { | |
return navigator.appVersion; | |
} | |
if (args === "name") { | |
return navigator.appName; | |
} | |
return navigator.appName; | |
}, | |
/** | |
* Select a element by id or class | |
* | |
* @method find | |
* @param {String} id or class to select | |
* @return {Mixed} | |
*/ | |
find: function (selector) { | |
if (selector.indexOf('#') > -1) { | |
selector = selector.replace('#', ""); | |
window.corejsObject = this.findById(selector); | |
return this; | |
} | |
if (selector.indexOf('.') > -1) { | |
selector = selector.replace('.', ""); | |
window.corejsObject = this.findByClass(selector); | |
return this; | |
} | |
return "i dont know anything about this selector"; | |
}, | |
/** | |
* Select a element by id | |
* | |
* @method findById | |
* @param {String} id to select | |
* @return {Mixed} | |
*/ | |
findById: function (selector) { | |
return document.getElementById(selector); | |
}, | |
/** | |
* Select a element by class | |
* | |
* @method findByClass | |
* @param {String} class to select | |
* @return {Mixed} | |
*/ | |
findByClass: function (selector) { | |
if (typeof document.getElementsByClassName !== 'function') { | |
document.getElementsByClassName = function () { | |
var elms = document.getElementsByTagName('*'); | |
var ei = []; | |
var i; | |
var ecl; | |
var j; | |
for (i = 0; i < elms.length; i++) { | |
if (elms[i].getAttribute('class')) { | |
ecl = elms[i].getAttribute('class').split(' '); | |
for (j = 0; j < ecl.length; j++) { | |
if (ecl[j].toLowerCase() === arguments[0].toLowerCase()) { | |
ei.push(elms[i]); | |
} | |
} | |
} else if (elms[i].className) { | |
ecl = elms[i].className.split(' '); | |
for (j = 0; j < ecl.length; j++) { | |
if (ecl[j].toLowerCase() === arguments[0].toLowerCase()) { | |
ei.push(elms[i]); | |
} | |
} | |
} | |
} | |
return ei; | |
}; | |
}; | |
var elements; | |
elements = document.getElementsByClassName(selector); | |
return elements; | |
}, | |
/** | |
* Add events to queue | |
* | |
* @method addEvent | |
* @param {Object} obj selected element | |
* @param {String} type [click,hover....] | |
* @param {Function} function or callback to execute | |
*/ | |
addEvent: function (obj, type, fn) { | |
if (obj.addEventListener) { | |
obj.addEventListener(type, fn, false); | |
EventCache.add(obj, type, fn); | |
} else if (obj.attachEvent) { | |
obj["e" + type + fn] = fn; | |
obj[type + fn] = function () { | |
obj["e" + type + fn](window.event); | |
}; | |
obj.attachEvent("on" + type, obj[type + fn]); | |
EventCache.add(obj, type, fn); | |
} else { | |
obj["on" + type] = obj["e" + type + fn]; | |
} | |
}, | |
/** | |
* Add click event | |
* | |
* @method click | |
* @param {Object} element | |
* @param {Function} callback | |
*/ | |
click: function (callback) { | |
var element; | |
if(typeof window.corejsObject === 'strint') | |
{ | |
element = window.corejsObject; | |
}else{ | |
element = window.corejsObject[0]; | |
} | |
this.addEvent(element, 'click', callback); | |
return this; | |
}, | |
/** | |
* Append tag after element | |
* | |
* @method append | |
* @param {Object} selected element | |
* @param {Function} function or callback to execute | |
*/ | |
append: function (tag, innerHtml, attributes) { | |
var i; | |
var newTag; | |
newTag = document.createElement(tag); | |
newTag.innerHTML = innerHtml; | |
if(typeof attributes === 'Array') | |
{ | |
if (attributes.length > 0) { | |
for (i = 0; i < attributes.length; i++) { | |
newTag.setAttribute(attributes[i][0], attributes[i][1]); | |
} | |
} | |
} | |
window.corejsObject.appendChild(newTag); | |
return this; | |
}, | |
/** | |
* Clear elements inner html | |
* | |
* @method clear | |
* @param {Object} | |
*/ | |
clear: function () { | |
window.corejsObject.innerHTML = ""; | |
return this; | |
}, | |
/** | |
* changes elements inner html | |
* | |
* @method html | |
* @param {String} html inner html | |
*/ | |
html: function (html) { | |
window.corejsObject.innerHTML = html; | |
return this; | |
}, | |
/** | |
* add css attribute | |
* | |
* @method css | |
* @param {String} attribute | |
* @param {String} value | |
*/ | |
css: function(attribute, value) | |
{ | |
window.corejsObject.style[attribute] = value; | |
return this; | |
}, | |
/** | |
* hide element | |
* | |
* @method hide | |
*/ | |
hide: function() | |
{ | |
window.corejsObject.style.display = "none"; | |
return this; | |
}, | |
/** | |
* show element | |
* | |
* @method show | |
*/ | |
show: function() | |
{ | |
window.corejsObject.style.display = "block"; | |
return this; | |
}, | |
}; | |
// thx to http://dustindiaz.com/rock-solid-addevent | |
var EventCache = function () { | |
var listEvents = []; | |
return { | |
listEvents: listEvents, | |
add: function (node, sEventName, fHandler) { | |
listEvents.push(arguments); | |
}, | |
flush: function () { | |
var i, item; | |
for (i = listEvents.length - 1; i >= 0; i = i - 1) { | |
item = listEvents[i]; | |
if (item[0].removeEventListener) { | |
item[0].removeEventListener(item[1], item[2], item[3]); | |
}; | |
if (item[1].substring(0, 2) !== "on") { | |
item[1] = "on" + item[1]; | |
}; | |
if (item[0].detachEvent) { | |
item[0].detachEvent(item[1], item[2]); | |
}; | |
item[0][item[1]] = null; | |
}; | |
} | |
}; | |
}(); | |
return { | |
ready: function(arg1) { return corejs.ready(arg1) }, | |
browser: function(arg1) {return corejs.browser(arg1) }, | |
find: function(arg1) { return corejs.find(arg1) }, | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment