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.

13 年之前
13 年之前
13 年之前
13 年之前
13 年之前
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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. + this.saved_wrapper
  27. + wn.PageLayout (this.page_layout_layout)
  28. + this.wrapper
  29. + this.wtab (table)
  30. + this.main
  31. + this.head
  32. + this.body
  33. + this.layout
  34. + this.footer
  35. + this.sidebar
  36. + this.print_wrapper
  37. + this.head
  38. */
  39. wn.provide('_f');
  40. _f.frms = {};
  41. _f.Frm = function(doctype, parent) {
  42. this.docname = '';
  43. this.doctype = doctype;
  44. this.display = 0;
  45. var me = this;
  46. this.is_editable = {};
  47. this.opendocs = {};
  48. this.sections = [];
  49. this.grids = [];
  50. this.cscript = {};
  51. this.pformat = {};
  52. this.fetch_dict = {};
  53. this.parent = parent;
  54. this.tinymce_id_list = [];
  55. this.setup_meta(doctype);
  56. // notify on rename
  57. var me = this;
  58. $(document).bind('rename', function(event, dt, old_name, new_name) {
  59. if(dt==me.doctype)
  60. me.rename_notify(dt, old_name, new_name)
  61. });
  62. }
  63. // ======================================================================================
  64. _f.Frm.prototype.check_doctype_conflict = function(docname) {
  65. var me = this;
  66. if(this.doctype=='DocType' && docname=='DocType') {
  67. msgprint('Allowing DocType, DocType. Be careful!')
  68. } else if(this.doctype=='DocType') {
  69. if(wn.views.formview[docname]) {
  70. msgprint("Cannot open DocType when its instance is open")
  71. throw 'doctype open conflict'
  72. }
  73. } else {
  74. if(wn.views.formview.DocType && wn.views.formview.DocType.frm.opendocs[this.doctype]) {
  75. msgprint("Cannot open instance when its DocType is open")
  76. throw 'doctype open conflict'
  77. }
  78. }
  79. }
  80. _f.Frm.prototype.setup = function() {
  81. var me = this;
  82. this.fields = [];
  83. this.fields_dict = {};
  84. // wrapper
  85. this.wrapper = this.parent;
  86. // create area for print fomrat
  87. this.setup_print_layout();
  88. // thank you goes here (in case of Guest, don't refresh, just say thank you!)
  89. this.saved_wrapper = $a(this.wrapper, 'div');
  90. // 2 column layout
  91. this.setup_std_layout();
  92. // client script must be called after "setup" - there are no fields_dict attached to the frm otherwise
  93. this.setup_client_script();
  94. this.setup_done = true;
  95. }
  96. // ======================================================================================
  97. _f.Frm.prototype.setup_print_layout = function() {
  98. this.print_wrapper = $a(this.wrapper, 'div');
  99. this.print_head = $a(this.print_wrapper, 'div');
  100. this.print_body = $a(this.print_wrapper,'div', 'layout_wrapper', {
  101. padding:'23px',
  102. minHeight: '800px'
  103. });
  104. var t= make_table(this.print_head, 1 ,2, '100%', [], {padding: '6px'});
  105. this.view_btn_wrapper = $a($td(t,0,0) , 'span', 'green_buttons');
  106. this.view_btn = $btn(this.view_btn_wrapper, 'View Details', function() { cur_frm.edit_doc() },
  107. {marginRight:'4px'}, 'green');
  108. this.print_btn = $btn($td(t,0,0), 'Print', function() { cur_frm.print_doc() });
  109. $y($td(t,0,1), {textAlign: 'right'});
  110. this.print_close_btn = $btn($td(t,0,1), 'Close', function() { window.back(); });
  111. }
  112. _f.Frm.prototype.onhide = function() { if(_f.cur_grid_cell) _f.cur_grid_cell.grid.cell_deselect(); }
  113. // ======================================================================================
  114. _f.Frm.prototype.setup_std_layout = function() {
  115. this.page_layout = new wn.PageLayout({
  116. parent: this.wrapper,
  117. main_width: this.meta.in_dialog ? '100%' : '75%',
  118. sidebar_width: this.meta.in_dialog ? '0%' : '25%'
  119. })
  120. // only tray
  121. this.meta.section_style='Simple'; // always simple!
  122. // layout
  123. this.layout = new Layout(this.page_layout.body, '100%');
  124. // sidebar
  125. if(this.meta.in_dialog) {
  126. // hide sidebar
  127. $(this.page_layout.wrapper).removeClass('layout-wrapper-background');
  128. $(this.page_layout.main).removeClass('layout-main-section');
  129. $(this.page_layout.sidebar_area).toggle(false);
  130. } else {
  131. // module link
  132. this.setup_sidebar();
  133. }
  134. // footer
  135. this.setup_footer();
  136. // header - no headers for tables and guests
  137. if(!(this.meta.istable || user=='Guest'))
  138. this.frm_head = new _f.FrmHeader(this.page_layout.head, this);
  139. // hide close btn for dialog rendering
  140. if(this.frm_head && this.meta.in_dialog) this.frm_head.hide_close();
  141. // bg colour
  142. if(this.meta.colour)
  143. this.layout.wrapper.style.backgroundColor = '#'+this.meta.colour.split(':')[1];
  144. // create fields
  145. this.setup_fields_std();
  146. }
  147. _f.Frm.prototype.setup_print = function() {
  148. var l = []
  149. this.default_format = 'Standard';
  150. for(var key in locals['Print Format']) {
  151. if(locals['Print Format'][key].doc_type == this.meta.name) {
  152. l.push(locals['Print Format'][key].name);
  153. }
  154. }
  155. // if default print format is given, use it
  156. if(this.meta.default_print_format)
  157. this.default_format = this.meta.default_print_format;
  158. l.push('Standard');
  159. this.print_sel = $a(null, 'select', '', {width:'160px'});
  160. add_sel_options(this.print_sel, l);
  161. this.print_sel.value = this.default_format;
  162. }
  163. _f.Frm.prototype.print_doc = function() {
  164. if(this.doc.docstatus==2) {
  165. msgprint("Cannot Print Cancelled Documents.");
  166. return;
  167. }
  168. _p.show_dialog(); // multiple options
  169. }
  170. // ======================================================================================
  171. _f.Frm.prototype.email_doc = function() {
  172. // make selector
  173. if(!_e.dialog) _e.make();
  174. // set print selector
  175. sel = this.print_sel;
  176. var c = $td(_e.dialog.rows['Format'].tab,0,1);
  177. if(c.cur_sel) {
  178. c.removeChild(c.cur_sel);
  179. c.cur_sel = null;
  180. }
  181. c.appendChild(this.print_sel);
  182. c.cur_sel = this.print_sel;
  183. // hide / show attachments
  184. _e.dialog.widgets['Send With Attachments'].checked = 0;
  185. if(cur_frm.doc.file_list) {
  186. $ds(_e.dialog.rows['Send With Attachments']);
  187. } else {
  188. $dh(_e.dialog.rows['Send With Attachments']);
  189. }
  190. _e.dialog.widgets['Subject'].value = get_doctype_label(this.meta.name) + ': ' + this.docname;
  191. _e.dialog.show();
  192. }
  193. // ======================================================================================
  194. _f.Frm.prototype.rename_notify = function(dt, old, name) {
  195. // editable
  196. this.is_editable[name] = this.is_editable[old];
  197. delete this.is_editable[old];
  198. // from form
  199. if(this.docname == old)
  200. this.docname = name;
  201. // cleanup
  202. if(this && this.opendocs[old]) {
  203. // local doctype copy
  204. local_dt[dt][name] = local_dt[dt][old];
  205. local_dt[dt][old] = null;
  206. }
  207. delete this.opendocs[old];
  208. this.opendocs[name] = true;
  209. }
  210. // SETUP
  211. // ======================================================================================
  212. _f.Frm.prototype.setup_meta = function() {
  213. this.meta = get_local('DocType',this.doctype);
  214. this.perm = get_perm(this.doctype); // for create
  215. if(this.meta.istable) { this.meta.in_dialog = 1 }
  216. this.setup_print();
  217. }
  218. // --------------------------------------------------------------------------------------
  219. _f.Frm.prototype.setup_sidebar = function() {
  220. this.sidebar = new wn.widgets.form.sidebar.Sidebar(this);
  221. }
  222. // --------------------------------------------------------------------------------------
  223. _f.Frm.prototype.setup_footer = function() {
  224. var me = this;
  225. // footer toolbar
  226. var f = this.page_layout.footer;
  227. // save buttom
  228. f.save_area = $a(this.page_layout.footer,'div','',{display:'none', marginTop:'11px'});
  229. f.help_area = $a(this.page_layout.footer,'div');
  230. var b = $btn(f.save_area, 'Save',
  231. function() { cur_frm.save('Save'); },{marginLeft:'0px'},'green');
  232. // show / hide save
  233. f.show_save = function() {
  234. $ds(me.page_layout.footer.save_area);
  235. }
  236. f.hide_save = function() {
  237. $dh(me.page_layout.footer.save_area);
  238. }
  239. }
  240. // --------------------------------------------------------------------------------------
  241. _f.Frm.prototype.setup_fields_std = function() {
  242. var fl = fields_list[this.doctype];
  243. fl.sort(function(a,b) { return a.idx - b.idx});
  244. if(fl[0]&&fl[0].fieldtype!="Section Break" || get_url_arg('embed')) {
  245. this.layout.addrow(); // default section break
  246. if(fl[0].fieldtype!="Column Break") {// without column too
  247. var c = this.layout.addcell();
  248. $y(c.wrapper, {padding: '8px'});
  249. }
  250. }
  251. var sec;
  252. for(var i=0;i<fl.length;i++) {
  253. var f=fl[i];
  254. // if section break and next item
  255. // is a section break then ignore
  256. if(f.fieldtype=='Section Break' && fl[i+1] && fl[i+1].fieldtype=='Section Break')
  257. continue;
  258. var fn = f.fieldname?f.fieldname:f.label;
  259. var fld = make_field(f, this.doctype, this.layout.cur_cell, this);
  260. this.fields[this.fields.length] = fld;
  261. this.fields_dict[fn] = fld;
  262. if(sec && ['Section Break', 'Column Break'].indexOf(f.fieldtype)==-1) {
  263. fld.parent_section = sec;
  264. sec.fields.push(fld);
  265. }
  266. if(f.fieldtype=='Section Break') {
  267. sec = fld;
  268. this.sections.push(fld);
  269. }
  270. // default col-break after sec-break
  271. if((f.fieldtype=='Section Break')&&(fl[i+1])&&(fl[i+1].fieldtype!='Column Break')&&!f.hidden) {
  272. var c = this.layout.addcell();
  273. $y(c.wrapper, {padding: '8px'});
  274. }
  275. }
  276. }
  277. // --------------------------------------------------------------------------------------
  278. _f.Frm.prototype.add_custom_button = function(label, fn, icon) {
  279. this.frm_head.add_button(label, fn, icon);
  280. }
  281. _f.Frm.prototype.clear_custom_buttons = function() {
  282. //
  283. }
  284. // --------------------------------------------------------------------------------------
  285. _f.Frm.prototype.add_fetch = function(link_field, src_field, tar_field) {
  286. if(!this.fetch_dict[link_field]) {
  287. this.fetch_dict[link_field] = {'columns':[], 'fields':[]}
  288. }
  289. this.fetch_dict[link_field].columns.push(src_field);
  290. this.fetch_dict[link_field].fields.push(tar_field);
  291. }
  292. // --------------------------------------------------------------------------------------
  293. _f.Frm.prototype.setup_client_script = function() {
  294. // setup client obj
  295. if(this.meta.client_script_core || this.meta.client_script || this.meta.__js) {
  296. this.runclientscript('setup', this.doctype, this.docname);
  297. }
  298. }
  299. // --------------------------------------------------------------------------------------
  300. _f.Frm.prototype.refresh_print_layout = function() {
  301. $ds(this.print_wrapper);
  302. $dh(this.page_layout.wrapper);
  303. var me = this;
  304. var print_callback = function(print_html) {
  305. me.print_body.innerHTML = print_html;
  306. }
  307. // print head
  308. if(cur_frm.doc.select_print_heading)
  309. cur_frm.set_print_heading(cur_frm.doc.select_print_heading)
  310. if(user!='Guest') {
  311. $di(this.view_btn_wrapper);
  312. // archive
  313. if(cur_frm.doc.__archived) {
  314. $dh(this.view_btn_wrapper);
  315. }
  316. } else {
  317. $dh(this.view_btn_wrapper);
  318. $dh(this.print_close_btn);
  319. }
  320. // create print format here
  321. _p.build(this.default_format, print_callback, null, 1);
  322. }
  323. // --------------------------------------------------------------------------------------
  324. _f.Frm.prototype.show_the_frm = function() {
  325. // show the dialog
  326. if(this.meta.in_dialog && !this.parent.dialog.display) {
  327. if(!this.meta.istable)
  328. this.parent.table_form = false;
  329. this.parent.dialog.show();
  330. }
  331. }
  332. // --------------------------------------------------------------------------------------
  333. _f.Frm.prototype.set_print_heading = function(txt) {
  334. this.pformat[cur_frm.docname] = txt;
  335. }
  336. // --------------------------------------------------------------------------------------
  337. _f.Frm.prototype.defocus_rest = function() {
  338. // deselect others
  339. if(_f.cur_grid_cell) _f.cur_grid_cell.grid.cell_deselect();
  340. }
  341. // -------- Permissions -------
  342. // Returns global permissions, at all levels
  343. // ======================================================================================
  344. _f.Frm.prototype.get_doc_perms = function() {
  345. var p = [0,0,0,0,0,0];
  346. for(var i=0; i<this.perm.length; i++) {
  347. if(this.perm[i]) {
  348. if(this.perm[i][READ]) p[READ] = 1;
  349. if(this.perm[i][WRITE]) p[WRITE] = 1;
  350. if(this.perm[i][SUBMIT]) p[SUBMIT] = 1;
  351. if(this.perm[i][CANCEL]) p[CANCEL] = 1;
  352. if(this.perm[i][AMEND]) p[AMEND] = 1;
  353. }
  354. }
  355. return p;
  356. }
  357. // refresh
  358. // ======================================================================================
  359. _f.Frm.prototype.refresh_header = function() {
  360. // set title
  361. // main title
  362. if(!this.meta.in_dialog) {
  363. set_title(this.meta.issingle ? this.doctype : this.docname);
  364. }
  365. // show / hide buttons
  366. if(this.frm_head)this.frm_head.refresh();
  367. // add to recent
  368. if(wn.ui.toolbar.recent) wn.ui.toolbar.recent.add(this.doctype, this.docname, 1);
  369. }
  370. // --------------------------------------------------------------------------------------
  371. _f.Frm.prototype.check_doc_perm = function() {
  372. // get perm
  373. var dt = this.parent_doctype?this.parent_doctype : this.doctype;
  374. var dn = this.parent_docname?this.parent_docname : this.docname;
  375. this.perm = get_perm(dt, dn);
  376. this.orig_perm = get_perm(dt, dn, 1);
  377. if(!this.perm[0][READ]) {
  378. if(user=='Guest') {
  379. // allow temp access? via encryted akey
  380. if(_f.temp_access[dt] && _f.temp_access[dt][dn]) {
  381. this.perm = [[1,0,0]]
  382. return 1;
  383. }
  384. }
  385. window.back();
  386. return 0;
  387. }
  388. return 1
  389. }
  390. // --------------------------------------------------------------------------------------
  391. _f.Frm.prototype.refresh = function(docname) {
  392. // record switch
  393. if(docname) {
  394. if(this.docname != docname && !this.meta.in_dialog && !this.meta.istable) scroll(0, 0);
  395. this.docname = docname;
  396. }
  397. if(!this.meta.istable) {
  398. cur_frm = this;
  399. this.parent.cur_frm = this;
  400. }
  401. if(this.docname) { // document to show
  402. // check permissions
  403. if(!this.check_doc_perm()) return;
  404. // do setup
  405. if(!this.setup_done) this.setup();
  406. // set customized permissions for this record
  407. this.runclientscript('set_perm',this.doctype, this.docname);
  408. // set the doc
  409. this.doc = get_local(this.doctype, this.docname);
  410. // load the record for the first time, if not loaded (call 'onload')
  411. cur_frm.cscript.is_onload = false;
  412. if(!this.opendocs[this.docname]) {
  413. cur_frm.cscript.is_onload = true;
  414. this.setnewdoc(this.docname);
  415. }
  416. // editable
  417. if(this.doc.__islocal)
  418. this.is_editable[this.docname] = 1; // new is editable
  419. this.editable = this.is_editable[this.docname];
  420. if(!this.doc.__archived && (this.editable || (!this.editable && this.meta.istable))) {
  421. // show form layout (with fields etc)
  422. // ----------------------------------
  423. if(this.print_wrapper) {
  424. $dh(this.print_wrapper);
  425. $ds(this.page_layout.wrapper);
  426. }
  427. // header
  428. if(!this.meta.istable) {
  429. this.refresh_header();
  430. this.sidebar && this.sidebar.refresh();
  431. }
  432. // call trigger
  433. this.runclientscript('refresh');
  434. // trigger global trigger
  435. // to use this
  436. // $(docuemnt).bind('form_refresh', function() { })
  437. $(document).trigger('form_refresh');
  438. // fields
  439. this.refresh_fields();
  440. // dependent fields
  441. this.refresh_dependency();
  442. // footer
  443. this.refresh_footer();
  444. // layout
  445. if(this.layout) this.layout.show();
  446. // call onload post render for callbacks to be fired
  447. if(cur_frm.cscript.is_onload) {
  448. this.runclientscript('onload_post_render', this.doctype, this.docname);
  449. }
  450. // focus on first input
  451. if(this.doc.docstatus==0) {
  452. $(this.wrapper).find('.form-layout-row :input:first').focus();
  453. }
  454. } else {
  455. // show print layout
  456. // ----------------------------------
  457. this.refresh_header();
  458. if(this.print_wrapper) {
  459. this.refresh_print_layout();
  460. }
  461. this.runclientscript('edit_status_changed');
  462. }
  463. $(cur_frm.wrapper).trigger('render_complete');
  464. }
  465. }
  466. // --------------------------------------------------------------------------------------
  467. _f.Frm.prototype.refresh_footer = function() {
  468. var f = this.page_layout.footer;
  469. if(f.save_area) {
  470. if(get_url_arg('embed') || (this.editable && !this.meta.in_dialog && this.doc.docstatus==0 && !this.meta.istable && this.get_doc_perms()[WRITE])) {
  471. f.show_save();
  472. } else {
  473. f.hide_save();
  474. }
  475. }
  476. }
  477. // --------------------------------------------------------------------------------------
  478. _f.Frm.prototype.refresh_fields = function() {
  479. // refresh fields
  480. for(var i=0; i<this.fields.length; i++) {
  481. var f = this.fields[i];
  482. f.perm = this.perm;
  483. f.docname = this.docname;
  484. // if field is identifiable (not blank section or column break)
  485. // get the "customizable" parameters for this record
  486. var fn = f.df.fieldname || f.df.label;
  487. if(fn)
  488. f.df = get_field(this.doctype, fn, this.docname);
  489. if(f.df.fieldtype!='Section Break' && f.refresh) {
  490. f.refresh();
  491. }
  492. }
  493. // refresh sections
  494. $.each(this.sections, function(i, f) {
  495. f.refresh(true);
  496. })
  497. // cleanup activities after refresh
  498. this.cleanup_refresh(this);
  499. }
  500. // --------------------------------------------------------------------------------------
  501. _f.Frm.prototype.cleanup_refresh = function() {
  502. var me = this;
  503. if(me.fields_dict['amended_from']) {
  504. if (me.doc.amended_from) {
  505. unhide_field('amended_from'); unhide_field('amendment_date');
  506. } else {
  507. hide_field('amended_from'); hide_field('amendment_date');
  508. }
  509. }
  510. if(me.fields_dict['trash_reason']) {
  511. if(me.doc.trash_reason && me.doc.docstatus == 2) {
  512. unhide_field('trash_reason');
  513. } else {
  514. hide_field('trash_reason');
  515. }
  516. }
  517. if(me.meta.autoname && me.meta.autoname.substr(0,6)=='field:' && !me.doc.__islocal) {
  518. var fn = me.meta.autoname.substr(6);
  519. set_field_permlevel(fn,1); // make it readonly / hidden
  520. }
  521. }
  522. // Resolve "depends_on" and show / hide accordingly
  523. // ======================================================================================
  524. _f.Frm.prototype.refresh_dependency = function() {
  525. var me = this;
  526. var doc = locals[this.doctype][this.docname];
  527. // build dependants' dictionary
  528. var dep_dict = {};
  529. var has_dep = false;
  530. for(fkey in me.fields) {
  531. var f = me.fields[fkey];
  532. f.dependencies_clear = true;
  533. var guardian = f.df.depends_on;
  534. if(guardian) {
  535. if(!dep_dict[guardian])
  536. dep_dict[guardian] = [];
  537. dep_dict[guardian][dep_dict[guardian].length] = f;
  538. has_dep = true;
  539. }
  540. }
  541. if(!has_dep)return;
  542. // show / hide based on values
  543. for(var i=me.fields.length-1;i>=0;i--) {
  544. var f = me.fields[i];
  545. f.guardian_has_value = true;
  546. if(f.df.depends_on) {
  547. // evaluate guardian
  548. var v = doc[f.df.depends_on];
  549. if(f.df.depends_on.substr(0,5)=='eval:') {
  550. f.guardian_has_value = eval(f.df.depends_on.substr(5));
  551. } else if(f.df.depends_on.substr(0,3)=='fn:') {
  552. f.guardian_has_value = me.runclientscript(f.df.depends_on.substr(3), me.doctype, me.docname);
  553. } else {
  554. if(v || (v==0 && !v.substr)) {
  555. // guardian has value
  556. } else {
  557. f.guardian_has_value = false;
  558. }
  559. }
  560. // show / hide
  561. if(f.guardian_has_value) {
  562. if(f.grid)f.grid.show(); else $ds(f.wrapper);
  563. } else {
  564. if(f.grid)f.grid.hide(); else $dh(f.wrapper);
  565. }
  566. }
  567. }
  568. }
  569. // setnewdoc is called when a record is loaded for the first time
  570. // ======================================================================================
  571. _f.Frm.prototype.setnewdoc = function(docname) {
  572. this.check_doctype_conflict(docname);
  573. // if loaded
  574. if(this.opendocs[docname]) { // already exists
  575. this.docname=docname;
  576. return;
  577. }
  578. //if(!this.meta)
  579. // this.setup_meta();
  580. // make a copy of the doctype for client script settings
  581. // each record will have its own client script
  582. Meta.make_local_dt(this.doctype,docname);
  583. this.docname = docname;
  584. var me = this;
  585. var viewname = docname;
  586. if(this.meta.issingle) viewname = this.doctype;
  587. // Client Script
  588. this.runclientscript('onload', this.doctype, this.docname);
  589. this.is_editable[docname] = 1;
  590. if(this.meta.read_only_onload) this.is_editable[docname] = 0;
  591. this.opendocs[docname] = true;
  592. }
  593. _f.Frm.prototype.edit_doc = function() {
  594. // set fields
  595. this.is_editable[this.docname] = true;
  596. this.refresh();
  597. }
  598. _f.Frm.prototype.show_doc = function(dn) {
  599. this.refresh(dn);
  600. }
  601. // ======================================================================================
  602. var validated; // bad design :(
  603. _f.Frm.prototype.save = function(save_action, call_back) {
  604. //alert(save_action);
  605. if(!save_action) save_action = 'Save';
  606. var me = this;
  607. if(this.savingflag) {
  608. msgprint("Document is currently saving....");
  609. return; // already saving (do not double save)
  610. }
  611. if(save_action=='Submit') {
  612. locals[this.doctype][this.docname].submitted_on = dateutil.full_str();
  613. locals[this.doctype][this.docname].submitted_by = user;
  614. }
  615. if(save_action=='Trash') {
  616. var reason = prompt('Reason for trash (mandatory)', '');
  617. if(!strip(reason)) {
  618. msgprint('Reason is mandatory, not trashed');
  619. return;
  620. }
  621. locals[this.doctype][this.docname].trash_reason = reason;
  622. }
  623. // run validations
  624. if(save_action=='Cancel') {
  625. var reason = prompt('Reason for cancellation (mandatory)', '');
  626. if(!strip(reason)) {
  627. msgprint('Reason is mandatory, not cancelled');
  628. return;
  629. }
  630. locals[this.doctype][this.docname].cancel_reason = reason;
  631. locals[this.doctype][this.docname].cancelled_on = dateutil.full_str();
  632. locals[this.doctype][this.docname].cancelled_by = user;
  633. } else if(save_action=='Update') {
  634. // no validation for update
  635. } else { // no validation for cancellation
  636. validated = true;
  637. if(this.cscript.validate)
  638. this.runclientscript('validate', this.doctype, this.docname);
  639. if(!validated) {
  640. this.savingflag = false;
  641. return 'Error';
  642. }
  643. }
  644. var ret_fn = function(r) {
  645. me.savingflag = false;
  646. if(user=='Guest' && !r.exc) {
  647. // if user is guest, show a message after succesful saving
  648. $dh(me.page_layout.wrapper);
  649. $ds(me.saved_wrapper);
  650. me.saved_wrapper.innerHTML =
  651. '<div style="padding: 150px 16px; text-align: center; font-size: 14px;">'
  652. + (cur_frm.message_after_save ? cur_frm.message_after_save : 'Your information has been sent. Thank you!')
  653. + '</div>';
  654. return; // no refresh
  655. }
  656. if(!me.meta.istable) {
  657. me.refresh();
  658. }
  659. if(call_back){
  660. call_back(r);
  661. }
  662. }
  663. var me = this;
  664. var ret_fn_err = function(r) {
  665. var doc = locals[me.doctype][me.docname];
  666. me.savingflag = false;
  667. ret_fn(r);
  668. }
  669. this.savingflag = true;
  670. if(this.docname && validated) {
  671. // scroll to top
  672. scroll(0, 0);
  673. return this.savedoc(save_action, ret_fn, ret_fn_err);
  674. }
  675. }
  676. // ======================================================================================
  677. _f.Frm.prototype.runscript = function(scriptname, callingfield, onrefresh) {
  678. var me = this;
  679. if(this.docname) {
  680. // make doc list
  681. var doclist = compress_doclist(make_doclist(this.doctype, this.docname));
  682. // send to run
  683. if(callingfield)callingfield.input.disabled = true;
  684. $c('runserverobj', {'docs':doclist, 'method':scriptname },
  685. function(r, rtxt) {
  686. // run refresh
  687. if(onrefresh)
  688. onrefresh(r,rtxt);
  689. // fields
  690. me.refresh_fields();
  691. // dependent fields
  692. me.refresh_dependency();
  693. // enable button
  694. if(callingfield)callingfield.input.done_working();
  695. }
  696. );
  697. }
  698. }
  699. // ======================================================================================
  700. _f.Frm.prototype.runclientscript = function(caller, cdt, cdn) {
  701. var _dt = this.parent_doctype ? this.parent_doctype : this.doctype;
  702. var _dn = this.parent_docname ? this.parent_docname : this.docname;
  703. var doc = get_local(_dt, _dn);
  704. if(!cdt)cdt = this.doctype;
  705. if(!cdn)cdn = this.docname;
  706. var ret = null;
  707. try {
  708. if(this.cscript[caller])
  709. ret = this.cscript[caller](doc, cdt, cdn);
  710. // for product
  711. if(this.cscript['custom_'+caller])
  712. ret += this.cscript['custom_'+caller](doc, cdt, cdn);
  713. } catch(e) {
  714. console.log(e);
  715. }
  716. if(caller && caller.toLowerCase()=='setup') {
  717. var doctype = get_local('DocType', this.doctype);
  718. // js
  719. var cs = doctype.__js || (doctype.client_script_core + doctype.client_script);
  720. if(cs) {
  721. try {
  722. var tmp = eval(cs);
  723. } catch(e) {
  724. console.log(e);
  725. }
  726. }
  727. // css
  728. if(doctype.__css) set_style(doctype.__css)
  729. // ---Client String----
  730. if(doctype.client_string) { // split client string
  731. this.cstring = {};
  732. var elist = doctype.client_string.split('---');
  733. for(var i=1;i<elist.length;i=i+2) {
  734. this.cstring[strip(elist[i])] = elist[i+1];
  735. }
  736. }
  737. }
  738. return ret;
  739. }
  740. // ======================================================================================
  741. _f.Frm.prototype.copy_doc = function(onload, from_amend) {
  742. if(!this.perm[0][CREATE]) {
  743. msgprint('You are not allowed to create '+this.meta.name);
  744. return;
  745. }
  746. var dn = this.docname;
  747. // copy parent
  748. var newdoc = LocalDB.copy(this.doctype, dn, from_amend);
  749. // do not copy attachments
  750. if(this.meta.allow_attach && newdoc.file_list)
  751. newdoc.file_list = null;
  752. // copy chidren
  753. var dl = make_doclist(this.doctype, dn);
  754. // table fields dict - for no_copy check
  755. var tf_dict = {};
  756. for(var d in dl) {
  757. d1 = dl[d];
  758. // get tabel field
  759. if(!tf_dict[d1.parentfield]) {
  760. tf_dict[d1.parentfield] = get_field(d1.parenttype, d1.parentfield);
  761. }
  762. if(d1.parent==dn && cint(tf_dict[d1.parentfield].no_copy)!=1) {
  763. var ch = LocalDB.copy(d1.doctype, d1.name, from_amend);
  764. ch.parent = newdoc.name;
  765. ch.docstatus = 0;
  766. ch.owner = user;
  767. ch.creation = '';
  768. ch.modified_by = user;
  769. ch.modified = '';
  770. }
  771. }
  772. newdoc.__islocal = 1;
  773. newdoc.docstatus = 0;
  774. newdoc.owner = user;
  775. newdoc.creation = '';
  776. newdoc.modified_by = user;
  777. newdoc.modified = '';
  778. if(onload)onload(newdoc);
  779. loaddoc(newdoc.doctype, newdoc.name);
  780. }
  781. // ======================================================================================
  782. _f.Frm.prototype.reload_doc = function() {
  783. this.check_doctype_conflict(this.docname);
  784. var me = this;
  785. var ret_fn = function(r, rtxt) {
  786. // n tweets and last comment
  787. me.runclientscript('setup', me.doctype, me.docname);
  788. me.refresh();
  789. }
  790. if(me.doc.__islocal) {
  791. // reload only doctype
  792. $c('webnotes.widgets.form.load.getdoctype', {'doctype':me.doctype }, ret_fn, null, null, 'Refreshing ' + me.doctype + '...');
  793. } else {
  794. // delete all unsaved rows
  795. var gl = me.grids;
  796. for(var i = 0; i < gl.length; i++) {
  797. var dt = gl[i].df.options;
  798. for(var dn in locals[dt]) {
  799. if(locals[dt][dn].__islocal && locals[dt][dn].parent == me.docname) {
  800. var d = locals[dt][dn];
  801. d.parent = '';
  802. d.docstatus = 2;
  803. d.__deleted = 1;
  804. }
  805. }
  806. }
  807. // reload doc and docytpe
  808. $c('webnotes.widgets.form.load.getdoc', {'name':me.docname, 'doctype':me.doctype, 'getdoctype':1, 'user':user}, ret_fn, null, null, 'Refreshing ' + me.docname + '...');
  809. }
  810. }
  811. // ======================================================================================
  812. _f.Frm.prototype.savedoc = function(save_action, onsave, onerr) {
  813. this.error_in_section = 0;
  814. save_doclist(this.doctype, this.docname, save_action, onsave, onerr);
  815. }
  816. _f.Frm.prototype.saveupdate = function() {
  817. this.save('Update');
  818. }
  819. _f.Frm.prototype.savesubmit = function() {
  820. var answer = confirm("Permanently Submit "+this.docname+"?");
  821. var me = this;
  822. if(answer) {
  823. this.save('Submit', function(r) {
  824. if(!r.exc && me.cscript.on_submit) {
  825. me.runclientscript('on_submit', me.doctype, me.docname);
  826. }
  827. });
  828. }
  829. }
  830. _f.Frm.prototype.savecancel = function() {
  831. var answer = confirm("Permanently Cancel "+this.docname+"?");
  832. if(answer) this.save('Cancel');
  833. }
  834. // delete the record
  835. _f.Frm.prototype.savetrash = function() {
  836. var me = this;
  837. var answer = confirm("Permanently Delete "+this.docname+"? This action cannot be reversed");
  838. if(answer) {
  839. $c('webnotes.model.delete_doc', {dt:this.doctype, dn:this.docname}, function(r,rt) {
  840. if(r.message=='okay') {
  841. // delete from locals
  842. LocalDB.delete_doc(me.doctype, me.docname);
  843. // delete from recent
  844. if(wn.ui.toolbar.recent) wn.ui.toolbar.recent.remove(me.doctype, me.docname);
  845. // "close"
  846. window.back();
  847. }
  848. })
  849. }
  850. }
  851. // ======================================================================================
  852. _f.Frm.prototype.amend_doc = function() {
  853. if(!this.fields_dict['amended_from']) {
  854. alert('"amended_from" field must be present to do an amendment.');
  855. return;
  856. }
  857. var me = this;
  858. var fn = function(newdoc) {
  859. newdoc.amended_from = me.docname;
  860. if(me.fields_dict && me.fields_dict['amendment_date'])
  861. newdoc.amendment_date = dateutil.obj_to_str(new Date());
  862. }
  863. this.copy_doc(fn, 1);
  864. }
  865. // ======================================================================================
  866. _f.get_value = function(dt, dn, fn) {
  867. if(locals[dt] && locals[dt][dn])
  868. return locals[dt][dn][fn];
  869. }
  870. _f.set_value = function(dt, dn, fn, v) {
  871. var d = locals[dt][dn];
  872. if(!d) {
  873. console.log('_f.set_value - '+ fn+': "'+dt+','+dn+'" not found');
  874. return;
  875. }
  876. var changed = d[fn] != v;
  877. if(changed && (d[fn]==null || v==null) && (cstr(d[fn])==cstr(v))) changed = 0;
  878. if(changed) {
  879. d[fn] = v;
  880. d.__unsaved = 1;
  881. var frm = wn.views.formview[d.doctype].frm;
  882. try {
  883. if(d.parent && d.parenttype) {
  884. locals[d.parenttype][d.parent].__unsaved = 1;
  885. frm = wn.views.formview[d.parenttype].frm;
  886. }
  887. } catch(e) {
  888. if(d.parent && d.parenttype)
  889. errprint('Setting __unsaved error:'+d.name+','+d.parent+','+d.parenttype);
  890. }
  891. if(frm && frm==cur_frm && frm.frm_head) {
  892. frm.frm_head.refresh_labels();
  893. }
  894. }
  895. }
  896. // ======================================================================================
  897. _f.Frm.prototype.show_comments = function() {
  898. if(!cur_frm.comments) {
  899. cur_frm.comments = new Dialog(540, 400, 'Comments');
  900. cur_frm.comments.comment_body = $a(cur_frm.comments.body, 'div', 'dialog_frm');
  901. $y(cur_frm.comments.body, {backgroundColor:'#EEE'});
  902. cur_frm.comments.list = new CommentList(cur_frm.comments.comment_body);
  903. }
  904. cur_frm.comments.list.dt = cur_frm.doctype;
  905. cur_frm.comments.list.dn = cur_frm.docname;
  906. cur_frm.comments.show();
  907. cur_frm.comments.list.run();
  908. }