Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

1365 řádky
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. if(this.df.placeholder) $(this.input).attr("placeholder", this.df.placeholder);
  345. this.get_value= function() {
  346. var v = this.input.value;
  347. if(this.validate)
  348. v = this.validate(v);
  349. return v;
  350. }
  351. this.input.name = this.df.fieldname;
  352. $(this.input).blur(function() {
  353. me.set_value(me.get_value ? me.get_value() : $(this.input).val());
  354. });
  355. this.set_value = function(val) {
  356. if(!me.last_value)me.last_value='';
  357. if(me.validate) {
  358. val = me.validate(val);
  359. if(me.last_value === val) return;
  360. me.input.value = (val==undefined) ? '' : val;
  361. } else if(me.last_value === val) { return; }
  362. me.set(val);
  363. if(me.format_input)
  364. me.format_input();
  365. if(in_list(['Currency','Float','Int'], me.df.fieldtype)) {
  366. if(flt(me.last_value)==flt(val)) {
  367. me.last_value = val;
  368. return; // do not run trigger
  369. }
  370. }
  371. me.last_value = val;
  372. me.run_trigger();
  373. }
  374. this.input.set_input = function(val) {
  375. if(val==null)val='';
  376. me.input.value = val;
  377. if(me.format_input)me.format_input();
  378. }
  379. }
  380. DataField.prototype.validate = function(v) {
  381. if(this.df.options == 'Phone') {
  382. if(v+''=='')return '';
  383. v1 = ''
  384. // phone may start with + and must only have numbers later, '-' and ' ' are stripped
  385. v = v.replace(/ /g, '').replace(/-/g, '').replace(/\(/g, '').replace(/\)/g, '');
  386. // allow initial +,0,00
  387. if(v && v.substr(0,1)=='+') {
  388. v1 = '+'; v = v.substr(1);
  389. }
  390. if(v && v.substr(0,2)=='00') {
  391. v1 += '00'; v = v.substr(2);
  392. }
  393. if(v && v.substr(0,1)=='0') {
  394. v1 += '0'; v = v.substr(1);
  395. }
  396. v1 += cint(v) + '';
  397. return v1;
  398. } else if(this.df.options == 'Email') {
  399. if(v+''=='')return '';
  400. if(!validate_email(v)) {
  401. msgprint(this.df.label + ': ' + v + ' is not a valid email id');
  402. return '';
  403. } else
  404. return v;
  405. } else {
  406. return v;
  407. }
  408. }
  409. // ======================================================================================
  410. function ReadOnlyField() { }
  411. ReadOnlyField.prototype = new Field();
  412. // ======================================================================================
  413. function HTMLField() { }
  414. HTMLField.prototype = new Field();
  415. HTMLField.prototype.with_label = 0;
  416. HTMLField.prototype.set_disp = function(val) { if(this.disp_area) this.disp_area.innerHTML = val; }
  417. HTMLField.prototype.set_input = function(val) { if(val) this.set_disp(val); }
  418. HTMLField.prototype.onrefresh = function() { if(this.df.options) this.set_disp(this.df.options); }
  419. // ======================================================================================
  420. var datepicker_active = 0;
  421. function get_datepicker_options() {
  422. var datepicker_options = {
  423. dateFormat: (sys_defaults.date_format || 'yy-mm-dd').replace('yyyy','yy'),
  424. altFormat:'yy-mm-dd',
  425. changeYear: true,
  426. yearRange: "-70Y:+10Y",
  427. beforeShow: function(input, inst) {
  428. datepicker_active = 1
  429. },
  430. onClose: function(dateText, inst) {
  431. datepicker_active = 0;
  432. if(_f.cur_grid_cell)
  433. _f.cur_grid_cell.grid.cell_deselect();
  434. },
  435. }
  436. return datepicker_options;
  437. }
  438. function DateField() { } DateField.prototype = new Field();
  439. DateField.prototype.make_input = function() {
  440. var me = this;
  441. this.input = $("<input type='text' data-fieldtype='Date'>")
  442. .appendTo(this.input_area).get(0);
  443. $(this.input).datepicker(get_datepicker_options());
  444. this.setup_input();
  445. }
  446. DateField.prototype.setup_input = function() {
  447. var me = this;
  448. me.input.onchange = function() {
  449. // input as dd-mm-yyyy
  450. if(this.value==null)this.value='';
  451. if(!this.not_in_form)
  452. me.set(dateutil.user_to_str(me.input.value));
  453. me.run_trigger();
  454. }
  455. me.input.set_input = function(val) {
  456. if(val==null)val='';
  457. else val=dateutil.str_to_user(val);
  458. me.input.value = val;
  459. }
  460. me.get_value = function() {
  461. if(me.input.value)
  462. return dateutil.user_to_str(me.input.value);
  463. }
  464. }
  465. DateField.prototype.set_disp = function(val) {
  466. var v = dateutil.str_to_user(val);
  467. if(v==null)v = '';
  468. this.set_disp_html(v);
  469. }
  470. DateField.prototype.validate = function(v) {
  471. var v = wn.datetime.validate(v);
  472. if(!v) {
  473. msgprint (wn._("Date must be in format") + ": " + (sys_defaults.date_format || "yyyy-mm-dd"));
  474. this.input.set_input('');
  475. return '';
  476. }
  477. return v;
  478. };
  479. // reference when a new record is created via link
  480. function LinkField() { } LinkField.prototype = new Field();
  481. LinkField.prototype.make_input = function() {
  482. var me = this;
  483. if(me.df.no_buttons) {
  484. this.txt = $("<input type='text'>")
  485. .appendTo(this.input_area).get(0);
  486. this.input = this.txt;
  487. } else {
  488. me.input = me.input_area;
  489. $(me.input_area).addClass("input-append link-field");
  490. me.txt = $('<input type="text" style="margin-right: 0px;">')
  491. .css({"width": me.in_filter ? "100px" : (me.in_grid ? "35%" : "60%")})
  492. .appendTo(me.input_area).get(0);
  493. me.btn = $('<button class="btn" title="'+wn._('Search Link')+'">\
  494. <i class="icon-search"></i></button>').appendTo(me.input_area).get(0);
  495. me.btn1 = $('<button class="btn" title="'+wn._('Open Link')+'">\
  496. <i class="icon-play"></i></button>').appendTo(me.input_area).get(0);
  497. me.btn2 = $('<button class="btn" title="'+wn._('Make New')+'">\
  498. <i class="icon-plus"></i></button>').appendTo(me.input_area).get(0);
  499. me.txt.name = me.df.fieldname;
  500. me.setdisabled = function(tf) { me.txt.disabled = tf; }
  501. // setup buttons
  502. me.setup_buttons();
  503. }
  504. me.onrefresh = function() {
  505. var can_create = in_list(wn.boot.profile.can_create, me.df.options);
  506. var can_read = in_list(wn.boot.profile.can_read, me.df.options);
  507. if(!can_create) $(this.btn2).remove();
  508. if(!can_read) $(this.btn1).remove();
  509. }
  510. me.onrefresh();
  511. me.txt.field_object = this;
  512. // set onchange triggers
  513. me.input.set_input = function(val) {
  514. if(val==undefined)val='';
  515. me.txt.value = val;
  516. }
  517. me.get_value = function() { return me.txt.value; }
  518. // increasing zindex of input to increase zindex of autosuggest
  519. // because of the increase in zindex of dialog_wrapper
  520. if(cur_dialog || me.dialog_wrapper) {
  521. var $dialog_wrapper = $(cur_dialog ? cur_dialog.wrapper : me.dialog_wrapper)
  522. var zindex = cint($dialog_wrapper.css("z-index"));
  523. $(me.txt).css({"z-index": (zindex >= 10 ? zindex : 10) + 1});
  524. }
  525. $(me.txt).autocomplete({
  526. source: function(request, response) {
  527. var args = {
  528. 'txt': request.term,
  529. 'dt': me.df.options,
  530. };
  531. var q = me.get_custom_query();
  532. if (typeof(q)==="string") {
  533. args.query = q;
  534. } else if($.isPlainObject(q)) {
  535. if(q.filters) {
  536. $.each(q.filters, function(key, value) {
  537. q.filters[key] = value===undefined ? null : value;
  538. });
  539. }
  540. $.extend(args, q);
  541. }
  542. wn.call({
  543. method:'webnotes.widgets.search.search_link',
  544. args: args,
  545. callback: function(r) {
  546. response(r.results);
  547. },
  548. });
  549. },
  550. select: function(event, ui) {
  551. me.set_input_value(ui.item.value);
  552. }
  553. }).data('autocomplete')._renderItem = function(ul, item) {
  554. return $('<li></li>')
  555. .data('item.autocomplete', item)
  556. .append(repl('<a><span style="font-weight: bold;">%(label)s</span><br>\
  557. <span style="font-size:10px;">%(info)s</span></a>',
  558. item))
  559. .appendTo(ul);
  560. };
  561. $(this.txt).change(function() {
  562. var val = $(this).val();//me.get_value();
  563. me.set_input_value_executed = false;
  564. if(!val) {
  565. if(selector && selector.display)
  566. return;
  567. me.set_input_value('');
  568. } else {
  569. // SetTimeout hack! if in put is set via autocomplete, do not validate twice
  570. setTimeout(function() {
  571. if (!me.set_input_value_executed) {
  572. me.set_input_value(val);
  573. }
  574. }, 1000);
  575. }
  576. })
  577. }
  578. LinkField.prototype.get_custom_query = function() {
  579. this.set_get_query();
  580. if(this.get_query) {
  581. if(cur_frm)
  582. var doc = locals[cur_frm.doctype][cur_frm.docname];
  583. return this.get_query(doc, this.doctype, this.docname);
  584. }
  585. }
  586. LinkField.prototype.setup_buttons = function() {
  587. var me = this;
  588. // magnifier - search
  589. me.btn.onclick = function() {
  590. selector.set(me, me.df.options, me.df.label);
  591. selector.show(me.txt);
  592. }
  593. // open
  594. if(me.btn1)me.btn1.onclick = function() {
  595. if(me.txt.value && me.df.options) { loaddoc(me.df.options, me.txt.value); }
  596. }
  597. // add button - for inline creation of records
  598. me.can_create = 0;
  599. if((!me.not_in_form) && in_list(profile.can_create, me.df.options)) {
  600. me.can_create = 1;
  601. me.btn2.onclick = function() {
  602. var on_save_callback = function(new_rec) {
  603. if(new_rec) {
  604. var d = _f.calling_doc_stack.pop(); // patch for composites
  605. locals[d[0]][d[1]][me.df.fieldname] = new_rec;
  606. me.refresh();
  607. if(me.grid)me.grid.refresh();
  608. // call script
  609. me.run_trigger();
  610. }
  611. }
  612. _f.calling_doc_stack.push([me.doctype, me.docname]);
  613. new_doc(me.df.options);
  614. }
  615. } else {
  616. $(me.btn2).remove();
  617. }
  618. }
  619. LinkField.prototype.set_input_value = function(val) {
  620. var me = this;
  621. // SetTimeout hack! if in put is set via autocomplete, do not validate twice
  622. me.set_input_value_executed = true;
  623. var from_selector = false;
  624. if(selector && selector.display) from_selector = true;
  625. // refresh mandatory style
  626. me.refresh_label_icon();
  627. // not in form, do nothing
  628. if(me.not_in_form) {
  629. $(this.txt).val(val);
  630. return;
  631. }
  632. // same value, do nothing
  633. if(cur_frm) {
  634. if(val == locals[me.doctype][me.docname][me.df.fieldname]) {
  635. //me.set(val); // one more time, grid bug?
  636. me.run_trigger(); // wanted - called as refresh?
  637. return;
  638. }
  639. }
  640. // set in locals
  641. me.set(val);
  642. // deselect cell if in grid
  643. if(_f.cur_grid_cell)
  644. _f.cur_grid_cell.grid.cell_deselect();
  645. if(val) {
  646. // validate only if val is not empty
  647. me.validate_link(val, from_selector);
  648. } else {
  649. // run trigger if value is cleared
  650. me.run_trigger();
  651. }
  652. }
  653. LinkField.prototype.validate_link = function(val, from_selector) {
  654. // validate the value just entered
  655. var me = this;
  656. if(this.df.options=="[Select]") {
  657. $(me.txt).val(val);
  658. me.run_trigger();
  659. return;
  660. }
  661. var fetch = '';
  662. if(cur_frm.fetch_dict[me.df.fieldname])
  663. fetch = cur_frm.fetch_dict[me.df.fieldname].columns.join(', ');
  664. $c('webnotes.widgets.form.utils.validate_link', {
  665. 'value':val,
  666. 'options':me.df.options,
  667. 'fetch': fetch
  668. },
  669. function(r,rt) {
  670. if(r.message=='Ok') {
  671. // set fetch values
  672. if($(me.txt).val()!=val) {
  673. if((me.grid && !from_selector) || (!me.grid)) {
  674. $(me.txt).val(val);
  675. }
  676. }
  677. if(r.fetch_values)
  678. me.set_fetch_values(r.fetch_values);
  679. me.run_trigger();
  680. } else {
  681. me.txt.value = '';
  682. me.set('');
  683. }
  684. }
  685. );
  686. }
  687. LinkField.prototype.set_fetch_values = function(fetch_values) {
  688. var fl = cur_frm.fetch_dict[this.df.fieldname].fields;
  689. var changed_fields = [];
  690. for(var i=0; i< fl.length; i++) {
  691. if(locals[this.doctype][this.docname][fl[i]]!=fetch_values[i]) {
  692. locals[this.doctype][this.docname][fl[i]] = fetch_values[i];
  693. if(!this.grid) {
  694. refresh_field(fl[i]);
  695. // call trigger on the target field
  696. changed_fields.push(fl[i]);
  697. }
  698. }
  699. }
  700. // run triggers
  701. for(i=0; i<changed_fields.length; i++) {
  702. if(cur_frm.fields_dict[changed_fields[i]]) // on main
  703. cur_frm.fields_dict[changed_fields[i]].run_trigger();
  704. }
  705. // refresh grid
  706. if(this.grid) this.grid.refresh();
  707. }
  708. LinkField.prototype.set_get_query = function() {
  709. if(this.get_query)return;
  710. if(this.grid) {
  711. var f = this.grid.get_field(this.df.fieldname);
  712. if(f.get_query) this.get_query = f.get_query;
  713. }
  714. }
  715. LinkField.prototype.set_disp = function(val) {
  716. var t = null;
  717. if(val)t = "<a href=\'javascript:loaddoc(\""+this.df.options+"\", \""+val+"\")\'>"+val+"</a>";
  718. this.set_disp_html(t);
  719. }
  720. // ======================================================================================
  721. function IntField() { } IntField.prototype = new DataField();
  722. IntField.prototype.validate = function(v) {
  723. if(isNaN(parseInt(v)))return null;
  724. return cint(v);
  725. };
  726. IntField.prototype.format_input = function() {
  727. if(this.input.value==null) this.input.value='';
  728. }
  729. // ======================================================================================
  730. function FloatField() { } FloatField.prototype = new DataField();
  731. FloatField.prototype.validate = function(v) {
  732. if(isNaN(parseFloat(v)))
  733. return null;
  734. else
  735. v = flt(v);
  736. return v;
  737. };
  738. FloatField.prototype.format_input = function() {
  739. if(this.input.value==null || this.input.value=='')
  740. this.input.value='';
  741. else {
  742. var format;
  743. if(this.get_field_currency) {
  744. format = get_number_format(this.get_field_currency());
  745. this.input.value =
  746. format_number(parseFloat(this.input.value), format);
  747. } else {
  748. var decimals = wn.boot.sysdefaults.float_precision ?
  749. parseInt(wn.boot.sysdefaults.float_precision) : null;
  750. this.input.value = format_number(parseFloat(this.input.value), null, decimals);
  751. }
  752. }
  753. }
  754. FloatField.prototype.onmake_input = function() {
  755. if(!this.input) return;
  756. this.input.onfocus = function() {
  757. this.select();
  758. }
  759. }
  760. FloatField.prototype.set_disp = function(val) {
  761. this.set_disp_html(wn.format(val, this.df, null, locals[this.doctype][this.name]));
  762. }
  763. function PercentField() { } PercentField.prototype = new FloatField();
  764. PercentField.prototype.set_disp = function(val) {
  765. this.set_disp_html(wn.format(val, this.df));
  766. }
  767. function CurrencyField() { } CurrencyField.prototype = new FloatField();
  768. CurrencyField.prototype.validate = function(v) {
  769. if(v==null || v=='')
  770. return 0;
  771. return flt(v, null, get_number_format(this.get_field_currency()));
  772. }
  773. CurrencyField.prototype.get_field_currency = function() {
  774. var doc = null;
  775. if(this.doctype && this.docname && locals[this.doctype])
  776. doc = locals[this.doctype][this.docname];
  777. return wn.meta.get_field_currency(this.df, doc);
  778. };
  779. CurrencyField.prototype.get_formatted = function(val) {
  780. if(this.not_in_form)
  781. return val;
  782. return format_currency(val, this.get_field_currency());
  783. }
  784. CurrencyField.prototype.set_disp = function(val) {
  785. this.set_disp_html(this.get_formatted(val));
  786. }
  787. function CheckField() { } CheckField.prototype = new Field();
  788. CheckField.prototype.validate = function(v) {
  789. return cint(v);
  790. };
  791. CheckField.prototype.onmake = function() {
  792. this.checkimg = $("<i class='icon-check'></i>").appendTo(this.disp_area);
  793. }
  794. CheckField.prototype.make_input = function() { var me = this;
  795. this.input = $("<input type='checkbox'>")
  796. .appendTo(this.input_area)
  797. .css({"border":"0px", "margin-top":"-2px", "width": "16px"}).get(0);
  798. $(this.input).change(function() {
  799. me.set(this.checked ? 1 : 0);
  800. me.run_trigger();
  801. })
  802. this.input.set_input = function(v) {
  803. me.input.checked = cint(v) ? true : false;
  804. }
  805. this.get_value= function() {
  806. return this.input.checked ? 1 : 0;
  807. }
  808. }
  809. CheckField.prototype.set_disp = function(val) {
  810. this.checkimg.toggle(cint(val) ? true : false);
  811. }
  812. function TextField() { } TextField.prototype = new Field();
  813. TextField.prototype.set_disp = function(val) {
  814. this.disp_area.innerHTML = replace_newlines(val);
  815. }
  816. TextField.prototype.make_input = function() {
  817. var me = this;
  818. if(this.in_grid)
  819. return; // do nothing, text dialog will take over
  820. this.input = $a(this.input_area, 'textarea');
  821. if(this.df.fieldtype=='Small Text') {
  822. $(this.input).css({height: "80px"});
  823. } else if(this.df.width) {
  824. $(this.input).css({height: cint(this.df.width) + "px"});
  825. } else {
  826. $(this.input).css({height: "160px"});
  827. }
  828. this.input.set_input = function(v) {
  829. me.input.value = (v==null ? "" : v);
  830. }
  831. this.input.onchange = function() {
  832. me.set(me.input.value);
  833. me.run_trigger();
  834. }
  835. this.get_value= function() {
  836. return this.input.value;
  837. }
  838. }
  839. // text dialog
  840. var text_dialog;
  841. function make_text_dialog() {
  842. var d = new Dialog(520,410,'Edit Text');
  843. d.make_body([
  844. ['Text', 'Enter Text'],
  845. ['HTML', 'Description'],
  846. ['Button', 'Update']
  847. ]);
  848. d.widgets['Update'].onclick = function() {
  849. var t = this.dialog;
  850. t.field.set(t.widgets['Enter Text'].value);
  851. t.hide();
  852. }
  853. d.onshow = function() {
  854. this.widgets['Enter Text'].style.height = '300px';
  855. var v = _f.get_value(this.field.doctype,this.field.docname,this.field.df.fieldname);
  856. this.widgets['Enter Text'].value = v==null?'':v;
  857. this.widgets['Enter Text'].focus();
  858. this.widgets['Description'].innerHTML = ''
  859. if(this.field.df.description)
  860. $a(this.widgets['Description'], 'div', 'help small', '', this.field.df.description);
  861. }
  862. d.onhide = function() {
  863. if(_f.cur_grid_cell)
  864. _f.cur_grid_cell.grid.cell_deselect();
  865. }
  866. text_dialog = d;
  867. }
  868. TextField.prototype.table_refresh = function() {
  869. if(!this.text_dialog)
  870. make_text_dialog();
  871. text_dialog.set_title(wn._('Enter text for')+': "'+ wn._(this.df.label) +'"');
  872. text_dialog.field = this;
  873. text_dialog.show();
  874. }
  875. // Select
  876. // ======================================================================================
  877. function SelectField() { } SelectField.prototype = new Field();
  878. SelectField.prototype.make_input = function() {
  879. var me = this;
  880. var opt=[];
  881. if(this.in_filter && (!this.df.single_select)) {
  882. // multiple select
  883. this.input = $a(this.input_area, 'select');
  884. this.input.multiple = true;
  885. this.input.style.height = '4em';
  886. this.input.lab = $a(this.input_area, 'div', {fontSize:'9px',color:'#999'});
  887. this.input.lab.innerHTML = '(Use Ctrl+Click to select multiple or de-select)'
  888. } else {
  889. // Single select
  890. this.input = $a(this.input_area, 'select');
  891. this.input.onchange = function() {
  892. if(me.validate)
  893. me.validate();
  894. me.set(sel_val(this));
  895. me.run_trigger();
  896. }
  897. if(this.df.options == 'attach_files:') {
  898. this.attach_files = true;
  899. $(this.input).css({"width": "70%"});
  900. $("<button class='btn' title='Add attachment'\
  901. style='margin-bottom: 9px; \
  902. padding-left: 6px; padding-right: 6px; margin-left: 6px;'>\
  903. <i class='icon-plus'></i></button>")
  904. .click(function() {
  905. cur_frm.attachments.new_attachment();
  906. })
  907. .appendTo(this.input_area);
  908. }
  909. }
  910. // set as single (to be called from report builder)
  911. this.set_as_single = function() {
  912. var i = this.input;
  913. i.multiple = false;
  914. i.style.height = null;
  915. if(i.lab)$dh(i.lab)
  916. }
  917. // refresh options list
  918. this.refresh_options = function(options) {
  919. if(options)
  920. me.df.options = options;
  921. if(this.attach_files)
  922. this.set_attach_options();
  923. if(typeof me.df.options=="object")
  924. me.options_list = me.df.options || [""];
  925. else
  926. me.options_list = me.df.options?me.df.options.split('\n'):[''];
  927. // add options
  928. if(me.in_filter && me.options_list[0]!='') {
  929. me.options_list = add_lists([''], me.options_list);
  930. }
  931. $(this.input).empty().add_options(me.options_list);
  932. }
  933. // refresh options
  934. this.onrefresh = function() {
  935. this.refresh_options();
  936. if(this.not_in_form) {
  937. this.input.value = '';
  938. return;
  939. }
  940. if(_f.get_value)
  941. var v = _f.get_value(this.doctype,this.docname,this.df.fieldname);
  942. else {
  943. if(this.options_list && this.options_list.length)
  944. var v = this.options_list[0];
  945. else
  946. var v = null;
  947. }
  948. this.input.set_input(v);
  949. }
  950. var _set_value = function(value) {
  951. // use option's value if dict, else use string for comparison and setting
  952. for(var i in (me.options_list || [""])) {
  953. var option = me.options_list[i];
  954. if($.isPlainObject(option)){
  955. option = option.value;
  956. }
  957. if(option === value) {
  958. me.input.value = value;
  959. break;
  960. }
  961. }
  962. }
  963. this.input.set_input=function(v) {
  964. if(!v) {
  965. if(!me.input.multiple) {
  966. if(me.docname) { // if called from onload without docname being set on fields
  967. _set_value(v);
  968. me.set(me.get_value());
  969. }
  970. }
  971. } else {
  972. if(me.options_list) {
  973. if(me.input.multiple) {
  974. for(var i=0; i<me.input.options.length; i++) {
  975. me.input.options[i].selected = 0;
  976. if(me.input.options[i].value && inList(typeof(v)=='string'?v.split(","):v, me.input.options[i].value))
  977. me.input.options[i].selected = 1;
  978. }
  979. } else {
  980. _set_value(v);
  981. }
  982. }
  983. }
  984. }
  985. this.get_value= function() {
  986. if(me.input.multiple) {
  987. var l = [];
  988. for(var i=0;i<me.input.options.length; i++ ) {
  989. if(me.input.options[i].selected)l[l.length] = me.input.options[i].value;
  990. }
  991. return l;
  992. } else {
  993. if(me.input.options) {
  994. var val = sel_val(me.input);
  995. if(!val && !me.input.selectedIndex)
  996. val = me.input.options[0].value;
  997. return val;
  998. }
  999. return me.input.value;
  1000. }
  1001. }
  1002. this.set_attach_options = function() {
  1003. if(!cur_frm) return;
  1004. var fl = cur_frm.doc.file_list;
  1005. if(fl) {
  1006. this.df.options = '';
  1007. var fl = fl.split('\n');
  1008. for(var i in fl) {
  1009. var fname = fl[i].split(',')[0];
  1010. if(fname.substr(0,4)!="http")
  1011. fname = "files/" + fname;
  1012. this.df.options += '\n' + fname;
  1013. }
  1014. this.set_description("");
  1015. } else {
  1016. this.df.options = ''
  1017. this.set_description(wn._("Please attach a file first."))
  1018. }
  1019. }
  1020. this.refresh();
  1021. }
  1022. function TimeField() { } TimeField.prototype = new DataField();
  1023. function import_timepicker() {
  1024. wn.require("lib/js/lib/jquery/jquery.ui.slider.min.js");
  1025. wn.require("lib/js/lib/jquery/jquery.ui.sliderAccess.js");
  1026. wn.require("lib/js/lib/jquery/jquery.ui.timepicker-addon.css");
  1027. wn.require("lib/js/lib/jquery/jquery.ui.timepicker-addon.js");
  1028. }
  1029. TimeField.prototype.make_input = function() {
  1030. import_timepicker();
  1031. var me = this;
  1032. this.input = $('<input type="text">')
  1033. .appendTo(this.input_area)
  1034. .timepicker({
  1035. timeFormat: 'hh:mm:ss',
  1036. }).get(0);
  1037. this.input.set_input = function(v) {
  1038. $(me.input).val(v);
  1039. };
  1040. this.input.onchange = function() {
  1041. if(!this.not_in_form)
  1042. me.set(me.input.value);
  1043. me.run_trigger();
  1044. };
  1045. }
  1046. function DateTimeField() { } DateTimeField.prototype = new DateField();
  1047. DateTimeField.prototype.make_input = function() {
  1048. import_timepicker();
  1049. var me = this;
  1050. args = get_datepicker_options();
  1051. args.timeFormat = "hh:mm:ss";
  1052. this.input = $('<input type="text" data-fieldtype="Datetime">')
  1053. .appendTo(this.input_area)
  1054. .datetimepicker(args).get(0);
  1055. this.setup_input();
  1056. }
  1057. var tmpid = 0;
  1058. _f.ButtonField = function() { };
  1059. _f.ButtonField.prototype = new Field();
  1060. _f.ButtonField.prototype.with_label = 0;
  1061. _f.ButtonField.prototype.make_input = function() { var me = this;
  1062. // make a button area for one button
  1063. if(!this.button_area)
  1064. this.button_area = $a(this.input_area, 'div','',{
  1065. marginBottom:'4px'});
  1066. // make the input
  1067. this.input = $btn(this.button_area,
  1068. me.df.label, null,
  1069. {fontWeight:'bold'}, null, 1)
  1070. $(this.input).click(function() {
  1071. if(me.not_in_form) return;
  1072. if(cur_frm.cscript[me.df.fieldname] && (!me.in_filter)) {
  1073. cur_frm.runclientscript(me.df.fieldname, me.doctype, me.docname);
  1074. } else {
  1075. cur_frm.runscript(me.df.options, me);
  1076. }
  1077. });
  1078. }
  1079. _f.ButtonField.prototype.hide = function() {
  1080. $dh(this.button_area);
  1081. };
  1082. _f.ButtonField.prototype.show = function() {
  1083. $ds(this.button_area);
  1084. };
  1085. _f.ButtonField.prototype.set = function(v) { }; // No Setter
  1086. _f.ButtonField.prototype.set_disp = function(val) { } // No Disp on readonly
  1087. function make_field(docfield, doctype, parent, frm, in_grid, hide_label) { // Factory
  1088. switch(docfield.fieldtype.toLowerCase()) {
  1089. // general fields
  1090. case 'data':var f = new DataField(); break;
  1091. case 'password':var f = new DataField(); break;
  1092. case 'int':var f = new IntField(); break;
  1093. case 'float':var f = new FloatField(); break;
  1094. case 'currency':var f = new CurrencyField(); break;
  1095. case 'percent':var f = new PercentField(); break;
  1096. case 'read only':var f = new ReadOnlyField(); break;
  1097. case 'link':var f = new LinkField(); break;
  1098. case 'long text': var f = new TextField(); break;
  1099. case 'date':var f = new DateField(); break;
  1100. case 'datetime':var f = new DateTimeField(); break;
  1101. case 'time':var f = new TimeField(); break;
  1102. case 'html':var f = new HTMLField(); break;
  1103. case 'check':var f = new CheckField(); break;
  1104. case 'text':var f = new TextField(); break;
  1105. case 'small text':var f = new TextField(); break;
  1106. case 'select':var f = new SelectField(); break;
  1107. case 'button':var f = new _f.ButtonField(); break;
  1108. // form fields
  1109. case 'code':var f = new _f.CodeField(); break;
  1110. case 'text editor':var f = new _f.CodeField(); break;
  1111. case 'table':var f = new _f.TableField(); break;
  1112. case 'section break':var f= new _f.SectionBreak(); break;
  1113. case 'column break':var f= new _f.ColumnBreak(); break;
  1114. case 'image':var f= new _f.ImageField(); break;
  1115. }
  1116. f.parent = parent;
  1117. f.doctype = doctype;
  1118. f.df = docfield;
  1119. f.perm = frm ? frm.perm : [[1,1,1]];
  1120. if(_f)
  1121. f.col_break_width = _f.cur_col_break_width;
  1122. if(in_grid) {
  1123. f.in_grid = true;
  1124. f.with_label = 0;
  1125. }
  1126. if(hide_label) {
  1127. f.with_label = 0;
  1128. }
  1129. if(frm) {
  1130. f.frm = frm;
  1131. if(parent)
  1132. f.layout_cell = parent.parentNode;
  1133. }
  1134. if(f.init) f.init();
  1135. f.make_body();
  1136. return f;
  1137. }