You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

260 lines
7.5 KiB

  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. showAllLSI: function() {
  188. if (this.useLocStorage) {
  189. var key = "";
  190. for (var i=0; i<=localStorage.length-1; i++) {
  191. key = localStorage.key(i);
  192. //console.log(key+': '+localStorage.getItem(key));
  193. }
  194. }
  195. }
  196. };
  197. // + + + + + + + + + + + + + + + + + + + + + + + + + + + +
  198. // shuffle func for random values
  199. // + + + + + + + + + + + + + + + + + + + + + + + + + + + +
  200. Array.prototype.shuffle = function(){
  201. var tmp, rand;
  202. for(var i =0; i < this.length; i++){
  203. rand = Math.floor(Math.random() * this.length);
  204. tmp = this[i];
  205. this[i] = this[rand];
  206. this[rand] =tmp;
  207. }
  208. };
  209. // + + + + + + + + + + + + + + + + + + + + + + + + + + + +
  210. // js trim func for ie
  211. // + + + + + + + + + + + + + + + + + + + + + + + + + + + +
  212. if(typeof String.prototype.trim !== 'function') {
  213. String.prototype.trim = function() {
  214. return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  215. };
  216. }
  217. /**
  218. * simplify setting and getting state out of a node
  219. * $("#my_id").data("my_data_attr") equals $$("#my_id").my_data_attr and
  220. * $("#my_id").data("my_data_attr", "my_data_val") equals $$("#my_id").my_data_attr = my_data_val
  221. * you can also do
  222. * $$("#my_id").my_data_val = $$("#my_id").my_data_val + 1.
  223. */
  224. var $$ = function(param) {
  225. var node = $(param)[0];
  226. var id = $.data(node);
  227. $.cache[id] = $.cache[id] || {};
  228. $.cache[id].node = node;
  229. return $.cache[id];
  230. };
  231. var alertFB = false;
  232. if (typeof console === "undefined" || typeof console.log === "undefined") {
  233. console = {};
  234. if (alertFB) {
  235. console.log = function(msg) {
  236. alert(msg);
  237. };
  238. } else {
  239. console.log = function() {};
  240. }
  241. }
  242. /**
  243. * custom event handler ‘show’/’hide’ events for using .on()
  244. */
  245. (function ($) {
  246. $.each(['show', 'hide'], function (i, e) {
  247. var el = $.fn[e];
  248. $.fn[e] = function () {
  249. this.trigger(e);
  250. return el.apply(this, arguments);
  251. };
  252. });
  253. })(jQuery);