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.

пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  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. disable_export_btn: function(btn) {
  121. if(!wn.user.is_report_manager()) {
  122. btn.prop("disabled", true).attr("title",
  123. wn._("Can only be exported by users with role 'Report Manager'"));
  124. }
  125. },
  126. validate_type: function ( val, type ) {
  127. // from https://github.com/guillaumepotier/Parsley.js/blob/master/parsley.js#L81
  128. var regExp;
  129. switch ( type ) {
  130. case "number":
  131. regExp = /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/;
  132. break;
  133. case "digits":
  134. regExp = /^\d+$/;
  135. break;
  136. case "alphanum":
  137. regExp = /^\w+$/;
  138. break;
  139. case "email":
  140. 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;
  141. break;
  142. case "url":
  143. 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;
  144. break;
  145. case "dateIso":
  146. regExp = /^(\d{4})\D?(0[1-9]|1[0-2])\D?([12]\d|0[1-9]|3[01])$/;
  147. break;
  148. default:
  149. return false;
  150. break;
  151. }
  152. // test regExp if not null
  153. return '' !== val ? regExp.test( val ) : false;
  154. },
  155. guess_style: function(text, default_style) {
  156. var style = default_style;
  157. if(!text)
  158. return style;
  159. if(has_words(["Open", "Pending"], text)) {
  160. style = "danger";
  161. } else if(has_words(["Closed", "Finished", "Converted", "Completed", "Confirmed",
  162. "Approved", "Yes", "Active"], text)) {
  163. style = "success";
  164. } else if(has_words(["Submitted"], text)) {
  165. style = "info";
  166. }
  167. return style;
  168. },
  169. sort: function(list, key, compare_type, reverse) {
  170. if(list.length < 2)
  171. return list;
  172. var sort_fn = {
  173. "string": function(a, b) {
  174. return cstr(a[key]).localeCompare(cstr(b[key]));
  175. },
  176. "number": function(a, b) {
  177. return flt(a[key]) - flt(b[key]);
  178. }
  179. };
  180. if(!compare_type)
  181. compare_type = typeof list[0][key]==="string" ? "string" : "number";
  182. list.sort(sort_fn[compare_type]);
  183. if(reverse) { list.reverse(); }
  184. return list;
  185. },
  186. unique: function(list) {
  187. var dict = {},
  188. arr = [];
  189. for(var i=0, l=list.length; i < l; i++) {
  190. if(!dict.hasOwnProperty(list[i])) {
  191. dict[list[i]] = null;
  192. arr.push(list[i]);
  193. }
  194. }
  195. return arr;
  196. },
  197. sum: function(list) {
  198. return list.reduce(function(previous_value, current_value) { return flt(previous_value) + flt(current_value); }, 0.0);
  199. },
  200. resize_image: function(reader, callback, max_width, max_height) {
  201. var tempImg = new Image();
  202. if(!max_width) max_width = 600;
  203. if(!max_height) max_height = 400;
  204. tempImg.src = reader.result;
  205. tempImg.onload = function() {
  206. var tempW = tempImg.width;
  207. var tempH = tempImg.height;
  208. if (tempW > tempH) {
  209. if (tempW > max_width) {
  210. tempH *= max_width / tempW;
  211. tempW = max_width;
  212. }
  213. } else {
  214. if (tempH > max_height) {
  215. tempW *= max_height / tempH;
  216. tempH = max_height;
  217. }
  218. }
  219. var canvas = document.createElement('canvas');
  220. canvas.width = tempW;
  221. canvas.height = tempH;
  222. var ctx = canvas.getContext("2d");
  223. ctx.drawImage(this, 0, 0, tempW, tempH);
  224. var dataURL = canvas.toDataURL("image/jpeg");
  225. callback(dataURL);
  226. }
  227. }
  228. };