25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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