1/** 2 * @file utility funcs for jQuery projects 3 * 4 */ 5// + + + + + + + + + + + + + + + + + + + + + + + + + + + + 6// object literal with funcs for jquery plug-ins 7// + + + + + + + + + + + + + + + + + + + + + + + + + + + + 8 var spc = { 9 /*general options */ 10 config: { 11 debug: false, 12 dev: true 13 }, 14 isDef: function(val){ 15 return (val===undefined) ? false : true; 16 }, 17 /* get options of object */ 18 get_options: function(key, options){ 19 var result = null; 20 if ('object' == typeof(options)) { 21 result = options[key]; 22 } 23 if (!result) { return ""; } 24 return result; 25 }, 26 /* set wai aria roles to list of containern */ 27 set_wa: function(contlist, ariaattr,ariaval){ 28 $(contlist).attr(ariaattr, ariaval); 29 }, 30 /* Encode/decode htmlentities */ 31 encode_entities: function(s){ 32 return $("<acronym/>").text(s).html(); 33 }, 34 decode_entities: function(s){ 35 return $("<acronym/>").html(s).text(); 36 }, 37 /* add func to load event */ 38 add_loadEvent: function(func_name){ 39 var lastonload = window.onload; 40 if (typeof window.onload != 'function') { window.onload = func_name; } 41 else { window.onload = function() { lastonload(); func_name(); }; } 42 }, 43 /* logging for debug */ 44 _debug: function(msg){ 45 if(this.config.debug) { 46 try{ 47 if(console){ 48 console.log(msg); 49 } else{ 50 alert(msg); 51 } 52 }catch(err){ 53 alert(msg); 54 } 55 } 56 }, 57 /* return obj values for debug */ 58 _get_objVs: function(objl){ 59 try{ 60 var p = typeof JSON != "undefined" ? JSON.stringify : function(objl){ 61 var arr = []; 62 $.each(objl,function(key,val){ 63 var next = key + ": "; 64 next += $.isPlainObject(val) ? printObj(val) : val; 65 arr.push( next ); 66 }); 67 return "{ " + arr.join(", ") + " }"; 68 }; 69 return p(objl); 70 }catch(err){ 71 this._debug(err); 72 return ''; 73 } 74 }, 75 aria_live: function(setobj){ 76 if(typeof(setobj)=='object'){ 77 setobj.attr('aria-live',"polite"); 78 } 79 }, 80 aria_role: function(setobj, role){ 81 if(typeof(setobj)=='object'){ 82 setobj.attr('role',role); 83 } 84 }, 85 change_tabindex: function(remobj,setobj,i){ 86 if(typeof(remobj)=='object'){ 87 remobj.removeAttr('tabindex'); 88 } 89 if(typeof(setobj)=='object'){ 90 setobj.attr('tabindex',i); 91 } 92 }, 93 /* set focus to dom object: param obj */ 94 set_newfocusObj: function(focusobj){ 95 try{ 96 if(focusobj) focusobj.focus(); 97 }catch(err){ 98 this._debug('exception: '+err); 99 } 100 }, 101 /* set focus to dom object: param id */ 102 set_newfocusId: function(fid){ 103 try{ 104 var focusobj = document.getElementById(fid); 105 if(focusobj) focusobj.focus(); 106 }catch(err){ 107 this._debug('exception: '+err); 108 } 109 }, 110 /* set focus to nonfocussable dom object: */ 111 set_newfocusBox: function(remobj,setobj){ 112 this.change_tabindex(remobj,setobj,0); 113 try{ 114 if(setobj) setobj.focus(); 115 }catch(err){ 116 this._debug('exception: '+err); 117 } 118 }, 119 /* set title(s) and remove other title(s) if set */ 120 set_title: function(remobj,setobj,ctitle){ 121 if(typeof(remobj)=='object'){ 122 remobj.removeAttr('title'); 123 } 124 if(typeof(setobj)=='object'){ 125 setobj.attr('title',ctitle); 126 } 127 }, 128 /* count appearances of dom elems with certain markup */ 129 count: function(jqdom){ 130 var num = 0; 131 $(jqdom).each(function() { 132 num++; 133 }); 134 return num; 135 }, 136 countOV: function(objlit){ 137 var i = 0; 138 for (var elem in objlit){ 139 i++; 140 } 141 return i; 142 }, 143 /*merge object literals (do not overwrite default, not recursively) */ 144 merge: function(objl1,objl2,objl3,objl4){ 145 return $.extend({},objl1,objl2,objl3,objl4); 146 }, 147 /*merge object literals (do not overwrite default, recursively) */ 148 mergeR: function(objl1,objl2,objl3,objl4){ 149 return $.extend(true,{},objl1,objl2,objl3,objl4); 150 }, 151 loadImage: function(isrc, func, errfunc){ 152 try{ 153 var img = new Image(); 154 img.onload = func; 155 img.onerror = errfunc; 156 img.src = isrc; 157 }catch(err){ 158 errfunc(); 159 } 160 }, 161 tb_getPageSize: function(){ 162 var de=document.documentElement; 163 var w=window.innerWidth||self.innerWidth||(de&&de.clientWidth)||document.body.clientWidth; 164 var h=window.innerHeight||self.innerHeight||(de&&de.clientHeight)||document.body.clientHeight; 165 arrayPageSize=[w,h]; 166 return arrayPageSize; 167 }, 168 useLocStorage: function(){ 169 return ('localStorage' in window && window.localStorage !== null); 170 }, 171 saveLSI: function(key, data){ 172 if (this.useLocStorage) { 173 localStorage.setItem(key, data); 174 } 175 }, 176 removeLSI: function(key){ 177 if (this.useLocStorage) { 178 localStorage.removeItem(key); 179 } 180 }, 181 getLSI: function(key){ 182 if (this.useLocStorage) { 183 return localStorage.getItem(key); 184 } 185 return ''; 186 187 }, 188 showAllLSI: function() { 189 if (this.useLocStorage) { 190 var key = ""; 191 for (var i=0; i<=localStorage.length-1; i++) { 192 key = localStorage.key(i); 193 //console.log(key+': '+localStorage.getItem(key)); 194 } 195 } 196 } 197}; 198 199 200// + + + + + + + + + + + + + + + + + + + + + + + + + + + + 201// shuffle func for random values 202// + + + + + + + + + + + + + + + + + + + + + + + + + + + + 203Array.prototype.shuffle = function(){ 204 var tmp, rand; 205 for(var i =0; i < this.length; i++){ 206 rand = Math.floor(Math.random() * this.length); 207 tmp = this[i]; 208 this[i] = this[rand]; 209 this[rand] =tmp; 210 } 211}; 212 213// + + + + + + + + + + + + + + + + + + + + + + + + + + + + 214// js trim func for ie 215// + + + + + + + + + + + + + + + + + + + + + + + + + + + + 216if(typeof String.prototype.trim !== 'function') { 217 String.prototype.trim = function() { 218 return this.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); 219 }; 220} 221 222/** 223 * simplify setting and getting state out of a node 224 * $("#my_id").data("my_data_attr") equals $$("#my_id").my_data_attr and 225 * $("#my_id").data("my_data_attr", "my_data_val") equals $$("#my_id").my_data_attr = my_data_val 226 * you can also do 227 * $$("#my_id").my_data_val = $$("#my_id").my_data_val + 1. 228 */ 229var $$ = function(param) { 230 var node = $(param)[0]; 231 var id = $.data(node); 232 $.cache[id] = $.cache[id] || {}; 233 $.cache[id].node = node; 234 return $.cache[id]; 235}; 236var alertFB = false; 237if (typeof console === "undefined" || typeof console.log === "undefined") { 238 console = {}; 239 if (alertFB) { 240 console.log = function(msg) { 241 alert(msg); 242 }; 243 } else { 244 console.log = function() {}; 245 } 246} 247 248/** 249 * custom event handler ‘show’/’hide’ events for using .on() 250 */ 251(function ($) { 252 $.each(['show', 'hide'], function (i, e) { 253 var el = $.fn[e]; 254 $.fn[e] = function () { 255 this.trigger(e); 256 return el.apply(this, arguments); 257 }; 258 }); 259})(jQuery); 260