您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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