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.
 
 
 
 
 
 

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