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