您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

820 行
20 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.toolbar
  26. + this.form_wrapper
  27. + this.main
  28. + this.head
  29. + this.body
  30. + this.layout
  31. + this.sidebar
  32. + this.print_wrapper
  33. + this.head
  34. + this.footer
  35. */
  36. wn.provide('_f');
  37. wn.provide('wn.ui.form');
  38. wn.ui.form.Controller = Class.extend({
  39. init: function(opts) {
  40. $.extend(this, opts);
  41. this.setup && this.setup();
  42. }
  43. });
  44. _f.frms = {};
  45. _f.Frm = function(doctype, parent, in_form) {
  46. this.docname = '';
  47. this.doctype = doctype;
  48. this.display = 0;
  49. this.refresh_if_stale_for = 120;
  50. var me = this;
  51. this.last_view_is_edit = {};
  52. this.opendocs = {};
  53. this.sections = [];
  54. this.grids = [];
  55. this.cscript = new wn.ui.form.Controller({frm:this});
  56. this.pformat = {};
  57. this.fetch_dict = {};
  58. this.parent = parent;
  59. this.tinymce_id_list = [];
  60. this.setup_meta(doctype);
  61. // show in form instead of in dialog, when called using url (router.js)
  62. this.in_form = in_form ? true : false;
  63. // notify on rename
  64. var me = this;
  65. $(document).on('rename', function(event, dt, old_name, new_name) {
  66. if(dt==me.doctype)
  67. me.rename_notify(dt, old_name, new_name)
  68. });
  69. }
  70. _f.Frm.prototype.check_doctype_conflict = function(docname) {
  71. var me = this;
  72. if(this.doctype=='DocType' && docname=='DocType') {
  73. msgprint('Allowing DocType, DocType. Be careful!')
  74. } else if(this.doctype=='DocType') {
  75. if (wn.views.formview[docname] || wn.pages['List/'+docname]) {
  76. msgprint("Cannot open DocType when its instance is open")
  77. throw 'doctype open conflict'
  78. }
  79. } else {
  80. if (wn.views.formview.DocType && wn.views.formview.DocType.frm.opendocs[this.doctype]) {
  81. msgprint("Cannot open instance when its DocType is open")
  82. throw 'doctype open conflict'
  83. }
  84. }
  85. }
  86. _f.Frm.prototype.setup = function() {
  87. var me = this;
  88. this.fields = [];
  89. this.fields_dict = {};
  90. this.state_fieldname = wn.workflow.get_state_fieldname(this.doctype);
  91. // wrapper
  92. this.wrapper = this.parent;
  93. wn.ui.make_app_page({
  94. parent: this.wrapper,
  95. single_column: true
  96. });
  97. this.appframe = this.wrapper.appframe;
  98. this.layout_main = $(this.wrapper)
  99. .find(".layout-main")
  100. .css({"padding-bottom": "0px"})
  101. .get(0);
  102. this.toolbar = new wn.ui.form.Toolbar({
  103. frm: this,
  104. appframe: this.appframe
  105. });
  106. this.frm_head = this.toolbar;
  107. // create area for print fomrat
  108. this.setup_print_layout();
  109. // 2 column layout
  110. this.setup_std_layout();
  111. // client script must be called after "setup" - there are no fields_dict attached to the frm otherwise
  112. this.script_manager = new wn.ui.form.ScriptManager({
  113. frm: this
  114. })
  115. this.watch_model_updates();
  116. this.footer = new wn.ui.form.Footer({
  117. frm: this,
  118. parent: this.layout_main
  119. })
  120. this.setup_done = true;
  121. }
  122. _f.Frm.prototype.watch_model_updates = function() {
  123. // watch model updates
  124. var me = this;
  125. // on main doc
  126. wn.model.on(me.doctype, "*", function(fieldname, value, doc) {
  127. // set input
  128. if(doc.name===me.docname) {
  129. me.fields_dict[fieldname]
  130. && me.fields_dict[fieldname].refresh(fieldname);
  131. me.script_manager.trigger(fieldname, doc.doctype, doc.name);
  132. me.refresh_dependency();
  133. }
  134. })
  135. // on table fields
  136. $.each(wn.model.get("DocField", {fieldtype:"Table", parent: me.doctype}), function(i, df) {
  137. wn.model.on(df.options, "*", function(fieldname, value, doc) {
  138. if(doc.parent===me.docname && doc.parentfield===df.fieldname) {
  139. me.fields_dict[df.fieldname].grid.set_value(fieldname, value, doc);
  140. me.script_manager.trigger(fieldname, doc.doctype, doc.name);
  141. }
  142. })
  143. })
  144. }
  145. _f.Frm.prototype.setup_print_layout = function() {
  146. var me = this;
  147. this.print_wrapper = $('<div>\
  148. <div class="print-format-area clear-fix" style="min-height: 400px;"></div>\
  149. </div>').appendTo(this.layout_main).get(0);
  150. //appframe.add_ripped_paper_effect(this.print_wrapper);
  151. this.print_body = $(this.print_wrapper).find(".print-format-area").get(0);
  152. }
  153. _f.Frm.prototype.onhide = function() {
  154. if(_f.cur_grid_cell) _f.cur_grid_cell.grid.cell_deselect();
  155. }
  156. _f.Frm.prototype.setup_std_layout = function() {
  157. this.form_wrapper = $('<div></div>').appendTo(this.layout_main).get(0);
  158. $parent = $(this.form_wrapper);
  159. this.head = $parent.find(".layout-appframe").get(0);
  160. this.main = this.form_wrapper;
  161. this.body_header = $a(this.main, 'div');
  162. this.body = $a(this.main, 'div');
  163. if(this.heading) {
  164. this.page_head = new PageHeader(this.head, this);
  165. }
  166. // only tray
  167. this.meta.section_style='Simple'; // always simple!
  168. // layout
  169. this.layout = new wn.ui.form.Layout({
  170. parent: this.body,
  171. doctype: this.doctype,
  172. frm: this,
  173. })
  174. // state
  175. this.states = new wn.ui.form.States({
  176. frm: this
  177. });
  178. }
  179. _f.Frm.prototype.setup_print = function() {
  180. this.print_formats = wn.meta.get_print_formats(this.meta.name);
  181. this.print_sel = $("<select>")
  182. .css({"width": "160px"}).add_options(this.print_formats).get(0);
  183. this.print_sel.value = this.print_formats[0];
  184. }
  185. _f.Frm.prototype.print_doc = function() {
  186. if(this.doc.docstatus==2) {
  187. msgprint("Cannot Print Cancelled Documents.");
  188. return;
  189. }
  190. _p.show_dialog(); // multiple options
  191. }
  192. // email the form
  193. _f.Frm.prototype.email_doc = function(message) {
  194. new wn.views.CommunicationComposer({
  195. doc: this.doc,
  196. subject: wn._(this.meta.name) + ': ' + this.docname,
  197. recipients: this.doc.email || this.doc.email_id || this.doc.contact_email,
  198. attach_document_print: true,
  199. message: message,
  200. real_name: this.doc.real_name || this.doc.contact_display || this.doc.contact_name
  201. });
  202. }
  203. // email the form
  204. _f.Frm.prototype.rename_doc = function() {
  205. wn.model.rename_doc(this.doctype, this.docname);
  206. }
  207. // notify this form of renamed records
  208. _f.Frm.prototype.rename_notify = function(dt, old, name) {
  209. // from form
  210. if(this.meta.istable)
  211. return;
  212. if(this.docname == old)
  213. this.docname = name;
  214. else
  215. return;
  216. // view_is_edit
  217. this.last_view_is_edit[name] = this.last_view_is_edit[old];
  218. delete this.last_view_is_edit[old];
  219. // cleanup
  220. if(this && this.opendocs[old]) {
  221. // delete docfield copy
  222. wn.meta.docfield_copy[dt][name] = wn.meta.docfield_copy[dt][old];
  223. delete wn.meta.docfield_copy[dt][old];
  224. }
  225. delete this.opendocs[old];
  226. this.opendocs[name] = true;
  227. if(this.meta.in_dialog || !this.in_form) {
  228. return;
  229. }
  230. wn.re_route[window.location.hash] = '#Form/' + encodeURIComponent(this.doctype) + '/' + encodeURIComponent(name);
  231. wn.set_route('Form', this.doctype, name);
  232. }
  233. // SETUP
  234. _f.Frm.prototype.setup_meta = function(doctype) {
  235. this.meta = wn.model.get_doc('DocType',this.doctype);
  236. this.perm = wn.perm.get_perm(this.doctype); // for create
  237. if(this.meta.istable) { this.meta.in_dialog = 1 }
  238. this.setup_print();
  239. }
  240. _f.Frm.prototype.set_intro = function(txt) {
  241. wn.utils.set_intro(this, this.body, txt);
  242. }
  243. _f.Frm.prototype.set_footnote = function(txt) {
  244. wn.utils.set_footnote(this, this.body, txt);
  245. }
  246. _f.Frm.prototype.add_custom_button = function(label, fn, icon) {
  247. this.appframe.add_button(label, fn, icon || "icon-arrow-right");
  248. }
  249. _f.Frm.prototype.clear_custom_buttons = function() {
  250. this.toolbar.refresh()
  251. }
  252. _f.Frm.prototype.add_fetch = function(link_field, src_field, tar_field) {
  253. if(!this.fetch_dict[link_field]) {
  254. this.fetch_dict[link_field] = {'columns':[], 'fields':[]}
  255. }
  256. this.fetch_dict[link_field].columns.push(src_field);
  257. this.fetch_dict[link_field].fields.push(tar_field);
  258. }
  259. _f.Frm.prototype.refresh_print_layout = function() {
  260. $ds(this.print_wrapper);
  261. $dh(this.form_wrapper);
  262. var me = this;
  263. var print_callback = function(print_html) {
  264. me.print_body.innerHTML = print_html;
  265. }
  266. // print head
  267. if(cur_frm.doc.select_print_heading)
  268. cur_frm.set_print_heading(cur_frm.doc.select_print_heading)
  269. if(user!='Guest') {
  270. $di(this.view_btn_wrapper);
  271. // archive
  272. if(cur_frm.doc.__archived) {
  273. $dh(this.view_btn_wrapper);
  274. }
  275. } else {
  276. $dh(this.view_btn_wrapper);
  277. $dh(this.print_close_btn);
  278. }
  279. // create print format here
  280. _p.build(this.$print_view_select.val(), print_callback, false, true, true);
  281. }
  282. _f.Frm.prototype.show_the_frm = function() {
  283. // show the dialog
  284. if(this.meta.in_dialog && !this.parent.dialog.display) {
  285. if(!this.meta.istable)
  286. this.parent.table_form = false;
  287. this.parent.dialog.show();
  288. }
  289. }
  290. _f.Frm.prototype.set_print_heading = function(txt) {
  291. this.pformat[cur_frm.docname] = txt;
  292. }
  293. _f.Frm.prototype.defocus_rest = function() {
  294. // deselect others
  295. if(_f.cur_grid_cell) _f.cur_grid_cell.grid.cell_deselect();
  296. }
  297. _f.Frm.prototype.refresh_header = function() {
  298. // set title
  299. // main title
  300. if(!this.meta.in_dialog || this.in_form) {
  301. set_title(this.meta.issingle ? this.doctype : this.docname);
  302. }
  303. if(wn.ui.toolbar.recent)
  304. wn.ui.toolbar.recent.add(this.doctype, this.docname, 1);
  305. // show / hide buttons
  306. if(this.frm_head) {
  307. this.frm_head.refresh();
  308. }
  309. }
  310. _f.Frm.prototype.check_doc_perm = function() {
  311. // get perm
  312. var dt = this.parent_doctype?this.parent_doctype : this.doctype;
  313. var dn = this.parent_docname?this.parent_docname : this.docname;
  314. this.perm = wn.perm.get_perm(dt, dn);
  315. if(!this.perm[0][READ]) {
  316. wn.set_route("403");
  317. return 0;
  318. }
  319. return 1
  320. }
  321. _f.Frm.prototype.refresh = function(docname) {
  322. // record switch
  323. if(docname) {
  324. if(this.docname != docname && (!this.meta.in_dialog || this.in_form) &&
  325. !this.meta.istable)
  326. scroll(0, 0);
  327. this.docname = docname;
  328. }
  329. cur_frm = this;
  330. if(this.docname) { // document to show
  331. // check permissions
  332. if(!this.check_doc_perm()) return;
  333. // read only (workflow)
  334. this.read_only = wn.workflow.is_read_only(this.doctype, this.docname);
  335. // set the doc
  336. this.doc = wn.model.get_doc(this.doctype, this.docname);
  337. // check if doctype is already open
  338. if (!this.opendocs[this.docname]) {
  339. this.check_doctype_conflict(this.docname);
  340. } else {
  341. if(this.doc && (!this.doc.__unsaved) && this.doc.__last_sync_on &&
  342. (new Date() - this.doc.__last_sync_on) > (this.refresh_if_stale_for * 1000)) {
  343. this.reload_doc();
  344. return;
  345. }
  346. }
  347. // do setup
  348. if(!this.setup_done) this.setup();
  349. // load the record for the first time, if not loaded (call 'onload')
  350. cur_frm.cscript.is_onload = false;
  351. if(!this.opendocs[this.docname]) {
  352. cur_frm.cscript.is_onload = true;
  353. this.setnewdoc(this.docname);
  354. }
  355. // view_is_edit
  356. if(this.doc.__islocal)
  357. this.last_view_is_edit[this.docname] = 1; // new is view_is_edit
  358. this.view_is_edit = this.last_view_is_edit[this.docname];
  359. if(this.view_is_edit || (!this.view_is_edit && this.meta.istable)) {
  360. if(this.print_wrapper) {
  361. $dh(this.print_wrapper);
  362. $ds(this.form_wrapper);
  363. }
  364. // header
  365. this.refresh_header();
  366. // call trigger
  367. this.script_manager.trigger("refresh");
  368. // trigger global trigger
  369. // to use this
  370. $(document).trigger('form_refresh');
  371. // fields
  372. this.refresh_fields();
  373. // call onload post render for callbacks to be fired
  374. if(this.cscript.is_onload) {
  375. this.script_manager.trigger("onload_post_render");
  376. }
  377. // focus on first input
  378. if(this.doc.docstatus==0) {
  379. var first = $(this.form_wrapper).find('.form-layout-row :input:first');
  380. if(!in_list(["Date", "Datetime"], first.attr("data-fieldtype"))) {
  381. first.focus();
  382. }
  383. }
  384. } else {
  385. this.refresh_header();
  386. if(this.print_wrapper) {
  387. this.refresh_print_layout();
  388. }
  389. }
  390. $(cur_frm.wrapper).trigger('render_complete');
  391. }
  392. }
  393. _f.Frm.prototype.refresh_field = function(fname) {
  394. cur_frm.fields_dict[fname] && cur_frm.fields_dict[fname].refresh
  395. && cur_frm.fields_dict[fname].refresh();
  396. }
  397. _f.Frm.prototype.refresh_fields = function() {
  398. this.layout.refresh();
  399. // cleanup activities after refresh
  400. this.cleanup_refresh(this);
  401. // dependent fields
  402. this.refresh_dependency();
  403. }
  404. _f.Frm.prototype.cleanup_refresh = function() {
  405. var me = this;
  406. if(me.fields_dict['amended_from']) {
  407. if (me.doc.amended_from) {
  408. unhide_field('amended_from');
  409. if (me.fields_dict['amendment_date']) unhide_field('amendment_date');
  410. } else {
  411. hide_field('amended_from');
  412. if (me.fields_dict['amendment_date']) hide_field('amendment_date');
  413. }
  414. }
  415. if(me.fields_dict['trash_reason']) {
  416. if(me.doc.trash_reason && me.doc.docstatus == 2) {
  417. unhide_field('trash_reason');
  418. } else {
  419. hide_field('trash_reason');
  420. }
  421. }
  422. if(me.meta.autoname && me.meta.autoname.substr(0,6)=='field:' && !me.doc.__islocal) {
  423. var fn = me.meta.autoname.substr(6);
  424. cur_frm.toggle_display(fn, false);
  425. }
  426. if(me.meta.autoname=="naming_series:" && !me.doc.__islocal) {
  427. cur_frm.toggle_display("naming_series", false);
  428. }
  429. }
  430. // Resolve "depends_on" and show / hide accordingly
  431. _f.Frm.prototype.refresh_dependency = function() {
  432. var me = this;
  433. var doc = locals[this.doctype][this.docname];
  434. // build dependants' dictionary
  435. var has_dep = false;
  436. for(fkey in me.fields) {
  437. var f = me.fields[fkey];
  438. f.dependencies_clear = true;
  439. if(f.df.depends_on) {
  440. has_dep = true;
  441. }
  442. }
  443. if(!has_dep)return;
  444. // show / hide based on values
  445. for(var i=me.fields.length-1;i>=0;i--) {
  446. var f = me.fields[i];
  447. f.guardian_has_value = true;
  448. if(f.df.depends_on) {
  449. // evaluate guardian
  450. var v = doc[f.df.depends_on];
  451. if(f.df.depends_on.substr(0,5)=='eval:') {
  452. f.guardian_has_value = eval(f.df.depends_on.substr(5));
  453. } else if(f.df.depends_on.substr(0,3)=='fn:') {
  454. f.guardian_has_value = me.script_manager.trigger(f.df.depends_on.substr(3), me.doctype, me.docname);
  455. } else {
  456. if(!v) {
  457. f.guardian_has_value = false;
  458. }
  459. }
  460. // show / hide
  461. if(f.guardian_has_value) {
  462. if(f.df.hidden != 0) {
  463. f.df.hidden = 0;
  464. f.refresh();
  465. }
  466. } else {
  467. if(f.df.hidden != 1) {
  468. f.df.hidden = 1;
  469. f.refresh();
  470. }
  471. }
  472. }
  473. }
  474. }
  475. // setnewdoc is called when a record is loaded for the first time
  476. // ======================================================================================
  477. _f.Frm.prototype.setnewdoc = function(docname) {
  478. // moved this call to refresh function
  479. // this.check_doctype_conflict(docname);
  480. // if loaded
  481. if(this.opendocs[docname]) { // already exists
  482. this.docname=docname;
  483. return;
  484. }
  485. this.docname = docname;
  486. var me = this;
  487. var viewname = this.meta.issingle ? this.doctype : docname;
  488. // Client Script
  489. this.script_manager.trigger("onload");
  490. this.last_view_is_edit[docname] = 1;
  491. if(cint(this.meta.read_only_onload)) this.last_view_is_edit[docname] = 0;
  492. this.opendocs[docname] = true;
  493. }
  494. _f.Frm.prototype.edit_doc = function() {
  495. // set fields
  496. this.last_view_is_edit[this.docname] = true;
  497. this.refresh();
  498. }
  499. _f.Frm.prototype.runscript = function(scriptname, callingfield, onrefresh) {
  500. var me = this;
  501. if(this.docname) {
  502. // make doc list
  503. var doclist = wn.model.compress(make_doclist(this.doctype, this.docname));
  504. // send to run
  505. if(callingfield)
  506. $(callingfield.input).set_working();
  507. $c('runserverobj', {'docs':doclist, 'method':scriptname },
  508. function(r, rtxt) {
  509. // run refresh
  510. if(onrefresh)
  511. onrefresh(r,rtxt);
  512. // fields
  513. me.refresh_fields();
  514. // enable button
  515. if(callingfield)
  516. $(callingfield.input).done_working();
  517. }
  518. );
  519. }
  520. }
  521. _f.Frm.prototype.copy_doc = function(onload, from_amend) {
  522. if(!this.perm[0][CREATE]) {
  523. msgprint('You are not allowed to create '+this.meta.name);
  524. return;
  525. }
  526. var dn = this.docname;
  527. // copy parent
  528. var newdoc = wn.model.copy_doc(this.doctype, dn, from_amend);
  529. // copy chidren
  530. var dl = make_doclist(this.doctype, dn);
  531. // table fields dict - for no_copy check
  532. var tf_dict = {};
  533. for(var d in dl) {
  534. d1 = dl[d];
  535. // get tabel field
  536. if(d1.parentfield && !tf_dict[d1.parentfield]) {
  537. tf_dict[d1.parentfield] = wn.meta.get_docfield(d1.parenttype, d1.parentfield);
  538. }
  539. if(d1.parent==dn && cint(tf_dict[d1.parentfield].no_copy)!=1) {
  540. var ch = wn.model.copy_doc(d1.doctype, d1.name, from_amend);
  541. ch.parent = newdoc.name;
  542. ch.docstatus = 0;
  543. ch.owner = user;
  544. ch.creation = '';
  545. ch.modified_by = user;
  546. ch.modified = '';
  547. }
  548. }
  549. newdoc.__islocal = 1;
  550. newdoc.docstatus = 0;
  551. newdoc.owner = user;
  552. newdoc.creation = '';
  553. newdoc.modified_by = user;
  554. newdoc.modified = '';
  555. if(onload)onload(newdoc);
  556. loaddoc(newdoc.doctype, newdoc.name);
  557. }
  558. _f.Frm.prototype.reload_doc = function() {
  559. this.check_doctype_conflict(this.docname);
  560. var me = this;
  561. var onsave = function(r, rtxt) {
  562. me.refresh();
  563. }
  564. if(!me.doc.__islocal) {
  565. wn.model.remove_from_locals(me.doctype, me.docname);
  566. wn.model.with_doc(me.doctype, me.docname, function() {
  567. me.refresh();
  568. })
  569. }
  570. }
  571. var validated;
  572. _f.Frm.prototype.save = function(save_action, callback, btn, on_error) {
  573. $(document.activeElement).blur();
  574. var me = this;
  575. if((!this.meta.in_dialog || this.in_form) && !this.meta.istable)
  576. scroll(0, 0);
  577. // validate
  578. if(save_action!="Cancel") {
  579. validated = true;
  580. this.script_manager.trigger("validate");
  581. if(!validated) {
  582. if(on_error)
  583. on_error();
  584. return;
  585. }
  586. }
  587. var doclist = new wn.model.DocList(this.doctype, this.docname);
  588. doclist.save(save_action || "Save", function(r) {
  589. if(!r.exc) {
  590. me.refresh();
  591. } else {
  592. if(on_error)
  593. on_error();
  594. }
  595. callback && callback(r);
  596. }, btn);
  597. }
  598. _f.Frm.prototype.savesubmit = function(btn, on_error) {
  599. var me = this;
  600. wn.confirm("Permanently Submit "+this.docname+"?", function() {
  601. me.save('Submit', function(r) {
  602. if(!r.exc) {
  603. me.script_manager.trigger("on_submit");
  604. }
  605. }, btn, on_error);
  606. });
  607. }
  608. _f.Frm.prototype.savecancel = function(btn, on_error) {
  609. var me = this;
  610. wn.confirm("Permanently Cancel "+this.docname+"?", function() {
  611. validated = true;
  612. me.script_manager.trigger("before_cancel");
  613. if(!validated) {
  614. if(on_error)
  615. on_error();
  616. return;
  617. }
  618. var doclist = new wn.model.DocList(me.doctype, me.docname);
  619. doclist.cancel(function(r) {
  620. if(!r.exc) {
  621. me.refresh();
  622. me.script_manager.trigger("after_cancel");
  623. }
  624. }, btn, on_error);
  625. });
  626. }
  627. // delete the record
  628. _f.Frm.prototype.savetrash = function() {
  629. wn.model.delete_doc(this.doctype, this.docname, function(r) {
  630. window.history.back();
  631. })
  632. }
  633. _f.Frm.prototype.amend_doc = function() {
  634. if(!this.fields_dict['amended_from']) {
  635. alert('"amended_from" field must be present to do an amendment.');
  636. return;
  637. }
  638. var me = this;
  639. var fn = function(newdoc) {
  640. newdoc.amended_from = me.docname;
  641. if(me.fields_dict && me.fields_dict['amendment_date'])
  642. newdoc.amendment_date = dateutil.obj_to_str(new Date());
  643. }
  644. this.copy_doc(fn, 1);
  645. }
  646. _f.Frm.prototype.disable_save = function() {
  647. // IMPORTANT: this function should be called in refresh event
  648. cur_frm.save_disabled = true;
  649. cur_frm.footer.hide_save();
  650. cur_frm.appframe.buttons.Save.remove();
  651. delete cur_frm.appframe.buttons.Save
  652. }
  653. _f.Frm.prototype.save_or_update = function() {
  654. if(this.save_disabled) return;
  655. if(this.doc.docstatus===0) {
  656. this.save();
  657. } else if(this.doc.docstatus===1 && this.doc.__unsaved) {
  658. this.frm_head.appframe.buttons['Update'].click();
  659. }
  660. }
  661. _f.get_value = function(dt, dn, fn) {
  662. if(locals[dt] && locals[dt][dn])
  663. return locals[dt][dn][fn];
  664. }
  665. _f.Frm.prototype.dirty = function() {
  666. this.doc.__unsaved = 1;
  667. $(this.wrapper).trigger('dirty')
  668. }
  669. _f.Frm.prototype.get_docinfo = function() {
  670. return wn.model.docinfo[this.doctype][this.docname];
  671. }