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.
 
 
 
 
 
 

1216 lines
31 KiB

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