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.
 
 
 
 
 
 

1026 lines
26 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.content
  26. + wn.PageLayout (this.page_layout)
  27. + this.wrapper
  28. + this.wtab (table)
  29. + this.main
  30. + this.head
  31. + this.body
  32. + this.layout
  33. + this.footer
  34. + this.sidebar
  35. + this.print_wrapper
  36. + this.head
  37. */
  38. wn.provide('_f');
  39. wn.provide('wn.ui.form');
  40. wn.ui.form.Controller = Class.extend({
  41. init: function(opts) {
  42. $.extend(this, opts);
  43. }
  44. });
  45. _f.frms = {};
  46. _f.Frm = function(doctype, parent, in_form) {
  47. this.docname = '';
  48. this.doctype = doctype;
  49. this.display = 0;
  50. this.refresh_if_stale_for = 600;
  51. var me = this;
  52. this.last_view_is_edit = {};
  53. this.opendocs = {};
  54. this.sections = [];
  55. this.grids = [];
  56. this.cscript = new wn.ui.form.Controller({frm:this});
  57. this.pformat = {};
  58. this.fetch_dict = {};
  59. this.parent = parent;
  60. this.tinymce_id_list = [];
  61. this.setup_meta(doctype);
  62. // show in form instead of in dialog, when called using url (router.js)
  63. this.in_form = in_form ? true : false;
  64. // notify on rename
  65. var me = this;
  66. $(document).bind('rename', function(event, dt, old_name, new_name) {
  67. if(dt==me.doctype)
  68. me.rename_notify(dt, old_name, new_name)
  69. });
  70. }
  71. // ======================================================================================
  72. _f.Frm.prototype.check_doctype_conflict = function(docname) {
  73. var me = this;
  74. if(this.doctype=='DocType' && docname=='DocType') {
  75. msgprint('Allowing DocType, DocType. Be careful!')
  76. } else if(this.doctype=='DocType') {
  77. if (wn.views.formview[docname] || wn.pages['List/'+docname]) {
  78. msgprint("Cannot open DocType when its instance is open")
  79. throw 'doctype open conflict'
  80. }
  81. } else {
  82. if (wn.views.formview.DocType && wn.views.formview.DocType.frm.opendocs[this.doctype]) {
  83. msgprint("Cannot open instance when its DocType is open")
  84. throw 'doctype open conflict'
  85. }
  86. }
  87. }
  88. _f.Frm.prototype.setup = function() {
  89. var me = this;
  90. this.fields = [];
  91. this.fields_dict = {};
  92. this.state_fieldname = wn.workflow.get_state_fieldname(this.doctype);
  93. // wrapper
  94. this.wrapper = this.parent;
  95. // create area for print fomrat
  96. this.setup_print_layout();
  97. // 2 column layout
  98. this.setup_std_layout();
  99. // client script must be called after "setup" - there are no fields_dict attached to the frm otherwise
  100. this.setup_client_script();
  101. this.setup_done = true;
  102. }
  103. // ======================================================================================
  104. _f.Frm.prototype.setup_print_layout = function() {
  105. var me = this;
  106. this.print_wrapper = $a(this.wrapper, 'div');
  107. wn.ui.make_app_page({
  108. parent: this.print_wrapper,
  109. single_column: true,
  110. set_document_title: false,
  111. title: me.doctype + ": Print View",
  112. module: me.meta.module
  113. });
  114. var appframe = this.print_wrapper.appframe;
  115. appframe.add_button("View Details", function() {
  116. me.edit_doc();
  117. }).addClass("btn-success");
  118. appframe.add_button("Print", function() {
  119. me.print_doc();
  120. }, 'icon-print');
  121. this.$print_view_select = appframe.add_select("Select Preview", this.print_formats)
  122. .css({"float":"right"})
  123. .val(this.print_formats[0])
  124. .change(function() {
  125. me.refresh_print_layout();
  126. })
  127. appframe.add_ripped_paper_effect(this.print_wrapper);
  128. var layout_main = $(this.print_wrapper).find(".layout-main");
  129. this.print_body = $("<div style='margin: 25px'>").appendTo(layout_main)
  130. .css("min-height", "400px").get(0);
  131. }
  132. _f.Frm.prototype.onhide = function() { if(_f.cur_grid_cell) _f.cur_grid_cell.grid.cell_deselect(); }
  133. // ======================================================================================
  134. _f.Frm.prototype.setup_std_layout = function() {
  135. this.page_layout = new wn.PageLayout({
  136. parent: this.wrapper,
  137. main_width: (this.meta.in_dialog && !this.in_form) ? '100%' : '75%',
  138. sidebar_width: (this.meta.in_dialog && !this.in_form) ? '0%' : '25%'
  139. })
  140. // only tray
  141. this.meta.section_style='Simple'; // always simple!
  142. // layout
  143. this.layout = new Layout(this.page_layout.body, '100%');
  144. // sidebar
  145. if(this.meta.in_dialog && !this.in_form) {
  146. // hide sidebar
  147. $(this.page_layout.wrapper).removeClass('layout-wrapper-background');
  148. $(this.page_layout.main).removeClass('layout-main-section');
  149. $(this.page_layout.sidebar_area).toggle(false);
  150. } else {
  151. // module link
  152. this.setup_sidebar();
  153. }
  154. // state
  155. this.states = new wn.ui.form.States({
  156. frm: this
  157. });
  158. // watermark
  159. if(!this.meta.issingle) {
  160. $('<div style="font-size: 21px; color: #aaa; float: right;\
  161. margin-top: -5px; margin-right: -5px; z-index: 5;">'
  162. + wn._(this.doctype) + '</div>')
  163. .prependTo(this.page_layout.main);
  164. }
  165. // footer
  166. this.setup_footer();
  167. // header - no headers for tables and guests
  168. if(!(this.meta.istable || (this.meta.in_dialog && !this.in_form)))
  169. this.frm_head = new _f.FrmHeader(this.page_layout.head, this);
  170. // create fields
  171. this.setup_fields_std();
  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.setup_sidebar = function() {
  235. this.sidebar = new wn.widgets.form.sidebar.Sidebar(this);
  236. }
  237. _f.Frm.prototype.setup_footer = function() {
  238. var me = this;
  239. // footer toolbar
  240. var f = this.page_layout.footer;
  241. // save buttom
  242. f.save_area = $a(this.page_layout.footer,'div','',{display:'none', marginTop:'11px'});
  243. f.help_area = $a(this.page_layout.footer,'div');
  244. var b = $("<button class='btn btn-info'><i class='icon-save'></i> Save</button>")
  245. .click(function() { me.save("Save", null, me); }).appendTo(f.save_area);
  246. // show / hide save
  247. f.show_save = function() {
  248. $ds(me.page_layout.footer.save_area);
  249. }
  250. f.hide_save = function() {
  251. $dh(me.page_layout.footer.save_area);
  252. }
  253. }
  254. _f.Frm.prototype.set_intro = function(txt) {
  255. wn.utils.set_intro(this, this.page_layout.body, txt);
  256. }
  257. _f.Frm.prototype.set_footnote = function(txt) {
  258. wn.utils.set_footnote(this, this.page_layout.body, txt);
  259. }
  260. _f.Frm.prototype.setup_fields_std = function() {
  261. var fl = wn.meta.docfield_list[this.doctype];
  262. fl.sort(function(a,b) { return a.idx - b.idx});
  263. if(fl[0]&&fl[0].fieldtype!="Section Break" || get_url_arg('embed')) {
  264. this.layout.addrow(); // default section break
  265. if(fl[0].fieldtype!="Column Break") {// without column too
  266. var c = this.layout.addcell();
  267. $y(c.wrapper, {padding: '8px'});
  268. }
  269. }
  270. var sec;
  271. for(var i=0;i<fl.length;i++) {
  272. var f=fl[i];
  273. // if section break and next item
  274. // is a section break then ignore
  275. if(f.fieldtype=='Section Break' && fl[i+1] && fl[i+1].fieldtype=='Section Break')
  276. continue;
  277. var fn = f.fieldname?f.fieldname:f.label;
  278. var fld = make_field(f, this.doctype, this.layout.cur_cell, this);
  279. this.fields[this.fields.length] = fld;
  280. this.fields_dict[fn] = fld;
  281. if(sec && ['Section Break', 'Column Break'].indexOf(f.fieldtype)==-1) {
  282. fld.parent_section = sec;
  283. sec.fields.push(fld);
  284. }
  285. if(f.fieldtype=='Section Break') {
  286. sec = fld;
  287. this.sections.push(fld);
  288. }
  289. // default col-break after sec-break
  290. if((f.fieldtype=='Section Break')&&(fl[i+1])&&(fl[i+1].fieldtype!='Column Break')) {
  291. var c = this.layout.addcell();
  292. $y(c.wrapper, {padding: '8px'});
  293. }
  294. }
  295. }
  296. _f.Frm.prototype.add_custom_button = function(label, fn, icon) {
  297. this.frm_head.appframe.add_button(label, fn, icon);
  298. }
  299. _f.Frm.prototype.clear_custom_buttons = function() {
  300. this.frm_head.refresh_toolbar()
  301. }
  302. _f.Frm.prototype.add_fetch = function(link_field, src_field, tar_field) {
  303. if(!this.fetch_dict[link_field]) {
  304. this.fetch_dict[link_field] = {'columns':[], 'fields':[]}
  305. }
  306. this.fetch_dict[link_field].columns.push(src_field);
  307. this.fetch_dict[link_field].fields.push(tar_field);
  308. }
  309. _f.Frm.prototype.setup_client_script = function() {
  310. // setup client obj
  311. if(this.meta.client_script_core || this.meta.client_script || this.meta.__js) {
  312. this.runclientscript('setup', this.doctype, this.docname);
  313. }
  314. }
  315. _f.Frm.prototype.refresh_print_layout = function() {
  316. $ds(this.print_wrapper);
  317. $dh(this.page_layout.wrapper);
  318. var me = this;
  319. var print_callback = function(print_html) {
  320. me.print_body.innerHTML = print_html;
  321. }
  322. // print head
  323. if(cur_frm.doc.select_print_heading)
  324. cur_frm.set_print_heading(cur_frm.doc.select_print_heading)
  325. if(user!='Guest') {
  326. $di(this.view_btn_wrapper);
  327. // archive
  328. if(cur_frm.doc.__archived) {
  329. $dh(this.view_btn_wrapper);
  330. }
  331. } else {
  332. $dh(this.view_btn_wrapper);
  333. $dh(this.print_close_btn);
  334. }
  335. // create print format here
  336. _p.build(this.$print_view_select.val(), print_callback, null, 1);
  337. }
  338. _f.Frm.prototype.show_the_frm = function() {
  339. // show the dialog
  340. if(this.meta.in_dialog && !this.parent.dialog.display) {
  341. if(!this.meta.istable)
  342. this.parent.table_form = false;
  343. this.parent.dialog.show();
  344. }
  345. }
  346. _f.Frm.prototype.set_print_heading = function(txt) {
  347. this.pformat[cur_frm.docname] = txt;
  348. }
  349. _f.Frm.prototype.defocus_rest = function() {
  350. // deselect others
  351. if(_f.cur_grid_cell) _f.cur_grid_cell.grid.cell_deselect();
  352. }
  353. _f.Frm.prototype.refresh_header = function() {
  354. // set title
  355. // main title
  356. if(!this.meta.in_dialog || this.in_form) {
  357. set_title(this.meta.issingle ? this.doctype : this.docname);
  358. }
  359. if(wn.ui.toolbar.recent)
  360. wn.ui.toolbar.recent.add(this.doctype, this.docname, 1);
  361. // show / hide buttons
  362. if(this.frm_head)this.frm_head.refresh();
  363. }
  364. _f.Frm.prototype.check_doc_perm = function() {
  365. // get perm
  366. var dt = this.parent_doctype?this.parent_doctype : this.doctype;
  367. var dn = this.parent_docname?this.parent_docname : this.docname;
  368. this.perm = wn.perm.get_perm(dt, dn);
  369. if(!this.perm[0][READ]) {
  370. wn.container.change_to('403');
  371. return 0;
  372. }
  373. return 1
  374. }
  375. _f.Frm.prototype.refresh = function(docname) {
  376. // record switch
  377. if(docname) {
  378. if(this.docname != docname && (!this.meta.in_dialog || this.in_form) &&
  379. !this.meta.istable)
  380. scroll(0, 0);
  381. this.docname = docname;
  382. }
  383. if(!this.meta.istable) {
  384. cur_frm = this;
  385. this.parent.cur_frm = this;
  386. }
  387. if(this.docname) { // document to show
  388. // check permissions
  389. if(!this.check_doc_perm()) return;
  390. // read only (workflow)
  391. this.read_only = wn.workflow.is_read_only(this.doctype, this.docname);
  392. // set the doc
  393. this.doc = wn.model.get_doc(this.doctype, this.docname);
  394. // check if doctype is already open
  395. if (!this.opendocs[this.docname]) {
  396. this.check_doctype_conflict(this.docname);
  397. } else {
  398. if(this.doc && this.doc.__last_sync_on &&
  399. (new Date() - this.doc.__last_sync_on) > (this.refresh_if_stale_for * 1000)) {
  400. this.reload_doc();
  401. return;
  402. }
  403. }
  404. // do setup
  405. if(!this.setup_done) this.setup();
  406. // set customized permissions for this record
  407. this.runclientscript('set_perm',this.doctype, this.docname);
  408. // load the record for the first time, if not loaded (call 'onload')
  409. cur_frm.cscript.is_onload = false;
  410. if(!this.opendocs[this.docname]) {
  411. cur_frm.cscript.is_onload = true;
  412. this.setnewdoc(this.docname);
  413. }
  414. // view_is_edit
  415. if(this.doc.__islocal)
  416. this.last_view_is_edit[this.docname] = 1; // new is view_is_edit
  417. this.view_is_edit = this.last_view_is_edit[this.docname];
  418. if(this.view_is_edit || (!this.view_is_edit && this.meta.istable)) {
  419. if(this.print_wrapper) {
  420. $dh(this.print_wrapper);
  421. $ds(this.page_layout.wrapper);
  422. }
  423. // header
  424. if(!this.meta.istable) {
  425. this.refresh_header();
  426. this.sidebar && this.sidebar.refresh();
  427. }
  428. // call trigger
  429. this.runclientscript('refresh');
  430. // trigger global trigger
  431. // to use this
  432. $(document).trigger('form_refresh');
  433. // fields
  434. this.refresh_fields();
  435. // dependent fields
  436. this.refresh_dependency();
  437. // footer
  438. this.refresh_footer();
  439. // layout
  440. if(this.layout) this.layout.show();
  441. // call onload post render for callbacks to be fired
  442. if(this.cscript.is_onload) {
  443. this.runclientscript('onload_post_render', this.doctype, this.docname);
  444. }
  445. // focus on first input
  446. if(this.doc.docstatus==0) {
  447. var first = $(this.wrapper).find('.form-layout-row :input:first');
  448. if(first.attr("data-fieldtype")!="Date") {
  449. first.focus();
  450. }
  451. }
  452. } else {
  453. this.refresh_header();
  454. if(this.print_wrapper) {
  455. this.refresh_print_layout();
  456. }
  457. this.runclientscript('edit_status_changed');
  458. }
  459. $(cur_frm.wrapper).trigger('render_complete');
  460. }
  461. }
  462. _f.Frm.prototype.refresh_footer = function() {
  463. var f = this.page_layout.footer;
  464. if(f.save_area) {
  465. // if save button is there in the header
  466. if(this.frm_head && this.frm_head.appframe.toolbar
  467. && this.frm_head.appframe.toolbar.find(".btn-save").length && !this.save_disabled
  468. && (this.fields && this.fields.length > 7)) {
  469. f.show_save();
  470. } else {
  471. f.hide_save();
  472. }
  473. }
  474. }
  475. _f.Frm.prototype.refresh_field = function(fname) {
  476. cur_frm.fields_dict[fname] && cur_frm.fields_dict[fname].refresh
  477. && cur_frm.fields_dict[fname].refresh();
  478. }
  479. _f.Frm.prototype.refresh_fields = function() {
  480. // refresh fields
  481. for(var i=0; i<this.fields.length; i++) {
  482. var f = this.fields[i];
  483. f.perm = this.perm;
  484. f.docname = this.docname;
  485. // if field is identifiable (not blank section or column break)
  486. // get the "customizable" parameters for this record
  487. var fn = f.df.fieldname || f.df.label;
  488. if(fn)
  489. f.df = wn.meta.get_docfield(this.doctype, fn, this.docname);
  490. if(f.df.fieldtype!='Section Break' && f.refresh) {
  491. f.refresh();
  492. }
  493. }
  494. // refresh sections
  495. $.each(this.sections, function(i, f) {
  496. f.refresh(true);
  497. })
  498. // cleanup activities after refresh
  499. this.cleanup_refresh(this);
  500. }
  501. _f.Frm.prototype.cleanup_refresh = function() {
  502. var me = this;
  503. if(me.fields_dict['amended_from']) {
  504. if (me.doc.amended_from) {
  505. unhide_field('amended_from');
  506. if (me.fields_dict['amendment_date']) unhide_field('amendment_date');
  507. } else {
  508. hide_field('amended_from');
  509. if (me.fields_dict['amendment_date']) hide_field('amendment_date');
  510. }
  511. }
  512. if(me.fields_dict['trash_reason']) {
  513. if(me.doc.trash_reason && me.doc.docstatus == 2) {
  514. unhide_field('trash_reason');
  515. } else {
  516. hide_field('trash_reason');
  517. }
  518. }
  519. if(me.meta.autoname && me.meta.autoname.substr(0,6)=='field:' && !me.doc.__islocal) {
  520. var fn = me.meta.autoname.substr(6);
  521. cur_frm.toggle_display(fn, false);
  522. }
  523. }
  524. // Resolve "depends_on" and show / hide accordingly
  525. _f.Frm.prototype.refresh_dependency = function() {
  526. var me = this;
  527. var doc = locals[this.doctype][this.docname];
  528. // build dependants' dictionary
  529. var has_dep = false;
  530. for(fkey in me.fields) {
  531. var f = me.fields[fkey];
  532. f.dependencies_clear = true;
  533. if(f.df.depends_on) {
  534. has_dep = true;
  535. }
  536. }
  537. if(!has_dep)return;
  538. // show / hide based on values
  539. for(var i=me.fields.length-1;i>=0;i--) {
  540. var f = me.fields[i];
  541. f.guardian_has_value = true;
  542. if(f.df.depends_on) {
  543. // evaluate guardian
  544. var v = doc[f.df.depends_on];
  545. if(f.df.depends_on.substr(0,5)=='eval:') {
  546. f.guardian_has_value = eval(f.df.depends_on.substr(5));
  547. } else if(f.df.depends_on.substr(0,3)=='fn:') {
  548. f.guardian_has_value = me.runclientscript(f.df.depends_on.substr(3), me.doctype, me.docname);
  549. } else {
  550. if(!v) {
  551. f.guardian_has_value = false;
  552. }
  553. }
  554. // show / hide
  555. if(f.guardian_has_value) {
  556. f.df.hidden = 0;
  557. f.refresh();
  558. } else {
  559. f.df.hidden = 1;
  560. f.refresh();
  561. }
  562. }
  563. }
  564. }
  565. // setnewdoc is called when a record is loaded for the first time
  566. // ======================================================================================
  567. _f.Frm.prototype.setnewdoc = function(docname) {
  568. // moved this call to refresh function
  569. // this.check_doctype_conflict(docname);
  570. // if loaded
  571. if(this.opendocs[docname]) { // already exists
  572. this.docname=docname;
  573. return;
  574. }
  575. // make a copy of the doctype for client script settings
  576. // each record will have its own client script
  577. wn.meta.make_docfield_copy_for(this.doctype,docname);
  578. this.docname = docname;
  579. var me = this;
  580. var viewname = this.meta.issingle ? this.doctype : docname;
  581. // Client Script
  582. this.runclientscript('onload', this.doctype, this.docname);
  583. this.last_view_is_edit[docname] = 1;
  584. if(cint(this.meta.read_only_onload)) this.last_view_is_edit[docname] = 0;
  585. this.opendocs[docname] = true;
  586. }
  587. _f.Frm.prototype.edit_doc = function() {
  588. // set fields
  589. this.last_view_is_edit[this.docname] = true;
  590. this.refresh();
  591. }
  592. _f.Frm.prototype.show_doc = function(dn) {
  593. this.refresh(dn);
  594. }
  595. _f.Frm.prototype.runscript = function(scriptname, callingfield, onrefresh) {
  596. var me = this;
  597. if(this.docname) {
  598. // make doc list
  599. var doclist = wn.model.compress(make_doclist(this.doctype, this.docname));
  600. // send to run
  601. if(callingfield)
  602. $(callingfield.input).set_working();
  603. $c('runserverobj', {'docs':doclist, 'method':scriptname },
  604. function(r, rtxt) {
  605. // run refresh
  606. if(onrefresh)
  607. onrefresh(r,rtxt);
  608. // fields
  609. me.refresh_fields();
  610. // dependent fields
  611. me.refresh_dependency();
  612. // enable button
  613. if(callingfield)
  614. $(callingfield.input).done_working();
  615. }
  616. );
  617. }
  618. }
  619. _f.Frm.prototype.runclientscript = function(caller, cdt, cdn) {
  620. if(!cdt)cdt = this.doctype;
  621. if(!cdn)cdn = this.docname;
  622. var ret = null;
  623. var doc = locals[cur_frm.doc.doctype][cur_frm.doc.name];
  624. try {
  625. if(this.cscript[caller])
  626. ret = this.cscript[caller](doc, cdt, cdn);
  627. if(this.cscript['custom_'+caller])
  628. ret += this.cscript['custom_'+caller](doc, cdt, cdn);
  629. } catch(e) {
  630. validated = false;
  631. console.log(e);
  632. }
  633. if(caller && caller.toLowerCase()=='setup') {
  634. this.setup_client_js();
  635. }
  636. return ret;
  637. }
  638. _f.Frm.prototype.setup_client_js = function(caller, cdt, cdn) {
  639. var doctype = wn.model.get_doc('DocType', this.doctype);
  640. // js
  641. var cs = doctype.__js || (doctype.client_script_core + doctype.client_script);
  642. if(cs) {
  643. try {
  644. var tmp = eval(cs);
  645. } catch(e) {
  646. show_alert("Error in Client Script.")
  647. console.log(e);
  648. }
  649. }
  650. // css
  651. if(doctype.__css) set_style(doctype.__css)
  652. // ---Client String----
  653. if(doctype.client_string) { // split client string
  654. this.cstring = {};
  655. var elist = doctype.client_string.split('---');
  656. for(var i=1;i<elist.length;i=i+2) {
  657. this.cstring[strip(elist[i])] = elist[i+1];
  658. }
  659. }
  660. }
  661. _f.Frm.prototype.copy_doc = function(onload, from_amend) {
  662. if(!this.perm[0][CREATE]) {
  663. msgprint('You are not allowed to create '+this.meta.name);
  664. return;
  665. }
  666. var dn = this.docname;
  667. // copy parent
  668. var newdoc = wn.model.copy_doc(this.doctype, dn, from_amend);
  669. // do not copy attachments
  670. if(this.meta.allow_attach && newdoc.file_list && !from_amend)
  671. newdoc.file_list = null;
  672. // copy chidren
  673. var dl = make_doclist(this.doctype, dn);
  674. // table fields dict - for no_copy check
  675. var tf_dict = {};
  676. for(var d in dl) {
  677. d1 = dl[d];
  678. // get tabel field
  679. if(d1.parentfield && !tf_dict[d1.parentfield]) {
  680. tf_dict[d1.parentfield] = wn.meta.get_docfield(d1.parenttype, d1.parentfield);
  681. }
  682. if(d1.parent==dn && cint(tf_dict[d1.parentfield].no_copy)!=1) {
  683. var ch = wn.model.copy_doc(d1.doctype, d1.name, from_amend);
  684. ch.parent = newdoc.name;
  685. ch.docstatus = 0;
  686. ch.owner = user;
  687. ch.creation = '';
  688. ch.modified_by = user;
  689. ch.modified = '';
  690. }
  691. }
  692. newdoc.__islocal = 1;
  693. newdoc.docstatus = 0;
  694. newdoc.owner = user;
  695. newdoc.creation = '';
  696. newdoc.modified_by = user;
  697. newdoc.modified = '';
  698. if(onload)onload(newdoc);
  699. loaddoc(newdoc.doctype, newdoc.name);
  700. }
  701. _f.Frm.prototype.reload_doc = function() {
  702. this.check_doctype_conflict(this.docname);
  703. var me = this;
  704. var onsave = function(r, rtxt) {
  705. // n tweets and last comment
  706. me.runclientscript('setup', me.doctype, me.docname);
  707. me.refresh();
  708. }
  709. if(me.doc.__islocal) {
  710. // reload only doctype
  711. $c('webnotes.widgets.form.load.getdoctype', {'doctype':me.doctype }, onsave, null, null, 'Refreshing ' + me.doctype + '...');
  712. } else {
  713. // reload doc and docytpe
  714. $c('webnotes.widgets.form.load.getdoc', {'name':me.docname, 'doctype':me.doctype, 'getdoctype':1, 'user':user}, onsave, null, null, 'Refreshing ' + me.docname + '...');
  715. }
  716. }
  717. var validated;
  718. _f.Frm.prototype.save = function(save_action, callback, btn) {
  719. $(document.activeElement).blur();
  720. var me = this;
  721. // validate
  722. if(save_action!="Cancel") {
  723. validated = true;
  724. this.runclientscript('validate');
  725. if(!validated) {
  726. return;
  727. }
  728. }
  729. var doclist = new wn.model.DocList(this.doctype, this.docname);
  730. doclist.save(save_action || "Save", function(r) {
  731. if(!r.exc) {
  732. me.refresh();
  733. if(save_action==="Save") {
  734. me.runclientscript("after_save", me.doctype, me.docname);
  735. }
  736. }
  737. callback && callback(r);
  738. }, btn);
  739. }
  740. _f.Frm.prototype.savesubmit = function(btn) {
  741. var me = this;
  742. wn.confirm("Permanently Submit "+this.docname+"?", function() {
  743. me.save('Submit', function(r) {
  744. if(!r.exc) {
  745. me.runclientscript('on_submit', me.doctype, me.docname);
  746. }
  747. }, btn);
  748. });
  749. }
  750. _f.Frm.prototype.savecancel = function(btn) {
  751. var me = this;
  752. wn.confirm("Permanently Cancel "+this.docname+"?", function() {
  753. me.runclientscript("before_cancel", me.doctype, me.docname);
  754. var doclist = new wn.model.DocList(me.doctype, me.docname);
  755. doclist.cancel(function(r) {
  756. if(!r.exc) {
  757. me.refresh();
  758. me.runclientscript("after_cancel", me.doctype, me.docname);
  759. }
  760. }, btn);
  761. });
  762. }
  763. // delete the record
  764. _f.Frm.prototype.savetrash = function() {
  765. wn.model.delete_doc(this.doctype, this.docname, function(r) {
  766. window.history.back();
  767. })
  768. }
  769. _f.Frm.prototype.amend_doc = function() {
  770. if(!this.fields_dict['amended_from']) {
  771. alert('"amended_from" field must be present to do an amendment.');
  772. return;
  773. }
  774. var me = this;
  775. var fn = function(newdoc) {
  776. newdoc.amended_from = me.docname;
  777. if(me.fields_dict && me.fields_dict['amendment_date'])
  778. newdoc.amendment_date = dateutil.obj_to_str(new Date());
  779. }
  780. this.copy_doc(fn, 1);
  781. }
  782. _f.Frm.prototype.disable_save = function() {
  783. // IMPORTANT: this function should be called in refresh event
  784. cur_frm.save_disabled = true;
  785. cur_frm.page_layout.footer.hide_save();
  786. cur_frm.frm_head.appframe.buttons.Save.remove();
  787. }
  788. _f.get_value = function(dt, dn, fn) {
  789. if(locals[dt] && locals[dt][dn])
  790. return locals[dt][dn][fn];
  791. }
  792. _f.Frm.prototype.set_value_in_locals = function(dt, dn, fn, v) {
  793. var d = locals[dt][dn];
  794. if (!d) return;
  795. var changed = d[fn] != v;
  796. if(changed && (d[fn]==null || v==null) && (cstr(d[fn])==cstr(v)))
  797. changed = false;
  798. if(changed) {
  799. d[fn] = v;
  800. if(d.parenttype)
  801. d.__unsaved = 1;
  802. this.set_unsaved();
  803. }
  804. }
  805. _f.Frm.prototype.set_unsaved = function() {
  806. if(cur_frm.doc.__unsaved)
  807. return;
  808. cur_frm.doc.__unsaved = 1;
  809. var frm_head;
  810. if(cur_frm.frm_head) {
  811. frm_head = cur_frm.frm_head;
  812. } else if(wn.container.page.frm && wn.container.page.frm.frm_head) {
  813. frm_head = wn.container.page.frm.frm_head
  814. }
  815. if(frm_head) frm_head.refresh_labels();
  816. }
  817. _f.Frm.prototype.show_comments = function() {
  818. if(!cur_frm.comments) {
  819. cur_frm.comments = new Dialog(540, 400, 'Comments');
  820. cur_frm.comments.comment_body = $a(cur_frm.comments.body, 'div', 'dialog_frm');
  821. $y(cur_frm.comments.body, {backgroundColor:'#EEE'});
  822. cur_frm.comments.list = new CommentList(cur_frm.comments.comment_body);
  823. }
  824. cur_frm.comments.list.dt = cur_frm.doctype;
  825. cur_frm.comments.list.dn = cur_frm.docname;
  826. cur_frm.comments.show();
  827. cur_frm.comments.list.run();
  828. }