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.
 
 
 
 
 
 

1338 rivejä
35 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. // fields.js
  23. //
  24. // Fields are divided into 2 types
  25. // 1. Standard fields are loaded with the libarary
  26. // 2. Special fields are loaded with form.compressed.js
  27. //
  28. //
  29. // + wrapper
  30. // + input_area
  31. // + display_area
  32. // ======================================================================================
  33. var no_value_fields = ['Section Break', 'Column Break', 'HTML', 'Table', 'FlexTable', 'Button', 'Image'];
  34. var codeid=0; var code_editors={};
  35. function Field() {
  36. this.with_label = 1;
  37. }
  38. Field.prototype.make_body = function() {
  39. var ischk = (this.df.fieldtype=='Check' ? 1 : 0);
  40. // parent element
  41. if(this.parent)
  42. this.wrapper = $a(this.parent, (this.with_label || this.df.fieldtype=="HTML" ? 'div' : 'span'));
  43. else
  44. this.wrapper = document.createElement((this.with_label || this.df.fieldtype=="HTML" ? 'div' : 'span'));
  45. $(this.wrapper).addClass("field-wrapper");
  46. this.label_area = $a(this.wrapper, 'div', '',
  47. {margin:'0px 0px 2px 0px', minHeight:'1em'});
  48. var label_wrapper = this.label_area;
  49. if(ischk && !this.in_grid) {
  50. var label_wrapper = $("<div style='margin-bottom: 9px'>").appendTo(this.label_area).get(0);
  51. this.input_area = $a(label_wrapper, 'span', '', {marginRight:'4px'});
  52. this.disp_area = $a(label_wrapper, 'span', '', {marginRight:'4px'});
  53. }
  54. // label
  55. if(this.with_label) {
  56. this.label_span = $a(label_wrapper, 'span', 'small')
  57. if(wn.boot && wn.boot.developer_mode)
  58. $(this.label_span).attr("title", this.df.fieldname);
  59. // error icon
  60. this.label_icon = $('<i class="icon icon-warning-sign">').toggle(false)
  61. .appendTo(label_wrapper).css('margin-left','7px')
  62. .attr("title", "This field is mandatory.");
  63. } else {
  64. this.label_span = $a(label_wrapper, 'span', '', {marginRight:'4px'})
  65. $dh(label_wrapper);
  66. }
  67. // make the input areas
  68. if(!this.input_area) {
  69. this.input_area = $a(this.wrapper, (this.with_label ? 'div' : 'span'));
  70. this.disp_area = $a(this.wrapper, (this.with_label ? 'div' : 'span'));
  71. }
  72. // apply style
  73. if(this.in_grid) {
  74. if(this.label_area) $dh(this.label_area);
  75. } else {
  76. this.input_area.className = 'input_area';
  77. $y(this.wrapper,{marginBottom:'9px'});
  78. // set description
  79. this.set_description();
  80. }
  81. // bind label refresh
  82. if(this.onmake)this.onmake();
  83. }
  84. Field.prototype.set_max_width = function() {
  85. var no_max = ['Code', 'Text Editor', 'Text', 'Small Text', 'Table', 'HTML']
  86. if(this.wrapper && this.layout_cell && this.layout_cell.parentNode.cells
  87. && this.layout_cell.parentNode.cells.length==1 && !in_list(no_max, this.df.fieldtype)) {
  88. $y(this.wrapper, {paddingRight:'50%'});
  89. }
  90. }
  91. Field.prototype.set_label = function() {
  92. if(this.with_label && this.label_area && this.label!=this.df.label) {
  93. this.label_span.innerHTML = wn._(this.df.label);
  94. this.label = this.df.label;
  95. }
  96. }
  97. Field.prototype.set_description = function(txt) {
  98. if(this.df.description || txt) {
  99. // parent
  100. if(!this.desc_area) {
  101. var p = in_list(['Text Editor', 'Code', 'Check'], this.df.fieldtype)
  102. ? this.label_area : this.wrapper;
  103. this.desc_area = $a(p, 'div', 'help small');
  104. }
  105. $(this.desc_area).html(wn._(this.df.description || txt));
  106. }
  107. }
  108. Field.prototype.get_status = function() {
  109. // if used in filters
  110. if(this.in_filter)
  111. this.not_in_form = this.in_filter;
  112. if(this.not_in_form) {
  113. return 'Write';
  114. }
  115. if(!this.df.permlevel) this.df.permlevel = 0;
  116. var p = this.perm[this.df.permlevel];
  117. var ret;
  118. // permission level
  119. if(p && p[WRITE] && !this.df.disabled)
  120. ret='Write';
  121. else if(p && p[READ])
  122. ret='Read';
  123. else
  124. ret='None';
  125. // hidden
  126. if(cint(this.df.hidden)) {
  127. ret = 'None';
  128. }
  129. // for submit
  130. if(ret=='Write' && cint(cur_frm.doc.docstatus) > 0) {
  131. ret = 'Read';
  132. }
  133. // allow on submit
  134. var a_o_s = cint(this.df.allow_on_submit);
  135. if(a_o_s && (this.in_grid || (this.frm && this.frm.meta.istable))) {
  136. // if grid is allow-on-submit, everything in it is too!
  137. a_o_s = null;
  138. if(this.in_grid)
  139. a_o_s = this.grid.field.df.allow_on_submit;
  140. if(this.frm.meta.istable) {
  141. a_o_s = cur_grid.field.df.allow_on_submit;
  142. }
  143. }
  144. if(ret=="Read" && a_o_s && cint(cur_frm.doc.docstatus)==1 &&
  145. cur_frm.perm[this.df.permlevel][WRITE]) {
  146. ret='Write';
  147. }
  148. // workflow state
  149. if(ret=="Write" && cur_frm.read_only) {
  150. ret = 'Read';
  151. }
  152. // make a field read_only if read_only
  153. // is checked (disregards write permission)
  154. if(ret=="Write" && cint(this.df.read_only)) {
  155. ret = "Read";
  156. }
  157. return ret;
  158. }
  159. Field.prototype.set_style_mandatory = function(add) {
  160. if(add) {
  161. $(this.txt ? this.txt : this.input).addClass('input-mandatory');
  162. if(this.disp_area) $(this.disp_area).addClass('input-mandatory');
  163. } else {
  164. $(this.txt ? this.txt : this.input).removeClass('input-mandatory');
  165. if(this.disp_area) $(this.disp_area).removeClass('input-mandatory');
  166. }
  167. }
  168. Field.prototype.refresh_mandatory = function() {
  169. if(this.in_filter)return;
  170. // mandatory changes
  171. if(this.df.reqd) {
  172. if(this.label_area) this.label_area.style.color= "#d22";
  173. this.set_style_mandatory(1);
  174. } else {
  175. if(this.label_area) this.label_area.style.color= "#222";
  176. this.set_style_mandatory(0);
  177. }
  178. this.refresh_label_icon()
  179. this.set_reqd = this.df.reqd;
  180. }
  181. Field.prototype.refresh_display = function() {
  182. // from permission
  183. if(!this.current_status || this.current_status!=this.disp_status) { // status changed
  184. if(this.disp_status=='Write') { // write
  185. if(this.make_input&&(!this.input)) { // make input if reqd
  186. this.make_input();
  187. if(this.txt || this.input)
  188. $(this.txt || this.input).addClass("mousetrap");
  189. if(this.onmake_input) this.onmake_input();
  190. }
  191. if(this.show) this.show()
  192. else { $ds(this.wrapper); }
  193. // input or content
  194. if(this.input) { // if there, show it!
  195. $ds(this.input_area);
  196. $dh(this.disp_area);
  197. if(this.input.refresh)this.input.refresh();
  198. } else { // no widget
  199. $dh(this.input_area);
  200. $ds(this.disp_area);
  201. }
  202. } else if(this.disp_status=='Read') {
  203. // read
  204. if(this.show) this.show()
  205. else { $ds(this.wrapper); }
  206. $dh(this.input_area);
  207. $ds(this.disp_area);
  208. } else {
  209. // None - hide all
  210. if(this.hide) this.hide();
  211. else $dh(this.wrapper);
  212. }
  213. this.current_status = this.disp_status;
  214. }
  215. }
  216. Field.prototype.refresh = function() {
  217. // get status
  218. this.disp_status = this.get_status();
  219. // if there is a special refresh in case of table, then this is not valid
  220. if(this.in_grid
  221. && this.table_refresh
  222. && this.disp_status == 'Write')
  223. { this.table_refresh(); return; }
  224. this.set_label();
  225. this.refresh_display();
  226. if(this.input) {
  227. if(this.input.refresh) this.input.refresh(this.df);
  228. }
  229. // further refresh
  230. if(this.onrefresh)
  231. this.onrefresh(); // called by various fields
  232. if(this.wrapper) {
  233. this.wrapper.fieldobj = this;
  234. $(this.wrapper).trigger('refresh');
  235. }
  236. if(!this.not_in_form)
  237. this.set_input(_f.get_value(this.doctype,this.docname,this.df.fieldname));
  238. this.refresh_mandatory();
  239. this.set_max_width();
  240. }
  241. Field.prototype.refresh_label_icon = function() {
  242. // mandatory
  243. var to_update = false;
  244. if(this.df.reqd && this.get_value && is_null(this.get_value()))
  245. to_update = true;
  246. if(!to_update && this.df.has_error) this.df.has_error = false;
  247. if(this.label_icon) this.label_icon.css("display", (to_update ? "inline-block" : "none"));
  248. $(this.txt ? this.txt : this.input).toggleClass('field-to-update', to_update);
  249. $(this.txt ? this.txt : this.input).toggleClass('field-has-error',
  250. this.df.has_error ? true : false);
  251. }
  252. // Set / display values
  253. // --------------------------------------------------------------------------------------------
  254. Field.prototype.set = function(val) {
  255. // not in form
  256. if(this.not_in_form)
  257. return;
  258. if((!this.docname) && this.grid) {
  259. this.docname = this.grid.add_newrow(); // new row
  260. }
  261. if(this.validate)
  262. val = this.validate(val);
  263. cur_frm.set_value_in_locals(this.doctype, this.docname, this.df.fieldname, val);
  264. this.value = val; // for return
  265. }
  266. Field.prototype.set_input = function(val) {
  267. this.value = val;
  268. if(this.input&&this.input.set_input) {
  269. if(val==null)this.input.set_input('');
  270. else this.input.set_input(val); // in widget
  271. }
  272. var disp_val = val;
  273. if(val==null) disp_val = '';
  274. this.set_disp(disp_val); // text
  275. }
  276. Field.prototype.run_trigger = function() {
  277. // update mandatory icon
  278. this.refresh_label_icon();
  279. if(this.not_in_form) {
  280. return;
  281. }
  282. if(cur_frm.cscript[this.df.fieldname])
  283. cur_frm.runclientscript(this.df.fieldname, this.doctype, this.docname);
  284. cur_frm.refresh_dependency();
  285. }
  286. Field.prototype.set_disp_html = function(t) {
  287. if(this.disp_area){
  288. $(this.disp_area).addClass('disp_area');
  289. this.disp_area.innerHTML = (t==null ? '' : t);
  290. if(!t) $(this.disp_area).addClass('disp_area_no_val');
  291. }
  292. }
  293. Field.prototype.set_disp = function(val) {
  294. this.set_disp_html(val);
  295. }
  296. // Show in GRID
  297. // --------------------------------------------------------------------------------------------
  298. // for grids (activate against a particular record in the table
  299. Field.prototype.activate = function(docname) {
  300. this.docname = docname;
  301. this.refresh();
  302. if(this.input) {
  303. var v = _f.get_value(this.doctype, this.docname, this.df.fieldname);
  304. this.last_value=v;
  305. // set input value
  306. if(this.input.onchange && this.input.get_value && this.input.get_value() !=v) {
  307. if(this.validate)
  308. this.input.set_value(this.validate(v));
  309. else
  310. this.input.set_value((v==null)?'':v);
  311. if(this.format_input)
  312. this.format_input();
  313. }
  314. if(this.input.focus){
  315. try{this.input.focus();} catch(e){} // IE Fix - Unexpected call???
  316. }
  317. }
  318. if(this.txt) {
  319. try{this.txt.focus();} catch(e){} // IE Fix - Unexpected call???
  320. this.txt.field_object = this;
  321. }
  322. }
  323. // ======================================================================================
  324. function DataField() { } DataField.prototype = new Field();
  325. DataField.prototype.make_input = function() {
  326. var me = this;
  327. this.input = $a_input(this.input_area, this.df.fieldtype=='Password' ? 'password' : 'text');
  328. this.get_value= function() {
  329. var v = this.input.value;
  330. if(this.validate)
  331. v = this.validate(v);
  332. return v;
  333. }
  334. this.input.name = this.df.fieldname;
  335. $(this.input).change(function() {
  336. //me.set_value(me.get_value && me.get_value() || $(this.input).val());
  337. // fix: allow 0 as value
  338. me.set_value(me.get_value ? me.get_value() : $(this.input).val());
  339. });
  340. this.set_value = function(val) {
  341. if(!me.last_value)me.last_value='';
  342. if(me.validate) {
  343. val = me.validate(val);
  344. me.input.value = val==undefined ? '' : val;
  345. }
  346. me.set(val);
  347. if(me.format_input)
  348. me.format_input();
  349. if(in_list(['Currency','Float','Int'], me.df.fieldtype)) {
  350. if(flt(me.last_value)==flt(val)) {
  351. me.last_value = val;
  352. return; // do not run trigger
  353. }
  354. }
  355. me.last_value = val;
  356. me.run_trigger();
  357. }
  358. this.input.set_input = function(val) {
  359. if(val==null)val='';
  360. me.input.value = val;
  361. if(me.format_input)me.format_input();
  362. }
  363. }
  364. DataField.prototype.validate = function(v) {
  365. if(this.df.options == 'Phone') {
  366. if(v+''=='')return '';
  367. v1 = ''
  368. // phone may start with + and must only have numbers later, '-' and ' ' are stripped
  369. v = v.replace(/ /g, '').replace(/-/g, '').replace(/\(/g, '').replace(/\)/g, '');
  370. // allow initial +,0,00
  371. if(v && v.substr(0,1)=='+') {
  372. v1 = '+'; v = v.substr(1);
  373. }
  374. if(v && v.substr(0,2)=='00') {
  375. v1 += '00'; v = v.substr(2);
  376. }
  377. if(v && v.substr(0,1)=='0') {
  378. v1 += '0'; v = v.substr(1);
  379. }
  380. v1 += cint(v) + '';
  381. return v1;
  382. } else if(this.df.options == 'Email') {
  383. if(v+''=='')return '';
  384. if(!validate_email(v)) {
  385. msgprint(this.df.label + ': ' + v + ' is not a valid email id');
  386. return '';
  387. } else
  388. return v;
  389. } else {
  390. return v;
  391. }
  392. }
  393. // ======================================================================================
  394. function ReadOnlyField() { }
  395. ReadOnlyField.prototype = new Field();
  396. // ======================================================================================
  397. function HTMLField() { }
  398. HTMLField.prototype = new Field();
  399. HTMLField.prototype.with_label = 0;
  400. HTMLField.prototype.set_disp = function(val) { if(this.disp_area) this.disp_area.innerHTML = val; }
  401. HTMLField.prototype.set_input = function(val) { if(val) this.set_disp(val); }
  402. HTMLField.prototype.onrefresh = function() { if(this.df.options) this.set_disp(this.df.options); }
  403. // ======================================================================================
  404. var datepicker_active = 0;
  405. function DateField() { } DateField.prototype = new Field();
  406. DateField.prototype.make_input = function() {
  407. var me = this;
  408. this.user_fmt = sys_defaults.date_format;
  409. if(!this.user_fmt)this.user_fmt = 'dd-mm-yy';
  410. this.input = $("<input type='text'>").appendTo(this.input_area).get(0);
  411. $(this.input).datepicker({
  412. dateFormat: me.user_fmt.replace('yyyy','yy'),
  413. altFormat:'yy-mm-dd',
  414. changeYear: true,
  415. beforeShow: function(input, inst) {
  416. datepicker_active = 1
  417. },
  418. onClose: function(dateText, inst) {
  419. datepicker_active = 0;
  420. if(_f.cur_grid_cell)
  421. _f.cur_grid_cell.grid.cell_deselect();
  422. }
  423. });
  424. var me = this;
  425. me.input.onchange = function() {
  426. // input as dd-mm-yyyy
  427. if(this.value==null)this.value='';
  428. if(!this.not_in_form)
  429. me.set(dateutil.user_to_str(me.input.value));
  430. me.run_trigger();
  431. }
  432. me.input.set_input = function(val) {
  433. if(val==null)val='';
  434. else val=dateutil.str_to_user(val);
  435. me.input.value = val;
  436. }
  437. me.get_value = function() {
  438. if(me.input.value)
  439. return dateutil.user_to_str(me.input.value);
  440. }
  441. }
  442. DateField.prototype.set_disp = function(val) {
  443. var v = dateutil.str_to_user(val);
  444. if(v==null)v = '';
  445. this.set_disp_html(v);
  446. }
  447. DateField.prototype.validate = function(v) {
  448. if(!v) return;
  449. var me = this;
  450. this.clear = function() {
  451. msgprint (wn._("Date must be in format") + ": " + this.user_fmt);
  452. me.input.set_input('');
  453. return '';
  454. }
  455. var t = $.map(v.split('-'), function(part) { return cint(part) ? part : null; });
  456. if(t.length!=3) { return this.clear(); }
  457. else if(cint(t[1])>12 || cint(t[1])<1) { return this.clear(); }
  458. else if(cint(t[2])>31 || cint(t[2])<1) { return this.clear(); }
  459. else if(String(cint(t[0])).length!=4) { return this.clear(); } // 4 char for year
  460. return v;
  461. };
  462. // ======================================================================================
  463. // reference when a new record is created via link
  464. function LinkField() { } LinkField.prototype = new Field();
  465. LinkField.prototype.make_input = function() {
  466. var me = this;
  467. if(me.df.no_buttons) {
  468. this.txt = $("<input type='text'>")
  469. .appendTo(this.input_area).get(0);
  470. this.input = this.txt;
  471. } else {
  472. me.input = me.input_area;
  473. $(me.input_area).addClass("input-append link-field");
  474. me.txt = $('<input type="text" style="margin-right: 0px;">')
  475. .css({"width": me.in_filter ? "100px" : (me.in_grid ? "35%" : "60%")})
  476. .appendTo(me.input_area).get(0);
  477. me.btn = $('<button class="btn" title="'+wn._('Search Link')+'">\
  478. <i class="icon-search"></i></button>').appendTo(me.input_area).get(0);
  479. me.btn1 = $('<button class="btn" title="'+wn._('Open Link')+'">\
  480. <i class="icon-play"></i></button>').appendTo(me.input_area).get(0);
  481. me.btn2 = $('<button class="btn" title="'+wn._('Make New')+'">\
  482. <i class="icon-plus"></i></button>').appendTo(me.input_area).get(0);
  483. me.txt.name = me.df.fieldname;
  484. me.setdisabled = function(tf) { me.txt.disabled = tf; }
  485. // setup buttons
  486. me.setup_buttons();
  487. }
  488. me.onrefresh = function() {
  489. var can_create = in_list(wn.boot.profile.can_create, me.df.options);
  490. var can_read = in_list(wn.boot.profile.can_read, me.df.options);
  491. if(!can_create) $(this.btn2).remove();
  492. if(!can_read) $(this.btn1).remove();
  493. }
  494. me.onrefresh();
  495. me.txt.field_object = this;
  496. // set onchange triggers
  497. me.input.set_input = function(val) {
  498. if(val==undefined)val='';
  499. me.txt.value = val;
  500. }
  501. me.get_value = function() { return me.txt.value; }
  502. $(me.txt).autocomplete({
  503. source: function(request, response) {
  504. wn.call({
  505. method:'webnotes.widgets.search.search_link',
  506. args: {
  507. 'txt': request.term,
  508. 'dt': me.df.options,
  509. 'query': me.get_custom_query()
  510. },
  511. callback: function(r) {
  512. response(r.results);
  513. },
  514. });
  515. },
  516. select: function(event, ui) {
  517. me.set_input_value(ui.item.value);
  518. }
  519. }).data('autocomplete')._renderItem = function(ul, item) {
  520. return $('<li></li>')
  521. .data('item.autocomplete', item)
  522. .append(repl('<a><span style="font-weight: bold;">%(label)s</span><br>\
  523. <span style="font-size:10px;">%(info)s</span></a>',
  524. item))
  525. .appendTo(ul);
  526. };
  527. $(this.txt).change(function() {
  528. var val = $(this).val();//me.get_value();
  529. me.set_input_value_executed = false;
  530. if(!val) {
  531. if(selector && selector.display)
  532. return;
  533. me.set_input_value('');
  534. } else {
  535. // SetTimeout hack! if in put is set via autocomplete, do not validate twice
  536. setTimeout(function() {
  537. if (!me.set_input_value_executed) {
  538. me.set_input_value(val);
  539. }
  540. }, 1000);
  541. }
  542. })
  543. }
  544. LinkField.prototype.get_custom_query = function() {
  545. this.set_get_query();
  546. if(this.get_query) {
  547. if(cur_frm)
  548. var doc = locals[cur_frm.doctype][cur_frm.docname];
  549. return this.get_query(doc, this.doctype, this.docname);
  550. }
  551. }
  552. LinkField.prototype.setup_buttons = function() {
  553. var me = this;
  554. // magnifier - search
  555. me.btn.onclick = function() {
  556. selector.set(me, me.df.options, me.df.label);
  557. selector.show(me.txt);
  558. }
  559. // open
  560. if(me.btn1)me.btn1.onclick = function() {
  561. if(me.txt.value && me.df.options) { loaddoc(me.df.options, me.txt.value); }
  562. }
  563. // add button - for inline creation of records
  564. me.can_create = 0;
  565. if((!me.not_in_form) && in_list(profile.can_create, me.df.options)) {
  566. me.can_create = 1;
  567. me.btn2.onclick = function() {
  568. var on_save_callback = function(new_rec) {
  569. if(new_rec) {
  570. var d = _f.calling_doc_stack.pop(); // patch for composites
  571. locals[d[0]][d[1]][me.df.fieldname] = new_rec;
  572. me.refresh();
  573. if(me.grid)me.grid.refresh();
  574. // call script
  575. me.run_trigger();
  576. }
  577. }
  578. _f.calling_doc_stack.push([me.doctype, me.docname]);
  579. new_doc(me.df.options);
  580. }
  581. } else {
  582. $(me.btn2).remove();
  583. }
  584. }
  585. LinkField.prototype.set_input_value = function(val) {
  586. var me = this;
  587. // SetTimeout hack! if in put is set via autocomplete, do not validate twice
  588. me.set_input_value_executed = true;
  589. var from_selector = false;
  590. if(selector && selector.display) from_selector = true;
  591. // refresh mandatory style
  592. me.refresh_label_icon();
  593. // not in form, do nothing
  594. if(me.not_in_form) {
  595. $(this.txt).val(val);
  596. return;
  597. }
  598. // same value, do nothing
  599. if(cur_frm) {
  600. if(val == locals[me.doctype][me.docname][me.df.fieldname]) {
  601. //me.set(val); // one more time, grid bug?
  602. me.run_trigger(); // wanted - called as refresh?
  603. return;
  604. }
  605. }
  606. // set in locals
  607. me.set(val);
  608. // deselect cell if in grid
  609. if(_f.cur_grid_cell)
  610. _f.cur_grid_cell.grid.cell_deselect();
  611. // run trigger if value is cleared
  612. if(locals[me.doctype][me.docname][me.df.fieldname] && !val) {
  613. me.run_trigger();
  614. return;
  615. }
  616. // validate only if val is not empty
  617. if (val) { me.validate_link(val, from_selector); }
  618. }
  619. LinkField.prototype.validate_link = function(val, from_selector) {
  620. // validate the value just entered
  621. var me = this;
  622. if(this.df.options=="[Select]") {
  623. $(me.txt).val(val);
  624. me.run_trigger();
  625. return;
  626. }
  627. var fetch = '';
  628. if(cur_frm.fetch_dict[me.df.fieldname])
  629. fetch = cur_frm.fetch_dict[me.df.fieldname].columns.join(', ');
  630. $c('webnotes.widgets.form.utils.validate_link', {
  631. 'value':val,
  632. 'options':me.df.options,
  633. 'fetch': fetch
  634. },
  635. function(r,rt) {
  636. if(r.message=='Ok') {
  637. // set fetch values
  638. if($(me.txt).val()!=val) {
  639. if((me.grid && !from_selector) || (!me.grid)) {
  640. $(me.txt).val(val);
  641. }
  642. }
  643. if(r.fetch_values)
  644. me.set_fetch_values(r.fetch_values);
  645. me.run_trigger();
  646. } else {
  647. me.txt.value = '';
  648. me.set('');
  649. }
  650. }
  651. );
  652. }
  653. LinkField.prototype.set_fetch_values = function(fetch_values) {
  654. var fl = cur_frm.fetch_dict[this.df.fieldname].fields;
  655. var changed_fields = [];
  656. for(var i=0; i< fl.length; i++) {
  657. if(locals[this.doctype][this.docname][fl[i]]!=fetch_values[i]) {
  658. locals[this.doctype][this.docname][fl[i]] = fetch_values[i];
  659. if(!this.grid) {
  660. refresh_field(fl[i]);
  661. // call trigger on the target field
  662. changed_fields.push(fl[i]);
  663. }
  664. }
  665. }
  666. // run triggers
  667. for(i=0; i<changed_fields.length; i++) {
  668. if(cur_frm.fields_dict[changed_fields[i]]) // on main
  669. cur_frm.fields_dict[changed_fields[i]].run_trigger();
  670. }
  671. // refresh grid
  672. if(this.grid) this.grid.refresh();
  673. }
  674. LinkField.prototype.set_get_query = function() {
  675. if(this.get_query)return;
  676. if(this.grid) {
  677. var f = this.grid.get_field(this.df.fieldname);
  678. if(f.get_query) this.get_query = f.get_query;
  679. }
  680. }
  681. LinkField.prototype.set_disp = function(val) {
  682. var t = null;
  683. if(val)t = "<a href=\'javascript:loaddoc(\""+this.df.options+"\", \""+val+"\")\'>"+val+"</a>";
  684. this.set_disp_html(t);
  685. }
  686. // ======================================================================================
  687. function IntField() { } IntField.prototype = new DataField();
  688. IntField.prototype.validate = function(v) {
  689. if(isNaN(parseInt(v)))return null;
  690. return cint(v);
  691. };
  692. IntField.prototype.format_input = function() {
  693. if(this.input.value==null) this.input.value='';
  694. }
  695. // ======================================================================================
  696. function FloatField() { } FloatField.prototype = new DataField();
  697. FloatField.prototype.validate = function(v) {
  698. var v= parseFloat(v);
  699. if(isNaN(v))
  700. return null;
  701. return v;
  702. };
  703. FloatField.prototype.format_input = function() {
  704. if(this.input.value==null) this.input.value='';
  705. }
  706. FloatField.prototype.onmake_input = function() {
  707. if(!this.input) return;
  708. this.input.onfocus = function() {
  709. this.select();
  710. }
  711. }
  712. // ======================================================================================
  713. function CurrencyField() { } CurrencyField.prototype = new FloatField();
  714. CurrencyField.prototype.format_input = function() {
  715. var v = fmt_money(this.input.value);
  716. if(this.not_in_form) {
  717. if(!flt(this.input.value)) v = ''; // blank in filter
  718. }
  719. this.input.value = v;
  720. }
  721. CurrencyField.prototype.validate = function(v) {
  722. if(v==null || v=='')
  723. return 0;
  724. return flt(v,2);
  725. }
  726. CurrencyField.prototype.set_disp = function(val) {
  727. var v = fmt_money(val);
  728. this.set_disp_html(v);
  729. }
  730. // ======================================================================================
  731. function CheckField() { } CheckField.prototype = new Field();
  732. CheckField.prototype.validate = function(v) {
  733. var v= parseInt(v); if(isNaN(v))return 0;
  734. return v;
  735. };
  736. CheckField.prototype.onmake = function() {
  737. this.checkimg = $("<i class='icon-check'></i>").appendTo(this.disp_area);
  738. }
  739. CheckField.prototype.make_input = function() { var me = this;
  740. this.input = $a_input(this.input_area,'checkbox');
  741. $y(this.input, {width:"16px", border:'0px', marginTop:'-2px'}); // no specs for checkbox
  742. $(this.input).click(function() {
  743. me.set(this.checked?1:0);
  744. me.run_trigger();
  745. })
  746. this.input.set_input = function(v) {
  747. v = parseInt(v); if(isNaN(v)) v = 0;
  748. if(v) me.input.checked = true;
  749. else me.input.checked=false;
  750. }
  751. this.get_value= function() {
  752. return this.input.checked?1:0;
  753. }
  754. }
  755. CheckField.prototype.set_disp = function(val) {
  756. this.checkimg.toggle(val);
  757. }
  758. // ======================================================================================
  759. function TextField() { } TextField.prototype = new Field();
  760. TextField.prototype.set_disp = function(val) {
  761. this.disp_area.innerHTML = replace_newlines(val);
  762. }
  763. TextField.prototype.make_input = function() {
  764. var me = this;
  765. if(this.in_grid)
  766. return; // do nothing, text dialog will take over
  767. this.input = $a(this.input_area, 'textarea');
  768. if(this.df.fieldtype=='Small Text') {
  769. $(this.input).css({height: "80px"});
  770. } else if(this.df.width) {
  771. $(this.input).css({height: cint(this.df.width) + "px"});
  772. } else {
  773. $(this.input).css({height: "160px"});
  774. }
  775. this.input.set_input = function(v) {
  776. me.input.value = v;
  777. }
  778. this.input.onchange = function() {
  779. me.set(me.input.value);
  780. me.run_trigger();
  781. }
  782. this.get_value= function() {
  783. return this.input.value;
  784. }
  785. }
  786. // text dialog
  787. var text_dialog;
  788. function make_text_dialog() {
  789. var d = new Dialog(520,410,'Edit Text');
  790. d.make_body([
  791. ['Text', 'Enter Text'],
  792. ['HTML', 'Description'],
  793. ['Button', 'Update']
  794. ]);
  795. d.widgets['Update'].onclick = function() {
  796. var t = this.dialog;
  797. t.field.set(t.widgets['Enter Text'].value);
  798. t.hide();
  799. }
  800. d.onshow = function() {
  801. this.widgets['Enter Text'].style.height = '300px';
  802. var v = _f.get_value(this.field.doctype,this.field.docname,this.field.df.fieldname);
  803. this.widgets['Enter Text'].value = v==null?'':v;
  804. this.widgets['Enter Text'].focus();
  805. this.widgets['Description'].innerHTML = ''
  806. if(this.field.df.description)
  807. $a(this.widgets['Description'], 'div', 'help small', '', this.field.df.description);
  808. }
  809. d.onhide = function() {
  810. if(_f.cur_grid_cell)
  811. _f.cur_grid_cell.grid.cell_deselect();
  812. }
  813. text_dialog = d;
  814. }
  815. TextField.prototype.table_refresh = function() {
  816. if(!this.text_dialog)
  817. make_text_dialog();
  818. text_dialog.set_title(wn._('Enter text for')+': "'+ wn._(this.df.label) +'"');
  819. text_dialog.field = this;
  820. text_dialog.show();
  821. }
  822. // Select
  823. // ======================================================================================
  824. function SelectField() { } SelectField.prototype = new Field();
  825. SelectField.prototype.make_input = function() {
  826. var me = this;
  827. var opt=[];
  828. if(this.in_filter && (!this.df.single_select)) {
  829. // multiple select
  830. this.input = $a(this.input_area, 'select');
  831. this.input.multiple = true;
  832. this.input.style.height = '4em';
  833. this.input.lab = $a(this.input_area, 'div', {fontSize:'9px',color:'#999'});
  834. this.input.lab.innerHTML = '(Use Ctrl+Click to select multiple or de-select)'
  835. } else {
  836. // Single select
  837. this.input = $a(this.input_area, 'select');
  838. this.input.onchange = function() {
  839. if(me.validate)
  840. me.validate();
  841. me.set(sel_val(this));
  842. me.run_trigger();
  843. }
  844. if(this.df.options == 'attach_files:') {
  845. this.file_attach = true;
  846. $(this.input).css({"width": "70%"});
  847. $("<button class='btn' title='Add attachment'\
  848. style='margin-bottom: 9px; \
  849. padding-left: 6px; padding-right: 6px; margin-left: 6px;'>\
  850. <i class='icon-plus'></i></button>")
  851. .click(function() {
  852. cur_frm.attachments.new_attachment();
  853. })
  854. .appendTo(this.input_area);
  855. }
  856. }
  857. // set as single (to be called from report builder)
  858. this.set_as_single = function() {
  859. var i = this.input;
  860. i.multiple = false;
  861. i.style.height = null;
  862. if(i.lab)$dh(i.lab)
  863. }
  864. // refresh options list
  865. this.refresh_options = function(options) {
  866. if(options)
  867. me.df.options = options;
  868. if(this.file_attach)
  869. this.set_attach_options();
  870. if(typeof me.df.options=="object")
  871. me.options_list = me.df.options || [""];
  872. else
  873. me.options_list = me.df.options?me.df.options.split('\n'):[''];
  874. // add options
  875. if(me.in_filter && me.options_list[0]!='') {
  876. me.options_list = add_lists([''], me.options_list);
  877. }
  878. $(this.input).empty().add_options(me.options_list);
  879. }
  880. // refresh options
  881. this.onrefresh = function() {
  882. this.refresh_options();
  883. if(this.not_in_form) {
  884. this.input.value = '';
  885. return;
  886. }
  887. if(_f.get_value)
  888. var v = _f.get_value(this.doctype,this.docname,this.df.fieldname);
  889. else {
  890. if(this.options_list && this.options_list.length)
  891. var v = this.options_list[0];
  892. else
  893. var v = null;
  894. }
  895. this.input.set_input(v);
  896. }
  897. this.input.set_input=function(v) {
  898. if(!v) {
  899. if(!me.input.multiple) {
  900. if(me.docname) { // if called from onload without docname being set on fields
  901. if(me.options_list && me.options_list.length) {
  902. me.set(me.options_list[0]);
  903. me.input.value = me.options_list[0];
  904. } else {
  905. me.input.value = '';
  906. }
  907. }
  908. }
  909. } else {
  910. if(me.options_list) {
  911. if(me.input.multiple) {
  912. for(var i=0; i<me.input.options.length; i++) {
  913. me.input.options[i].selected = 0;
  914. if(me.input.options[i].value && inList(typeof(v)=='string'?v.split(","):v, me.input.options[i].value))
  915. me.input.options[i].selected = 1;
  916. }
  917. } else if(in_list(me.options_list, v)){
  918. me.input.value = v;
  919. }
  920. }
  921. }
  922. }
  923. this.get_value= function() {
  924. if(me.input.multiple) {
  925. var l = [];
  926. for(var i=0;i<me.input.options.length; i++ ) {
  927. if(me.input.options[i].selected)l[l.length] = me.input.options[i].value;
  928. }
  929. return l;
  930. } else {
  931. if(me.input.options) {
  932. var val = sel_val(me.input);
  933. if(!val && !me.input.selectedIndex)
  934. val = me.input.options[0].value;
  935. return val;
  936. }
  937. return me.input.value;
  938. }
  939. }
  940. this.set_attach_options = function() {
  941. if(!cur_frm) return;
  942. var fl = cur_frm.doc.file_list;
  943. if(fl) {
  944. this.df.options = '';
  945. var fl = fl.split('\n');
  946. for(var i in fl) {
  947. this.df.options += '\n' + fl[i].split(',')[0];
  948. this.set_description("");
  949. }
  950. } else {
  951. this.df.options = ''
  952. this.set_description(wn._("Please attach a file first."))
  953. }
  954. }
  955. this.refresh();
  956. }
  957. // Time
  958. // ======================================================================================
  959. function TimeField() { } TimeField.prototype = new Field();
  960. TimeField.prototype.get_time = function() {
  961. return time_to_hhmm(sel_val(this.input_hr), sel_val(this.input_mn), sel_val(this.input_am));
  962. }
  963. TimeField.prototype.set_time = function(v) {
  964. //show_alert(ret);
  965. ret = time_to_ampm(v);
  966. this.input_hr.inp.value = ret[0];
  967. this.input_mn.inp.value = ret[1];
  968. this.input_am.inp.value = ret[2];
  969. }
  970. TimeField.prototype.set_style_mandatory = function() { }
  971. TimeField.prototype.make_input = function() { var me = this;
  972. this.input = $a(this.input_area, 'div', 'time_field');
  973. var t = make_table(this.input, 1, 3, '200px');
  974. var opt_hr = ['1','2','3','4','5','6','7','8','9','10','11','12'];
  975. var opt_mn = ['00','05','10','15','20','25','30','35','40','45','50','55'];
  976. var opt_am = ['AM','PM'];
  977. this.input_hr = new SelectWidget($td(t,0,0), opt_hr, '50px');
  978. this.input_mn = new SelectWidget($td(t,0,1), opt_mn, '50px');
  979. this.input_am = new SelectWidget($td(t,0,2), opt_am, '50px');
  980. var onchange_fn = function() {
  981. me.set(me.get_time());
  982. me.run_trigger();
  983. }
  984. this.input_hr.inp.onchange = onchange_fn;
  985. this.input_mn.inp.onchange = onchange_fn;
  986. this.input_am.inp.onchange = onchange_fn;
  987. this.onrefresh = function() {
  988. var v = _f.get_value ? _f.get_value(me.doctype,me.docname,me.df.fieldname) : null;
  989. me.set_time(v);
  990. if(!v)
  991. me.set(me.get_time());
  992. }
  993. this.input.set_input=function(v) {
  994. if(v==null)v='';
  995. me.set_time(v);
  996. }
  997. this.get_value = function() {
  998. return this.get_time();
  999. }
  1000. this.refresh();
  1001. }
  1002. TimeField.prototype.set_disp=function(v) {
  1003. var t = time_to_ampm(v);
  1004. var t = t[0]+':'+t[1]+' '+t[2];
  1005. this.set_disp_html(t);
  1006. }
  1007. var tmpid = 0;
  1008. // ======================================================================================
  1009. _f.ButtonField = function() { };
  1010. _f.ButtonField.prototype = new Field();
  1011. _f.ButtonField.prototype.with_label = 0;
  1012. _f.ButtonField.prototype.init = function() {
  1013. this.prev_button = null;
  1014. // if previous field is a button, add it to the same div!
  1015. // button-set structure
  1016. // + wrapper (1st button)
  1017. // + input_area
  1018. // + button_area
  1019. // + button_area
  1020. // + button_area
  1021. if(!this.frm) return;
  1022. if(cur_frm &&
  1023. cur_frm.fields[cur_frm.fields.length-1] &&
  1024. cur_frm.fields[cur_frm.fields.length-1].df.fieldtype=='Button') {
  1025. this.make_body = function() {
  1026. this.prev_button = cur_frm.fields[cur_frm.fields.length-1];
  1027. if(!this.prev_button.prev_button) {
  1028. // first button, make the button area
  1029. this.prev_button.button_area = $a(this.prev_button.input_area, 'span');
  1030. }
  1031. this.wrapper = this.prev_button.wrapper;
  1032. this.input_area = this.prev_button.input_area;
  1033. this.disp_area = this.prev_button.disp_area;
  1034. // all buttons in the same input_area
  1035. this.button_area = $a(this.prev_button.input_area, 'span');
  1036. }
  1037. }
  1038. }
  1039. _f.ButtonField.prototype.make_input = function() { var me = this;
  1040. if(!this.prev_button) {
  1041. $y(this.input_area,{marginTop:'4px', marginBottom: '4px'});
  1042. }
  1043. // make a button area for one button
  1044. if(!this.button_area)
  1045. this.button_area = $a(this.input_area, 'span','',{marginRight:'4px'});
  1046. // make the input
  1047. this.input = $btn(this.button_area,
  1048. me.df.label, null,
  1049. {fontWeight:'bold'}, null, 1)
  1050. $(this.input).click(function() {
  1051. if(me.not_in_form) return;
  1052. if(cur_frm.cscript[me.df.fieldname] && (!me.in_filter)) {
  1053. cur_frm.runclientscript(me.df.fieldname, me.doctype, me.docname);
  1054. } else {
  1055. cur_frm.runscript(me.df.options, me);
  1056. }
  1057. });
  1058. }
  1059. _f.ButtonField.prototype.hide = function() {
  1060. $dh(this.button_area);
  1061. };
  1062. _f.ButtonField.prototype.show = function() {
  1063. $ds(this.button_area);
  1064. };
  1065. _f.ButtonField.prototype.set = function(v) { }; // No Setter
  1066. _f.ButtonField.prototype.set_disp = function(val) { } // No Disp on readonly
  1067. // ======================================================================================
  1068. function make_field(docfield, doctype, parent, frm, in_grid, hide_label) { // Factory
  1069. switch(docfield.fieldtype.toLowerCase()) {
  1070. // general fields
  1071. case 'data':var f = new DataField(); break;
  1072. case 'password':var f = new DataField(); break;
  1073. case 'int':var f = new IntField(); break;
  1074. case 'float':var f = new FloatField(); break;
  1075. case 'currency':var f = new CurrencyField(); break;
  1076. case 'read only':var f = new ReadOnlyField(); break;
  1077. case 'link':var f = new LinkField(); break;
  1078. case 'date':var f = new DateField(); break;
  1079. case 'time':var f = new TimeField(); break;
  1080. case 'html':var f = new HTMLField(); break;
  1081. case 'check':var f = new CheckField(); break;
  1082. case 'text':var f = new TextField(); break;
  1083. case 'small text':var f = new TextField(); break;
  1084. case 'select':var f = new SelectField(); break;
  1085. case 'button':var f = new _f.ButtonField(); break;
  1086. // form fields
  1087. case 'code':var f = new _f.CodeField(); break;
  1088. case 'text editor':var f = new _f.CodeField(); break;
  1089. case 'table':var f = new _f.TableField(); break;
  1090. case 'section break':var f= new _f.SectionBreak(); break;
  1091. case 'column break':var f= new _f.ColumnBreak(); break;
  1092. case 'image':var f= new _f.ImageField(); break;
  1093. }
  1094. f.parent = parent;
  1095. f.doctype = doctype;
  1096. f.df = docfield;
  1097. f.perm = frm ? frm.perm : [[1,1,1]];
  1098. if(_f)
  1099. f.col_break_width = _f.cur_col_break_width;
  1100. if(in_grid) {
  1101. f.in_grid = true;
  1102. f.with_label = 0;
  1103. }
  1104. if(hide_label) {
  1105. f.with_label = 0;
  1106. }
  1107. if(frm) {
  1108. f.frm = frm;
  1109. if(parent)
  1110. f.layout_cell = parent.parentNode;
  1111. }
  1112. if(f.init) f.init();
  1113. f.make_body();
  1114. return f;
  1115. }