vanilla-lib

Vanilla JS library

Od 15.06.2018.. Pogledajte najnovija verzija.

Ovu skriptu ne treba izravno instalirati. To je biblioteka za druge skripte koje se uključuju u meta direktivu // @require https://updategf.qytechs.cn/scripts/369430/605945/vanilla-lib.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

/* vanilla-lib */
// @require      https://greasyfork.org/scripts/369430/code/vanilla-lib.js?version=605814
// @require      https://greasyfork.org/scripts/369430/code/vanilla-lib.js?version=605944
function ImportVanillaLib( scope ) {
	if ( null === scope || 'undefined' === typeof scope ) {
		return  false;
	}

	scope.mapFlat = ( array,func ) => array.map( x => func(x) ).reduce( (a,b) => a.concat(b) );
	scope.parenth = ( elem,nth ) => traverse(elem, scope.ifndef(nth, 1), 0);
	scope.ifndef  = ( expr,value ) => ( scope.ndef(expr) ? value : expr );
	scope.ispojo  = expr => scope.isobj(expr, Object);
	scope.export  = ( source,target,members ) => ( Object.keys(source).filter( key => ! members || members.includes(key) )
	                                                                  .forEach( key => target[ key ] = source[ key ] ) );
	scope.ifnan   = ( expr,value ) => ( isNaN(expr) ? value : expr );
	scope.isobj   = ( expr,type ) => ( 'object' === typeof expr && ( ! type || scope.isfn(expr.constructor)
	                                       && type === ( scope.isfn(type) ? expr.constructor : expr.constructor.name ) ) );
	scope.isarr   = expr => scope.isobj(expr, Array);
	scope.isstr   = expr => ( 'string' === typeof expr );
	scope.isfn    = expr => ( 'function' === typeof expr );
	scope.ndef    = expr => ( 'undefined' === typeof expr );
	scope.test    = ( expr,func,other ) => ( !! expr ? func(expr) : scope.isfn(other) ? other(expr) : other );
	scope.fire    = ( elem,event,args ) => elem.dispatchEvent( scope.isobj(event) ? even
	                                     : new Event( event, scope.ifndef(args, { 'bubbles':true, 'cancelable':true }) ) );
	scope.warn    = console.warn;
	scope.log     = console.debug;
	scope.on      = ( elem,event,func ) => elem.addEventListener(event, func);
	scope.$$      = ( sel,elem ) => Array.slice((elem || document).querySelectorAll(sel));
	scope.$       = ( sel,elem ) => (elem || document).querySelector(sel);
	
	scope.aggRate = ( amount,rate,periods ) => ( ! periods ? amount : scope.aggRate(amount * rate, rate, periods - 1) ),
	scope.toDec   = expr => ( Math.round(parseFloat((expr +'').replace(/\$|,/g, '')) * 100) / 100 );
	
	//Array.prototype.mapFlat = function( func ) { return  mapFlat(this, func); };
	
	
	scope.appendTo = function( element, parent, reference ) {
		if ( !! reference ) {
			parent    = reference.parentNode;
			reference = reference.nextSibling;
		}
	
		if ( !! reference ) {
			return  scope.prependTo(element, parent, reference);
		} else  if ( !! parent ) {
			parent.append(element);
		} else {
			scope.warn('*** appendTo() could not add element: No parent or reference element provided');
		}
	
		return  element;
	};
	
	scope.attr = function( elem, name, value ) {
		if ( scope.isarr(elem) ) {
			return  elem.map( el => scope.attr(el, name, value) );
		}
	
		scope.keysAndValues(name, value, ( n,v ) => ( null === v ? elem.removeAttribute(n) : elem.setAttribute(n, v) ) );
		return  elem;
	};
	
	scope.create = function( html, containerType ) {
		let  container = null,
		     result    = null,
		     attrs, style;
	
		if ( scope.isobj(containerType) ) {
			attrs         = containerType.attrs;
			style         = containerType.style;
			containerType = containerType.container;
		}
	
		containerType = containerType || 'div';
		create[ containerType ] =
		container               = create[ containerType ] || document.createElement(containerType);
		container.innerHTML = html;
		result = Array.slice(container.childNodes)
		         	.map( elem => (elem.remove(), elem) );
	
		if ( !! attrs ) {
			scope.attr(result, attrs);
		}
		if ( !! style ) {
			scope.css(result, style);
		}
	
		if ( 1 == result.length ) {
			result = result[ 0 ];
		}
		return  result;
	};
	
	scope.css = function( element, key, value ) {
		if ( isarr(element) ) {
			return  element.map( el => css(el, key, value) );
		}
	
		keysAndValues(key, value, ( k,v ) => element.style[ k ] = v );
		return  element;
	};
	
	scope.keysAndValues = function( key, value, action ) {
		// Case 1: key is an object (and there is no value)
		if ( scope.isobj(key) && ! value ) {
			return  Object.keys(key)
			        	.map( k => action(k, key[ k ]) );
		// Case 2: key is an array
		} else if ( scope.isarr(key) ) {
			// Case 1.a: value is an array of the same length
			if ( scope.isarr(value) && key.length === value.length ) {
				return  key.map( ( k,i ) => action(k, value[ i ]) );
			// Case 1.b: value is considered a simple, plain value
			} else {
				return  key.map( k => action(k, value) );
			}
		// Default Case: key and value considered as simple, plain values
		} else {
			return  action(key, value);
		}
	};
	
	scope.prependTo = function( element, parent, reference ) {
		if ( ! reference && !! parent ) {
			reference = parent.childNodes[ 0 ];
		}
	
		if ( !! reference ) {
			reference.parentNode.insertBefore(element, reference);
		} else if ( !! parent ) {
			parent.append(element);
		} else {
			scope.warn('*** prependTo() could not add element: No parent or reference element provided');
		}
	
		return  element;
	};
	
	scope.toDec2 = function( amount ) {
		amount = scope.toDec(amount);
		if ( isNaN(amount) ) {
			return  null;
		}
		let  segs = (amount +'').split('.');
		return  segs[ 0 ] +'.'+ ((segs[ 1 ] || 0) +'0').slice(0, 2);
	};
	
	scope.toMoney = function( amount ) {
		let  dec2 = scope.toDec2(amount);
		return  ( isNaN(dec2) ? null : dec2 < 0 ? '-$ '+ (-dec2) : '$ '+ dec2 );
	};
	
	scope.traverse = function( elem, up, sideways, elementsOnly, lastIfNull ) {
		let  last = elem;
		while ( !! elem && up -- > 0 )  elem = (last = elem, elem.parentNode);
	
		let  prop = ( elementsOnly ? 'Element' : '' ) +'Sibling';
		if ( sideways < 0 ) {
			while ( !! elem && sideways ++ < 0 )  elem = (last = elem, elem[ 'previous'+ prop ]);
		} else if ( sideways > 0 ) {
			while ( !! elem && sideways -- > 0 )  elem = (last = elem, elem[ 'next'+ prop ]);
		}
	
		return  ( ! lastIfNull ? elem : elem || last );
	};
}