/** * Flowers Boutique Framework: Utilities * * @package flowers_boutique * @since flowers_boutique 1.0 */ /* Global variables manipulations ---------------------------------------------------------------- */ // Global variables storage if (typeof FLOWERS_BOUTIQUE_STORAGE == 'undefined') var FLOWERS_BOUTIQUE_STORAGE = {}; // Get global variable function flowers_boutique_storage_get(var_name) { "use strict"; return flowers_boutique_isset(FLOWERS_BOUTIQUE_STORAGE[var_name]) ? FLOWERS_BOUTIQUE_STORAGE[var_name] : ''; } // Set global variable function flowers_boutique_storage_set(var_name, value) { "use strict"; FLOWERS_BOUTIQUE_STORAGE[var_name] = value; } // Inc/Dec global variable with specified value function flowers_boutique_storage_inc(var_name) { "use strict"; var value = arguments[1]==undefined ? 1 : arguments[1]; FLOWERS_BOUTIQUE_STORAGE[var_name] += value; } // Concatenate global variable with specified value function flowers_boutique_storage_concat(var_name, value) { "use strict"; FLOWERS_BOUTIQUE_STORAGE[var_name] += ''+value; } // Get global array element function flowers_boutique_storage_get_array(var_name, key) { "use strict"; return flowers_boutique_isset(FLOWERS_BOUTIQUE_STORAGE[var_name][key]) ? FLOWERS_BOUTIQUE_STORAGE[var_name][key] : ''; } // Set global array element function flowers_boutique_storage_set_array(var_name, key, value) { "use strict"; if (!flowers_boutique_isset(FLOWERS_BOUTIQUE_STORAGE[var_name])) FLOWERS_BOUTIQUE_STORAGE[var_name] = {}; FLOWERS_BOUTIQUE_STORAGE[var_name][key] = value; } // Inc/Dec global array element with specified value function flowers_boutique_storage_inc_array(var_name, key) { "use strict"; var value = arguments[2]==undefined ? 1 : arguments[2]; FLOWERS_BOUTIQUE_STORAGE[var_name][key] += value; } // Concatenate global array element with specified value function flowers_boutique_storage_concat_array(var_name, key, value) { "use strict"; FLOWERS_BOUTIQUE_STORAGE[var_name][key] += ''+value; } /* PHP-style functions ---------------------------------------------------------------- */ function flowers_boutique_isset(obj) { "use strict"; return typeof(obj) != 'undefined'; } function flowers_boutique_empty(obj) { "use strict"; return typeof(obj) == 'undefined' || (typeof(obj)=='object' && obj == null) || (typeof(obj)=='array' && obj.length == 0) || (typeof(obj)=='string' && flowers_boutique_alltrim(obj)=='') || obj===0; } function flowers_boutique_is_array(obj) { "use strict"; return typeof(obj)=='array'; } function flowers_boutique_is_object(obj) { "use strict"; return typeof(obj)=='object'; } function flowers_boutique_clone_object(obj) { "use strict"; if (obj == null || typeof(obj) != 'object') { return obj; } var temp = {}; for (var key in obj) { temp[key] = flowers_boutique_clone_object(obj[key]); } return temp; } function flowers_boutique_merge_objects(obj1, obj2) { "use strict"; for (var i in obj2) if ( obj2.hasOwnProperty( i ) ) obj1[i] = obj2[i]; return obj1; } // Generates a storable representation of a value function flowers_boutique_serialize(mixed_val) { "use strict"; var obj_to_array = arguments.length==1 || argument[1]===true; switch (typeof(mixed_val)) { case "number": if (isNaN(mixed_val) || !isFinite(mixed_val)) return false; else return (Math.floor(mixed_val) == mixed_val ? "i" : "d") + ":" + mixed_val + ";"; case "string": return "s:" + mixed_val.length + ":\"" + mixed_val + "\";"; case "boolean": return "b:" + (mixed_val ? "1" : "0") + ";"; case "object": if (mixed_val == null) return "N;"; else if (mixed_val instanceof Array) { var idxobj = { idx: -1 }; var map = []; for (var i=0; i=0; i--) { if (str.substr(i,1)!=' ') { end = i; break; } } } return str.substring(start, end+1); } function flowers_boutique_ltrim(str) { "use strict"; return flowers_boutique_alltrim(str, 'l'); } function flowers_boutique_rtrim(str) { "use strict"; return flowers_boutique_alltrim(str, 'r'); } function flowers_boutique_padl(str, len) { "use strict"; var ch = arguments[2] ? arguments[2] : ' '; var rez = str.substr(0,len); if (rez.length < len) { for (var i=0; i0) decimals--; var ch = num.substr(i,1); if (ch=='.') { if (precision>0) { res += ch; } decimals = precision; } else if ((ch>=0 && ch<=9) || (ch=='-' && i==0)) res+=ch; } if (precision>0 && decimals!=0) { if (decimals==-1) { res += '.'; decimals = precision; } for (i=decimals; i>0; i--) res +='0'; } //if (isNaN(res)) res = clearNumber(defa, precision, defa); return res; } // Convert number from decimal to hex function flowers_boutique_dec2hex(n) { "use strict"; return Number(n).toString(16); } // Convert number from hex to decimal function flowers_boutique_hex2dec(hex) { "use strict"; return parseInt(hex,16); } /* Array manipulations ---------------------------------------------------------------- */ function flowers_boutique_in_array(val, thearray) { "use strict"; var rez = false; for (var i=0; i thearray[y]) { var tmp = thearray[x]; thearray[x] = thearray[y]; thearray[y] = tmp; } } else { if (thearray[x].toLowerCase() > thearray[y].toLowerCase()) { tmp = thearray[x]; thearray[x] = thearray[y]; thearray[y] = tmp; } } } } return thearray; } /* Date manipulations ---------------------------------------------------------------- */ // Return array[Year, Month, Day, Hours, Minutes, Seconds] // from string: Year[-/.]Month[-/.]Day[T ]Hours:Minutes:Seconds function flowers_boutique_parse_date(dt) { "use strict"; dt = dt.replace(/\//g, '-').replace(/\./g, '-').replace(/T/g, ' ').split('+')[0]; var dt2 = dt.split(' '); var d = dt2[0].split('-'); var t = dt2[1].split(':'); d.push(t[0], t[1], t[2]); return d; } // Return difference string between two dates function flowers_boutique_get_date_difference(dt1) { "use strict"; var dt2 = arguments[1]!==undefined ? arguments[1] : ''; var short_date = arguments[2]!==undefined ? arguments[2] : true; var sec = arguments[3]!==undefined ? arguments[3] : false; var a1 = flowers_boutique_parse_date(dt1); dt1 = Date.UTC(a1[0], a1[1], a1[2], a1[3], a1[4], a1[5]); if (dt2 == '') { dt2 = new Date(); var a2 = [dt2.getFullYear(), dt2.getMonth()+1, dt2.getDate(), dt2.getHours(), dt2.getMinutes(), dt2.getSeconds()]; } else var a2 = flowers_boutique_parse_date(dt2); dt2 = Date.UTC(a2[0], a2[1], a2[2], a2[3], a2[4], a2[5]); var diff = Math.round((dt2 - dt1)/1000); var days = Math.floor(diff / (24*3600)); diff -= days * 24 * 3600; var hours = Math.floor(diff / 3600); diff -= hours * 3600; var minutes = Math.floor(diff / 60); diff -= minutes * 60; var rez = ''; if (days > 0) rez += (rez!='' ? ' ' : '') + days + ' day' + (days > 1 ? 's' : ''); if ((!short_date || rez=='') && hours > 0) rez += (rez!='' ? ' ' : '') + hours + ' hour' + (hours > 1 ? 's' : ''); if ((!short_date || rez=='') && minutes > 0) rez += (rez!='' ? ' ' : '') + minutes + ' minute' + (minutes > 1 ? 's' : ''); if (sec || rez=='') rez += rez!='' || sec ? (' ' + diff + ' second' + (diff > 1 ? 's' : '')) : 'less then minute'; return rez; } /* Colors functions ---------------------------------------------------------------- */ function flowers_boutique_hex2rgb(hex) { "use strict"; hex = parseInt(((hex.indexOf('#') > -1) ? hex.substring(1) : hex), 16); return {r: hex >> 16, g: (hex & 0x00FF00) >> 8, b: (hex & 0x0000FF)}; } function flowers_boutique_rgb2hex(color) { "use strict"; var aRGB; color = color.replace(/\s/g,"").toLowerCase(); if (color=='rgba(0,0,0,0)' || color=='rgba(0%,0%,0%,0%)') color = 'transparent'; if (color.indexOf('rgba(')==0) aRGB = color.match(/^rgba\((\d{1,3}[%]?),(\d{1,3}[%]?),(\d{1,3}[%]?),(\d{1,3}[%]?)\)$/i); else aRGB = color.match(/^rgb\((\d{1,3}[%]?),(\d{1,3}[%]?),(\d{1,3}[%]?)\)$/i); if(aRGB) { color = ''; for (var i=1; i<=3; i++) color += Math.round((aRGB[i][aRGB[i].length-1]=="%"?2.55:1)*parseInt(aRGB[i])).toString(16).replace(/^(.)$/,'0$1'); } else color = color.replace(/^#?([\da-f])([\da-f])([\da-f])$/i, '$1$1$2$2$3$3'); return (color.substr(0,1)!='#' ? '#' : '') + color; } function flowers_boutique_components2hex(r,g,b) { "use strict"; return '#'+ Number(r).toString(16).toUpperCase().replace(/^(.)$/,'0$1') + Number(g).toString(16).toUpperCase().replace(/^(.)$/,'0$1') + Number(b).toString(16).toUpperCase().replace(/^(.)$/,'0$1'); } function flowers_boutique_rgb2components(color) { "use strict"; color = flowers_boutique_rgb2hex(color); var matches = color.match(/^#?([\dabcdef]{2})([\dabcdef]{2})([\dabcdef]{2})$/i); if (!matches) return false; for (var i=1, rgb = new Array(3); i<=3; i++) rgb[i-1] = parseInt(matches[i],16); return rgb; } function flowers_boutique_hex2hsb(hex) { "use strict"; return flowers_boutique_rgb2hsb(flowers_boutique_hex2rgb(hex)); } function flowers_boutique_hsb2hex(hsb) { "use strict"; var rgb = flowers_boutique_hsb2rgb(hsb); return flowers_boutique_components2hex(rgb.r, rgb.g, rgb.b); } function flowers_boutique_rgb2hsb(rgb) { "use strict"; var hsb = {}; hsb.b = Math.max(Math.max(rgb.r,rgb.g),rgb.b); hsb.s = (hsb.b <= 0) ? 0 : Math.round(100*(hsb.b - Math.min(Math.min(rgb.r,rgb.g),rgb.b))/hsb.b); hsb.b = Math.round((hsb.b /255)*100); if ((rgb.r==rgb.g) && (rgb.g==rgb.b)) hsb.h = 0; else if (rgb.r>=rgb.g && rgb.g>=rgb.b) hsb.h = 60*(rgb.g-rgb.b)/(rgb.r-rgb.b); else if (rgb.g>=rgb.r && rgb.r>=rgb.b) hsb.h = 60 + 60*(rgb.g-rgb.r)/(rgb.g-rgb.b); else if (rgb.g>=rgb.b && rgb.b>=rgb.r) hsb.h = 120 + 60*(rgb.b-rgb.r)/(rgb.g-rgb.r); else if (rgb.b>=rgb.g && rgb.g>=rgb.r) hsb.h = 180 + 60*(rgb.b-rgb.g)/(rgb.b-rgb.r); else if (rgb.b>=rgb.r && rgb.r>=rgb.g) hsb.h = 240 + 60*(rgb.r-rgb.g)/(rgb.b-rgb.g); else if (rgb.r>=rgb.b && rgb.b>=rgb.g) hsb.h = 300 + 60*(rgb.r-rgb.b)/(rgb.r-rgb.g); else hsb.h = 0; hsb.h = Math.round(hsb.h); return hsb; } function flowers_boutique_hsb2rgb(hsb) { "use strict"; var rgb = {}; var h = Math.round(hsb.h); var s = Math.round(hsb.s*255/100); var v = Math.round(hsb.b*255/100); if (s == 0) { rgb.r = rgb.g = rgb.b = v; } else { var t1 = v; var t2 = (255-s)*v/255; var t3 = (t1-t2)*(h%60)/60; if (h==360) h = 0; if (h<60) { rgb.r=t1; rgb.b=t2; rgb.g=t2+t3; } else if (h<120) { rgb.g=t1; rgb.b=t2; rgb.r=t1-t3; } else if (h<180) { rgb.g=t1; rgb.r=t2; rgb.b=t2+t3; } else if (h<240) { rgb.b=t1; rgb.r=t2; rgb.g=t1-t3; } else if (h<300) { rgb.b=t1; rgb.g=t2; rgb.r=t2+t3; } else if (h<360) { rgb.r=t1; rgb.g=t2; rgb.b=t1-t3; } else { rgb.r=0; rgb.g=0; rgb.b=0; } } return { r:Math.round(rgb.r), g:Math.round(rgb.g), b:Math.round(rgb.b) }; } function flowers_boutique_color_picker(){ "use strict"; var id = arguments[0] ? arguments[0] : "iColorPicker"+Math.round(Math.random()*1000); var colors = arguments[1] ? arguments[1] : '#f00,#ff0,#0f0,#0ff,#00f,#f0f,#fff,#ebebeb,#e1e1e1,#d7d7d7,#cccccc,#c2c2c2,#b7b7b7,#acacac,#a0a0a0,#959595,' +'#ee1d24,#fff100,#00a650,#00aeef,#2f3192,#ed008c,#898989,#7d7d7d,#707070,#626262,#555,#464646,#363636,#262626,#111,#000,' +'#f7977a,#fbad82,#fdc68c,#fff799,#c6df9c,#a4d49d,#81ca9d,#7bcdc9,#6ccff7,#7ca6d8,#8293ca,#8881be,#a286bd,#bc8cbf,#f49bc1,#f5999d,' +'#f16c4d,#f68e54,#fbaf5a,#fff467,#acd372,#7dc473,#39b778,#16bcb4,#00bff3,#438ccb,#5573b7,#5e5ca7,#855fa8,#a763a9,#ef6ea8,#f16d7e,' +'#ee1d24,#f16522,#f7941d,#fff100,#8fc63d,#37b44a,#00a650,#00a99e,#00aeef,#0072bc,#0054a5,#2f3192,#652c91,#91278f,#ed008c,#ee105a,' +'#9d0a0f,#a1410d,#a36209,#aba000,#588528,#197b30,#007236,#00736a,#0076a4,#004a80,#003370,#1d1363,#450e61,#62055f,#9e005c,#9d0039,' +'#790000,#7b3000,#7c4900,#827a00,#3e6617,#045f20,#005824,#005951,#005b7e,#003562,#002056,#0c004b,#30004a,#4b0048,#7a0045,#7a0026'; var colorsList = colors.split(','); var tbl = ''; for (var i=0; i0 ? '' : '') + ''; tbl += ''; } tbl += '' + '' + '' + '' + '
 
