Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var DOM = (function(defSearch, map, forEach, string, bool, array, toArray, reduce, uPush, contains, XHR, extend, iterable, ele, node, undefined) {
- var args, global = this,
- doc = global.document, find = defSearch;
- DOM.searchEngine = function(func) {
- find = func instanceof Function?func:defSearch;
- };
- DOM.prototype.find = function(query) {
- var input = find(query),
- results = [];
- $iterate(this, node, function(node) {
- forEach(input, function(result) {
- if(contains(node, result))
- uPush(results, result);
- });
- }); return new DOM(results);
- }; DOM.prototype.remove = function() {
- $iterate(this, node, function(node) {
- if(node.parentNode)
- node.parentNode.removeChild(node);
- }); return this;
- }; DOM.prototype.append = function() { var that = this;
- iterate(arguments, node, function(child) { var first = child.nodeType !== 11;
- $iterate(that, node, function(parent) {
- parent.appendChild(first?(first = false) || child:
- child.cloneNode(true));
- });
- }); return this;
- }; DOM.prototype.clone = function(copyChildren) {
- var results = [];
- $iterate(this, node, function(node) {
- results.push(node.cloneNode(copyChildren));
- }); return new DOM(results);
- }; DOM.prototype.each = function(callback) {
- if(!(callback instanceof Function)) return this;
- $iterate(this, null, function(item) {
- callback(item);
- }); return this;
- };
- if(XHR) DOM.Ajax = function(opts) {
- var xhr = XHR(), auth = opts && opts.auth;
- if(opts.headers) for(var name in opts.headers)
- xhr.setRequestHeader(name, opts.headers[name]);
- xhr.open.apply(xhr, string(opts)?[ "GET", opts]:
- [opts.method || "GET",
- opts.address,
- opts.async || true,
- auth?opts.user:undefined,
- auth?opts.pass:undefined]);
- return DOM.call(this, xhr);
- }; DOM.Window = function(opts) {
- if(!arguments.length) return DOM.call(this, global.open());
- if(string(opts)) return DOM.call(this, global.open(opts));
- var attr = new Array(3);
- attr[2] = [];
- for(var item in opts) {
- switch(item) {
- case "address": attr[0] = opts[item]; break;
- case "name": attr[1] = opts[item]; break;
- default: attr[2].push(
- bool(opts[item])?item + "=" + (opts[item]?"yes":"no"):
- item + "=" + opts[item]
- );
- }
- } attr[2] = attr[2].join(",");
- return DOM.call(this, Function.prototype.apply.call(global.open, global, attr));
- }; DOM.Fragment = function() {
- var frag = doc.createDocumentFragment();
- iterate(arguments, node, function(element) {
- frag.appendChild(element);
- }); return DOM.call(this, frag);
- };
- (function() {
- DOM.Element = create(doc, doc.createElement );
- DOM.Text = create(doc, doc.createTextNode);
- function create(context, func) {
- return function(name) {
- name = arguments.length > 1?map(arguments, function(name) {
- return func.call(context, name);
- }): func.call(context, name);
- return DOM.call(this, name);
- };
- };
- })(DOM);
- if(DOM.Ajax) extend(DOM.Ajax, DOM);
- extend(DOM.Window, DOM);
- extend(DOM.Fragment, DOM);
- extend(DOM.Element, DOM);
- extend(DOM.Text, DOM);
- return(DOM);
- function $iterate($, filter, callback) {
- if(! $.wrappee ) return;
- if( $.iterable)
- iterate($.wrappee, filter, callback);
- else if(!filter || filter($.wrappee))
- callback($.wrappee);
- }; function iterate($, filter, callback) {
- forEach($, function(item) {
- if(!item) return;
- if( item instanceof DOM) $iterate(item, filter, callback);
- else if(iterable(item)) iterate(item, filter, callback);
- else if(!filter || filter( item)) callback(item);
- });
- }; function wrap(query, context) {
- if(query instanceof DOM) {
- this.wrappee = query.wrappee;
- this.iterable = query.iterable;
- } else { var length;
- this.wrappee = string(query)?find(query):
- Object(query);
- this.wrappee = array( this.wrappee)?
- (!(length = this.wrappee.length)?null:
- (length === 1?this.wrappee[0]:this.wrappee)):
- this.wrappee;
- this.iterable = iterable(this.wrappee);
- }
- }; function DOM() {
- if(!(this instanceof arguments.callee)) {
- args = arguments;
- return new arguments.callee;
- } wrap.apply(this, args || arguments);
- if(args) args = null;
- };
- })(Sizzle,
- Array.map,
- Array.forEach,
- String.isString,
- Boolean.isBoolean,
- Array.isArray,
- Array.from,
- Array.reduce,
- Array.uPush,
- (function() {
- var func = document.createElement("span").contains;
- return func?function(x, y) {
- return func.call(x, y);
- }:function(x, y) {
- return x.compareDocumentPosition(y) & 16;
- }
- })(),
- (function() {
- try { new XMLHttpRequest; return function() {
- return new XMLHttpRequest;
- }} catch (e) {try{
- new ActiveXObject("Msxml2.XMLHTTP"); return function() {
- return new ActiveXObject("Msxml2.XMLHTTP");
- }} catch (e) {try {
- new ActiveXObject("Microsoft.XMLHTTP"); return function() {
- return new ActiveXObject("Microsoft.XMLHTTP");
- }} catch (e) {}}}
- })(), function(func, obj) {
- func.prototype = Object.create(obj.prototype);
- func.prototype.constructor = func;
- }, function(obj) {
- var length;
- return !!( obj instanceof Object &&
- "length" in obj && (
- !(length = obj.length) ||
- length - 1 in obj))
- }, function(obj) {
- return obj && "HTMLElement" in this?
- obj instanceof HTMLElement:
- obj.nodeType === 1;
- }, function( obj) {
- return obj && "Node" in this?
- obj instanceof Node:
- "nodeType" in obj;
- });
Add Comment
Please, Sign In to add comment