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

958 行
24 KiB

  1. // Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  2. // MIT License. See license.txt
  3. /* Form page structure
  4. + this.parent (either FormContainer or Dialog)
  5. + this.wrapper
  6. + this.toolbar
  7. + this.form_wrapper
  8. + this.head
  9. + this.body
  10. + this.layout
  11. + this.sidebar
  12. + this.footer
  13. */
  14. frappe.provide('_f');
  15. frappe.provide('frappe.ui.form');
  16. frappe.ui.form.Controller = Class.extend({
  17. init: function(opts) {
  18. $.extend(this, opts);
  19. this.setup && this.setup();
  20. }
  21. });
  22. _f.frms = {};
  23. _f.Frm = function(doctype, parent, in_form) {
  24. this.docname = '';
  25. this.doctype = doctype;
  26. this.hidden = false;
  27. this.refresh_if_stale_for = 120;
  28. var me = this;
  29. this.opendocs = {};
  30. this.sections = [];
  31. this.grids = [];
  32. this.cscript = new frappe.ui.form.Controller({frm:this});
  33. this.events = {};
  34. this.pformat = {};
  35. this.fetch_dict = {};
  36. this.parent = parent;
  37. this.tinymce_id_list = [];
  38. this.setup_meta(doctype);
  39. // show in form instead of in dialog, when called using url (router.js)
  40. this.in_form = in_form ? true : false;
  41. // notify on rename
  42. var me = this;
  43. $(document).on('rename', function(event, dt, old_name, new_name) {
  44. if(dt==me.doctype)
  45. me.rename_notify(dt, old_name, new_name)
  46. });
  47. }
  48. _f.Frm.prototype.check_doctype_conflict = function(docname) {
  49. var me = this;
  50. if(this.doctype=='DocType' && docname=='DocType') {
  51. msgprint(__('Allowing DocType, DocType. Be careful!'))
  52. } else if(this.doctype=='DocType') {
  53. if (frappe.views.formview[docname] || frappe.pages['List/'+docname]) {
  54. window.location.reload();
  55. // msgprint(__("Cannot open {0} when its instance is open", ['DocType']))
  56. // throw 'doctype open conflict'
  57. }
  58. } else {
  59. if (frappe.views.formview.DocType && frappe.views.formview.DocType.frm.opendocs[this.doctype]) {
  60. window.location.reload();
  61. // msgprint(__("Cannot open instance when its {0} is open", ['DocType']))
  62. // throw 'doctype open conflict'
  63. }
  64. }
  65. }
  66. _f.Frm.prototype.setup = function() {
  67. var me = this;
  68. this.fields = [];
  69. this.fields_dict = {};
  70. this.state_fieldname = frappe.workflow.get_state_fieldname(this.doctype);
  71. // wrapper
  72. this.wrapper = this.parent;
  73. frappe.ui.make_app_page({
  74. parent: this.wrapper,
  75. single_column: this.meta.hide_toolbar
  76. });
  77. this.page = this.wrapper.page;
  78. this.layout_main = this.page.main.get(0);
  79. this.toolbar = new frappe.ui.form.Toolbar({
  80. frm: this,
  81. page: this.page
  82. });
  83. // print layout
  84. this.setup_print_layout();
  85. // 2 column layout
  86. this.setup_std_layout();
  87. // client script must be called after "setup" - there are no fields_dict attached to the frm otherwise
  88. this.script_manager = new frappe.ui.form.ScriptManager({
  89. frm: this
  90. });
  91. this.script_manager.setup();
  92. this.watch_model_updates();
  93. if(!this.meta.hide_toolbar) {
  94. this.footer = new frappe.ui.form.Footer({
  95. frm: this,
  96. parent: $('<div>').appendTo(this.page.main.parent())
  97. })
  98. $("body").attr("data-sidebar", 1);
  99. }
  100. this.setup_drag_drop();
  101. this.setup_done = true;
  102. }
  103. _f.Frm.prototype.setup_drag_drop = function() {
  104. var me = this;
  105. $(this.wrapper).on('dragenter dragover', false)
  106. .on('drop', function (e) {
  107. var dataTransfer = e.originalEvent.dataTransfer;
  108. if (!(dataTransfer && dataTransfer.files && dataTransfer.files.length > 0)) {
  109. return;
  110. }
  111. e.stopPropagation();
  112. e.preventDefault();
  113. if(me.doc.__islocal) {
  114. msgprint(__("Please save before attaching."));
  115. throw "attach error";
  116. }
  117. if(me.attachments.max_reached()) {
  118. msgprint(__("Maximum Attachment Limit for this record reached."));
  119. throw "attach error";
  120. }
  121. frappe.upload.upload_file(dataTransfer.files[0], me.attachments.get_args(), {
  122. callback: function(attachment, r) {
  123. me.attachments.attachment_uploaded(attachment, r);
  124. },
  125. confirm_is_private: true
  126. });
  127. });
  128. }
  129. _f.Frm.prototype.setup_print_layout = function() {
  130. var me = this;
  131. this.print_preview = new frappe.ui.form.PrintPreview({
  132. frm: this
  133. });
  134. // show edit button for print view
  135. this.page.wrapper.on('view-change', function() {
  136. me.toolbar.set_primary_action();
  137. });
  138. }
  139. _f.Frm.prototype.print_doc = function() {
  140. if(this.print_preview.wrapper.is(":visible")) {
  141. this.hide_print();
  142. return;
  143. }
  144. if(!frappe.model.can_print(this.doc.doctype, this)) {
  145. msgprint(__("You are not allowed to print this document"));
  146. return;
  147. }
  148. this.print_preview.refresh_print_options().trigger("change");
  149. this.page.set_view("print");
  150. this.print_preview.set_user_lang();
  151. }
  152. _f.Frm.prototype.set_hidden = function(status) {
  153. // set hidden if hide_first is set
  154. this.hidden = status;
  155. var form_page = this.page.wrapper.find('.form-page');
  156. form_page.toggleClass('hidden', this.hidden);
  157. this.toolbar.refresh();
  158. if(status===true) {
  159. msg = __('Edit {0} properties', [__(this.doctype)]);
  160. this.layout.show_message('<div style="padding-left: 15px; padding-right: 15px;">\
  161. <a class="text-muted" onclick="cur_frm.set_hidden(false)">' + msg + '</a></div>');
  162. } else {
  163. // clear message
  164. this.layout.show_message();
  165. frappe.utils.scroll_to(form_page);
  166. }
  167. }
  168. _f.Frm.prototype.hide_print = function() {
  169. if(this.setup_done && this.page.current_view_name==="print") {
  170. this.page.set_view(this.page.previous_view_name==="print" ?
  171. "main" : (this.page.previous_view_name || "main"));
  172. }
  173. }
  174. _f.Frm.prototype.watch_model_updates = function() {
  175. // watch model updates
  176. var me = this;
  177. // on main doc
  178. frappe.model.on(me.doctype, "*", function(fieldname, value, doc) {
  179. // set input
  180. if(doc.name===me.docname) {
  181. me.dirty();
  182. me.fields_dict[fieldname]
  183. && me.fields_dict[fieldname].refresh(fieldname);
  184. me.layout.refresh_dependency();
  185. me.script_manager.trigger(fieldname, doc.doctype, doc.name);
  186. }
  187. })
  188. // on table fields
  189. var table_fields = frappe.get_children("DocType", me.doctype, "fields", {fieldtype:"Table"});
  190. // using $.each to preserve df via closure
  191. $.each(table_fields, function(i, df) {
  192. frappe.model.on(df.options, "*", function(fieldname, value, doc) {
  193. if(doc.parent===me.docname && doc.parentfield===df.fieldname) {
  194. me.dirty();
  195. me.fields_dict[df.fieldname].grid.set_value(fieldname, value, doc);
  196. me.script_manager.trigger(fieldname, doc.doctype, doc.name);
  197. }
  198. });
  199. });
  200. }
  201. _f.Frm.prototype.setup_std_layout = function() {
  202. this.form_wrapper = $('<div></div>').appendTo(this.layout_main);
  203. this.body = $('<div></div>').appendTo(this.form_wrapper);
  204. // only tray
  205. this.meta.section_style='Simple'; // always simple!
  206. // layout
  207. this.layout = new frappe.ui.form.Layout({
  208. parent: this.body,
  209. doctype: this.doctype,
  210. frm: this,
  211. });
  212. this.layout.make();
  213. this.fields_dict = this.layout.fields_dict;
  214. this.fields = this.layout.fields_list;
  215. this.document_flow = new frappe.ui.form.DocumentFlow({
  216. frm: this
  217. });
  218. this.dashboard = new frappe.ui.form.Dashboard({
  219. frm: this,
  220. });
  221. // state
  222. this.states = new frappe.ui.form.States({
  223. frm: this
  224. });
  225. }
  226. // email the form
  227. _f.Frm.prototype.email_doc = function(message) {
  228. new frappe.views.CommunicationComposer({
  229. doc: this.doc,
  230. frm: this,
  231. subject: __(this.meta.name) + ': ' + this.docname,
  232. recipients: this.doc.email || this.doc.email_id || this.doc.contact_email,
  233. attach_document_print: true,
  234. message: message,
  235. real_name: this.doc.real_name || this.doc.contact_display || this.doc.contact_name
  236. });
  237. }
  238. // rename the form
  239. _f.Frm.prototype.rename_doc = function() {
  240. frappe.model.rename_doc(this.doctype, this.docname);
  241. }
  242. _f.Frm.prototype.share_doc = function() {
  243. this.shared.show();
  244. }
  245. // notify this form of renamed records
  246. _f.Frm.prototype.rename_notify = function(dt, old, name) {
  247. // from form
  248. if(this.meta.istable)
  249. return;
  250. if(this.docname == old)
  251. this.docname = name;
  252. else
  253. return;
  254. // cleanup
  255. if(this && this.opendocs[old] && frappe.meta.docfield_copy[dt]) {
  256. // delete docfield copy
  257. frappe.meta.docfield_copy[dt][name] = frappe.meta.docfield_copy[dt][old];
  258. delete frappe.meta.docfield_copy[dt][old];
  259. }
  260. delete this.opendocs[old];
  261. this.opendocs[name] = true;
  262. if(this.meta.in_dialog || !this.in_form) {
  263. return;
  264. }
  265. frappe.re_route[window.location.hash] = '#Form/' + encodeURIComponent(this.doctype) + '/' + encodeURIComponent(name);
  266. frappe.set_route('Form', this.doctype, name);
  267. }
  268. // SETUP
  269. _f.Frm.prototype.setup_meta = function(doctype) {
  270. this.meta = frappe.get_doc('DocType',this.doctype);
  271. this.perm = frappe.perm.get_perm(this.doctype); // for create
  272. if(this.meta.istable) { this.meta.in_dialog = 1 }
  273. }
  274. _f.Frm.prototype.refresh_header = function(is_a_different_doc) {
  275. // set title
  276. // main title
  277. if(!this.meta.in_dialog || this.in_form) {
  278. frappe.utils.set_title(this.meta.issingle ? this.doctype : this.docname);
  279. }
  280. if(frappe.ui.toolbar.recent)
  281. frappe.ui.toolbar.recent.add(this.doctype, this.docname, 1);
  282. // show / hide buttons
  283. if(this.toolbar) {
  284. if (is_a_different_doc) {
  285. this.toolbar.current_status = undefined;
  286. }
  287. this.toolbar.refresh();
  288. }
  289. this.document_flow.refresh();
  290. this.dashboard.refresh();
  291. if(this.meta.is_submittable &&
  292. ! this.is_dirty() &&
  293. ! this.is_new() &&
  294. this.doc.docstatus===0) {
  295. this.dashboard.add_comment(__('Submit this document to confirm'), true);
  296. }
  297. this.clear_custom_buttons();
  298. this.show_web_link();
  299. }
  300. _f.Frm.prototype.show_web_link = function() {
  301. var doc = this.doc, me = this;
  302. if(!doc.__islocal && doc.__onload && doc.__onload.is_website_generator) {
  303. me.web_link && me.web_link.remove();
  304. if(doc.__onload.published) {
  305. me.add_web_link("/" + doc.route)
  306. }
  307. }
  308. }
  309. _f.Frm.prototype.add_web_link = function(path) {
  310. this.web_link = this.sidebar.add_user_action("See on Website",
  311. function() {}).attr("href", path || this.doc.route).attr("target", "_blank");
  312. }
  313. _f.Frm.prototype.check_doc_perm = function() {
  314. // get perm
  315. var dt = this.parent_doctype?this.parent_doctype : this.doctype;
  316. this.perm = frappe.perm.get_perm(dt, this.doc);
  317. if(!this.perm[0].read) {
  318. return 0;
  319. }
  320. return 1
  321. }
  322. _f.Frm.prototype.refresh = function(docname) {
  323. var is_a_different_doc = docname ? true : false;
  324. if(docname) {
  325. // record switch
  326. if(this.docname != docname && (!this.meta.in_dialog || this.in_form) &&
  327. !this.meta.istable) {
  328. frappe.utils.scroll_to(0);
  329. this.hide_print();
  330. }
  331. frappe.ui.form.close_grid_form();
  332. this.docname = docname;
  333. }
  334. cur_frm = this;
  335. if(this.docname) { // document to show
  336. // set the doc
  337. this.doc = frappe.get_doc(this.doctype, this.docname);
  338. // check permissions
  339. if(!this.check_doc_perm()) {
  340. frappe.show_not_permitted(__(this.doctype) + " " + __(this.docname));
  341. return;
  342. }
  343. // read only (workflow)
  344. this.read_only = frappe.workflow.is_read_only(this.doctype, this.docname);
  345. // check if doctype is already open
  346. if (!this.opendocs[this.docname]) {
  347. this.check_doctype_conflict(this.docname);
  348. } else {
  349. if(this.doc && (!this.doc.__unsaved) && this.doc.__last_sync_on &&
  350. (new Date() - this.doc.__last_sync_on) > (this.refresh_if_stale_for * 1000)) {
  351. this.reload_doc();
  352. return;
  353. }
  354. }
  355. // do setup
  356. if(!this.setup_done) this.setup();
  357. // load the record for the first time, if not loaded (call 'onload')
  358. this.cscript.is_onload = false;
  359. if(!this.opendocs[this.docname]) {
  360. var me = this;
  361. this.cscript.is_onload = true;
  362. this.setnewdoc();
  363. $(document).trigger("form-load", [this]);
  364. $(this.page.wrapper).on('hide', function(e) {
  365. $(document).trigger("form-unload", [me]);
  366. });
  367. } else {
  368. this.render_form(is_a_different_doc);
  369. if (this.doc.localname) {
  370. // trigger form-rename and remove .localname
  371. delete this.doc.localname;
  372. $(document).trigger("form-rename", [this]);
  373. }
  374. }
  375. // if print format is shown, refresh the format
  376. if(this.print_preview.wrapper.is(":visible")) {
  377. this.print_preview.preview();
  378. }
  379. if(is_a_different_doc) {
  380. if(this.show_print_first && this.doc.docstatus===1) {
  381. // show print view
  382. this.print_doc();
  383. } else {
  384. if(this.hide_first && !this.doc.__unsaved && !this.doc.__islocal) {
  385. this.set_hidden(true);
  386. } else {
  387. if(this.hidden) {
  388. this.set_hidden(false);
  389. }
  390. }
  391. }
  392. }
  393. this.show_if_needs_refresh();
  394. }
  395. }
  396. _f.Frm.prototype.show_if_needs_refresh = function() {
  397. if(this.doc.__needs_refresh) {
  398. if(this.doc.__unsaved) {
  399. this.dashboard.set_headline_alert(__("This form has been modified after you have loaded it")
  400. + '<a class="btn btn-xs btn-primary pull-right" onclick="cur_frm.reload_doc()">'
  401. + __("Refresh") + '</a>', "alert-warning");
  402. } else {
  403. this.reload_doc();
  404. }
  405. }
  406. }
  407. _f.Frm.prototype.render_form = function(is_a_different_doc) {
  408. if(!this.meta.istable) {
  409. this.layout.doc = this.doc;
  410. this.layout.attach_doc_and_docfields()
  411. this.sidebar = new frappe.ui.form.Sidebar({
  412. frm: this,
  413. page: this.page
  414. });
  415. this.sidebar.make();
  416. // header must be refreshed before client methods
  417. // because add_custom_button
  418. this.refresh_header(is_a_different_doc);
  419. // clear layout message
  420. this.layout.show_message();
  421. // call trigger
  422. this.script_manager.trigger("refresh");
  423. // trigger global trigger
  424. // to use this
  425. $(document).trigger('form_refresh', [this]);
  426. // fields
  427. this.refresh_fields();
  428. // call onload post render for callbacks to be fired
  429. if(this.cscript.is_onload) {
  430. this.script_manager.trigger("onload_post_render");
  431. }
  432. // focus on first input
  433. if(this.doc.docstatus==0) {
  434. var first = this.form_wrapper.find('.form-layout :input:first');
  435. if(!in_list(["Date", "Datetime"], first.attr("data-fieldtype"))) {
  436. first.focus();
  437. }
  438. }
  439. } else {
  440. this.refresh_header(is_a_different_doc);
  441. }
  442. $(this.wrapper).trigger('render_complete');
  443. if(!this.hidden) {
  444. this.layout.show_empty_form_message();
  445. }
  446. this.scroll_to_element();
  447. }
  448. _f.Frm.prototype.refresh_field = function(fname) {
  449. if(this.fields_dict[fname] && this.fields_dict[fname].refresh) {
  450. this.fields_dict[fname].refresh();
  451. this.layout.refresh_dependency();
  452. }
  453. }
  454. _f.Frm.prototype.refresh_fields = function() {
  455. this.layout.refresh(this.doc);
  456. this.layout.primary_button = $(this.wrapper).find(".btn-primary");
  457. // cleanup activities after refresh
  458. this.cleanup_refresh(this);
  459. }
  460. _f.Frm.prototype.cleanup_refresh = function() {
  461. var me = this;
  462. if(me.fields_dict['amended_from']) {
  463. if (me.doc.amended_from) {
  464. unhide_field('amended_from');
  465. if (me.fields_dict['amendment_date']) unhide_field('amendment_date');
  466. } else {
  467. hide_field('amended_from');
  468. if (me.fields_dict['amendment_date']) hide_field('amendment_date');
  469. }
  470. }
  471. if(me.fields_dict['trash_reason']) {
  472. if(me.doc.trash_reason && me.doc.docstatus == 2) {
  473. unhide_field('trash_reason');
  474. } else {
  475. hide_field('trash_reason');
  476. }
  477. }
  478. if(me.meta.autoname && me.meta.autoname.substr(0,6)=='field:' && !me.doc.__islocal) {
  479. var fn = me.meta.autoname.substr(6);
  480. if (me.doc[fn]) {
  481. me.toggle_display(fn, false);
  482. }
  483. }
  484. if(me.meta.autoname=="naming_series:" && !me.doc.__islocal) {
  485. me.toggle_display("naming_series", false);
  486. }
  487. }
  488. _f.Frm.prototype.setnewdoc = function() {
  489. // moved this call to refresh function
  490. // this.check_doctype_conflict(docname);
  491. var me = this;
  492. // hide any open grid
  493. this.script_manager.trigger("before_load", this.doctype, this.docname, function() {
  494. me.script_manager.trigger("onload");
  495. me.opendocs[me.docname] = true;
  496. me.render_form();
  497. frappe.after_ajax(function() {
  498. me.trigger_link_fields();
  499. });
  500. frappe.breadcrumbs.add(me.meta.module, me.doctype)
  501. });
  502. // update seen
  503. if(this.meta.track_seen) {
  504. $('.list-id[data-name="'+ me.docname +'"]').addClass('seen');
  505. }
  506. }
  507. _f.Frm.prototype.trigger_link_fields = function() {
  508. // trigger link fields which have default values set
  509. if (this.is_new() && this.doc.__run_link_triggers) {
  510. $.each(this.fields_dict, function(fieldname, field) {
  511. if (field.df.fieldtype=="Link" && this.doc[fieldname]) {
  512. // triggers add fetch, sets value in model and runs triggers
  513. field.set_value(this.doc[fieldname]);
  514. }
  515. });
  516. delete this.doc.__run_link_triggers;
  517. }
  518. }
  519. _f.Frm.prototype.runscript = function(scriptname, callingfield, onrefresh) {
  520. var me = this;
  521. if(this.docname) {
  522. // send to run
  523. if(callingfield)
  524. $(callingfield.input).set_working();
  525. frappe.call({
  526. method: "runserverobj",
  527. args: {'docs':this.doc, 'method':scriptname },
  528. btn: callingfield.$input,
  529. callback: function(r) {
  530. if(!r.exc) {
  531. if(onrefresh) {
  532. onrefresh(r);
  533. }
  534. me.refresh_fields();
  535. }
  536. }
  537. });
  538. }
  539. }
  540. _f.Frm.prototype.copy_doc = function(onload, from_amend) {
  541. this.validate_form_action("Create");
  542. var newdoc = frappe.model.copy_doc(this.doc, from_amend);
  543. newdoc.idx = null;
  544. newdoc.__run_link_triggers = false;
  545. if(onload) {
  546. onload(newdoc);
  547. }
  548. frappe.set_route('Form', newdoc.doctype, newdoc.name);
  549. }
  550. _f.Frm.prototype.reload_doc = function() {
  551. this.check_doctype_conflict(this.docname);
  552. var me = this;
  553. var onsave = function(r, rtxt) {
  554. me.refresh();
  555. }
  556. if(!me.doc.__islocal) {
  557. frappe.model.remove_from_locals(me.doctype, me.docname);
  558. frappe.model.with_doc(me.doctype, me.docname, function() {
  559. me.refresh();
  560. })
  561. }
  562. }
  563. var validated;
  564. _f.Frm.prototype.save = function(save_action, callback, btn, on_error) {
  565. btn && $(btn).prop("disabled", true);
  566. $(document.activeElement).blur();
  567. frappe.ui.form.close_grid_form();
  568. // let any pending js process finish
  569. var me = this;
  570. setTimeout(function() { me._save(save_action, callback, btn, on_error) }, 100);
  571. }
  572. _f.Frm.prototype._save = function(save_action, callback, btn, on_error) {
  573. var me = this;
  574. if(!save_action) save_action = "Save";
  575. this.validate_form_action(save_action);
  576. if((!this.meta.in_dialog || this.in_form) && !this.meta.istable) {
  577. frappe.utils.scroll_to(0);
  578. }
  579. var after_save = function(r) {
  580. if(!r.exc) {
  581. if (["Save", "Update", "Amend"].indexOf(save_action)!==-1) {
  582. frappe.utils.play_sound("click");
  583. }
  584. me.script_manager.trigger("after_save");
  585. me.refresh();
  586. } else {
  587. if(on_error)
  588. on_error();
  589. }
  590. callback && callback(r);
  591. }
  592. if(save_action != "Update") {
  593. // validate
  594. validated = true;
  595. $.when(this.script_manager.trigger("validate"), this.script_manager.trigger("before_save"))
  596. .done(function() {
  597. // done is called after all ajaxes in validate & before_save are completed :)
  598. if(!validated) {
  599. btn && $(btn).prop("disabled", false);
  600. if(on_error) {
  601. on_error();
  602. }
  603. return;
  604. }
  605. frappe.ui.form.save(me, save_action, after_save, btn);
  606. });
  607. } else {
  608. frappe.ui.form.save(me, save_action, after_save, btn);
  609. }
  610. }
  611. _f.Frm.prototype.savesubmit = function(btn, callback, on_error) {
  612. var me = this;
  613. this.validate_form_action("Submit");
  614. frappe.confirm(__("Permanently Submit {0}?", [this.docname]), function() {
  615. validated = true;
  616. me.script_manager.trigger("before_submit").done(function() {
  617. if(!validated) {
  618. if(on_error)
  619. on_error();
  620. return;
  621. }
  622. me.save('Submit', function(r) {
  623. if(!r.exc) {
  624. frappe.utils.play_sound("submit");
  625. callback && callback();
  626. me.script_manager.trigger("on_submit");
  627. }
  628. }, btn, on_error);
  629. });
  630. }, on_error);
  631. };
  632. _f.Frm.prototype.savecancel = function(btn, callback, on_error) {
  633. var me = this;
  634. this.validate_form_action('Cancel');
  635. frappe.confirm(__("Permanently Cancel {0}?", [this.docname]), function() {
  636. validated = true;
  637. me.script_manager.trigger("before_cancel").done(function() {
  638. if(!validated) {
  639. if(on_error)
  640. on_error();
  641. return;
  642. }
  643. var after_cancel = function(r) {
  644. if(!r.exc) {
  645. frappe.utils.play_sound("cancel");
  646. me.refresh();
  647. callback && callback();
  648. me.script_manager.trigger("after_cancel");
  649. } else {
  650. on_error();
  651. }
  652. }
  653. frappe.ui.form.save(me, "cancel", after_cancel, btn);
  654. });
  655. }, on_error);
  656. }
  657. // delete the record
  658. _f.Frm.prototype.savetrash = function() {
  659. this.validate_form_action("Delete");
  660. frappe.model.delete_doc(this.doctype, this.docname, function(r) {
  661. window.history.back();
  662. })
  663. }
  664. _f.Frm.prototype.amend_doc = function() {
  665. if(!this.fields_dict['amended_from']) {
  666. alert('"amended_from" field must be present to do an amendment.');
  667. return;
  668. }
  669. this.validate_form_action("Amend");
  670. var me = this;
  671. var fn = function(newdoc) {
  672. newdoc.amended_from = me.docname;
  673. if(me.fields_dict && me.fields_dict['amendment_date'])
  674. newdoc.amendment_date = dateutil.obj_to_str(new Date());
  675. }
  676. this.copy_doc(fn, 1);
  677. frappe.utils.play_sound("click");
  678. }
  679. _f.Frm.prototype.disable_save = function() {
  680. // IMPORTANT: this function should be called in refresh event
  681. this.save_disabled = true;
  682. this.toolbar.current_status = null;
  683. this.page.clear_primary_action();
  684. }
  685. _f.Frm.prototype.enable_save = function() {
  686. this.save_disabled = false;
  687. this.toolbar.set_primary_action();
  688. }
  689. _f.Frm.prototype.save_or_update = function() {
  690. if(this.save_disabled) return;
  691. if(this.doc.docstatus===0) {
  692. this.save();
  693. } else if(this.doc.docstatus===1 && this.doc.__unsaved) {
  694. this.save("Update");
  695. }
  696. }
  697. _f.Frm.prototype.dirty = function() {
  698. this.doc.__unsaved = 1;
  699. $(this.wrapper).trigger('dirty');
  700. }
  701. _f.Frm.prototype.get_docinfo = function() {
  702. return frappe.model.docinfo[this.doctype][this.docname];
  703. }
  704. _f.Frm.prototype.is_dirty = function() {
  705. return this.doc.__unsaved;
  706. }
  707. _f.Frm.prototype.is_new = function() {
  708. return this.doc.__islocal;
  709. }
  710. _f.Frm.prototype.reload_docinfo = function(callback) {
  711. var me = this;
  712. frappe.call({
  713. method: "frappe.desk.form.load.get_docinfo",
  714. args: {
  715. doctype: me.doctype,
  716. name: me.doc.name
  717. },
  718. callback: function(r) {
  719. // docinfo will be synced
  720. if(callback) callback(r.docinfo);
  721. me.timeline.refresh();
  722. me.assign_to.refresh();
  723. me.attachments.refresh();
  724. }
  725. })
  726. }
  727. _f.Frm.prototype.get_perm = function(permlevel, access_type) {
  728. return this.perm[permlevel] ? this.perm[permlevel][access_type] : null;
  729. }
  730. _f.Frm.prototype.set_intro = function(txt, append) {
  731. this.dashboard.set_headline_alert(txt);
  732. //frappe.utils.set_intro(this, this.body, txt, append);
  733. }
  734. _f.Frm.prototype.set_footnote = function(txt) {
  735. frappe.utils.set_footnote(this, this.body, txt);
  736. }
  737. _f.Frm.prototype.add_custom_button = function(label, fn, group) {
  738. // temp! old parameter used to be icon
  739. if(group && group.indexOf("icon")!==-1) group = null;
  740. return this.page.add_inner_button(label, fn, group);
  741. }
  742. _f.Frm.prototype.clear_custom_buttons = function() {
  743. this.page.clear_inner_toolbar();
  744. this.page.clear_user_actions();
  745. }
  746. _f.Frm.prototype.add_fetch = function(link_field, src_field, tar_field) {
  747. if(!this.fetch_dict[link_field]) {
  748. this.fetch_dict[link_field] = {'columns':[], 'fields':[]}
  749. }
  750. this.fetch_dict[link_field].columns.push(src_field);
  751. this.fetch_dict[link_field].fields.push(tar_field);
  752. }
  753. _f.Frm.prototype.set_print_heading = function(txt) {
  754. this.pformat[this.docname] = txt;
  755. }
  756. _f.Frm.prototype.action_perm_type_map = {
  757. "Create": "create",
  758. "Save": "write",
  759. "Submit": "submit",
  760. "Update": "submit",
  761. "Cancel": "cancel",
  762. "Amend": "amend",
  763. "Delete": "delete"
  764. };
  765. _f.Frm.prototype.validate_form_action = function(action) {
  766. var perm_to_check = this.action_perm_type_map[action];
  767. if (!this.perm[0][perm_to_check]) {
  768. frappe.throw (__("No permission to '{0}' {1}", [__(action), __(this.doc.doctype)]));
  769. }
  770. };
  771. _f.Frm.prototype.get_handlers = function(fieldname, doctype, docname) {
  772. return this.script_manager.get_handlers(fieldname, doctype || this.doctype, docname || this.docname)
  773. }
  774. _f.Frm.prototype.has_perm = function(ptype) {
  775. return frappe.perm.has_perm(this.doctype, 0, ptype, this.doc);
  776. }
  777. _f.Frm.prototype.scroll_to_element = function() {
  778. if (frappe.route_options && frappe.route_options.scroll_to) {
  779. var scroll_to = frappe.route_options.scroll_to;
  780. delete frappe.route_options.scroll_to;
  781. var selector = [];
  782. for (var key in scroll_to) {
  783. var value = scroll_to[key];
  784. selector.push(repl('[data-%(key)s="%(value)s"]', {key: key, value: value}));
  785. }
  786. selector = $(selector.join(" "));
  787. if (selector.length) {
  788. frappe.utils.scroll_to(selector);
  789. }
  790. } else {
  791. frappe.utils.scroll_to(0);
  792. }
  793. }