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.
 
 
 
 
 
 

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