您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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