1/** 2 * ThemeREX Framework: Utilities 3 * 4 * @package themerex 5 * @since themerex 1.0 6 */ 7 8 9/* Global variables manipulations 10---------------------------------------------------------------- */ 11 12// Global variables storage 13if (typeof THEMEREX_GLOBALS == 'undefined') var THEMEREX_GLOBALS = {}; 14 15// Get global variable 16function themerex_get_global(var_name) { 17 return themerex_isset(THEMEREX_GLOBALS[var_name]) ? THEMEREX_GLOBALS[var_name] : ''; 18} 19 20// Set global variable 21function themerex_set_global(var_name, value) { 22 THEMEREX_GLOBALS[var_name] = value; 23} 24 25// Inc/Dec global variable with specified value 26function themerex_inc_global(var_name) { 27 var value = arguments[1]==undefined ? 1 : arguments[1]; 28 THEMEREX_GLOBALS[var_name] += value; 29} 30 31// Concatenate global variable with specified value 32function themerex_concat_global(var_name, value) { 33 THEMEREX_GLOBALS[var_name] += ''+value; 34} 35 36// Get global array element 37function themerex_get_global_array(var_name, key) { 38 return themerex_isset(THEMEREX_GLOBALS[var_name][key]) ? THEMEREX_GLOBALS[var_name][key] : ''; 39} 40 41// Set global array element 42function themerex_set_global_array(var_name, key, value) { 43 if (!themerex_isset(THEMEREX_GLOBALS[var_name])) THEMEREX_GLOBALS[var_name] = {}; 44 THEMEREX_GLOBALS[var_name][key] = value; 45} 46 47// Inc/Dec global array element with specified value 48function themerex_inc_global_array(var_name, key) { 49 var value = arguments[2]==undefined ? 1 : arguments[2]; 50 THEMEREX_GLOBALS[var_name][key] += value; 51} 52 53// Concatenate global array element with specified value 54function themerex_concat_global_array(var_name, key, value) { 55 THEMEREX_GLOBALS[var_name][key] += ''+value; 56} 57 58 59 60/* PHP-style functions 61---------------------------------------------------------------- */ 62function themerex_isset(obj) { 63 return typeof(obj) != 'undefined'; 64} 65 66function themerex_empty(obj) { 67 return typeof(obj) == 'undefined' || (typeof(obj)=='object' && obj == null) || (typeof(obj)=='array' && obj.length == 0) || (typeof(obj)=='string' && themerex_alltrim(obj)=='') || obj===0; 68} 69 70function themerex_is_array(obj) { 71 "use strict"; 72 return typeof(obj)=='array'; 73} 74 75function themerex_is_object(obj) { 76 "use strict"; 77 return typeof(obj)=='object'; 78} 79 80function themerex_clone_object(obj) { 81 if (obj == null || typeof(obj) != 'object') { 82 return obj; 83 } 84 var temp = {}; 85 for (var key in obj) { 86 temp[key] = themerex_clone_object(obj[key]); 87 } 88 return temp; 89} 90 91function themerex_merge_objects(obj1, obj2) { 92 "use strict"; 93 for (var i in obj2) obj1[i] = obj2[i]; 94 return obj1; 95} 96 97 98 99/* String functions 100---------------------------------------------------------------- */ 101 102function themerex_in_list(str, list) { 103 "use strict"; 104 var delim = arguments[2] ? arguments[2] : '|'; 105 var icase = arguments[3] ? arguments[3] : true; 106 var retval = false; 107 if (icase) { 108 if (typeof(str)=='string') str = str.toLowerCase(); 109 list = list.toLowerCase(); 110 } 111 var parts = list.split(delim); 112 for (var i=0; i<parts.length; i++) { 113 if (parts[i]==str) { 114 retval=true; 115 break; 116 } 117 } 118 return retval; 119} 120 121function themerex_alltrim(str) { 122 "use strict"; 123 var dir = arguments[1] ? arguments[1] : 'a'; 124 var rez = ''; 125 var i, start = 0, end = str.length-1; 126 if (dir=='a' || dir=='l') { 127 for (i=0; i<str.length; i++) { 128 if (str.substr(i,1)!=' ') { 129 start = i; 130 break; 131 } 132 } 133 } 134 if (dir=='a' || dir=='r') { 135 for (i=str.length-1; i>=0; i--) { 136 if (str.substr(i,1)!=' ') { 137 end = i; 138 break; 139 } 140 } 141 } 142 return str.substring(start, end+1); 143} 144 145function themerex_ltrim(str) { 146 "use strict"; 147 return themerex_alltrim(str, 'l'); 148} 149 150function themerex_rtrim(str) { 151 "use strict"; 152 return themerex_alltrim(str, 'r'); 153} 154 155function themerex_padl(str, len) { 156 "use strict"; 157 var ch = arguments[2] ? arguments[2] : ' '; 158 var rez = str.substr(0,len); 159 if (rez.length < len) { 160 for (var i=0; i<len-str.length; i++) 161 rez += ch; 162 } 163 return rez; 164} 165 166function themerex_padr(str, len) { 167 "use strict"; 168 var ch = arguments[2] ? arguments[2] : ' '; 169 var rez = str.substr(0,len); 170 if (rez.length < len) { 171 for (var i=0; i<len-str.length; i++) 172 rez = ch + rez; 173 } 174 return rez; 175} 176 177function themerex_padc(str, len) { 178 "use strict"; 179 var ch = arguments[2] ? arguments[2] : ' '; 180 var rez = str.substr(0,len); 181 if (rez.length < len) { 182 for (var i=0; i<Math.floor((len-str.length)/2); i++) 183 rez = ch + rez + ch; 184 } 185 return rez+(rez.length<len ? ch : ''); 186} 187 188function themerex_replicate(str, num) { 189 "use strict"; 190 var rez = ''; 191 for (var i=0; i<num; i++) { 192 rez += str; 193 } 194 return rez; 195} 196 197 198 199/* Numbers functions 200---------------------------------------------------------------- */ 201 202// Round number to specified precision.
203// For example: num=1.12345, prec=2, rounded=1.12 204// num=12345, prec=-2, rounded=12300 205function themerex_round_number(num) { 206 "use strict"; 207 var precision = arguments[1] ? arguments[1] : 0; 208 var p = Math.pow(10, precision); 209 return Math.round(num*p)/p; 210} 211 212// Clear number from any characters and append it with 0 to desired precision 213// For example: num=test1.12dd, prec=3, cleared=1.120 214function themerex_clear_number(num) { 215 "use strict"; 216 var precision = arguments[1] ? arguments[1] : 0; 217 var defa = arguments[2] ? arguments[2] : 0; 218 var res = ''; 219 var decimals = -1; 220 num = ""+num; 221 if (num=="") num=""+defa; 222 for (var i=0; i<num.length; i++) { 223 if (decimals==0) break; 224 else if (decimals>0) decimals--; 225 var ch = num.substr(i,1); 226 if (ch=='.') { 227 if (precision>0) { 228 res += ch; 229 } 230 decimals = precision; 231 } else if ((ch>=0 && ch<=9) || (ch=='-' && i==0)) 232 res+=ch; 233 } 234 if (precision>0 && decimals!=0) { 235 if (decimals==-1) { 236 res += '.'; 237 decimals = precision; 238 } 239 for (i=decimals; i>0; i--) 240 res +='0'; 241 } 242 //if (isNaN(res)) res = clearNumber(defa, precision, defa); 243 return res; 244} 245 246// Convert number from decimal to hex 247function themerex_dec2hex(n) { 248 "use strict"; 249 return Number(n).toString(16); 250} 251 252// Convert number from hex to decimal 253function themerex_hex2dec(hex) { 254 "use strict"; 255 return parseInt(hex,16); 256} 257 258 259 260/* Array manipulations 261---------------------------------------------------------------- */ 262 263function themerex_in_array(val, thearray) { 264 "use strict"; 265 var rez = false; 266 for (var i=0; i<thearray.length-1; i++) { 267 if (thearray[i] == val) { 268 rez = true; 269 break; 270 } 271 } 272 return rez; 273} 274 275function themerex_sort_array(thearray) { 276 "use strict"; 277 var caseSensitive = arguments[1] ? arguments[1] : false; 278 for (var x=0; x<thearray.length-1; x++) { 279 for (var y=(x+1); y<thearray.length; y++) { 280 if (caseSensitive) { 281 if (thearray[x] > thearray[y]) { 282 tmp = thearray[x]; 283 thearray[x] = thearray[y]; 284 thearray[y] = tmp; 285 } 286 } else { 287 if (thearray[x].toLowerCase() > thearray[y].toLowerCase()) { 288 tmp = thearray[x]; 289 thearray[x] = thearray[y]; 290 thearray[y] = tmp; 291 } 292 } 293 } 294 } 295 return thearray; 296} 297 298 299 300/* Date manipulations 301---------------------------------------------------------------- */ 302 303// Return array[Year, Month, Day, Hours, Minutes, Seconds] 304// from string: Year[-/.]Month[-/.]Day[T ]Hours:Minutes:Seconds 305function themerex_parse_date(dt) { 306 "use strict"; 307 dt = dt.replace(/\//g, '-').replace(/\./g, '-').replace(/T/g, ' ').split('+')[0]; 308 var dt2 = dt.split(' '); 309 var d = dt2[0].split('-'); 310 var t = dt2[1].split(':'); 311 d.push(t[0], t[1], t[2]); 312 return d; 313} 314 315// Return difference string between two dates 316function themerex_get_date_difference(dt1) { 317 "use strict"; 318 var dt2 = arguments[1]!==undefined ? arguments[1] : ''; 319 var short_date = arguments[2]!==undefined ? arguments[2] : true; 320 var sec = arguments[3]!==undefined ? arguments[3] : false; 321 var a1 = themerex_parse_date(dt1); 322 dt1 = Date.UTC(a1[0], a1[1], a1[2], a1[3], a1[4], a1[5]); 323 if (dt2 == '') { 324 dt2 = new Date(); 325 var a2 = [dt2.getFullYear(), dt2.getMonth()+1, dt2.getDate(), dt2.getHours(), dt2.getMinutes(), dt2.getSeconds()]; 326 } else 327 var a2 = themerex_parse_date(dt2); 328 dt2 = Date.UTC(a2[0], a2[1], a2[2], a2[3], a2[4], a2[5]); 329 var diff = Math.round((dt2 - dt1)/1000); 330 var days = Math.floor(diff / (24*3600)); 331 diff -= days * 24 * 3600; 332 var hours = Math.floor(diff / 3600); 333 diff -= hours * 3600; 334 var minutes = Math.floor(diff / 60); 335 diff -= minutes * 60; 336 rez = ''; 337 if (days > 0) 338 rez += (rez!='' ? ' ' : '') + days + ' day' + (days > 1 ? 's' : ''); 339 if ((!short_date || rez=='') && hours > 0) 340 rez += (rez!='' ? ' ' : '') + hours + ' hour' + (hours > 1 ? 's' : ''); 341 if ((!short_date || rez=='') && minutes > 0) 342 rez += (rez!='' ? ' ' : '') + minutes + ' minute' + (minutes > 1 ? 's' : ''); 343 if (sec || rez=='') 344 rez += rez!='' || sec ? (' ' + diff + ' second' + (diff > 1 ? 's' : '')) : 'less then minute'; 345 return rez; 346} 347 348 349 350/* Colors functions 351---------------------------------------------------------------- */ 352 353function themerex_hex2rgb(hex) { 354 hex = parseInt(((hex.indexOf('#') > -1) ? hex.substring(1) : hex), 16); 355 return {r: hex >> 16, g: (hex & 0x00FF00) >> 8, b: (hex & 0x0000FF)}; 356} 357 358function themerex_rgb2hex(color) { 359 "use strict"; 360 var aRGB; 361 color = color.replace(/\s/g,"").toLowerCase(); 362 if (color=='rgba(0,0,0,0)' || color=='rgba(0%,0%,0%,0%)') 363 color = 'transparent'; 364 if (color.indexOf('rgba(')==0) 365 aRGB = color.match(/^rgba\((\d{1,3}[%]?),(\d{1,3}[%]?),(\d{1,3}[%]?),(\d{1,3}[%]?)\)$/i); 366 else 367 aRGB = color.match(/^rgb\((\d{1,3}[%]?),(\d{1,3}[%]?),(\d{1,3}[%]?)\)$/i); 368 369 if(aRGB) { 370 color = ''; 371 for (var i=1; i<=3; i++) 372 color += Math.round((aRGB[i][aRGB[i].length-1]=="%"?2.55:1)*parseInt(aRGB[i])).toString(16).replace(/^(.)$/,'0$1'); 373 } else 374 color = color.replace(/^#?([\da-f])([\da-f])([\da-f])$/i, '$1$1$2$2$3$3'); 375 return (color.substr(0,1)!='#' ? '#' : '') + color; 376} 377 378function themerex_components2hex(r,g,b) { 379 "use strict"; 380 return '#'+ 381 Number(r).toString(16).toUpperCase().replace(/^(.)$/,'0$1') + 382 Number(g).toString(16).toUpperCase().replace(/^(.)$/,'0$1') + 383 Number(b).toString(16).toUpperCase().replace(/^(.)$/,'0$1'); 384} 385 386function themerex_rgb2components(color) { 387 "use strict"; 388 color = themerex_rgb2hex(color); 389 var matches = color.match(/^#?([\dabcdef]{2})([\dabcdef]{2})([\dabcdef]{2})$/i); 390 if (!matches) return false;
391 for (var i=1, rgb = new Array(3); i<=3; i++) 392 rgb[i-1] = parseInt(matches[i],16); 393 return rgb; 394} 395 396function themerex_hex2hsb(hex) { 397 "use strict"; 398 return themerex_rgb2hsb(themerex_hex2rgb(hex)); 399} 400 401function themerex_hsb2hex(hsb) { 402 var rgb = themerex_hsb2rgb(hsb); 403 return themerex_components2hex(rgb.r, rgb.g, rgb.b); 404} 405 406function themerex_rgb2hsb(rgb) { 407 "use strict"; 408 var hsb = {}; 409 hsb.b = Math.max(Math.max(rgb.r,rgb.g),rgb.b); 410 hsb.s = (hsb.b <= 0) ? 0 : Math.round(100*(hsb.b - Math.min(Math.min(rgb.r,rgb.g),rgb.b))/hsb.b); 411 hsb.b = Math.round((hsb.b /255)*100); 412 if ((rgb.r==rgb.g) && (rgb.g==rgb.b)) hsb.h = 0; 413 else if (rgb.r>=rgb.g && rgb.g>=rgb.b) hsb.h = 60*(rgb.g-rgb.b)/(rgb.r-rgb.b); 414 else if (rgb.g>=rgb.r && rgb.r>=rgb.b) hsb.h = 60 + 60*(rgb.g-rgb.r)/(rgb.g-rgb.b); 415 else if (rgb.g>=rgb.b && rgb.b>=rgb.r) hsb.h = 120 + 60*(rgb.b-rgb.r)/(rgb.g-rgb.r); 416 else if (rgb.b>=rgb.g && rgb.g>=rgb.r) hsb.h = 180 + 60*(rgb.b-rgb.g)/(rgb.b-rgb.r); 417 else if (rgb.b>=rgb.r && rgb.r>=rgb.g) hsb.h = 240 + 60*(rgb.r-rgb.g)/(rgb.b-rgb.g); 418 else if (rgb.r>=rgb.b && rgb.b>=rgb.g) hsb.h = 300 + 60*(rgb.r-rgb.b)/(rgb.r-rgb.g); 419 else hsb.h = 0; 420 hsb.h = Math.round(hsb.h); 421 return hsb; 422} 423 424function themerex_hsb2rgb(hsb) { 425 var rgb = {}; 426 var h = Math.round(hsb.h); 427 var s = Math.round(hsb.s*255/100); 428 var v = Math.round(hsb.b*255/100); 429 if (s == 0) { 430 rgb.r = rgb.g = rgb.b = v; 431 } else { 432 var t1 = v; 433 var t2 = (255-s)*v/255; 434 var t3 = (t1-t2)*(h%60)/60; 435 if (h==360) h = 0; 436 if (h<60) { rgb.r=t1; rgb.b=t2; rgb.g=t2+t3; } 437 else if (h<120) { rgb.g=t1; rgb.b=t2; rgb.r=t1-t3; } 438 else if (h<180) { rgb.g=t1; rgb.r=t2; rgb.b=t2+t3; } 439 else if (h<240) { rgb.b=t1; rgb.r=t2; rgb.g=t1-t3; } 440 else if (h<300) { rgb.b=t1; rgb.g=t2; rgb.r=t2+t3; } 441 else if (h<360) { rgb.r=t1; rgb.g=t2; rgb.b=t1-t3; } 442 else { rgb.r=0; rgb.g=0; rgb.b=0; } 443 } 444 return { r:Math.round(rgb.r), g:Math.round(rgb.g), b:Math.round(rgb.b) }; 445} 446 447function themerex_color_picker(){ 448 "use strict"; 449 var id = arguments[0] ? arguments[0] : "iColorPicker"+Math.round(Math.random()*1000); 450 var colors = arguments[1] ? arguments[1] : 451 '#f00,#ff0,#0f0,#0ff,#00f,#f0f,#fff,#ebebeb,#e1e1e1,#d7d7d7,#cccccc,#c2c2c2,#b7b7b7,#acacac,#a0a0a0,#959595,' 452 +'#ee1d24,#fff100,#00a650,#00aeef,#2f3192,#ed008c,#898989,#7d7d7d,#707070,#626262,#555,#464646,#363636,#262626,#111,#000,' 453 +'#f7977a,#fbad82,#fdc68c,#fff799,#c6df9c,#a4d49d,#81ca9d,#7bcdc9,#6ccff7,#7ca6d8,#8293ca,#8881be,#a286bd,#bc8cbf,#f49bc1,#f5999d,' 454 +'#f16c4d,#f68e54,#fbaf5a,#fff467,#acd372,#7dc473,#39b778,#16bcb4,#00bff3,#438ccb,#5573b7,#5e5ca7,#855fa8,#a763a9,#ef6ea8,#f16d7e,' 455 +'#ee1d24,#f16522,#f7941d,#fff100,#8fc63d,#37b44a,#00a650,#00a99e,#00aeef,#0072bc,#0054a5,#2f3192,#652c91,#91278f,#ed008c,#ee105a,' 456 +'#9d0a0f,#a1410d,#a36209,#aba000,#588528,#197b30,#007236,#00736a,#0076a4,#004a80,#003370,#1d1363,#450e61,#62055f,#9e005c,#9d0039,' 457 +'#790000,#7b3000,#7c4900,#827a00,#3e6617,#045f20,#005824,#005951,#005b7e,#003562,#002056,#0c004b,#30004a,#4b0048,#7a0045,#7a0026'; 458 var colorsList = colors.split(','); 459 var tbl = '<table class="colorPickerTable"><thead>'; 460 for (var i=0; i<colorsList.length; i++) { 461 if (i%16==0) tbl += (i>0 ? '</tr>' : '') + '<tr>'; 462 tbl += '<td style="background-color:'+colorsList[i]+'"> </td>'; 463 } 464 tbl += '</tr></thead><tbody>' 465 + '<tr style="height:60px;">' 466 + '<td colspan="8" id="'+id+'_colorPreview" style="vertical-align:middle;text-align:center;border:1px solid #000;background:#fff;">' 467 + '<input style="width:55px;color:#000;border:1px solid rgb(0, 0, 0);padding:5px;background-color:#fff;font:11px Arial, Helvetica, sans-serif;" maxlength="7" />' 468 + '<a href="#" id="'+id+'_moreColors" class="iColorPicker_moreColors"></a>' 469 + '</td>' 470 + '<td colspan="8" id="'+id+'_colorOriginal" style="vertical-align:middle;text-align:center;border:1px solid #000;background:#fff;">' 471 + '<input style="width:55px;color:#000;border:1px solid rgb(0, 0, 0);padding:5px;background-color:#fff;font:11px Arial, Helvetica, sans-serif;" readonly="readonly" />' 472 + '</td>' 473 + '</tr></tbody></table>'; 474 //tbl += '<style>#iColorPicker input{margin:2px}</style>'; 475 476 jQuery(document.createElement("div")) 477 .attr("id", id) 478 .css('display','none') 479 .html(tbl) 480 .appendTo("body") 481 .addClass("iColorPickerTable") 482 .on('mouseover', 'thead td', function(){ 483 "use strict"; 484 var aaa = themerex_rgb2hex(jQuery(this).css('background-color')); 485 jQuery('#'+id+'_colorPreview').css('background',aaa); 486 jQuery('#'+id+'_colorPreview input').val(aaa); 487 }) 488 .on('keypress', '#'+id+'_colorPreview input', function(key){ 489 "use strict"; 490 var aaa = jQuery(this).val()
491 if (aaa.length<7 && ((key.which>=48 && key.which<=57) || (key.which>=97 && key.which<=102) || (key.which===35 || aaa.length===0))) { 492 aaa += String.fromCharCode(key.which); 493 } else if (key.which == 8 && aaa.length>0) { 494 aaa = aaa.substring(0, aaa.length-1); 495 } else if (key.which===13 && (aaa.length===4 || aaa.length===7)) { 496 var fld = jQuery('#'+id).data('field'); 497 var func = jQuery('#'+id).data('func'); 498 if (func!=null && func!='undefined') { 499 func(fld, aaa); 500 } else { 501 fld.val(aaa).css('backgroundColor', aaa).trigger('change'); 502 } 503 jQuery('#'+id+'_Bg').fadeOut(500); 504 jQuery('#'+id).fadeOut(500); 505 506 } else { 507 key.preventDefault(); 508 return false; 509 } 510 if (aaa.substr(0,1)==='#' && (aaa.length===4 || aaa.length===7)) { 511 jQuery('#'+id+'_colorPreview').css('background',aaa); 512 } 513 }) 514 .on('click', 'thead td', function(e){ 515 "use strict"; 516 var fld = jQuery('#'+id).data('field'); 517 var func = jQuery('#'+id).data('func'); 518 var aaa = themerex_rgb2hex(jQuery(this).css('background-color')); 519 if (func!=null && func!='undefined') { 520 func(fld, aaa); 521 } else { 522 fld.val(aaa).css('backgroundColor', aaa).trigger('change'); 523 } 524 jQuery('#'+id+'_Bg').fadeOut(500); 525 jQuery('#'+id).fadeOut(500); 526 e.preventDefault(); 527 return false; 528 }) 529 .on('click', 'tbody .iColorPicker_moreColors', function(e){ 530 "use strict"; 531 var thead = jQuery(this).parents('table').find('thead'); 532 var out = ''; 533 if (thead.hasClass('more_colors')) { 534 for (var i=0; i<colorsList.length; i++) { 535 if (i%16==0) out += (i>0 ? '</tr>' : '') + '<tr>'; 536 out += '<td style="background-color:'+colorsList[i]+'"> </td>'; 537 } 538 thead.removeClass('more_colors').empty().html(out+'</tr>'); 539 jQuery('#'+id+'_colorPreview').attr('colspan', 8); 540 jQuery('#'+id+'_colorOriginal').attr('colspan', 8); 541 } else { 542 var rgb=[0,0,0], i=0, j=-1; // Set j=-1 or j=0 - show 2 different colors layouts 543 while (rgb[0]<0xF || rgb[1]<0xF || rgb[2]<0xF) { 544 if (i%18==0) out += (i>0 ? '</tr>' : '') + '<tr>'; 545 i++; 546 out += '<td style="background-color:'+themerex_components2hex(rgb[0]*16+rgb[0],rgb[1]*16+rgb[1],rgb[2]*16+rgb[2])+'"> </td>'; 547 rgb[2]+=3; 548 if (rgb[2]>0xF) { 549 rgb[1]+=3; 550 if (rgb[1]>(j===0 ? 6 : 0xF)) { 551 rgb[0]+=3; 552 if (rgb[0]>0xF) { 553 if (j===0) { 554 j=1; 555 rgb[0]=0; 556 rgb[1]=9; 557 rgb[2]=0; 558 } else { 559 break; 560 } 561 } else { 562 rgb[1]=(j < 1 ? 0 : 9); 563 rgb[2]=0; 564 } 565 } else { 566 rgb[2]=0; 567 } 568 } 569 } 570 thead.addClass('more_colors').empty().html(out+'<td style="background-color:#ffffff" colspan="8"> </td></tr>'); 571 jQuery('#'+id+'_colorPreview').attr('colspan', 9); 572 jQuery('#'+id+'_colorOriginal').attr('colspan', 9); 573 } 574 jQuery('#'+id+' table.colorPickerTable thead td') 575 .css({ 576 'width':'12px', 577 'height':'14px', 578 'border':'1px solid #000', 579 'cursor':'pointer' 580 }); 581 e.preventDefault(); 582 return false; 583 }); 584 jQuery(document.createElement("div")) 585 .attr("id", id+"_Bg") 586 .click(function(e) { 587 "use strict"; 588 jQuery("#"+id+"_Bg").fadeOut(500); 589 jQuery("#"+id).fadeOut(500); 590 e.preventDefault(); 591 return false; 592 }) 593 .appendTo("body"); 594 jQuery('#'+id+' table.colorPickerTable thead td') 595 .css({ 596 'width':'12px', 597 'height':'14px', 598 'border':'1px solid #000', 599 'cursor':'pointer' 600 }); 601 jQuery('#'+id+' table.colorPickerTable') 602 .css({'border-collapse':'collapse'}); 603 jQuery('#'+id) 604 .css({ 605 'border':'1px solid #ccc', 606 'background':'#333', 607 'padding':'5px', 608 'color':'#fff', 609 'z-index':999999 610 }); 611 jQuery('#'+id+'_colorPreview') 612 .css({'height':'50px'}); 613 return id; 614} 615 616function themerex_color_picker_show(id, fld, func) { 617 "use strict"; 618 if (id===null || id==='') { 619 id = jQuery('.iColorPickerTable').attr('id'); 620 } 621 var eICP = fld.offset(); 622 var w = jQuery('#'+id).width(); 623 var h = jQuery('#'+id).height(); 624 var l = eICP.left + w < jQuery(window).width()-10 ? eICP.left : jQuery(window).width()-10 - w; 625 var t = eICP.top + fld.outerHeight() + h < jQuery(document).scrollTop() + jQuery(window).height()-10 ? eICP.top + fld.outerHeight() : eICP.top - h - 13; 626 jQuery("#"+id) 627 .data({field: fld, func: func}) 628 .css({ 629 'top':t+"px", 630 'left':l+"px", 631 'position':'absolute', 632 'z-index':100001 633 }) 634 .fadeIn(500); 635 jQuery("#"+id+"_Bg") 636 .css({ 637 'position':'fixed', 638 'z-index':100000, 639 'top':0, 640 'left':0, 641 'width':'100%', 642 'height':'100%' 643 }) 644 .fadeIn(500); 645 var def = fld.val().substr(0, 1)=='#' ? fld.val() : themerex_rgb2hex(fld.css('backgroundColor')); 646 jQuery('#'+id+'_colorPreview input,#'+id+'_colorOriginal input').val(def); 647 jQuery('#'+id+'_colorPreview,#'+id+'_colorOriginal').css('background',def); 648} 649 650 651 652/* Cookies manipulations 653---------------------------------------------------------------- */ 654 655function themerex_get_cookie(name) { 656 "use strict";
657 var defa = arguments[1]!=undefined ? arguments[1] : null; 658 var start = document.cookie.indexOf(name + '='); 659 var len = start + name.length + 1; 660 if ((!start) && (name != document.cookie.substring(0, name.length))) { 661 return defa; 662 } 663 if (start == -1) 664 return defa; 665 var end = document.cookie.indexOf(';', len); 666 if (end == -1) 667 end = document.cookie.length; 668 return unescape(document.cookie.substring(len, end)); 669} 670 671 672function themerex_set_cookie(name, value, expires, path, domain, secure) { 673 "use strict"; 674 var expires = arguments[2]!=undefined ? arguments[2] : 0; 675 var path = arguments[3]!=undefined ? arguments[3] : '/'; 676 var domain = arguments[4]!=undefined ? arguments[4] : ''; 677 var secure = arguments[5]!=undefined ? arguments[5] : ''; 678 var today = new Date(); 679 today.setTime(today.getTime()); 680 if (expires) { 681 expires = expires * 1000 * 60 * 60 * 24; 682 } 683 var expires_date = new Date(today.getTime() + (expires)); 684 document.cookie = name + '=' 685 + escape(value) 686 + ((expires) ? ';expires=' + expires_date.toGMTString() : '') 687 + ((path) ? ';path=' + path : '') 688 + ((domain) ? ';domain=' + domain : '') 689 + ((secure) ? ';secure' : ''); 690} 691 692 693function themerex_del_cookie(name, path, domain) { 694 "use strict";
695 var path = arguments[1]!=undefined ? arguments[1] : '/'; 696 var domain = arguments[2]!=undefined ? arguments[2] : ''; 697 if (themerex_get_cookie(name)) 698 document.cookie = name + '=' + ((path) ? ';path=' + path : '') 699 + ((domain) ? ';domain=' + domain : '') 700 + ';expires=Thu, 01-Jan-1970 00:00:01 GMT'; 701} 702 703 704 705/* ListBox and ComboBox manipulations 706---------------------------------------------------------------- */ 707 708function themerex_clear_listbox(box) { 709 "use strict"; 710 for (var i=box.options.length-1; i>=0; i--) 711 box.options[i] = null; 712} 713 714function themerex_add_listbox_item(box, val, text) { 715 "use strict"; 716 var item = new Option(); 717 item.value = val; 718 item.text = text; 719 box.options.add(item); 720} 721 722function themerex_del_listbox_item_by_value(box, val) { 723 "use strict"; 724 for (var i=0; i<box.options.length; i++) { 725 if (box.options[i].value == val) { 726 box.options[i] = null; 727 break; 728 } 729 } 730} 731 732function themerex_del_listbox_item_by_text(box, txt) { 733 "use strict"; 734 for (var i=0; i<box.options.length; i++) { 735 if (box.options[i].text == txt) { 736 box.options[i] = null; 737 break; 738 } 739 } 740} 741 742function themerex_find_listbox_item_by_value(box, val) { 743 "use strict"; 744 var idx = -1; 745 for (var i=0; i<box.options.length; i++) { 746 if (box.options[i].value == val) { 747 idx = i; 748 break; 749 } 750 } 751 return idx; 752} 753 754function themerex_find_listbox_item_by_text(box, txt) { 755 "use strict"; 756 var idx = -1; 757 for (var i=0; i<box.options.length; i++) { 758 if (box.options[i].text == txt) { 759 idx = i; 760 break; 761 } 762 } 763 return idx; 764} 765 766function themerex_select_listbox_item_by_value(box, val) { 767 "use strict"; 768 for (var i = 0; i < box.options.length; i++) { 769 box.options[i].selected = (val == box.options[i].value); 770 } 771} 772 773function themerex_select_listbox_item_by_text(box, txt) { 774 "use strict"; 775 for (var i = 0; i < box.options.length; i++) { 776 box.options[i].selected = (txt == box.options[i].text); 777 } 778} 779 780function themerex_get_listbox_values(box) { 781 "use strict"; 782 var delim = arguments[1] ? arguments[1] : ','; 783 var str = ''; 784 for (var i=0; i<box.options.length; i++) { 785 str += (str ? delim : '') + box.options[i].value; 786 } 787 return str; 788} 789 790function themerex_get_listbox_texts(box) { 791 "use strict"; 792 var delim = arguments[1] ? arguments[1] : ','; 793 var str = ''; 794 for (var i=0; i<box.options.length; i++) { 795 str += (str ? delim : '') + box.options[i].text; 796 } 797 return str; 798} 799 800function themerex_sort_listbox(box) { 801 "use strict"; 802 var temp_opts = new Array(); 803 var temp = new Option(); 804 for(var i=0; i<box.options.length; i++) { 805 temp_opts[i] = box.options[i].clone(); 806 } 807 for(var x=0; x<temp_opts.length-1; x++) { 808 for(var y=(x+1); y<temp_opts.length; y++) { 809 if(temp_opts[x].text > temp_opts[y].text) { 810 temp = temp_opts[x]; 811 temp_opts[x] = temp_opts[y]; 812 temp_opts[y] = temp; 813 } 814 } 815 } 816 for(var i=0; i<box.options.length; i++) { 817 box.options[i] = temp_opts[i].clone(); 818 } 819} 820 821function themerex_get_listbox_selected_index(box) { 822 "use strict"; 823 for (var i = 0; i < box.options.length; i++) { 824 if (box.options[i].selected) 825 return i; 826 } 827 return -1; 828} 829 830function themerex_get_listbox_selected_value(box) { 831 "use strict"; 832 for (var i = 0; i < box.options.length; i++) { 833 if (box.options[i].selected) { 834 return box.options[i].value; 835 } 836 } 837 return null; 838} 839 840function themerex_get_listbox_selected_text(box) { 841 "use strict"; 842 for (var i = 0; i < box.options.length; i++) { 843 if (box.options[i].selected) { 844 return box.options[i].text; 845 } 846 } 847 return null; 848} 849 850function themerex_get_listbox_selected_option(box) { 851 "use strict"; 852 for (var i = 0; i < box.options.length; i++) { 853 if (box.options[i].selected) { 854 return box.options[i]; 855 } 856 } 857 return null; 858} 859 860 861 862/* Radio buttons manipulations 863---------------------------------------------------------------- */ 864 865function themerex_get_radio_value(radioGroupObj) { 866 "use strict"; 867 for (var i=0; i < radioGroupObj.length; i++) 868 if (radioGroupObj[i].checked) return radioGroupObj[i].value; 869 return null; 870} 871 872function themerex_set_radio_checked_by_num(radioGroupObj, num) { 873 "use strict"; 874 for (var i=0; i < radioGroupObj.length; i++) 875 if (radioGroupObj[i].checked && i!=num) radioGroupObj[i].checked=false; 876 else if (i==num) radioGroupObj[i].checked=true; 877} 878 879function themerex_set_radio_checked_by_value(radioGroupObj, val) { 880 "use strict"; 881 for (var i=0; i < radioGroupObj.length; i++) 882 if (radioGroupObj[i].checked && radioGroupObj[i].value!=val) radioGroupObj[i].checked=false; 883 else if (radioGroupObj[i].value==val) radioGroupObj[i].checked=true; 884} 885 886 887 888/* Form manipulations 889---------------------------------------------------------------- */ 890
891/* 892// Usage example: 893var error = themerex_form_validate(jQuery(form_selector), { // -------- Options --------- 894 error_message_show: true, // Display or not error message 895 error_message_time: 5000, // Time to display error message 896 error_message_class: 'sc_infobox sc_infobox_style_error', // Class, appended to error message block 897 error_message_text: 'Global error text', // Global error message text (if don't write message in checked field) 898 error_fields_class: 'error_fields_class', // Class, appended to error fields 899 exit_after_first_error: false, // Cancel validation and exit after first error 900 rules: [ 901 { 902 field: 'author', // Checking field name 903 min_length: { value: 1, message: 'The author name can\'t be empty' }, // Min character count (0 - don't check), message - if error occurs 904 max_length: { value: 60, message: 'Too long author name'} // Max character count (0 - don't check), message - if error occurs 905 }, 906 { 907 field: 'email', 908 min_length: { value: 7, message: 'Too short (or empty) email address' }, 909 max_length: { value: 60, message: 'Too long email address'}, 910 mask: { value: '^([a-z0-9_\\-]+\\.)*[a-z0-9_\\-]+@[a-z0-9_\\-]+(\\.[a-z0-9_\\-]+)*\\.[a-z]{2,6}$', message: 'Invalid email address'} 911 }, 912 { 913 field: 'comment', 914 min_length: { value: 1, message: 'The comment text can\'t be empty' }, 915 max_length: { value: 200, message: 'Too long comment'} 916 }, 917 { 918 field: 'pwd1', 919 min_length: { value: 5, message: 'The password can\'t be less then 5 characters' }, 920 max_length: { value: 20, message: 'Too long password'} 921 }, 922 { 923 field: 'pwd2', 924 equal_to: { value: 'pwd1', message: 'The passwords in both fields must be equals' } 925 } 926 ] 927}); 928*/ 929 930function themerex_form_validate(form, opt) { 931 "use strict"; 932 var error_msg = ''; 933 form.find(":input").each(function() { 934 "use strict"; 935 if (error_msg!='' && opt.exit_after_first_error) return; 936 for (var i = 0; i < opt.rules.length; i++) { 937 if (jQuery(this).attr("name") == opt.rules[i].field) { 938 var val = jQuery(this).val(); 939 var error = false; 940 if (typeof(opt.rules[i].min_length) == 'object') { 941 if (opt.rules[i].min_length.value > 0 && val.length < opt.rules[i].min_length.value) { 942 if (error_msg=='') jQuery(this).get(0).focus(); 943 error_msg += '<p class="error_item">' + (typeof(opt.rules[i].min_length.message)!='undefined' ? opt.rules[i].min_length.message : opt.error_message_text ) + '</p>' 944 error = true; 945 } 946 } 947 if ((!error || !opt.exit_after_first_error) && typeof(opt.rules[i].max_length) == 'object') { 948 if (opt.rules[i].max_length.value > 0 && val.length > opt.rules[i].max_length.value) { 949 if (error_msg=='') jQuery(this).get(0).focus(); 950 error_msg += '<p class="error_item">' + (typeof(opt.rules[i].max_length.message)!='undefined' ? opt.rules[i].max_length.message : opt.error_message_text ) + '</p>' 951 error = true; 952 } 953 } 954 if ((!error || !opt.exit_after_first_error) && typeof(opt.rules[i].mask) == 'object') { 955 if (opt.rules[i].mask.value != '') { 956 var regexp = new RegExp(opt.rules[i].mask.value); 957 if (!regexp.test(val)) { 958 if (error_msg=='') jQuery(this).get(0).focus(); 959 error_msg += '<p class="error_item">' + (typeof(opt.rules[i].mask.message)!='undefined' ? opt.rules[i].mask.message : opt.error_message_text ) + '</p>' 960 error = true; 961 } 962 } 963 } 964 if ((!error || !opt.exit_after_first_error) && typeof(opt.rules[i].equal_to) == 'object') { 965 if (opt.rules[i].equal_to.value != '' && val!=jQuery(jQuery(this).get(0).form[opt.rules[i].equal_to.value]).val()) { 966 if (error_msg=='') jQuery(this).get(0).focus(); 967 error_msg += '<p class="error_item">' + (typeof(opt.rules[i].equal_to.message)!='undefined' ? opt.rules[i].equal_to.message : opt.error_message_text ) + '</p>' 968 error = true; 969 } 970 } 971 if (opt.error_fields_class != '') jQuery(this).toggleClass(opt.error_fields_class, error); 972 } 973 } 974 }); 975 if (error_msg!='' && opt.error_message_show) { 976 var error_message_box = form.find(".result"); 977 if (error_message_box.length == 0) error_message_box = form.parent().find(".result"); 978 if (error_message_box.length == 0) { 979 form.append('<div class="result"></div>'); 980 error_message_box = form.find(".result"); 981 } 982 if (opt.error_message_class) error_message_box.toggleClass(opt.error_message_class, true); 983 error_message_box.html(error_msg).fadeIn(); 984 setTimeout(function() { error_message_box.fadeOut(); }, opt.error_message_time); 985 } 986 return error_msg!=''; 987} 988 989 990 991/* Document manipulations 992---------------------------------------------------------------- */ 993 994// Animated scroll to selected id 995function themerex_document_animate_to(id) { 996 if (id.indexOf('#')==-1) id = '#' + id; 997 var obj = jQuery(id).eq(0); 998 if (obj.length == 0) return; 999 var oft = jQuery(id).offset().top; 1000 var st = jQuery(window).scrollTop(); 1001 var speed = Math.min(1600, Math.max(400, Math.round(Math.abs(oft-st) / jQuery(window).height() * 100))); 1002 jQuery('body,html').animate( {scrollTop: oft - jQuery('#wpadminbar').height() - jQuery('header.fixedTopMenu .topWrap').height()}, speed, 'swing'); 1003} 1004 1005// Change browser address without reload page 1006function themerex_document_set_location(curLoc){ 1007 try { 1008 history.pushState(null, null, curLoc); 1009 return; 1010 } catch(e) {}
1011 location.href = curLoc; 1012} 1013 1014// Add hidden elements init functions after tab, accordion, toggles activate 1015function themerex_add_hidden_elements_handler(key, handler) { 1016 themerex_set_global_array('init_hidden_elements', key, handler); 1017} 1018 1019// Init hidden elements after tab, accordion, toggles activate 1020function themerex_init_hidden_elements(cont) { 1021 if (THEMEREX_GLOBALS['init_hidden_elements']) { 1022 for (key in THEMEREX_GLOBALS['init_hidden_elements']) { 1023 THEMEREX_GLOBALS['init_hidden_elements'][key](cont); 1024 } 1025 } 1026} 1027 1028 1029 1030/* Browsers detection 1031---------------------------------------------------------------- */ 1032 1033function themerex_browser_is_mobile() { 1034 var check = false; 1035 (function(a){if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od|ad)|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(a)||/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(a.substr(0,4)))check = true})(navigator.userAgent||navigator.vendor||window.opera); 1036 return check; 1037} 1038function themerex_browser_is_ios() { 1039 return navigator.userAgent.match(/iPad|iPhone|iPod/i) != null; 1040} 1041 1042 1043 1044/* File functions 1045---------------------------------------------------------------- */ 1046 1047function themerex_get_file_name(path) { 1048 path = path.replace(/\\/g, '/'); 1049 var pos = path.lastIndexOf('/'); 1050 if (pos >= 0) 1051 path = path.substr(pos+1); 1052 return path; 1053} 1054 1055function themerex_get_file_ext(path) { 1056 var pos = path.lastIndexOf('.'); 1057 path = pos >= 0 ? path.substr(pos+1) : ''; 1058 return path; 1059} 1060 1061 1062 1063/* Image functions 1064---------------------------------------------------------------- */ 1065 1066// Return true, if all images in the specified container are loaded 1067function themerex_check_images_complete(cont) { 1068 var complete = true; 1069 cont.find('img').each(function() { 1070 if (!complete) return; 1071 if (!jQuery(this).get(0).complete) complete = false; 1072 }); 1073 return complete; 1074}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.