Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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