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.
 
 
 
 
 
 

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