Não pode escolher mais do que 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.
 
 
 
 
 
 

930 linhas
23 KiB

  1. // Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. //
  3. // MIT License (MIT)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a
  6. // copy of this software and associated documentation files (the "Software"),
  7. // to deal in the Software without restriction, including without limitation
  8. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. // and/or sell copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22. /* Form page structure
  23. + this.parent (either FormContainer or Dialog)
  24. + this.wrapper
  25. + this.toolbar
  26. + this.form_wrapper
  27. + this.main
  28. + this.head
  29. + this.body
  30. + this.layout
  31. + this.sidebar
  32. + this.print_wrapper
  33. + this.head
  34. + this.footer
  35. */
  36. wn.provide('_f');
  37. wn.provide('wn.ui.form');
  38. wn.ui.form.Controller = Class.extend({
  39. init: function(opts) {
  40. $.extend(this, opts);
  41. this.setup && this.setup();
  42. }
  43. });
  44. _f.frms = {};
  45. _f.Frm = function(doctype, parent, in_form) {
  46. this.docname = '';
  47. this.doctype = doctype;
  48. this.display = 0;
  49. this.refresh_if_stale_for = 120;
  50. var me = this;
  51. this.last_view_is_edit = {};
  52. this.opendocs = {};
  53. this.sections = [];
  54. this.grids = [];
  55. this.cscript = new wn.ui.form.Controller({frm:this});
  56. this.pformat = {};
  57. this.fetch_dict = {};
  58. this.parent = parent;
  59. this.tinymce_id_list = [];
  60. this.setup_meta(doctype);
  61. // show in form instead of in dialog, when called using url (router.js)
  62. this.in_form = in_form ? true : false;
  63. // notify on rename
  64. var me = this;
  65. $(document).bind('rename', function(event, dt, old_name, new_name) {
  66. if(dt==me.doctype)
  67. me.rename_notify(dt, old_name, new_name)
  68. });
  69. }
  70. _f.Frm.prototype.check_doctype_conflict = function(docname) {
  71. var me = this;
  72. if(this.doctype=='DocType' && docname=='DocType') {
  73. msgprint('Allowing DocType, DocType. Be careful!')
  74. } else if(this.doctype=='DocType') {
  75. if (wn.views.formview[docname] || wn.pages['List/'+docname]) {
  76. msgprint("Cannot open DocType when its instance is open")
  77. throw 'doctype open conflict'
  78. }
  79. } else {
  80. if (wn.views.formview.DocType && wn.views.formview.DocType.frm.opendocs[this.doctype]) {
  81. msgprint("Cannot open instance when its DocType is open")
  82. throw 'doctype open conflict'
  83. }
  84. }
  85. }
  86. _f.Frm.prototype.setup = function() {
  87. var me = this;
  88. this.fields = [];
  89. this.fields_dict = {};
  90. this.state_fieldname = wn.workflow.get_state_fieldname(this.doctype);
  91. // wrapper
  92. this.wrapper = this.parent;
  93. wn.ui.make_app_page({
  94. parent: this.wrapper,
  95. single_column: true
  96. });
  97. this.appframe = this.wrapper.appframe;
  98. this.layout_main = $(this.wrapper).find(".layout-main").get(0);
  99. this.toolbar = new wn.ui.form.Toolbar({
  100. frm: this,
  101. appframe: this.appframe
  102. })
  103. // create area for print fomrat
  104. this.setup_print_layout();
  105. // 2 column layout
  106. this.setup_std_layout();
  107. // client script must be called after "setup" - there are no fields_dict attached to the frm otherwise
  108. this.setup_client_script();
  109. this.setup_header();
  110. this.footer = new wn.ui.form.Footer({
  111. frm: this,
  112. parent: this.layout_main
  113. })
  114. this.setup_done = true;
  115. }
  116. _f.Frm.prototype.setup_print_layout = function() {
  117. var me = this;
  118. this.print_wrapper = $('<div>\
  119. <form class="form-inline" style="margin-bottom: 10px;">\
  120. <select class="col-span-2 preview-select"></select> \
  121. <button class="btn btn-edit btn-info"><i class="icon-edit"></i> Edit</button>\
  122. </form>\
  123. <div class="print-format-area clear-fix" style="min-height: 400px;"></div>\
  124. </div>').appendTo(this.layout_main).get(0);
  125. $(this.print_wrapper).find(".btn-edit").click(function() {
  126. me.edit_doc();
  127. return false;
  128. })
  129. this.$print_view_select = $(this.print_wrapper).find(".preview-select")
  130. .add_options(this.print_formats)
  131. .val(this.print_formats[0])
  132. .change(function() {
  133. me.refresh_print_layout();
  134. })
  135. //appframe.add_ripped_paper_effect(this.print_wrapper);
  136. this.print_body = $(this.print_wrapper).find(".print-format-area").get(0);
  137. }
  138. _f.Frm.prototype.onhide = function() {
  139. if(_f.cur_grid_cell) _f.cur_grid_cell.grid.cell_deselect();
  140. }
  141. _f.Frm.prototype.setup_std_layout = function() {
  142. this.form_wrapper = $('<div></div>').appendTo(this.layout_main).get(0);
  143. $parent = $(this.form_wrapper);
  144. this.head = $parent.find(".layout-appframe").get(0);
  145. this.main = this.form_wrapper;
  146. this.body_header = $a(this.main, 'div');
  147. this.body = $a(this.main, 'div');
  148. if(this.heading) {
  149. this.page_head = new PageHeader(this.head, this);
  150. }
  151. // only tray
  152. this.meta.section_style='Simple'; // always simple!
  153. // layout
  154. this.layout = new wn.ui.form.Layout({
  155. parent: this.body,
  156. doctype: this.doctype,
  157. frm: this,
  158. })
  159. // state
  160. this.states = new wn.ui.form.States({
  161. frm: this
  162. });
  163. }
  164. _f.Frm.prototype.setup_header = function() {
  165. // header - no headers for tables and guests
  166. if(!(this.meta.istable || (this.meta.in_dialog && !this.in_form)))
  167. this.frm_head = new _f.FrmHeader(this.head, this);
  168. this.toolbar = new wn.ui.form.Toolbar({
  169. frm: this,
  170. appframe: this.appframe
  171. })
  172. }
  173. _f.Frm.prototype.setup_print = function() {
  174. this.print_formats = wn.meta.get_print_formats(this.meta.name);
  175. this.print_sel = $a(null, 'select', '', {width:'160px'});
  176. add_sel_options(this.print_sel, this.print_formats);
  177. this.print_sel.value = this.print_formats[0];
  178. }
  179. _f.Frm.prototype.print_doc = function() {
  180. if(this.doc.docstatus==2) {
  181. msgprint("Cannot Print Cancelled Documents.");
  182. return;
  183. }
  184. _p.show_dialog(); // multiple options
  185. }
  186. // email the form
  187. _f.Frm.prototype.email_doc = function(message) {
  188. new wn.views.CommunicationComposer({
  189. doc: this.doc,
  190. subject: wn._(this.meta.name) + ': ' + this.docname,
  191. recipients: this.doc.email || this.doc.email_id || this.doc.contact_email,
  192. attach_document_print: true,
  193. message: message,
  194. real_name: this.doc.real_name || this.doc.contact_display || this.doc.contact_name
  195. });
  196. }
  197. // email the form
  198. _f.Frm.prototype.rename_doc = function() {
  199. wn.model.rename_doc(this.doctype, this.docname);
  200. }
  201. // notify this form of renamed records
  202. _f.Frm.prototype.rename_notify = function(dt, old, name) {
  203. // from form
  204. if(this.meta.istable)
  205. return;
  206. if(this.docname == old)
  207. this.docname = name;
  208. else
  209. return;
  210. // view_is_edit
  211. this.last_view_is_edit[name] = this.last_view_is_edit[old];
  212. delete this.last_view_is_edit[old];
  213. // cleanup
  214. if(this && this.opendocs[old]) {
  215. // delete docfield copy
  216. wn.meta.docfield_copy[dt][name] = wn.meta.docfield_copy[dt][old];
  217. delete wn.meta.docfield_copy[dt][old];
  218. }
  219. delete this.opendocs[old];
  220. this.opendocs[name] = true;
  221. if(this.meta.in_dialog || !this.in_form) {
  222. return;
  223. }
  224. wn.re_route[window.location.hash] = '#Form/' + encodeURIComponent(this.doctype) + '/' + encodeURIComponent(name);
  225. wn.set_route('Form', this.doctype, name);
  226. }
  227. // SETUP
  228. _f.Frm.prototype.setup_meta = function(doctype) {
  229. this.meta = wn.model.get_doc('DocType',this.doctype);
  230. this.perm = wn.perm.get_perm(this.doctype); // for create
  231. if(this.meta.istable) { this.meta.in_dialog = 1 }
  232. this.setup_print();
  233. }
  234. _f.Frm.prototype.set_intro = function(txt) {
  235. wn.utils.set_intro(this, this.body, txt);
  236. }
  237. _f.Frm.prototype.set_footnote = function(txt) {
  238. wn.utils.set_footnote(this, this.body, txt);
  239. }
  240. _f.Frm.prototype.add_custom_button = function(label, fn, icon) {
  241. this.appframe.add_button(label, fn, icon, true);
  242. }
  243. _f.Frm.prototype.clear_custom_buttons = function() {
  244. this.toolbar.refresh()
  245. }
  246. _f.Frm.prototype.add_fetch = function(link_field, src_field, tar_field) {
  247. if(!this.fetch_dict[link_field]) {
  248. this.fetch_dict[link_field] = {'columns':[], 'fields':[]}
  249. }
  250. this.fetch_dict[link_field].columns.push(src_field);
  251. this.fetch_dict[link_field].fields.push(tar_field);
  252. }
  253. _f.Frm.prototype.setup_client_script = function() {
  254. // setup client obj
  255. if(this.meta.client_script_core || this.meta.client_script || this.meta.__js) {
  256. this.runclientscript('setup', this.doctype, this.docname);
  257. }
  258. }
  259. _f.Frm.prototype.refresh_print_layout = function() {
  260. $ds(this.print_wrapper);
  261. $dh(this.form_wrapper);
  262. var me = this;
  263. var print_callback = function(print_html) {
  264. me.print_body.innerHTML = print_html;
  265. }
  266. // print head
  267. if(cur_frm.doc.select_print_heading)
  268. cur_frm.set_print_heading(cur_frm.doc.select_print_heading)
  269. if(user!='Guest') {
  270. $di(this.view_btn_wrapper);
  271. // archive
  272. if(cur_frm.doc.__archived) {
  273. $dh(this.view_btn_wrapper);
  274. }
  275. } else {
  276. $dh(this.view_btn_wrapper);
  277. $dh(this.print_close_btn);
  278. }
  279. // create print format here
  280. _p.build(this.$print_view_select.val(), print_callback, null, 1);
  281. }
  282. _f.Frm.prototype.show_the_frm = function() {
  283. // show the dialog
  284. if(this.meta.in_dialog && !this.parent.dialog.display) {
  285. if(!this.meta.istable)
  286. this.parent.table_form = false;
  287. this.parent.dialog.show();
  288. }
  289. }
  290. _f.Frm.prototype.set_print_heading = function(txt) {
  291. this.pformat[cur_frm.docname] = txt;
  292. }
  293. _f.Frm.prototype.defocus_rest = function() {
  294. // deselect others
  295. if(_f.cur_grid_cell) _f.cur_grid_cell.grid.cell_deselect();
  296. }
  297. _f.Frm.prototype.refresh_header = function() {
  298. // set title
  299. // main title
  300. if(!this.meta.in_dialog || this.in_form) {
  301. set_title(this.meta.issingle ? this.doctype : this.docname);
  302. }
  303. if(wn.ui.toolbar.recent)
  304. wn.ui.toolbar.recent.add(this.doctype, this.docname, 1);
  305. // show / hide buttons
  306. if(this.frm_head) {
  307. this.frm_head.refresh();
  308. this.toolbar.refresh();
  309. }
  310. }
  311. _f.Frm.prototype.check_doc_perm = function() {
  312. // get perm
  313. var dt = this.parent_doctype?this.parent_doctype : this.doctype;
  314. var dn = this.parent_docname?this.parent_docname : this.docname;
  315. this.perm = wn.perm.get_perm(dt, dn);
  316. if(!this.perm[0][READ]) {
  317. wn.set_route("403");
  318. return 0;
  319. }
  320. return 1
  321. }
  322. _f.Frm.prototype.refresh = function(docname) {
  323. // record switch
  324. if(docname) {
  325. if(this.docname != docname && (!this.meta.in_dialog || this.in_form) &&
  326. !this.meta.istable)
  327. scroll(0, 0);
  328. this.docname = docname;
  329. }
  330. if(!this.meta.istable) {
  331. cur_frm = this;
  332. this.parent.cur_frm = this;
  333. }
  334. if(this.docname) { // document to show
  335. // check permissions
  336. if(!this.check_doc_perm()) return;
  337. // read only (workflow)
  338. this.read_only = wn.workflow.is_read_only(this.doctype, this.docname);
  339. // set the doc
  340. this.doc = wn.model.get_doc(this.doctype, this.docname);
  341. // check if doctype is already open
  342. if (!this.opendocs[this.docname]) {
  343. this.check_doctype_conflict(this.docname);
  344. } else {
  345. if(this.doc && this.doc.__last_sync_on &&
  346. (new Date() - this.doc.__last_sync_on) > (this.refresh_if_stale_for * 1000)) {
  347. this.reload_doc();
  348. return;
  349. }
  350. }
  351. // do setup
  352. if(!this.setup_done) this.setup();
  353. // set customized permissions for this record
  354. this.runclientscript('set_perm', this.doctype, this.docname);
  355. // load the record for the first time, if not loaded (call 'onload')
  356. cur_frm.cscript.is_onload = false;
  357. if(!this.opendocs[this.docname]) {
  358. cur_frm.cscript.is_onload = true;
  359. this.setnewdoc(this.docname);
  360. }
  361. // view_is_edit
  362. if(this.doc.__islocal)
  363. this.last_view_is_edit[this.docname] = 1; // new is view_is_edit
  364. this.view_is_edit = this.last_view_is_edit[this.docname];
  365. if(this.view_is_edit || (!this.view_is_edit && this.meta.istable)) {
  366. if(this.print_wrapper) {
  367. $dh(this.print_wrapper);
  368. $ds(this.form_wrapper);
  369. }
  370. // header
  371. if(!this.meta.istable) {
  372. this.refresh_header();
  373. }
  374. // call trigger
  375. this.runclientscript('refresh');
  376. // trigger global trigger
  377. // to use this
  378. $(document).trigger('form_refresh');
  379. // fields
  380. this.refresh_fields();
  381. // dependent fields
  382. this.refresh_dependency();
  383. // call onload post render for callbacks to be fired
  384. if(this.cscript.is_onload) {
  385. this.runclientscript('onload_post_render', this.doctype, this.docname);
  386. }
  387. // focus on first input
  388. if(this.doc.docstatus==0) {
  389. var first = $(this.form_wrapper).find('.form-layout-row :input:first');
  390. if(!in_list(["Date", "Datetime"], first.attr("data-fieldtype"))) {
  391. first.focus();
  392. }
  393. }
  394. } else {
  395. this.refresh_header();
  396. if(this.print_wrapper) {
  397. this.refresh_print_layout();
  398. }
  399. this.runclientscript('edit_status_changed');
  400. }
  401. $(cur_frm.wrapper).trigger('render_complete');
  402. }
  403. }
  404. _f.Frm.prototype.refresh_field = function(fname) {
  405. cur_frm.fields_dict[fname] && cur_frm.fields_dict[fname].refresh
  406. && cur_frm.fields_dict[fname].refresh();
  407. }
  408. _f.Frm.prototype.refresh_fields = function() {
  409. this.layout.refresh();
  410. // cleanup activities after refresh
  411. this.cleanup_refresh(this);
  412. }
  413. _f.Frm.prototype.cleanup_refresh = function() {
  414. var me = this;
  415. if(me.fields_dict['amended_from']) {
  416. if (me.doc.amended_from) {
  417. unhide_field('amended_from');
  418. if (me.fields_dict['amendment_date']) unhide_field('amendment_date');
  419. } else {
  420. hide_field('amended_from');
  421. if (me.fields_dict['amendment_date']) hide_field('amendment_date');
  422. }
  423. }
  424. if(me.fields_dict['trash_reason']) {
  425. if(me.doc.trash_reason && me.doc.docstatus == 2) {
  426. unhide_field('trash_reason');
  427. } else {
  428. hide_field('trash_reason');
  429. }
  430. }
  431. if(me.meta.autoname && me.meta.autoname.substr(0,6)=='field:' && !me.doc.__islocal) {
  432. var fn = me.meta.autoname.substr(6);
  433. cur_frm.toggle_display(fn, false);
  434. }
  435. if(me.meta.autoname=="naming_series:" && !me.doc.__islocal) {
  436. cur_frm.toggle_display("naming_series", false);
  437. }
  438. }
  439. // Resolve "depends_on" and show / hide accordingly
  440. _f.Frm.prototype.refresh_dependency = function() {
  441. var me = this;
  442. var doc = locals[this.doctype][this.docname];
  443. // build dependants' dictionary
  444. var has_dep = false;
  445. for(fkey in me.fields) {
  446. var f = me.fields[fkey];
  447. f.dependencies_clear = true;
  448. if(f.df.depends_on) {
  449. has_dep = true;
  450. }
  451. }
  452. if(!has_dep)return;
  453. // show / hide based on values
  454. for(var i=me.fields.length-1;i>=0;i--) {
  455. var f = me.fields[i];
  456. f.guardian_has_value = true;
  457. if(f.df.depends_on) {
  458. // evaluate guardian
  459. var v = doc[f.df.depends_on];
  460. if(f.df.depends_on.substr(0,5)=='eval:') {
  461. f.guardian_has_value = eval(f.df.depends_on.substr(5));
  462. } else if(f.df.depends_on.substr(0,3)=='fn:') {
  463. f.guardian_has_value = me.runclientscript(f.df.depends_on.substr(3), me.doctype, me.docname);
  464. } else {
  465. if(!v) {
  466. f.guardian_has_value = false;
  467. }
  468. }
  469. // show / hide
  470. if(f.guardian_has_value) {
  471. f.df.hidden = 0;
  472. f.refresh();
  473. } else {
  474. f.df.hidden = 1;
  475. f.refresh();
  476. }
  477. }
  478. }
  479. }
  480. // setnewdoc is called when a record is loaded for the first time
  481. // ======================================================================================
  482. _f.Frm.prototype.setnewdoc = function(docname) {
  483. // moved this call to refresh function
  484. // this.check_doctype_conflict(docname);
  485. // if loaded
  486. if(this.opendocs[docname]) { // already exists
  487. this.docname=docname;
  488. return;
  489. }
  490. // make a copy of the doctype for client script settings
  491. // each record will have its own client script
  492. wn.meta.make_docfield_copy_for(this.doctype,docname);
  493. this.docname = docname;
  494. var me = this;
  495. var viewname = this.meta.issingle ? this.doctype : docname;
  496. // Client Script
  497. this.runclientscript('onload', this.doctype, this.docname);
  498. this.last_view_is_edit[docname] = 1;
  499. if(cint(this.meta.read_only_onload)) this.last_view_is_edit[docname] = 0;
  500. this.opendocs[docname] = true;
  501. }
  502. _f.Frm.prototype.edit_doc = function() {
  503. // set fields
  504. this.last_view_is_edit[this.docname] = true;
  505. this.refresh();
  506. }
  507. _f.Frm.prototype.show_doc = function(dn) {
  508. this.refresh(dn);
  509. }
  510. _f.Frm.prototype.runscript = function(scriptname, callingfield, onrefresh) {
  511. var me = this;
  512. if(this.docname) {
  513. // make doc list
  514. var doclist = wn.model.compress(make_doclist(this.doctype, this.docname));
  515. // send to run
  516. if(callingfield)
  517. $(callingfield.input).set_working();
  518. $c('runserverobj', {'docs':doclist, 'method':scriptname },
  519. function(r, rtxt) {
  520. // run refresh
  521. if(onrefresh)
  522. onrefresh(r,rtxt);
  523. // fields
  524. me.refresh_fields();
  525. // dependent fields
  526. me.refresh_dependency();
  527. // enable button
  528. if(callingfield)
  529. $(callingfield.input).done_working();
  530. }
  531. );
  532. }
  533. }
  534. _f.Frm.prototype.runclientscript = function(caller, cdt, cdn) {
  535. if(!cdt)cdt = this.doctype;
  536. if(!cdn)cdn = this.docname;
  537. var ret = null;
  538. var doc = locals[cur_frm.doc.doctype][cur_frm.doc.name];
  539. try {
  540. if(this.cscript[caller])
  541. ret = this.cscript[caller](doc, cdt, cdn);
  542. if(this.cscript['custom_'+caller])
  543. ret += this.cscript['custom_'+caller](doc, cdt, cdn);
  544. } catch(e) {
  545. validated = false;
  546. // show error message
  547. this.log_error(caller, e);
  548. }
  549. if(caller && caller.toLowerCase()=='setup') {
  550. this.setup_client_js();
  551. }
  552. return ret;
  553. }
  554. _f.Frm.prototype.setup_client_js = function(caller, cdt, cdn) {
  555. var doctype = wn.model.get_doc('DocType', this.doctype);
  556. // js
  557. var cs = doctype.__js || (doctype.client_script_core + doctype.client_script);
  558. if(cs) {
  559. try {
  560. var tmp = eval(cs);
  561. } catch(e) {
  562. show_alert("Error in Client Script.");
  563. this.log_error(caller || "setup_client_js", e);
  564. }
  565. }
  566. // css
  567. if(doctype.__css) wn.dom.set_style(doctype.__css);
  568. // ---Client String----
  569. if(doctype.client_string) { // split client string
  570. this.cstring = {};
  571. var elist = doctype.client_string.split('---');
  572. for(var i=1;i<elist.length;i=i+2) {
  573. this.cstring[strip(elist[i])] = elist[i+1];
  574. }
  575. }
  576. }
  577. _f.Frm.prototype.log_error = function(caller, e) {
  578. console.group && console.group();
  579. console.log("----- error in client script -----");
  580. console.log("method: " + caller);
  581. console.log(e);
  582. console.log("error message: " + e.message);
  583. console.trace && console.trace();
  584. console.log("----- end of error message -----");
  585. console.group && console.groupEnd();
  586. }
  587. _f.Frm.prototype.copy_doc = function(onload, from_amend) {
  588. if(!this.perm[0][CREATE]) {
  589. msgprint('You are not allowed to create '+this.meta.name);
  590. return;
  591. }
  592. var dn = this.docname;
  593. // copy parent
  594. var newdoc = wn.model.copy_doc(this.doctype, dn, from_amend);
  595. // do not copy attachments
  596. if(this.meta.allow_attach && newdoc.file_list && !from_amend)
  597. newdoc.file_list = null;
  598. // copy chidren
  599. var dl = make_doclist(this.doctype, dn);
  600. // table fields dict - for no_copy check
  601. var tf_dict = {};
  602. for(var d in dl) {
  603. d1 = dl[d];
  604. // get tabel field
  605. if(d1.parentfield && !tf_dict[d1.parentfield]) {
  606. tf_dict[d1.parentfield] = wn.meta.get_docfield(d1.parenttype, d1.parentfield);
  607. }
  608. if(d1.parent==dn && cint(tf_dict[d1.parentfield].no_copy)!=1) {
  609. var ch = wn.model.copy_doc(d1.doctype, d1.name, from_amend);
  610. ch.parent = newdoc.name;
  611. ch.docstatus = 0;
  612. ch.owner = user;
  613. ch.creation = '';
  614. ch.modified_by = user;
  615. ch.modified = '';
  616. }
  617. }
  618. newdoc.__islocal = 1;
  619. newdoc.docstatus = 0;
  620. newdoc.owner = user;
  621. newdoc.creation = '';
  622. newdoc.modified_by = user;
  623. newdoc.modified = '';
  624. if(onload)onload(newdoc);
  625. loaddoc(newdoc.doctype, newdoc.name);
  626. }
  627. _f.Frm.prototype.reload_doc = function() {
  628. this.check_doctype_conflict(this.docname);
  629. var me = this;
  630. var onsave = function(r, rtxt) {
  631. // n tweets and last comment
  632. //me.runclientscript('setup', me.doctype, me.docname);
  633. me.refresh();
  634. }
  635. if(!me.doc.__islocal) {
  636. wn.model.remove_from_locals(me.doctype, me.docname);
  637. wn.model.with_doc(me.doctype, me.docname, function() {
  638. me.refresh();
  639. })
  640. }
  641. }
  642. var validated;
  643. _f.Frm.prototype.save = function(save_action, callback, btn, on_error) {
  644. $(document.activeElement).blur();
  645. var me = this;
  646. if((!this.meta.in_dialog || this.in_form) && !this.meta.istable)
  647. scroll(0, 0);
  648. // validate
  649. if(save_action!="Cancel") {
  650. validated = true;
  651. this.runclientscript('validate');
  652. if(!validated) {
  653. if(on_error)
  654. on_error();
  655. return;
  656. }
  657. }
  658. var doclist = new wn.model.DocList(this.doctype, this.docname);
  659. doclist.save(save_action || "Save", function(r) {
  660. if(!r.exc) {
  661. me.refresh();
  662. if(save_action==="Save") {
  663. me.runclientscript("after_save", me.doctype, me.docname);
  664. }
  665. } else {
  666. if(on_error)
  667. on_error();
  668. }
  669. callback && callback(r);
  670. }, btn);
  671. }
  672. _f.Frm.prototype.savesubmit = function(btn, on_error) {
  673. var me = this;
  674. wn.confirm("Permanently Submit "+this.docname+"?", function() {
  675. me.save('Submit', function(r) {
  676. if(!r.exc) {
  677. me.runclientscript('on_submit', me.doctype, me.docname);
  678. }
  679. }, btn, on_error);
  680. });
  681. }
  682. _f.Frm.prototype.savecancel = function(btn, on_error) {
  683. var me = this;
  684. wn.confirm("Permanently Cancel "+this.docname+"?", function() {
  685. validated = true;
  686. me.runclientscript("before_cancel", me.doctype, me.docname);
  687. if(!validated) {
  688. if(on_error)
  689. on_error();
  690. return;
  691. }
  692. var doclist = new wn.model.DocList(me.doctype, me.docname);
  693. doclist.cancel(function(r) {
  694. if(!r.exc) {
  695. me.refresh();
  696. me.runclientscript("after_cancel", me.doctype, me.docname);
  697. }
  698. }, btn, on_error);
  699. });
  700. }
  701. // delete the record
  702. _f.Frm.prototype.savetrash = function() {
  703. wn.model.delete_doc(this.doctype, this.docname, function(r) {
  704. window.history.back();
  705. })
  706. }
  707. _f.Frm.prototype.amend_doc = function() {
  708. if(!this.fields_dict['amended_from']) {
  709. alert('"amended_from" field must be present to do an amendment.');
  710. return;
  711. }
  712. var me = this;
  713. var fn = function(newdoc) {
  714. newdoc.amended_from = me.docname;
  715. if(me.fields_dict && me.fields_dict['amendment_date'])
  716. newdoc.amendment_date = dateutil.obj_to_str(new Date());
  717. }
  718. this.copy_doc(fn, 1);
  719. }
  720. _f.Frm.prototype.disable_save = function() {
  721. // IMPORTANT: this function should be called in refresh event
  722. cur_frm.save_disabled = true;
  723. cur_frm.footer.hide_save();
  724. cur_frm.form_wrapper.appframe.buttons.Save.remove();
  725. delete cur_frm.form_wrapper.appframe.buttons.Save
  726. }
  727. _f.get_value = function(dt, dn, fn) {
  728. if(locals[dt] && locals[dt][dn])
  729. return locals[dt][dn][fn];
  730. }
  731. _f.Frm.prototype.set_value_in_locals = function(dt, dn, fn, v) {
  732. var d = locals[dt][dn];
  733. if (!d) return;
  734. var changed = d[fn] != v;
  735. if(changed && (d[fn]==null || v==null) && (cstr(d[fn])==cstr(v)))
  736. changed = false;
  737. if(changed) {
  738. d[fn] = v;
  739. this.dirty();
  740. }
  741. }
  742. _f.Frm.prototype.dirty = function() {
  743. this.doc.__unsaved = 1;
  744. $(this.wrapper).trigger('dirty')
  745. }
  746. _f.Frm.prototype.show_comments = function() {
  747. if(!cur_frm.comments) {
  748. cur_frm.comments = new Dialog(540, 400, 'Comments');
  749. cur_frm.comments.comment_body = $a(cur_frm.comments.body, 'div', 'dialog_frm');
  750. $y(cur_frm.comments.body, {backgroundColor:'#EEE'});
  751. cur_frm.comments.list = new CommentList(cur_frm.comments.comment_body);
  752. }
  753. cur_frm.comments.list.dt = cur_frm.doctype;
  754. cur_frm.comments.list.dn = cur_frm.docname;
  755. cur_frm.comments.show();
  756. cur_frm.comments.list.run();
  757. }