25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

1211 satır
32 KiB

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