You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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