Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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