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.
 
 
 
 
 
 

464 lines
13 KiB

  1. // Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. //
  3. // MIT License (MIT)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a
  6. // copy of this software and associated documentation files (the "Software"),
  7. // to deal in the Software without restriction, including without limitation
  8. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. // and/or sell copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22. wn.dom.set_unique_id = function(ele) {
  23. var id = 'unique-' + wn.dom.id_count;
  24. ele.setAttribute('id', id);
  25. wn.dom.id_count++;
  26. return id;
  27. }
  28. // short hand functions for setting up
  29. // rich text editor tinymce
  30. wn.tinymce = {
  31. add_simple: function(ele, height) {
  32. if(ele.myid) {
  33. tinyMCE.execCommand( 'mceAddControl', true, ele.myid);
  34. return;
  35. }
  36. // no create
  37. ele.myid = wn.dom.set_unique_id(ele);
  38. $(ele).tinymce({
  39. // Location of TinyMCE script
  40. script_url : 'lib/js/legacy/tiny_mce_33/tiny_mce.js',
  41. height: height ? height : '200px',
  42. // General options
  43. theme : "advanced",
  44. theme_advanced_buttons1 : "bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright,justifyfull,bullist,numlist,outdent,indent,link,unlink,forecolor,backcolor,code,",
  45. theme_advanced_buttons2 : "",
  46. theme_advanced_buttons3 : "",
  47. theme_advanced_toolbar_location : "top",
  48. theme_advanced_toolbar_align : "left",
  49. theme_advanced_path : false,
  50. theme_advanced_resizing : false
  51. });
  52. },
  53. remove: function(ele) {
  54. tinyMCE.execCommand( 'mceRemoveControl', true, ele.myid);
  55. },
  56. get_value: function(ele) {
  57. return tinymce.get(ele.myid).getContent();
  58. }
  59. }
  60. wn.ele = {
  61. link: function(args) {
  62. var span = $a(args.parent, 'span', 'link_type', args.style);
  63. span.loading_img = $a(args.parent,'img','',{margin:'0px 4px -2px 4px', display:'none'});
  64. span.loading_img.src= 'lib/images/ui/button-load.gif';
  65. span.innerHTML = args.label;
  66. span.user_onclick = args.onclick;
  67. span.onclick = function() { if(!this.disabled) this.user_onclick(this); }
  68. // working
  69. span.set_working = function() {
  70. this.disabled = 1;
  71. $di(this.loading_img);
  72. }
  73. span.done_working = function() {
  74. this.disabled = 0;
  75. $dh(this.loading_img);
  76. }
  77. return span;
  78. }
  79. }
  80. function $ln(parent, label, onclick, style) {
  81. return wn.ele.link({parent:parent, label:label, onclick:onclick, style:style})
  82. }
  83. function $btn(parent, label, onclick, style, css_class, is_ajax) {
  84. wn.require('lib/js/wn/ui/button.js');
  85. if(css_class==='green') css_class='btn-primary';
  86. return new wn.ui.Button(
  87. {parent:parent, label:label, onclick:onclick, style:style, is_ajax: is_ajax, css_class: css_class}
  88. ).btn;
  89. }
  90. // item (for tabs and triggers)
  91. // ====================================
  92. $item_normal = function(ele) {
  93. $y(ele, {padding:'6px 8px',cursor:'pointer',marginRight:'8px', whiteSpace:'nowrap',overflow:'hidden',borderBottom:'1px solid #DDD'});
  94. $bg(ele,'#FFF'); $fg(ele,'#000');
  95. }
  96. $item_active = function(ele) {
  97. $bg(ele,'#FE8'); $fg(ele,'#000');
  98. }
  99. $item_selected = function(ele) {
  100. $bg(ele,'#777'); $fg(ele,'#FFF');
  101. }
  102. $item_pressed = function(ele) {
  103. $bg(ele,'#F90'); $fg(ele,'#FFF');
  104. };
  105. (function($) {
  106. $.fn.set_working = function() {
  107. var ele = this.get(0);
  108. if(ele.loading_img) {
  109. $di(ele.loading_img)
  110. } else {
  111. ele.disabled = 1;
  112. ele.loading_img = $a(ele.parentNode,'img','',
  113. {marginLeft:'4px',marginBottom:'-2px',display:'inline'});
  114. ele.loading_img.src = 'lib/images/ui/button-load.gif';
  115. }
  116. }
  117. $.fn.done_working = function() {
  118. var ele = this.get(0);
  119. ele.disabled = 0;
  120. if(ele.loading_img) { $dh(ele.loading_img) };
  121. }
  122. })(jQuery);
  123. // set out of 100
  124. function set_opacity(ele, ieop) {
  125. var op = ieop / 100;
  126. if (ele.filters) { // internet explorer
  127. try {
  128. ele.filters.item("DXImageTransform.Microsoft.Alpha").opacity = ieop;
  129. } catch (e) {
  130. ele.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity='+ieop+')';
  131. }
  132. } else { // other browsers
  133. ele.style.opacity = op;
  134. }
  135. }
  136. // border radius
  137. // ====================================
  138. $br = function(ele, r, corners) {
  139. if(corners) {
  140. var cl = ['top-left', 'top-right', 'bottom-right' , 'bottom-left'];
  141. for(var i=0; i<4; i++) {
  142. if(corners[i]) {
  143. $(ele).css('-moz-border-radius-'+cl[i].replace('-',''),r).css('-webkit-'+cl[i]+'-border-radius',r);
  144. }
  145. }
  146. } else {
  147. $(ele).css('-moz-border-radius',r).css('-webkit-border-radius',r).css('border-radius',r);
  148. }
  149. }
  150. $bs = function(ele, r) { $(ele).css('-moz-box-shadow',r).css('-webkit-box-shadow',r).css('box-shadow',r); }
  151. // Select
  152. // ====================================
  153. function empty_select(s) {
  154. if(s.custom_select) { s.empty(); return; }
  155. if(s.inp)s = s.inp;
  156. if(s) {
  157. var tmplen = s.length; for(var i=0;i<tmplen; i++) s.options[0] = null;
  158. }
  159. }
  160. function sel_val(s) {
  161. if(s.custom_select) {
  162. return s.inp.value ? s.inp.value : '';
  163. }
  164. if(s.inp)s = s.inp;
  165. try {
  166. if(s.selectedIndex<s.options.length) return s.options[s.selectedIndex].value;
  167. else return '';
  168. } catch(err) { return ''; /* IE fix */ }
  169. }
  170. function add_sel_options(s, list, sel_val, o_style) {
  171. if(s.custom_select) {
  172. s.set_options(list)
  173. if(sel_val) s.inp.value = sel_val;
  174. return;
  175. }
  176. if(s.inp)s = s.inp;
  177. for(var i=0, len=list.length; i<len; i++) {
  178. var o = new Option(list[i], list[i], false, (list[i]==sel_val? true : false));
  179. if(o_style) $y(o, o_style);
  180. s.options[s.options.length] = o;
  181. }
  182. }
  183. function cint(v, def) {
  184. v=v+'';
  185. v=lstrip(v, ['0']);
  186. v=parseInt(v);
  187. if(isNaN(v))v=def?def:0; return v;
  188. }
  189. function validate_email(id) {
  190. if(strip(id.toLowerCase()).search("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")==-1) return 0; else return 1; }
  191. function validate_spl_chars(txt) {
  192. if(txt.search(/^[a-zA-Z0-9_\- ]*$/)==-1) return 1; else return 0; }
  193. function d2h(d) {return cint(d).toString(16);}
  194. function h2d(h) {return parseInt(h,16);}
  195. var $n = '\n';
  196. function set_title(t) {
  197. document.title = (wn.title_prefix ? (wn.title_prefix + ' - ') : '') + t;
  198. }
  199. function $a(parent, newtag, className, cs, innerHTML, onclick) {
  200. if(parent && parent.substr)parent = $i(parent);
  201. var c = document.createElement(newtag);
  202. if(parent)
  203. parent.appendChild(c);
  204. // if image, 3rd parameter is source
  205. if(className) {
  206. if(newtag.toLowerCase()=='img')
  207. c.src = className
  208. else
  209. c.className = className;
  210. }
  211. if(cs)$y(c,cs);
  212. if(innerHTML) c.innerHTML = innerHTML;
  213. if(onclick) c.onclick = onclick;
  214. return c;
  215. }
  216. function $a_input(p, in_type, attributes, cs) {
  217. if(!attributes) attributes = {};
  218. var $input = $(p).append('<input type="'+ in_type +'">').find('input:last');
  219. for(key in attributes)
  220. $input.attr(key, attributes[key]);
  221. var input = $input.get(0);
  222. if(cs)
  223. $y(input,cs);
  224. return input;
  225. }
  226. function $dh(d) {
  227. if(d && d.substr)d=$i(d);
  228. if(d && d.style.display.toLowerCase() != 'none') d.style.display = 'none';
  229. }
  230. function $ds(d) {
  231. if(d && d.substr)d=$i(d);
  232. var t = 'block';
  233. if(d && in_list(['span','img','button'], d.tagName.toLowerCase()))
  234. t = 'inline'
  235. if(d && d.style.display.toLowerCase() != t)
  236. d.style.display = t;
  237. }
  238. function $di(d) { if(d && d.substr)d=$i(d); if(d)d.style.display = 'inline'; }
  239. function $i(id) {
  240. if(!id) return null;
  241. if(id && id.appendChild)return id; // already an element
  242. return document.getElementById(id);
  243. }
  244. function $w(e,w) { if(e && e.style && w)e.style.width = w; }
  245. function $h(e,h) { if(e && e.style && h)e.style.height = h; }
  246. function $bg(e,w) { if(e && e.style && w)e.style.backgroundColor = w; }
  247. function $y(ele, s) {
  248. if(ele && s) {
  249. for(var i in s) ele.style[i]=s[i];
  250. };
  251. return ele;
  252. }
  253. function $yt(tab, r, c, s) { /// set style on tables with wildcards
  254. var rmin = r; var rmax = r;
  255. if(r=='*') { rmin = 0; rmax = tab.rows.length-1; }
  256. if(r.search && r.search('-')!= -1) {
  257. r = r.split('-');
  258. rmin = cint(r[0]); rmax = cint(r[1]);
  259. }
  260. var cmin = c; var cmax = c;
  261. if(c=='*') { cmin = 0; cmax = tab.rows[0].cells.length-1; }
  262. if(c.search && c.search('-')!= -1) {
  263. c = c.split('-');
  264. rmin = cint(c[0]); rmax = cint(c[1]);
  265. }
  266. for(var ri = rmin; ri<=rmax; ri++) {
  267. for(var ci = cmin; ci<=cmax; ci++)
  268. $y($td(tab,ri,ci),s);
  269. }
  270. }
  271. // add css classes etc
  272. function set_style(txt) {
  273. var se = document.createElement('style');
  274. se.type = "text/css";
  275. if (se.styleSheet) {
  276. se.styleSheet.cssText = txt;
  277. } else {
  278. se.appendChild(document.createTextNode(txt));
  279. }
  280. document.getElementsByTagName('head')[0].appendChild(se);
  281. }
  282. // Make table
  283. function make_table(parent, nr, nc, table_width, widths, cell_style, table_style) {
  284. var t = $a(parent, 'table');
  285. t.style.borderCollapse = 'collapse';
  286. if(table_width) t.style.width = table_width;
  287. if(cell_style) t.cell_style=cell_style;
  288. for(var ri=0;ri<nr;ri++) {
  289. var r = t.insertRow(ri);
  290. for(var ci=0;ci<nc;ci++) {
  291. var c = r.insertCell(ci);
  292. if(ri==0 && widths && widths[ci]) {
  293. // set widths
  294. c.style.width = widths[ci];
  295. }
  296. if(cell_style) {
  297. for(var s in cell_style) c.style[s] = cell_style[s];
  298. }
  299. }
  300. }
  301. t.append_row = function() { return append_row(this); }
  302. if(table_style) $y(t, table_style);
  303. return t;
  304. }
  305. function append_row(t, at, style) {
  306. var r = t.insertRow(at ? at : t.rows.length);
  307. if(t.rows.length>1) {
  308. for(var i=0;i<t.rows[0].cells.length;i++) {
  309. var c = r.insertCell(i);
  310. if(style) $y(c, style);
  311. }
  312. }
  313. return r
  314. }
  315. function $td(t,r,c) {
  316. if(r<0)r=t.rows.length+r;
  317. if(c<0)c=t.rows[0].cells.length+c;
  318. return t.rows[r].cells[c];
  319. }
  320. // sum of values in a table column
  321. function $sum(t, cidx) {
  322. var s = 0;
  323. if(cidx<1)cidx = t.rows[0].cells.length + cidx;
  324. for(var ri=0; ri<t.rows.length; ri++) {
  325. var c = t.rows[ri].cells[cidx];
  326. if(c.div) s += flt(c.div.innerHTML);
  327. else if(c.value) s+= flt(c.value);
  328. else s += flt(c.innerHTML);
  329. }
  330. return s;
  331. }
  332. function objpos(obj){
  333. if(obj.substr)obj = $i(obj);
  334. var p = $(obj).offset();
  335. return {x : cint(p.left), y : cint(p.top) }
  336. }
  337. function get_screen_dims() {
  338. var d={};
  339. d.w = 0; d.h = 0;
  340. if( typeof( window.innerWidth ) == 'number' ) {
  341. //Non-IE
  342. d.w = window.innerWidth;
  343. d.h = window.innerHeight;
  344. } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
  345. //IE 6+ in 'standards compliant mode'
  346. d.w = document.documentElement.clientWidth;
  347. d.h = document.documentElement.clientHeight;
  348. } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
  349. //IE 4 compatible
  350. d.w = document.body.clientWidth;
  351. d.h = document.body.clientHeight;
  352. }
  353. return d
  354. }
  355. // get full page size
  356. function get_page_size(){
  357. return [$(document).height(), $(document).width()];
  358. }
  359. // get scroll top
  360. function get_scroll_top() {
  361. var st = 0;
  362. if(document.documentElement && document.documentElement.scrollTop)
  363. st = document.documentElement.scrollTop;
  364. else if(document.body && document.body.scrollTop)
  365. st = document.body.scrollTop;
  366. return st;
  367. }
  368. // URL utilities
  369. wn.urllib = {
  370. // get argument from url
  371. get_arg: function(name) {
  372. name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  373. var regexS = "[\\?&]"+name+"=([^&#]*)";
  374. var regex = new RegExp( regexS );
  375. var results = regex.exec( window.location.href );
  376. if( results == null )
  377. return "";
  378. else
  379. return decodeURIComponent(results[1]);
  380. },
  381. // returns url dictionary
  382. get_dict: function() {
  383. var d = {}
  384. var t = window.location.href.split('?')[1];
  385. if(!t) return d;
  386. if(t.indexOf('#')!=-1) t = t.split('#')[0];
  387. if(!t) return d;
  388. t = t.split('&');
  389. for(var i=0; i<t.length; i++) {
  390. var a = t[i].split('=');
  391. d[decodeURIComponent(a[0])] = decodeURIComponent(a[1]);
  392. }
  393. return d;
  394. },
  395. // returns the base url with http + domain + path (-index.cgi or # or ?)
  396. get_base_url: function() {
  397. var url= window.location.href.split('#')[0].split('?')[0].split('index.html')[0];
  398. if(url.substr(url.length-1, 1)=='/') url = url.substr(0, url.length-1)
  399. return url
  400. },
  401. // return the relative http url for
  402. // a file upload / attachment
  403. // by file id / name
  404. get_file_url: function(file_id) {
  405. return repl('files/%(fn)s', {fn:file_id})
  406. }
  407. }
  408. get_url_arg = wn.urllib.get_arg;
  409. get_url_dict = wn.urllib.get_dict;