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.
 
 
 
 
 
 

268 lines
7.5 KiB

  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. // MIT License. See license.txt
  3. wn.provide("wn.ui.form");
  4. wn.ui.form.Toolbar = Class.extend({
  5. init: function(opts) {
  6. $.extend(this, opts);
  7. this.make();
  8. this.add_update_button_on_dirty();
  9. this.appframe.add_module_icon(this.frm.meta.module, this.frm.doctype);
  10. this.appframe.set_views_for(this.frm.meta.name, "form");
  11. },
  12. make: function() {
  13. this.set_title();
  14. this.show_title_as_dirty();
  15. if(this.frm.meta.hide_toolbar) {
  16. this.appframe.set_title_right();
  17. this.frm.save_disabled = true;
  18. this.appframe.iconbar.hide();
  19. } else {
  20. this.appframe.iconbar.clear(1)
  21. this.make_file_menu();
  22. this.make_cancel_amend_button();
  23. this.set_title_right();
  24. this.show_infobar();
  25. if(this.frm.doc.__islocal) {
  26. this.appframe.iconbar.hide(2);
  27. this.appframe.iconbar.hide(3);
  28. } else {
  29. this.appframe.iconbar.show(2);
  30. this.appframe.iconbar.show(3);
  31. }
  32. }
  33. },
  34. refresh: function() {
  35. this.make();
  36. },
  37. set_title: function() {
  38. if(this.frm.meta.title_field) {
  39. var title = this.frm.doc[this.frm.meta.title_field] || wn._(this.frm.docname);
  40. } else {
  41. var title = wn._(this.frm.docname);
  42. }
  43. var me = this;
  44. this.appframe.set_title(title);
  45. if(this.frm.meta.issingle) {
  46. this.appframe.set_title_left('<i class="icon-angle-left"></i> ' + wn._(this.frm.meta.module),
  47. function() { wn.set_route(wn.modules[me.frm.meta.module].link); });
  48. } else {
  49. this.appframe.set_title_left('<i class="icon-angle-left"></i> ' + wn._(this.frm.doctype),
  50. function() { wn.set_route("List", me.frm.doctype); });
  51. }
  52. },
  53. show_infobar: function() {
  54. /* docs:
  55. Render info bar that shows timestamp, # of comments, # of attachments etc.
  56. only if saved doc. (doc.__islocal is falsy)
  57. */
  58. if(this.infobar)
  59. this.infobar.refresh();
  60. else
  61. this.infobar = new wn.ui.form.InfoBar({appframe:this.appframe, frm:this.frm});
  62. },
  63. get_dropdown_menu: function(label) {
  64. return this.appframe.add_dropdown(label);
  65. },
  66. get_lock_status: function() {
  67. if(this.frm.meta.is_submittable && !this.frm.doc.__islocal) {
  68. switch(this.frm.doc.docstatus) {
  69. case 0:
  70. return ' <i class="icon-unlock text-muted" title="Not Submitted">';
  71. case 1:
  72. return ' <i class="icon-lock text-primary" title="Submitted">';
  73. case 2:
  74. return ' <i class="icon-remove text-danger" title="Cancelled">';
  75. }
  76. } else {
  77. return "";
  78. }
  79. },
  80. make_file_menu: function() {
  81. var me = this;
  82. var p = this.frm.perm[0];
  83. var docstatus = cint(this.frm.doc.docstatus);
  84. $(".custom-menu").remove();
  85. // New
  86. if(p[CREATE]) {
  87. this.appframe.add_dropdown_button("File", wn._("New") + " "
  88. + wn._(me.frm.doctype), function() {
  89. new_doc(me.frm.doctype);}, 'icon-plus');
  90. }
  91. // Save
  92. if(docstatus==0 && p[WRITE] && !this.read_only) {
  93. this.appframe.add_dropdown_button("File", wn._("Save"), function() {
  94. me.frm.save('Save', null, this);}, 'icon-save');
  95. }
  96. // Submit
  97. if(docstatus==0 && !this.frm.doc.__unsaved && p[SUBMIT] && !this.read_only) {
  98. this.appframe.add_dropdown_button("File", wn._("Submit"), function() {
  99. me.frm.savesubmit(this);}, 'icon-lock');
  100. }
  101. // Cancel
  102. if(this.can_cancel()) {
  103. this.appframe.add_dropdown_button("File", wn._("Cancel"), function() {
  104. me.frm.savecancel(this);}, 'icon-remove');
  105. }
  106. // Amend
  107. if(this.can_amend()) {
  108. this.appframe.add_dropdown_button("File", wn._("Amend"), function() {
  109. me.frm.amend_doc();}, 'icon-pencil');
  110. }
  111. // Print
  112. if(!(me.frm.doc.__islocal || me.frm.meta.allow_print)) {
  113. this.appframe.add_dropdown_button("File", wn._("Print..."), function() {
  114. me.frm.print_doc();}, 'icon-print');
  115. }
  116. // email
  117. if(!(me.frm.doc.__islocal || me.frm.meta.allow_email)) {
  118. this.appframe.add_dropdown_button("File", wn._("Email..."), function() {
  119. me.frm.email_doc();}, 'icon-envelope');
  120. }
  121. // Linked With
  122. if(!me.frm.doc.__islocal && !me.frm.meta.issingle) {
  123. this.appframe.add_dropdown_button("File", wn._('Linked With'), function() {
  124. me.show_linked_with();
  125. }, "icon-link")
  126. }
  127. // copy
  128. if(in_list(profile.can_create, me.frm.doctype) && !me.frm.meta.allow_copy) {
  129. this.appframe.add_dropdown_button("File", wn._("Copy"), function() {
  130. me.frm.copy_doc();}, 'icon-file');
  131. }
  132. // rename
  133. if(me.frm.meta.allow_rename && me.frm.perm[0].write) {
  134. this.appframe.add_dropdown_button("File", wn._("Rename..."), function() {
  135. me.frm.rename_doc();}, 'icon-retweet');
  136. }
  137. // delete
  138. if((cint(me.frm.doc.docstatus) != 1) && !me.frm.doc.__islocal
  139. && wn.model.can_delete(me.frm.doctype)) {
  140. this.appframe.add_dropdown_button("File", wn._("Delete"), function() {
  141. me.frm.savetrash();}, 'icon-remove-sign');
  142. }
  143. },
  144. can_save: function() {
  145. return this.get_docstatus()===0;
  146. },
  147. can_submit: function() {
  148. return this.get_docstatus()===0
  149. && !this.frm.doc.__islocal
  150. && !this.frm.doc.__unsaved
  151. && this.frm.perm[0].submit
  152. && !this.has_workflow();
  153. },
  154. can_update: function() {
  155. return this.get_docstatus()===1
  156. && !this.frm.doc.__islocal
  157. && this.frm.perm[0].submit
  158. && this.frm.doc.__unsaved
  159. },
  160. can_cancel: function() {
  161. return this.get_docstatus()===1
  162. && this.frm.perm[0].cancel
  163. && !this.read_only
  164. && !this.has_workflow();
  165. },
  166. can_amend: function() {
  167. return this.get_docstatus()===2
  168. && this.frm.perm[0].amend
  169. && !this.read_only;
  170. },
  171. has_workflow: function() {
  172. if(this._has_workflow === undefined)
  173. this._has_workflow = wn.model.get("Workflow", {document_type: this.frm.doctype}).length;
  174. return this._has_workflow;
  175. },
  176. get_docstatus: function() {
  177. return cint(this.frm.doc.docstatus);
  178. },
  179. show_linked_with: function() {
  180. if(!this.frm.linked_with) {
  181. this.frm.linked_with = new wn.ui.form.LinkedWith({
  182. frm: this.frm
  183. });
  184. }
  185. this.frm.linked_with.show();
  186. },
  187. set_title_right: function() {
  188. var me = this,
  189. current = this.appframe.get_title_right_text(),
  190. status = null;
  191. if(this.can_submit()) {
  192. status = "Submit";
  193. } else if(this.can_save()) {
  194. if(!this.frm.save_disabled) {
  195. status = "Save";
  196. }
  197. } else if(this.can_update()) {
  198. status = "Update";
  199. } else if(this.can_cancel()) {
  200. status = "Cancel";
  201. } else if(this.can_amend()) {
  202. status = "Amend";
  203. }
  204. if(status) {
  205. if(status!==current) {
  206. this.appframe.set_title_right(status, {
  207. "Save": function() { me.frm.save('Save', null, this); },
  208. "Submit": function() { me.frm.savesubmit(this); },
  209. "Update": function() { me.frm.save('Update', null, this); },
  210. "Cancel": function() { me.frm.savecancel(this); },
  211. "Amend": function() { me.frm.amend_doc(); }
  212. }[status], null, status==="Cancel" ? "btn-default" : "btn-primary");
  213. }
  214. } else {
  215. this.appframe.set_title_right();
  216. }
  217. },
  218. make_cancel_amend_button: function() {
  219. var me = this;
  220. var docstatus = cint(this.frm.doc.docstatus);
  221. var p = this.frm.perm[0];
  222. var has_workflow = this.has_workflow();
  223. if(has_workflow) {
  224. return;
  225. } else if(docstatus==1 && p[CANCEL]) {
  226. this.appframe.add_button('Cancel', function() {
  227. me.frm.savecancel(this) }, 'icon-remove');
  228. } else if(docstatus==2 && p[AMEND]) {
  229. this.appframe.add_button('Amend', function() {
  230. me.frm.amend_doc() }, 'icon-pencil', true);
  231. }
  232. },
  233. add_update_button_on_dirty: function() {
  234. var me = this;
  235. $(this.frm.wrapper).on("dirty", function() {
  236. me.show_title_as_dirty();
  237. })
  238. },
  239. show_title_as_dirty: function() {
  240. if(this.frm.save_disabled)
  241. return;
  242. this.appframe.get_title_area()
  243. .toggleClass("text-warning", this.frm.doc.__unsaved ? true : false)
  244. this.set_title();
  245. this.set_title_right();
  246. }
  247. })