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.
 
 
 
 
 
 

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