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.

gantt.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. * Redmine - project management software
  3. * Copyright (C) 2006- Jean-Philippe Lang
  4. * This code is released under the GNU General Public License.
  5. */
  6. var draw_gantt = null;
  7. var draw_top;
  8. var draw_right;
  9. var draw_left;
  10. var rels_stroke_width = 2;
  11. function setDrawArea() {
  12. draw_top = $("#gantt_draw_area").position().top;
  13. draw_right = $("#gantt_draw_area").width();
  14. draw_left = $("#gantt_area").scrollLeft();
  15. }
  16. function getRelationsArray() {
  17. var arr = new Array();
  18. $.each($('div.task_todo[data-rels]'), function(index_div, element) {
  19. if(!$(element).is(':visible')) return true;
  20. var element_id = $(element).attr("id");
  21. if (element_id != null) {
  22. var issue_id = element_id.replace("task-todo-issue-", "");
  23. var data_rels = $(element).data("rels");
  24. for (rel_type_key in data_rels) {
  25. $.each(data_rels[rel_type_key], function(index_issue, element_issue) {
  26. arr.push({issue_from: issue_id, issue_to: element_issue,
  27. rel_type: rel_type_key});
  28. });
  29. }
  30. }
  31. });
  32. return arr;
  33. }
  34. function drawRelations() {
  35. var arr = getRelationsArray();
  36. $.each(arr, function(index_issue, element_issue) {
  37. var issue_from = $("#task-todo-issue-" + element_issue["issue_from"]);
  38. var issue_to = $("#task-todo-issue-" + element_issue["issue_to"]);
  39. if (issue_from.length == 0 || issue_to.length == 0) {
  40. return;
  41. }
  42. var issue_height = issue_from.height();
  43. var issue_from_top = issue_from.position().top + (issue_height / 2) - draw_top;
  44. var issue_from_right = issue_from.position().left + issue_from.width();
  45. var issue_to_top = issue_to.position().top + (issue_height / 2) - draw_top;
  46. var issue_to_left = issue_to.position().left;
  47. var color = issue_relation_type[element_issue["rel_type"]]["color"];
  48. var landscape_margin = issue_relation_type[element_issue["rel_type"]]["landscape_margin"];
  49. var issue_from_right_rel = issue_from_right + landscape_margin;
  50. var issue_to_left_rel = issue_to_left - landscape_margin;
  51. draw_gantt.path(["M", issue_from_right + draw_left, issue_from_top,
  52. "L", issue_from_right_rel + draw_left, issue_from_top])
  53. .attr({stroke: color,
  54. "stroke-width": rels_stroke_width
  55. });
  56. if (issue_from_right_rel < issue_to_left_rel) {
  57. draw_gantt.path(["M", issue_from_right_rel + draw_left, issue_from_top,
  58. "L", issue_from_right_rel + draw_left, issue_to_top])
  59. .attr({stroke: color,
  60. "stroke-width": rels_stroke_width
  61. });
  62. draw_gantt.path(["M", issue_from_right_rel + draw_left, issue_to_top,
  63. "L", issue_to_left + draw_left, issue_to_top])
  64. .attr({stroke: color,
  65. "stroke-width": rels_stroke_width
  66. });
  67. } else {
  68. var issue_middle_top = issue_to_top +
  69. (issue_height *
  70. ((issue_from_top > issue_to_top) ? 1 : -1));
  71. draw_gantt.path(["M", issue_from_right_rel + draw_left, issue_from_top,
  72. "L", issue_from_right_rel + draw_left, issue_middle_top])
  73. .attr({stroke: color,
  74. "stroke-width": rels_stroke_width
  75. });
  76. draw_gantt.path(["M", issue_from_right_rel + draw_left, issue_middle_top,
  77. "L", issue_to_left_rel + draw_left, issue_middle_top])
  78. .attr({stroke: color,
  79. "stroke-width": rels_stroke_width
  80. });
  81. draw_gantt.path(["M", issue_to_left_rel + draw_left, issue_middle_top,
  82. "L", issue_to_left_rel + draw_left, issue_to_top])
  83. .attr({stroke: color,
  84. "stroke-width": rels_stroke_width
  85. });
  86. draw_gantt.path(["M", issue_to_left_rel + draw_left, issue_to_top,
  87. "L", issue_to_left + draw_left, issue_to_top])
  88. .attr({stroke: color,
  89. "stroke-width": rels_stroke_width
  90. });
  91. }
  92. draw_gantt.path(["M", issue_to_left + draw_left, issue_to_top,
  93. "l", -4 * rels_stroke_width, -2 * rels_stroke_width,
  94. "l", 0, 4 * rels_stroke_width, "z"])
  95. .attr({stroke: "none",
  96. fill: color,
  97. "stroke-linecap": "butt",
  98. "stroke-linejoin": "miter"
  99. });
  100. });
  101. }
  102. function getProgressLinesArray() {
  103. var arr = new Array();
  104. var today_left = $('#today_line').position().left;
  105. arr.push({left: today_left, top: 0});
  106. $.each($('div.issue-subject, div.version-name'), function(index, element) {
  107. if(!$(element).is(':visible')) return true;
  108. var t = $(element).position().top - draw_top ;
  109. var h = ($(element).height() / 9);
  110. var element_top_upper = t - h;
  111. var element_top_center = t + (h * 3);
  112. var element_top_lower = t + (h * 8);
  113. var issue_closed = $(element).children('span').hasClass('issue-closed');
  114. var version_closed = $(element).children('span').hasClass('version-closed');
  115. if (issue_closed || version_closed) {
  116. arr.push({left: today_left, top: element_top_center});
  117. } else {
  118. var issue_done = $("#task-done-" + $(element).attr("id"));
  119. var is_behind_start = $(element).children('span').hasClass('behind-start-date');
  120. var is_over_end = $(element).children('span').hasClass('over-end-date');
  121. if (is_over_end) {
  122. arr.push({left: draw_right, top: element_top_upper, is_right_edge: true});
  123. arr.push({left: draw_right, top: element_top_lower, is_right_edge: true, none_stroke: true});
  124. } else if (issue_done.length > 0) {
  125. var done_left = issue_done.first().position().left +
  126. issue_done.first().width();
  127. arr.push({left: done_left, top: element_top_center});
  128. } else if (is_behind_start) {
  129. arr.push({left: 0 , top: element_top_upper, is_left_edge: true});
  130. arr.push({left: 0 , top: element_top_lower, is_left_edge: true, none_stroke: true});
  131. } else {
  132. var todo_left = today_left;
  133. var issue_todo = $("#task-todo-" + $(element).attr("id"));
  134. if (issue_todo.length > 0){
  135. todo_left = issue_todo.first().position().left;
  136. }
  137. arr.push({left: Math.min(today_left, todo_left), top: element_top_center});
  138. }
  139. }
  140. });
  141. return arr;
  142. }
  143. function drawGanttProgressLines() {
  144. var arr = getProgressLinesArray();
  145. var color = $("#today_line")
  146. .css("border-left-color");
  147. var i;
  148. for(i = 1 ; i < arr.length ; i++) {
  149. if (!("none_stroke" in arr[i]) &&
  150. (!("is_right_edge" in arr[i - 1] && "is_right_edge" in arr[i]) &&
  151. !("is_left_edge" in arr[i - 1] && "is_left_edge" in arr[i]))
  152. ) {
  153. var x1 = (arr[i - 1].left == 0) ? 0 : arr[i - 1].left + draw_left;
  154. var x2 = (arr[i].left == 0) ? 0 : arr[i].left + draw_left;
  155. draw_gantt.path(["M", x1, arr[i - 1].top,
  156. "L", x2, arr[i].top])
  157. .attr({stroke: color, "stroke-width": 2});
  158. }
  159. }
  160. }
  161. function drawSelectedColumns(){
  162. if ($("#draw_selected_columns").prop('checked')) {
  163. if(isMobile()) {
  164. $('td.gantt_selected_column').each(function(i) {
  165. $(this).hide();
  166. });
  167. }else{
  168. $('.gantt_subjects_container').addClass('draw_selected_columns');
  169. $('td.gantt_selected_column').each(function() {
  170. $(this).show();
  171. var column_name = $(this).attr('id');
  172. $(this).resizable({
  173. alsoResize: '.gantt_' + column_name + '_container, .gantt_' + column_name + '_container > .gantt_hdr',
  174. minWidth: 20,
  175. handles: "e",
  176. create: function() {
  177. $(".ui-resizable-e").css("cursor","ew-resize");
  178. }
  179. }).on('resize', function (e) {
  180. e.stopPropagation();
  181. });
  182. });
  183. }
  184. }else{
  185. $('td.gantt_selected_column').each(function (i) {
  186. $(this).hide();
  187. $('.gantt_subjects_container').removeClass('draw_selected_columns');
  188. });
  189. }
  190. }
  191. function drawGanttHandler() {
  192. var folder = document.getElementById('gantt_draw_area');
  193. if(draw_gantt != null)
  194. draw_gantt.clear();
  195. else
  196. draw_gantt = Raphael(folder);
  197. setDrawArea();
  198. drawSelectedColumns();
  199. if ($("#draw_progress_line").prop('checked'))
  200. try{drawGanttProgressLines();}catch(e){}
  201. if ($("#draw_relations").prop('checked'))
  202. drawRelations();
  203. $('#content').addClass('gantt_content');
  204. }
  205. function resizableSubjectColumn(){
  206. $('.issue-subject, .project-name, .version-name').each(function(){
  207. $(this).width($(".gantt_subjects_column").width()-$(this).position().left);
  208. });
  209. $('td.gantt_subjects_column').resizable({
  210. alsoResize: '.gantt_subjects_container, .gantt_subjects_container>.gantt_hdr, .project-name, .issue-subject, .version-name',
  211. minWidth: 100,
  212. handles: 'e',
  213. create: function( event, ui ) {
  214. $('.ui-resizable-e').css('cursor','ew-resize');
  215. }
  216. }).on('resize', function (e) {
  217. e.stopPropagation();
  218. });
  219. if(isMobile()) {
  220. $('td.gantt_subjects_column').resizable('disable');
  221. }else{
  222. $('td.gantt_subjects_column').resizable('enable');
  223. };
  224. }
  225. ganttEntryClick = function(e){
  226. var icon_expander = e.target;
  227. var subject = $(icon_expander.parentElement);
  228. var subject_left = parseInt(subject.css('left')) + parseInt(icon_expander.offsetWidth);
  229. var target_shown = null;
  230. var target_top = 0;
  231. var total_height = 0;
  232. var out_of_hierarchy = false;
  233. var iconChange = null;
  234. if(subject.hasClass('open'))
  235. iconChange = function(element){
  236. $(element).find('.expander').switchClass('icon-expanded', 'icon-collapsed');
  237. $(element).removeClass('open');
  238. };
  239. else
  240. iconChange = function(element){
  241. $(element).find('.expander').switchClass('icon-collapsed', 'icon-expanded');
  242. $(element).addClass('open');
  243. };
  244. iconChange(subject);
  245. subject.nextAll('div').each(function(_, element){
  246. var el = $(element);
  247. var json = el.data('collapse-expand');
  248. var number_of_rows = el.data('number-of-rows');
  249. var el_task_bars = '#gantt_area form > div[data-collapse-expand="' + json.obj_id + '"][data-number-of-rows="' + number_of_rows + '"]';
  250. var el_selected_columns = 'td.gantt_selected_column div[data-collapse-expand="' + json.obj_id + '"][data-number-of-rows="' + number_of_rows + '"]';
  251. if(out_of_hierarchy || parseInt(el.css('left')) <= subject_left){
  252. out_of_hierarchy = true;
  253. if(target_shown == null) return false;
  254. var new_top_val = parseInt(el.css('top')) + total_height * (target_shown ? -1 : 1);
  255. el.css('top', new_top_val);
  256. $([el_task_bars, el_selected_columns].join()).each(function(_, el){
  257. $(el).css('top', new_top_val);
  258. });
  259. return true;
  260. }
  261. var is_shown = el.is(':visible');
  262. if(target_shown == null){
  263. target_shown = is_shown;
  264. target_top = parseInt(el.css('top'));
  265. total_height = 0;
  266. }
  267. if(is_shown == target_shown){
  268. $(el_task_bars).each(function(_, task) {
  269. var el_task = $(task);
  270. if(!is_shown)
  271. el_task.css('top', target_top + total_height);
  272. if(!el_task.hasClass('tooltip'))
  273. el_task.toggle(!is_shown);
  274. });
  275. $(el_selected_columns).each(function (_, attr) {
  276. var el_attr = $(attr);
  277. if (!is_shown)
  278. el_attr.css('top', target_top + total_height);
  279. el_attr.toggle(!is_shown);
  280. });
  281. if(!is_shown)
  282. el.css('top', target_top + total_height);
  283. iconChange(el);
  284. el.toggle(!is_shown);
  285. total_height += parseInt(json.top_increment);
  286. }
  287. });
  288. drawGanttHandler();
  289. };
  290. function disable_unavailable_columns(unavailable_columns) {
  291. $.each(unavailable_columns, function (index, value) {
  292. $('#available_c, #selected_c').children("[value='" + value + "']").prop('disabled', true);
  293. });
  294. }