Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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