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

13 年前
10 年前
13 年前
13 年前
13 年前
13 年前
11 年前
9 年前
9 年前
9 年前
9 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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.refresh();
  290. if(this.meta.is_submittable &&
  291. ! this.is_dirty() &&
  292. ! this.is_new() &&
  293. this.doc.docstatus===0) {
  294. this.dashboard.add_comment(__('Submit this document to confirm'), true);
  295. }
  296. this.clear_custom_buttons();
  297. this.show_web_link();
  298. }
  299. _f.Frm.prototype.show_web_link = function() {
  300. var doc = this.doc, me = this;
  301. if(!doc.__islocal && doc.__onload && doc.__onload.is_website_generator) {
  302. me.web_link && me.web_link.remove();
  303. if(doc.__onload.published) {
  304. me.add_web_link("/" + doc.route)
  305. }
  306. }
  307. }
  308. _f.Frm.prototype.add_web_link = function(path) {
  309. this.web_link = this.sidebar.add_user_action("See on Website",
  310. function() {}).attr("href", path || this.doc.route).attr("target", "_blank");
  311. }
  312. _f.Frm.prototype.check_doc_perm = function() {
  313. // get perm
  314. var dt = this.parent_doctype?this.parent_doctype : this.doctype;
  315. this.perm = frappe.perm.get_perm(dt, this.doc);
  316. if(!this.perm[0].read) {
  317. return 0;
  318. }
  319. return 1
  320. }
  321. _f.Frm.prototype.refresh = function(docname) {
  322. var is_a_different_doc = docname ? true : false;
  323. if(docname) {
  324. // record switch
  325. if(this.docname != docname && (!this.meta.in_dialog || this.in_form) &&
  326. !this.meta.istable) {
  327. frappe.utils.scroll_to(0);
  328. this.hide_print();
  329. }
  330. frappe.ui.form.close_grid_form();
  331. this.docname = docname;
  332. }
  333. cur_frm = this;
  334. if(this.docname) { // document to show
  335. // set the doc
  336. this.doc = frappe.get_doc(this.doctype, this.docname);
  337. // check permissions
  338. if(!this.check_doc_perm()) {
  339. frappe.show_not_permitted(__(this.doctype) + " " + __(this.docname));
  340. return;
  341. }
  342. // read only (workflow)
  343. this.read_only = frappe.workflow.is_read_only(this.doctype, this.docname);
  344. // check if doctype is already open
  345. if (!this.opendocs[this.docname]) {
  346. this.check_doctype_conflict(this.docname);
  347. } else {
  348. if(this.doc && (!this.doc.__unsaved) && this.doc.__last_sync_on &&
  349. (new Date() - this.doc.__last_sync_on) > (this.refresh_if_stale_for * 1000)) {
  350. this.reload_doc();
  351. return;
  352. }
  353. }
  354. // do setup
  355. if(!this.setup_done) this.setup();
  356. // load the record for the first time, if not loaded (call 'onload')
  357. this.cscript.is_onload = false;
  358. if(!this.opendocs[this.docname]) {
  359. var me = this;
  360. this.cscript.is_onload = true;
  361. this.setnewdoc();
  362. $(document).trigger("form-load", [this]);
  363. $(this.page.wrapper).on('hide', function(e) {
  364. $(document).trigger("form-unload", [me]);
  365. });
  366. } else {
  367. this.render_form(is_a_different_doc);
  368. if (this.doc.localname) {
  369. // trigger form-rename and remove .localname
  370. delete this.doc.localname;
  371. $(document).trigger("form-rename", [this]);
  372. }
  373. }
  374. // if print format is shown, refresh the format
  375. if(this.print_preview.wrapper.is(":visible")) {
  376. this.print_preview.preview();
  377. }
  378. if(is_a_different_doc) {
  379. if(this.show_print_first && this.doc.docstatus===1) {
  380. // show print view
  381. this.print_doc();
  382. } else {
  383. if(this.hide_first && !this.doc.__unsaved && !this.doc.__islocal) {
  384. this.set_hidden(true);
  385. } else {
  386. if(this.hidden) {
  387. this.set_hidden(false);
  388. }
  389. }
  390. }
  391. }
  392. this.show_if_needs_refresh();
  393. }
  394. }
  395. _f.Frm.prototype.show_if_needs_refresh = function() {
  396. if(this.doc.__needs_refresh) {
  397. if(this.doc.__unsaved) {
  398. this.dashboard.set_headline_alert(__("This form has been modified after you have loaded it")
  399. + '<a class="btn btn-xs btn-primary pull-right" onclick="cur_frm.reload_doc()">'
  400. + __("Refresh") + '</a>', "alert-warning");
  401. } else {
  402. this.reload_doc();
  403. }
  404. }
  405. }
  406. _f.Frm.prototype.render_form = function(is_a_different_doc) {
  407. if(!this.meta.istable) {
  408. this.layout.doc = this.doc;
  409. this.layout.attach_doc_and_docfields()
  410. this.sidebar = new frappe.ui.form.Sidebar({
  411. frm: this,
  412. page: this.page
  413. });
  414. this.sidebar.make();
  415. // header must be refreshed before client methods
  416. // because add_custom_button
  417. this.refresh_header(is_a_different_doc);
  418. // clear layout message
  419. this.layout.show_message();
  420. // call trigger
  421. this.script_manager.trigger("refresh");
  422. // trigger global trigger
  423. // to use this
  424. $(document).trigger('form_refresh', [this]);
  425. // fields
  426. this.refresh_fields();
  427. // call onload post render for callbacks to be fired
  428. if(this.cscript.is_onload) {
  429. this.script_manager.trigger("onload_post_render");
  430. }
  431. // focus on first input
  432. if(this.doc.docstatus==0) {
  433. var first = this.form_wrapper.find('.form-layout :input:first');
  434. if(!in_list(["Date", "Datetime"], first.attr("data-fieldtype"))) {
  435. first.focus();
  436. }
  437. }
  438. } else {
  439. this.refresh_header(is_a_different_doc);
  440. }
  441. $(this.wrapper).trigger('render_complete');
  442. if(!this.hidden) {
  443. this.layout.show_empty_form_message();
  444. }
  445. this.scroll_to_element();
  446. }
  447. _f.Frm.prototype.refresh_field = function(fname) {
  448. if(this.fields_dict[fname] && this.fields_dict[fname].refresh) {
  449. this.fields_dict[fname].refresh();
  450. this.layout.refresh_dependency();
  451. }
  452. }
  453. _f.Frm.prototype.refresh_fields = function() {
  454. this.layout.refresh(this.doc);
  455. this.layout.primary_button = $(this.wrapper).find(".btn-primary");
  456. // cleanup activities after refresh
  457. this.cleanup_refresh(this);
  458. }
  459. _f.Frm.prototype.cleanup_refresh = function() {
  460. var me = this;
  461. if(me.fields_dict['amended_from']) {
  462. if (me.doc.amended_from) {
  463. unhide_field('amended_from');
  464. if (me.fields_dict['amendment_date']) unhide_field('amendment_date');
  465. } else {
  466. hide_field('amended_from');
  467. if (me.fields_dict['amendment_date']) hide_field('amendment_date');
  468. }
  469. }
  470. if(me.fields_dict['trash_reason']) {
  471. if(me.doc.trash_reason && me.doc.docstatus == 2) {
  472. unhide_field('trash_reason');
  473. } else {
  474. hide_field('trash_reason');
  475. }
  476. }
  477. if(me.meta.autoname && me.meta.autoname.substr(0,6)=='field:' && !me.doc.__islocal) {
  478. var fn = me.meta.autoname.substr(6);
  479. if (me.doc[fn]) {
  480. me.toggle_display(fn, false);
  481. }
  482. }
  483. if(me.meta.autoname=="naming_series:" && !me.doc.__islocal) {
  484. me.toggle_display("naming_series", false);
  485. }
  486. }
  487. _f.Frm.prototype.setnewdoc = function() {
  488. // moved this call to refresh function
  489. // this.check_doctype_conflict(docname);
  490. var me = this;
  491. // hide any open grid
  492. this.script_manager.trigger("before_load", this.doctype, this.docname, function() {
  493. me.script_manager.trigger("onload");
  494. me.opendocs[me.docname] = true;
  495. me.render_form();
  496. frappe.after_ajax(function() {
  497. me.trigger_link_fields();
  498. });
  499. frappe.breadcrumbs.add(me.meta.module, me.doctype)
  500. });
  501. // update seen
  502. if(this.meta.track_seen) {
  503. $('.list-id[data-name="'+ me.docname +'"]').addClass('seen');
  504. }
  505. }
  506. _f.Frm.prototype.trigger_link_fields = function() {
  507. // trigger link fields which have default values set
  508. if (this.is_new() && this.doc.__run_link_triggers) {
  509. $.each(this.fields_dict, function(fieldname, field) {
  510. if (field.df.fieldtype=="Link" && this.doc[fieldname]) {
  511. // triggers add fetch, sets value in model and runs triggers
  512. field.set_value(this.doc[fieldname]);
  513. }
  514. });
  515. delete this.doc.__run_link_triggers;
  516. }
  517. }
  518. _f.Frm.prototype.runscript = function(scriptname, callingfield, onrefresh) {
  519. var me = this;
  520. if(this.docname) {
  521. // send to run
  522. if(callingfield)
  523. $(callingfield.input).set_working();
  524. frappe.call({
  525. method: "runserverobj",
  526. args: {'docs':this.doc, 'method':scriptname },
  527. btn: callingfield.$input,
  528. callback: function(r) {
  529. if(!r.exc) {
  530. if(onrefresh) {
  531. onrefresh(r);
  532. }
  533. me.refresh_fields();
  534. }
  535. }
  536. });
  537. }
  538. }
  539. _f.Frm.prototype.copy_doc = function(onload, from_amend) {
  540. this.validate_form_action("Create");
  541. var newdoc = frappe.model.copy_doc(this.doc, from_amend);
  542. newdoc.idx = null;
  543. newdoc.__run_link_triggers = false;
  544. if(onload) {
  545. onload(newdoc);
  546. }
  547. frappe.set_route('Form', newdoc.doctype, newdoc.name);
  548. }
  549. _f.Frm.prototype.reload_doc = function() {
  550. this.check_doctype_conflict(this.docname);
  551. var me = this;
  552. var onsave = function(r, rtxt) {
  553. me.refresh();
  554. }
  555. if(!me.doc.__islocal) {
  556. frappe.model.remove_from_locals(me.doctype, me.docname);
  557. frappe.model.with_doc(me.doctype, me.docname, function() {
  558. me.refresh();
  559. })
  560. }
  561. }
  562. var validated;
  563. _f.Frm.prototype.save = function(save_action, callback, btn, on_error) {
  564. btn && $(btn).prop("disabled", true);
  565. $(document.activeElement).blur();
  566. frappe.ui.form.close_grid_form();
  567. // let any pending js process finish
  568. var me = this;
  569. setTimeout(function() { me._save(save_action, callback, btn, on_error) }, 100);
  570. }
  571. _f.Frm.prototype._save = function(save_action, callback, btn, on_error) {
  572. var me = this;
  573. if(!save_action) save_action = "Save";
  574. this.validate_form_action(save_action);
  575. if((!this.meta.in_dialog || this.in_form) && !this.meta.istable) {
  576. frappe.utils.scroll_to(0);
  577. }
  578. var after_save = function(r) {
  579. if(!r.exc) {
  580. if (["Save", "Update", "Amend"].indexOf(save_action)!==-1) {
  581. frappe.utils.play_sound("click");
  582. }
  583. me.script_manager.trigger("after_save");
  584. me.refresh();
  585. } else {
  586. if(on_error)
  587. on_error();
  588. }
  589. callback && callback(r);
  590. }
  591. if(save_action != "Update") {
  592. // validate
  593. validated = true;
  594. $.when(this.script_manager.trigger("validate"), this.script_manager.trigger("before_save"))
  595. .done(function() {
  596. // done is called after all ajaxes in validate & before_save are completed :)
  597. if(!validated) {
  598. btn && $(btn).prop("disabled", false);
  599. if(on_error) {
  600. on_error();
  601. }
  602. return;
  603. }
  604. frappe.ui.form.save(me, save_action, after_save, btn);
  605. });
  606. } else {
  607. frappe.ui.form.save(me, save_action, after_save, btn);
  608. }
  609. }
  610. _f.Frm.prototype.savesubmit = function(btn, callback, on_error) {
  611. var me = this;
  612. this.validate_form_action("Submit");
  613. frappe.confirm(__("Permanently Submit {0}?", [this.docname]), function() {
  614. validated = true;
  615. me.script_manager.trigger("before_submit").done(function() {
  616. if(!validated) {
  617. if(on_error)
  618. on_error();
  619. return;
  620. }
  621. me.save('Submit', function(r) {
  622. if(!r.exc) {
  623. frappe.utils.play_sound("submit");
  624. callback && callback();
  625. me.script_manager.trigger("on_submit");
  626. }
  627. }, btn, on_error);
  628. });
  629. }, on_error);
  630. };
  631. _f.Frm.prototype.savecancel = function(btn, callback, on_error) {
  632. var me = this;
  633. this.validate_form_action('Cancel');
  634. frappe.confirm(__("Permanently Cancel {0}?", [this.docname]), function() {
  635. validated = true;
  636. me.script_manager.trigger("before_cancel").done(function() {
  637. if(!validated) {
  638. if(on_error)
  639. on_error();
  640. return;
  641. }
  642. var after_cancel = function(r) {
  643. if(!r.exc) {
  644. frappe.utils.play_sound("cancel");
  645. me.refresh();
  646. callback && callback();
  647. me.script_manager.trigger("after_cancel");
  648. } else {
  649. on_error();
  650. }
  651. }
  652. frappe.ui.form.save(me, "cancel", after_cancel, btn);
  653. });
  654. }, on_error);
  655. }
  656. // delete the record
  657. _f.Frm.prototype.savetrash = function() {
  658. this.validate_form_action("Delete");
  659. frappe.model.delete_doc(this.doctype, this.docname, function(r) {
  660. window.history.back();
  661. })
  662. }
  663. _f.Frm.prototype.amend_doc = function() {
  664. if(!this.fields_dict['amended_from']) {
  665. alert('"amended_from" field must be present to do an amendment.');
  666. return;
  667. }
  668. this.validate_form_action("Amend");
  669. var me = this;
  670. var fn = function(newdoc) {
  671. newdoc.amended_from = me.docname;
  672. if(me.fields_dict && me.fields_dict['amendment_date'])
  673. newdoc.amendment_date = dateutil.obj_to_str(new Date());
  674. }
  675. this.copy_doc(fn, 1);
  676. frappe.utils.play_sound("click");
  677. }
  678. _f.Frm.prototype.disable_save = function() {
  679. // IMPORTANT: this function should be called in refresh event
  680. this.save_disabled = true;
  681. this.toolbar.current_status = null;
  682. this.page.clear_primary_action();
  683. }
  684. _f.Frm.prototype.enable_save = function() {
  685. this.save_disabled = false;
  686. this.toolbar.set_primary_action();
  687. }
  688. _f.Frm.prototype.save_or_update = function() {
  689. if(this.save_disabled) return;
  690. if(this.doc.docstatus===0) {
  691. this.save();
  692. } else if(this.doc.docstatus===1 && this.doc.__unsaved) {
  693. this.save("Update");
  694. }
  695. }
  696. _f.Frm.prototype.dirty = function() {
  697. this.doc.__unsaved = 1;
  698. $(this.wrapper).trigger('dirty');
  699. }
  700. _f.Frm.prototype.get_docinfo = function() {
  701. return frappe.model.docinfo[this.doctype][this.docname];
  702. }
  703. _f.Frm.prototype.is_dirty = function() {
  704. return this.doc.__unsaved;
  705. }
  706. _f.Frm.prototype.is_new = function() {
  707. return this.doc.__islocal;
  708. }
  709. _f.Frm.prototype.reload_docinfo = function(callback) {
  710. var me = this;
  711. frappe.call({
  712. method: "frappe.desk.form.load.get_docinfo",
  713. args: {
  714. doctype: me.doctype,
  715. name: me.doc.name
  716. },
  717. callback: function(r) {
  718. // docinfo will be synced
  719. if(callback) callback(r.docinfo);
  720. me.timeline.refresh();
  721. me.assign_to.refresh();
  722. me.attachments.refresh();
  723. }
  724. })
  725. }
  726. _f.Frm.prototype.get_perm = function(permlevel, access_type) {
  727. return this.perm[permlevel] ? this.perm[permlevel][access_type] : null;
  728. }
  729. _f.Frm.prototype.set_intro = function(txt, append) {
  730. this.dashboard.set_headline_alert(txt);
  731. //frappe.utils.set_intro(this, this.body, txt, append);
  732. }
  733. _f.Frm.prototype.set_footnote = function(txt) {
  734. frappe.utils.set_footnote(this, this.body, txt);
  735. }
  736. _f.Frm.prototype.add_custom_button = function(label, fn, group) {
  737. // temp! old parameter used to be icon
  738. if(group && group.indexOf("icon")!==-1) group = null;
  739. return this.page.add_inner_button(label, fn, group);
  740. }
  741. _f.Frm.prototype.clear_custom_buttons = function() {
  742. this.page.clear_inner_toolbar();
  743. this.page.clear_user_actions();
  744. }
  745. _f.Frm.prototype.add_fetch = function(link_field, src_field, tar_field) {
  746. if(!this.fetch_dict[link_field]) {
  747. this.fetch_dict[link_field] = {'columns':[], 'fields':[]}
  748. }
  749. this.fetch_dict[link_field].columns.push(src_field);
  750. this.fetch_dict[link_field].fields.push(tar_field);
  751. }
  752. _f.Frm.prototype.set_print_heading = function(txt) {
  753. this.pformat[this.docname] = txt;
  754. }
  755. _f.Frm.prototype.action_perm_type_map = {
  756. "Create": "create",
  757. "Save": "write",
  758. "Submit": "submit",
  759. "Update": "submit",
  760. "Cancel": "cancel",
  761. "Amend": "amend",
  762. "Delete": "delete"
  763. };
  764. _f.Frm.prototype.validate_form_action = function(action) {
  765. var perm_to_check = this.action_perm_type_map[action];
  766. if (!this.perm[0][perm_to_check]) {
  767. frappe.throw (__("No permission to '{0}' {1}", [__(action), __(this.doc.doctype)]));
  768. }
  769. };
  770. _f.Frm.prototype.get_handlers = function(fieldname, doctype, docname) {
  771. return this.script_manager.get_handlers(fieldname, doctype || this.doctype, docname || this.docname)
  772. }
  773. _f.Frm.prototype.has_perm = function(ptype) {
  774. return frappe.perm.has_perm(this.doctype, 0, ptype, this.doc);
  775. }
  776. _f.Frm.prototype.scroll_to_element = function() {
  777. if (frappe.route_options && frappe.route_options.scroll_to) {
  778. var scroll_to = frappe.route_options.scroll_to;
  779. delete frappe.route_options.scroll_to;
  780. var selector = [];
  781. for (var key in scroll_to) {
  782. var value = scroll_to[key];
  783. selector.push(repl('[data-%(key)s="%(value)s"]', {key: key, value: value}));
  784. }
  785. selector = $(selector.join(" "));
  786. if (selector.length) {
  787. frappe.utils.scroll_to(selector);
  788. }
  789. } else {
  790. frappe.utils.scroll_to(0);
  791. }
  792. }