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.

utils.js 8.2 KiB

12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. // MIT License. See license.txt
  3. wn.provide('wn.utils');
  4. wn.utils = {
  5. get_file_link: function(filename) {
  6. if(wn.utils.is_url(filename)) {
  7. return filename;
  8. } else if(filename.indexOf("/")===-1) {
  9. return "files/" + filename;
  10. } else {
  11. return filename;
  12. }
  13. },
  14. is_html: function(txt) {
  15. if(txt.indexOf("<br>")==-1 && txt.indexOf("<p")==-1
  16. && txt.indexOf("<img")==-1 && txt.indexOf("<div")==-1) {
  17. return false;
  18. }
  19. return true;
  20. },
  21. is_url: function(txt) {
  22. return txt.toLowerCase().substr(0,7)=='http://'
  23. || txt.toLowerCase().substr(0,8)=='https://'
  24. },
  25. remove_script_and_style: function(txt) {
  26. return (!txt || (txt.indexOf("<script>")===-1 && txt.indexOf("<style>")===-1)) ? txt :
  27. $("<div></div>").html(txt).find("script,noscript,style,title,meta").remove().end().html();
  28. },
  29. filter_dict: function(dict, filters) {
  30. var ret = [];
  31. if(typeof filters=='string') {
  32. return [dict[filters]]
  33. }
  34. $.each(dict, function(i, d) {
  35. for(key in filters) {
  36. if($.isArray(filters[key])) {
  37. if(filters[key][0]=="in") {
  38. if(filters[key][1].indexOf(d[key])==-1)
  39. return;
  40. } else if(filters[key][0]=="not in") {
  41. if(filters[key][1].indexOf(d[key])!=-1)
  42. return;
  43. } else if(filters[key][0]=="<") {
  44. if (!(d[key] < filters[key])) return;
  45. } else if(filters[key][0]=="<=") {
  46. if (!(d[key] <= filters[key])) return;
  47. } else if(filters[key][0]==">") {
  48. if (!(d[key] > filters[key])) return;
  49. } else if(filters[key][0]==">=") {
  50. if (!(d[key] >= filters[key])) return;
  51. }
  52. } else {
  53. if(d[key]!=filters[key]) return;
  54. }
  55. }
  56. ret.push(d);
  57. });
  58. return ret;
  59. },
  60. comma_or: function(list) {
  61. return wn.utils.comma_sep(list, " " + wn._("or") + " ");
  62. },
  63. comma_and: function(list) {
  64. return wn.utils.comma_sep(list, " " + wn._("and") + " ");
  65. },
  66. comma_sep: function(list, sep) {
  67. if(list instanceof Array) {
  68. if(list.length==0) {
  69. return "";
  70. } else if (list.length==1) {
  71. return list[0];
  72. } else {
  73. return list.slice(0, list.length-1).join(", ") + sep + list.slice(-1)[0];
  74. }
  75. } else {
  76. return list;
  77. }
  78. },
  79. set_intro: function(me, wrapper, txt) {
  80. if(!me.intro_area) {
  81. me.intro_area = $('<div class="alert alert-info form-intro-area">')
  82. .prependTo(wrapper);
  83. }
  84. if(txt) {
  85. me.intro_area.html(txt);
  86. } else {
  87. me.intro_area.remove();
  88. me.intro_area = null;
  89. }
  90. },
  91. set_footnote: function(me, wrapper, txt) {
  92. if(!me.footnote_area) {
  93. me.footnote_area = $('<div class="alert alert-info form-intro-area" style="margin-top: 20px;">')
  94. .appendTo(wrapper);
  95. }
  96. if(txt) {
  97. if(txt.search(/<p>/)==-1) txt = '<p>' + txt + '</p>';
  98. me.footnote_area.html(txt);
  99. } else {
  100. me.footnote_area.remove();
  101. me.footnote_area = null;
  102. }
  103. },
  104. get_args_dict_from_url: function(txt) {
  105. var args = {};
  106. $.each(decodeURIComponent(txt).split("&"), function(i, arg) {
  107. arg = arg.split("=");
  108. args[arg[0]] = arg[1]
  109. });
  110. return args;
  111. },
  112. get_url_from_dict: function(args) {
  113. return $.map(args, function(val, key) {
  114. if(val!==null)
  115. return encodeURIComponent(key)+"="+encodeURIComponent(val);
  116. else
  117. return null;
  118. }).join("&") || "";
  119. },
  120. validate_type: function ( val, type ) {
  121. // from https://github.com/guillaumepotier/Parsley.js/blob/master/parsley.js#L81
  122. var regExp;
  123. switch ( type ) {
  124. case "number":
  125. regExp = /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/;
  126. break;
  127. case "digits":
  128. regExp = /^\d+$/;
  129. break;
  130. case "alphanum":
  131. regExp = /^\w+$/;
  132. break;
  133. case "email":
  134. 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;
  135. break;
  136. case "url":
  137. 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;
  138. break;
  139. case "dateIso":
  140. regExp = /^(\d{4})\D?(0[1-9]|1[0-2])\D?([12]\d|0[1-9]|3[01])$/;
  141. break;
  142. default:
  143. return false;
  144. break;
  145. }
  146. // test regExp if not null
  147. return '' !== val ? regExp.test( val ) : false;
  148. },
  149. guess_style: function(text, default_style) {
  150. var style = default_style;
  151. if(!text)
  152. return style;
  153. if(has_words(["Open", "Pending"], text)) {
  154. style = "danger";
  155. } else if(has_words(["Closed", "Finished", "Converted", "Completed", "Confirmed",
  156. "Approved", "Yes", "Active"], text)) {
  157. style = "success";
  158. } else if(has_words(["Submitted"], text)) {
  159. style = "info";
  160. }
  161. return style;
  162. },
  163. sort: function(list, key, compare_type, reverse) {
  164. if(list.length < 2)
  165. return list;
  166. var sort_fn = {
  167. "string": function(a, b) {
  168. return cstr(a[key]).localeCompare(cstr(b[key]));
  169. },
  170. "number": function(a, b) {
  171. return flt(a[key]) - flt(b[key]);
  172. }
  173. };
  174. if(!compare_type)
  175. compare_type = typeof list[0][key]==="string" ? "string" : "number";
  176. list.sort(sort_fn[compare_type]);
  177. if(reverse) { list.reverse(); }
  178. return list;
  179. },
  180. unique: function(list) {
  181. var dict = {},
  182. arr = [];
  183. for(var i=0, l=list.length; i < l; i++) {
  184. if(!dict.hasOwnProperty(list[i])) {
  185. dict[list[i]] = null;
  186. arr.push(list[i]);
  187. }
  188. }
  189. return arr;
  190. },
  191. dict: function(keys,values) {
  192. // make dictionaries from keys and values
  193. var out = [];
  194. $.each(values, function(row_idx, row) {
  195. var new_row = {};
  196. $.each(keys, function(key_idx, key) {
  197. new_row[key] = row[key_idx];
  198. })
  199. out.push(new_row);
  200. });
  201. return out;
  202. },
  203. sum: function(list) {
  204. return list.reduce(function(previous_value, current_value) { return flt(previous_value) + flt(current_value); }, 0.0);
  205. },
  206. resize_image: function(reader, callback, max_width, max_height) {
  207. var tempImg = new Image();
  208. if(!max_width) max_width = 600;
  209. if(!max_height) max_height = 400;
  210. tempImg.src = reader.result;
  211. tempImg.onload = function() {
  212. var tempW = tempImg.width;
  213. var tempH = tempImg.height;
  214. if (tempW > tempH) {
  215. if (tempW > max_width) {
  216. tempH *= max_width / tempW;
  217. tempW = max_width;
  218. }
  219. } else {
  220. if (tempH > max_height) {
  221. tempW *= max_height / tempH;
  222. tempH = max_height;
  223. }
  224. }
  225. var canvas = document.createElement('canvas');
  226. canvas.width = tempW;
  227. canvas.height = tempH;
  228. var ctx = canvas.getContext("2d");
  229. ctx.drawImage(this, 0, 0, tempW, tempH);
  230. var dataURL = canvas.toDataURL("image/jpeg");
  231. setTimeout(function() { callback(dataURL); }, 10 );
  232. }
  233. }
  234. };