Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

1015 rader
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.is_editable = {};
  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. // watermark
  154. $('<div style="font-size: 21px; color: #aaa; float: right;\
  155. margin-top: -5px; margin-right: -5px; z-index: 5;">'
  156. + this.doctype + '</div>')
  157. .prependTo(this.page_layout.main);
  158. // footer
  159. this.setup_footer();
  160. // header - no headers for tables and guests
  161. if(!(this.meta.istable || (this.meta.in_dialog && !this.in_form)))
  162. this.frm_head = new _f.FrmHeader(this.page_layout.head, this);
  163. // create fields
  164. this.setup_fields_std();
  165. }
  166. _f.Frm.prototype.setup_print = function() {
  167. this.print_formats = wn.meta.get_print_formats(this.meta.name);
  168. this.print_sel = $a(null, 'select', '', {width:'160px'});
  169. add_sel_options(this.print_sel, this.print_formats);
  170. this.print_sel.value = this.print_formats[0];
  171. }
  172. _f.Frm.prototype.print_doc = function() {
  173. if(this.doc.docstatus==2) {
  174. msgprint("Cannot Print Cancelled Documents.");
  175. return;
  176. }
  177. _p.show_dialog(); // multiple options
  178. }
  179. // email the form
  180. _f.Frm.prototype.email_doc = function(message) {
  181. new wn.views.CommunicationComposer({
  182. doc: this.doc,
  183. subject: wn._(this.meta.name) + ': ' + this.docname,
  184. recipients: this.doc.email || this.doc.email_id || this.doc.contact_email,
  185. attach_document_print: true,
  186. message: message,
  187. real_name: this.doc.real_name || this.doc.contact_display || this.doc.contact_name
  188. });
  189. }
  190. // email the form
  191. _f.Frm.prototype.rename_doc = function() {
  192. wn.model.rename_doc(this.doctype, this.docname);
  193. }
  194. // notify this form of renamed records
  195. _f.Frm.prototype.rename_notify = function(dt, old, name) {
  196. // from form
  197. if(this.meta.istable)
  198. return;
  199. if(this.docname == old)
  200. this.docname = name;
  201. else
  202. return;
  203. // editable
  204. this.is_editable[name] = this.is_editable[old];
  205. delete this.is_editable[old];
  206. // cleanup
  207. if(this && this.opendocs[old]) {
  208. // delete docfield copy
  209. wn.meta.docfield_copy[dt][name] = wn.meta.docfield_copy[dt][old];
  210. delete wn.meta.docfield_copy[dt][old];
  211. }
  212. delete this.opendocs[old];
  213. this.opendocs[name] = true;
  214. if(this.meta.in_dialog || !this.in_form) {
  215. return;
  216. }
  217. wn.re_route[window.location.hash] = '#Form/' + encodeURIComponent(this.doctype) + '/' + encodeURIComponent(name);
  218. wn.set_route('Form', this.doctype, name);
  219. }
  220. // SETUP
  221. _f.Frm.prototype.setup_meta = function(doctype) {
  222. this.meta = wn.model.get_doc('DocType',this.doctype);
  223. this.perm = wn.perm.get_perm(this.doctype); // for create
  224. if(this.meta.istable) { this.meta.in_dialog = 1 }
  225. this.setup_print();
  226. }
  227. _f.Frm.prototype.setup_sidebar = function() {
  228. this.sidebar = new wn.widgets.form.sidebar.Sidebar(this);
  229. }
  230. _f.Frm.prototype.setup_footer = function() {
  231. var me = this;
  232. // footer toolbar
  233. var f = this.page_layout.footer;
  234. // save buttom
  235. f.save_area = $a(this.page_layout.footer,'div','',{display:'none', marginTop:'11px'});
  236. f.help_area = $a(this.page_layout.footer,'div');
  237. var b = $("<button class='btn btn-info'><i class='icon-save'></i> Save</button>")
  238. .click(function() { me.save("Save", null, me); }).appendTo(f.save_area);
  239. // show / hide save
  240. f.show_save = function() {
  241. $ds(me.page_layout.footer.save_area);
  242. }
  243. f.hide_save = function() {
  244. $dh(me.page_layout.footer.save_area);
  245. }
  246. }
  247. _f.Frm.prototype.set_intro = function(txt) {
  248. wn.utils.set_intro(this, this.page_layout.body, txt);
  249. }
  250. _f.Frm.prototype.set_footnote = function(txt) {
  251. wn.utils.set_footnote(this, this.page_layout.body, txt);
  252. }
  253. _f.Frm.prototype.setup_fields_std = function() {
  254. var fl = wn.meta.docfield_list[this.doctype];
  255. fl.sort(function(a,b) { return a.idx - b.idx});
  256. if(fl[0]&&fl[0].fieldtype!="Section Break" || get_url_arg('embed')) {
  257. this.layout.addrow(); // default section break
  258. if(fl[0].fieldtype!="Column Break") {// without column too
  259. var c = this.layout.addcell();
  260. $y(c.wrapper, {padding: '8px'});
  261. }
  262. }
  263. var sec;
  264. for(var i=0;i<fl.length;i++) {
  265. var f=fl[i];
  266. // if section break and next item
  267. // is a section break then ignore
  268. if(f.fieldtype=='Section Break' && fl[i+1] && fl[i+1].fieldtype=='Section Break')
  269. continue;
  270. var fn = f.fieldname?f.fieldname:f.label;
  271. var fld = make_field(f, this.doctype, this.layout.cur_cell, this);
  272. this.fields[this.fields.length] = fld;
  273. this.fields_dict[fn] = fld;
  274. if(sec && ['Section Break', 'Column Break'].indexOf(f.fieldtype)==-1) {
  275. fld.parent_section = sec;
  276. sec.fields.push(fld);
  277. }
  278. if(f.fieldtype=='Section Break') {
  279. sec = fld;
  280. this.sections.push(fld);
  281. }
  282. // default col-break after sec-break
  283. if((f.fieldtype=='Section Break')&&(fl[i+1])&&(fl[i+1].fieldtype!='Column Break')) {
  284. var c = this.layout.addcell();
  285. $y(c.wrapper, {padding: '8px'});
  286. }
  287. }
  288. }
  289. _f.Frm.prototype.add_custom_button = function(label, fn, icon) {
  290. this.frm_head.appframe.add_button(label, fn, icon);
  291. }
  292. _f.Frm.prototype.clear_custom_buttons = function() {
  293. this.frm_head.refresh_toolbar()
  294. }
  295. _f.Frm.prototype.add_fetch = function(link_field, src_field, tar_field) {
  296. if(!this.fetch_dict[link_field]) {
  297. this.fetch_dict[link_field] = {'columns':[], 'fields':[]}
  298. }
  299. this.fetch_dict[link_field].columns.push(src_field);
  300. this.fetch_dict[link_field].fields.push(tar_field);
  301. }
  302. _f.Frm.prototype.setup_client_script = function() {
  303. // setup client obj
  304. if(this.meta.client_script_core || this.meta.client_script || this.meta.__js) {
  305. this.runclientscript('setup', this.doctype, this.docname);
  306. }
  307. }
  308. _f.Frm.prototype.refresh_print_layout = function() {
  309. $ds(this.print_wrapper);
  310. $dh(this.page_layout.wrapper);
  311. var me = this;
  312. var print_callback = function(print_html) {
  313. me.print_body.innerHTML = print_html;
  314. }
  315. // print head
  316. if(cur_frm.doc.select_print_heading)
  317. cur_frm.set_print_heading(cur_frm.doc.select_print_heading)
  318. if(user!='Guest') {
  319. $di(this.view_btn_wrapper);
  320. // archive
  321. if(cur_frm.doc.__archived) {
  322. $dh(this.view_btn_wrapper);
  323. }
  324. } else {
  325. $dh(this.view_btn_wrapper);
  326. $dh(this.print_close_btn);
  327. }
  328. // create print format here
  329. _p.build(this.$print_view_select.val(), print_callback, null, 1);
  330. }
  331. _f.Frm.prototype.show_the_frm = function() {
  332. // show the dialog
  333. if(this.meta.in_dialog && !this.parent.dialog.display) {
  334. if(!this.meta.istable)
  335. this.parent.table_form = false;
  336. this.parent.dialog.show();
  337. }
  338. }
  339. _f.Frm.prototype.set_print_heading = function(txt) {
  340. this.pformat[cur_frm.docname] = txt;
  341. }
  342. _f.Frm.prototype.defocus_rest = function() {
  343. // deselect others
  344. if(_f.cur_grid_cell) _f.cur_grid_cell.grid.cell_deselect();
  345. }
  346. _f.Frm.prototype.refresh_header = function() {
  347. // set title
  348. // main title
  349. if(!this.meta.in_dialog || this.in_form) {
  350. set_title(this.meta.issingle ? this.doctype : this.docname);
  351. }
  352. if(wn.ui.toolbar.recent)
  353. wn.ui.toolbar.recent.add(this.doctype, this.docname, 1);
  354. // show / hide buttons
  355. if(this.frm_head)this.frm_head.refresh();
  356. }
  357. _f.Frm.prototype.check_doc_perm = function() {
  358. // get perm
  359. var dt = this.parent_doctype?this.parent_doctype : this.doctype;
  360. var dn = this.parent_docname?this.parent_docname : this.docname;
  361. this.perm = wn.perm.get_perm(dt, dn);
  362. this.orig_perm = wn.perm.get_perm(dt, dn, 1);
  363. if(!this.perm[0][READ]) {
  364. if(user=='Guest') {
  365. // allow temp access? via encryted akey
  366. if(_f.temp_access[dt] && _f.temp_access[dt][dn]) {
  367. this.perm = [[1,0,0]]
  368. return 1;
  369. }
  370. }
  371. window.history.back();
  372. return 0;
  373. }
  374. return 1
  375. }
  376. _f.Frm.prototype.refresh = function(docname) {
  377. // record switch
  378. if(docname) {
  379. if(this.docname != docname && (!this.meta.in_dialog || this.in_form) &&
  380. !this.meta.istable)
  381. scroll(0, 0);
  382. this.docname = docname;
  383. }
  384. if(!this.meta.istable) {
  385. cur_frm = this;
  386. this.parent.cur_frm = this;
  387. }
  388. if(this.docname) { // document to show
  389. // check permissions
  390. if(!this.check_doc_perm()) return;
  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. // editable
  414. if(this.doc.__islocal)
  415. this.is_editable[this.docname] = 1; // new is editable
  416. this.editable = this.is_editable[this.docname];
  417. if(this.editable || (!this.editable && this.meta.istable)) {
  418. // show form layout (with fields etc)
  419. // ----------------------------------
  420. if(this.print_wrapper) {
  421. $dh(this.print_wrapper);
  422. $ds(this.page_layout.wrapper);
  423. }
  424. // header
  425. if(!this.meta.istable) {
  426. this.refresh_header();
  427. this.sidebar && this.sidebar.refresh();
  428. }
  429. // call trigger
  430. this.runclientscript('refresh');
  431. // trigger global trigger
  432. // to use this
  433. $(document).trigger('form_refresh');
  434. // fields
  435. this.refresh_fields();
  436. // dependent fields
  437. this.refresh_dependency();
  438. // footer
  439. this.refresh_footer();
  440. // layout
  441. if(this.layout) this.layout.show();
  442. // call onload post render for callbacks to be fired
  443. if(this.cscript.is_onload) {
  444. this.runclientscript('onload_post_render', this.doctype, this.docname);
  445. }
  446. // focus on first input
  447. if(this.doc.docstatus==0) {
  448. $(this.wrapper).find('.form-layout-row :input:first').focus();
  449. }
  450. } else {
  451. // show print layout
  452. // ----------------------------------
  453. this.refresh_header();
  454. if(this.print_wrapper) {
  455. this.refresh_print_layout();
  456. }
  457. this.runclientscript('edit_status_changed');
  458. }
  459. $(cur_frm.wrapper).trigger('render_complete');
  460. }
  461. }
  462. _f.Frm.prototype.refresh_footer = function() {
  463. var f = this.page_layout.footer;
  464. if(f.save_area) {
  465. if(this.editable && (!this.meta.in_dialog || this.in_form)
  466. && this.doc.docstatus==0 && !this.meta.istable && this.perm[0][WRITE]
  467. && (this.fields && this.fields.length > 7) && !this.save_disabled) {
  468. f.show_save();
  469. } else {
  470. f.hide_save();
  471. }
  472. }
  473. }
  474. _f.Frm.prototype.refresh_field = function(fname) {
  475. cur_frm.fields_dict[fname] && cur_frm.fields_dict[fname].refresh
  476. && cur_frm.fields_dict[fname].refresh();
  477. }
  478. _f.Frm.prototype.refresh_fields = function() {
  479. // refresh fields
  480. for(var i=0; i<this.fields.length; i++) {
  481. var f = this.fields[i];
  482. f.perm = this.perm;
  483. f.docname = this.docname;
  484. // if field is identifiable (not blank section or column break)
  485. // get the "customizable" parameters for this record
  486. var fn = f.df.fieldname || f.df.label;
  487. if(fn)
  488. f.df = wn.meta.get_docfield(this.doctype, fn, this.docname);
  489. if(f.df.fieldtype!='Section Break' && f.refresh) {
  490. f.refresh();
  491. }
  492. }
  493. // refresh sections
  494. $.each(this.sections, function(i, f) {
  495. f.refresh(true);
  496. })
  497. // cleanup activities after refresh
  498. this.cleanup_refresh(this);
  499. }
  500. _f.Frm.prototype.cleanup_refresh = function() {
  501. var me = this;
  502. if(me.fields_dict['amended_from']) {
  503. if (me.doc.amended_from) {
  504. unhide_field('amended_from');
  505. if (me.fields_dict['amendment_date']) unhide_field('amendment_date');
  506. } else {
  507. hide_field('amended_from');
  508. if (me.fields_dict['amendment_date']) hide_field('amendment_date');
  509. }
  510. }
  511. if(me.fields_dict['trash_reason']) {
  512. if(me.doc.trash_reason && me.doc.docstatus == 2) {
  513. unhide_field('trash_reason');
  514. } else {
  515. hide_field('trash_reason');
  516. }
  517. }
  518. if(me.meta.autoname && me.meta.autoname.substr(0,6)=='field:' && !me.doc.__islocal) {
  519. var fn = me.meta.autoname.substr(6);
  520. cur_frm.toggle_display(fn, false);
  521. }
  522. }
  523. // Resolve "depends_on" and show / hide accordingly
  524. _f.Frm.prototype.refresh_dependency = function() {
  525. var me = this;
  526. var doc = locals[this.doctype][this.docname];
  527. // build dependants' dictionary
  528. var has_dep = false;
  529. for(fkey in me.fields) {
  530. var f = me.fields[fkey];
  531. f.dependencies_clear = true;
  532. if(f.df.depends_on) {
  533. has_dep = true;
  534. }
  535. }
  536. if(!has_dep)return;
  537. // show / hide based on values
  538. for(var i=me.fields.length-1;i>=0;i--) {
  539. var f = me.fields[i];
  540. f.guardian_has_value = true;
  541. if(f.df.depends_on) {
  542. // evaluate guardian
  543. var v = doc[f.df.depends_on];
  544. if(f.df.depends_on.substr(0,5)=='eval:') {
  545. f.guardian_has_value = eval(f.df.depends_on.substr(5));
  546. } else if(f.df.depends_on.substr(0,3)=='fn:') {
  547. f.guardian_has_value = me.runclientscript(f.df.depends_on.substr(3), me.doctype, me.docname);
  548. } else {
  549. if(!v) {
  550. f.guardian_has_value = false;
  551. }
  552. }
  553. // show / hide
  554. if(f.guardian_has_value) {
  555. f.df.hidden = 0;
  556. f.refresh();
  557. } else {
  558. f.df.hidden = 1;
  559. f.refresh();
  560. }
  561. }
  562. }
  563. }
  564. // setnewdoc is called when a record is loaded for the first time
  565. // ======================================================================================
  566. _f.Frm.prototype.setnewdoc = function(docname) {
  567. // moved this call to refresh function
  568. // this.check_doctype_conflict(docname);
  569. // if loaded
  570. if(this.opendocs[docname]) { // already exists
  571. this.docname=docname;
  572. return;
  573. }
  574. // make a copy of the doctype for client script settings
  575. // each record will have its own client script
  576. wn.meta.make_docfield_copy_for(this.doctype,docname);
  577. this.docname = docname;
  578. var me = this;
  579. var viewname = this.meta.issingle ? this.doctype : docname;
  580. // Client Script
  581. this.runclientscript('onload', this.doctype, this.docname);
  582. this.is_editable[docname] = 1;
  583. if(cint(this.meta.read_only_onload)) this.is_editable[docname] = 0;
  584. this.opendocs[docname] = true;
  585. }
  586. _f.Frm.prototype.edit_doc = function() {
  587. // set fields
  588. this.is_editable[this.docname] = true;
  589. this.refresh();
  590. }
  591. _f.Frm.prototype.show_doc = function(dn) {
  592. this.refresh(dn);
  593. }
  594. _f.Frm.prototype.runscript = function(scriptname, callingfield, onrefresh) {
  595. var me = this;
  596. if(this.docname) {
  597. // make doc list
  598. var doclist = wn.model.compress(make_doclist(this.doctype, this.docname));
  599. // send to run
  600. if(callingfield)
  601. $(callingfield.input).set_working();
  602. $c('runserverobj', {'docs':doclist, 'method':scriptname },
  603. function(r, rtxt) {
  604. // run refresh
  605. if(onrefresh)
  606. onrefresh(r,rtxt);
  607. // fields
  608. me.refresh_fields();
  609. // dependent fields
  610. me.refresh_dependency();
  611. // enable button
  612. if(callingfield)
  613. $(callingfield.input).done_working();
  614. }
  615. );
  616. }
  617. }
  618. _f.Frm.prototype.runclientscript = function(caller, cdt, cdn) {
  619. if(!cdt)cdt = this.doctype;
  620. if(!cdn)cdn = this.docname;
  621. var ret = null;
  622. var doc = locals[cur_frm.doc.doctype][cur_frm.doc.name];
  623. try {
  624. if(this.cscript[caller])
  625. ret = this.cscript[caller](doc, cdt, cdn);
  626. if(this.cscript['custom_'+caller])
  627. ret += this.cscript['custom_'+caller](doc, cdt, cdn);
  628. } catch(e) {
  629. validated = false;
  630. console.log(e);
  631. }
  632. if(caller && caller.toLowerCase()=='setup') {
  633. var doctype = wn.model.get_doc('DocType', this.doctype);
  634. // js
  635. var cs = doctype.__js || (doctype.client_script_core + doctype.client_script);
  636. if(cs) {
  637. try {
  638. var tmp = eval(cs);
  639. } catch(e) {
  640. console.log(e);
  641. }
  642. }
  643. // css
  644. if(doctype.__css) set_style(doctype.__css)
  645. // ---Client String----
  646. if(doctype.client_string) { // split client string
  647. this.cstring = {};
  648. var elist = doctype.client_string.split('---');
  649. for(var i=1;i<elist.length;i=i+2) {
  650. this.cstring[strip(elist[i])] = elist[i+1];
  651. }
  652. }
  653. }
  654. return ret;
  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) return;
  802. cur_frm.doc.__unsaved = 1;
  803. var frm_head;
  804. if(cur_frm.frm_head) {
  805. frm_head = cur_frm.frm_head;
  806. } else if(wn.container.page.frm && wn.container.page.frm.frm_head) {
  807. frm_head = wn.container.page.frm.frm_head
  808. }
  809. if(frm_head) frm_head.refresh_labels();
  810. }
  811. _f.Frm.prototype.show_comments = function() {
  812. if(!cur_frm.comments) {
  813. cur_frm.comments = new Dialog(540, 400, 'Comments');
  814. cur_frm.comments.comment_body = $a(cur_frm.comments.body, 'div', 'dialog_frm');
  815. $y(cur_frm.comments.body, {backgroundColor:'#EEE'});
  816. cur_frm.comments.list = new CommentList(cur_frm.comments.comment_body);
  817. }
  818. cur_frm.comments.list.dt = cur_frm.doctype;
  819. cur_frm.comments.list.dn = cur_frm.docname;
  820. cur_frm.comments.show();
  821. cur_frm.comments.list.run();
  822. }