File modules/cms/ui/themes/default/script/Oquery.min.js
Last commit: Tue Mar 1 23:03:32 2022 +0100 dankert New: OQuery support for 'parent(selector)' and 'parents(selector)'
1 let query = function (selector ) { 2 if ( typeof selector === 'string' ) 3 return query.createQuery( document.querySelectorAll(selector) ); 4 else if ( selector instanceof HTMLElement ) 5 return query.createQuery([selector] ); 6 else if ( selector instanceof OQuery ) 7 return selector; 8 else 9 return query.createQuery( [] ); 10 } 11 query.createQuery = function(nodeList ) { 12 return new OQuery( nodeList ); 13 } 14 query.create = function(tagName ) { 15 return query.createQuery( [document.createElement( tagName )] ); 16 }; 17 query.id = function(id ) { 18 let el = document.getElementById( id ); 19 if ( el ) 20 return query.createQuery( [el] ); 21 else 22 return query.createQuery( [] ); 23 }; 24 query.one = function(selector ) { 25 return query.createQuery( [document.querySelector( selector )] ); 26 }; 27 query.extend = function() { 28 for(let i=1; i<arguments.length; i++) 29 for(let key in arguments[i]) 30 if(arguments[i].hasOwnProperty(key)) 31 arguments[0][key] = arguments[i][key]; 32 return arguments[0]; 33 } 34 export default query; 35 export class OQuery { 36 static fn = OQuery.prototype; 37 createNew(nodeList) { 38 return new OQuery(nodeList) 39 }; 40 constructor( nodeList ) { 41 this.nodes = Array.isArray(nodeList) ? nodeList : Array.from(nodeList) 42 } 43 get( idx ) { 44 return this.nodes[idx]; 45 } 46 first() { 47 return this.createNew( this.nodes.length > 0 ? [this.nodes[0]] : [] ); 48 }; 49 get length() { 50 return this.nodes.length; 51 } 52 parent( selector = null ) { 53 return this.createNew( 54 this.nodes.map(node => node.parentElement ) 55 .filter( node => !!node ) 56 .filter( node => !selector || node.matches(selector) ) 57 ); 58 }; 59 parents( selector = null ) { 60 let parents = []; 61 for( let node of this.nodes ) 62 while (node) { 63 node = node.parentElement; 64 if ( node && (!selector || node.matches(selector)) ) 65 parents.unshift(node); 66 } 67 return this.createNew( parents ); 68 }; 69 closest( selector ) { 70 return this.createNew( this.nodes.map(node => node.closest( selector ) ).filter( node => node !== null ) ); 71 }; 72 children( selector ) { 73 let result = []; 74 for( let node of this.nodes ) 75 result = result.concat( Array.from(node.children).filter( node => !selector || node.matches(selector) ) ); 76 return this.createNew( result ); 77 }; 78 find(selector) { 79 let result = []; 80 for( let node of this.nodes ) 81 result = result.concat( Array.from(node.querySelectorAll(selector)) ); 82 return this.createNew( result ); 83 }; 84 text( value ) { 85 if ( typeof value !== 'undefined' ) { 86 this.nodes.forEach(node => node.textContent = value ); 87 return this; 88 } 89 else { 90 return this.nodes[0].textContent; 91 } 92 }; 93 addClass( name ) { 94 this.nodes.forEach(node => node.classList.add( name ) ); 95 return this; 96 }; 97 removeClass ( name ) { 98 this.nodes.forEach( 99 node => node.classList.remove( name ) 100 ); 101 return this; 102 }; 103 hasClass( name ) { 104 for( let node of this.nodes ) 105 if ( node.classList.contains( name ) ) 106 return true; 107 return false; 108 }; 109 toggleClass( name ) { 110 if ( this.hasClass( name ) ) 111 this.removeClass( name ); 112 else 113 this.addClass( name ); 114 return this; 115 }; 116 remove() { 117 this.nodes.forEach(node => node.remove() ); 118 return this; 119 }; 120 click ( handler ) { 121 this.on( 'click',handler ); 122 return this; 123 }; 124 dblclick ( handler ) { 125 this.on( 'dblclick',handler ); 126 return this; 127 }; 128 mouseover( handler ) { 129 this.on( 'mouseover',handler ); 130 return this; 131 }; 132 keypress( handler ) { 133 this.on( 'keypress',handler ); 134 return this; 135 }; 136 keyup( handler ) { 137 this.on( 'keyup',handler ); 138 return this; 139 }; 140 submit( handler ) { 141 this.on( 'submit',handler ); 142 return this; 143 } 144 change( handler ) { 145 this.on( 'change',handler ) 146 return this; 147 } 148 input( handler ) { 149 this.on( 'input',handler ) 150 return this; 151 } 152 on ( event,handler ) { 153 if ( typeof handler !== 'undefined') 154 this.nodes.forEach(node => node.addEventListener( event,handler.bind(node) ) ); 155 else 156 this.nodes.forEach(node => node.dispatchEvent( new Event(event) ) ); 157 return this; 158 }; 159 each( handler ) { 160 let idx = -1; 161 for( let node of this.nodes ) 162 if ( handler.call(node,idx,node) === false ) 163 break; 164 return this; 165 } 166 toggle( handler ) { 167 let idx = -1; 168 for( let node of this.nodes ) 169 if ( handler.call(node,idx,node) === false ) 170 node.style.display = 'none'; 171 else 172 node.style.display = ''; 173 return this; 174 } 175 hide() { 176 this.nodes.forEach(node => node.style.display = 'none' ); 177 return this; 178 } 179 show() { 180 this.nodes.forEach(node => node.style.display = '' ); 181 return this; 182 } 183 append( el ) { 184 this.nodes.forEach(node => el.nodes.forEach(elnode => node.appendChild(elnode) ) ); 185 return this; 186 } 187 appendTo( el ) { 188 let to = query( el ); 189 to.append( this ) 190 return this; 191 } 192 attr( name,value ) { 193 if ( typeof value === 'undefined' ) 194 return this.nodes.length > 0 ? this.nodes[0].getAttribute(name) : ''; 195 this.nodes.forEach(node => node.setAttribute(name,value) ); 196 return this; 197 } 198 data( name,value) { 199 if ( typeof value === 'undefined' ) 200 if ( typeof name === 'undefined' ) 201 return this.nodes.length > 0 ? this.nodes[0].dataset : {}; 202 else 203 return this.nodes.length > 0 ? this.nodes[0].dataset[name] : null; 204 this.nodes.forEach(node => node.dataset[name] = value ); 205 return this; 206 } 207 html( value ) { 208 if ( typeof value === 'undefined') 209 return this.nodes.length > 0 ? this.nodes[0].innerHTML : ''; 210 this.nodes.forEach(node => node.innerHTML = value ); 211 return this; 212 } 213 val( value = null ) { 214 if ( value !== null ) { 215 this.nodes.forEach(node => node.value = value ); 216 return this; 217 } 218 else 219 return this.nodes.length > 0 ? this.nodes[0].value : ''; 220 } 221 empty() { 222 this.nodes.forEach(node => { 223 while (node.firstChild) { 224 node.removeChild(node.firstChild); 225 } 226 } ); 227 return this; 228 } 229 is( selector ) { 230 for( let node of this.nodes ) 231 if ( node.matches(selector) ) 232 return true; 233 return false; 234 } 235 toArray() { 236 return this.nodes; 237 } 238 eq( index ) { 239 return this.createNew( [ this.nodes[index] ] ); 240 } 241 index() { 242 if ( this.nodes.length == 0 ) 243 return -1; 244 let node = this.nodes[0]; 245 let children = node.parentNode.childNodes; 246 let num = 0; 247 for (let i=0; i<children.length; i++) { 248 if ( children[i] == node) return num; 249 if ( children[i].nodeType==1) num++; 250 } 251 return -1; 252 } 253 }
Downloadmodules/cms/ui/themes/default/script/Oquery.min.js
History Tue, 1 Mar 2022 23:03:32 +0100 dankert New: OQuery support for 'parent(selector)' and 'parents(selector)' Tue, 22 Feb 2022 21:37:08 +0100 dankert Fix: Displaying the QRCode was broken. Now the QR code is styled with CSS :) Sat, 18 Dec 2021 03:47:23 +0100 dankert New: Every ES6-Module should have a minified version for performance reasons. Bad: The Minifier "Jsqueeze" is unable to minify ES6-modules, so we had to implement a simple JS-Minifier which strips out all comments.