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.
 
 
 
 
 
 

192 lines
6.8 KiB

  1. wn.provide('wn.utils');
  2. wn.utils = {
  3. get_file_link: function(filename) {
  4. return wn.utils.is_url(filename) || (filename.indexOf("images/")!=-1) || (filename.indexOf("files/")!=-1)
  5. ? filename : 'files/' + filename;
  6. },
  7. is_html: function(txt) {
  8. if(txt.indexOf("<br>")==-1 && txt.indexOf("<p")==-1
  9. && txt.indexOf("<img")==-1 && txt.indexOf("<div")==-1) {
  10. return false;
  11. }
  12. return true;
  13. },
  14. is_url: function(txt) {
  15. return txt.toLowerCase().substr(0,7)=='http://'
  16. || txt.toLowerCase().substr(0,8)=='https://'
  17. },
  18. escape_script_and_style: function(txt) {
  19. return (!txt || (txt.indexOf("<script>")===-1 && txt.indexOf("<style>")===-1)) ? txt :
  20. "<pre>" + $("<div>").text(txt).html() + "</pre>";
  21. },
  22. filter_dict: function(dict, filters) {
  23. var ret = [];
  24. if(typeof filters=='string') {
  25. return [dict[filters]]
  26. }
  27. $.each(dict, function(i, d) {
  28. for(key in filters) {
  29. if($.isArray(filters[key])) {
  30. if(filters[key][0]=="in") {
  31. if(filters[key][1].indexOf(d[key])==-1)
  32. return;
  33. } else if(filters[key][0]=="not in") {
  34. if(filters[key][1].indexOf(d[key])!=-1)
  35. return;
  36. }
  37. } else {
  38. if(d[key]!=filters[key]) return;
  39. }
  40. }
  41. ret.push(d);
  42. });
  43. return ret;
  44. },
  45. comma_or: function(list) {
  46. return wn.utils.comma_sep(list, " " + wn._("or") + " ");
  47. },
  48. comma_and: function(list) {
  49. return wn.utils.comma_sep(list, " " + wn._("and") + " ");
  50. },
  51. comma_sep: function(list, sep) {
  52. if(list instanceof Array) {
  53. if(list.length==0) {
  54. return "";
  55. } else if (list.length==1) {
  56. return list[0];
  57. } else {
  58. return list.slice(0, list.length-1).join(", ") + sep + list.slice(-1)[0];
  59. }
  60. } else {
  61. return list;
  62. }
  63. },
  64. set_intro: function(me, wrapper, txt) {
  65. if(!me.intro_area) {
  66. me.intro_area = $('<div class="alert alert-info form-intro-area">')
  67. .prependTo(wrapper);
  68. }
  69. if(txt) {
  70. if(txt.search(/<p>/)==-1) txt = '<p>' + txt + '</p>';
  71. me.intro_area.html(txt);
  72. } else {
  73. me.intro_area.remove();
  74. me.intro_area = null;
  75. }
  76. },
  77. set_footnote: function(me, wrapper, txt) {
  78. if(!me.footnote_area) {
  79. me.footnote_area = $('<div class="alert alert-info form-intro-area" style="margin-top: 20px;">')
  80. .appendTo(wrapper);
  81. }
  82. if(txt) {
  83. if(txt.search(/<p>/)==-1) txt = '<p>' + txt + '</p>';
  84. me.footnote_area.html(txt);
  85. } else {
  86. me.footnote_area.remove();
  87. me.footnote_area = null;
  88. }
  89. },
  90. get_args_dict_from_url: function(txt) {
  91. var args = {};
  92. $.each(decodeURIComponent(txt).split("&"), function(i, arg) {
  93. arg = arg.split("=");
  94. args[arg[0]] = arg[1]
  95. });
  96. return args;
  97. },
  98. get_url_from_dict: function(args) {
  99. return encodeURIComponent($.map(args, function(val, key) { return key+"="+val; }).join("&") || "");
  100. },
  101. disable_export_btn: function(btn) {
  102. if(!wn.user.is_report_manager()) {
  103. btn.attr("disabled", "disabled").attr("title",
  104. wn._("Can only be exported by users with role 'Report Manager'"));
  105. }
  106. },
  107. validate_type: function ( val, type ) {
  108. // from https://github.com/guillaumepotier/Parsley.js/blob/master/parsley.js#L81
  109. var regExp;
  110. switch ( type ) {
  111. case "number":
  112. regExp = /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/;
  113. break;
  114. case "digits":
  115. regExp = /^\d+$/;
  116. break;
  117. case "alphanum":
  118. regExp = /^\w+$/;
  119. break;
  120. case "email":
  121. regExp = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
  122. break;
  123. case "url":
  124. regExp = /^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i;
  125. break;
  126. case "dateIso":
  127. regExp = /^(\d{4})\D?(0[1-9]|1[0-2])\D?([12]\d|0[1-9]|3[01])$/;
  128. break;
  129. default:
  130. return false;
  131. break;
  132. }
  133. // test regExp if not null
  134. return '' !== val ? regExp.test( val ) : false;
  135. },
  136. guess_style: function(text, default_style) {
  137. var style = default_style;
  138. if(!text)
  139. return style;
  140. if(has_words(["Open", "Pending"], text)) {
  141. style = "danger";
  142. } else if(has_words(["Closed", "Finished", "Converted", "Completed", "Confirmed",
  143. "Approved", "Yes", "Active"], text)) {
  144. style = "success";
  145. } else if(has_words(["Submitted"], text)) {
  146. style = "info";
  147. }
  148. return style;
  149. },
  150. sort: function(list, key, compare_type, reverse) {
  151. if(list.length < 2)
  152. return list;
  153. var sort_fn = {
  154. "string": function(a, b) {
  155. return cstr(a[key]).localeCompare(cstr(b[key]));
  156. },
  157. "number": function(a, b) {
  158. return flt(a[key]) - flt(b[key]);
  159. }
  160. };
  161. if(!compare_type)
  162. compare_type = typeof list[0][key]==="string" ? "string" : "number";
  163. list.sort(sort_fn[compare_type]);
  164. if(reverse) { list.reverse(); }
  165. return list;
  166. },
  167. unique: function(list) {
  168. var dict = {},
  169. arr = [];
  170. for(var i=0, l=list.length; i < l; i++) {
  171. if(!dict.hasOwnProperty(list[i])) {
  172. dict[list[i]] = null;
  173. arr.push(list[i]);
  174. }
  175. }
  176. return arr;
  177. },
  178. sum: function(list) {
  179. return list.reduce(function(previous_value, current_value) { return flt(previous_value) + flt(current_value); }, 0.0);
  180. },
  181. };