' + '' + '' + '' + '' + '
'; jQuery(document.createElement("div")) .attr("id", id) .css('display','none') .html(tbl) .appendTo("body") .addClass("iColorPickerTable") .on('mouseover', 'thead td', function(){ "use strict"; var aaa = flowers_boutique_rgb2hex(jQuery(this).css('background-color')); jQuery('#'+id+'_colorPreview').css('background',aaa); jQuery('#'+id+'_colorPreview input').val(aaa); }) .on('keypress', '#'+id+'_colorPreview input', function(key){ "use strict"; var aaa = jQuery(this).val() if (aaa.length<7 && ((key.which>=48 && key.which<=57) || (key.which>=97 && key.which<=102) || (key.which===35 || aaa.length===0))) { aaa += String.fromCharCode(key.which); } else if (key.which == 8 && aaa.length>0) { aaa = aaa.substring(0, aaa.length-1); } else if (key.which===13 && (aaa.length===4 || aaa.length===7)) { var fld = jQuery('#'+id).data('field'); var func = jQuery('#'+id).data('func'); if (func!=null && func!='undefined') { func(fld, aaa); } else { fld.val(aaa).css('backgroundColor', aaa).trigger('change'); } jQuery('#'+id+'_Bg').fadeOut(500); jQuery('#'+id).fadeOut(500); } else { key.preventDefault(); return false; } if (aaa.substr(0,1)==='#' && (aaa.length===4 || aaa.length===7)) { jQuery('#'+id+'_colorPreview').css('background',aaa); } }) .on('click', 'thead td', function(e){ "use strict"; var fld = jQuery('#'+id).data('field'); var func = jQuery('#'+id).data('func'); var aaa = flowers_boutique_rgb2hex(jQuery(this).css('background-color')); if (func!=null && func!='undefined') { func(fld, aaa); } else { fld.val(aaa).css('backgroundColor', aaa).trigger('change'); } jQuery('#'+id+'_Bg').fadeOut(500); jQuery('#'+id).fadeOut(500); e.preventDefault(); return false; }) .on('click', 'tbody .iColorPicker_moreColors', function(e){ "use strict"; var thead = jQuery(this).parents('table').find('thead'); var out = ''; if (thead.hasClass('more_colors')) { for (var i=0; i0 ? '' : '') + ''; out += ' '; } thead.removeClass('more_colors').empty().html(out+''); jQuery('#'+id+'_colorPreview').attr('colspan', 8); jQuery('#'+id+'_colorOriginal').attr('colspan', 8); } else { var rgb=[0,0,0], i=0, j=-1; // Set j=-1 or j=0 - show 2 different colors layouts while (rgb[0]<0xF || rgb[1]<0xF || rgb[2]<0xF) { if (i%18==0) out += (i>0 ? '' : '') + ''; i++; out += ' '; rgb[2]+=3; if (rgb[2]>0xF) { rgb[1]+=3; if (rgb[1]>(j===0 ? 6 : 0xF)) { rgb[0]+=3; if (rgb[0]>0xF) { if (j===0) { j=1; rgb[0]=0; rgb[1]=9; rgb[2]=0; } else { break; } } else { rgb[1]=(j < 1 ? 0 : 9); rgb[2]=0; } } else { rgb[2]=0; } } } thead.addClass('more_colors').empty().html(out+' '); jQuery('#'+id+'_colorPreview').attr('colspan', 9); jQuery('#'+id+'_colorOriginal').attr('colspan', 9); } jQuery('#'+id+' table.colorPickerTable thead td') .css({ 'width':'12px', 'height':'14px', 'border':'1px solid #000', 'cursor':'pointer' }); e.preventDefault(); return false; }); jQuery(document.createElement("div")) .attr("id", id+"_Bg") .on('click', function(e) { "use strict"; jQuery("#"+id+"_Bg").fadeOut(500); jQuery("#"+id).fadeOut(500); e.preventDefault(); return false; }) .appendTo("body"); jQuery('#'+id+' table.colorPickerTable thead td') .css({ 'width':'12px', 'height':'14px', 'border':'1px solid #000', 'cursor':'pointer' }); jQuery('#'+id+' table.colorPickerTable') .css({'border-collapse':'collapse'}); jQuery('#'+id) .css({ 'border':'1px solid #ccc', 'background':'#333', 'padding':'5px', 'color':'#fff', 'z-index':999999 }); jQuery('#'+id+'_colorPreview') .css({'height':'50px'}); return id; } function flowers_boutique_color_picker_show(id, fld, func) { "use strict"; if (id===null || id==='') { id = jQuery('.iColorPickerTable').attr('id'); } var eICP = fld.offset(); var w = jQuery('#'+id).width(); var h = jQuery('#'+id).height(); var l = eICP.left + w < jQuery(window).width()-10 ? eICP.left : jQuery(window).width()-10 - w; var t = eICP.top + fld.outerHeight() + h < jQuery(document).scrollTop() + jQuery(window).height()-10 ? eICP.top + fld.outerHeight() : eICP.top - h - 13; jQuery("#"+id) .data({field: fld, func: func}) .css({ 'top':t+"px", 'left':l+"px", 'position':'absolute', 'z-index':100001 }) .fadeIn(500); jQuery("#"+id+"_Bg") .css({ 'position':'fixed', 'z-index':100000, 'top':0, 'left':0, 'width':'100%', 'height':'100%' }) .fadeIn(500); var def = fld.val().substr(0, 1)=='#' ? fld.val() : flowers_boutique_rgb2hex(fld.css('backgroundColor')); jQuery('#'+id+'_colorPreview input,#'+id+'_colorOriginal input').val(def); jQuery('#'+id+'_colorPreview,#'+id+'_colorOriginal').css('background',def); } /* Cookies manipulations ---------------------------------------------------------------- */ function flowers_boutique_get_cookie(name) { "use strict"; var defa = arguments[1]!=undefined ? arguments[1] : null; var start = document.cookie.indexOf(name + '='); var len = start + name.length + 1; if ((!start) && (name != document.cookie.substring(0, name.length))) { return defa; } if (start == -1) return defa; var end = document.cookie.indexOf(';', len); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(len, end)); } function flowers_boutique_set_cookie(name, value, expires, path, domain, secure) { "use strict"; var expires = arguments[2]!=undefined ? arguments[2] : 0; var path = arguments[3]!=undefined ? arguments[3] : '/'; var domain = arguments[4]!=undefined ? arguments[4] : ''; var secure = arguments[5]!=undefined ? arguments[5] : ''; var today = new Date(); today.setTime(today.getTime()); if (expires) { expires = expires * 1000 * 60 * 60 * 24; } var expires_date = new Date(today.getTime() + (expires)); document.cookie = name + '=' + escape(value) + ((expires) ? ';expires=' + expires_date.toGMTString() : '') + ((path) ? ';path=' + path : '') + ((domain) ? ';domain=' + domain : '') + ((secure) ? ';secure' : ''); } function flowers_boutique_del_cookie(name, path, domain) { "use strict"; var path = arguments[1]!=undefined ? arguments[1] : '/'; var domain = arguments[2]!=undefined ? arguments[2] : ''; if (flowers_boutique_get_cookie(name)) document.cookie = name + '=' + ((path) ? ';path=' + path : '') + ((domain) ? ';domain=' + domain : '') + ';expires=Thu, 01-Jan-1970 00:00:01 GMT'; } /* ListBox and ComboBox manipulations ---------------------------------------------------------------- */ function flowers_boutique_clear_listbox(box) { "use strict"; for (var i=box.options.length-1; i>=0; i--) box.options[i] = null; } function flowers_boutique_add_listbox_item(box, val, text) { "use strict"; var item = new Option(); item.value = val; item.text = text; box.options.add(item); } function flowers_boutique_del_listbox_item_by_value(box, val) { "use strict"; for (var i=0; i temp_opts[y].text) { temp = temp_opts[x]; temp_opts[x] = temp_opts[y]; temp_opts[y] = temp; } } } for(var i=0; i