Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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