Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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