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.
 
 
 
 
 
 

232 lines
6.7 KiB

  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  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.appframe.clear_buttons();
  14. this.set_title();
  15. if(!this.frm.view_is_edit) {
  16. // print view
  17. this.show_print_toolbar();
  18. }
  19. this.show_title_as_dirty();
  20. if(this.frm.meta.hide_toolbar) {
  21. this.frm.save_disabled = true;
  22. return;
  23. }
  24. this.make_file_menu();
  25. this.show_infobar();
  26. },
  27. refresh: function() {
  28. this.make();
  29. },
  30. set_title: function() {
  31. var title = this.frm.docname;
  32. if(title.length > 30) {
  33. title = title.substr(0,30) + "...";
  34. }
  35. this.appframe.set_title(title, wn._(this.frm.docname));
  36. this.appframe.set_sub_title(wn._(this.frm.doctype));
  37. },
  38. show_infobar: function() {
  39. /* docs:
  40. Render info bar that shows timestamp, # of comments, # of attachments etc.
  41. only if saved doc. (doc.__islocal is falsy)
  42. */
  43. if(this.infobar)
  44. this.infobar.refresh();
  45. else
  46. this.infobar = new wn.ui.form.InfoBar({appframe:this.appframe, frm:this.frm});
  47. },
  48. show_print_toolbar: function() {
  49. var me = this;
  50. this.appframe.add_button("Edit", function() {
  51. me.frm.edit_doc();
  52. return false;
  53. }, 'icon-pencil')
  54. this.frm.$print_view_select =
  55. this.appframe.add_select("Print Format", this.frm.print_formats)
  56. .val(this.frm.print_formats[0])
  57. .change(function() {
  58. me.frm.refresh_print_layout();
  59. });
  60. },
  61. get_dropdown_menu: function(label) {
  62. return this.appframe.add_dropdown(label);
  63. },
  64. set_docstatus_label: function() {
  65. var status_bar_parent = this.frm.appframe.$w.find(".status-bar").empty();
  66. if(this.frm.meta.is_submittable && !this.frm.doc.__islocal) {
  67. var status_bar = $("<h4>")
  68. .css({"margin": "0px", "margin-top": "-15px"})
  69. .appendTo(status_bar_parent);
  70. switch(this.frm.doc.docstatus) {
  71. case 0:
  72. $('<span class="label"><i class="icon-unlock"> To Submit</span>')
  73. .appendTo(status_bar);
  74. break;
  75. case 1:
  76. $('<span class="label label-success"><i class="icon-lock"> Submitted</span>')
  77. .appendTo(status_bar);
  78. break;
  79. case 2:
  80. $('<span class="label label-danger"><i class="icon-remove"> Cancelled</span>')
  81. .appendTo(status_bar);
  82. break;
  83. }
  84. }
  85. },
  86. make_file_menu: function() {
  87. var me = this;
  88. var p = this.frm.perm[0];
  89. var docstatus = cint(this.frm.doc.docstatus);
  90. // New
  91. if(p[CREATE]) {
  92. this.appframe.add_dropdown_button("File", wn._("New") + " "
  93. + wn._(me.frm.doctype), function() {
  94. new_doc(me.frm.doctype);}, 'icon-plus');
  95. }
  96. // Save
  97. if(docstatus==0 && p[WRITE] && !this.read_only) {
  98. this.appframe.add_dropdown_button("File", wn._("Save"), function() {
  99. me.frm.save('Save', null, this);}, 'icon-save');
  100. }
  101. // Print
  102. if(!(me.frm.doc.__islocal || me.frm.meta.allow_print)) {
  103. this.appframe.add_dropdown_button("File", wn._("Print..."), function() {
  104. me.frm.print_doc();}, 'icon-print');
  105. }
  106. // email
  107. if(!(me.frm.doc.__islocal || me.frm.meta.allow_email)) {
  108. this.appframe.add_dropdown_button("File", wn._("Email..."), function() {
  109. me.frm.email_doc();}, 'icon-envelope');
  110. }
  111. // Linked With
  112. if(!me.frm.doc.__islocal && !me.frm.meta.issingle) {
  113. this.appframe.add_dropdown_button("File", wn._('Linked With'), function() {
  114. if(!me.frm.linked_with) {
  115. me.frm.linked_with = new wn.ui.form.LinkedWith({
  116. frm: me.frm
  117. });
  118. }
  119. me.frm.linked_with.show();
  120. }, "icon-link")
  121. }
  122. // copy
  123. if(in_list(profile.can_create, me.frm.doctype) && !me.frm.meta.allow_copy) {
  124. this.appframe.add_dropdown_button("File", wn._("Copy"), function() {
  125. me.frm.copy_doc();}, 'icon-file');
  126. }
  127. // rename
  128. if(me.frm.meta.allow_rename && me.frm.perm[0][WRITE]) {
  129. this.appframe.add_dropdown_button("File", wn._("Rename..."), function() {
  130. me.frm.rename_doc();}, 'icon-retweet');
  131. }
  132. // delete
  133. if((cint(me.frm.doc.docstatus) != 1) && !me.frm.doc.__islocal
  134. && wn.model.can_delete(me.frm.doctype)) {
  135. this.appframe.add_dropdown_button("File", wn._("Delete"), function() {
  136. me.frm.savetrash();}, 'icon-remove-sign');
  137. }
  138. },
  139. set_title_button: function() {
  140. var me = this;
  141. var docstatus = cint(this.frm.doc.docstatus);
  142. var p = this.frm.perm[0];
  143. var has_workflow = wn.model.get("Workflow", {document_type: me.frm.doctype}).length;
  144. // remove existing title buttons
  145. this.appframe.toolbar.find(".btn-title").remove();
  146. // Print Preview
  147. if(this.frm.meta.read_only_onload && !this.frm.doc.__islocal && this.frm.view_is_edit) {
  148. this.appframe.add_button(wn._('Preview'), function() {
  149. me.frm.last_view_is_edit[me.frm.docname] = 0;
  150. me.frm.refresh(); }, 'icon-print');
  151. }
  152. if(has_workflow && (this.frm.doc.__islocal || this.frm.doc.__unsaved)) {
  153. this.make_save_button();
  154. } else if(!has_workflow) {
  155. if(docstatus==0 && p[SUBMIT] && (!me.frm.doc.__islocal)
  156. && (!me.frm.doc.__unsaved)) {
  157. this.appframe.add_button('Submit', function() {
  158. me.frm.savesubmit(this);}, 'icon-lock', true)
  159. .removeClass("btn-default")
  160. .addClass("btn-primary");
  161. }
  162. else if(docstatus==0) {
  163. this.make_save_button();
  164. }
  165. else if(docstatus==1 && p[CANCEL]) {
  166. this.appframe.add_button('Cancel', function() {
  167. me.frm.savecancel(this) }, 'icon-remove');
  168. }
  169. else if(docstatus==2 && p[AMEND]) {
  170. this.appframe.add_button('Amend', function() {
  171. me.frm.amend_doc() }, 'icon-pencil', true);
  172. }
  173. }
  174. },
  175. make_save_button: function() {
  176. if(this.frm.save_disabled)
  177. return;
  178. var me = this;
  179. this.appframe.add_button('Save', function() {
  180. me.frm.save('Save', null, this);}, 'icon-save', true)
  181. .removeClass("btn-default")
  182. .addClass("btn-primary");
  183. },
  184. add_update_button_on_dirty: function() {
  185. var me = this;
  186. $(this.frm.wrapper).on("dirty", function() {
  187. me.show_title_as_dirty();
  188. // show update button if unsaved
  189. var docstatus = cint(me.frm.doc.docstatus);
  190. if(docstatus==1 && me.frm.perm[0][SUBMIT]
  191. && !me.appframe.$w.find(".action-update").length) {
  192. me.appframe.add_button("Update", function() {
  193. me.frm.save('Update', null, me);
  194. }, 'icon-save', true)
  195. .removeClass("btn-default")
  196. .addClass("btn-primary action-update");
  197. }
  198. })
  199. },
  200. show_title_as_dirty: function() {
  201. if(this.frm.save_disabled)
  202. return;
  203. this.appframe.get_title_area()
  204. .toggleClass("text-warning", this.frm.doc.__unsaved ? true : false);
  205. this.set_title_button();
  206. this.set_docstatus_label();
  207. },
  208. make_actions_menu: function() {
  209. if(this.actions_setup) return;
  210. var menu = this.get_dropdown_menu("Actions");
  211. this.actions_setup = true;
  212. }
  213. })