Skip to content

Instantly share code, notes, and snippets.

@abriemme
Created November 12, 2014 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abriemme/c6b235416009297c4b77 to your computer and use it in GitHub Desktop.
Save abriemme/c6b235416009297c4b77 to your computer and use it in GitHub Desktop.
super-tiny jQuery-like API JavaScript library
/*
* for more info see : https://github.com/dciccale/ki.js/blob/master/ki.js
* ie8 compatible !!
* included functions : $.create, $.each $.isFunction, on, off, hasCLass, addClass, removeClass, each show, append, hide, hmtl, attr, removeAttr, css, remove, ...
* usefull for developing third-party jsavascript widgets
*/
!function (window, document, array, prototype, event_listener) {
f = document['add' + event_listener]
function init(selector, d, i) {
for(d = (selector && selector.nodeType ? [selector] : '' + selector === selector ? document.querySelectorAll(selector) : selector), i = d.length; i--; array.unshift.call(this, d[i]));
}
$ = function (selector) {
return /^f/.test(typeof selector) ? /in/.test(document.readyState) ? setTimeout('$('+selector+')', 9) : selector() : new init(selector)
}
$.each = function(arr, callback) {
var i = 0, l = arr.length;
for(; i < l; ++i)
callback(i, arr[i]);
return this;
};
$.isFunction = function(f) { return typeof f === "function"; };
$.extend = function(target, source) {
for(i in source) {
target[i] = source[i]
}
return target
};
$.create = function (node_type, attributes, elem) {
elem = $(document.createElement(node_type))
for (var key in attributes)
elem.attr(key, attributes[key])
return elem
};
function classReg(c) { return new RegExp("(^|\\s+)" + c + "(\\s+|$)");}
$[prototype] = init[prototype] = {
length: 0,
on: function (event_type, callback) {
return this.each(function (elem) {
f ? elem['add' + event_listener](event_type, callback, false) : elem.attachEvent('on' + event_type, callback)
})
},
off: function (event_type, callback) {
return this.each(function (elem) {
f ? elem['remove' + event_listener](event_type, callback) : elem.detachEvent('on' + event_type, callback)
})
},
hasClass: function (c) {
return classReg(c).test(this[0].className);
},
addClass: function (c) {
return this.each(function (b) {
if (!$(b).hasClass(c))
b.className += ' ' + c;
});
},
removeClass: function (c) {
return this.each(function (b) {
b.className = b.className.replace(classReg(c), ' ');
});
},
each: function (callback, context) {
for (var c = this, d = 0, e = c.length; d < e; ++d) {
callback.call(context || c[d], c[d], d, c)
}
return c
},
show: function() {
return this.each(function(b) {
b.style.display = '';
});
},
hide: function() {
return this.each(function(b) {
b.style.display = 'none';
});
},
splice: array.splice,
append: function(elem) {
return this.each(function(b) {
b.appendChild(elem[0]);
});
},
html: function(html_content) {
return html_content === array._ ? this[0].innerHTML : this.each(function(b) {
b.innerHTML = html_content;
});
},
remove: function() {
return this.each(function(b) {
b.parentNode.removeChild(b);
});
},
attr: function(attr, new_value) {
return new_value === array._ ? this[0].getAttribute(attr) : this.each(function(c) {
c.setAttribute(attr, new_value);
});
},
removeAttr: function(attr) {
return this.each(function(b) {
b.removeAttribute(attr);
});
},
css: function(a, b) {
return b === []._ ? this[0].style[a] : this.each(function(c) {
c.style[a] = b;
});
}
};
}(window, document, [], 'prototype', 'EventListener');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment