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

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