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.
 
 
 
 
 
 

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