UNPKG

jdoms

Version:

jDoms is an interactive Javascript library for DOM manipulations and most regulars functions related with DOM.

1,942 lines (1,562 loc) 95.5 kB
/*! * jDoms JavaScript Library * https://mamedul.gitlab.io/dev-projects/jdoms * * Copyright (c) 2021-2022 by MAMEDUL ISLAM * * Licensed under the MIT license: * https://opensource.org/licenses/MIT * * Project home: * https://mamedul.gitlab.io/dev-projects/jdoms * * Version: 1.1.0 */ (function (global, factory) { 'use strict'; if (typeof module === "object" && typeof module.exports === "object") { module.exports = global.document ? factory(global, true) : function (win) { if (!win.document) { throw new Error("jDoms requires a document which has a window"); } return factory(win); }; } else if (typeof define === 'function' && define.amd) { define(factory); factory(global); } else { factory(global); } if( typeof global !== "undefined" ){ global = window||globalThis||self||this; } global.jDoms = factory(); }((typeof window !== "undefined" ? window||globalThis : this||self), (function () { 'use strict'; var version = "1.1.0", developer = "MAMEDUL ISLAM"; /* Important polyfills */ if(typeof String.prototype.trim !== 'function') { String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }; } if (!Array.prototype.filter) { Array.prototype.filter = function(fun) { var len = this.length >>> 0; if (typeof fun != "function") { throw new TypeError(); } var res = []; var thisP = arguments[1]; for (var i = 0; i < len; i++) { if (i in this) { var val = this[i]; if (fun.call(thisP, val, i, this)) { res.push(val); } } } return res; }; } if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(obj, start) { for (var i = (start || 0), j = this.length; i < j; i++) { if (this[i] === obj) { return i; } } return -1; }; } /* Inside global functions */ var _isUndefined = function(variable){ return (typeof variable==="undefined"); }; var _isDefined = function(variable){ return (typeof variable!=="undefined"); }; var _isSet = function(variable){ return (typeof variable!=="undefined"); }; var _isString = function(variable){ return (typeof variable==="string"); }; var _isNumber = function(variable){ return (typeof variable==="number"); }; var _isInt = function(variable){ return (typeof variable==="number" && parseInt(variable)===variable ); }; var _isFinite = function(variable){ return isFinite(variable); }; var _isInfinity = function(variable){ return (typeof variable==="number" && !isFinite(variable)); }; var _isBoolean = function(variable){ return (typeof variable==="boolean"); }; var _isBigInt = function(variable){ return (typeof variable==="bigint"); }; var _isFlatten = function(variable){ return (typeof variable==="string" || typeof variable==="number" || typeof variable==="boolean"); }; var _isEmpty = function(variable){ if( typeof variable==='undefined'||variable===""||variable===0||variable===[]||variable==={}||variable===false||variable===null||variable==NaN){ return true; } return false; }; var _isArray = function(variable){ if(typeof Array!="undefined" && typeof Array.isArray!="undefined" ){ return Array.isArray(variable); } if(variable.constructor && variable.constructor.name && variable.constructor.name=="Array"){ return true; } if(typeof Object!="undefined" && Object.prototype && Object.prototype.toString && Object.prototype.toString.call && Object.prototype.toString.call(variable)=="[object Array]" );{ return true; } return false; }; var _isFunction = function(variable){ return (typeof variable==="function"); }; var _isObject = function(variable){ return (typeof variable==="object"); }; var _isPureObject = function(variable){ return (typeof variable==="object" && typeof Object!="undefined" && variable===Object(variable) ); }; var _isSymbol = function(variable){ return (typeof variable==="undefined"); }; var _isjDoms = function(variable){ return ( (typeof variable==="function" && variable.name=="jDoms") || (typeof variable==="object" && typeof variable.constructor!="undefined" && typeof variable.constructor.name!="undefined" && variable.constructor.name=='jDoms') ); }; var _sizeOf = function(variable){ var _size = 0; if(typeof variable=="boolean"||typeof variable=="function"||typeof variable=="symbol"){ _size = 1; }else if(typeof variable=="bigint"){ if(typeof BigInt == "function" && typeof BigInt.valueOf == "function"){ _size = (""+BigInt(variable).valueOf()).length; }else{ _size = (""+variable).length + 1; } }else if(typeof variable=="string"||typeof variable=="number"){ _size = (""+variable).length; }else if(typeof variable=="object"){ if(typeof Object=="function" && typeof Object.keys=="function"){ _size = Object.keys(variable).length; } if(_size==0){ var key; for (key in variable) { if (_isSet(variable.hasOwnProperty) && variable.hasOwnProperty(key)){ _size++; }else{ continue; } } } if(_size==0 && typeof variable.nodeName!="undefined" && typeof variable.nodeType!="undefined" && typeof variable.nodeValue!="undefined" ){ _size++; } } return _size; }; var _now = function(){ return (new Date()).getTime(); }; var _trim = function(str){ try{ if(_isString(str)){ if(str.trim){ return str.trim(); }else{ return str.replace(/^\s+|\s+$/gm,''); } }else if( _isArray(str) || _isObject(str) ){ for(var key in str ){ str[key] = _trim(str[key]); } return str; } }catch(err){} return str; }; var _unique = function (arr) { if(_isArray(arr)){ arr = arr.filter(function (v, i, a) { return a.indexOf(v) === i; }); }else if(_isString(arr) ||_isNumber(arr)){ var new_str = ""; arr = "" + arr; for(var i=0; i<arr.length; i++){ if(new_str.indexOf(arr[i])<0){ new_str+=arr[i]; } } return new_str; } return arr; }; var _isUnique = function (arr) { return _sizeOf( arr ) == _sizeOf( _unique(arr) ); }; var _merge = function (obj, nextObj) { if(_isObject(obj) && _isObject(nextObj)){ for (var p in nextObj) { try { if ( nextObj[p].constructor==Object ) { obj[p] = MergeRecursive(obj[p], nextObj[p]); } else { obj[p] = nextObj[p]; } } catch(e) { obj[p] = nextObj[p]; } } } return obj; }; var _escape = function(str){ try{ return encodeURI(str); }catch(err){} return ""; }; var _unescape = function(str){ try{ return decodeURI(str); }catch(err){} return str; }; var _encodeBase64 = function( rawStr ){ if(_isSet(btoa)){ return btoa(rawStr); } var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; var _utf8_encode = function (string) { string = string.replace(/\r\n/g,"\n"); var utfText = ""; for (var n = 0; n < string.length; n++) { var c = string.charCodeAt(n); if (c < 128) { utfText += String.fromCharCode(c); }else if((c > 127) && (c < 2048)) { utfText += String.fromCharCode((c >> 6) | 192); utfText += String.fromCharCode((c & 63) | 128); }else { utfText += String.fromCharCode((c >> 12) | 224); utfText += String.fromCharCode(((c >> 6) & 63) | 128); utfText += String.fromCharCode((c & 63) | 128); } } return utfText; }; var _encode = function (str) { var output = ""; var chr1, chr2, chr3, enc1, enc2, enc3, enc4; var i = 0; str = _utf8_encode(str); while (i < str.length) { chr1 = str.charCodeAt(i++); chr2 = str.charCodeAt(i++); chr3 = str.charCodeAt(i++); enc1 = chr1 >> 2; enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); enc4 = chr3 & 63; if (isNaN(chr2)) { enc3 = enc4 = 64; } else if (isNaN(chr3)) { enc4 = 64; } output = output + _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4); } return output; }; return _encode (rawStr); }; var _decodeBase64 = function( encodedStr ){ if(_isSet(atob)){ return atob(encodedStr); } var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; var _utf8_decode = function (utfText) { var string = ""; var i = 0; var c = 0, c1 = 0, c2 = 0; while ( i < utfText.length ) { c = utfText.charCodeAt(i); if (c < 128) { string += String.fromCharCode(c); i++; }else if((c > 191) && (c < 224)) { c2 = utfText.charCodeAt(i+1); string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); i += 2; }else { c2 = utfText.charCodeAt(i+1); c3 = utfText.charCodeAt(i+2); string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); i += 3; } } return string; }; var _decode = function (str) { var output = ""; var chr1, chr2, chr3; var enc1, enc2, enc3, enc4; var i = 0; str = str.replace(/[^A-Za-z0-9\+\/\=]/g, ""); while (i < str.length) { enc1 = _keyStr.indexOf(str.charAt(i++)); enc2 = _keyStr.indexOf(str.charAt(i++)); enc3 = _keyStr.indexOf(str.charAt(i++)); enc4 = _keyStr.indexOf(str.charAt(i++)); chr1 = (enc1 << 2) | (enc2 >> 4); chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); chr3 = ((enc3 & 3) << 6) | enc4; output = output + String.fromCharCode(chr1); if (enc3 != 64) { output = output + String.fromCharCode(chr2);} if (enc4 != 64) { output = output + String.fromCharCode(chr3);} } output = _utf8_decode(output); return output; }; return _decode (encodedStr); }; var _stringify = function() { function e(e) { return c.lastIndex = 0, c.test(e) ? '"' + e.replace(c, function(e) { var n = f[e]; return _isString(n) ? n : "\\u" + ("0000" + e.charCodeAt(0).toString(16)).slice(-4); }) + '"' : '"' + e + '"'; } function n(e) { var n = []; for (var o in e){ n = n.concat(o); } return n; } function o(n) { var g = ['!doctype', 'br', 'hr', 'img', 'link', 'meta', 'input']; var o = n.tagName.toLowerCase(), t = "<" + o; return n.attributes.length > 0 && (t += " "), t += Array.prototype.map.call(n.attributes, function(n) { return "" === n.value ? n.name : n.name + "=" + e(n.value); }).join(" "), t += g.indexOf(o)>-1? ">":">"+n.innerHTML+"</"+o+">"; } function t(r, c) { var f, u, d, p, g, m, h = a; try { if (g = c[r], g && "object" == typeof g && _isFunction(g.toJSON) && (g = g.toJSON(r)), g instanceof HTMLElement){ return o(g); } if (g instanceof RegExp) { return String(g); } if (g instanceof MimeType || g instanceof Plugin) { return Object.prototype.toString.call(g); } switch (typeof g) { case "string": return e(g); case "boolean": case "null": case "number": case "undefined": return String(g); case "function": case "object": if (!g) return "null"; var y = s.indexOf(g) + 1; if (y > 0) return "/**ref:" + y.toString(16) + "**/"; if (s.push(g), y = s.length, m = "/**id:" + y.toString(16) + "**/", _isFunction(g)) return m + " " + String(g); if (a += l, p = [], "[object Array]" === Object.prototype.toString.apply(g)) { for (d = g.length, f = 0; f < d; f += 1) p[f] = t(f, g) || "null"; return u = 0 === p.length ? "[]" : "[\n" + a + m + "\n" + a + p.join(",\n" + a) + "\n" + h + "]", a = h, u; } var q = n(g), k; for(var x in q) { if(_isSet(q.hasOwnProperty) && !q.hasOwnProperty(x)){continue;} k = q[x]; u = t(k, g), u && p.push(e(k) + ": " + u); } return u = 0 === p.length ? "{}" : "{\n" + a + m + "\n" + a + p.join(",\n" + a) + "\n" + h + "}", a = h, u; } } catch (b) { return ""; } } function r(e) { if (_isString(e)) return e; a = "", l = " ", s = []; for (var n = t("", { "": e }), o = s.length; o;) new RegExp("/\\*\\*ref:" + o.toString(16) + "\\*\\*/").test(n) || (n = n.replace(new RegExp("[\r\n\t ]*/\\*\\*id:" + o.toString(16) + "\\*\\*/", "g"), "")), o--; return s = null, n; } var a, l, s, c = /[\\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, f = { "\b": "\\b", "\t": "\\t", "\n": "\\n", "\f": "\\f", "\r": "\\r", '"': '\\"', "\\": "\\\\" }; return { quote: e, getString: r }; }(); var _delay = function (callback, ms) { var delay = (function () { var timer = 0; return function (callback, ms) { clearTimeout(timer); timer = setTimeout(callback, ms||0); }; })(); return delay(callback, ms); }; var _promise = function(resolve, reject){ var jDomsPromise = function (jDomsPromise) { var that = this; this.state = 'pending'; this.value = undefined; this.consumers = []; jDomsPromise(this.resolve.bind(this), this.reject.bind(this)); }; jDomsPromise.prototype.fulfill = function (value) { if (this.state !== 'pending') return; this.state = 'fulfilled'; this.value = value; this.done(); }; jDomsPromise.prototype.reject = function (reason) { if (this.state !== 'pending') return; this.state = 'rejected'; this.value = reason; this.done(); }; jDomsPromise.prototype.then = function(onFulfilled, onRejected ) { var consumer = new jDomsPromise(function () {}); consumer.onFulfilled = _isFunction(onFulfilled)? onFulfilled : null; consumer.onRejected = _isFunction(onRejected)? onRejected : null; this.consumers = this.consumers.concat(consumer); this.done(); return consumer; }; jDomsPromise.prototype.done = function() { var promise = this; if (this.state === 'pending') return; var callbackName = this.state == 'fulfilled' ? 'onFulfilled' : 'onRejected'; var resolver = this.state == 'fulfilled' ? 'resolve' : 'reject'; setTimeout(function() { var pConsumers = promise.consumers.splice(0); for( var key in pConsumers ) { var consumer = pConsumers[key]; try { var callback = consumer[callbackName]; if (callback) { consumer.resolve(callback(promise.value)); } else { consumer[resolver](promise.value); } } catch (e) { consumer.reject(e); } } }, 0); }; jDomsPromise.prototype.resolve = function(x) { var wasCalled, then; if (this === x) { throw new TypeError('Circular reference: promise value is promise itself'); } if (x instanceof jDomsPromise) { x.then(this.resolve.bind(this), this.reject.bind(this)); } else if (x === Object(x)) { try { then = x.then; if (_isFunction(then)) { then.call(x, function resolve(y) { if (wasCalled) return; wasCalled = true; this.resolve(y); }.bind(this), function reject(reasonY) { if (wasCalled) return; wasCalled = true; this.reject(reasonY); }.bind(this)); } else { this.fulfill(x); } } catch(e) { if (wasCalled) return; this.reject(e); } } else { this.fulfill(x); } }; return new jDomsPromise(resolve, reject); }; var _dateParse = function(date){ return Date.parse(date); }; var _urlParse = function (url) { var loc= location||window.location; var data = { hash: loc.hash||"", host: loc.host||"", hostname: loc.hostname||"", href: loc||"", origin: loc.origin||"", password: loc.password||"", pathname: loc.pathname||"", port: loc.port||"", protocol: loc.protocol||"", search: loc.search||"", username: loc.username||"" }; var a = document.createElement("a"); a.href = url; data.hash = a.hash; data.host = a.host; data.hostname = a.hostname; data.href = a.href; data.origin = a.origin; data.password = a.password; data.pathname = a.pathname; data.port = a.port; data.protocol = a.protocol; data.search = a.search; data.username = a.username; return data; }; var _jsonParse = function (json_codes) { var data = {}; if ( _isObject(JSON) && _isFunction(JSON.parse) ) { try { data = JSON.parse(json_codes); } catch (err) {} } else if (_isObject(Components) && _isFunction(Components.classes) && _isFunction(Components.interfaces)) { try { var nativeJSON = Components.classes["@mozilla.org/dom/json;1"].createInstance(Components.interfaces.nsIJSON); data = nativeJSON.decode(json_codes); } catch (err) {} } else { var jsonTrims = json_codes.replace(/^\s+/, "").replace(/\s+$/, ""); var new_data = (function (jsstring) { var rvalidchars = /^[\],:{}\s]*$/, rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g; if (rvalidchars.test(jsonTrims.replace(rvalidescape, "@") .replace(rvalidtokens, "]") .replace(rvalidbraces, ""))) { return (new Function("return " + jsonTrims))(); } })(); data = !new_data ? data : new_data; } return data; }; var _xmlParse = function(xml){ if (window.DOMParser) { var parser = new DOMParser(); return parser.parseFromString(xml,"text/xml"); } if(_isSet(ActiveXObject)){ var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = false; return xmlDoc.loadXML(xml); } return {}; }; var _htmlParse = function(html){ if (html.toLowerCase().replace(/^\s+|\s+$/gm,'').indexOf('<!doctype') === 0 && document.implementation && document.implementation.createHTMLDocument ) { var doc = document.implementation.createHTMLDocument(""); doc.documentElement.innerHTML = html; return doc; } else if ('content' in document.createElement('template')) { var elm = document.createElement('template'); elm.innerHTML = html; return elm.content; } else { var docFrag = document.createDocumentFragment(); var el = document.createElement('body'); el.innerHTML = html; for (i = 0; 0 < el.childNodes.length;) { docFrag.appendChild(el.childNodes[i]); } return docFrag; } }; var _ajax = function (ajx) { var xhr; if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else if (window.ActiveXObject) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } if (ajx.xhr && ( _isFunction(ajx.method) || _isObject(ajx.method) ) ) { xhr = ajx.xhr; } var enctype = 'application'; if (ajx.enctype && _isString(ajx.method)) { if (ajx.method == 'multipart/form-data') { enctype = 'multipart'; } else if (ajx.method == 'text/plain') { enctype = 'text'; } } var url = ''; if (_isString(ajx.method)||_isNumber(ajx.method)) { url = ajx.url; } var method = 'GET'; if ( _isString(ajx.method) && _isString(ajx.method) && ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS', 'PATCH'].indexOf(ajx.method.toUpperCase()) > -1) { method = ajx.method.toUpperCase(); } var async = true; if (_isBoolean(ajx.async)) { xhr.async = ajx.async; } if (ajx.onBefore) { ajx.onBefore(xhr); } var data = _isFunction(FormData) ? new FormData() : ''; if (ajx.data && _isObject(ajx.data)) { for (var k in ajx.data) { if( _isFunction(FormData) ){ data.append(k, ajx.data[k]); }else{ try{ data+= k=+encodeURIComponent(ajx.data[k])+'&'; data = data.substring(0,data.length-1); }catch(err){} } } } if (_isString(ajx.data)) { data = ajx.data; if (method == 'GET' || method == 'PUT') { url = url + (data[0] == '?' ? data : '?' + data); } } xhr.withCredentials = _urlParse(url).hostname != window.location.hostname; if (_isBoolean(ajx.credentials)) { xhr.withCredentials = ajx.credentials; } if (_isString(ajx.type)) { xhr.responseType = ajx.type; } xhr.open(method, url, true); if (_isString(ajx.method) && ajx.method.toUpperCase() == 'POST') { xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8"); } if ( _isString(ajx.username) || _isNumber(ajx.username) || _isString(ajx.password) || _isNumber(ajx.password) ) { var authType = "Basic"; if ( _isString(ajx.authType) ){ authType = ajx.authType; } var authLogin = ""; if ( _isSet(ajx.username) && _isSet(ajx.password) ) { authLogin = _encodeBase64(ajx.username+':'+ajx.password); }else if ( _isSet(ajx.username) && _isUndefined(ajx.password) ) { authLogin = _encodeBase64(ajx.username); }else if ( _isUndefined(ajx.username) && _isSet(ajx.password) ) { authLogin = _encodeBase64(ajx.password); } xhr.setRequestHeader("Authorization", authType + ' ' + authLogin ); authType = null; authLogin = null; } if (_isFlatten(ajx.cache)) { xhr.setRequestHeader("Cache-control", ajx.cache); } //default, no-cache, reload, force-cache, only-if-cached if (_isObject(ajx.headers) || _isArray(ajx.headers) ) { for (var k in ajx.headers) { try{ xhr.setRequestHeader(k,ajx.headers[k]); }catch(err){} } } if (ajx.timeout && _isFlatten(ajx.timeout)) { xhr.timeout = parseInt(ajx.timeout); } xhr.onreadystatechange = function () { if (ajx.onReadyStateChange) { ajx.onReadyStateChange(xhr, this.readyState, this.status); } if (this.readyState == 0) { if (ajx.onNotConnected) { ajx.onNotConnected(xhr); } } if (this.readyState == 1) { if (ajx.onConnected) { ajx.onNotConnected(xhr); } } if (this.readyState == 2) { if (ajx.onReceived) { ajx.onReceived(xhr); } } if (this.readyState == 3) { if (ajx.onLoading) { ajx.onReceived(xhr); } } if (this.readyState == 4) { if (ajx.onComplete) { ajx.onComplete(xhr); } if (this.status == 200) { if (ajx.onSuccess) { ajx.onSuccess(xhr); } } } }; xhr.onloadstart = function (xhr) { if (ajx.onLoadStart) { ajx.onLoadStart(xhr); } }; xhr.onloadend = function (xhr) { if (ajx.onLoadEnd) { ajx.onLoadEnd(xhr); } }; xhr.ontimeout = function (evt) { if (ajx.onTimeout) { var loaded = false, total = false; if (evt.lengthComputable) { loaded = evt.loaded || evt.position; total = evt.loaded; } ajx.onTimeout(xhr, loaded, total); } }; xhr.onload = function (xhr) { if (ajx.onLoad) { ajx.onLoad(xhr); } }; xhr.onprogress = function (evt) { if (ajx.onProgress) { var loaded = false, total = false; if (evt.lengthComputable) { loaded = evt.loaded || evt.position; total = evt.loaded; } ajx.onProgress(xhr, loaded, total); } }; xhr.onabort = function (xhr) { if (ajx.onAbort) { ajx.onAbort(xhr); } }; xhr.onerror = function (xhr) { if (ajx.onError) { ajx.onError(xhr); } }; xhr.send(data); return xhr; }; var _eventCallback = function(ev){ var _ev = { altKey: true, bubbles: true, cancelable: true, changedTouches: true, ctrlKey: true, detail: true, eventPhase: true, metaKey: true, pageX: true, pageY: true, shiftKey: true, view: true, "char": true, code: true, charCode: true, key: true, keyCode: true, button: true, buttons: true, clientX: true, clientY: true, offsetX: true, offsetY: true, pointerId: true, pointerType: true, screenX: true, screenY: true, targetTouches: true, touches: true, which: true }; var tempArray = jDoms(_eventCallback.that).doms(_eventCallback.selectors); for (var q = 0; q < tempArray.length; q++) { ev = ev||window.event; var tgt = ev.target||ev.currentTarget||ev.srcElement; if( tempArray[q] == tgt || jDoms(tempArray[q]).child(tgt).length>0 || ( (_isSet(tempArray[q].compareDocumentPosition)) && tempArray[q].compareDocumentPosition(tgt)==16 ) || ( (_isSet(tempArray[q].contains)) && tempArray[q].contains(tgt) ) ){ for( var ek in ev ){ if(_isSet(ev.hasOwnProperty) && !ev.hasOwnProperty(ek)){continue;} _ev[ek] = ev[ek]; } var tgt = ["target", "targetElement", "srcElement"]; for(var t=0; t<tgt.length; t++){ _ev[tgt[t]] = tempArray[q]; if(_isSet(Object.defineProperty)){ Object.defineProperty(_ev, tgt[t], { writable: false, value: tempArray[q] }); } } _eventCallback.callback(_ev); } } this.that = null; this.selectors = null; this.callback = null; }; _eventCallback.type = null; _eventCallback.that = null; _eventCallback.selectors = null; _eventCallback.callback = null; /* Default prototype functions */ var functions = { version: version, length: function(){ return this.length || 0; }, doms: function (selectors) { var args = arguments || []; selectors = selectors || args[0]; var context = args[1] || ''; var new_selectors = []; if (this.length > 0) { for (var k = 0; k < this.length; k++) { var new_doms = jDoms(selectors, context, this[k]); for (var x = 0; x < new_doms.length; x++) { new_selectors = new_selectors.concat(new_doms[x]); } } } return jDoms(new_selectors); }, find: function (selectors) { var args = arguments; var context = args[1] || ''; return this.doms(selectors, context); }, search: function (selectors) { var args = arguments; var context = args[1] || ''; return this.doms(selectors, context); }, index: function(index){ try{ return (this.length==0) ? this._[parseInt(index)] : this[parseInt(index)] ; }catch(err){} return null; }, domIndex: function(index){ var item = []; try{ item = this[parseInt(index)] ; }catch(err){} return jDoms( item ); }, key: function(key){ try{ if( _isSet(this._.hasOwnProperty) ){ return this._[key]; } }catch(err){} return null; }, each: function (callback) { for (var i = 0; i < this.length; i++) { callback(this[i], i, this); } return this; }, parent: function () { var args = arguments || []; var selectors = args[0]; var array1 = []; var array2 = []; for (var i = 0; i < this.length; i++) { array1 = array1.concat(this[i].parentElement || this[i].parentNode); if (_isSet(selectors)) { var tempArray = jDoms(selectors, "", (this[i].ownerDocument || document)); for (var n = 0; n < tempArray.length; n++) { array2 = array2.concat(tempArray[n]); } } } if (_isSet(selectors)) { if (array2.length > 1) { array2 = array2.filter(function (v, i, a) { return a.indexOf(v) === i; }); } array1 = array1.filter(function (n) { return array2.indexOf(n) !== -1; }); } array1 = _unique(array1); return jDoms(array1); }, closest: function (selectors) { if (window.Element && !Element.prototype.closest) { Element.prototype.closest = function (s) { var matches = new jDoms.jDomsList(selectors, "", (this.document || this.ownerDocument || document)), i, el = this; do { i = matches.length; while (--i >= 0 && matches.item(i) !== el) {} } while ((i < 0) && (el = (el.parentElement || el.parentNode))); return el || []; }; } var array1 = []; for (var i = 0; i < this.length; i++) { if (_isSet(this[i].closest)) { var closestDoms = this[i].closest(selectors); for (var x = 0; x < closestDoms.length; x++) { array1 = array1.concat(closestDoms[x]); } } } array1 = _unique( array1 ); return jDoms(array1); }, child: function(){ var args = arguments || []; var selectors = args[0]; var array1 = []; var array2 = []; var childList, n; for (var i = 0; i < this.length; i++) { if(_isSet(this[i].children)){ childList = this[i].children; for (n = 0; n < childList.length; n++) { array1 = array1.concat(childList[n]); } }else if(_isSet(this[i].childNodes)){ childList = this[i].childNodes; for (n = 0; n < childList.length; n++) { if ( childList[i].nodeName && childList[i].nodeType==Node.ELEMENT_NODE ) { array1 = array1.concat(childList[n]); } } } array1 = array1.concat(); if (_isSet(selectors)) { var tempArray = jDoms(selectors, "", (this[i].ownerDocument || document)); for (var nn = 0; nn < tempArray.length; nn++) { array2 = array2.concat(tempArray[nn]); } } } if (_isSet(selectors)) { if (array2.length > 1) { array2 = array2.filter(function (v, p, a) { return a.indexOf(v) === p; }); } array1 = array1.filter(function (p) { return array2.indexOf(p) !== -1; }); } array1 = _unique( array1 ); return jDoms(array1); }, children: function(){ var args = arguments || []; var selectors = args[0]; return this.child(selectors); }, match: function (selectors) { var args = arguments || []; selectors = selectors || args[0] || []; var context = args[1] || '', root = _isSet(args[2]) ? args[2] : document; var array1 = []; var array2 = []; for (var i = 0; i < this.length; i++) { array1 = array1.concat(this[i]); } var tempArray = jDoms(selectors, context, root); for (var n = 0; n < tempArray.length; n++) { array2 = array2.concat(tempArray[n]); } if (_isSet(selectors)) { if (array2.length > 1) { array2 = array2.filter(function (v, i, a) { return a.indexOf(v) === i; }); } array1 = array1.filter(function (n) { return array2.indexOf(n) !== -1; }); } array1 = _unique(array1); return jDoms(array1); }, matchTo: function (selectors) { var args = arguments || []; selectors = selectors || args[0] || []; var context = args[1] || '', root = _isSet(args[2]) ? args[2] : document; var array1 = []; var array2 = []; for (var i = 0; i < this.length; i++) { array1 = array1.concat(this[i]); } var tempArray = jDoms(selectors, context, root); for (var n = 0; n < tempArray.length; n++) { array2 = array2.concat(tempArray[n]); } if (_isSet(selectors)) { if (array2.length > 1) { array2 = array2.filter(function (v, i, a) { return a.indexOf(v) === i; }); } array2 = array2.filter(function (n) { return array1.indexOf(n) !== -1; }); } array2 = _unique(array2); return jDoms(array2); }, merge: function (selectors) { var args = arguments || []; selectors = selectors || args[0] || []; var context = args[1] || '', root = _isSet(args[2]) ? args[2] : document; var array1 = []; var array2 = []; for (var i = 0; i < this.length; i++) { array1 = array1.concat(this[i]); } var tempArray = jDoms(selectors, context, root); for (var n = 0; n < tempArray.length; n++) { array2 = array2.concat(tempArray[n]); } if (_isSet(selectors)) { for( var k=0; k<array2.length; k++){ if(array1.indexOf(array2[k])<0){ array1 = array1.concat(array2[k]); } } } array1 = _unique(array1); return jDoms(array1); }, unique: function(){ var array1 = []; for (var i = 0; i < this.length; i++) { array1 = array1.concat(this[i]); } array1 = _unique(array1); return jDoms(array1); }, not: function (selectors) { var args = arguments || []; selectors = selectors || args[0]; var context = args[1] || "", root = _isSet(args[2]) ? args[2] : document; var array1 = []; var array2 = []; for (var i = 0; i < this.length; i++) { array1 = array1.concat(this[i]); } var tempArray = jDoms(selectors, context, root); for (var n = 0; n < tempArray.length; n++) { array2 = array2.concat(tempArray[n]); } if (_isSet(selectors)) { if (array2.length > 1) { array2 = array2.filter(function (v, i, a) { return a.indexOf(v) === i; }); } array1 = array1.filter(function (n) { return array2.indexOf(n) === -1; }); } array1 = _unique( array1 ); return jDoms(array1); }, is: function (selectors) { if (_isUndefined(this[0])) { return false; } var array1 = [ this[0] ]; var array2 = []; var tempArray = jDoms(this[0]).match(selectors); for (var n = 0; n < tempArray.length; n++) { array2 = array2.concat(tempArray[n]); } array1 = array1.filter(function (n) { return array2.indexOf(n) !== -1; }); return array1.length > 0 ? true : false; }, isSame: function (selectors) { var args = arguments || []; selectors = selectors || args[0] || []; var context = args[1] || '', root = _isSet(args[2]) ? args[2] : document; var a = []; var b = []; for (var i = 0; i < this.length; i++) { a = a.concat(this[i]); } var tempArray = jDoms(selectors, context, root); for (var n = 0; n < tempArray.length; n++) { b = b.concat(tempArray[n]); } a = jDoms.unique(a); b = jDoms.unique(b); if (a.length !== b.length){ return false; } if (a === b) { return true; } for (var k = 0; k < a.length; k++) { if ( b.indexOf(a[k])==-1){ return false; } } return true; }, isContains: function(selectors){ var args = arguments || []; selectors = selectors || args[0]; var context = args[1] || '', root = _isSet(args[2]) ? args[2] : document; var first = this[0]; var second = jDoms(selectors, context, root)[0]; if(_isSet(first) && _isSet(second) && _isSet(first.contains) ){ return first.contains(second); } return false; }, isContainsBy: function(selectors){ var args = arguments || []; selectors = selectors || args[0]; var context = args[1] || '', root = _isSet(args[2]) ? args[2] : document; var second = this[0]; var first = jDoms(selectors, context, root)[0]; if(_isSet(first) && _isSet(second) && _isSet(first.contains) ){ return first.contains(second); } return false; }, previous: function () { var args = arguments || []; var selectors = args[0]; var array1 = []; var array2 = []; for (var i = 0; i < this.length; i++) { var previousSibling = this[i].previousSibling; while (previousSibling && previousSibling.nodeType != 1) { previousSibling = previousSibling.previousSibling; } array1 = array1.concat(previousSibling); if (_isSet(selectors)) { var tempArray = jDoms(selectors, "", (this[i].ownerDocument || document)); for (var n = 0; n < tempArray.length; n++) { array2 = array2.concat(tempArray[n]); } } } if (_isSet(selectors)) { if (array2.length > 1) { array2 = array2.filter(function (v, i, a) { return a.indexOf(v) === i; }); } array1 = array1.filter(function (n) { return array2.indexOf(n) !== -1; }); } array1 = _unique(array1); return jDoms(array1); }, next: function () { var args = arguments || []; var selectors = args[0]; var array1 = []; var array2 = []; for (var i = 0; i < this.length; i++) { var nextSibling = this[i].nextSibling; while (nextSibling && nextSibling.nodeType != 1) { nextSibling = nextSibling.nextSibling; } array1 = array1.concat(nextSibling); if (_isSet(selectors)) { var tempArray = jDoms(selectors, "", (this[i].ownerDocument || document)); for (var n = 0; n < tempArray.length; n++) { array2 = array2.concat(tempArray[n]); } } } if (_isSet(selectors)) { if (array2.length > 1) { array2 = array2.filter(function (v, p, a) { return a.indexOf(v) === p; }); } array1 = array1.filter(function (p) { return array2.indexOf(p) !== -1; }); } array1 = _unique(array1); return jDoms(array1); }, first: function () { var array1 = this[0] ? [this[0]] : []; if (!array1) { return jDoms(array1); } var args = arguments || []; var selectors = args[0]; var array2 = []; if (_isSet(selectors)) { var tempArray = jDoms(selectors, "", (array1[0].ownerDocument || document)); for (var n = 0; n < tempArray.length; n++) { array2 = array2.concat(tempArray[n]); } } if (_isSet(selectors)) { if (array2.length > 1) { array2 = array2.filter(function (v, i, a) { return a.indexOf(v) === i; }); } array1 = array1.filter(function (n) { return array2.indexOf(n) !== -1; }); } array1 = _unique(array1); return jDoms(array1); }, last: function () { var array1 = this[this.length - 1] ? [this[this.length - 1]] : []; if (!array1) { return jDoms(array1); } var args = arguments || []; var selectors = args[0]; var array2 = []; if (_isSet(selectors)) { var tempArray = jDoms(selectors, "", (array1[0].ownerDocument || document)); for (var n = 0; n < tempArray.length; n++) { array2 = array2.concat(tempArray[n]); } } if (_isSet(selectors)) { if (array2.length > 1) { array2 = array2.filter(function (v, i, a) { return a.indexOf(v) === i; }); } array1 = array1.filter(function (n) { return array2.indexOf(n) !== -1; }); } array1 = _unique(array1); return jDoms(array1); }, clone: function () { var args = arguments; var deep = (_isSet(args[0]) && args[0] === false) ? false : true; for (var i = 0; i < this.length; i++) { if (_isSet(this[i].cloneNode)) { return this[i].cloneNode(deep); } } return this; }, addEvent: function (evt) { var args = arguments; evt = evt || args[0] || ""; var selectors, callback, useCapture = false; var evtArray = evt.split(" "); if (_isSet(args[3])) { selectors = args[1]; callback = args[2]; useCapture = (args[3] === true); } else if (_isSet(args[2])) { selectors = args[1]; callback = args[2]; } else if (_isSet(args[1])) { callback = args[1]; } var array2 = [], tempArray, n, reCallback, that; for (var i = 0; i < this.length; i++) { that = this[i]; for (n = 0; n < evtArray.length; n++) { if (_isSet(selectors)) { _eventCallback.that = this[i]; _eventCallback.selectors = selectors; _eventCallback.callback = callback; _eventCallback.type = evtArray[n]; jDoms(that).setProperty("jDomsEvent-"+evtArray[n]+"-"+callback.name, callback.name); if (this[i].addEventListener) { this[i].addEventListener(evtArray[n], _eventCallback, useCapture); } else if (this[i].attachEvent) { this[i].attachEvent('on' + evtArray[n], _eventCallback); } }else{ if (this[i].addEventListener) { this[i].addEventListener(evtArray[n], callback, useCapture); } else if (this[i].attachEvent) { this[i].attachEvent('on' + evtArray[n], callback); } } } } }, on: function (evt) { var args = arguments; evt = args[0]; var selectors = args[1], callback = args[2], useCapture = args[3]; return this.addEvent(evt, selectors, callback, useCapture); }, removeEvent: function (evt) { var args = arguments; evt = evt || args[0] || ""; var selectors, callback, useCapture = false; var evtArray = evt.split(" "); if (_isSet(args[3])) { selectors = args[1]; callback = args[2]; useCapture = (args[3] === true); } else if (_isSet(args[2])) { selectors = args[1]; callback = args[2]; } else if (_isSet(args[1])) { callback = args[1]; } var array2 = [], tempArray, n, reCallback, that; if(_isFunction(callback)){ for (var i = 0; i < this.length; i++) { that = this[i]; for (n = 0; n < evtArray.length; n++) { if (_isSet(selectors)) { if(jDoms(that).getProperty("jDomsEvent-"+evtArray[n]+"-"+callback.name)==callback.name){ if (this[i].removeEventListener) { this[i].removeEventListener(evtArray[n], _eventCallback, useCapture); } else if (this[i].detachEvent) { this[i].detachEvent('on' + evtArray[n], _eventCallback); } } }else{ if (this[i].removeEventListener) { this[i].removeEventListener(evtArray[n], callback, useCapture); } else if (this[i].detachEvent) { this[i].detachEvent('on' + evtArray[n], callback); } } } } } }, off: function (evt) { var args = arguments; evt = args[0]; var selectors = args[1], callback = args[2], useCapture = args[3]; return this.removeEvent(evt, selectors, callback, useCapture); }, fireEvent: function (eventName) { for (var i = 0; i < this.length; i++) { var doc = document; if (this[i].ownerDocument) { doc = this[i].ownerDocument; } else if (this[i].nodeType == 9) { doc = this[i]; } if (this[i].dispatchEvent) { var eventClass = ""; switch (eventName) { case "click": case "mousedown": case "mouseup": eventClass = "MouseEvents"; break; case "focus": case "change": case "blur": case "select": eventClass = "HTMLEvents"; break; } var event = doc.createEvent(eventClass); var bubbles = eventName == "change" ? false : true; if ( _isSet(event.initEvent) ) { event.initEvent(eventName, bubbles, true); } event.synthetic = true; this[i].dispatchEvent(event, true); } else if (this[i].fireEvent && doc.createEventObject) { var event = doc.createEventObject(); event.synthetic = true; this[i].fireEvent("on" + eventName, event); } } return this; }, trigger: function (eventName) { return this.fireEvent(eventName); }, create: function(){ var array1 = []; if(_isString(this._)){ var tags = this._.replace(/(?:\s*-\s*)+|\s{2,}/g, " ").split(" "); var tag, tagName; for( var i=0; i<tags.length; i++){ tag = null; tagName = tags[i].replace(/[^A-Za-z]/gi,""); if( _isString(this.__) ){ tag = document.createElementNS( this.__, tagName ); if( _isObject(this.___) ){ try{ for( var attrNameNS in this.___ ){ if( _isSet(this.__.hasOwnProperty) && !this.__.hasOwnProperty(attrNameNS) ){ continue; } tag.setAttribute( attrNameNS, this.___[attrNameNS] ); } }catch(err){} } } if( _isObject(this.__) ){ try{ tag = document.createElement( tagName ); for( var attrName in this.__ ){ if( _isSet(this.__.hasOwnProperty) && !this.__.hasOwnProperty(attrName) ){ continue; } tag.setAttribute( attrName, this.__[attrName] ); } tag.innerHTML = this.___; }catch(err){} } if(tag){ if(_isSet(arguments[0])){ var props = arguments[0]; if( _isObject(props) ){ try{ for( var propName in props ){ if( _isSet(props.hasOwnProperty) && !props.hasOwnProperty(propName) ){ continue; } tag[propName] = props[propName]; } }catch(err){} } } array1 = array1.concat(tag); } } } return jDoms(array1); }, createElement: function(){ return this.create( arguments[0] ); }, getHtml: function () { var args = arguments || []; for (var i = 0; i < this.length; i++) { if (_isSet(args[0]) && args[0] === true) { if (_isSet(this[i].outerHTML)) { return this[i].outerHTML; } else { e.insertAdjacentHTML('beforeBegin', code); var new_elem = e.previousSibling; e.parentElement.removeChild(e); var html = ""; if ((_isSet(this[i].nodeName) || _isSet(this[i].tagName)) && _isSet(this[i].attributes)) { var endless = ['!doctype', 'br', 'hr', 'img', 'link', 'meta', 'input']; var tag = this[i].nodeName || this[i].tagName; tag = tag.toLowerCase(); var attrs = ""; for (var n = 0; n < this[i].attributes.length; n++) { attrs += this[i].attributes[n].nodeName; attrs += this[i].attributes[n].nodeValue.indexOf('"') > -1 ? "'" : '"'; attrs += this[i].attributes[n].nodeValue; attrs += this[i].attributes[n].nodeValue.indexOf('"') > -1 ? "'" : '"'; } html = _isSet(this[i].innerHTML)? this[i].innerHTML : ""; return "<" + tag + (attrs == "" ? "" : " " + attrs) + (endless.indexOf(tag) > -1 ? "/>" : ">" + html + "<" + tag + ">"); } } } if (_isSet(this[i].innerHTML)) { return this[i].innerHTML; } } return ""; }, setHtml: function (html) { var args = arguments || []; for (var i = 0; i < this.length; i++) { if (_isSet(args[1]) && args[1] === true) { if (_isSet(this[i].outerHTML)) { this[i].outerHTML = html; } else { this[i].insertAdjacentHTML('beforeBegin', code); var parent = this[i].parentElement || this[i].parentNode; parent.removeChild(e); } } else if (_isSet(this[i].innerHTML)) { this[i].innerHTML = html; } } return this; }, html: function () { var args = arguments || []; var set = _isSet(args[0]); var outer = ((_isSet(args[1]) && args[1] === true) === true); if (set) { return this.setHtml(args[0], outer); } else { return this.getHtml(outer); } }, appendHtml: function (html) { for (var i = 0; i < this.length; i++) { try{ if( (_isObject(html) && _isNumber(html.length)) || _isArray(html) ){ for( var x=0; x<html.length; x++ ){ this[i].appendChild(html[x]); } }else if(_isSet(html.nodeType)){ this[i].appendChild(html); }else{ var frag = document.createDocumentFragment(); var span = document.createElement("span"); span.innerHTML = html; for (var n = 0; n < span.childNodes.length; n++) { frag.appendChild(span.childNodes[n]); } this[i].appendChild(frag); } }catch(err){} } return this; }, append: function (html){ return this.appendHtml(html); }, prependHtml: function (html) { for (var i = 0; i < this.length; i++) { try{ if(_isObject(html)){ if(_isSet(html.hasOwnProperty)){ for(var k in html){ if(!html.hasOwnProperty(k)){continue;} if (this[i].firstChild) { this[i].insertBefore(html[k], this[i].firstChild); } else { this[i].appendChild(html[k]); } } }else if(_isSet(html.nodeType)){ if (this[i].firstChild) { this[i].insertBefore(html, this[i].firstChild); } else { this[i].appendChild(html); } } }else{ var frag = document.createDocumentFragment(); var span = document.createElement("span"); span.innerHTML = html; for (var n = 0; n < span.childNodes.length; n++) { frag.appendChild(span.childNodes[n]); } if (this[i].firstChild) { this[i].insertBefore(frag, this[i].firstChild); } else { this[i].appendChild(frag); } } }catch(err){} } return this; }, prepend: function (html){ return this.prependHtml(html); }, appendHtmlTo: function (selectors) { var args = arguments; selectors = args[0] || selectors || []; var context = args[1] || '', root = _isSet(args[2]) ? args[2] : document; var jDoms2 = jDoms(selectors, context, root); return jDoms2.appendHtml(this); }, appendTo: function (selectors){ var args = arguments; selectors = args[0] || selectors || []; var context = args[1] || '', root = _isSet(args[2]) ? args[2] : document; return this.appendHtmlTo(selectors,context,root); }, prependHtmlTo: function (selectors) { var args = arguments || []; selectors = selectors || args[0] || []; var context = args[1] || '', root = _isSet(args[2]) ? args[2] : document; var jDoms2 = jDoms(selectors, context, root); return jDoms2.prependHtml(this); }, prependTo: function (selectors){ var args = arguments; selectors = args[0] || selectors || []; var context = args[1] || '', root = _isSet(args[2]