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.
 
 
 
 
 
 

180 lines
4.5 KiB

  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. // MIT License. See license.txt
  3. wn.provide("wn.views.calendar");
  4. wn.views.GanttFactory = wn.views.Factory.extend({
  5. make: function(route) {
  6. var me = this;
  7. wn.model.with_doctype(route[1], function() {
  8. var page = me.make_page();
  9. $(page).on("show", function() {
  10. me.set_filters_from_route_options();
  11. });
  12. var options = {
  13. doctype: route[1],
  14. page: page
  15. };
  16. $.extend(options, wn.views.calendar[route[1]] || {});
  17. page.ganttview = new wn.views.Gantt(options);
  18. });
  19. }
  20. });
  21. wn.views.Gantt = Class.extend({
  22. init: function(opts) {
  23. $.extend(this, opts);
  24. wn.require('assets/webnotes/js/lib/jQuery.Gantt/css/style.css');
  25. wn.require('assets/webnotes/js/lib/jQuery.Gantt/js/jquery.fn.gantt.js');
  26. this.make_page();
  27. wn.route_options ?
  28. this.set_filters_from_route_options() :
  29. this.refresh();
  30. },
  31. make_page: function() {
  32. var module = locals.DocType[this.doctype].module,
  33. me = this;
  34. this.appframe = this.page.appframe;
  35. this.appframe.set_title(wn._("Gantt Chart") + " - " + wn._(this.doctype));
  36. this.appframe.add_module_icon(module)
  37. this.appframe.set_views_for(this.doctype, "gantt");
  38. this.appframe.set_title_right("Refresh",
  39. function() { me.refresh(); }, "icon-refresh")
  40. this.appframe.add_field({fieldtype:"Date", label:"From",
  41. fieldname:"start", "default": wn.datetime.month_start(), input_css: {"z-index": 3}});
  42. this.appframe.add_field({fieldtype:"Date", label:"To",
  43. fieldname:"end", "default": wn.datetime.month_end(), input_css: {"z-index": 3}});
  44. if(this.filters) {
  45. $.each(this.filters, function(i, df) {
  46. me.appframe.add_field(df);
  47. return false;
  48. });
  49. }
  50. },
  51. refresh: function() {
  52. var parent = $(this.page)
  53. .find(".layout-main")
  54. .empty()
  55. .css('min-height', '300px')
  56. .html('<div class="alert alert-info">Loading...</div>');
  57. var me = this;
  58. return wn.call({
  59. method: this.get_events_method,
  60. type: "GET",
  61. args: {
  62. doctype: this.doctype,
  63. start: this.appframe.fields_dict.start.get_parsed_value(),
  64. end: this.appframe.fields_dict.end.get_parsed_value(),
  65. filters: this.get_filters()
  66. },
  67. callback: function(r) {
  68. $(parent).empty();
  69. if(!r.message || !r.message.length) {
  70. $(parent).html('<div class="alert alert-info">' + wn._('Nothing to show for this selection') + '</div>');
  71. } else {
  72. var gantt_area = $('<div class="gantt">').appendTo(parent);
  73. gantt_area.gantt({
  74. source: me.get_source(r),
  75. navigate: "scroll",
  76. scale: "days",
  77. minScale: "hours",
  78. maxScale: "months",
  79. onItemClick: function(data) {
  80. wn.set_route('Form', me.doctype, data.name);
  81. },
  82. onAddClick: function(dt, rowId) {
  83. newdoc(me.doctype);
  84. }
  85. });
  86. }
  87. }
  88. })
  89. },
  90. set_filter: function(doctype, value) {
  91. var me = this;
  92. if(this.filters) {
  93. $.each(this.filters, function(i, df) {
  94. if(df.options===value)
  95. me.appframe.fields_dict[df.fieldname].set_input(value);
  96. return false;
  97. });
  98. }
  99. },
  100. get_filters: function() {
  101. var filter_vals = {},
  102. me = this;
  103. if(this.filters) {
  104. $.each(this.filters, function(i, df) {
  105. filter_vals[df.fieldname || df.label] =
  106. me.appframe.fields_dict[df.fieldname || df.label].get_parsed_value();
  107. });
  108. }
  109. return filter_vals;
  110. },
  111. get_source: function(r) {
  112. var source = [],
  113. me = this;
  114. // projects
  115. $.each(r.message, function(i,v) {
  116. // standardize values
  117. $.each(me.field_map, function(target, source) {
  118. v[target] = v[source];
  119. });
  120. if(v.start && !v.end) {
  121. v.end = new Date(v.start)
  122. v.end.setHours(v.end.getHours() + 1);
  123. }
  124. if(v.start && v.end) {
  125. source.push({
  126. name: v.title,
  127. desc: v.status,
  128. values: [{
  129. name: v.title,
  130. desc: v.title + "<br>" + v.status,
  131. from: '/Date("'+v.start+'")/',
  132. to: '/Date("'+v.end+'")/',
  133. customClass: {
  134. 'danger':'ganttRed',
  135. 'warning':'ganttOrange',
  136. 'info':'ganttBlue',
  137. 'success':'ganttGreen',
  138. '':'ganttGray'
  139. }[me.style_map ?
  140. me.style_map[v.status] :
  141. wn.utils.guess_style(v.status, "standard")],
  142. dataObj: v
  143. }]
  144. })
  145. }
  146. });
  147. return source
  148. },
  149. set_filters_from_route_options: function() {
  150. var me = this;
  151. if(wn.route_options) {
  152. $.each(wn.route_options, function(k, value) {
  153. if(me.appframe.fields_dict[k]) {
  154. me.appframe.fields_dict[k].set_input(value);
  155. me.refresh();
  156. return false;
  157. };
  158. })
  159. wn.route_options = null;
  160. }
  161. }
  162. });