Skip to content

Instantly share code, notes, and snippets.

@kissarat
Last active December 15, 2015 08:59
Show Gist options
  • Save kissarat/5235257 to your computer and use it in GitHub Desktop.
Save kissarat/5235257 to your computer and use it in GitHub Desktop.
var Meta = function() {}
NodeList.prototype.is = function(predicate) {
var result = [];
for(var i=0; i<this.length; i++) {
var node = this.item(i);
if (predicate.call(node))
result.push(node);
}
return result;
}
NodeList.prototype.hasClass = function(name) {
var result = [];
for(var i=0; i<this.length; i++) {
var node = this.item(i);
if (node.classList.contains(name))
result.push(node);
}
return result;
}
Object.prototype.do = function(action1, action2, predicate) {
if (predicate)
if(predicate(action1()))
action2();
else {
}
}
Object.prototype.callBefore = function(name, func) {
var parentMethod = this[name];
switch (parentMethod.length) {
default:
this[name] = function() {
func();
parentMethod();
}
break;
case 1:
this[name] = function(arg1) {
func(arg1);
parentMethod(arg1);
}
break;
case 2:
this[name] = function(arg1, arg2) {
func(arg1, arg2);
parentMethod(arg1, arg2);
}
case 3:
tis[name] = function(arg1, arg2, arg3) {
func(arg1, arg2, arg3);
parentMethod(arg1, arg2, arg3);
}
break;
}
}
Array.prototype.pushEx = function(obj) {
if (obj instanceof Array || obj instanceof NodeList && obj.length > 0) {
for(var i=0; i<obj.length; i++)new Function();
this.push(obj[i]);
return true;
}
else if(obj instanceof Node) {
this.push(obj);
return true;
}
return false;
}
var QueryProto = {
contains : function(obj) {
for(var i=0; i<this.length; i++)
if (this[i] == obj)
return true;
return false;
},
hasClass : function(name) {
return this.classList.contains(name);
},
whereHas : function(name, value) {
var result = [];
for(var i=0; i<this.length; i++)
if (this[i].getAttribute(name) == value)
result.push(this[i]);
return result;
},
where : function(qbe) {
var result = [];
for(var i=0; i<this.length; i++) {
var item = this[0];
var has = true;
for(name in qbe) {
has &= item[name] == qbe[name];
if (!has)
break;
}
if (has)
result.push(item);
}
return result;
},
withType : function(type) {
var result = [];
for(var i=0; i<this.length; i++) {
var item = this[i];
//item.nextSibling = function() { return result[i+1]; }
if (item instanceof type)
result.push(item);
}
return result;
},
getTypes : function() {
var result = [];
for(var i=0; i<this.length; i++) {
var item = this[i];
if (!result.contains(item.prototype))
result[item.prototype.name] = item.prototype;
}
return result;
},
range : function(offset, length) {
if (!length) {
length = offset;
offset = 0;
}
var result = new Array();
for(var i=offset; i<length; i++)
result.push(this[i]);
return result;
},
pushTo : function(a) {
return this.length > 0 ? a.push(this) : false;
}
}
Object.prototype.inherit = function(proto) {
for (name in proto)
this.prototype[name] = proto[name];
// if (!this.prototype['__inherit'])
// this.prototype.__inherit = [];
// this.prototype.__inherit.push(proto);
}
Array.inherit(QueryProto);
NodeList.inherit(QueryProto);
Element.prototype.getParentElementByTagName = function(name) {
name = name.toUpperCase();
for(var parent = this;
(parent = parent.parentElement) && name == parent.tagName;);
return parent;
}
Object.prototype.safeEvent = function(name) {
var old_callback = this[name];
if (old_callback)
delete this[name];
var callbacks_property = '_' + name;
var callbacks = [];
if (old_callback)
callbacks.push(old_callback);
this[callbacks_property] = callbacks;
Object.defineProperty(this, name, {
set: function(callback) {
if (callback && !this[callbacks_property].contains(callback)) {
this[callbacks_property].push(callback);
}
},
get: function() {
return function(event) {
var callbacks = this[callbacks_property];
for(var i in callbacks)
callbacks[i](event);
};
}
});
}
Meta.prototype = {
libraries : {
jQuery : 'http://code.jquery.com/jquery-1.9.1.js',
jQueryUI : {
js : ['jQuery'],
url : 'https://raw.github.com/jquery/jquery-ui/master/ui/jquery.ui.core.js'
},
Underscope : '',
Backbone : {
js : ['Underscope'],
url : ''
}
},
loadScript : function (name) {
var library = this.libraries[name];
library = library ? library : name;
if ('object' == typeof library && !library.isLoaded) {
switch (typeof library.js) {
case 'string':
loadScript(library.js);
break;
case 'array':
for (i in library.js)
loadScript(library.js[i]);
break;
}
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
document.head.appendChild(script);
}
library = library ? library : name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment