JsUtils

JavaScript Utility functions

Verzia zo dňa 16.08.2016. Pozri najnovšiu verziu.

Tento skript by nemal byť nainštalovaný priamo. Je to knižnica pre ďalšie skripty, ktorú by mali používať cez meta príkaz // @require https://update.greasyfork.org/scripts/19117/142003/JsUtils.js

(function(a){a.fn.extend({sortList:function(){return function(){if(0!==this.length){if(!this.is("ul, ol"))throw"Error: sortList can only be used on a ul or ol tag!";var c=arguments,b=!1;if("undefined"!==typeof c[0]){if("boolean"!==typeof c[0])throw"the first argument must be a boolean";b=c[0]}for(var d=[],e=0;e<this.length;e++){var f=a(this.get(e)),g=f.children("li").not(function(b,e){for(b=1;b<c.length;b++){var f=c[b];if(a.trim(a(this).text())===f)return d.push(a(this)),!0}return!1}).get();g.sort(function(b,
d){return a(b).text().toUpperCase().localeCompare(a(d).text().toUpperCase())});a.each(g,function(a,b){f.append(b)});!0===b&&a.each(d,function(b,d){a(this).parent().append(this)})}return this}}.apply(this,arguments)},offOn:function(a,b){this.off(a).on(a,b);return this},sortSelect:function(){if(!this.is("select"))throw"SortSelect can only be used on a select node";var c=[];a.each(this.children(),function(b,d){"string"===typeof a(this).data("sort-ignore")&&c.push(this)});var b=this.children("option").not(function(b){return 0<=
a.inArray(this,c)?!0:!1});b.sort(function(b,d){return a(b).text().toUpperCase().localeCompare(a(d).text().toUpperCase())});for(var d=0;d<b.length;d++)this.append(b[d]);return this},center:function(){this.css("position","absolute");this.css("top",Math.max(0,(a(window).height()-a(this).outerHeight())/2+a(window).scrollTop())+"px");this.css("left",Math.max(0,(a(window).width()-a(this).outerWidth())/2+a(window).scrollLeft())+"px");return this},sortTable:function(c){"undefined"===typeof c&&(c=-1);if(0!==
this.length){if("number"!==typeof c)throw"col must be of type number. Got type"+typeof c+" instead";if(!this.is("table"))throw"Error: sortList can only be used on a table!";var b=a(this).find("tbody").children("tr").get();-1===c?b.sort(function(b,d){return a(b).text().toUpperCase().localeCompare(a(d).text().toUpperCase())}):b.sort(function(b,d){var g=a(b).children("td").eq(c).text().toUpperCase(),h=a(d).children("td").eq(c).text().toUpperCase();return g.localeCompare(h)});for(var d=0;d<b.length;d++)a(this).find("tbody").append(b[d]);
return this}},changetext:function(a){if(0!==this.length)return this.contents().filter(function(){return 3===this.nodeType})[0].nodeValue=a,this},enableButton:function(a){if(0!==this.length){if(!this.is("a, button"))throw"This function may only be used on a button or anchor tag";if("undefined"===typeof a||null===a)throw"The argument passed to this function must be a boolean";!0===a?this.prop("disabled",!1).removeClass("disabled"):this.prop("disabled",!0).addClass("disabled");return this}}});(function(){(function(){var c=
jQuery.fn.hide;a.fn.hide=function(){c.apply(this,arguments);this.is(":hidden")||this.addClass("hide");return this}})();(function(){var c=jQuery.fn.show;a.fn.show=function(){c.apply(this,arguments);var a;a=this.prop("nodeName");var d;a=document.createElement(a);d="getComputedStyle"in window;document.body.appendChild(a);d=(d?window.getComputedStyle(a,""):a.currentStyle).display;document.body.removeChild(a);a=d;this.is(":hidden")&&(this.hasClass("hide")?this.removeClass("hide").css("display",a):this.hasClass("hidden")&&
this.removeClass("hidden").css("display",a),this.is(":hidden")&&this.css("display",a+" !important"));return this}})()})();(function(){(function(){Object.defineProperty(jQuery.expr.filters,"offscreen",{value:function(c){c=a(c);var b=a(window),d=b.scrollTop(),e=b.scrollLeft(),f;f=e+b.width();var b=d+b.height(),g=c.offset();g.right=g.left+c.outerWidth();g.bottom=g.top+c.outerHeight();return f<g.left||e>g.right||b<g.top||d>g.bottom}})})()})();(function(){(function(){var a=jQuery.fn.addClass;jQuery.fn.addClass=
function(){var b=a.apply(this,arguments);jQuery(this).trigger("cssClassChanged");return b}})()})()})(jQuery);
var ObjectUtil=function(){var a=function(){if(null==arguments||0===arguments.length)return!1;for(var a=0;a<arguments.length;a++){var b=arguments[a];if(void 0===b||null===b)return!1}return!0},c=function(a){return a instanceof jQuery||"jquery"in Object(a)?!0:!1},b=function(){if(null==arguments||0===arguments.length)return!1;for(var a=0;a<arguments.length;a++){var b=arguments[a];if(void 0===b||null===b||0===b.length||"string"!==typeof b||0===$.trim(b).length)return!1}return!0};return{isjQuery:c,validString:function(){return b.apply(this,
arguments)},extendObj:function(a,b){if("function"!==typeof b)throw Error("sub must be a Constructor");if("function"!==typeof a)throw Error("base must be a Constructor");b.prototype=Object.create(a.prototype);return b.prototype.constructor=b},stringContains:function(a,b){return 0>~a.indexOf(b)},getElementFromJqueryArray:function(a,b){if(!c(a))throw new TypeError("element must be an instance of Jquery");if("number"!==typeof b)throw new TypeError("element must be a number");return a.filter(function(a){return a===
b})},deepCompare:function(){function a(b,c){var e;if(isNaN(b)&&isNaN(c)&&"number"===typeof b&&"number"===typeof c||b===c)return!0;if("function"===typeof b&&"function"===typeof c||b instanceof Date&&c instanceof Date||b instanceof RegExp&&c instanceof RegExp||b instanceof String&&c instanceof String||b instanceof Number&&c instanceof Number)return b.toString()===c.toString();if(!(b instanceof Object&&c instanceof Object)||b.isPrototypeOf(c)||c.isPrototypeOf(b)||b.constructor!==c.constructor||b.prototype!==
c.prototype||-1<g.indexOf(b)||-1<h.indexOf(c))return!1;for(e in c)if(c.hasOwnProperty(e)!==b.hasOwnProperty(e)||typeof c[e]!==typeof b[e])return!1;for(e in b){if(c.hasOwnProperty(e)!==b.hasOwnProperty(e)||typeof c[e]!==typeof b[e])return!1;switch(typeof b[e]){case "object":case "function":g.push(b);h.push(c);if(!a(b[e],c[e]))return!1;g.pop();h.pop();break;default:if(b[e]!==c[e])return!1}}return!0}var b,c,g,h;if(1>arguments.length)return!0;b=1;for(c=arguments.length;b<c;b++)if(g=[],h=[],!a(arguments[0],
arguments[b]))return!1;return!0},guid:function(){function a(){return Math.floor(65536*(1+Math.random())).toString(16).substring(1)}return a()+a()+"-"+a()+"-"+a()+"-"+a()+"-"+a()+a()+a()},isValidObject:function(a){return"undefined"!==typeof a&&null!==a&&"object"===typeof a&&0<Object.keys(a).length},clone:function(a,b){return b?$.extend(!0,{},a):$.extend(a,!0)},assertValid:function(){return a.apply(this,arguments)}}}(),LanguageFormatUtils=function(){return{isRtl:function(a){return RegExp("^[^\u0591-\u07ff\u200f\u202b\u202e\ufb1d-\ufdfd\ufe70-\ufefc]*?[\u0591-\u07ff\u200f\u202b\u202e\ufb1d-\ufdfd\ufe70-\ufefc]").test(a)}}}(),
DomUtil=function(){return{injectCss:function(a){(new RegExp(/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/)).test(a)?$("<link>").prop({type:"text/css",rel:"stylesheet"}).attr("href",a).appendTo("head"):$("<style>").prop("type","text/css").html(a).appendTo("head")}}}(),UrlUtils=function(){return{addParameter:function(a,c,b,d){var e;if(0<a.indexOf("#")){var f=a.indexOf("#");e=a.substring(a.indexOf("#"),a.length)}else e="",f=a.length;a=a.substring(0,f).split("?");f="";if(1<a.length)for(var g=
a[1].split("&"),h=0;h<g.length;h++){var k=g[h].split("=");k[0]!=c&&(f=""==f?"?":f+"&",f+=k[0]+"="+(k[1]?k[1]:""))}""==f&&(f="?");d?f="?"+c+"="+b+(1<f.length?"&"+f.substring(1):""):(""!==f&&"?"!=f&&(f+="&"),f+=c+"="+(b?b:""));return a[0]+f+e}}}(),XmlUtil=function(){return{encodeXml:function(a){if("string"===typeof a)return a.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&apos;")},decodeXml:function(a){if("string"===typeof a)return a.replace(/&apos;/g,
"'").replace(/&quot;/g,'"').replace(/&gt;/g,">").replace(/&lt;/g,"<").replace(/&amp;/g,"&")}}}(),XssEncoder=function(a){return a}(XmlUtil||{}),fileUtil=function(){var a=function(){return window.File&&window.FileReader&&window.FileList&&window.Blob?!0:!1},c=function(a){ObjectUtil.isjQuery(a)&&(a=a.val());a=a.substr(a.lastIndexOf(".")+1);if(""===a)throw"No file extension";return a.toLowerCase()},b=function(a,b){if(0==a)return"0 Byte";var c=Math.floor(Math.log(a)/Math.log(1024));return(a/Math.pow(1024,
c)).toPrecision(b+1||3)+" "+"Bytes KB MB GB TB PB EB ZB YB".split(" ")[c]},d=function(){return{getImageInfoFromInput:function(d,f){function g(a,c){var d=new FileReader;d.addEventListener("load",function(){var e=new Image;e.addEventListener("load",function(){var f={};Object.defineProperties(f,{fileName:{value:a.name},imageWidth:{value:e.width},imageHeight:{value:e.height},imageType:{value:a.type},fileSize:{value:b(a.size)},base64:{value:d.result}});Object.freeze(f);c.resolve(f,this)});e.src=h?window.URL.createObjectURL(a):
d.result;h&&window.URL.revokeObjectURL(a)});d.readAsDataURL(a);return c.promise()}if(a()){if(ObjectUtil.isjQuery(d)){if(0===d.length)throw"Input does not exist";d=d[0]}BrowserUtils.isChrome()&&(window.URL=window.webkitURL);var h=window.URL,k=d.files;if(!k)throw new TypeError("File upload not supported by your browser.");if(0<k.length)if(k=k[0],/\.(png|jpg|gif)$/i.test(k.name)){var l=$.Deferred();$.when(g(k,l)).done(function(a,b){f.call(b,a,b)})}else throw new ImageBoundException("Unsupported Image extension: '"+
c(k.name)+"' file name: '"+k.name+"'");}}}}();return{getFileExt:c,getFileNameFromInput:function(b){if(ObjectUtil.isjQuery(b)){if(0===b.length)throw"Input does not exist";b=b[0]}if(null===b)throw"Input does not exist";if("url"===b.getAttribute("type")||!a()){b=b.value;if("string"!==typeof b)throw"path does not contain a value, this maybe because you did not pass in an object that represents an input";return b.replace(/^C:\\fakepath\\/i,"").split("\\").pop().split("/").pop()}return b.files[0].name},
supportsFileApi:a,ImageUtils:d,b64toBlob:function(a,b,c){b=b||"";c=c||512;a=atob(a);for(var d=[],k=0;k<a.length;k+=c){for(var l=a.slice(k,k+c),n=Array(l.length),m=0;m<l.length;m++)n[m]=l.charCodeAt(m);l=new Uint8Array(n);d.push(l)}return new Blob(d,{type:b})},fileToBase64:function(a,b){if(ObjectUtil.isjQuery(a)){if(0===a.length)throw"Input does not exist";a=a[0]}var c=a.files;if(!c)throw new TypeError("File upload not supported by your browser.");var c=c[0],d=new FileReader;d.addEventListener("load",
function(){b.call(d.result,d.result)});d.readAsDataURL(c)}}}(),AjaxUtils=function(){return{preformAjaxCall:function(a,c,b,d){d="undefined"===typeof d?!0:d;if("undefined"===typeof c)console.error("Type must be supplied");else return b="undefined"===typeof b?null:b,a={type:c,url:a,cache:!1,async:d,dataType:"json"},"POST"===c&&(null!==b&&(a.data=b),a.contentType="application/json"),9===IeUtils.getIeVersion()&&"undefined"!==typeof $.support&&($.support.cors=!0),$.ajax(a)}}}(),dragDrop=function(a,c){return{fileUtil:a,
canSupportDragAndDrop:function(){return a.supportsFileApi()}}}(fileUtil||{},window),ArrayUtils=function(){return{removeFromArray:function(a,c){console&&console.warn("The use of 'removeFromArray' is depricated, please use 'yourArray._remove_(item)' instead");for(var b=a.length;b--;)ObjectUtil.isjQuery(c)?$(a[b]).is(c)&&a.splice(b,1):a[b]===c&&a.splice(b,1)},arrayCopy:function(a,c){if(!Array.isArray(a))throw new TypeError("array to copy must be of type 'Array'");return $.extend("undefined"===typeof c?
!1:c,[],a)},validArray:function(a){return Array.isArray(a)&&0<a.length},arraySort:function(a){a.sort(function(a,b){return a.toUpperCase().localeCompare(b.toUpperCase())})},objectArrayIndex:function(a,c){if(-1<a.indexOf(c))return a.indexOf(c);if(-1<$.inArray(c,a))return $.inArray(c,a);for(var b=0,d=a.length;b<d;b++)if(ObjectUtil.deepCompare(a[b],c))return b;return-1}}}(),ColourUtils=function(){var a=Math.random();return{getRandomColour:function(){a+=.618033988749895;var c=a%=1,b=parseInt(6*c),d=6*
c-b,c=.75*(1-.7),e=.75*(1-.7*d),d=.75*(1-.7*(1-d)),f,g,h;0==b?(f=.75,g=d,h=c):1==b?(f=e,g=.75,h=c):2==b?(f=c,g=.75,h=d):3==b?(f=c,g=e,h=.75):4==b?(f=d,g=c,h=.75):5==b&&(f=.75,g=c,h=e);return"rgb("+parseInt(256*f)+","+parseInt(256*g)+","+parseInt(256*h)+")"}}}(),BrowserUtils=function(){var a=function(){var a=function(){var a=window.navigator.userAgent,c=a.indexOf("Edge/");return 0<c?parseInt(a.substring(c+5,a.indexOf(".",c)),10):null};return{getVersion:a,isEdge:function(){return null!==a()}}}();return{detectMobile:function(){var a=
!1,b=navigator.userAgent||navigator.vendor||window.opera;if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(b)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(b.substr(0,
4)))a=!0;return a},isChrome:function(){return null!==window.chrome&&"Google Inc."===window.navigator.vendor?!0:!1},isFireFox:function(){return-1<navigator.userAgent.toLowerCase().indexOf("firefox")?!0:!1},isWebkit:function(){return/webkit/.test(navigator.userAgent.toLowerCase())},isEdgeOrIe:function(){return a.isEdge()||IeUtils.isIe()},Edge:a}}(),IeUtils=function(){var a=function(){return-1!==navigator.appVersion.indexOf("MSIE 10")},c=function(){return!!navigator.userAgent.match(/Trident.*rv[ :]*11\./)},
b=function(){return 0<window.navigator.userAgent.indexOf("MSIE ")|c()||a()?!0:!1};return{isIe:b,getIeVersion:function(){if(!b())return null;var d="unknown",e={"5.5":5.5,"5.6":6,"5.7":7,"5.8":8,9:9},f=(new Function("/*@cc_on return @_jscript_version; @*/"))();"undefined"!==typeof f?(d=e[f],"undefined"===typeof d&&(d=a()?10:c()?11:-1!=navigator.appVersion.indexOf("MSIE 9.")?9:"unknown")):a()?d=10:c()&&(d=11);return d}}}(),VersionUtil=function(){return{isHigherVersion:function(a,c){if("string"!==typeof a&&
"string"!==typeof c)throw"versions must be strings";for(var b=a.split("."),d=c.split("."),e=b.length>d.length?d.length:b.length,f=0;f<e;f++){var g=parseInt(b[f]),h=parseInt(d[f]);if(g>h)return!0;if(h>g)return!1}return b.length>=d.length}}}(),QueryString=function(){for(var a={},c=window.location.search.substring(1).split("&"),b=0;b<c.length;b++){var d=c[b].split("=");"undefined"===typeof a[d[0]]?a[d[0]]=d[1]:"string"===typeof a[d[0]]?a[d[0]]=[a[d[0]],d[1]]:a[d[0]].push(d[1])}return a}();
(function(){function a(a){var c=1;"-"==a[0]&&(c=-1,a=a.substr(1));return function(e,f){if("undefined"===typeof e[a]||"undefined"===typeof f[a])throw"There is no property with the value of "+a.toString()+" inside this object";return e[a].toUpperCase().localeCompare(f[a].toUpperCase())*c}}function c(){if(0==arguments.length)throw"Zero length arguments not allowed for Array.sortBy()";var b=arguments;return function(c,e){for(var f=0,g=0;0==f&&g<b.length;g++)f=a(b[g])(c,e);return f}}Object.defineProperty(Array.prototype,
"sortBy",{enumerable:!1,writable:!0,value:function(){return this.sort(c.apply(null,arguments))},configurable:!0})})();
(function(){Array.prototype._remove_||(Object.defineProperty(Array.prototype,"_remove_",{enumerable:!1,value:function(a){for(var c=0,b=this.length;c<b;c++){var d=this[c];ObjectUtil.isjQuery(a)?$(d).is(a)&&this.splice(c,1):(a===d||ObjectUtil.deepCompare(a,d))&&this.splice(c,1)}},configurable:!0}),Array.prototype.moveItem||Object.defineProperty(Array.prototype,"moveItem",{value:function(a,c){for(;0>a;)a+=this.length;for(;0>c;)c+=this.length;if(c>=this.length)for(var b=c-this.length;b--+1;)this.push(void 0);
this.splice(c,0,this.splice(a,1)[0])},configurable:!0}))})();(function(){Object.defineProperty(Function.prototype,"getName",{value:function(){try{return/^function\s+([\w\$]+)\s*\(/.exec(this.toString())[1]}catch(a){return null}},configurable:!0})})();
var LinkedArray=function(){var a=function b(a){if("undefined"===typeof a)throw new TypeError("a type must be supplied");var e=[];Object.setPrototypeOf(e,b.prototype);Object.defineProperty(e,"_type",{value:a});return e};a.prototype=[];a.prototype.push=function(){for(var a=1===arguments.length?[arguments[0]]:Array.apply(null,arguments),d=0;d<a.length;d++){var e=a[d];if(Array.isArray(this._type)){if(!Array.isArray(e))throw new TypeError("this array only accepts instance of arrays");}else if("object"===
typeof this._type||"function"===typeof this._type){if("undefined"===typeof e)break;if(!(e instanceof this._type)){a="This array only accepts instance of {supplied object}";if(""!==this._type.name)a="This array only accepts instance of "+this._type.name;else try{a="This array only accepts instance of "+this._type.getName()}catch(f){}throw new TypeError(a);}}else{if("number"===typeof this._type&&"number"!==typeof e)throw new TypeError("this array only accepts "+typeof this._type);if("boolean"===typeof this._type&&
"boolean"!==typeof e)throw new TypeError("this array only accepts "+typeof this._type);if("string"===typeof this._type&&"string"!==typeof e)throw new TypeError("this array only accepts "+typeof this._type);}}a.forEach(function(a,b,d){Array.prototype.push.call(this,a)},this)};a.prototype.getType=function(){return"function"===typeof this._type?new this._type:"object"===typeof this._type?this._type:typeof this._type};return a}();
(function(){Object.defineProperties(String.prototype,{replaceAll:{value:function(a,c){return this.split(a).join(c)},configurable:!0}})})();var ImageBoundException=function(){function a(a){this.name="ImageBoundException";this.message=a||"An error occured with this image";this.stack=Error().stack}a.prototype=Object.create(Error.prototype);return a.prototype.constructor=a}();