Skip to content

Instantly share code, notes, and snippets.

@jsmaker
Created June 28, 2012 08:47
Show Gist options
  • Save jsmaker/3009975 to your computer and use it in GitHub Desktop.
Save jsmaker/3009975 to your computer and use it in GitHub Desktop.
Generic Collection generator (work in progress)
function Elm(node) {
if (typeof node === 'string') {
node = document.querySelector(node);
}
this.node = node; //|| document.createElement('div');
}
Elm.prototype.createDomElement = function (string) {
var node = document.createElement('body');
node.innerHTML = string;
if (node.children.length > 1) {
var frag = document.createDocumentFragment();
for (var i = 0, j = node.children.length; i < j; i += 1) {
frag.appendChild(node.children[i]);
}
return frag;
}
return node.firstChild;
};
Elm.prototype.css = function (params) {
var style;
for (var p in params) {
style = this.node.style;
if (typeof style[p] === 'string') {
style[p] = params[p];
}
}
return this;
};
Elm.prototype.attr = function (params) {
for (var p in params) {
this.node.setAttribute(p, params[p]);
}
return this;
};
Elm.prototype.rAttr = function (args) {
for (var i = arguments.length; i--; ) {
this.node.removeAttribute(arguments[i]);
}
return this;
};
Elm.prototype.html = function (html) {
if (typeof html === 'string') {
this.node.innerHTML = html;
return this;
}
return this.node.innerHTML;
};
Elm.prototype.append = function (child) {
if (typeof child === 'string') {
child = this.createDomElement(child);
console.log(child);
}
if (child && child.nodeType === 1) {
this.node.appendChild(child);
}
return this;
};
Elm.prototype.prepend = function (child) {
if (typeof child === 'string') {
child = this.createDomElement(child);
}
if (child && child.nodeType === 1) {
this.node.insertBefore(child, this.node.firstChild);
}
return this;
};
Elm.prototype.remove = function (node) {
if (node) {
this.node.removeChild(node);
} else {
this.node.parentNode.removeChild(this.node);
}
return this;
};
Elm.prototype.is = function (selector, node) {
node = node || this.node;
if (node === document) {
return false;
}
var nodes = node.parentNode.querySelectorAll(selector);
for (var i = nodes.length; i--; ) {
if (nodes[i] === node) {
return true;
}
}
return false;
};
Elm.prototype.parent = function (selector, node) {
node = node || this.node;
var parent = node.parentNode;
if (selector) {
while (parent && !this.is(selector, parent)) {
parent = parent.parentNode;
}
}
return new Elm(parent);
};
Elm.prototype.removeClass = function (className) {
var regex = new RegExp('(?:\\s|^)' + className + '(?:\\s|$)', 'g');
this.node.className = this.node.className.replace(regex, ' ');
return this;
};
Elm.prototype.addClass = function (className) {
this.removeClass(className);
this.node.className += ' ' + className;
return this;
};
Elm.prototype.hasClass = function (className) {
var regex = new RegExp('(?:\\s|^)' + className + '(?:\\s|$)');
return !!this.node.className.match(regex);
};
Elm.prototype.toggleClass = function (className) {
this.hasClass(className) ? this.removeClass(className) : this.addClass(className);
return this;
};
function Pool(proto) {
this.___api___ = Pool.createApiOn(this, proto);
this.___elements___ = [];
this.___returns___ = [];
}
Pool.createApiOn = function (obj, proto) {
var prop,
protoProp,
props = Object.getOwnPropertyNames(proto),
i,
type1 = 'exec',
type2 = 'execP',
len = props.length;
for (i = 0; i < len; i += 1) {
prop = props[i];
protoProp = proto[prop];
if (typeof protoProp === 'function') {
obj[prop] = Pool.createFunction(type1, prop);
obj[prop + '$'] = Pool.createIndependedFunction(prop);
} else {
//props.splice(i, 1);
obj[prop] = Pool.createFunction(type2, prop);
}
}
return props;
};
Pool.createFunction = function (fnName, prop) {
return function () {
Pool[fnName].call(this, prop, arguments);
return this;
};
};
Pool.createIndependedFunction = function (fnName) {
return function (args, replace, mod) {
Pool.independed.call(this, fnName, args, replace, mod);
return this;
};
};
Pool.exec = function (fnName, args) {
var i,
len = this.___elements___.length;
this.___returns___ = [];
for (i = 0; i < len; i++) {
var elm = this.___elements___[i];
this.___returns___[i] = elm[fnName].apply(elm, args);
}
return this;
};
Pool.execP = function (propNmae, value) {
var i,
len = this.___elements___.length;
if (value) {
this.___returns___ = [];
}
for (i = 0; i < len; i++) {
var elm = this.___elements___[i];
if (value) {
elm[propNmae] = value;
} else {
this.___returns___[i] = elm[propNmae];
}
}
return this;
};
Pool.independed = function (fnName, args, replace, mod) {
var i,
len = this.___elements___.length;
mod = mod || args.length;
this.___returns___ = [];
for (i = 0; i < len; i++) {
var elm = this.___elements___[i];
this.___returns___[i] = elm[fnName].apply(elm, args[i % mod]);
if (replace) {
this.___elements___[i] = this.___returns___[i];
}
}
return this;
};
Pool.prototype.add = function () {
this.___elements___.push.apply(this.___elements___, arguments);
return this;
};
Pool.prototype.elexec = function (fnName) {
this.___elements___[fnName].apply(this.___elements___, arguments);
return this;
};
Pool.prototype.returns = function (cb) {
if (typeof cb === 'function') {
var vs = this.___returns___.slice();
setTimeout(function () {
cb(vs);
delete vs;
}, 0);
}
if (cb) {
return this;
}
return this.___returns___;
};
var sCol = new Pool(String.prototype);
var aCol = new Pool(Array.prototype);
var oCol = new Pool(Object);
aCol.add([], [], [], [], []);
oCol.add({}, {}, {}, {}, {});
sCol.add('a', 'b', 'c', 'dsa');
console.log(sCol.replace$([['a', '123123aaa'], ['b', '4asdbbdsa2']], 1).returns(function (e) {
console.log('sdf', e);
}));
console.log(sCol.replace$([['b', '4asddsa2'], ['a', '123123aaa'], ], true).returns(function (e) {
console.log('sdf', e);
}));
console.log(sCol.replace$([['b', '4asddsa2']], true).returns(function (e) {
console.log('sdf', e);
}));
function createDomCollection(selector) {
var col = new Pool(Elm.prototype);
var elms = document.querySelectorAll(selector);
var i;
for (i = 0; i < elms.length; i++) {
col.add(new Elm(elms[i]));
}
return col;
}
var $ = createDomCollection;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment