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.

dom.js 13 KiB

14 years ago
13 years ago
13 years ago
13 years ago
13 years ago
14 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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-info';
  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 SelectWidget(parent, options, width, editable, bg_color) {
  154. var me = this;
  155. // native select
  156. this.inp = $a(parent, 'select');
  157. if(options) add_sel_options(this.inp, options);
  158. if(width) $y(this.inp, {width:width});
  159. this.set_width = function(w) { $y(this.inp, {width:w}) };
  160. this.set_options = function(o) { add_sel_options(this.inp, o); }
  161. this.inp.onchange = function() {
  162. if(me.onchange)me.onchange(this);
  163. }
  164. return;
  165. }
  166. function empty_select(s) {
  167. if(s.custom_select) { s.empty(); return; }
  168. if(s.inp)s = s.inp;
  169. if(s) {
  170. var tmplen = s.length; for(var i=0;i<tmplen; i++) s.options[0] = null;
  171. }
  172. }
  173. function sel_val(s) {
  174. if(s.custom_select) {
  175. return s.inp.value ? s.inp.value : '';
  176. }
  177. if(s.inp)s = s.inp;
  178. try {
  179. if(s.selectedIndex<s.options.length) return s.options[s.selectedIndex].value;
  180. else return '';
  181. } catch(err) { return ''; /* IE fix */ }
  182. }
  183. function add_sel_options(s, list, sel_val, o_style) {
  184. if(s.custom_select) {
  185. s.set_options(list)
  186. if(sel_val) s.inp.value = sel_val;
  187. return;
  188. }
  189. if(s.inp)s = s.inp;
  190. for(var i=0, len=list.length; i<len; i++) {
  191. var o = new Option(list[i], list[i], false, (list[i]==sel_val? true : false));
  192. if(o_style) $y(o, o_style);
  193. s.options[s.options.length] = o;
  194. }
  195. }
  196. function cint(v, def) {
  197. v=v+'';
  198. v=lstrip(v, ['0']);
  199. v=parseInt(v);
  200. if(isNaN(v))v=def?def:0; return v;
  201. }
  202. function validate_email(id) {
  203. 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; }
  204. function validate_spl_chars(txt) {
  205. if(txt.search(/^[a-zA-Z0-9_\- ]*$/)==-1) return 1; else return 0; }
  206. function d2h(d) {return cint(d).toString(16);}
  207. function h2d(h) {return parseInt(h,16);}
  208. var $n = '\n';
  209. function set_title(t) {
  210. document.title = (wn.title_prefix ? (wn.title_prefix + ' - ') : '') + t;
  211. }
  212. function $a(parent, newtag, className, cs, innerHTML, onclick) {
  213. if(parent && parent.substr)parent = $i(parent);
  214. var c = document.createElement(newtag);
  215. if(parent)
  216. parent.appendChild(c);
  217. // if image, 3rd parameter is source
  218. if(className) {
  219. if(newtag.toLowerCase()=='img')
  220. c.src = className
  221. else
  222. c.className = className;
  223. }
  224. if(cs)$y(c,cs);
  225. if(innerHTML) c.innerHTML = innerHTML;
  226. if(onclick) c.onclick = onclick;
  227. return c;
  228. }
  229. function $a_input(p, in_type, attributes, cs) {
  230. if(!attributes) attributes = {};
  231. var $input = $(p).append('<input type="'+ in_type +'">').find('input:last');
  232. for(key in attributes)
  233. $input.attr(key, attributes[key]);
  234. var input = $input.get(0);
  235. if(cs)
  236. $y(input,cs);
  237. return input;
  238. }
  239. function $dh(d) {
  240. if(d && d.substr)d=$i(d);
  241. if(d && d.style.display.toLowerCase() != 'none') d.style.display = 'none';
  242. }
  243. function $ds(d) {
  244. if(d && d.substr)d=$i(d);
  245. var t = 'block';
  246. if(d && in_list(['span','img','button'], d.tagName.toLowerCase()))
  247. t = 'inline'
  248. if(d && d.style.display.toLowerCase() != t)
  249. d.style.display = t;
  250. }
  251. function $di(d) { if(d && d.substr)d=$i(d); if(d)d.style.display = 'inline'; }
  252. function $i(id) {
  253. if(!id) return null;
  254. if(id && id.appendChild)return id; // already an element
  255. return document.getElementById(id);
  256. }
  257. function $w(e,w) { if(e && e.style && w)e.style.width = w; }
  258. function $h(e,h) { if(e && e.style && h)e.style.height = h; }
  259. function $bg(e,w) { if(e && e.style && w)e.style.backgroundColor = w; }
  260. function $y(ele, s) {
  261. if(ele && s) {
  262. for(var i in s) ele.style[i]=s[i];
  263. };
  264. return ele;
  265. }
  266. function $yt(tab, r, c, s) { /// set style on tables with wildcards
  267. var rmin = r; var rmax = r;
  268. if(r=='*') { rmin = 0; rmax = tab.rows.length-1; }
  269. if(r.search && r.search('-')!= -1) {
  270. r = r.split('-');
  271. rmin = cint(r[0]); rmax = cint(r[1]);
  272. }
  273. var cmin = c; var cmax = c;
  274. if(c=='*') { cmin = 0; cmax = tab.rows[0].cells.length-1; }
  275. if(c.search && c.search('-')!= -1) {
  276. c = c.split('-');
  277. rmin = cint(c[0]); rmax = cint(c[1]);
  278. }
  279. for(var ri = rmin; ri<=rmax; ri++) {
  280. for(var ci = cmin; ci<=cmax; ci++)
  281. $y($td(tab,ri,ci),s);
  282. }
  283. }
  284. // add css classes etc
  285. function set_style(txt) {
  286. var se = document.createElement('style');
  287. se.type = "text/css";
  288. if (se.styleSheet) {
  289. se.styleSheet.cssText = txt;
  290. } else {
  291. se.appendChild(document.createTextNode(txt));
  292. }
  293. document.getElementsByTagName('head')[0].appendChild(se);
  294. }
  295. // Make table
  296. function make_table(parent, nr, nc, table_width, widths, cell_style, table_style) {
  297. var t = $a(parent, 'table');
  298. t.style.borderCollapse = 'collapse';
  299. if(table_width) t.style.width = table_width;
  300. if(cell_style) t.cell_style=cell_style;
  301. for(var ri=0;ri<nr;ri++) {
  302. var r = t.insertRow(ri);
  303. for(var ci=0;ci<nc;ci++) {
  304. var c = r.insertCell(ci);
  305. if(ri==0 && widths && widths[ci]) {
  306. // set widths
  307. c.style.width = widths[ci];
  308. }
  309. if(cell_style) {
  310. for(var s in cell_style) c.style[s] = cell_style[s];
  311. }
  312. }
  313. }
  314. t.append_row = function() { return append_row(this); }
  315. if(table_style) $y(t, table_style);
  316. return t;
  317. }
  318. function append_row(t, at, style) {
  319. var r = t.insertRow(at ? at : t.rows.length);
  320. if(t.rows.length>1) {
  321. for(var i=0;i<t.rows[0].cells.length;i++) {
  322. var c = r.insertCell(i);
  323. if(style) $y(c, style);
  324. }
  325. }
  326. return r
  327. }
  328. function $td(t,r,c) {
  329. if(r<0)r=t.rows.length+r;
  330. if(c<0)c=t.rows[0].cells.length+c;
  331. return t.rows[r].cells[c];
  332. }
  333. // sum of values in a table column
  334. function $sum(t, cidx) {
  335. var s = 0;
  336. if(cidx<1)cidx = t.rows[0].cells.length + cidx;
  337. for(var ri=0; ri<t.rows.length; ri++) {
  338. var c = t.rows[ri].cells[cidx];
  339. if(c.div) s += flt(c.div.innerHTML);
  340. else if(c.value) s+= flt(c.value);
  341. else s += flt(c.innerHTML);
  342. }
  343. return s;
  344. }
  345. function objpos(obj){
  346. if(obj.substr)obj = $i(obj);
  347. var p = $(obj).offset();
  348. return {x : cint(p.left), y : cint(p.top) }
  349. }
  350. function get_screen_dims() {
  351. var d={};
  352. d.w = 0; d.h = 0;
  353. if( typeof( window.innerWidth ) == 'number' ) {
  354. //Non-IE
  355. d.w = window.innerWidth;
  356. d.h = window.innerHeight;
  357. } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
  358. //IE 6+ in 'standards compliant mode'
  359. d.w = document.documentElement.clientWidth;
  360. d.h = document.documentElement.clientHeight;
  361. } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
  362. //IE 4 compatible
  363. d.w = document.body.clientWidth;
  364. d.h = document.body.clientHeight;
  365. }
  366. return d
  367. }
  368. // get full page size
  369. function get_page_size(){
  370. return [$(document).height(), $(document).width()];
  371. }
  372. // get scroll top
  373. function get_scroll_top() {
  374. var st = 0;
  375. if(document.documentElement && document.documentElement.scrollTop)
  376. st = document.documentElement.scrollTop;
  377. else if(document.body && document.body.scrollTop)
  378. st = document.body.scrollTop;
  379. return st;
  380. }
  381. // URL utilities
  382. wn.urllib = {
  383. // get argument from url
  384. get_arg: function(name) {
  385. name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  386. var regexS = "[\\?&]"+name+"=([^&#]*)";
  387. var regex = new RegExp( regexS );
  388. var results = regex.exec( window.location.href );
  389. if( results == null )
  390. return "";
  391. else
  392. return decodeURIComponent(results[1]);
  393. },
  394. // returns url dictionary
  395. get_dict: function() {
  396. var d = {}
  397. var t = window.location.href.split('?')[1];
  398. if(!t) return d;
  399. if(t.indexOf('#')!=-1) t = t.split('#')[0];
  400. if(!t) return d;
  401. t = t.split('&');
  402. for(var i=0; i<t.length; i++) {
  403. var a = t[i].split('=');
  404. d[decodeURIComponent(a[0])] = decodeURIComponent(a[1]);
  405. }
  406. return d;
  407. },
  408. // returns the base url with http + domain + path (-index.cgi or # or ?)
  409. get_base_url: function() {
  410. var url= window.location.href.split('#')[0].split('?')[0].split('index.cgi')[0];
  411. if(url.substr(url.length-1, 1)=='/') url = url.substr(0, url.length-1)
  412. return url
  413. },
  414. // return the relative http url for
  415. // a file upload / attachment
  416. // by file id / name
  417. get_file_url: function(file_id) {
  418. return repl('files/%(fn)s', {fn:file_id})
  419. }
  420. }
  421. get_url_arg = wn.urllib.get_arg;
  422. get_url_dict = wn.urllib.get_dict;