Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

1145 Zeilen
41 KiB

  1. /*
  2. Copyright (c) 2012-2017 Open Lab
  3. Written by Roberto Bicchierai and Silvia Chelazzi http://roberto.open-lab.com
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. todo For compatibility with IE and SVGElements.getElementsByClassName not implemented changed every find starting from SVGElement (the other works fine)
  21. .find(".classname")) -> .find("[class*=classname])
  22. */
  23. function Ganttalendar(zoom, startmillis, endMillis, master, minGanttSize) {
  24. this.master = master; // is the a GantEditor instance
  25. this.element; // is the jquery element containing gantt
  26. this.svg; // instance of svg object containing gantt
  27. this.tasksGroup; //instance of svg group containing tasks
  28. this.linksGroup; //instance of svg group containing links
  29. this.zoom = zoom;
  30. this.minGanttSize = minGanttSize;
  31. this.includeToday = true; //when true today is always visible. If false boundaries comes from tasks periods
  32. this.showCriticalPath = false; //when true critical path is highlighted
  33. this.zoomLevels = [ "d", "w","w2","w3", "m","m2", "q", "q2", "s", "y"];
  34. this.element = this.create(zoom, startmillis, endMillis);
  35. this.linkOnProgress = false; //set to true when creating a new link
  36. this.rowHeight = 30; // todo get it from css?
  37. this.taskHeight=20;
  38. this.taskVertOffset=(this.rowHeight-this.taskHeight)/2;
  39. }
  40. Ganttalendar.prototype.zoomGantt = function (isPlus) {
  41. var curLevel = this.zoom;
  42. var pos = this.zoomLevels.indexOf(curLevel + "");
  43. var centerMillis=this.getCenterMillis();
  44. var newPos = pos;
  45. if (isPlus) {
  46. newPos = pos <= 0 ? 0 : pos - 1;
  47. } else {
  48. newPos = pos >= this.zoomLevels.length - 1 ? this.zoomLevels.length - 1 : pos + 1;
  49. }
  50. if (newPos != pos) {
  51. curLevel = this.zoomLevels[newPos];
  52. this.zoom = curLevel;
  53. this.refreshGantt();
  54. this.goToMillis(centerMillis);
  55. }
  56. };
  57. Ganttalendar.prototype.create = function (zoom, originalStartmillis, originalEndMillis) {
  58. //console.debug("Gantt.create " + new Date(originalStartmillis) + " - " + new Date(originalEndMillis));
  59. var self = this;
  60. function getPeriod(zoomLevel, stMil, endMillis) {
  61. var start = new Date(stMil);
  62. var end = new Date(endMillis);
  63. start.setHours(0, 0, 0, 0);
  64. end.setHours(23, 59, 59, 999);
  65. //reset hours
  66. if (zoomLevel == "d") {
  67. start.setFirstDayOfThisWeek();
  68. end.setFirstDayOfThisWeek();
  69. end.setDate(end.getDate() + 6);
  70. //reset day of week
  71. } else if (zoomLevel == "w" ) {
  72. start.setFirstDayOfThisWeek();
  73. start.setDate(start.getDate()-7);
  74. end.setFirstDayOfThisWeek();
  75. end.setDate(end.getDate() + 13);
  76. } else if (zoomLevel == "w2" ) {
  77. start.setFirstDayOfThisWeek();
  78. start.setDate(start.getDate()-7);
  79. end.setFirstDayOfThisWeek();
  80. end.setDate(end.getDate() + 20);
  81. } else if (zoomLevel == "w3" ) {
  82. start.setFirstDayOfThisWeek();
  83. start.setDate(start.getDate()-7);
  84. end.setFirstDayOfThisWeek();
  85. end.setDate(end.getDate() + 27);
  86. //reset day of month
  87. } else if (zoomLevel == "m") {
  88. start.setDate(1);
  89. start.setMonth(start.getMonth()-1);
  90. end.setDate(1);
  91. end.setMonth(end.getMonth() + 2);
  92. end.setDate(end.getDate() - 1);
  93. } else if (zoomLevel == "m2") {
  94. start.setDate(1);
  95. start.setMonth(start.getMonth()-1);
  96. end.setDate(1);
  97. end.setMonth(end.getMonth() + 3);
  98. end.setDate(end.getDate() - 1);
  99. //reset to day of week
  100. } else if (zoomLevel == "q") {
  101. start.setDate(start.getDate()-start.getDay()+1); //ISO 8601 counts week of year starting on Moday
  102. start.setDate(start.getDate()-7);
  103. end.setFirstDayOfThisWeek();
  104. end.setDate(end.getDate() + 13);
  105. //reset to quarter
  106. } else if (zoomLevel == "q2") {
  107. start.setDate(1);
  108. start.setMonth(Math.floor(start.getMonth() / 3) * 3);
  109. start.setMonth(start.getMonth()-3);
  110. end.setDate(1);
  111. end.setMonth(Math.floor(end.getMonth() / 3) * 3 + 6);
  112. end.setDate(end.getDate() - 1);
  113. //reset to semester
  114. } else if (zoomLevel == "s") {
  115. start.setDate(1);
  116. start.setMonth(Math.floor(start.getMonth() / 6) * 6);
  117. start.setMonth(start.getMonth()-6);
  118. end.setDate(1);
  119. end.setMonth(Math.floor(end.getMonth() / 6) * 6 + 12);
  120. end.setDate(end.getDate() - 1);
  121. //reset to year - > gen
  122. } else if (zoomLevel == "y") {
  123. start.setDate(1);
  124. start.setMonth(0);
  125. start.setFullYear(start.getFullYear()-1);
  126. end.setDate(1);
  127. end.setMonth(24);
  128. end.setDate(end.getDate() - 1);
  129. }
  130. return {start:start.getTime(), end:end.getTime()};
  131. }
  132. function createHeadCell(lbl, span, additionalClass, width) {
  133. var th = $("<th>").html(lbl).attr("colSpan", span);
  134. if (width)
  135. th.width(width);
  136. if (additionalClass)
  137. th.addClass(additionalClass);
  138. return th;
  139. }
  140. function createBodyCell(span, isEnd, additionalClass) {
  141. var ret = $("<td>").html("").attr("colSpan", span).addClass("ganttBodyCell");
  142. if (isEnd)
  143. ret.addClass("end");
  144. if (additionalClass)
  145. ret.addClass(additionalClass);
  146. return ret;
  147. }
  148. function createGantt(zoom, startPeriod, endPeriod) {
  149. var tr1 = $("<tr>").addClass("ganttHead1");
  150. var tr2 = $("<tr>").addClass("ganttHead2");
  151. var trBody = $("<tr>").addClass("ganttBody");
  152. function iterate(renderFunction1, renderFunction2) {
  153. var start = new Date(startPeriod);
  154. //loop for header1
  155. while (start.getTime() <= endPeriod) {
  156. renderFunction1(start);
  157. }
  158. //loop for header2
  159. start = new Date(startPeriod);
  160. while (start.getTime() <= endPeriod) {
  161. renderFunction2(start);
  162. }
  163. }
  164. //this is computed by hand in order to optimize cell size
  165. var computedTableWidth;
  166. var computedScaleX;
  167. // year
  168. if (zoom == "y") {
  169. computedScaleX=100/(3600000 * 24*180); //1 sem= 100px
  170. iterate(function (date) {
  171. tr1.append(createHeadCell(date.format("yyyy"), 2));
  172. date.setFullYear(date.getFullYear() + 1);
  173. }, function (date) {
  174. var end = new Date(date.getTime());
  175. end.setMonth(end.getMonth() + 6);
  176. var periodWidth=(end.getTime()-date.getTime())*computedScaleX;
  177. var sem = (Math.floor(date.getMonth() / 6) + 1);
  178. tr2.append(createHeadCell(GanttMaster.messages["GANTT_SEMESTER_SHORT"] + sem, 1, null, periodWidth));
  179. trBody.append(createBodyCell(1, sem == 2));
  180. date.setMonth(date.getMonth() + 6);
  181. });
  182. //semester
  183. } else if (zoom == "s") {
  184. computedScaleX=200/(3600000 * 24*90); //1 quarter= 200px
  185. iterate(function (date) {
  186. var end = new Date(date.getTime());
  187. end.setMonth(end.getMonth() + 6);
  188. end.setDate(end.getDate() - 1);
  189. tr1.append(createHeadCell(date.format("MMMM") + " - " + end.format("MMMM yyyy"), 6));
  190. date.setMonth(date.getMonth() + 6);
  191. }, function (date) {
  192. var end = new Date(date.getTime());
  193. end.setMonth(end.getMonth() + 1);
  194. var periodWidth=(end.getTime()-date.getTime())*computedScaleX;
  195. tr2.append(createHeadCell(date.format("MMM"), 1, null, periodWidth));
  196. trBody.append(createBodyCell(1, (date.getMonth()+1) % 6 == 0));
  197. date.setMonth(date.getMonth() + 1);
  198. });
  199. //quarter
  200. } else if (zoom == "q2") {
  201. computedScaleX=150/(3600000 * 24*30); //1 month= 150px
  202. iterate(function (date) {
  203. var end = new Date(date.getTime());
  204. end.setMonth(end.getMonth() + 3);
  205. end.setDate(end.getDate() - 1);
  206. tr1.append(createHeadCell(date.format("MMMM") + " - " + end.format("MMMM yyyy"), 3));
  207. date.setMonth(date.getMonth() + 3);
  208. }, function (date) {
  209. var end = new Date(date.getTime());
  210. end.setMonth(end.getMonth() + 1);
  211. var periodWidth=(end.getTime()-date.getTime())*computedScaleX;
  212. var lbl = date.format("MMMM");
  213. tr2.append(createHeadCell(lbl, 1, null, periodWidth));
  214. trBody.append(createBodyCell(1, date.getMonth() % 3 == 2));
  215. date.setMonth(date.getMonth() + 1);
  216. });
  217. // quarter / week of year
  218. } else if (zoom == "q") {
  219. computedScaleX=300/(3600000 * 24*30); //1 month= 300px
  220. iterate(function (date) {
  221. var end = new Date(date.getTime());
  222. end.setMonth(end.getMonth() + 3);
  223. end.setDate(end.getDate() - 1);
  224. tr1.append(createHeadCell(date.format("MMMM") + " - " + end.format("MMMM yyyy"), Math.round((end.getTime()-date.getTime())/(3600000*24))));
  225. date.setMonth(date.getMonth() + 3);
  226. }, function (date) {
  227. var end = new Date(date.getTime());
  228. end.setDate(end.getDate() + 7);
  229. var periodWidth=(end.getTime()-date.getTime())*computedScaleX;
  230. var lbl ="<small>"+i18n["WEEK_SHORT"].toLowerCase()+"</small> "+ date.format("w");
  231. tr2.append(createHeadCell(lbl, 7, null, periodWidth));
  232. trBody.append(createBodyCell(7,false));
  233. date.setDate(date.getDate() + 7);
  234. });
  235. //month
  236. } else if (zoom == "m2") {
  237. computedScaleX=15/(3600000 * 24); //1 day= 15px
  238. iterate(function (date) {
  239. var sm = date.getTime();
  240. date.setMonth(date.getMonth() + 1);
  241. var daysInMonth = Math.round((date.getTime() - sm) / (3600000 * 24));
  242. tr1.append(createHeadCell(new Date(sm).format("MMMM yyyy"), daysInMonth)); //spans mumber of dayn in the month
  243. }, function (date) {
  244. var end = new Date(date.getTime());
  245. end.setDate(end.getDate() + 1);
  246. var periodWidth=(end.getTime()-date.getTime())*computedScaleX;
  247. tr2.append(createHeadCell(date.format("d"), 1, isHoliday(date) ? "holyH headSmall" : "headSmall", periodWidth));
  248. var nd = new Date(date.getTime());
  249. nd.setDate(date.getDate() + 1);
  250. trBody.append(createBodyCell(1, nd.getDate() == 1, isHoliday(date) ? "holy" : null));
  251. date.setDate(date.getDate() + 1);
  252. });
  253. } else if (zoom == "m") {
  254. computedScaleX=25/(3600000 * 24); //1 day= 25px
  255. iterate(function (date) {
  256. var sm = date.getTime();
  257. date.setMonth(date.getMonth() + 1);
  258. var daysInMonth = Math.round((date.getTime() - sm) / (3600000 * 24));
  259. tr1.append(createHeadCell(new Date(sm).format("MMMM yyyy"), daysInMonth)); //spans mumber of dayn in the month
  260. }, function (date) {
  261. var end = new Date(date.getTime());
  262. end.setDate(end.getDate() + 1);
  263. var periodWidth=(end.getTime()-date.getTime())*computedScaleX;
  264. tr2.append(createHeadCell(date.format("d"), 1, isHoliday(date) ? "holyH" : null, periodWidth));
  265. var nd = new Date(date.getTime());
  266. nd.setDate(date.getDate() + 1);
  267. trBody.append(createBodyCell(1, nd.getDate() == 1, isHoliday(date) ? "holy" : null));
  268. date.setDate(date.getDate() + 1);
  269. });
  270. //week
  271. } else if (zoom == "w3") {
  272. computedScaleX=30/(3600000 * 24); //1 day= 30px
  273. iterate(function (date) {
  274. var end = new Date(date.getTime());
  275. end.setDate(end.getDate() + 6);
  276. tr1.append(createHeadCell(date.format("MMM d") + " - " + end.format("MMM d 'yy"), 7));
  277. date.setDate(date.getDate() + 7);
  278. }, function (date) {
  279. var end = new Date(date.getTime());
  280. end.setDate(end.getDate() + 1);
  281. var periodWidth=(end.getTime()-date.getTime())*computedScaleX;
  282. tr2.append(createHeadCell(date.format("EEEE").substr(0, 1), 1, isHoliday(date) ? "holyH" : null, periodWidth));
  283. trBody.append(createBodyCell(1, date.getDay() % 7 == (self.master.firstDayOfWeek + 6) % 7, isHoliday(date) ? "holy" : null));
  284. date.setDate(date.getDate() + 1);
  285. });
  286. } else if (zoom == "w2") {
  287. computedScaleX=40/(3600000 * 24); //1 day= 40px
  288. iterate(function (date) {
  289. var end = new Date(date.getTime());
  290. end.setDate(end.getDate() + 6);
  291. tr1.append(createHeadCell(date.format("MMM d") + " - " + end.format("MMM d 'yy"), 7));
  292. date.setDate(date.getDate() + 7);
  293. }, function (date) {
  294. var end = new Date(date.getTime());
  295. end.setDate(end.getDate() + 1);
  296. var periodWidth=(end.getTime()-date.getTime())*computedScaleX;
  297. tr2.append(createHeadCell(date.format("EEEE").substr(0, 1), 1, isHoliday(date) ? "holyH" : null, periodWidth));
  298. trBody.append(createBodyCell(1, date.getDay() % 7 == (self.master.firstDayOfWeek + 6) % 7, isHoliday(date) ? "holy" : null));
  299. date.setDate(date.getDate() + 1);
  300. });
  301. } else if (zoom == "w") {
  302. computedScaleX=50/(3600000 * 24);//1 day= 50px
  303. iterate(function (date) {
  304. var end = new Date(date.getTime());
  305. end.setDate(end.getDate() + 6);
  306. tr1.append(createHeadCell(date.format("MMM d") + " - " + end.format("MMM d 'yy"), 7));
  307. date.setDate(date.getDate() + 7);
  308. }, function (date) {
  309. var end = new Date(date.getTime());
  310. end.setDate(end.getDate() + 1);
  311. var periodWidth=(end.getTime()-date.getTime())*computedScaleX;
  312. tr2.append(createHeadCell(date.format("EEEE").substr(0, 1), 1, isHoliday(date) ? "holyH" : null, periodWidth));
  313. trBody.append(createBodyCell(1, date.getDay() % 7 == (self.master.firstDayOfWeek + 6) % 7, isHoliday(date) ? "holy" : null));
  314. date.setDate(date.getDate() + 1);
  315. });
  316. //days
  317. } else if (zoom == "d") {
  318. computedScaleX=100/(3600000 * 24);//1 day= 100px
  319. iterate(function (date) {
  320. var end = new Date(date.getTime());
  321. end.setDate(end.getDate() + 6);
  322. tr1.append(createHeadCell(date.format("MMMM d") + " - " + end.format("MMMM d yyyy"), 7));
  323. date.setDate(date.getDate() + 7);
  324. }, function (date) {
  325. var end = new Date(date.getTime());
  326. end.setDate(end.getDate() + 1);
  327. var periodWidth=(end.getTime()-date.getTime())*computedScaleX;
  328. tr2.append(createHeadCell(date.format("EEE d"), 1, isHoliday(date) ? "holyH" : null, periodWidth));
  329. trBody.append(createBodyCell(1, date.getDay() % 7 == (self.master.firstDayOfWeek + 6) % 7, isHoliday(date) ? "holy" : null));
  330. date.setDate(date.getDate() + 1);
  331. });
  332. } else {
  333. console.error("Wrong level " + zoom);
  334. }
  335. computedTableWidth = (endPeriod - startPeriod)*computedScaleX;
  336. //set a minimal width
  337. computedTableWidth = Math.max(computedTableWidth, self.minGanttSize);
  338. var table = $("<table cellspacing=0 cellpadding=0>");
  339. table.append(tr1).append(tr2); // removed as on FF there are rounging issues //.css({width:computedTableWidth});
  340. var head = table.clone().addClass("ganttFixHead");
  341. table.append(trBody).addClass("ganttTable");
  342. var height = self.master.editor.element.height();
  343. table.height(height);
  344. var box = $("<div>");
  345. box.addClass("gantt unselectable").attr("unselectable", "true").css({position:"relative", width:computedTableWidth});
  346. box.append(table);
  347. box.append(head);
  348. //create the svg
  349. box.svg({settings:{class:"ganttSVGBox"},
  350. onLoad: function (svg) {
  351. //console.debug("svg loaded", svg);
  352. //creates gradient and definitions
  353. var defs = svg.defs('myDefs');
  354. //create backgound_self.master.resourceUrl +"hasExternalDeps.png",
  355. var extDep = svg.pattern(defs, "extDep", 0, 0, 10, 10, 0, 0, 10, 10, {patternUnits:'userSpaceOnUse'});
  356. var img = svg.image(extDep, 0, 0, 10, 10, "/Content/jquerygantt/res/hasExternalDeps.png", { opacity: .3 });
  357. self.svg = svg;
  358. $(svg).addClass("ganttSVGBox");
  359. //creates grid group
  360. var gridGroup = svg.group("gridGroup");
  361. //creates rows grid
  362. for (var i = 40; i <= height; i += self.rowHeight)
  363. //svg.line(gridGroup, 0, i, "100%", i, {class:"ganttLinesSVG"});
  364. svg.rect(gridGroup, 0, i, "100%",self.rowHeight, {class:"ganttLinesSVG"});
  365. //creates links group
  366. self.linksGroup = svg.group("linksGroup");
  367. //creates tasks group
  368. self.tasksGroup = svg.group("tasksGroup");
  369. //compute scalefactor fx
  370. //self.fx = computedTableWidth / (endPeriod - startPeriod);
  371. self.fx = computedScaleX;
  372. // drawTodayLine
  373. if (new Date().getTime() > self.startMillis && new Date().getTime() < self.endMillis) {
  374. var x = Math.round(((new Date().getTime()) - self.startMillis) * self.fx);
  375. svg.line(gridGroup, x, 0, x, "100%", {class:"ganttTodaySVG"});
  376. }
  377. }
  378. });
  379. return box;
  380. }
  381. //if include today synch extremes
  382. if (this.includeToday) {
  383. var today = new Date().getTime();
  384. originalStartmillis = originalStartmillis > today ? today : originalStartmillis;
  385. originalEndMillis = originalEndMillis < today ? today : originalEndMillis;
  386. }
  387. //get best dimension fo gantt
  388. var period = getPeriod(zoom, originalStartmillis, originalEndMillis); //this is enlarged to match complete periods basing on zoom level
  389. //console.debug(new Date(period.start) + " " + new Date(period.end));
  390. self.startMillis = period.start; //real dimension of gantt
  391. self.endMillis = period.end;
  392. self.originalStartMillis = originalStartmillis; //minimal dimension required by user or by task duration
  393. self.originalEndMillis = originalEndMillis;
  394. var table = createGantt(zoom, period.start, period.end);
  395. return table;
  396. };
  397. //<%-------------------------------------- GANT TASK GRAPHIC ELEMENT --------------------------------------%>
  398. Ganttalendar.prototype.drawTask = function (task) {
  399. //console.debug("drawTask", task.name,new Date(task.start));
  400. var self = this;
  401. //var prof = new Profiler("ganttDrawTask");
  402. editorRow = task.rowElement;
  403. var top = editorRow.position().top + editorRow.offsetParent().scrollTop();
  404. //var normStart=Math.round(task.start/(3600000*24))*(3600000*24)
  405. //var normX = Math.round((normStart - self.startMillis) * self.fx);
  406. var x = Math.round((task.start - self.startMillis) * self.fx);
  407. //console.debug(x,normX)
  408. task.hasChild = task.isParent();
  409. var taskBox = $(_createTaskSVG(task, {x:x, y:top+self.taskVertOffset, width:Math.round((task.end - task.start) * self.fx),height:self.taskHeight}));
  410. task.ganttElement = taskBox;
  411. if (self.showCriticalPath && task.isCritical)
  412. taskBox.addClass("critical");
  413. if (this.master.permissions.canWrite && task.canWrite) {
  414. //bind all events on taskBox
  415. taskBox
  416. .click(function (e) { // manages selection
  417. e.stopPropagation();// to avoid body remove focused
  418. self.element.find("[class*=focused]").removeClass("focused");
  419. $(".ganttSVGBox .focused").removeClass("focused");
  420. var el = $(this);
  421. if (!self.resDrop)
  422. el.addClass("focused");
  423. self.resDrop = false; //hack to avoid select
  424. $("body").off("click.focused").one("click.focused", function () {
  425. $(".ganttSVGBox .focused").removeClass("focused");
  426. })
  427. }).dblclick(function () {
  428. self.master.showTaskEditor($(this).attr("taskid"));
  429. }).mouseenter(function () {
  430. //bring to top
  431. var el = $(this);
  432. if (!self.linkOnProgress) {
  433. el.find("[class*=linkHandleSVG]").show();
  434. } else {
  435. el.addClass("linkOver");
  436. }
  437. }).mouseleave(function () {
  438. var el = $(this);
  439. el.removeClass("linkOver").find("[class*=linkHandleSVG]").hide();
  440. }).mouseup(function (e) {
  441. $(":focus").blur(); // in order to save grid field when moving task
  442. }).mousedown(function () {
  443. var task = self.master.getTask($(this).attr("taskid"));
  444. task.rowElement.click();
  445. }).dragExtedSVG($(self.svg.root()), {
  446. canResize: this.master.permissions.canWrite && task.canWrite,
  447. canDrag: !task.depends && this.master.permissions.canWrite && task.canWrite,
  448. startDrag: function (e) {
  449. $(".ganttSVGBox .focused").removeClass("focused");
  450. },
  451. drag: function (e) {
  452. $("[from=" + task.id + "],[to=" + task.id + "]").trigger("update");
  453. },
  454. drop: function (e) {
  455. self.resDrop = true; //hack to avoid select
  456. var taskbox = $(this);
  457. var task = self.master.getTask(taskbox.attr("taskid"));
  458. var s = Math.round((parseFloat(taskbox.attr("x")) / self.fx) + self.startMillis);
  459. self.master.beginTransaction();
  460. self.master.moveTask(task, new Date(s));
  461. self.master.endTransaction();
  462. },
  463. startResize:function (e) {
  464. //console.debug("startResize");
  465. $(".ganttSVGBox .focused").removeClass("focused");
  466. var taskbox = $(this);
  467. var text = $(self.svg.text(parseInt(taskbox.attr("x")) + parseInt(taskbox.attr("width") + 8), parseInt(taskbox.attr("y")), "", {"font-size":"10px", "fill":"red"}));
  468. taskBox.data("textDur", text);
  469. },
  470. resize: function (e) {
  471. //find and update links from, to
  472. var taskbox = $(this);
  473. var st = Math.round((parseFloat(taskbox.attr("x")) / self.fx) + self.startMillis);
  474. var en = Math.round(((parseFloat(taskbox.attr("x")) + parseFloat(taskbox.attr("width"))) / self.fx) + self.startMillis);
  475. var d = computeStartDate(st).distanceInWorkingDays(computeEndDate(en))+1;
  476. var text = taskBox.data("textDur");
  477. text.attr("x", parseInt(taskbox.attr("x")) + parseInt(taskbox.attr("width")) + 8).html(d);
  478. $("[from=" + task.id + "],[to=" + task.id + "]").trigger("update");
  479. },
  480. stopResize: function (e) {
  481. self.resDrop = true; //hack to avoid select
  482. //console.debug(ui)
  483. var textBox = taskBox.data("textDur");
  484. if (textBox)
  485. textBox.remove();
  486. var taskbox = $(this);
  487. var task = self.master.getTask(taskbox.attr("taskid"));
  488. var st = Math.round((parseFloat(taskbox.attr("x")) / self.fx) + self.startMillis);
  489. var en = Math.round(((parseFloat(taskbox.attr("x")) + parseFloat(taskbox.attr("width"))) / self.fx) + self.startMillis);
  490. self.master.beginTransaction();
  491. self.master.changeTaskDates(task, new Date(st), new Date(en));
  492. self.master.endTransaction();
  493. }
  494. });
  495. //binding for creating link
  496. taskBox.find("[class*=linkHandleSVG]").mousedown(function (e) {
  497. e.preventDefault();
  498. e.stopPropagation();
  499. var taskBox = $(this).closest(".taskBoxSVG");
  500. var svg = $(self.svg.root());
  501. var offs = svg.offset();
  502. self.linkOnProgress = true;
  503. self.linkFromEnd = $(this).is(".taskLinkEndSVG");
  504. svg.addClass("linkOnProgress");
  505. // create the line
  506. var startX = parseFloat(taskBox.attr("x")) + (self.linkFromEnd ? parseFloat(taskBox.attr("width")) : 0);
  507. var startY = parseFloat(taskBox.attr("y")) + parseFloat(taskBox.attr("height")) / 2;
  508. var line = self.svg.line(startX, startY, e.pageX - offs.left - 5, e.pageY - offs.top - 5, {class:"linkLineSVG"});
  509. var circle = self.svg.circle(startX, startY, 5, {class:"linkLineSVG"});
  510. //bind mousemove to draw a line
  511. svg.bind("mousemove.linkSVG", function (e) {
  512. var offs = svg.offset();
  513. var nx = e.pageX - offs.left;
  514. var ny = e.pageY - offs.top;
  515. var c = Math.sqrt(Math.pow(nx - startX, 2) + Math.pow(ny - startY, 2));
  516. nx = nx - (nx - startX) * 10 / c;
  517. ny = ny - (ny - startY) * 10 / c;
  518. self.svg.change(line, { x2:nx, y2:ny});
  519. self.svg.change(circle, { cx:nx, cy:ny});
  520. });
  521. //bind mouseup un body to stop
  522. $("body").one("mouseup.linkSVG", function (e) {
  523. $(line).remove();
  524. $(circle).remove();
  525. self.linkOnProgress = false;
  526. svg.removeClass("linkOnProgress");
  527. $(self.svg.root()).unbind("mousemove.linkSVG");
  528. var targetBox = $(e.target).closest(".taskBoxSVG");
  529. //console.debug("create link from " + taskBox.attr("taskid") + " to " + targetBox.attr("taskid"));
  530. if (targetBox && targetBox.attr("taskid") != taskBox.attr("taskid")) {
  531. var taskTo;
  532. var taskFrom;
  533. if (self.linkFromEnd) {
  534. taskTo = self.master.getTask(targetBox.attr("taskid"));
  535. taskFrom = self.master.getTask(taskBox.attr("taskid"));
  536. } else {
  537. taskFrom = self.master.getTask(targetBox.attr("taskid"));
  538. taskTo = self.master.getTask(taskBox.attr("taskid"));
  539. }
  540. if (taskTo && taskFrom) {
  541. var gap = 0;
  542. var depInp = taskTo.rowElement.find("[name=depends]");
  543. depInp.val(depInp.val() + ((depInp.val() + "").length > 0 ? "," : "") + (taskFrom.getRow() + 1) + (gap != 0 ? ":" + gap : ""));
  544. depInp.blur();
  545. }
  546. }
  547. })
  548. });
  549. }
  550. //ask for redraw link
  551. self.redrawLinks();
  552. //prof.stop();
  553. function _createTaskSVG(task, dimensions) {
  554. var svg = self.svg;
  555. var taskSvg = svg.svg(self.tasksGroup, dimensions.x, dimensions.y, dimensions.width, dimensions.height, {class:"taskBox taskBoxSVG taskStatusSVG", status:task.status, taskid:task.id });
  556. //svg.title(taskSvg, task.name);
  557. //external box
  558. var layout = svg.rect(taskSvg, 0, 0, "100%", "100%", {class:"taskLayout", rx:"2", ry:"2"});
  559. //svg.rect(taskSvg, 0, 0, "100%", "100%", {fill:"rgba(255,255,255,.3)"});
  560. //external dep
  561. if (task.hasExternalDep)
  562. svg.rect(taskSvg, 0, 0, "100%", "100%", {fill:"url(#extDep)"});
  563. //progress
  564. if (task.progress > 0) {
  565. var progress = svg.rect(taskSvg, 0, "20%", (task.progress > 100 ? 100 : task.progress) + "%", "60%", {rx:"2", ry:"2",fill:"rgba(0,0,0,.4)"});
  566. if (dimensions.width > 50) {
  567. var textStyle = {fill:"#888", "font-size":"10px",class:"textPerc teamworkIcons",transform:"translate(5)"};
  568. if (task.progress > 100)
  569. textStyle["font-weight"]="bold";
  570. if (task.progress > 90)
  571. textStyle.transform = "translate(-40)";
  572. svg.text(taskSvg, (task.progress > 90 ? 100 : task.progress) + "%", (self.rowHeight-5)/2, (task.progress>100?"!!! ":"")+ task.progress + "%", textStyle);
  573. }
  574. }
  575. if (task.hasChild)
  576. svg.rect(taskSvg, 0, 0, "100%", 3, {fill:"#000"});
  577. if (task.startIsMilestone) {
  578. svg.image(taskSvg, -9, dimensions.height / 2 - 9, 18, 18, "/Content/jquerygantt/res/milestone.png")
  579. }
  580. if (task.endIsMilestone) {
  581. svg.image(taskSvg, "100%", dimensions.height / 2 - 9, 18, 18, "/Content/jquerygantt/res/milestone.png", { transform: "translate(-9)" })
  582. }
  583. //task label
  584. svg.text(taskSvg, "100%", 18, task.name, {class:"taskLabelSVG", transform:"translate(20,-5)"});
  585. //link tool
  586. if (task.level>0){
  587. svg.circle(taskSvg, "0", dimensions.height/2,dimensions.height/3, {class:"taskLinkStartSVG linkHandleSVG", transform:"translate("+(-dimensions.height/3+1)+")"});
  588. svg.circle(taskSvg, "100%",dimensions.height/2,dimensions.height/3, {class:"taskLinkEndSVG linkHandleSVG", transform:"translate("+(dimensions.height/3-1)+")"});
  589. }
  590. return taskSvg
  591. }
  592. };
  593. Ganttalendar.prototype.addTask = function (task) {
  594. //set new boundaries for gantt
  595. this.originalEndMillis = this.originalEndMillis > task.end ? this.originalEndMillis : task.end;
  596. this.originalStartMillis = this.originalStartMillis < task.start ? this.originalStartMillis : task.start;
  597. };
  598. //<%-------------------------------------- GANT DRAW LINK SVG ELEMENT --------------------------------------%>
  599. //'from' and 'to' are tasks already drawn
  600. Ganttalendar.prototype.drawLink = function (from, to, type) {
  601. var self = this;
  602. //console.debug("drawLink")
  603. var peduncolusSize = 10;
  604. /**
  605. * Given an item, extract its rendered position
  606. * width and height into a structure.
  607. */
  608. function buildRect(item) {
  609. var p = item.ganttElement.position();
  610. var rect = {
  611. left: parseFloat(item.ganttElement.attr("x")),
  612. top: parseFloat(item.ganttElement.attr("y")),
  613. width: parseFloat(item.ganttElement.attr("width")),
  614. height:parseFloat(item.ganttElement.attr("height"))
  615. };
  616. return rect;
  617. }
  618. /**
  619. * The default rendering method, which paints a start to end dependency.
  620. */
  621. function drawStartToEnd(from, to, ps) {
  622. var svg = self.svg;
  623. //this function update an existing link
  624. function update() {
  625. var group = $(this);
  626. var from = group.data("from");
  627. var to = group.data("to");
  628. var rectFrom = buildRect(from);
  629. var rectTo = buildRect(to);
  630. var fx1 = rectFrom.left;
  631. var fx2 = rectFrom.left + rectFrom.width;
  632. var fy = rectFrom.height / 2 + rectFrom.top;
  633. var tx1 = rectTo.left;
  634. var tx2 = rectTo.left + rectTo.width;
  635. var ty = rectTo.height / 2 + rectTo.top;
  636. var tooClose = tx1 < fx2 + 2 * ps;
  637. var r = 5; //radius
  638. var arrowOffset = 5;
  639. var up = fy > ty;
  640. var fup = up ? -1 : 1;
  641. var prev = fx2 + 2 * ps > tx1;
  642. var fprev = prev ? -1 : 1;
  643. var image = group.find("image");
  644. var p = svg.createPath();
  645. if (tooClose) {
  646. var firstLine = fup * (rectFrom.height / 2 - 2 * r + 2);
  647. p.move(fx2, fy)
  648. .line(ps, 0, true)
  649. .arc(r, r, 90, false, !up, r, fup * r, true)
  650. .line(0, firstLine, true)
  651. .arc(r, r, 90, false, !up, -r, fup * r, true)
  652. .line(fprev * 2 * ps + (tx1 - fx2), 0, true)
  653. .arc(r, r, 90, false, up, -r, fup * r, true)
  654. .line(0, (Math.abs(ty - fy) - 4 * r - Math.abs(firstLine)) * fup - arrowOffset, true)
  655. .arc(r, r, 90, false, up, r, fup * r, true)
  656. .line(ps, 0, true);
  657. image.attr({x:tx1 - 5, y:ty - 5 - arrowOffset});
  658. } else {
  659. p.move(fx2, fy)
  660. .line((tx1 - fx2) / 2 - r, 0, true)
  661. .arc(r, r, 90, false, !up, r, fup * r, true)
  662. .line(0, ty - fy - fup * 2 * r + arrowOffset, true)
  663. .arc(r, r, 90, false, up, r, fup * r, true)
  664. .line((tx1 - fx2) / 2 - r, 0, true);
  665. image.attr({x:tx1 - 5, y:ty - 5 + arrowOffset});
  666. }
  667. group.find("path").attr({d:p.path()});
  668. }
  669. // create the group
  670. var group = svg.group(self.linksGroup, "" + from.id + "-" + to.id);
  671. svg.title(group, from.name + " -> " + to.name);
  672. var p = svg.createPath();
  673. //add the arrow
  674. svg.image(group, 0, 0, 5, 10, "/Content/jquerygantt/res/linkArrow.png");
  675. //create empty path
  676. svg.path(group, p, {class:"taskLinkPathSVG"});
  677. //set "from" and "to" to the group, bind "update" and trigger it
  678. var jqGroup = $(group).data({from:from, to:to }).attr({from:from.id, to:to.id}).on("update", update).trigger("update");
  679. if (self.showCriticalPath && from.isCritical && to.isCritical)
  680. jqGroup.addClass("critical");
  681. jqGroup.addClass("linkGroup");
  682. return jqGroup;
  683. }
  684. /**
  685. * A rendering method which paints a start to start dependency.
  686. */
  687. function drawStartToStart(from, to) {
  688. console.error("StartToStart not supported on SVG");
  689. var rectFrom = buildRect(from);
  690. var rectTo = buildRect(to);
  691. }
  692. var link;
  693. // Dispatch to the correct renderer
  694. if (type == 'start-to-start') {
  695. link = drawStartToStart(from, to, peduncolusSize);
  696. } else {
  697. link = drawStartToEnd(from, to, peduncolusSize);
  698. }
  699. if (this.master.permissions.canWrite && (from.canWrite || to.canWrite)) {
  700. link.click(function (e) {
  701. var el = $(this);
  702. e.stopPropagation();// to avoid body remove focused
  703. self.element.find("[class*=focused]").removeClass("focused");
  704. $(".ganttSVGBox .focused").removeClass("focused");
  705. var el = $(this);
  706. if (!self.resDrop)
  707. el.addClass("focused");
  708. self.resDrop = false; //hack to avoid select
  709. $("body").off("click.focused").one("click.focused", function () {
  710. $(".ganttSVGBox .focused").removeClass("focused");
  711. })
  712. });
  713. }
  714. };
  715. Ganttalendar.prototype.redrawLinks = function () {
  716. //console.debug("redrawLinks ");
  717. var self = this;
  718. this.element.stopTime("ganttlnksredr");
  719. this.element.oneTime(60, "ganttlnksredr", function () {
  720. //var prof=new Profiler("gd_drawLink_real");
  721. //remove all links
  722. $("#linksSVG").empty();
  723. var collapsedDescendant = [];
  724. //[expand]
  725. var collapsedDescendant = self.master.getCollapsedDescendant();
  726. for (var i = 0; i < self.master.links.length; i++) {
  727. var link = self.master.links[i];
  728. if (collapsedDescendant.indexOf(link.from) >= 0 || collapsedDescendant.indexOf(link.to) >= 0) continue;
  729. self.drawLink(link.from, link.to);
  730. }
  731. //prof.stop();
  732. });
  733. };
  734. Ganttalendar.prototype.reset = function () {
  735. this.element.find("[class*=linkGroup]").remove();
  736. this.element.find("[taskid]").remove();
  737. };
  738. Ganttalendar.prototype.redrawTasks = function () {
  739. //[expand]
  740. var collapsedDescendant = this.master.getCollapsedDescendant();
  741. for (var i = 0; i < this.master.tasks.length; i++) {
  742. var task = this.master.tasks[i];
  743. if (collapsedDescendant.indexOf(task) >= 0) continue;
  744. this.drawTask(task);
  745. }
  746. };
  747. Ganttalendar.prototype.refreshGantt = function () {
  748. //console.debug("refreshGantt")
  749. if (this.showCriticalPath) {
  750. this.master.computeCriticalPath();
  751. }
  752. var par = this.element.parent();
  753. //try to maintain last scroll
  754. var scrollY = par.scrollTop();
  755. var scrollX = par.scrollLeft();
  756. this.element.remove();
  757. //guess the zoom level in base of period
  758. if (!this.zoom) {
  759. var days = Math.round((this.originalEndMillis - this.originalStartMillis) / (3600000 * 24));
  760. //"d", "w","w2","w3", "m","m2", "q", "s", "y"
  761. this.zoom = this.zoomLevels[days < 2 ? 0 : (days < 15 ? 1 : (days < 30 ? 2 : (days < 45 ? 3 : (days < 60 ? 4 : (days < 90 ? 5 : (days < 180 ? 6 : (days < 600 ? 7 : 8 ) ) ) ) ) ) )];
  762. }
  763. var domEl = this.create(this.zoom, this.originalStartMillis, this.originalEndMillis);
  764. this.element = domEl;
  765. par.append(domEl);
  766. this.redrawTasks();
  767. //set old scroll
  768. //console.debug("old scroll:",scrollX,scrollY)
  769. par.scrollTop(scrollY);
  770. par.scrollLeft(scrollX);
  771. //set current task
  772. this.synchHighlight();
  773. };
  774. Ganttalendar.prototype.fitGantt = function () {
  775. delete this.zoom;
  776. this.refreshGantt();
  777. };
  778. Ganttalendar.prototype.synchHighlight = function () {
  779. //console.debug("synchHighlight",this.master.currentTask);
  780. if (this.master.currentTask ){
  781. // take care of collapsed rows
  782. var ganttHighLighterPosition=this.master.editor.element.find(".taskEditRow:visible").index(this.master.currentTask.rowElement);
  783. this.master.gantt.element.find(".ganttLinesSVG").removeClass("rowSelected").eq(ganttHighLighterPosition).addClass("rowSelected");
  784. } else {
  785. $(".rowSelected").removeClass("rowSelected"); // todo non c'era
  786. }
  787. };
  788. Ganttalendar.prototype.getCenterMillis= function () {
  789. return parseInt((this.element.parent().scrollLeft()+this.element.parent().width()/2)/this.fx+this.startMillis);
  790. };
  791. Ganttalendar.prototype.goToMillis= function (millis) {
  792. var x = Math.round(((millis) - this.startMillis) * this.fx) -this.element.parent().width()/2;
  793. this.element.parent().scrollLeft(x);
  794. };
  795. Ganttalendar.prototype.centerOnToday = function () {
  796. this.goToMillis(new Date().getTime());
  797. };
  798. /**
  799. * Allows drag and drop and extesion of task boxes. Only works on x axis
  800. * @param opt
  801. * @return {*}
  802. */
  803. $.fn.dragExtedSVG = function (svg, opt) {
  804. //doing this can work with one svg at once only
  805. var target;
  806. var svgX;
  807. var offsetMouseRect;
  808. var options = {
  809. canDrag: true,
  810. canResize: true,
  811. resizeZoneWidth:10,
  812. minSize: 10,
  813. startDrag: function (e) {},
  814. drag: function (e) {},
  815. drop: function (e) {},
  816. startResize: function (e) {},
  817. resize: function (e) {},
  818. stopResize: function (e) {}
  819. };
  820. $.extend(options, opt);
  821. this.each(function () {
  822. var el = $(this);
  823. svgX = svg.parent().offset().left; //parent is used instead of svg for a Firefox oddity
  824. if (options.canDrag)
  825. el.addClass("deSVGdrag");
  826. if (options.canResize || options.canDrag) {
  827. el.bind("mousedown.deSVG",function (e) {
  828. //console.debug("mousedown.deSVG");
  829. if ($(e.target).is("image")) {
  830. e.preventDefault();
  831. }
  832. target = $(this);
  833. var x1 = parseFloat(el.find("[class*=taskLayout]").offset().left);
  834. var x2 = x1 + parseFloat(el.attr("width"));
  835. var posx = e.pageX;
  836. $("body").unselectable();
  837. //start resize end
  838. if (options.canResize && (x2-x1)>3*options.resizeZoneWidth && (posx<=x2 && posx >= x2- options.resizeZoneWidth)) {
  839. //store offset mouse x2
  840. offsetMouseRect = x2 - e.pageX;
  841. target.attr("oldw", target.attr("width"));
  842. var one = true;
  843. //bind event for start resizing
  844. $(svg).bind("mousemove.deSVG", function (e) {
  845. if (one) {
  846. //trigger startResize
  847. options.startResize.call(target.get(0), e);
  848. one = false;
  849. }
  850. //manage resizing
  851. var nW = e.pageX - x1 + offsetMouseRect;
  852. target.attr("width", nW < options.minSize ? options.minSize : nW);
  853. //callback
  854. options.resize.call(target.get(0), e);
  855. });
  856. //bind mouse up on body to stop resizing
  857. $("body").one("mouseup.deSVG", stopResize);
  858. //start resize start
  859. } else if (options.canResize && (x2-x1)>3*options.resizeZoneWidth && (posx>=x1 && posx<=x1+options.resizeZoneWidth)) {
  860. //store offset mouse x1
  861. offsetMouseRect = parseFloat(target.attr("x"));
  862. target.attr("oldw", target.attr("width")); //todo controllare se è ancora usato oldw
  863. var one = true;
  864. //bind event for start resizing
  865. $(svg).bind("mousemove.deSVG", function (e) {
  866. if (one) {
  867. //trigger startResize
  868. options.startResize.call(target.get(0), e);
  869. one = false;
  870. }
  871. //manage resizing
  872. var nx1= offsetMouseRect-(posx-e.pageX);
  873. var nW = (x2-x1) + (posx-e.pageX);
  874. nW=nW < options.minSize ? options.minSize : nW;
  875. target.attr("x",nx1);
  876. target.attr("width", nW);
  877. //callback
  878. options.resize.call(target.get(0), e);
  879. });
  880. //bind mouse up on body to stop resizing
  881. $("body").one("mouseup.deSVG", stopResize);
  882. // start drag
  883. } else if (options.canDrag) {
  884. //store offset mouse x1
  885. offsetMouseRect = parseFloat(target.attr("x")) - e.pageX;
  886. target.attr("oldx", target.attr("x"));
  887. var one = true;
  888. //bind event for start dragging
  889. $(svg).bind("mousemove.deSVG",function (e) {
  890. if (one) {
  891. //trigger startDrag
  892. options.startDrag.call(target.get(0), e);
  893. one = false;
  894. }
  895. //manage resizing
  896. target.attr("x", offsetMouseRect + e.pageX);
  897. //callback
  898. options.drag.call(target.get(0), e);
  899. }).bind("mouseleave.deSVG", drop);
  900. //bind mouse up on body to stop resizing
  901. $("body").one("mouseup.deSVG", drop);
  902. }
  903. }
  904. ).bind("mousemove.deSVG",
  905. function (e) {
  906. var el = $(this);
  907. var x1 = el.find("[class*=taskLayout]").offset().left;
  908. var x2 = x1 + parseFloat(el.attr("width"));
  909. var posx = e.pageX;
  910. //set cursor handle
  911. if (options.canResize && (x2-x1)>3*options.resizeZoneWidth &&((posx<=x2 && posx >= x2- options.resizeZoneWidth) || (posx>=x1 && posx<=x1+options.resizeZoneWidth))) {
  912. el.addClass("deSVGhand");
  913. } else {
  914. el.removeClass("deSVGhand");
  915. }
  916. }
  917. ).addClass("deSVG");
  918. }
  919. });
  920. return this;
  921. function stopResize(e) {
  922. $(svg).unbind("mousemove.deSVG").unbind("mouseup.deSVG").unbind("mouseleave.deSVG");
  923. if (target && target.attr("oldw")!=target.attr("width"))
  924. options.stopResize.call(target.get(0), e); //callback
  925. target = undefined;
  926. $("body").clearunselectable();
  927. }
  928. function drop(e) {
  929. $(svg).unbind("mousemove.deSVG").unbind("mouseup.deSVG").unbind("mouseleave.deSVG");
  930. if (target && target.attr("oldx") != target.attr("x"))
  931. options.drop.call(target.get(0), e); //callback
  932. target = undefined;
  933. $("body").clearunselectable();
  934. }
  935. };