選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

form.js 26 KiB

13年前
13年前
13年前
13年前
13年前
13年前
13年前
13年前
13年前
13年前
12年前
12年前
12年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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. if(!this.intro_area) {
  249. this.intro_area = $('<div class="alert form-intro-area" style="margin-top: 20px;">')
  250. .insertBefore(this.page_layout.body.firstChild);
  251. }
  252. if(txt) {
  253. if(txt.search(/<p>/)==-1) txt = '<p>' + txt + '</p>';
  254. this.intro_area.html(txt);
  255. } else {
  256. this.intro_area.remove();
  257. this.intro_area = null;
  258. }
  259. }
  260. _f.Frm.prototype.set_footnote = function(txt) {
  261. if(!this.footnote_area) {
  262. this.footnote_area = $('<div class="alert form-intro-area">')
  263. .insertAfter(this.page_layout.body.lastChild);
  264. }
  265. if(txt) {
  266. if(txt.search(/<p>/)==-1) txt = '<p>' + txt + '</p>';
  267. this.footnote_area.html(txt);
  268. } else {
  269. this.footnote_area.remove();
  270. this.footnote_area = null;
  271. }
  272. }
  273. _f.Frm.prototype.setup_fields_std = function() {
  274. var fl = wn.meta.docfield_list[this.doctype];
  275. fl.sort(function(a,b) { return a.idx - b.idx});
  276. if(fl[0]&&fl[0].fieldtype!="Section Break" || get_url_arg('embed')) {
  277. this.layout.addrow(); // default section break
  278. if(fl[0].fieldtype!="Column Break") {// without column too
  279. var c = this.layout.addcell();
  280. $y(c.wrapper, {padding: '8px'});
  281. }
  282. }
  283. var sec;
  284. for(var i=0;i<fl.length;i++) {
  285. var f=fl[i];
  286. // if section break and next item
  287. // is a section break then ignore
  288. if(f.fieldtype=='Section Break' && fl[i+1] && fl[i+1].fieldtype=='Section Break')
  289. continue;
  290. var fn = f.fieldname?f.fieldname:f.label;
  291. var fld = make_field(f, this.doctype, this.layout.cur_cell, this);
  292. this.fields[this.fields.length] = fld;
  293. this.fields_dict[fn] = fld;
  294. if(sec && ['Section Break', 'Column Break'].indexOf(f.fieldtype)==-1) {
  295. fld.parent_section = sec;
  296. sec.fields.push(fld);
  297. }
  298. if(f.fieldtype=='Section Break') {
  299. sec = fld;
  300. this.sections.push(fld);
  301. }
  302. // default col-break after sec-break
  303. if((f.fieldtype=='Section Break')&&(fl[i+1])&&(fl[i+1].fieldtype!='Column Break')) {
  304. var c = this.layout.addcell();
  305. $y(c.wrapper, {padding: '8px'});
  306. }
  307. }
  308. }
  309. _f.Frm.prototype.add_custom_button = function(label, fn, icon) {
  310. this.frm_head.appframe.add_button(label, fn, icon);
  311. }
  312. _f.Frm.prototype.clear_custom_buttons = function() {
  313. this.frm_head.refresh_toolbar()
  314. }
  315. _f.Frm.prototype.add_fetch = function(link_field, src_field, tar_field) {
  316. if(!this.fetch_dict[link_field]) {
  317. this.fetch_dict[link_field] = {'columns':[], 'fields':[]}
  318. }
  319. this.fetch_dict[link_field].columns.push(src_field);
  320. this.fetch_dict[link_field].fields.push(tar_field);
  321. }
  322. _f.Frm.prototype.setup_client_script = function() {
  323. // setup client obj
  324. if(this.meta.client_script_core || this.meta.client_script || this.meta.__js) {
  325. this.runclientscript('setup', this.doctype, this.docname);
  326. }
  327. }
  328. _f.Frm.prototype.refresh_print_layout = function() {
  329. $ds(this.print_wrapper);
  330. $dh(this.page_layout.wrapper);
  331. var me = this;
  332. var print_callback = function(print_html) {
  333. me.print_body.innerHTML = print_html;
  334. }
  335. // print head
  336. if(cur_frm.doc.select_print_heading)
  337. cur_frm.set_print_heading(cur_frm.doc.select_print_heading)
  338. if(user!='Guest') {
  339. $di(this.view_btn_wrapper);
  340. // archive
  341. if(cur_frm.doc.__archived) {
  342. $dh(this.view_btn_wrapper);
  343. }
  344. } else {
  345. $dh(this.view_btn_wrapper);
  346. $dh(this.print_close_btn);
  347. }
  348. // create print format here
  349. _p.build(this.$print_view_select.val(), print_callback, null, 1);
  350. }
  351. _f.Frm.prototype.show_the_frm = function() {
  352. // show the dialog
  353. if(this.meta.in_dialog && !this.parent.dialog.display) {
  354. if(!this.meta.istable)
  355. this.parent.table_form = false;
  356. this.parent.dialog.show();
  357. }
  358. }
  359. _f.Frm.prototype.set_print_heading = function(txt) {
  360. this.pformat[cur_frm.docname] = txt;
  361. }
  362. _f.Frm.prototype.defocus_rest = function() {
  363. // deselect others
  364. if(_f.cur_grid_cell) _f.cur_grid_cell.grid.cell_deselect();
  365. }
  366. _f.Frm.prototype.refresh_header = function() {
  367. // set title
  368. // main title
  369. if(!this.meta.in_dialog || this.in_form) {
  370. set_title(this.meta.issingle ? this.doctype : this.docname);
  371. }
  372. if(wn.ui.toolbar.recent)
  373. wn.ui.toolbar.recent.add(this.doctype, this.docname, 1);
  374. // show / hide buttons
  375. if(this.frm_head)this.frm_head.refresh();
  376. }
  377. _f.Frm.prototype.check_doc_perm = function() {
  378. // get perm
  379. var dt = this.parent_doctype?this.parent_doctype : this.doctype;
  380. var dn = this.parent_docname?this.parent_docname : this.docname;
  381. this.perm = wn.perm.get_perm(dt, dn);
  382. this.orig_perm = wn.perm.get_perm(dt, dn, 1);
  383. if(!this.perm[0][READ]) {
  384. if(user=='Guest') {
  385. // allow temp access? via encryted akey
  386. if(_f.temp_access[dt] && _f.temp_access[dt][dn]) {
  387. this.perm = [[1,0,0]]
  388. return 1;
  389. }
  390. }
  391. window.history.back();
  392. return 0;
  393. }
  394. return 1
  395. }
  396. _f.Frm.prototype.refresh = function(docname) {
  397. // record switch
  398. if(docname) {
  399. if(this.docname != docname && (!this.meta.in_dialog || this.in_form) &&
  400. !this.meta.istable)
  401. scroll(0, 0);
  402. this.docname = docname;
  403. }
  404. if(!this.meta.istable) {
  405. cur_frm = this;
  406. this.parent.cur_frm = this;
  407. }
  408. if(this.docname) { // document to show
  409. // check permissions
  410. if(!this.check_doc_perm()) return;
  411. // set the doc
  412. this.doc = wn.model.get_doc(this.doctype, this.docname);
  413. // check if doctype is already open
  414. if (!this.opendocs[this.docname]) {
  415. this.check_doctype_conflict(this.docname);
  416. } else {
  417. if(this.doc && this.doc.__last_sync_on &&
  418. (new Date() - this.doc.__last_sync_on) / 1000 > this.refresh_if_stale_for) {
  419. this.reload_doc();
  420. return;
  421. }
  422. }
  423. // do setup
  424. if(!this.setup_done) this.setup();
  425. // set customized permissions for this record
  426. this.runclientscript('set_perm',this.doctype, this.docname);
  427. // load the record for the first time, if not loaded (call 'onload')
  428. cur_frm.cscript.is_onload = false;
  429. if(!this.opendocs[this.docname]) {
  430. cur_frm.cscript.is_onload = true;
  431. this.setnewdoc(this.docname);
  432. }
  433. // editable
  434. if(this.doc.__islocal)
  435. this.is_editable[this.docname] = 1; // new is editable
  436. this.editable = this.is_editable[this.docname];
  437. if(this.editable || (!this.editable && this.meta.istable)) {
  438. // show form layout (with fields etc)
  439. // ----------------------------------
  440. if(this.print_wrapper) {
  441. $dh(this.print_wrapper);
  442. $ds(this.page_layout.wrapper);
  443. }
  444. // header
  445. if(!this.meta.istable) {
  446. this.refresh_header();
  447. this.sidebar && this.sidebar.refresh();
  448. }
  449. // call trigger
  450. this.runclientscript('refresh');
  451. // trigger global trigger
  452. // to use this
  453. $(document).trigger('form_refresh');
  454. // fields
  455. this.refresh_fields();
  456. // dependent fields
  457. this.refresh_dependency();
  458. // footer
  459. this.refresh_footer();
  460. // layout
  461. if(this.layout) this.layout.show();
  462. // call onload post render for callbacks to be fired
  463. if(this.cscript.is_onload) {
  464. this.runclientscript('onload_post_render', this.doctype, this.docname);
  465. }
  466. // focus on first input
  467. if(this.doc.docstatus==0) {
  468. $(this.wrapper).find('.form-layout-row :input:first').focus();
  469. }
  470. } else {
  471. // show print layout
  472. // ----------------------------------
  473. this.refresh_header();
  474. if(this.print_wrapper) {
  475. this.refresh_print_layout();
  476. }
  477. this.runclientscript('edit_status_changed');
  478. }
  479. $(cur_frm.wrapper).trigger('render_complete');
  480. }
  481. }
  482. _f.Frm.prototype.refresh_footer = function() {
  483. var f = this.page_layout.footer;
  484. if(f.save_area) {
  485. if(this.editable && (!this.meta.in_dialog || this.in_form)
  486. && this.doc.docstatus==0 && !this.meta.istable && this.perm[0][WRITE]
  487. && (this.fields && this.fields.length > 7) && !this.save_disabled) {
  488. f.show_save();
  489. } else {
  490. f.hide_save();
  491. }
  492. }
  493. }
  494. _f.Frm.prototype.refresh_field = function(fname) {
  495. cur_frm.fields_dict[fname] && cur_frm.fields_dict[fname].refresh
  496. && cur_frm.fields_dict[fname].refresh();
  497. }
  498. _f.Frm.prototype.refresh_fields = function() {
  499. // refresh fields
  500. for(var i=0; i<this.fields.length; i++) {
  501. var f = this.fields[i];
  502. f.perm = this.perm;
  503. f.docname = this.docname;
  504. // if field is identifiable (not blank section or column break)
  505. // get the "customizable" parameters for this record
  506. var fn = f.df.fieldname || f.df.label;
  507. if(fn)
  508. f.df = wn.meta.get_docfield(this.doctype, fn, this.docname);
  509. if(f.df.fieldtype!='Section Break' && f.refresh) {
  510. f.refresh();
  511. }
  512. }
  513. // refresh sections
  514. $.each(this.sections, function(i, f) {
  515. f.refresh(true);
  516. })
  517. // cleanup activities after refresh
  518. this.cleanup_refresh(this);
  519. }
  520. _f.Frm.prototype.cleanup_refresh = function() {
  521. var me = this;
  522. if(me.fields_dict['amended_from']) {
  523. if (me.doc.amended_from) {
  524. unhide_field('amended_from');
  525. if (me.fields_dict['amendment_date']) unhide_field('amendment_date');
  526. } else {
  527. hide_field('amended_from');
  528. if (me.fields_dict['amendment_date']) hide_field('amendment_date');
  529. }
  530. }
  531. if(me.fields_dict['trash_reason']) {
  532. if(me.doc.trash_reason && me.doc.docstatus == 2) {
  533. unhide_field('trash_reason');
  534. } else {
  535. hide_field('trash_reason');
  536. }
  537. }
  538. if(me.meta.autoname && me.meta.autoname.substr(0,6)=='field:' && !me.doc.__islocal) {
  539. var fn = me.meta.autoname.substr(6);
  540. cur_frm.toggle_display(fn, false);
  541. }
  542. }
  543. // Resolve "depends_on" and show / hide accordingly
  544. _f.Frm.prototype.refresh_dependency = function() {
  545. var me = this;
  546. var doc = locals[this.doctype][this.docname];
  547. // build dependants' dictionary
  548. var has_dep = false;
  549. for(fkey in me.fields) {
  550. var f = me.fields[fkey];
  551. f.dependencies_clear = true;
  552. if(f.df.depends_on) {
  553. has_dep = true;
  554. }
  555. }
  556. if(!has_dep)return;
  557. // show / hide based on values
  558. for(var i=me.fields.length-1;i>=0;i--) {
  559. var f = me.fields[i];
  560. f.guardian_has_value = true;
  561. if(f.df.depends_on) {
  562. // evaluate guardian
  563. var v = doc[f.df.depends_on];
  564. if(f.df.depends_on.substr(0,5)=='eval:') {
  565. f.guardian_has_value = eval(f.df.depends_on.substr(5));
  566. } else if(f.df.depends_on.substr(0,3)=='fn:') {
  567. f.guardian_has_value = me.runclientscript(f.df.depends_on.substr(3), me.doctype, me.docname);
  568. } else {
  569. if(!v) {
  570. f.guardian_has_value = false;
  571. }
  572. }
  573. // show / hide
  574. if(f.guardian_has_value) {
  575. f.df.hidden = 0;
  576. f.refresh();
  577. } else {
  578. f.df.hidden = 1;
  579. f.refresh();
  580. }
  581. }
  582. }
  583. }
  584. // setnewdoc is called when a record is loaded for the first time
  585. // ======================================================================================
  586. _f.Frm.prototype.setnewdoc = function(docname) {
  587. // moved this call to refresh function
  588. // this.check_doctype_conflict(docname);
  589. // if loaded
  590. if(this.opendocs[docname]) { // already exists
  591. this.docname=docname;
  592. return;
  593. }
  594. // make a copy of the doctype for client script settings
  595. // each record will have its own client script
  596. wn.meta.make_docfield_copy_for(this.doctype,docname);
  597. this.docname = docname;
  598. var me = this;
  599. var viewname = this.meta.issingle ? this.doctype : docname;
  600. // Client Script
  601. this.runclientscript('onload', this.doctype, this.docname);
  602. this.is_editable[docname] = 1;
  603. if(cint(this.meta.read_only_onload)) this.is_editable[docname] = 0;
  604. this.opendocs[docname] = true;
  605. }
  606. _f.Frm.prototype.edit_doc = function() {
  607. // set fields
  608. this.is_editable[this.docname] = true;
  609. this.refresh();
  610. }
  611. _f.Frm.prototype.show_doc = function(dn) {
  612. this.refresh(dn);
  613. }
  614. _f.Frm.prototype.runscript = function(scriptname, callingfield, onrefresh) {
  615. var me = this;
  616. if(this.docname) {
  617. // make doc list
  618. var doclist = wn.model.compress(make_doclist(this.doctype, this.docname));
  619. // send to run
  620. if(callingfield)
  621. $(callingfield.input).set_working();
  622. $c('runserverobj', {'docs':doclist, 'method':scriptname },
  623. function(r, rtxt) {
  624. // run refresh
  625. if(onrefresh)
  626. onrefresh(r,rtxt);
  627. // fields
  628. me.refresh_fields();
  629. // dependent fields
  630. me.refresh_dependency();
  631. // enable button
  632. if(callingfield)
  633. $(callingfield.input).done_working();
  634. }
  635. );
  636. }
  637. }
  638. _f.Frm.prototype.runclientscript = function(caller, cdt, cdn) {
  639. if(!cdt)cdt = this.doctype;
  640. if(!cdn)cdn = this.docname;
  641. var ret = null;
  642. var doc = locals[cur_frm.doc.doctype][cur_frm.doc.name];
  643. try {
  644. if(this.cscript[caller])
  645. ret = this.cscript[caller](doc, cdt, cdn);
  646. if(this.cscript['custom_'+caller])
  647. ret += this.cscript['custom_'+caller](doc, cdt, cdn);
  648. } catch(e) {
  649. validated = false;
  650. console.log(e);
  651. }
  652. if(caller && caller.toLowerCase()=='setup') {
  653. var doctype = wn.model.get_doc('DocType', this.doctype);
  654. // js
  655. var cs = doctype.__js || (doctype.client_script_core + doctype.client_script);
  656. if(cs) {
  657. try {
  658. var tmp = eval(cs);
  659. } catch(e) {
  660. console.log(e);
  661. }
  662. }
  663. // css
  664. if(doctype.__css) set_style(doctype.__css)
  665. // ---Client String----
  666. if(doctype.client_string) { // split client string
  667. this.cstring = {};
  668. var elist = doctype.client_string.split('---');
  669. for(var i=1;i<elist.length;i=i+2) {
  670. this.cstring[strip(elist[i])] = elist[i+1];
  671. }
  672. }
  673. }
  674. return ret;
  675. }
  676. _f.Frm.prototype.copy_doc = function(onload, from_amend) {
  677. if(!this.perm[0][CREATE]) {
  678. msgprint('You are not allowed to create '+this.meta.name);
  679. return;
  680. }
  681. var dn = this.docname;
  682. // copy parent
  683. var newdoc = wn.model.copy_doc(this.doctype, dn, from_amend);
  684. // do not copy attachments
  685. if(this.meta.allow_attach && newdoc.file_list && !from_amend)
  686. newdoc.file_list = null;
  687. // copy chidren
  688. var dl = make_doclist(this.doctype, dn);
  689. // table fields dict - for no_copy check
  690. var tf_dict = {};
  691. for(var d in dl) {
  692. d1 = dl[d];
  693. // get tabel field
  694. if(d1.parentfield && !tf_dict[d1.parentfield]) {
  695. tf_dict[d1.parentfield] = wn.meta.get_docfield(d1.parenttype, d1.parentfield);
  696. }
  697. if(d1.parent==dn && cint(tf_dict[d1.parentfield].no_copy)!=1) {
  698. var ch = wn.model.copy_doc(d1.doctype, d1.name, from_amend);
  699. ch.parent = newdoc.name;
  700. ch.docstatus = 0;
  701. ch.owner = user;
  702. ch.creation = '';
  703. ch.modified_by = user;
  704. ch.modified = '';
  705. }
  706. }
  707. newdoc.__islocal = 1;
  708. newdoc.docstatus = 0;
  709. newdoc.owner = user;
  710. newdoc.creation = '';
  711. newdoc.modified_by = user;
  712. newdoc.modified = '';
  713. if(onload)onload(newdoc);
  714. loaddoc(newdoc.doctype, newdoc.name);
  715. }
  716. _f.Frm.prototype.reload_doc = function() {
  717. this.check_doctype_conflict(this.docname);
  718. var me = this;
  719. var onsave = function(r, rtxt) {
  720. // n tweets and last comment
  721. me.runclientscript('setup', me.doctype, me.docname);
  722. me.refresh();
  723. }
  724. if(me.doc.__islocal) {
  725. // reload only doctype
  726. $c('webnotes.widgets.form.load.getdoctype', {'doctype':me.doctype }, onsave, null, null, 'Refreshing ' + me.doctype + '...');
  727. } else {
  728. // reload doc and docytpe
  729. $c('webnotes.widgets.form.load.getdoc', {'name':me.docname, 'doctype':me.doctype, 'getdoctype':1, 'user':user}, onsave, null, null, 'Refreshing ' + me.docname + '...');
  730. }
  731. }
  732. var validated;
  733. _f.Frm.prototype.save = function(save_action, callback, btn) {
  734. $(document.activeElement).blur();
  735. var me = this;
  736. var doclist = new wn.model.DocList(this.doctype, this.docname);
  737. // validate
  738. if(save_action!="Cancel") {
  739. validated = true;
  740. this.runclientscript('validate');
  741. if(!validated) {
  742. return;
  743. }
  744. }
  745. doclist.save(save_action || "Save", function(r) {
  746. if(!r.exc) {
  747. me.refresh();
  748. if(save_action==="Save") {
  749. me.runclientscript("after_save", me.doctype, me.docname);
  750. }
  751. }
  752. callback && callback(r);
  753. }, btn);
  754. }
  755. _f.Frm.prototype.savesubmit = function(btn) {
  756. var me = this;
  757. wn.confirm("Permanently Submit "+this.docname+"?", function() {
  758. me.save('Submit', function(r) {
  759. if(!r.exc) {
  760. me.runclientscript('on_submit', me.doctype, me.docname);
  761. }
  762. }, btn);
  763. });
  764. }
  765. _f.Frm.prototype.savecancel = function(btn) {
  766. var me = this;
  767. wn.confirm("Permanently Cancel "+this.docname+"?", function() {
  768. me.runclientscript("before_cancel", me.doctype, me.docname);
  769. var doclist = new wn.model.DocList(me.doctype, me.docname);
  770. doclist.cancel(function(r) {
  771. if(!r.exc) {
  772. me.refresh();
  773. me.runclientscript("after_cancel", me.doctype, me.docname);
  774. }
  775. }, btn);
  776. });
  777. }
  778. // delete the record
  779. _f.Frm.prototype.savetrash = function() {
  780. wn.model.delete_doc(this.doctype, this.docname, function(r) {
  781. window.history.back();
  782. })
  783. }
  784. _f.Frm.prototype.amend_doc = function() {
  785. if(!this.fields_dict['amended_from']) {
  786. alert('"amended_from" field must be present to do an amendment.');
  787. return;
  788. }
  789. var me = this;
  790. var fn = function(newdoc) {
  791. newdoc.amended_from = me.docname;
  792. if(me.fields_dict && me.fields_dict['amendment_date'])
  793. newdoc.amendment_date = dateutil.obj_to_str(new Date());
  794. }
  795. this.copy_doc(fn, 1);
  796. }
  797. _f.Frm.prototype.disable_save = function() {
  798. // IMPORTANT: this function should be called in refresh event
  799. cur_frm.save_disabled = true;
  800. cur_frm.page_layout.footer.hide_save();
  801. cur_frm.frm_head.appframe.buttons.Save.remove();
  802. }
  803. _f.get_value = function(dt, dn, fn) {
  804. if(locals[dt] && locals[dt][dn])
  805. return locals[dt][dn][fn];
  806. }
  807. _f.Frm.prototype.set_value_in_locals = function(dt, dn, fn, v) {
  808. var d = locals[dt][dn];
  809. if (!d) return;
  810. var changed = d[fn] != v;
  811. if(changed && (d[fn]==null || v==null) && (cstr(d[fn])==cstr(v)))
  812. changed = false;
  813. if(changed) {
  814. d[fn] = v;
  815. if(d.parenttype)
  816. d.__unsaved = 1;
  817. this.set_unsaved();
  818. }
  819. }
  820. _f.Frm.prototype.set_unsaved = function() {
  821. if(cur_frm.doc.__unsaved) return;
  822. cur_frm.doc.__unsaved = 1;
  823. var frm_head;
  824. if(cur_frm.frm_head) {
  825. frm_head = cur_frm.frm_head;
  826. } else if(wn.container.page.frm && wn.container.page.frm.frm_head) {
  827. frm_head = wn.container.page.frm.frm_head
  828. }
  829. if(frm_head) frm_head.refresh_labels();
  830. }
  831. _f.Frm.prototype.show_comments = function() {
  832. if(!cur_frm.comments) {
  833. cur_frm.comments = new Dialog(540, 400, 'Comments');
  834. cur_frm.comments.comment_body = $a(cur_frm.comments.body, 'div', 'dialog_frm');
  835. $y(cur_frm.comments.body, {backgroundColor:'#EEE'});
  836. cur_frm.comments.list = new CommentList(cur_frm.comments.comment_body);
  837. }
  838. cur_frm.comments.list.dt = cur_frm.doctype;
  839. cur_frm.comments.list.dn = cur_frm.docname;
  840. cur_frm.comments.show();
  841. cur_frm.comments.list.run();
  842. }