Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

1177 rindas
30 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. /* Form page structure
  23. + this.parent (either FormContainer or Dialog)
  24. + this.wrapper
  25. + this.content
  26. + this.saved_wrapper
  27. + wn.PageLayout (this.page_layout)
  28. + this.wrapper
  29. + this.wtab (table)
  30. + this.main
  31. + this.head
  32. + this.body
  33. + this.layout
  34. + this.footer
  35. + this.sidebar
  36. + this.print_wrapper
  37. + this.head
  38. */
  39. wn.provide('_f');
  40. _f.frms = {};
  41. _f.Frm = function(doctype, parent, in_form) {
  42. this.docname = '';
  43. this.doctype = doctype;
  44. this.display = 0;
  45. var me = this;
  46. this.is_editable = {};
  47. this.opendocs = {};
  48. this.sections = [];
  49. this.grids = [];
  50. this.cscript = {};
  51. this.pformat = {};
  52. this.fetch_dict = {};
  53. this.parent = parent;
  54. this.tinymce_id_list = [];
  55. this.setup_meta(doctype);
  56. // show in form instead of in dialog, when called using url (router.js)
  57. this.in_form = in_form ? true : false;
  58. // notify on rename
  59. var me = this;
  60. $(document).bind('rename', function(event, dt, old_name, new_name) {
  61. //console.log(arguments)
  62. if(dt==me.doctype)
  63. me.rename_notify(dt, old_name, new_name)
  64. });
  65. }
  66. // ======================================================================================
  67. _f.Frm.prototype.check_doctype_conflict = function(docname) {
  68. var me = this;
  69. if(this.doctype=='DocType' && docname=='DocType') {
  70. msgprint('Allowing DocType, DocType. Be careful!')
  71. } else if(this.doctype=='DocType') {
  72. if(wn.views.formview[docname]) {
  73. msgprint("Cannot open DocType when its instance is open")
  74. throw 'doctype open conflict'
  75. }
  76. } else {
  77. if(wn.views.formview.DocType && wn.views.formview.DocType.frm.opendocs[this.doctype]) {
  78. msgprint("Cannot open instance when its DocType is open")
  79. throw 'doctype open conflict'
  80. }
  81. }
  82. }
  83. _f.Frm.prototype.setup = function() {
  84. var me = this;
  85. this.fields = [];
  86. this.fields_dict = {};
  87. // wrapper
  88. this.wrapper = this.parent;
  89. // create area for print fomrat
  90. this.setup_print_layout();
  91. // thank you goes here (in case of Guest, don't refresh, just say thank you!)
  92. this.saved_wrapper = $a(this.wrapper, 'div');
  93. // 2 column layout
  94. this.setup_std_layout();
  95. // client script must be called after "setup" - there are no fields_dict attached to the frm otherwise
  96. this.setup_client_script();
  97. this.setup_done = true;
  98. }
  99. // ======================================================================================
  100. _f.Frm.prototype.setup_print_layout = function() {
  101. this.print_wrapper = $a(this.wrapper, 'div');
  102. this.print_head = $a(this.print_wrapper, 'div');
  103. this.print_body = $a(this.print_wrapper,'div', 'layout_wrapper', {
  104. padding:'23px',
  105. minHeight: '800px'
  106. });
  107. var t= make_table(this.print_head, 1 ,2, '100%', [], {padding: '6px'});
  108. this.view_btn_wrapper = $a($td(t,0,0) , 'span', 'green_buttons');
  109. this.view_btn = $btn(this.view_btn_wrapper, 'View Details', function() { cur_frm.edit_doc() },
  110. {marginRight:'4px'}, 'green');
  111. this.print_btn = $btn($td(t,0,0), 'Print', function() { cur_frm.print_doc() });
  112. $y($td(t,0,1), {textAlign: 'right'});
  113. this.print_close_btn = $btn($td(t,0,1), 'Close', function() { window.history.back(); });
  114. }
  115. _f.Frm.prototype.onhide = function() { if(_f.cur_grid_cell) _f.cur_grid_cell.grid.cell_deselect(); }
  116. // ======================================================================================
  117. _f.Frm.prototype.setup_std_layout = function() {
  118. this.page_layout = new wn.PageLayout({
  119. parent: this.wrapper,
  120. main_width: (this.meta.in_dialog && !this.in_form) ? '100%' : '75%',
  121. sidebar_width: (this.meta.in_dialog && !this.in_form) ? '0%' : '25%'
  122. })
  123. // only tray
  124. this.meta.section_style='Simple'; // always simple!
  125. // layout
  126. this.layout = new Layout(this.page_layout.body, '100%');
  127. // sidebar
  128. if(this.meta.in_dialog && !this.in_form) {
  129. // hide sidebar
  130. $(this.page_layout.wrapper).removeClass('layout-wrapper-background');
  131. $(this.page_layout.main).removeClass('layout-main-section');
  132. $(this.page_layout.sidebar_area).toggle(false);
  133. } else {
  134. // module link
  135. this.setup_sidebar();
  136. }
  137. // footer
  138. this.setup_footer();
  139. // header - no headers for tables and guests
  140. if(!(this.meta.istable || user=='Guest' || (this.meta.in_dialog && !this.in_form)))
  141. this.frm_head = new _f.FrmHeader(this.page_layout.head, this);
  142. // create fields
  143. this.setup_fields_std();
  144. }
  145. _f.Frm.prototype.setup_print = function() {
  146. var l = []
  147. this.default_format = 'Standard';
  148. for(var key in locals['Print Format']) {
  149. if(locals['Print Format'][key].doc_type == this.meta.name) {
  150. l.push(locals['Print Format'][key].name);
  151. }
  152. }
  153. // if default print format is given, use it
  154. if(this.meta.default_print_format)
  155. this.default_format = this.meta.default_print_format;
  156. l.push('Standard');
  157. this.print_sel = $a(null, 'select', '', {width:'160px'});
  158. add_sel_options(this.print_sel, l);
  159. this.print_sel.value = this.default_format;
  160. }
  161. _f.Frm.prototype.print_doc = function() {
  162. if(this.doc.docstatus==2) {
  163. msgprint("Cannot Print Cancelled Documents.");
  164. return;
  165. }
  166. _p.show_dialog(); // multiple options
  167. }
  168. // email the form
  169. _f.Frm.prototype.email_doc = function() {
  170. // make selector
  171. if(!_e.dialog) _e.make();
  172. _e.dialog.widgets['To'].value = '';
  173. if (cur_frm.doc && cur_frm.doc.contact_email) {
  174. _e.dialog.widgets['To'].value = cur_frm.doc.contact_email;
  175. }
  176. // set print selector
  177. sel = this.print_sel;
  178. var c = $td(_e.dialog.rows['Format'].tab,0,1);
  179. if(c.cur_sel) {
  180. c.removeChild(c.cur_sel);
  181. c.cur_sel = null;
  182. }
  183. c.appendChild(this.print_sel);
  184. c.cur_sel = this.print_sel;
  185. // hide / show attachments
  186. _e.dialog.widgets['Send With Attachments'].checked = 0;
  187. if(cur_frm.doc.file_list) {
  188. $ds(_e.dialog.rows['Send With Attachments']);
  189. } else {
  190. $dh(_e.dialog.rows['Send With Attachments']);
  191. }
  192. _e.dialog.widgets['Subject'].value = get_doctype_label(this.meta.name) + ': ' + this.docname;
  193. _e.dialog.show();
  194. }
  195. // notify this form of renamed records
  196. _f.Frm.prototype.rename_notify = function(dt, old, name) {
  197. // from form
  198. if(this.meta.in_dialog && !this.in_form)
  199. return;
  200. if(this.docname == old)
  201. this.docname = name;
  202. else
  203. return; // thats it, not for children!
  204. // editable
  205. this.is_editable[name] = this.is_editable[old];
  206. delete this.is_editable[old];
  207. // cleanup
  208. if(this && this.opendocs[old]) {
  209. // local doctype copy
  210. local_dt[dt][name] = local_dt[dt][old];
  211. local_dt[dt][old] = null;
  212. }
  213. delete this.opendocs[old];
  214. this.opendocs[name] = true;
  215. wn.re_route[window.location.hash] = '#Form/' + encodeURIComponent(this.doctype) + '/' + encodeURIComponent(name);
  216. wn.set_route('Form', this.doctype, name);
  217. }
  218. // SETUP
  219. _f.Frm.prototype.setup_meta = function(doctype) {
  220. this.meta = get_local('DocType',this.doctype);
  221. this.perm = get_perm(this.doctype); // for create
  222. if(this.meta.istable) { this.meta.in_dialog = 1 }
  223. this.setup_print();
  224. }
  225. _f.Frm.prototype.setup_sidebar = function() {
  226. this.sidebar = new wn.widgets.form.sidebar.Sidebar(this);
  227. }
  228. _f.Frm.prototype.setup_footer = function() {
  229. var me = this;
  230. // footer toolbar
  231. var f = this.page_layout.footer;
  232. // save buttom
  233. f.save_area = $a(this.page_layout.footer,'div','',{display:'none', marginTop:'11px'});
  234. f.help_area = $a(this.page_layout.footer,'div');
  235. var b = $btn(f.save_area, 'Save',
  236. function() { cur_frm.save('Save'); },{marginLeft:'0px'},'green');
  237. // show / hide save
  238. f.show_save = function() {
  239. $ds(me.page_layout.footer.save_area);
  240. }
  241. f.hide_save = function() {
  242. $dh(me.page_layout.footer.save_area);
  243. }
  244. }
  245. _f.Frm.prototype.set_intro = function(txt) {
  246. if(!this.intro_area) {
  247. this.intro_area = $('<div class="help-box form-intro-area">')
  248. .insertBefore(this.page_layout.body.firstChild);
  249. }
  250. if(txt) {
  251. if(txt.search(/<p>/)==-1) txt = '<p>' + txt + '</p>';
  252. this.intro_area.html(txt);
  253. } else {
  254. this.intro_area.remove();
  255. this.intro_area = null;
  256. }
  257. }
  258. _f.Frm.prototype.set_footnote = function(txt) {
  259. if(!this.footnote_area) {
  260. this.footnote_area = $('<div class="help-box form-intro-area">')
  261. .insertAfter(this.page_layout.body.lastChild);
  262. }
  263. if(txt) {
  264. if(txt.search(/<p>/)==-1) txt = '<p>' + txt + '</p>';
  265. this.footnote_area.html(txt);
  266. } else {
  267. this.footnote_area.remove();
  268. this.footnote_area = null;
  269. }
  270. }
  271. _f.Frm.prototype.setup_fields_std = function() {
  272. var fl = wn.meta.docfield_list[this.doctype];
  273. fl.sort(function(a,b) { return a.idx - b.idx});
  274. if(fl[0]&&fl[0].fieldtype!="Section Break" || get_url_arg('embed')) {
  275. this.layout.addrow(); // default section break
  276. if(fl[0].fieldtype!="Column Break") {// without column too
  277. var c = this.layout.addcell();
  278. $y(c.wrapper, {padding: '8px'});
  279. }
  280. }
  281. var sec;
  282. for(var i=0;i<fl.length;i++) {
  283. var f=fl[i];
  284. // if section break and next item
  285. // is a section break then ignore
  286. if(f.fieldtype=='Section Break' && fl[i+1] && fl[i+1].fieldtype=='Section Break')
  287. continue;
  288. var fn = f.fieldname?f.fieldname:f.label;
  289. var fld = make_field(f, this.doctype, this.layout.cur_cell, this);
  290. this.fields[this.fields.length] = fld;
  291. this.fields_dict[fn] = fld;
  292. if(sec && ['Section Break', 'Column Break'].indexOf(f.fieldtype)==-1) {
  293. fld.parent_section = sec;
  294. sec.fields.push(fld);
  295. }
  296. if(f.fieldtype=='Section Break') {
  297. sec = fld;
  298. this.sections.push(fld);
  299. }
  300. // default col-break after sec-break
  301. if((f.fieldtype=='Section Break')&&(fl[i+1])&&(fl[i+1].fieldtype!='Column Break')) {
  302. var c = this.layout.addcell();
  303. $y(c.wrapper, {padding: '8px'});
  304. }
  305. }
  306. }
  307. // --------------------------------------------------------------------------------------
  308. _f.Frm.prototype.add_custom_button = function(label, fn, icon) {
  309. this.frm_head.appframe.add_button(label, fn, icon);
  310. }
  311. _f.Frm.prototype.clear_custom_buttons = function() {
  312. this.frm_head.refresh_toolbar()
  313. }
  314. // --------------------------------------------------------------------------------------
  315. _f.Frm.prototype.add_fetch = function(link_field, src_field, tar_field) {
  316. if(!this.fetch_dict[link_field]) {
  317. this.fetch_dict[link_field] = {'columns':[], 'fields':[]}
  318. }
  319. this.fetch_dict[link_field].columns.push(src_field);
  320. this.fetch_dict[link_field].fields.push(tar_field);
  321. }
  322. // --------------------------------------------------------------------------------------
  323. _f.Frm.prototype.setup_client_script = function() {
  324. // setup client obj
  325. if(this.meta.client_script_core || this.meta.client_script || this.meta.__js) {
  326. this.runclientscript('setup', this.doctype, this.docname);
  327. }
  328. }
  329. // --------------------------------------------------------------------------------------
  330. _f.Frm.prototype.refresh_print_layout = function() {
  331. $ds(this.print_wrapper);
  332. $dh(this.page_layout.wrapper);
  333. var me = this;
  334. var print_callback = function(print_html) {
  335. me.print_body.innerHTML = print_html;
  336. }
  337. // print head
  338. if(cur_frm.doc.select_print_heading)
  339. cur_frm.set_print_heading(cur_frm.doc.select_print_heading)
  340. if(user!='Guest') {
  341. $di(this.view_btn_wrapper);
  342. // archive
  343. if(cur_frm.doc.__archived) {
  344. $dh(this.view_btn_wrapper);
  345. }
  346. } else {
  347. $dh(this.view_btn_wrapper);
  348. $dh(this.print_close_btn);
  349. }
  350. // create print format here
  351. _p.build(this.default_format, print_callback, null, 1);
  352. }
  353. // --------------------------------------------------------------------------------------
  354. _f.Frm.prototype.show_the_frm = function() {
  355. // show the dialog
  356. if(this.meta.in_dialog && !this.parent.dialog.display) {
  357. if(!this.meta.istable)
  358. this.parent.table_form = false;
  359. this.parent.dialog.show();
  360. }
  361. }
  362. // --------------------------------------------------------------------------------------
  363. _f.Frm.prototype.set_print_heading = function(txt) {
  364. this.pformat[cur_frm.docname] = txt;
  365. }
  366. // --------------------------------------------------------------------------------------
  367. _f.Frm.prototype.defocus_rest = function() {
  368. // deselect others
  369. if(_f.cur_grid_cell) _f.cur_grid_cell.grid.cell_deselect();
  370. }
  371. // -------- Permissions -------
  372. // Returns global permissions, at all levels
  373. // ======================================================================================
  374. _f.Frm.prototype.get_doc_perms = function() {
  375. var p = [0,0,0,0,0,0];
  376. for(var i=0; i<this.perm.length; i++) {
  377. if(this.perm[i]) {
  378. if(this.perm[i][READ]) p[READ] = 1;
  379. if(this.perm[i][WRITE]) p[WRITE] = 1;
  380. if(this.perm[i][SUBMIT]) p[SUBMIT] = 1;
  381. if(this.perm[i][CANCEL]) p[CANCEL] = 1;
  382. if(this.perm[i][AMEND]) p[AMEND] = 1;
  383. }
  384. }
  385. return p;
  386. }
  387. // refresh
  388. // ======================================================================================
  389. _f.Frm.prototype.refresh_header = function() {
  390. // set title
  391. // main title
  392. if(!this.meta.in_dialog || this.in_form) {
  393. set_title(this.meta.issingle ? this.doctype : this.docname);
  394. }
  395. // form title
  396. //this.page_layout.main_head.innerHTML = '<h2>'+this.docname+'</h2>';
  397. // show / hide buttons
  398. if(this.frm_head)this.frm_head.refresh();
  399. // add to recent
  400. if(wn.ui.toolbar.recent)
  401. wn.ui.toolbar.recent.add(this.doctype, this.docname, 1);
  402. }
  403. // --------------------------------------------------------------------------------------
  404. _f.Frm.prototype.check_doc_perm = function() {
  405. // get perm
  406. var dt = this.parent_doctype?this.parent_doctype : this.doctype;
  407. var dn = this.parent_docname?this.parent_docname : this.docname;
  408. this.perm = get_perm(dt, dn);
  409. this.orig_perm = get_perm(dt, dn, 1);
  410. if(!this.perm[0][READ]) {
  411. if(user=='Guest') {
  412. // allow temp access? via encryted akey
  413. if(_f.temp_access[dt] && _f.temp_access[dt][dn]) {
  414. this.perm = [[1,0,0]]
  415. return 1;
  416. }
  417. }
  418. window.back();
  419. return 0;
  420. }
  421. return 1
  422. }
  423. // --------------------------------------------------------------------------------------
  424. _f.Frm.prototype.refresh = function(docname) {
  425. // record switch
  426. if(docname) {
  427. if(this.docname != docname && (!this.meta.in_dialog || this.in_form) && !this.meta.istable)
  428. scroll(0, 0);
  429. this.docname = docname;
  430. }
  431. if(!this.meta.istable) {
  432. cur_frm = this;
  433. this.parent.cur_frm = this;
  434. }
  435. if(this.docname) { // document to show
  436. // check permissions
  437. if(!this.check_doc_perm()) return;
  438. // set the doc
  439. this.doc = get_local(this.doctype, this.docname);
  440. // do setup
  441. if(!this.setup_done) this.setup();
  442. // set customized permissions for this record
  443. this.runclientscript('set_perm',this.doctype, this.docname);
  444. // load the record for the first time, if not loaded (call 'onload')
  445. cur_frm.cscript.is_onload = false;
  446. if(!this.opendocs[this.docname]) {
  447. cur_frm.cscript.is_onload = true;
  448. this.setnewdoc(this.docname);
  449. }
  450. // editable
  451. if(this.doc.__islocal)
  452. this.is_editable[this.docname] = 1; // new is editable
  453. this.editable = this.is_editable[this.docname];
  454. if(!this.doc.__archived && (this.editable || (!this.editable && this.meta.istable))) {
  455. // show form layout (with fields etc)
  456. // ----------------------------------
  457. if(this.print_wrapper) {
  458. $dh(this.print_wrapper);
  459. $ds(this.page_layout.wrapper);
  460. }
  461. // header
  462. if(!this.meta.istable) {
  463. this.refresh_header();
  464. this.sidebar && this.sidebar.refresh();
  465. }
  466. // call trigger
  467. this.runclientscript('refresh');
  468. // trigger global trigger
  469. // to use this
  470. // $(docuemnt).bind('form_refresh', function() { })
  471. $(document).trigger('form_refresh');
  472. // fields
  473. this.refresh_fields();
  474. // dependent fields
  475. this.refresh_dependency();
  476. // footer
  477. this.refresh_footer();
  478. // layout
  479. if(this.layout) this.layout.show();
  480. // call onload post render for callbacks to be fired
  481. if(cur_frm.cscript.is_onload) {
  482. this.runclientscript('onload_post_render', this.doctype, this.docname);
  483. }
  484. // focus on first input
  485. if(this.doc.docstatus==0) {
  486. $(this.wrapper).find('.form-layout-row :input:first').focus();
  487. }
  488. } else {
  489. // show print layout
  490. // ----------------------------------
  491. this.refresh_header();
  492. if(this.print_wrapper) {
  493. this.refresh_print_layout();
  494. }
  495. this.runclientscript('edit_status_changed');
  496. }
  497. $(cur_frm.wrapper).trigger('render_complete');
  498. }
  499. }
  500. // --------------------------------------------------------------------------------------
  501. _f.Frm.prototype.refresh_footer = function() {
  502. var f = this.page_layout.footer;
  503. if(f.save_area) {
  504. if(this.editable && (!this.meta.in_dialog || this.in_form)
  505. && this.doc.docstatus==0 && !this.meta.istable && this.get_doc_perms()[WRITE]
  506. && (this.fields && this.fields.length > 7)) {
  507. f.show_save();
  508. } else {
  509. f.hide_save();
  510. }
  511. }
  512. }
  513. _f.Frm.prototype.refresh_field = function(fname) {
  514. cur_frm.fields_dict[fname] && cur_frm.fields_dict[fname].refresh
  515. && cur_frm.fields_dict[fname].refresh();
  516. }
  517. _f.Frm.prototype.refresh_fields = function() {
  518. // refresh fields
  519. for(var i=0; i<this.fields.length; i++) {
  520. var f = this.fields[i];
  521. f.perm = this.perm;
  522. f.docname = this.docname;
  523. // if field is identifiable (not blank section or column break)
  524. // get the "customizable" parameters for this record
  525. var fn = f.df.fieldname || f.df.label;
  526. if(fn)
  527. f.df = wn.meta.get_docfield(this.doctype, fn, this.docname);
  528. if(f.df.fieldtype!='Section Break' && f.refresh) {
  529. f.refresh();
  530. }
  531. }
  532. // refresh sections
  533. $.each(this.sections, function(i, f) {
  534. f.refresh(true);
  535. })
  536. // cleanup activities after refresh
  537. this.cleanup_refresh(this);
  538. }
  539. _f.Frm.prototype.cleanup_refresh = function() {
  540. var me = this;
  541. if(me.fields_dict['amended_from']) {
  542. if (me.doc.amended_from) {
  543. unhide_field('amended_from'); unhide_field('amendment_date');
  544. } else {
  545. hide_field('amended_from'); hide_field('amendment_date');
  546. }
  547. }
  548. if(me.fields_dict['trash_reason']) {
  549. if(me.doc.trash_reason && me.doc.docstatus == 2) {
  550. unhide_field('trash_reason');
  551. } else {
  552. hide_field('trash_reason');
  553. }
  554. }
  555. if(me.meta.autoname && me.meta.autoname.substr(0,6)=='field:' && !me.doc.__islocal) {
  556. var fn = me.meta.autoname.substr(6);
  557. cur_frm.toggle_display(fn, false);
  558. }
  559. }
  560. // Resolve "depends_on" and show / hide accordingly
  561. _f.Frm.prototype.refresh_dependency = function() {
  562. var me = this;
  563. var doc = locals[this.doctype][this.docname];
  564. // build dependants' dictionary
  565. var dep_dict = {};
  566. var has_dep = false;
  567. for(fkey in me.fields) {
  568. var f = me.fields[fkey];
  569. f.dependencies_clear = true;
  570. var guardian = f.df.depends_on;
  571. if(guardian) {
  572. if(!dep_dict[guardian])
  573. dep_dict[guardian] = [];
  574. dep_dict[guardian][dep_dict[guardian].length] = f;
  575. has_dep = true;
  576. }
  577. }
  578. if(!has_dep)return;
  579. // show / hide based on values
  580. for(var i=me.fields.length-1;i>=0;i--) {
  581. var f = me.fields[i];
  582. f.guardian_has_value = true;
  583. if(f.df.depends_on) {
  584. // evaluate guardian
  585. var v = doc[f.df.depends_on];
  586. if(f.df.depends_on.substr(0,5)=='eval:') {
  587. f.guardian_has_value = eval(f.df.depends_on.substr(5));
  588. } else if(f.df.depends_on.substr(0,3)=='fn:') {
  589. f.guardian_has_value = me.runclientscript(f.df.depends_on.substr(3), me.doctype, me.docname);
  590. } else {
  591. if(v || (v==0 && !v.substr)) {
  592. // guardian has value
  593. } else {
  594. f.guardian_has_value = false;
  595. }
  596. }
  597. // show / hide
  598. if(f.guardian_has_value) {
  599. if(f.grid)f.grid.show(); else $ds(f.wrapper);
  600. } else {
  601. if(f.grid)f.grid.hide(); else $dh(f.wrapper);
  602. }
  603. }
  604. }
  605. }
  606. // setnewdoc is called when a record is loaded for the first time
  607. // ======================================================================================
  608. _f.Frm.prototype.setnewdoc = function(docname) {
  609. this.check_doctype_conflict(docname);
  610. // if loaded
  611. if(this.opendocs[docname]) { // already exists
  612. this.docname=docname;
  613. return;
  614. }
  615. //if(!this.meta)
  616. // this.setup_meta();
  617. // make a copy of the doctype for client script settings
  618. // each record will have its own client script
  619. Meta.make_local_dt(this.doctype,docname);
  620. this.docname = docname;
  621. var me = this;
  622. var viewname = docname;
  623. if(this.meta.issingle) viewname = this.doctype;
  624. // Client Script
  625. this.runclientscript('onload', this.doctype, this.docname);
  626. this.is_editable[docname] = 1;
  627. if(this.meta.read_only_onload) this.is_editable[docname] = 0;
  628. this.opendocs[docname] = true;
  629. }
  630. _f.Frm.prototype.edit_doc = function() {
  631. // set fields
  632. this.is_editable[this.docname] = true;
  633. this.refresh();
  634. }
  635. _f.Frm.prototype.show_doc = function(dn) {
  636. this.refresh(dn);
  637. }
  638. // ======================================================================================
  639. var validated; // bad design :(
  640. _f.Frm.prototype.save = function(save_action, call_back) {
  641. //alert(save_action);
  642. if(!save_action) save_action = 'Save';
  643. var me = this;
  644. if(this.savingflag) {
  645. msgprint("Document is currently saving....");
  646. return; // already saving (do not double save)
  647. }
  648. if(save_action=='Submit') {
  649. locals[this.doctype][this.docname].submitted_on = dateutil.full_str();
  650. locals[this.doctype][this.docname].submitted_by = user;
  651. }
  652. if(save_action=='Trash') {
  653. var reason = prompt('Reason for trash (mandatory)', '');
  654. if(!strip(reason)) {
  655. msgprint('Reason is mandatory, not trashed');
  656. return;
  657. }
  658. locals[this.doctype][this.docname].trash_reason = reason;
  659. }
  660. // run validations
  661. if(save_action=='Cancel') {
  662. var reason = prompt('Reason for cancellation (mandatory)', '');
  663. if(!strip(reason)) {
  664. msgprint('Reason is mandatory, not cancelled');
  665. return;
  666. }
  667. locals[this.doctype][this.docname].cancel_reason = reason;
  668. locals[this.doctype][this.docname].cancelled_on = dateutil.full_str();
  669. locals[this.doctype][this.docname].cancelled_by = user;
  670. } else if(save_action=='Update') {
  671. // no validation for update
  672. } else { // no validation for cancellation
  673. validated = true;
  674. if(this.cscript.validate)
  675. this.runclientscript('validate');
  676. if(!validated) {
  677. this.savingflag = false;
  678. return 'Error';
  679. }
  680. }
  681. var ret_fn = function(r) {
  682. me.savingflag = false;
  683. if(!me.meta.istable && r) {
  684. me.refresh(r.docname);
  685. }
  686. if(call_back){
  687. call_back(r);
  688. }
  689. }
  690. var me = this;
  691. var ret_fn_err = function(r) {
  692. var doc = locals[me.doctype][me.docname];
  693. me.savingflag = false;
  694. ret_fn(r);
  695. }
  696. this.savingflag = true;
  697. if(this.docname && validated) {
  698. // scroll to top
  699. scroll(0, 0);
  700. return this.savedoc(save_action, ret_fn, ret_fn_err);
  701. }
  702. }
  703. _f.Frm.prototype.runscript = function(scriptname, callingfield, onrefresh) {
  704. var me = this;
  705. if(this.docname) {
  706. // make doc list
  707. var doclist = compress_doclist(make_doclist(this.doctype, this.docname));
  708. // send to run
  709. if(callingfield)
  710. $(callingfield.input).set_working();
  711. $c('runserverobj', {'docs':doclist, 'method':scriptname },
  712. function(r, rtxt) {
  713. // run refresh
  714. if(onrefresh)
  715. onrefresh(r,rtxt);
  716. // fields
  717. me.refresh_fields();
  718. // dependent fields
  719. me.refresh_dependency();
  720. // enable button
  721. if(callingfield)
  722. $(callingfield.input).done_working();
  723. }
  724. );
  725. }
  726. }
  727. _f.Frm.prototype.runclientscript = function(caller, cdt, cdn) {
  728. if(!cdt)cdt = this.doctype;
  729. if(!cdn)cdn = this.docname;
  730. var ret = null;
  731. var doc = cur_frm.doc;
  732. try {
  733. if(this.cscript[caller])
  734. ret = this.cscript[caller](doc, cdt, cdn);
  735. // for product
  736. if(this.cscript['custom_'+caller])
  737. ret += this.cscript['custom_'+caller](doc, cdt, cdn);
  738. } catch(e) {
  739. console.log(e);
  740. }
  741. if(caller && caller.toLowerCase()=='setup') {
  742. var doctype = get_local('DocType', this.doctype);
  743. // js
  744. var cs = doctype.__js || (doctype.client_script_core + doctype.client_script);
  745. if(cs) {
  746. try {
  747. var tmp = eval(cs);
  748. } catch(e) {
  749. console.log(e);
  750. }
  751. }
  752. // css
  753. if(doctype.__css) set_style(doctype.__css)
  754. // ---Client String----
  755. if(doctype.client_string) { // split client string
  756. this.cstring = {};
  757. var elist = doctype.client_string.split('---');
  758. for(var i=1;i<elist.length;i=i+2) {
  759. this.cstring[strip(elist[i])] = elist[i+1];
  760. }
  761. }
  762. }
  763. return ret;
  764. }
  765. _f.Frm.prototype.copy_doc = function(onload, from_amend) {
  766. if(!this.perm[0][CREATE]) {
  767. msgprint('You are not allowed to create '+this.meta.name);
  768. return;
  769. }
  770. var dn = this.docname;
  771. // copy parent
  772. var newdoc = LocalDB.copy(this.doctype, dn, from_amend);
  773. // do not copy attachments
  774. if(this.meta.allow_attach && newdoc.file_list && !from_amend)
  775. newdoc.file_list = null;
  776. // copy chidren
  777. var dl = make_doclist(this.doctype, dn);
  778. // table fields dict - for no_copy check
  779. var tf_dict = {};
  780. for(var d in dl) {
  781. d1 = dl[d];
  782. // get tabel field
  783. if(!tf_dict[d1.parentfield]) {
  784. tf_dict[d1.parentfield] = wn.meta.get_docfield(d1.parenttype, d1.parentfield);
  785. }
  786. if(d1.parent==dn && cint(tf_dict[d1.parentfield].no_copy)!=1) {
  787. var ch = LocalDB.copy(d1.doctype, d1.name, from_amend);
  788. ch.parent = newdoc.name;
  789. ch.docstatus = 0;
  790. ch.owner = user;
  791. ch.creation = '';
  792. ch.modified_by = user;
  793. ch.modified = '';
  794. }
  795. }
  796. newdoc.__islocal = 1;
  797. newdoc.docstatus = 0;
  798. newdoc.owner = user;
  799. newdoc.creation = '';
  800. newdoc.modified_by = user;
  801. newdoc.modified = '';
  802. if(onload)onload(newdoc);
  803. loaddoc(newdoc.doctype, newdoc.name);
  804. }
  805. _f.Frm.prototype.reload_doc = function() {
  806. this.check_doctype_conflict(this.docname);
  807. var me = this;
  808. var ret_fn = function(r, rtxt) {
  809. // n tweets and last comment
  810. me.runclientscript('setup', me.doctype, me.docname);
  811. me.refresh();
  812. }
  813. if(me.doc.__islocal) {
  814. // reload only doctype
  815. $c('webnotes.widgets.form.load.getdoctype', {'doctype':me.doctype }, ret_fn, null, null, 'Refreshing ' + me.doctype + '...');
  816. } else {
  817. // reload doc and docytpe
  818. $c('webnotes.widgets.form.load.getdoc', {'name':me.docname, 'doctype':me.doctype, 'getdoctype':1, 'user':user}, ret_fn, null, null, 'Refreshing ' + me.docname + '...');
  819. }
  820. }
  821. _f.Frm.prototype.savedoc = function(save_action, onsave, onerr) {
  822. save_doclist(this.doctype, this.docname, save_action, onsave, onerr);
  823. }
  824. _f.Frm.prototype.saveupdate = function() {
  825. this.save('Update');
  826. }
  827. _f.Frm.prototype.savesubmit = function() {
  828. var answer = confirm("Permanently Submit "+this.docname+"?");
  829. var me = this;
  830. if(answer) {
  831. this.save('Submit', function(r) {
  832. if(!r.exc && me.cscript.on_submit) {
  833. me.runclientscript('on_submit', me.doctype, me.docname);
  834. }
  835. });
  836. }
  837. }
  838. _f.Frm.prototype.savecancel = function() {
  839. var answer = confirm("Permanently Cancel "+this.docname+"?");
  840. if(answer) this.save('Cancel');
  841. }
  842. // delete the record
  843. _f.Frm.prototype.savetrash = function() {
  844. var me = this;
  845. var answer = confirm("Permanently Delete "+this.docname+"? This action cannot be reversed");
  846. if(answer) {
  847. $c('webnotes.model.delete_doc', {dt:this.doctype, dn:this.docname}, function(r,rt) {
  848. if(r.message=='okay') {
  849. // delete from locals
  850. LocalDB.delete_doc(me.doctype, me.docname);
  851. // delete from recent
  852. if(wn.ui.toolbar.recent) wn.ui.toolbar.recent.remove(me.doctype, me.docname);
  853. // "close"
  854. window.history.back();
  855. }
  856. })
  857. }
  858. }
  859. _f.Frm.prototype.amend_doc = function() {
  860. if(!this.fields_dict['amended_from']) {
  861. alert('"amended_from" field must be present to do an amendment.');
  862. return;
  863. }
  864. var me = this;
  865. var fn = function(newdoc) {
  866. newdoc.amended_from = me.docname;
  867. if(me.fields_dict && me.fields_dict['amendment_date'])
  868. newdoc.amendment_date = dateutil.obj_to_str(new Date());
  869. }
  870. this.copy_doc(fn, 1);
  871. }
  872. _f.get_value = function(dt, dn, fn) {
  873. if(locals[dt] && locals[dt][dn])
  874. return locals[dt][dn][fn];
  875. }
  876. _f.Frm.prototype.set_value_in_locals = function(dt, dn, fn, v) {
  877. var d = locals[dt][dn];
  878. var changed = d[fn] != v;
  879. if(changed && (d[fn]==null || v==null) && (cstr(d[fn])==cstr(v)))
  880. changed = false;
  881. if(changed) {
  882. d[fn] = v;
  883. if(d.parenttype)
  884. d.__unsaved = 1;
  885. this.set_unsaved();
  886. }
  887. }
  888. _f.Frm.prototype.set_unsaved = function() {
  889. if(cur_frm.doc.__unsaved) return;
  890. cur_frm.doc.__unsaved = 1;
  891. cur_frm.frm_head.refresh_labels()
  892. }
  893. _f.Frm.prototype.show_comments = function() {
  894. if(!cur_frm.comments) {
  895. cur_frm.comments = new Dialog(540, 400, 'Comments');
  896. cur_frm.comments.comment_body = $a(cur_frm.comments.body, 'div', 'dialog_frm');
  897. $y(cur_frm.comments.body, {backgroundColor:'#EEE'});
  898. cur_frm.comments.list = new CommentList(cur_frm.comments.comment_body);
  899. }
  900. cur_frm.comments.list.dt = cur_frm.doctype;
  901. cur_frm.comments.list.dn = cur_frm.docname;
  902. cur_frm.comments.show();
  903. cur_frm.comments.list.run();
  904. }
  905. _f.Frm.prototype.get_doc = function() {
  906. return locals[this.doctype][this.docname];
  907. }
  908. _f.Frm.prototype.get_doclist = function() {
  909. return make_doclist(this.doctype, this.docname);
  910. }
  911. _f.Frm.prototype.field_map = function(fnames, fn) {
  912. if(typeof fnames=='string') {
  913. if(fnames == '*') {
  914. fnames = keys(this.fields_dict);
  915. } else {
  916. fnames = [fnames];
  917. }
  918. }
  919. $.each(fnames, function(i,f) {
  920. //var field = cur_frm.fields_dict[f]; - much better design
  921. var field = wn.meta.get_docfield(cur_frm.doctype, f, cur_frm.docname)
  922. if(field) {
  923. fn(field);
  924. cur_frm.refresh_field(f);
  925. };
  926. })
  927. }
  928. _f.Frm.prototype.set_df_property = function(fieldname, property, value) {
  929. var field = wn.meta.get_docfield(cur_frm.doctype, fieldname, cur_frm.docname)
  930. if(field) {
  931. field[property] = value;
  932. cur_frm.refresh_field(fieldname);
  933. };
  934. }
  935. _f.Frm.prototype.toggle_enable = function(fnames, enable) {
  936. cur_frm.field_map(fnames, function(field) { field.disabled = enable ? false : true; });
  937. }
  938. _f.Frm.prototype.toggle_reqd = function(fnames, mandatory) {
  939. cur_frm.field_map(fnames, function(field) { field.reqd = mandatory ? true : false; });
  940. }
  941. _f.Frm.prototype.toggle_display = function(fnames, show) {
  942. cur_frm.field_map(fnames, function(field) { field.hidden = show ? false : true; });
  943. }
  944. _f.Frm.prototype.call_server = function(method, args, callback) {
  945. $c_obj(cur_frm.get_doclist(), method, args, callback);
  946. }
  947. _f.Frm.prototype.set_value = function(field, value) {
  948. cur_frm.get_doc()[field] = value;
  949. cur_frm.fields_dict[field].refresh();
  950. }