Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

form.js 24 KiB

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