From 37534fada3011d806c9d63601411dbccdab0ba4e Mon Sep 17 00:00:00 2001 From: Stuart Date: Tue, 6 Aug 2019 14:45:16 +0100 Subject: [PATCH] Update stuQuery --- stuquery.js | 880 +++++++++++++++++++++++++----------------------- stuquery.min.js | 8 +- 2 files changed, 459 insertions(+), 429 deletions(-) diff --git a/stuquery.js b/stuquery.js index 2af2dd0..6139cfc 100644 --- a/stuquery.js +++ b/stuquery.js @@ -1,16 +1,62 @@ /*! - * stuQuery v1.0.14 + * stuQuery */ -// I don't like to pollute the global namespace -// but I can't get this to work any other way. -var eventcache = {}; +(function(root){ -function stuQuery(els){ - // Make our own fake, tiny, version of jQuery simulating the parts we need - var elements; + var eventcache = {}; - this.querySelector = function(els,selector){ - var result = new Array(); + function stuQuery(els){ + // Make our own fake, tiny, version of jQuery simulating the parts we need + this.stuquery = "1.0.26"; + + this.getBy = function(e,s){ + var i,m,k; + i = -1; + var result = []; + if(s.indexOf(':eq') > 0){ + m = s.replace(/(.*)\:eq\(([0-9]+)\)/,'$1 $2').split(" "); + s = m[0]; + i = parseInt(m[1]); + } + if(s[0] == '.') els = e.getElementsByClassName(s.substr(1)); + else if(s[0] == '#') els = e.getElementById(s.substr(1)); + else els = e.getElementsByTagName(s); + if(!els) els = []; + // If it is a select field we don't want to select the options within it + if(els.nodeName && els.nodeName=="SELECT") result.push(els); + else{ + if(typeof els.length!=="number") els = [els]; + for(k = 0; k < els.length; k++){ result.push(els[k]); } + if(i >= 0 && result.length > 0){ + if(i < result.length) result = [result[i]]; + else result = []; + } + } + return result; + }; + this.matchSelector = function(e,s){ + // Does this one element match the s + if(s[0] == '.'){ + s = s.substr(1); + for(var i = 0; i < e.classList.length; i++) if(e.classList[i] == s) return true; + }else if(s[0] == '#'){ + if(e.id == s.substr(1)) return true; + }else{ + if(e.tagName == s.toUpperCase()) return true; + } + return false; + }; + if(typeof els==="string") this.e = this.querySelector(document,els); + else if(typeof els==="object") this.e = (typeof els.length=="number") ? els : [els]; + for(var it in this.e){ + if(this.e[it]) this[it] = this.e[it]; + } + this.length = (this.e ? this.e.length : 0); + + return this; + } + stuQuery.prototype.querySelector = function(els,selector){ + var result = []; var a,els2,i,j,k,tmp; if(selector.indexOf(':eq') >= 0){ a = selector.split(' '); @@ -18,7 +64,7 @@ function stuQuery(els){ if(i==0){ tmp = this.getBy(els,a[i]); }else{ - els2 = new Array(); + els2 = []; for(j = 0; j < tmp.length; j++) els2 = els2.concat(this.getBy(tmp[j],a[i])); tmp = els2.splice(0); } @@ -26,465 +72,449 @@ function stuQuery(els){ }else tmp = els.querySelectorAll(selector); // We can use the built-in selector for(k = 0; k < tmp.length; k++){ result.push(tmp[k]); } return result; - } - this.getBy = function(e,s){ - var i = -1; - var result = new Array(); - if(s.indexOf(':eq') > 0){ - var m = s.replace(/(.*)\:eq\(([0-9]+)\)/,'$1 $2').split(" "); - s = m[0]; - i = parseInt(m[1]); + }; + stuQuery.prototype.ready = function(f){ + if(/in/.test(document.readyState)) setTimeout('S(document).ready('+f+')',9); + else f(); + }; + stuQuery.prototype.html = function(html){ + // Return HTML or set the HTML + if(typeof html==="number") html = ''+html; + if(typeof html!=="string" && this.length == 1) return this[0].innerHTML; + if(typeof html==="string") for(var i = 0; i < this.length; i++) this[i].innerHTML = html; + return this; + }; + stuQuery.prototype.append = function(html){ + if(!html && this.length == 1) return this[0].innerHTML; + if(html){ + for(var i = 0; i < this.length; i++){ + var d = document.createElement('template'); + d.innerHTML = html; + var c = (typeof d.content==="undefined" ? d : d.content); + if(c.childNodes.length > 0) while(c.childNodes.length > 0) this[i].appendChild(c.childNodes[0]); + else this[i].append(html); + } } - if(s[0] == '.') els = e.getElementsByClassName(s.substr(1)); - else if(s[0] == '#') els = e.getElementById(s.substr(1)); - else els = e.getElementsByTagName(s); - if(!els) els = []; - // If it is a select field we don't want to select the options within it - if(els.nodeName && els.nodeName=="SELECT") result.push(els); - else{ - if(typeof els.length!=="number") els = [els]; - for(k = 0; k < els.length; k++){ result.push(els[k]); } - if(i >= 0 && result.length > 0){ - if(i < result.length) result = [result[i]]; - else result = []; + return this; + }; + stuQuery.prototype.prepend = function(t){ + var i,j,d,e; + if(!t && this.length==1) return this[0].innerHTML; + for(i = 0 ; i < this.length ; i++){ + d = document.createElement('div'); + d.innerHTML = t; + e = d.childNodes; + for(j = e.length-1; j >= 0; j--) this[i].insertBefore(e[j], this[i].firstChild); + } + return this; + }; + stuQuery.prototype.before=function(t){ + var i,d,e,j; + for(i = 0 ; i < this.length ; i++){ + d = document.createElement('div'); + d.innerHTML = t; + e = d.childNodes; + for(j = 0; j < e.length; j++) this[i].parentNode.insertBefore(e[j], this[i]); + } + return this; + }; + stuQuery.prototype.after = function(t){ + for(var i = 0 ; i < this.length ; i++) this[i].insertAdjacentHTML('afterend', t); + return this; + }; + function NodeMatch(a,el){ + if(a && a.length > 0){ + for(var i = 0; i < a.length; i++){ + if(a[i].node == el) return {'success':true,'match':i}; } } - return result; + return {'success':false}; } - this.matchSelector = function(e,s){ - var result = false; - // Does this one element match the s - if(s[0] == '.'){ - s = s.substr(1); - for(var i = 0; i < e.classList.length; i++) if(e.classList[i] == s) return true; - }else if(s[0] == '#'){ - if(e.id == s.substr(1)) return true; - }else{ - if(e.tagName == s.toUpperCase()) return true; + function storeEvents(e,event,fn,fn2,data){ + if(!eventcache[event]) eventcache[event] = []; + eventcache[event].push({'node':e,'fn':fn,'fn2':fn2,'data':data}); + } + function getEvent(e){ + if(eventcache[e.type]){ + var m = NodeMatch(eventcache[e.type],e.currentTarget); + if(m.success){ + if(m.match.data) e.data = eventcache[e.type][m.match].data; + return {'fn':eventcache[e.type][m.match].fn,'data':e}; + } } - return false; + return function(){ return {'fn':''}; }; } + stuQuery.prototype.off = function(event){ + // Try to remove an event with attached data and supplied function, fn. - if(typeof els==="string") this.e = this.querySelector(document,els); - else if(typeof els==="object") this.e = (typeof els.length=="number") ? els : [els]; - for(var it in this.e) this[it] = this.e[it]; - this.length = (this.e ? this.e.length : 0); - - return this; -} -stuQuery.prototype.ready = function(f){ /in/.test(document.readyState)?setTimeout('S(document).ready('+f+')',9):f() } -stuQuery.prototype.html = function(html){ - // Return HTML or set the HTML - if(typeof html==="number") html = ''+html; - if(typeof html!=="string" && this.length == 1) return this[0].innerHTML; - if(typeof html==="string") for(var i = 0; i < this.length; i++) this[i].innerHTML = html; - return this; -} -stuQuery.prototype.append = function(html){ - if(!html && this.length == 1) return this[0].innerHTML; - if(html){ - for(var i = 0; i < this.length; i++){ - var d = document.createElement('template'); - d.innerHTML = html; - var c = (typeof d.content==="undefined" ? d : d.content); - if(c.childNodes.length > 0) while(c.childNodes.length > 0) this[i].appendChild(c.childNodes[0]); - else this[i].append(html); + // If the remove function doesn't exist, we make it + if(typeof Element.prototype.removeEventListener !== "function"){ + Element.prototype.removeEventListener = function (sEventType, fListener /*, useCapture (will be ignored!) */) { + if (!oListeners.hasOwnProperty(sEventType)) { return; } + var oEvtListeners = oListeners[sEventType]; + for (var nElIdx = -1, iElId = 0; iElId < oEvtListeners.aEls.length; iElId++) { + if (oEvtListeners.aEls[iElId] === this) { nElIdx = iElId; break; } + } + if (nElIdx === -1) { return; } + for (var iLstId = 0, aElListeners = oEvtListeners.aEvts[nElIdx]; iLstId < aElListeners.length; iLstId++) { + if (aElListeners[iLstId] === fListener) { aElListeners.splice(iLstId, 1); } + } + }; } - } - return this; -} -stuQuery.prototype.prepend = function(j){ - if(!j && this.length==1) return this[0].innerHTML; - if(j) for(var e=0;e 0){ - for(var i = 0; i < a.length; i++){ - if(a[i].node == el) return {'success':true,'match':i}; + for(var i = 0; i < this.length; i++){ + var m = NodeMatch(eventcache[event],this.e[i]); + if(m.success){ + this[i].removeEventListener(event,eventcache[event][m.match].fn2,false); + eventcache[event].splice(m.match,1); + } } - } - return {'success':false}; -} -function storeEvents(e,event,fn,fn2,data){ - if(!eventcache[event]) eventcache[event] = new Array(); - eventcache[event].push({'node':e,'fn':fn,'fn2':fn2,'data':data}); -} -function getEvent(e){ - if(eventcache[e.type]){ - var m = NodeMatch(eventcache[e.type],e.currentTarget); - if(m.success){ - if(m.match.data) e.data = eventcache[e.type][m.match].data; - return {'fn':eventcache[e.type][m.match].fn,'data':e}; + return this; + }; + stuQuery.prototype.on = function(event,data,fn){ + // Add events + var events = (event || window.event).split(/ /); + if(typeof data==="function" && !fn){ + fn = data; + data = ""; } - } - return function(){ return {'fn':''}; } -} -stuQuery.prototype.off = function(event){ - // Try to remove an event with attached data and supplied function, fn. + if(typeof fn !== "function") return this; - // If the remove function doesn't exist, we make it - if(typeof Element.prototype.removeEventListener !== "function"){ - Element.prototype.removeEventListener = function (sEventType, fListener /*, useCapture (will be ignored!) */) { - if (!oListeners.hasOwnProperty(sEventType)) { return; } - var oEvtListeners = oListeners[sEventType]; - for (var nElIdx = -1, iElId = 0; iElId < oEvtListeners.aEls.length; iElId++) { - if (oEvtListeners.aEls[iElId] === this) { nElIdx = iElId; break; } - } - if (nElIdx === -1) { return; } - for (var iLstId = 0, aElListeners = oEvtListeners.aEvts[nElIdx]; iLstId < aElListeners.length; iLstId++) { - if (aElListeners[iLstId] === fListener) { aElListeners.splice(iLstId, 1); } + if(this.length > 0){ + var _obj = this; + var f; + for(var ev = 0; ev < events.length; ev++){ + event = events[ev]; + f = function(b){ + var e = getEvent({'currentTarget':this,'type':event,'data':data,'originalEvent':b,'preventDefault':function(){ if(b.preventDefault) b.preventDefault(); },'stopPropagation':function(){ + if(b.stopImmediatePropagation) b.stopImmediatePropagation(); + if(b.stopPropagation) b.stopPropagation(); + if(b.cancelBubble!=null) b.cancelBubble = true; + }}); + if(typeof e.fn === "function") return e.fn.call(_obj,e.data); + }; + for(var i = 0; i < this.length; i++){ + storeEvents(this[i],event,fn,f,data); + if(this[i].addEventListener) this[i].addEventListener(event, f, false); + else if(this[i].attachEvent) this[i].attachEvent(event, f); + } } } - } - for(var i = 0; i < this.length; i++){ - var m = NodeMatch(eventcache[event],this.e[i]); - if(m.success){ - this[i].removeEventListener(event,eventcache[event][m.match].fn2,false); - eventcache[event].splice(m.match,1); - } - } - return this; -} -stuQuery.prototype.on = function(event,data,fn){ - // Add events - var events = (event || window.event).split(/ /); - this.cache = [4,5,6]; - if(typeof data==="function" && !fn){ - fn = data; - data = ""; - } - if(typeof fn !== "function") return this; + return this; + }; + stuQuery.prototype.trigger = function(e,orig){ + var event; // The custom event that will be created + var events = e.split(/ /); - if(this.length > 0){ - var _obj = this; - var f; for(var ev = 0; ev < events.length; ev++){ - event = events[ev]; - f = function(b){ - var e = getEvent({'currentTarget':this,'type':event,'data':data,'originalEvent':b,'preventDefault':function(){ if(b.preventDefault) b.preventDefault(); },'stopPropagation':function(){ - if(b.stopImmediatePropagation) b.stopImmediatePropagation(); - if(b.stopPropagation) b.stopPropagation(); - if(b.cancelBubble!=null) b.cancelBubble = true; - }}); - if(typeof e.fn === "function") return e.fn.call(_obj,e.data); + if(document.createEvent) { + event = document.createEvent("HTMLEvents"); + event.initEvent((orig||events[ev]), true, true); + }else{ + event = document.createEventObject(); + event.eventType = (orig||events[ev]); } - for(var i = 0; i < this.length; i++){ - storeEvents(this[i],event,fn,f,data); - if(this[i].addEventListener) this[i].addEventListener(event, f, false); - else if(this[i].attachEvent) this[i].attachEvent(event, f); + event.eventName = e; + + for(var i = 0 ; i < this.length ; i++){ + if(document.createEvent) this[i].dispatchEvent(event); + else this[i].fireEvent("on" + event.eventType, event); } } - } - return this; -} -stuQuery.prototype.trigger = function(e){ - var event; // The custom event that will be created - events = e.split(/ /); - for(var ev = 0; ev < events.length; ev++){ - if(document.createEvent) { - event = document.createEvent("HTMLEvents"); - event.initEvent(events[ev], true, true); - }else{ - event = document.createEventObject(); - event.eventType = events[ev]; + return this; + }; + stuQuery.prototype.focus = function(){ + // If there is only one element, we trigger the focus event + if(this.length == 1) this[0].focus(); + return this; + }; + stuQuery.prototype.blur = function(){ + // If there is only one element, we trigger the blur event + if(this.length == 1) this[0].blur(); + return this; + }; + stuQuery.prototype.remove = function(){ + // Remove DOM elements + if(this.length < 1) return this; + for(var i = this.length-1; i >= 0; i--){ + if(!this[i]) return; + if(typeof this[i].remove==="function") this[i].remove(); + else if(typeof this[i].parentElement.removeChild==="function") this[i].parentElement.removeChild(this[i]); } - - event.eventName = e; - - for(var i = 0 ; i < this.length ; i++){ - if(document.createEvent) this[i].dispatchEvent(event); - else this[i].fireEvent("on" + event.eventType, event); + return this; + }; + stuQuery.prototype.hasClass = function(cls){ + // Check if a DOM element has the specified class + var result = true; + for(var i = 0; i < this.length; i++){ + if(!this[i].className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"))) result = false; } - } - - return this; -} -stuQuery.prototype.focus = function(){ - // If there is only one element, we trigger the focus event - if(this.length == 1) this[0].focus(); - return this; -} -stuQuery.prototype.blur = function(){ - // If there is only one element, we trigger the blur event - if(this.length == 1) this[0].blur(); - return this; -} -stuQuery.prototype.remove = function(){ - // Remove DOM elements - if(this.length < 1) return this; - for(var i = this.length-1; i >= 0; i--){ - if(!this[i]) return; - if(typeof this[i].remove==="function") this[i].remove(); - else if(typeof this[i].parentElement.removeChild==="function") this[i].parentElement.removeChild(this[i]); - } - return this; -} -stuQuery.prototype.hasClass = function(cls){ - // Check if a DOM element has the specified class - var result = true; - for(var i = 0; i < this.length; i++){ - if(!this[i].className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"))) result = false; - } - return result; -} -stuQuery.prototype.toggleClass = function(cls){ - // Toggle a class on a DOM element - for(var i = 0; i < this.length; i++){ - if(this[i].className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"))) this[i].className = this[i].className.replace(new RegExp("(\\s|^)" + cls + "(\\s|$)", "g")," ").replace(/ $/,''); - else this[i].className = (this[i].className+' '+cls).replace(/^ /,''); - } - return this; -} -stuQuery.prototype.addClass = function(cls){ - // Add a class on a DOM element - for(var i = 0; i < this.length; i++){ - if(!this[i].className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"))) this[i].className = (this[i].className+' '+cls).replace(/^ /,''); - } - return this; -} -stuQuery.prototype.removeClass = function(cls){ - // Remove a class on a DOM element - for(var i = 0; i < this.length; i++){ - while(this[i].className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"))) this[i].className = this[i].className.replace(new RegExp("(\\s|^)" + cls + "(\\s|$)", "g")," ").replace(/ $/,'').replace(/^ /,''); - } - return this; -} -stuQuery.prototype.css = function(css){ - var styles; - if(this.length==1 && typeof css==="string"){ - styles = window.getComputedStyle(this[0]); - return styles[css]; - } - for(var i = 0; i < this.length; i++){ - // Read the currently set style - styles = {}; - var style = this[i].getAttribute('style'); - if(style){ - var bits = this[i].getAttribute('style').split(";"); - for(var b = 0; b < bits.length; b++){ - var pairs = bits[b].split(":"); - if(pairs.length==2) styles[pairs[0]] = pairs[1]; + return result; + }; + stuQuery.prototype.toggleClass = function(cls){ + // Toggle a class on a DOM element + for(var i = 0; i < this.length; i++){ + if(this[i].className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"))) this[i].className = this[i].className.replace(new RegExp("(\\s|^)" + cls + "(\\s|$)", "g")," ").replace(/ $/,''); + else this[i].className = (this[i].className+' '+cls).replace(/^ /,''); + } + return this; + }; + stuQuery.prototype.addClass = function(cls){ + // Add a class on a DOM element + for(var i = 0; i < this.length; i++){ + if(!this[i].className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"))) this[i].className = (this[i].className+' '+cls).replace(/^ /,''); + } + return this; + }; + stuQuery.prototype.removeClass = function(cls){ + // Remove a class on a DOM element + for(var i = 0; i < this.length; i++){ + while(this[i].className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"))) this[i].className = this[i].className.replace(new RegExp("(\\s|^)" + cls + "(\\s|$)", "g")," ").replace(/ $/,'').replace(/^ /,''); + } + return this; + }; + stuQuery.prototype.css = function(css){ + var styles,i,key; + if(this.length==1 && typeof css==="string"){ + styles = window.getComputedStyle(this[0]); + return styles[css]; + } + for(i = 0; i < this.length; i++){ + // Read the currently set style + styles = {}; + var style = this[i].getAttribute('style'); + if(style){ + var bits = this[i].getAttribute('style').split(";"); + for(var b = 0; b < bits.length; b++){ + var pairs = bits[b].split(":"); + if(pairs.length==2) styles[pairs[0]] = pairs[1]; + } + } + if(typeof css==="object"){ + // Add the user-provided style to what was there + for(key in css){ + if(typeof css[key]!=="undefined") styles[key] = css[key]; + } + // Build the CSS string + var newstyle = ''; + for(key in styles){ + if(typeof styles[key]!=="undefined"){ + if(newstyle) newstyle += ';'; + if(styles[key]) newstyle += key+':'+styles[key]; + } + } + // Update style + this[i].setAttribute('style',newstyle); } } - if(typeof css==="object"){ - // Add the user-provided style to what was there - for(key in css) styles[key] = css[key]; - // Build the CSS string - var newstyle = ''; - for(key in styles){ - if(newstyle) newstyle += ';'; - if(styles[key]) newstyle += key+':'+styles[key]; + return this; + }; + stuQuery.prototype.parent = function(){ + var tmp = []; + for(var i = 0; i < this.length; i++) tmp.push(this[i].parentElement); + return S(tmp); + }; + stuQuery.prototype.children = function(c){ + var i; + // Only look one level down + if(typeof c==="string"){ + // We are using a selector + var result = []; + for(i = 0; i < this.length; i++){ + for(var ch = 0; ch < this[i].children.length; ch++){ + if(this.matchSelector(this[i].children[ch],c)) result.push(this[i].children[ch]); + } } - // Update style - this[i].setAttribute('style',newstyle); + return S(result); + }else{ + // We are using an index + for(i = 0; i < this.length; i++) this[i] = (this[i].children.length > c ? this[i].children[c] : this[i]); + return this; } - } - return this; -} -stuQuery.prototype.parent = function(){ - var tmp = []; - for(var i = 0; i < this.length; i++) tmp.push(this[i].parentElement); - return S(tmp); -} -stuQuery.prototype.children = function(c){ - // Only look one level down - if(typeof c==="string"){ - // We are using a selector + }; + stuQuery.prototype.find = function(selector){ var result = []; - for(var i = 0; i < this.length; i++){ - for(var ch = 0; ch < this[i].children.length; ch++){ - if(this.matchSelector(this[i].children[ch],c)) result.push(this[i].children[ch]); + for(var i = 0; i < this.length; i++) result = result.concat(this.querySelector(this[i],selector)); + // Return a new instance of stuQuery + return S(result); + }; + function getset(s,attr,val,typs){ + var tmp = []; + for(var i = 0; i < s.length; i++){ + tmp.push(s[i].getAttribute(attr)); + var ok = false; + for(var j in typs){ if(typeof val===typs[j]) ok = true; } + if(ok){ + if(val) s[i].setAttribute(attr,val); + else s[i].removeAttribute(attr); } } - return S(result); - }else{ - // We are using an index - for(var i = 0; i < this.length; i++) this[i] = (this[i].children.length > c ? this[i].children[c] : this[i]); - return this; + if(tmp.length==1) tmp = tmp[0]; + if(typeof val==="undefined") return tmp; + else return s; } -} -stuQuery.prototype.find = function(selector){ - var tmp = []; - var result = new Array(); - for(var i = 0; i < this.length; i++) result = result.concat(this.querySelector(this[i],selector)); - // Return a new instance of stuQuery - return S(result); -} -function getset(s,attr,val,typs){ - var tmp = []; - for(var i = 0; i < s.length; i++){ - tmp.push(s[i].getAttribute(attr)); - var ok = false; - for(var j in typs){ if(typeof val===typs[j]) ok = true; } - if(ok){ - if(val) s[i].setAttribute(attr,val) - else s[i].removeAttribute(attr); + stuQuery.prototype.attr = function(attr,val){ + return getset(this,attr,val,["string","number"]); + }; + stuQuery.prototype.prop = function(attr,val){ + return getset(this,attr,val,["boolean"]); + }; + stuQuery.prototype.clone = function(){ + var span = document.createElement('div'); + span.appendChild(this[0].cloneNode(true)); + return span.innerHTML; + }; + stuQuery.prototype.replaceWith = function(html){ + var tempDiv; + var clone = S(this.e); + for(var i = 0; i < this.length; i++){ + tempDiv = document.createElement('div'); + tempDiv.innerHTML = html; + clone[i] = tempDiv.cloneNode(true); + this[i].parentNode.replaceChild(clone[i], this[i]); } - } - if(tmp.length==1) tmp = tmp[0]; - if(typeof val==="undefined") return tmp; - else return s; -} -stuQuery.prototype.attr = function(attr,val){ - return getset(this,attr,val,["string","number"]); -} -stuQuery.prototype.prop = function(attr,val){ - return getset(this,attr,val,["boolean"]); -} -stuQuery.prototype.clone = function(){ - var span = document.createElement('div'); - span.appendChild(this[0].cloneNode(true)); - return span.innerHTML; -} -stuQuery.prototype.replaceWith = function(html){ - var span = document.createElement("span"); - span.innerHTML = html; - var clone = S(this.e); - for(var i = 0; i < this.length; i++) clone[0].parentNode.replaceChild(span, clone[0]); - return clone; -} -stuQuery.prototype.width = function(){ - if(this.length > 1) return; - return this[0].offsetWidth; -} -stuQuery.prototype.height = function(){ - if(this.length > 1) return; - return this[0].offsetHeight; -} -stuQuery.prototype.outerWidth = function(){ - if(this.length > 1) return; - var s = getComputedStyle(this[0]); - return this[0].offsetWidth + parseInt(s.marginLeft) + parseInt(s.marginRight); -} -stuQuery.prototype.outerHeight = function(){ - if(this.length > 1) return; - var s = getComputedStyle(this[0]); - return this[0].offsetHeight + parseInt(s.marginTop) + parseInt(s.marginBottom); -} -stuQuery.prototype.offset = function(){ - var rect = this[0].getBoundingClientRect(); + return clone; + }; + stuQuery.prototype.width = function(){ + if(this.length > 1) return; + return this[0].offsetWidth; + }; + stuQuery.prototype.height = function(){ + if(this.length > 1) return; + return this[0].offsetHeight; + }; + stuQuery.prototype.outerWidth = function(){ + if(this.length > 1) return; + var s = getComputedStyle(this[0]); + return this[0].offsetWidth + parseInt(s.marginLeft) + parseInt(s.marginRight); + }; + stuQuery.prototype.outerHeight = function(){ + if(this.length > 1) return; + var s = getComputedStyle(this[0]); + return this[0].offsetHeight + parseInt(s.marginTop) + parseInt(s.marginBottom); + }; + stuQuery.prototype.offset = function(){ + var rect = this[0].getBoundingClientRect(); - return { - top: rect.top + document.body.scrollTop, - left: rect.left + document.body.scrollLeft - } -} -stuQuery.prototype.position = function(){ - if(this.length > 1) return; - return {left: this[0].offsetLeft, top: this[0].offsetTop}; -} -stuQuery.prototype.ajax = function(url,attrs){ - //========================================================= - // ajax(url,{'complete':function,'error':function,'dataType':'json'}) - // complete: function - a function executed on completion - // error: function - a function executed on an error - // cache: break the cache - // dataType: json - will convert the text to JSON - // jsonp - will add a callback function and convert the results to JSON + return { + top: rect.top + document.body.scrollTop, + left: rect.left + document.body.scrollLeft + }; + }; + stuQuery.prototype.position = function(){ + if(this.length > 1) return; + return {left: this[0].offsetLeft, top: this[0].offsetTop}; + }; + stuQuery.prototype.ajax = function(url,attrs){ + //========================================================= + // ajax(url,{'complete':function,'error':function,'dataType':'json'}) + // complete: function - a function executed on completion + // error: function - a function executed on an error + // cache: break the cache + // dataType: json - will convert the text to JSON + // jsonp - will add a callback function and convert the results to JSON - if(typeof url!=="string") return false; - if(!attrs) attrs = {}; - var cb = "",qs = ""; - var oReq; - // If part of the URL is query string we split that first - if(url.indexOf("?") > 0){ - urlbits = url.split("?"); - if(urlbits.length){ - url = urlbits[0]; - qs = urlbits[1]; + if(typeof url!=="string") return false; + if(!attrs) attrs = {}; + var cb = "",qs = ""; + var oReq,urlbits; + // If part of the URL is query string we split that first + if(url.indexOf("?") > 0){ + urlbits = url.split("?"); + if(urlbits.length){ + url = urlbits[0]; + qs = urlbits[1]; + } } - } - if(attrs['dataType']=="jsonp"){ - cb = 'fn_'+(new Date()).getTime(); - window[cb] = function(rsp){ - if(typeof attrs.success==="function") attrs.success.call((attrs['this'] ? attrs['this'] : this), rsp, attrs); - }; - } - if(typeof attrs.cache==="boolean" && !attrs.cache) qs += (qs ? '&':'')+(new Date()).valueOf(); - if(cb) qs += (qs ? '&':'')+'callback='+cb; - if(attrs.data) qs += (qs ? '&':'')+attrs.data; + if(attrs.dataType=="jsonp"){ + cb = 'fn_'+(new Date()).getTime(); + window[cb] = function(rsp){ + if(typeof attrs.success==="function") attrs.success.call((attrs['this'] ? attrs['this'] : this), rsp, attrs); + }; + } + if(typeof attrs.cache==="boolean" && !attrs.cache) qs += (qs ? '&':'')+(new Date()).valueOf(); + if(cb) qs += (qs ? '&':'')+'callback='+cb; + if(attrs.data) qs += (qs ? '&':'')+attrs.data; - // Build the URL to query - attrs['url'] = url+(qs ? '?'+qs:''); + // Build the URL to query + if(attrs.method=="POST") attrs.url = url; + else attrs.url = url+(qs ? '?'+qs:''); - if(attrs['dataType']=="jsonp"){ - var script = document.createElement('script'); - script.src = attrs['url']; - document.body.appendChild(script); - return this; - } + if(attrs.dataType=="jsonp"){ + var script = document.createElement('script'); + script.src = attrs.url; + document.body.appendChild(script); + return this; + } - // code for IE7+/Firefox/Chrome/Opera/Safari or for IE6/IE5 - oReq = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); - oReq.addEventListener("load", window[cb] || complete); - oReq.addEventListener("error", error); - oReq.addEventListener("progress", progress); - if(attrs.beforeSend) oReq = attrs.beforeSend.call((attrs['this'] ? attrs['this'] : this), oReq, attrs); + // code for IE7+/Firefox/Chrome/Opera/Safari or for IE6/IE5 + oReq = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); + oReq.addEventListener("load", window[cb] || complete); + oReq.addEventListener("error", error); + oReq.addEventListener("progress", progress); + var responseTypeAware = 'responseType' in oReq; + if(attrs.beforeSend) oReq = attrs.beforeSend.call((attrs['this'] ? attrs['this'] : this), oReq, attrs); - function complete(evt) { - if(oReq.status === 200) { + function complete(evt) { attrs.header = oReq.getAllResponseHeaders(); - var rsp = oReq.response || oReq.responseText; - // Parse out content in the appropriate callback - if(attrs['dataType']=="json") try { rsp = JSON.parse(rsp.replace(/[\n\r]/g,"\\n").replace(/^([^\(]+)\((.*)\)([^\)]*)$/,function(e,a,b,c){ return (a==cb) ? b:''; }).replace(/\\n/g,"\n")) } catch(e){}; - if(attrs['dataType']=="script"){ - var fileref=document.createElement('script'); - fileref.setAttribute("type","text/javascript"); - fileref.innerHTML = rsp; - document.head.appendChild(fileref); + var rsp; + if(oReq.status == 200 || oReq.status == 201 || oReq.status == 202) { + rsp = oReq.response; + if(oReq.responseType=="" || oReq.responseType=="text") rsp = oReq.responseText; + if(attrs.dataType=="json"){ + try { + if(typeof rsp==="string") rsp = JSON.parse(rsp.replace(/[\n\r]/g,"\\n").replace(/^([^\(]+)\((.*)\)([^\)]*)$/,function(e,a,b,c){ return (a==cb) ? b:''; }).replace(/\\n/g,"\n")); + } catch(e){ error(e); } + } + + // Parse out content in the appropriate callback + if(attrs.dataType=="script"){ + var fileref=document.createElement('script'); + fileref.setAttribute("type","text/javascript"); + fileref.innerHTML = rsp; + document.head.appendChild(fileref); + } + attrs.statusText = 'success'; + if(typeof attrs.success==="function") attrs.success.call((attrs['this'] ? attrs['this'] : this), rsp, attrs); + }else{ + attrs.statusText = 'error'; + error(evt); } - attrs['statusText'] = 'success'; - if(typeof attrs.success==="function") attrs.success.call((attrs['this'] ? attrs['this'] : this), rsp, attrs); - }else{ - attrs['statusText'] = 'error'; - error(evt); + if(typeof attrs.complete==="function") attrs.complete.call((attrs['this'] ? attrs['this'] : this), rsp, attrs); } - if(typeof attrs.complete==="function") attrs.complete.call((attrs['this'] ? attrs['this'] : this), rsp, attrs); - } - function error(evt){ - if(typeof attrs.error==="function") attrs.error.call((attrs['this'] ? attrs['this'] : this),evt,attrs); - } - - function progress(evt){ - if(typeof attrs.progress==="function") attrs.progress.call((attrs['this'] ? attrs['this'] : this),evt,attrs); - } + function error(evt){ + if(typeof attrs.error==="function") attrs.error.call((attrs['this'] ? attrs['this'] : this),evt,attrs); + } + + function progress(evt){ + if(typeof attrs.progress==="function") attrs.progress.call((attrs['this'] ? attrs['this'] : this),evt,attrs); + } - if(attrs['dataType']) oReq.responseType = attrs['dataType']; + if(responseTypeAware && attrs.dataType){ + try { oReq.responseType = attrs.dataType; } + catch(err){ error(err); } + } - try{ oReq.open('GET', attrs['url']); } - catch(err){ error(err); } + try{ oReq.open((attrs.method||'GET'), attrs.url, true); } + catch(err){ error(err); } - try{ oReq.send(); } - catch(err){ error(err); } + if(attrs.method=="POST") oReq.setRequestHeader('Content-type','application/x-www-form-urlencoded'); - return this; -} -stuQuery.prototype.loadJSON = function(url,fn,attrs){ - if(!attrs) attrs = {}; - attrs.dataType = "json"; - attrs.complete = fn; - this.ajax(url,attrs); - return this; -} + try{ oReq.send((attrs.method=="POST" ? qs : null)); } + catch(err){ error(err); } + + return this; + }; + stuQuery.prototype.loadJSON = function(url,fn,attrs){ + if(!attrs) attrs = {}; + attrs.dataType = "json"; + attrs.complete = fn; + this.ajax(url,attrs); + return this; + }; -function S(e) { - return new stuQuery(e); -} + root.stuQuery = stuQuery; + root.S = function(e){ return new stuQuery(e); }; +})(window || this); diff --git a/stuquery.min.js b/stuquery.min.js index ae21358..d100c07 100644 --- a/stuquery.min.js +++ b/stuquery.min.js @@ -1,4 +1,4 @@ -/*! - * stuQuery v1.0.14 - */ -var eventcache={};function stuQuery(t){this.querySelector=function(t,e){var n,r,s,i,o,h,a=new Array;if(e.indexOf(":eq")>=0)for(n=e.split(" "),s=0;s0){var i=n.replace(/(.*)\:eq\(([0-9]+)\)/,"$1 $2").split(" ");n=i[0],r=parseInt(i[1])}if((t="."==n[0]?e.getElementsByClassName(n.substr(1)):"#"==n[0]?e.getElementById(n.substr(1)):e.getElementsByTagName(n))||(t=[]),t.nodeName&&"SELECT"==t.nodeName)s.push(t);else{for("number"!=typeof t.length&&(t=[t]),k=0;k=0&&s.length>0&&(s=r0)for(var n=0;n0)for(;r.childNodes.length>0;)this[e].appendChild(r.childNodes[0]);else this[e].append(t)}return this},stuQuery.prototype.prepend=function(t){if(!t&&1==this.length)return this[0].innerHTML;if(t)for(var e=0;e0)for(var s,i=this,o=0;o=0;t--){if(!this[t])return;"function"==typeof this[t].remove?this[t].remove():"function"==typeof this[t].parentElement.removeChild&&this[t].parentElement.removeChild(this[t])}return this},stuQuery.prototype.hasClass=function(t){for(var e=!0,n=0;nt?this[n].children[t]:this[n];return this},stuQuery.prototype.find=function(t){for(var e=new Array,n=0;n1))return this[0].offsetWidth},stuQuery.prototype.height=function(){if(!(this.length>1))return this[0].offsetHeight},stuQuery.prototype.outerWidth=function(){if(!(this.length>1)){var t=getComputedStyle(this[0]);return this[0].offsetWidth+parseInt(t.marginLeft)+parseInt(t.marginRight)}},stuQuery.prototype.outerHeight=function(){if(!(this.length>1)){var t=getComputedStyle(this[0]);return this[0].offsetHeight+parseInt(t.marginTop)+parseInt(t.marginBottom)}},stuQuery.prototype.offset=function(){var t=this[0].getBoundingClientRect();return{top:t.top+document.body.scrollTop,left:t.left+document.body.scrollLeft}},stuQuery.prototype.position=function(){if(!(this.length>1))return{left:this[0].offsetLeft,top:this[0].offsetTop}},stuQuery.prototype.ajax=function(t,e){if("string"!=typeof t)return!1;e||(e={});var n,r="",s="";if(t.indexOf("?")>0&&(urlbits=t.split("?"),urlbits.length&&(t=urlbits[0],s=urlbits[1])),"jsonp"==e.dataType&&(r="fn_"+(new Date).getTime(),window[r]=function(t){"function"==typeof e.success&&e.success.call(e.this?e.this:this,t,e)}),"boolean"!=typeof e.cache||e.cache||(s+=(s?"&":"")+(new Date).valueOf()),r&&(s+=(s?"&":"")+"callback="+r),e.data&&(s+=(s?"&":"")+e.data),e.url=t+(s?"?"+s:""),"jsonp"==e.dataType){var i=document.createElement("script");return i.src=e.url,document.body.appendChild(i),this}function o(t){"function"==typeof e.error&&e.error.call(e.this?e.this:this,t,e)}(n=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject("Microsoft.XMLHTTP")).addEventListener("load",window[r]||function(t){if(200===n.status){e.header=n.getAllResponseHeaders();var s=n.response||n.responseText;if("json"==e.dataType)try{s=JSON.parse(s.replace(/[\n\r]/g,"\\n").replace(/^([^\(]+)\((.*)\)([^\)]*)$/,function(t,e,n,s){return e==r?n:""}).replace(/\\n/g,"\n"))}catch(t){}if("script"==e.dataType){var i=document.createElement("script");i.setAttribute("type","text/javascript"),i.innerHTML=s,document.head.appendChild(i)}e.statusText="success","function"==typeof e.success&&e.success.call(e.this?e.this:this,s,e)}else e.statusText="error",o(t);"function"==typeof e.complete&&e.complete.call(e.this?e.this:this,s,e)}),n.addEventListener("error",o),n.addEventListener("progress",function(t){"function"==typeof e.progress&&e.progress.call(e.this?e.this:this,t,e)}),e.beforeSend&&(n=e.beforeSend.call(e.this?e.this:this,n,e)),e.dataType&&(n.responseType=e.dataType);try{n.open("GET",e.url)}catch(t){o(t)}try{n.send()}catch(t){o(t)}return this},stuQuery.prototype.loadJSON=function(t,e,n){return n||(n={}),n.dataType="json",n.complete=e,this.ajax(t,n),this}; \ No newline at end of file +/*! + * stuQuery + */ +(function(b){var f={};function d(h){this.stuquery="1.0.26";this.getBy=function(q,p){var o,l,n;o=-1;var j=[];if(p.indexOf(":eq")>0){l=p.replace(/(.*)\:eq\(([0-9]+)\)/,"$1 $2").split(" ");p=l[0];o=parseInt(l[1])}if(p[0]=="."){h=q.getElementsByClassName(p.substr(1))}else{if(p[0]=="#"){h=q.getElementById(p.substr(1))}else{h=q.getElementsByTagName(p)}}if(!h){h=[]}if(h.nodeName&&h.nodeName=="SELECT"){j.push(h)}else{if(typeof h.length!=="number"){h=[h]}for(n=0;n=0&&j.length>0){if(o=0){q=h.split(" ");for(p=0;p0){while(l.childNodes.length>0){this[h].appendChild(l.childNodes[0])}}else{this[h].append(j)}}}return this};d.prototype.prepend=function(l){var k,h,n,m;if(!l&&this.length==1){return this[0].innerHTML}for(k=0;k=0;h--){this[k].insertBefore(m[h],this[k].firstChild)}}return this};d.prototype.before=function(l){var k,n,m,h;for(k=0;k0){for(var j=0;j0){var p=this;var n;for(var l=0;l=0;h--){if(!this[h]){return}if(typeof this[h].remove==="function"){this[h].remove()}else{if(typeof this[h].parentElement.removeChild==="function"){this[h].parentElement.removeChild(this[h])}}}return this};d.prototype.hasClass=function(j){var h=true;for(var k=0;kl?this[j].children[l]:this[j])}return this}};d.prototype.find=function(j){var h=[];for(var k=0;k1){return}return this[0].offsetWidth};d.prototype.height=function(){if(this.length>1){return}return this[0].offsetHeight};d.prototype.outerWidth=function(){if(this.length>1){return}var h=getComputedStyle(this[0]);return this[0].offsetWidth+parseInt(h.marginLeft)+parseInt(h.marginRight)};d.prototype.outerHeight=function(){if(this.length>1){return}var h=getComputedStyle(this[0]);return this[0].offsetHeight+parseInt(h.marginTop)+parseInt(h.marginBottom)};d.prototype.offset=function(){var h=this[0].getBoundingClientRect();return{top:h.top+document.body.scrollTop,left:h.left+document.body.scrollLeft}};d.prototype.position=function(){if(this.length>1){return}return{left:this[0].offsetLeft,top:this[0].offsetTop}};d.prototype.ajax=function(i,s){if(typeof i!=="string"){return false}if(!s){s={}}var l="",o="";var q,r;if(i.indexOf("?")>0){r=i.split("?");if(r.length){i=r[0];o=r[1]}}if(s.dataType=="jsonp"){l="fn_"+(new Date()).getTime();window[l]=function(t){if(typeof s.success==="function"){s.success.call((s["this"]?s["this"]:this),t,s)}}}if(typeof s.cache==="boolean"&&!s.cache){o+=(o?"&":"")+(new Date()).valueOf()}if(l){o+=(o?"&":"")+"callback="+l}if(s.data){o+=(o?"&":"")+s.data}if(s.method=="POST"){s.url=i}else{s.url=i+(o?"?"+o:"")}if(s.dataType=="jsonp"){var p=document.createElement("script");p.src=s.url;document.body.appendChild(p);return this}q=(window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");q.addEventListener("load",window[l]||j);q.addEventListener("error",n);q.addEventListener("progress",h);var k="responseType" in q;if(s.beforeSend){q=s.beforeSend.call((s["this"]?s["this"]:this),q,s)}function j(t){s.header=q.getAllResponseHeaders();var u;if(q.status==200||q.status==201||q.status==202){u=q.response;if(q.responseType==""||q.responseType=="text"){u=q.responseText}if(s.dataType=="json"){try{if(typeof u==="string"){u=JSON.parse(u.replace(/[\n\r]/g,"\\n").replace(/^([^\(]+)\((.*)\)([^\)]*)$/,function(z,y,x,A){return(y==l)?x:""}).replace(/\\n/g,"\n"))}}catch(w){n(w)}}if(s.dataType=="script"){var v=document.createElement("script");v.setAttribute("type","text/javascript");v.innerHTML=u;document.head.appendChild(v)}s.statusText="success";if(typeof s.success==="function"){s.success.call((s["this"]?s["this"]:this),u,s)}}else{s.statusText="error";n(t)}if(typeof s.complete==="function"){s.complete.call((s["this"]?s["this"]:this),u,s)}}function n(t){if(typeof s.error==="function"){s.error.call((s["this"]?s["this"]:this),t,s)}}function h(t){if(typeof s.progress==="function"){s.progress.call((s["this"]?s["this"]:this),t,s)}}if(k&&s.dataType){try{q.responseType=s.dataType}catch(m){n(m)}}try{q.open((s.method||"GET"),s.url,true)}catch(m){n(m)}if(s.method=="POST"){q.setRequestHeader("Content-type","application/x-www-form-urlencoded")}try{q.send((s.method=="POST"?o:null))}catch(m){n(m)}return this};d.prototype.loadJSON=function(i,j,h){if(!h){h={}}h.dataType="json";h.complete=j;this.ajax(i,h);return this};b.stuQuery=d;b.S=function(h){return new d(h)}})(window||this); \ No newline at end of file