You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

form.js 22 KiB

13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
12 years ago
12 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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.sidebar
  32. + this.print_wrapper
  33. + this.head
  34. + this.footer
  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.script_manager = new wn.ui.form.ScriptManager({
  109. frm: this
  110. })
  111. this.watch_model_updates();
  112. this.setup_header();
  113. this.footer = new wn.ui.form.Footer({
  114. frm: this,
  115. parent: this.layout_main
  116. })
  117. this.setup_done = true;
  118. }
  119. _f.Frm.prototype.watch_model_updates = function() {
  120. // watch model updates
  121. var me = this;
  122. // on main doc
  123. wn.model.on(me.doctype, "*", function(fieldname, value, doc) {
  124. // set input
  125. if(doc.name===me.docname) {
  126. me.fields_dict[fieldname]
  127. && me.fields_dict[fieldname].set_input(value);
  128. me.script_manager.trigger(fieldname, doc.doctype, doc.name);
  129. }
  130. })
  131. // on table fields
  132. $.each(wn.model.get("DocField", {fieldtype:"Table", parent: me.doctype}), function(i, df) {
  133. wn.model.on(df.options, "*", function(fieldname, value, doc) {
  134. if(doc.parent===me.docname) {
  135. me.fields_dict[df.fieldname].grid.set_value(fieldname, value, doc);
  136. me.script_manager.trigger(fieldname, doc.doctype, doc.name);
  137. }
  138. })
  139. })
  140. }
  141. _f.Frm.prototype.setup_print_layout = function() {
  142. var me = this;
  143. this.print_wrapper = $('<div>\
  144. <form class="form-inline" style="margin-bottom: 10px;">\
  145. <select class="col-span-2 preview-select"></select> \
  146. <button class="btn btn-edit btn-info"><i class="icon-edit"></i> Edit</button>\
  147. </form>\
  148. <div class="print-format-area clear-fix" style="min-height: 400px;"></div>\
  149. </div>').appendTo(this.layout_main).get(0);
  150. $(this.print_wrapper).find(".btn-edit").click(function() {
  151. me.edit_doc();
  152. return false;
  153. })
  154. this.$print_view_select = $(this.print_wrapper).find(".preview-select")
  155. .add_options(this.print_formats)
  156. .val(this.print_formats[0])
  157. .change(function() {
  158. me.refresh_print_layout();
  159. })
  160. //appframe.add_ripped_paper_effect(this.print_wrapper);
  161. this.print_body = $(this.print_wrapper).find(".print-format-area").get(0);
  162. }
  163. _f.Frm.prototype.onhide = function() {
  164. if(_f.cur_grid_cell) _f.cur_grid_cell.grid.cell_deselect();
  165. }
  166. _f.Frm.prototype.setup_std_layout = function() {
  167. this.form_wrapper = $('<div></div>').appendTo(this.layout_main).get(0);
  168. $parent = $(this.form_wrapper);
  169. this.head = $parent.find(".layout-appframe").get(0);
  170. this.main = this.form_wrapper;
  171. this.body_header = $a(this.main, 'div');
  172. this.body = $a(this.main, 'div');
  173. if(this.heading) {
  174. this.page_head = new PageHeader(this.head, this);
  175. }
  176. // only tray
  177. this.meta.section_style='Simple'; // always simple!
  178. // layout
  179. this.layout = new wn.ui.form.Layout({
  180. parent: this.body,
  181. doctype: this.doctype,
  182. frm: this,
  183. })
  184. // state
  185. this.states = new wn.ui.form.States({
  186. frm: this
  187. });
  188. }
  189. _f.Frm.prototype.setup_header = function() {
  190. // header - no headers for tables and guests
  191. if(!(this.meta.istable || (this.meta.in_dialog && !this.in_form)))
  192. this.frm_head = new _f.FrmHeader(this.head, this);
  193. this.toolbar = new wn.ui.form.Toolbar({
  194. frm: this,
  195. appframe: this.appframe
  196. })
  197. }
  198. _f.Frm.prototype.setup_print = function() {
  199. this.print_formats = wn.meta.get_print_formats(this.meta.name);
  200. this.print_sel = $a(null, 'select', '', {width:'160px'});
  201. add_sel_options(this.print_sel, this.print_formats);
  202. this.print_sel.value = this.print_formats[0];
  203. }
  204. _f.Frm.prototype.print_doc = function() {
  205. if(this.doc.docstatus==2) {
  206. msgprint("Cannot Print Cancelled Documents.");
  207. return;
  208. }
  209. _p.show_dialog(); // multiple options
  210. }
  211. // email the form
  212. _f.Frm.prototype.email_doc = function(message) {
  213. new wn.views.CommunicationComposer({
  214. doc: this.doc,
  215. subject: wn._(this.meta.name) + ': ' + this.docname,
  216. recipients: this.doc.email || this.doc.email_id || this.doc.contact_email,
  217. attach_document_print: true,
  218. message: message,
  219. real_name: this.doc.real_name || this.doc.contact_display || this.doc.contact_name
  220. });
  221. }
  222. // email the form
  223. _f.Frm.prototype.rename_doc = function() {
  224. wn.model.rename_doc(this.doctype, this.docname);
  225. }
  226. // notify this form of renamed records
  227. _f.Frm.prototype.rename_notify = function(dt, old, name) {
  228. // from form
  229. if(this.meta.istable)
  230. return;
  231. if(this.docname == old)
  232. this.docname = name;
  233. else
  234. return;
  235. // view_is_edit
  236. this.last_view_is_edit[name] = this.last_view_is_edit[old];
  237. delete this.last_view_is_edit[old];
  238. // cleanup
  239. if(this && this.opendocs[old]) {
  240. // delete docfield copy
  241. wn.meta.docfield_copy[dt][name] = wn.meta.docfield_copy[dt][old];
  242. delete wn.meta.docfield_copy[dt][old];
  243. }
  244. delete this.opendocs[old];
  245. this.opendocs[name] = true;
  246. if(this.meta.in_dialog || !this.in_form) {
  247. return;
  248. }
  249. wn.re_route[window.location.hash] = '#Form/' + encodeURIComponent(this.doctype) + '/' + encodeURIComponent(name);
  250. wn.set_route('Form', this.doctype, name);
  251. }
  252. // SETUP
  253. _f.Frm.prototype.setup_meta = function(doctype) {
  254. this.meta = wn.model.get_doc('DocType',this.doctype);
  255. this.perm = wn.perm.get_perm(this.doctype); // for create
  256. if(this.meta.istable) { this.meta.in_dialog = 1 }
  257. this.setup_print();
  258. }
  259. _f.Frm.prototype.set_intro = function(txt) {
  260. wn.utils.set_intro(this, this.body, txt);
  261. }
  262. _f.Frm.prototype.set_footnote = function(txt) {
  263. wn.utils.set_footnote(this, this.body, txt);
  264. }
  265. _f.Frm.prototype.add_custom_button = function(label, fn, icon) {
  266. this.appframe.add_button(label, fn, icon, true);
  267. }
  268. _f.Frm.prototype.clear_custom_buttons = function() {
  269. this.toolbar.refresh()
  270. }
  271. _f.Frm.prototype.add_fetch = function(link_field, src_field, tar_field) {
  272. if(!this.fetch_dict[link_field]) {
  273. this.fetch_dict[link_field] = {'columns':[], 'fields':[]}
  274. }
  275. this.fetch_dict[link_field].columns.push(src_field);
  276. this.fetch_dict[link_field].fields.push(tar_field);
  277. }
  278. _f.Frm.prototype.refresh_print_layout = function() {
  279. $ds(this.print_wrapper);
  280. $dh(this.form_wrapper);
  281. var me = this;
  282. var print_callback = function(print_html) {
  283. me.print_body.innerHTML = print_html;
  284. }
  285. // print head
  286. if(cur_frm.doc.select_print_heading)
  287. cur_frm.set_print_heading(cur_frm.doc.select_print_heading)
  288. if(user!='Guest') {
  289. $di(this.view_btn_wrapper);
  290. // archive
  291. if(cur_frm.doc.__archived) {
  292. $dh(this.view_btn_wrapper);
  293. }
  294. } else {
  295. $dh(this.view_btn_wrapper);
  296. $dh(this.print_close_btn);
  297. }
  298. // create print format here
  299. _p.build(this.$print_view_select.val(), print_callback, null, 1);
  300. }
  301. _f.Frm.prototype.show_the_frm = function() {
  302. // show the dialog
  303. if(this.meta.in_dialog && !this.parent.dialog.display) {
  304. if(!this.meta.istable)
  305. this.parent.table_form = false;
  306. this.parent.dialog.show();
  307. }
  308. }
  309. _f.Frm.prototype.set_print_heading = function(txt) {
  310. this.pformat[cur_frm.docname] = txt;
  311. }
  312. _f.Frm.prototype.defocus_rest = function() {
  313. // deselect others
  314. if(_f.cur_grid_cell) _f.cur_grid_cell.grid.cell_deselect();
  315. }
  316. _f.Frm.prototype.refresh_header = function() {
  317. // set title
  318. // main title
  319. if(!this.meta.in_dialog || this.in_form) {
  320. set_title(this.meta.issingle ? this.doctype : this.docname);
  321. }
  322. if(wn.ui.toolbar.recent)
  323. wn.ui.toolbar.recent.add(this.doctype, this.docname, 1);
  324. // show / hide buttons
  325. if(this.frm_head) {
  326. this.frm_head.refresh();
  327. this.toolbar.refresh();
  328. }
  329. }
  330. _f.Frm.prototype.check_doc_perm = function() {
  331. // get perm
  332. var dt = this.parent_doctype?this.parent_doctype : this.doctype;
  333. var dn = this.parent_docname?this.parent_docname : this.docname;
  334. this.perm = wn.perm.get_perm(dt, dn);
  335. if(!this.perm[0][READ]) {
  336. wn.set_route("403");
  337. return 0;
  338. }
  339. return 1
  340. }
  341. _f.Frm.prototype.refresh = function(docname) {
  342. // record switch
  343. if(docname) {
  344. if(this.docname != docname && (!this.meta.in_dialog || this.in_form) &&
  345. !this.meta.istable)
  346. scroll(0, 0);
  347. this.docname = docname;
  348. }
  349. if(!this.meta.istable) {
  350. cur_frm = this;
  351. this.parent.cur_frm = this;
  352. }
  353. if(this.docname) { // document to show
  354. // check permissions
  355. if(!this.check_doc_perm()) return;
  356. // read only (workflow)
  357. this.read_only = wn.workflow.is_read_only(this.doctype, this.docname);
  358. // set the doc
  359. this.doc = wn.model.get_doc(this.doctype, this.docname);
  360. // check if doctype is already open
  361. if (!this.opendocs[this.docname]) {
  362. this.check_doctype_conflict(this.docname);
  363. } else {
  364. if(this.doc && this.doc.__last_sync_on &&
  365. (new Date() - this.doc.__last_sync_on) > (this.refresh_if_stale_for * 1000)) {
  366. this.reload_doc();
  367. return;
  368. }
  369. }
  370. // do setup
  371. if(!this.setup_done) this.setup();
  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.script_manager.trigger("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. // call onload post render for callbacks to be fired
  401. if(this.cscript.is_onload) {
  402. this.script_manager.trigger("onload_post_render");
  403. }
  404. // focus on first input
  405. if(this.doc.docstatus==0) {
  406. var first = $(this.form_wrapper).find('.form-layout-row :input:first');
  407. if(!in_list(["Date", "Datetime"], first.attr("data-fieldtype"))) {
  408. first.focus();
  409. }
  410. }
  411. } else {
  412. this.refresh_header();
  413. if(this.print_wrapper) {
  414. this.refresh_print_layout();
  415. }
  416. }
  417. $(cur_frm.wrapper).trigger('render_complete');
  418. }
  419. }
  420. _f.Frm.prototype.refresh_field = function(fname) {
  421. cur_frm.fields_dict[fname] && cur_frm.fields_dict[fname].refresh
  422. && cur_frm.fields_dict[fname].refresh();
  423. }
  424. _f.Frm.prototype.refresh_fields = function() {
  425. this.layout.refresh();
  426. // cleanup activities after refresh
  427. this.cleanup_refresh(this);
  428. }
  429. _f.Frm.prototype.cleanup_refresh = function() {
  430. var me = this;
  431. if(me.fields_dict['amended_from']) {
  432. if (me.doc.amended_from) {
  433. unhide_field('amended_from');
  434. if (me.fields_dict['amendment_date']) unhide_field('amendment_date');
  435. } else {
  436. hide_field('amended_from');
  437. if (me.fields_dict['amendment_date']) hide_field('amendment_date');
  438. }
  439. }
  440. if(me.fields_dict['trash_reason']) {
  441. if(me.doc.trash_reason && me.doc.docstatus == 2) {
  442. unhide_field('trash_reason');
  443. } else {
  444. hide_field('trash_reason');
  445. }
  446. }
  447. if(me.meta.autoname && me.meta.autoname.substr(0,6)=='field:' && !me.doc.__islocal) {
  448. var fn = me.meta.autoname.substr(6);
  449. cur_frm.toggle_display(fn, false);
  450. }
  451. if(me.meta.autoname=="naming_series:" && !me.doc.__islocal) {
  452. cur_frm.toggle_display("naming_series", false);
  453. }
  454. }
  455. // Resolve "depends_on" and show / hide accordingly
  456. _f.Frm.prototype.refresh_dependency = function() {
  457. var me = this;
  458. var doc = locals[this.doctype][this.docname];
  459. // build dependants' dictionary
  460. var has_dep = false;
  461. for(fkey in me.fields) {
  462. var f = me.fields[fkey];
  463. f.dependencies_clear = true;
  464. if(f.df.depends_on) {
  465. has_dep = true;
  466. }
  467. }
  468. if(!has_dep)return;
  469. // show / hide based on values
  470. for(var i=me.fields.length-1;i>=0;i--) {
  471. var f = me.fields[i];
  472. f.guardian_has_value = true;
  473. if(f.df.depends_on) {
  474. // evaluate guardian
  475. var v = doc[f.df.depends_on];
  476. if(f.df.depends_on.substr(0,5)=='eval:') {
  477. f.guardian_has_value = eval(f.df.depends_on.substr(5));
  478. } else if(f.df.depends_on.substr(0,3)=='fn:') {
  479. f.guardian_has_value = me.script_manager.trigger(f.df.depends_on.substr(3), me.doctype, me.docname);
  480. } else {
  481. if(!v) {
  482. f.guardian_has_value = false;
  483. }
  484. }
  485. // show / hide
  486. if(f.guardian_has_value) {
  487. f.df.hidden = 0;
  488. f.refresh();
  489. } else {
  490. f.df.hidden = 1;
  491. f.refresh();
  492. }
  493. }
  494. }
  495. }
  496. // setnewdoc is called when a record is loaded for the first time
  497. // ======================================================================================
  498. _f.Frm.prototype.setnewdoc = function(docname) {
  499. // moved this call to refresh function
  500. // this.check_doctype_conflict(docname);
  501. // if loaded
  502. if(this.opendocs[docname]) { // already exists
  503. this.docname=docname;
  504. return;
  505. }
  506. // make a copy of the doctype for client script settings
  507. // each record will have its own client script
  508. wn.meta.make_docfield_copy_for(this.doctype,docname);
  509. this.docname = docname;
  510. var me = this;
  511. var viewname = this.meta.issingle ? this.doctype : docname;
  512. // Client Script
  513. this.script_manager.trigger("onload");
  514. this.last_view_is_edit[docname] = 1;
  515. if(cint(this.meta.read_only_onload)) this.last_view_is_edit[docname] = 0;
  516. this.opendocs[docname] = true;
  517. }
  518. _f.Frm.prototype.edit_doc = function() {
  519. // set fields
  520. this.last_view_is_edit[this.docname] = true;
  521. this.refresh();
  522. }
  523. _f.Frm.prototype.show_doc = function(dn) {
  524. this.refresh(dn);
  525. }
  526. _f.Frm.prototype.runscript = function(scriptname, callingfield, onrefresh) {
  527. var me = this;
  528. if(this.docname) {
  529. // make doc list
  530. var doclist = wn.model.compress(make_doclist(this.doctype, this.docname));
  531. // send to run
  532. if(callingfield)
  533. $(callingfield.input).set_working();
  534. $c('runserverobj', {'docs':doclist, 'method':scriptname },
  535. function(r, rtxt) {
  536. // run refresh
  537. if(onrefresh)
  538. onrefresh(r,rtxt);
  539. // fields
  540. me.refresh_fields();
  541. // dependent fields
  542. me.refresh_dependency();
  543. // enable button
  544. if(callingfield)
  545. $(callingfield.input).done_working();
  546. }
  547. );
  548. }
  549. }
  550. _f.Frm.prototype.copy_doc = function(onload, from_amend) {
  551. if(!this.perm[0][CREATE]) {
  552. msgprint('You are not allowed to create '+this.meta.name);
  553. return;
  554. }
  555. var dn = this.docname;
  556. // copy parent
  557. var newdoc = wn.model.copy_doc(this.doctype, dn, from_amend);
  558. // do not copy attachments
  559. if(this.meta.allow_attach && newdoc.file_list && !from_amend)
  560. newdoc.file_list = null;
  561. // copy chidren
  562. var dl = make_doclist(this.doctype, dn);
  563. // table fields dict - for no_copy check
  564. var tf_dict = {};
  565. for(var d in dl) {
  566. d1 = dl[d];
  567. // get tabel field
  568. if(d1.parentfield && !tf_dict[d1.parentfield]) {
  569. tf_dict[d1.parentfield] = wn.meta.get_docfield(d1.parenttype, d1.parentfield);
  570. }
  571. if(d1.parent==dn && cint(tf_dict[d1.parentfield].no_copy)!=1) {
  572. var ch = wn.model.copy_doc(d1.doctype, d1.name, from_amend);
  573. ch.parent = newdoc.name;
  574. ch.docstatus = 0;
  575. ch.owner = user;
  576. ch.creation = '';
  577. ch.modified_by = user;
  578. ch.modified = '';
  579. }
  580. }
  581. newdoc.__islocal = 1;
  582. newdoc.docstatus = 0;
  583. newdoc.owner = user;
  584. newdoc.creation = '';
  585. newdoc.modified_by = user;
  586. newdoc.modified = '';
  587. if(onload)onload(newdoc);
  588. loaddoc(newdoc.doctype, newdoc.name);
  589. }
  590. _f.Frm.prototype.reload_doc = function() {
  591. this.check_doctype_conflict(this.docname);
  592. var me = this;
  593. var onsave = function(r, rtxt) {
  594. me.refresh();
  595. }
  596. if(!me.doc.__islocal) {
  597. wn.model.remove_from_locals(me.doctype, me.docname);
  598. wn.model.with_doc(me.doctype, me.docname, function() {
  599. me.refresh();
  600. })
  601. }
  602. }
  603. var validated;
  604. _f.Frm.prototype.save = function(save_action, callback, btn, on_error) {
  605. $(document.activeElement).blur();
  606. var me = this;
  607. if((!this.meta.in_dialog || this.in_form) && !this.meta.istable)
  608. scroll(0, 0);
  609. // validate
  610. if(save_action!="Cancel") {
  611. validated = true;
  612. this.script_manager.trigger("validate");
  613. if(!validated) {
  614. if(on_error)
  615. on_error();
  616. return;
  617. }
  618. }
  619. var doclist = new wn.model.DocList(this.doctype, this.docname);
  620. doclist.save(save_action || "Save", function(r) {
  621. if(!r.exc) {
  622. me.refresh();
  623. } else {
  624. if(on_error)
  625. on_error();
  626. }
  627. callback && callback(r);
  628. }, btn);
  629. }
  630. _f.Frm.prototype.savesubmit = function(btn, on_error) {
  631. var me = this;
  632. wn.confirm("Permanently Submit "+this.docname+"?", function() {
  633. me.save('Submit', function(r) {
  634. if(!r.exc) {
  635. me.script_manager.trigger("on_submit");
  636. }
  637. }, btn, on_error);
  638. });
  639. }
  640. _f.Frm.prototype.savecancel = function(btn, on_error) {
  641. var me = this;
  642. wn.confirm("Permanently Cancel "+this.docname+"?", function() {
  643. validated = true;
  644. me.script_manager.trigger("before_cancel");
  645. if(!validated) {
  646. if(on_error)
  647. on_error();
  648. return;
  649. }
  650. var doclist = new wn.model.DocList(me.doctype, me.docname);
  651. doclist.cancel(function(r) {
  652. if(!r.exc) {
  653. me.refresh();
  654. me.script_manager.trigger("after_cancel");
  655. }
  656. }, btn, on_error);
  657. });
  658. }
  659. // delete the record
  660. _f.Frm.prototype.savetrash = function() {
  661. wn.model.delete_doc(this.doctype, this.docname, function(r) {
  662. window.history.back();
  663. })
  664. }
  665. _f.Frm.prototype.amend_doc = function() {
  666. if(!this.fields_dict['amended_from']) {
  667. alert('"amended_from" field must be present to do an amendment.');
  668. return;
  669. }
  670. var me = this;
  671. var fn = function(newdoc) {
  672. newdoc.amended_from = me.docname;
  673. if(me.fields_dict && me.fields_dict['amendment_date'])
  674. newdoc.amendment_date = dateutil.obj_to_str(new Date());
  675. }
  676. this.copy_doc(fn, 1);
  677. }
  678. _f.Frm.prototype.disable_save = function() {
  679. // IMPORTANT: this function should be called in refresh event
  680. cur_frm.save_disabled = true;
  681. cur_frm.footer.hide_save();
  682. cur_frm.form_wrapper.appframe.buttons.Save.remove();
  683. delete cur_frm.form_wrapper.appframe.buttons.Save
  684. }
  685. _f.get_value = function(dt, dn, fn) {
  686. if(locals[dt] && locals[dt][dn])
  687. return locals[dt][dn][fn];
  688. }
  689. _f.Frm.prototype.set_value_in_locals = function(dt, dn, fn, v) {
  690. var d = locals[dt][dn];
  691. if (!d) return;
  692. var changed = d[fn] != v;
  693. if(changed && (d[fn]==null || v==null) && (cstr(d[fn])==cstr(v)))
  694. changed = false;
  695. if(changed) {
  696. d[fn] = v;
  697. this.dirty();
  698. }
  699. }
  700. _f.Frm.prototype.dirty = function() {
  701. this.doc.__unsaved = 1;
  702. $(this.wrapper).trigger('dirty')
  703. }
  704. _f.Frm.prototype.show_comments = function() {
  705. if(!cur_frm.comments) {
  706. cur_frm.comments = new Dialog(540, 400, 'Comments');
  707. cur_frm.comments.comment_body = $a(cur_frm.comments.body, 'div', 'dialog_frm');
  708. $y(cur_frm.comments.body, {backgroundColor:'#EEE'});
  709. cur_frm.comments.list = new CommentList(cur_frm.comments.comment_body);
  710. }
  711. cur_frm.comments.list.dt = cur_frm.doctype;
  712. cur_frm.comments.list.dn = cur_frm.docname;
  713. cur_frm.comments.show();
  714. cur_frm.comments.list.run();
  715. }