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_header.js 10 KiB

13 年之前
13 年之前
13 年之前
13 年之前
13 年之前
13 年之前
13 年之前
13 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. // features
  23. // --------
  24. // toolbar - standard and custom
  25. // label - saved, submitted etc
  26. // breadcrumbs
  27. // save / submit button toggle based on "saved" or not
  28. // highlight and fade name based on refresh
  29. _f.FrmHeader = Class.extend({
  30. init: function(parent, frm) {
  31. this.buttons = {};
  32. this.$w = $('<div class="form-header">\
  33. <div class="form-header-main">\
  34. <span class="label-area"></span>\
  35. <span class="breadcrumb-area"></span>\
  36. <span class="close">&times;</span>\
  37. </div>\
  38. <div class="form-header-toolbar">\
  39. </div>\
  40. </div>').appendTo(parent);
  41. this.$w.find('.close').click(function() {
  42. window.history.back();
  43. })
  44. },
  45. refresh: function() {
  46. // refresh breadcrumbs
  47. wn.views.breadcrumbs($(this.$w.find('.breadcrumb-area')),
  48. cur_frm.meta.module, cur_frm.meta.name, cur_frm.docname);
  49. this.refresh_labels();
  50. this.refresh_toolbar();
  51. },
  52. refresh_labels: function() {
  53. var labinfo = {
  54. 0: ['Draft', ''],
  55. 1: ['Submitted', 'label-info'],
  56. 2: ['Cancelled', 'label-important']
  57. }[cint(cur_frm.doc.docstatus)];
  58. if(cur_frm.doc.__unsaved) {
  59. labinfo[1] = 'label-warning'
  60. }
  61. this.$w.find('.label-area').html(repl('<span class="label %(lab_class)s">\
  62. %(lab_status)s</span>', {
  63. lab_status: labinfo[0],
  64. lab_class: labinfo[1]
  65. }));
  66. },
  67. refresh_toolbar: function() {
  68. // clear
  69. this.$w.find('.form-header-toolbar').empty();
  70. var p = cur_frm.get_doc_perms();
  71. // Edit
  72. if(cur_frm.meta.read_only_onload && !cur_frm.doc.__islocal) {
  73. if(!cur_frm.editable)
  74. this.add_button('Edit', function() {
  75. cur_frm.edit_doc();
  76. },'icon-pencil');
  77. else
  78. this.add_button('Print View', function() {
  79. cur_frm.is_editable[cur_frm.docname] = 0;
  80. cur_frm.refresh(); }, 'icon-print' );
  81. }
  82. var docstatus = cint(cur_frm.doc.docstatus);
  83. // Save
  84. if(docstatus==0 && p[WRITE]) {
  85. this.add_button('Save', function() { cur_frm.save('Save');}, '');
  86. this.buttons['Save'].addClass('btn-primary');
  87. }
  88. // Submit
  89. if(docstatus==0 && p[SUBMIT] && (!cur_frm.doc.__islocal))
  90. this.add_button('Submit', function() { cur_frm.savesubmit();}, 'icon-lock');
  91. // Update after sumit
  92. if(docstatus==1 && p[SUBMIT]) {
  93. this.add_button('Update', function() { cur_frm.savesubmit();}, '');
  94. if(!cur_frm.doc.__unsaved) this.buttons['Update'].toggle(false);
  95. }
  96. // Cancel
  97. if(docstatus==1 && p[CANCEL])
  98. this.add_button('Cancel', function() { cur_frm.savecancel() }, 'icon-remove');
  99. // Amend
  100. if(docstatus==2 && p[AMEND])
  101. this.add_button('Amend', function() { cur_frm.amend_doc() }, 'icon-pencil');
  102. },
  103. add_button: function(label, click, icon) {
  104. args = { label: label, icon:'' };
  105. if(icon) {
  106. args.icon = '<i class="'+icon+'"></i>';
  107. }
  108. this.buttons[label] = $(repl('<button class="btn btn-small">\
  109. %(icon)s %(label)s</button>', args))
  110. .click(click)
  111. .appendTo(this.$w.find('.form-header-toolbar'));
  112. },
  113. show: function() {
  114. },
  115. hide: function() {
  116. },
  117. hide_close: function() {
  118. this.$w.find('.close').toggle(false);
  119. }
  120. })
  121. /*
  122. _f.FrmHeader = function(parent, frm) {
  123. var me = this;
  124. this.wrapper = $a(parent, 'div');
  125. if(frm.meta.in_dialog) $y(this.wrapper, {marginLeft:'8px', marginRight:'8px'});
  126. this.page_head = new PageHeader(this.wrapper);
  127. wn.views.breadcrumbs(this.page_head.breadcrumbs, frm.meta.module, frm.meta.name);
  128. // doctype
  129. this.dt_area = $a(this.page_head.main_head, 'span', '', {marginRight:'8px', display:'inline'})
  130. // name
  131. var div = $a(null, 'div', '', {marginBottom:'4px'});
  132. this.page_head.wrapper.insertBefore(div, this.page_head.sub_head);
  133. this.dn_area = $a(div, 'span', '', {fontSize:'14px', fontWeight:'normal', marginRight:'8px', padding: '2px'})
  134. // status
  135. this.status_area = $a(div, 'span', '', {marginRight:'8px', marginBottom:'2px', cursor:'pointer', textShadow:'none'})
  136. }
  137. _f.FrmHeader.prototype.show = function() { $ds(this.wrapper); }
  138. _f.FrmHeader.prototype.hide = function() { $dh(this.wrapper); }
  139. // toolbar buttons
  140. // =======================================================================
  141. _f.FrmHeader.prototype.refresh= function() {
  142. var me = this;
  143. var p = cur_frm.get_doc_perms();
  144. this.page_head.clear_toolbar();
  145. // Edit
  146. if(cur_frm.meta.read_only_onload && !cur_frm.doc.__islocal) {
  147. if(!cur_frm.editable)
  148. this.page_head.add_button('Edit', function() {
  149. cur_frm.edit_doc();
  150. }, 1, 'icon-pencil', 1
  151. );
  152. else
  153. this.page_head.add_button('Print View', function() {
  154. cur_frm.is_editable[cur_frm.docname] = 0;
  155. cur_frm.refresh(); }, 1, 'icon-print' );
  156. }
  157. // Save
  158. if(cur_frm.editable && cint(cur_frm.doc.docstatus)==0 && p[WRITE])
  159. this.page_head.add_button('Save', function() { cur_frm.save('Save');}, 1, '',1);
  160. // Submit
  161. if(cint(cur_frm.doc.docstatus)==0 && p[SUBMIT] && (!cur_frm.doc.__islocal))
  162. this.page_head.add_button('Submit', function() { cur_frm.savesubmit(); }, 0, 'icon-lock');
  163. // Update after sumit
  164. if(cint(cur_frm.doc.docstatus)==1 && p[SUBMIT]) {
  165. this.update_btn = this.page_head.add_button('Update', function() { cur_frm.saveupdate(); }, 1, 'icon-ok', 1);
  166. if(!cur_frm.doc.__unsaved) $dh(this.update_btn);
  167. }
  168. // Cancel
  169. if(cint(cur_frm.doc.docstatus)==1 && p[CANCEL])
  170. this.page_head.add_button('Cancel', function() { cur_frm.savecancel() }, 0, 'icon-remove');
  171. // Amend
  172. if(cint(cur_frm.doc.docstatus)==2 && p[AMEND])
  173. this.page_head.add_button('Amend', function() { cur_frm.amend_doc() }, 0, 'icon-pencil');
  174. }
  175. _f.FrmHeader.prototype.show_toolbar = function() { $ds(this.wrapper); this.refresh(); }
  176. _f.FrmHeader.prototype.hide_toolbar = function() { $dh(this.wrapper); }
  177. // refresh toolbar
  178. // -------------------------------------------------------------------
  179. _f.FrmHeader.prototype.refresh_toolbar = function() {
  180. var m = cur_frm.meta;
  181. if(m.hide_heading || cur_frm.in_dialog) {
  182. // no heading... poof
  183. this.hide();
  184. } else {
  185. this.show();
  186. // with or without toolbar?
  187. if(m.hide_toolbar) {
  188. this.hide_toolbar();
  189. } else {
  190. this.show_toolbar();
  191. }
  192. }
  193. //this.refresh_comments();
  194. }
  195. // make the status tag
  196. // -------------------------------------------------------------------
  197. _f.FrmHeader.prototype.get_status_tags = function(doc, f) {
  198. var make_tag = function(label, col) {
  199. var s= $a(null, 'span', '', {padding: '2px', backgroundColor:col, color:'#FFF', fontWeight:'bold', marginLeft:(f.meta.issingle ? '0px' : '8px'), fontSize:'11px'});
  200. $(s).css('-moz-border-radius','3px').css('-webkit-border-radius','3px')
  201. s.innerHTML = label;
  202. return s;
  203. }
  204. var sp1 = null; var sp2 = null;
  205. if(doc.__islocal) {
  206. label = 'Unsaved Draft'; col = '#F81';
  207. } else if(cint(doc.__unsaved)) {
  208. label = 'Not Saved'; col = '#F81';
  209. if(doc.docstatus==1 && this.update_btn) $ds(this.update_btn);
  210. } else if(cint(doc.docstatus)==0) {
  211. label = 'Saved'; col = '#0A1';
  212. // if submittable, show it
  213. if(f.get_doc_perms()[SUBMIT]) {
  214. sp2 = make_tag('To Be Submitted', '#888');
  215. }
  216. } else if(cint(doc.docstatus)==1) {
  217. label = 'Submitted'; col = '#44F';
  218. } else if(cint(doc.docstatus)==2) {
  219. label = 'Cancelled'; col = '#F44';
  220. }
  221. sp1 = make_tag(label, col);
  222. this.set_in_recent(doc, col);
  223. return [sp1, sp2];
  224. }
  225. // refresh "recent" tag colour
  226. // -------------------------------------------------------------------
  227. _f.FrmHeader.prototype.set_in_recent = function(doc, col) {
  228. var tn = $i('rec_'+doc.doctype+'-'+doc.name);
  229. if(tn)
  230. $y(tn,{backgroundColor:col});
  231. }
  232. // set the button color of save / submit
  233. _f.FrmHeader.prototype.set_save_submit_color = function(doc) {
  234. var save_btn = this.page_head.buttons['Save'];
  235. var submit_btn = this.page_head.buttons['Submit'];
  236. if(cint(doc.docstatus)==0 && submit_btn && save_btn) {
  237. if(cint(doc.__unsaved)) {
  238. $(save_btn).addClass('btn-info');
  239. $(save_btn).find('i').addClass('icon-white');
  240. $(submit_btn).removeClass('btn-info');
  241. $(submit_btn).find('i').removeClass('icon-white');
  242. } else {
  243. $(submit_btn).addClass('btn-info');
  244. $(submit_btn).find('i').addClass('icon-white');
  245. $(save_btn).removeClass('btn-info');
  246. $(save_btn).find('i').removeClass('icon-white');
  247. }
  248. }
  249. }
  250. // refresh the labels!
  251. // -------------------------------------------------------------------
  252. _f.FrmHeader.prototype.refresh_labels = function(f) {
  253. var ph = this.page_head;
  254. var me = this;
  255. // main title
  256. this.dt_area.innerHTML = get_doctype_label(f.doctype);
  257. if(f.meta.issingle) $(this.dn_area).toggle(false);
  258. // sub title
  259. this.dn_area.innerHTML = '';
  260. if(!f.meta.issingle)
  261. this.dn_area.innerHTML = f.docname;
  262. $(this.dn_area)
  263. .removeClass('background-fade-in')
  264. .css('background-color', '#ff8')
  265. // get the doc
  266. var doc = locals[f.doctype][f.docname];
  267. // get the tags
  268. var sl = this.get_status_tags(doc, f);
  269. // set save, submit color
  270. this.set_save_submit_color(doc);
  271. // add the tags
  272. var t = this.status_area;
  273. t.innerHTML = '';
  274. t.appendChild(sl[0]);
  275. if(sl[1])t.appendChild(sl[1]);
  276. setTimeout('$(cur_frm.frm_head.dn_area).addClass("background-fade-in")\
  277. .css("background-color", "white")', 1500)
  278. }
  279. */