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.
 
 
 
 
 
 

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