openrat-cms

OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs | README

Oquery.js (5797B)


      1 /*! OQuery */
      2 /**
      3  * OQuery is a very light ES6-ready replacement for JQuery
      4  *
      5  */
      6 let query = function (selector ) {
      7 
      8 	if   ( typeof selector === 'string' )
      9 		return query.createQuery( document.querySelectorAll(selector) );
     10 	else if ( selector instanceof HTMLElement )
     11 		return query.createQuery([selector] );
     12 	else if ( selector instanceof OQuery )
     13 		return selector;
     14 	else
     15 		//console.warn( new Error("Illegal argument '"+selector+"' of type "+(typeof selector)) );
     16 		return query.createQuery( [] );
     17 
     18 }
     19 
     20 query.createQuery = function(nodeList ) {
     21 	return new OQuery( nodeList );
     22 }
     23 
     24 query.create = function(tagName ) {
     25 	return query.createQuery( [document.createElement( tagName )] );
     26 };
     27 
     28 query.id = function(id ) {
     29 	let el = document.getElementById( id );
     30 	if   ( el )
     31 		return query.createQuery( [el] );
     32 	else
     33 		return query.createQuery( [] );
     34 };
     35 
     36 query.one = function(selector ) {
     37 	return query.createQuery( [document.querySelector( selector )] );
     38 };
     39 
     40 query.extend = function() {
     41 	for(let i=1; i<arguments.length; i++)
     42 		for(let key in arguments[i])
     43 			if(arguments[i].hasOwnProperty(key))
     44 				arguments[0][key] = arguments[i][key];
     45 	return arguments[0];
     46 }
     47 
     48 export default query;
     49 
     50 
     51 export class OQuery {
     52 
     53 	static fn = OQuery.prototype;
     54 	
     55 	createNew(nodeList) {
     56 		return new OQuery(nodeList)
     57 	};
     58 
     59 	constructor( nodeList ) {
     60 
     61 		this.nodes = Array.isArray(nodeList) ? nodeList : Array.from(nodeList)
     62 	}
     63 
     64 	get( idx ) {
     65 		return this.nodes[idx];
     66 	}
     67 	first() {
     68 		return this.createNew( this.nodes.length > 0 ? [this.nodes[0]] : [] );
     69 	};
     70 
     71 
     72 	parent() {
     73 		return this.createNew( this.nodes.map(node => node.parentNode ).filter( node => node !== null ) );
     74 	};
     75 
     76 	closest( selector ) {
     77 		return this.createNew( this.nodes.map(node => node.closest( selector ) ).filter( node => node !== null ) );
     78 	};
     79 
     80 	children( selector ) {
     81 		let result = [];
     82 		for( let node of this.nodes )
     83 			result = result.concat( Array.from(node.children).filter( node => selector ? node.matches(selector) : true ) );
     84 		return this.createNew( result );
     85 	};
     86 
     87 	find(selector) {
     88 		let result = [];
     89 		for( let node of this.nodes )
     90 			result = result.concat( Array.from(node.querySelectorAll(selector)) );
     91 		return this.createNew( result );
     92 	};
     93 
     94 	text( value ) {
     95 
     96 		if   ( typeof value !== 'undefined'  ) {
     97 			this.nodes.forEach(node => node.textContent = value );
     98 			return this;
     99 		}
    100 		else {
    101 			return this.nodes[0].textContent;
    102 		}
    103 	};
    104 
    105 	addClass( name ) {
    106 		this.nodes.forEach(node => node.classList.add( name ) );
    107 		return this;
    108 	};
    109 
    110 	removeClass ( name )  {
    111 
    112 		this.nodes.forEach(
    113 			node => node.classList.remove( name )
    114 		);
    115 		return this;
    116 	};
    117 
    118 	hasClass( name ) {
    119 		for( let node of this.nodes )
    120 			if  ( node.classList.contains( name ) )
    121 				return true;
    122 
    123 		return false;
    124 	};
    125 
    126 	toggleClass( name ) {
    127 		if   ( this.hasClass( name ) )
    128 			this.removeClass( name );
    129 		else
    130 			this.addClass( name );
    131 
    132 		return this;
    133 	};
    134 
    135 
    136 	remove() {
    137 		this.nodes.forEach(node => node.remove() );
    138 		return this;
    139 	};
    140 
    141 
    142 
    143 	click ( handler ) {
    144 		this.on( 'click',handler );
    145 		return this;
    146 	};
    147 	dblclick ( handler ) {
    148 		this.on( 'dblclick',handler );
    149 		return this;
    150 	};
    151 	mouseover( handler ) {
    152 		this.on( 'mouseover',handler );
    153 		return this;
    154 	};
    155 	keypress( handler ) {
    156 		this.on( 'keypress',handler );
    157 		return this;
    158 	};
    159 	keyup( handler ) {
    160 		this.on( 'keyup',handler );
    161 		return this;
    162 	};
    163 	submit( handler ) {
    164 		this.on( 'submit',handler );
    165 		return this;
    166 	}
    167 	change( handler ) {
    168 		this.on( 'change',handler )
    169 		return this;
    170 	}
    171 	input( handler ) {
    172 		this.on( 'input',handler )
    173 		return this;
    174 	}
    175 
    176 
    177 
    178 	on ( event,handler ) {
    179 		if   ( typeof handler !== 'undefined')
    180 			this.nodes.forEach(node => node.addEventListener( event,handler.bind(node) ) );
    181 		else
    182 			this.nodes.forEach(node => node.dispatchEvent( new Event(event) ) );
    183 		return this;
    184 	};
    185 
    186 	each( handler ) {
    187 		let idx = -1;
    188 		for( let node of this.nodes )
    189 			if   ( handler.call(node,idx,node) === false )
    190 				break;
    191 
    192 		return this;
    193 	}
    194 
    195 	hide() {
    196 		this.nodes.forEach(node => node.style.display = 'none' );
    197 		return this;
    198 	}
    199 
    200 	show() {
    201 		this.nodes.forEach(node => node.style.display = '' );
    202 		return this;
    203 	}
    204 
    205 	append( el ) {
    206 		this.nodes.forEach(node => el.nodes.forEach(elnode => node.appendChild(elnode) ) );
    207 		return this;
    208 	}
    209 
    210 	appendTo( el ) {
    211 		let to = query( el );
    212 		to.append( this )
    213 		return this;
    214 	}
    215 
    216 	attr( name,value ) {
    217 		if   ( typeof value === 'undefined' )
    218 			return this.nodes.length > 0 ? this.nodes[0].getAttribute(name) : '';
    219 
    220 		this.nodes.forEach(node => node.setAttribute(name,value) );
    221 		return this;
    222 	}
    223 
    224 	data( name,value) {
    225 		if   ( typeof value === 'undefined' )
    226 			return this.nodes.length > 0 ? this.nodes[0].dataset[name] : '';
    227 
    228 		this.nodes.forEach(node => node.dataset[name] = value );
    229 		return this;
    230 
    231 	}
    232 
    233 	html( value ) {
    234 		if   ( typeof value === 'undefined')
    235 			return this.nodes.length > 0 ? this.nodes[0].innerHTML : '';
    236 
    237 		this.nodes.forEach(node => node.innerHTML = value );
    238 		return this;
    239 	}
    240 
    241 	val( value = null ) {
    242 		if   ( value !== null ) {
    243 			this.nodes.forEach(node => node.value = value );
    244 			return this;
    245 		}
    246 		else
    247 			return this.nodes.length > 0 ? this.nodes[0].value : '';
    248 	}
    249 
    250 	empty() {
    251 		this.nodes.forEach(node => {
    252 			while (node.firstChild) {
    253 				node.removeChild(node.firstChild);
    254 			}
    255 		} );
    256 
    257 		return this;
    258 	}
    259 
    260 	is( selector ) {
    261 		for( let node of this.nodes )
    262 			if   ( node.matches(selector) )
    263 				return true;
    264 
    265 		return false;
    266 	}
    267 
    268 	toArray() {
    269 		return this.nodes;
    270 	}
    271 
    272 	eq( index ) {
    273 		return this.createNew( [ this.nodes[index] ] );
    274 	}
    275 
    276 	index() {
    277 		if   ( this.nodes.length == 0 )
    278 			return -1;
    279 
    280 		let node = this.nodes[0];
    281 
    282 		let children = node.parentNode.childNodes;
    283 		let num = 0;
    284 		for (let i=0; i<children.length; i++) {
    285 			if ( children[i] == node) return num;
    286 			if ( children[i].nodeType==1) num++;
    287 		}
    288 		return -1;
    289 	}
    290 
    291 }