Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

1489 строки
42 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. */
  21. function GanttMaster() {
  22. this.tasks = [];
  23. this.deletedTaskIds = [];
  24. this.links = [];
  25. this.editor; //element for editor
  26. this.gantt; //element for gantt
  27. this.splitter; //element for splitter
  28. this.isMultiRoot=false; // set to true in case of tasklist
  29. this.element;
  30. this.resources; //list of resources
  31. this.roles; //list of roles
  32. this.minEditableDate = 0;
  33. this.maxEditableDate = Infinity;
  34. this.set100OnClose=false;
  35. this.fillWithEmptyLines=true; //when is used by widget it could be usefull to do not fill with empty lines
  36. this.minRowsInEditor=30; // number of rows always visible in editor
  37. this.permissions = {
  38. canWriteOnParent: true,
  39. canWrite: true,
  40. canAdd: true,
  41. canInOutdent: true,
  42. canMoveUpDown: true,
  43. canSeePopEdit: true,
  44. canSeeFullEdit: true,
  45. canSeeDep: true,
  46. canSeeCriticalPath: true,
  47. canAddIssue: false,
  48. cannotCloseTaskIfIssueOpen: false
  49. };
  50. this.firstDayOfWeek = Date.firstDayOfWeek;
  51. this.serverClientTimeOffset = 0;
  52. this.currentTask; // task currently selected;
  53. this.resourceUrl = "/static/css/gantt/res/"; // URL to resources (images etc.)
  54. this.__currentTransaction; // a transaction object holds previous state during changes
  55. this.__undoStack = [];
  56. this.__redoStack = [];
  57. this.__inUndoRedo = false; // a control flag to avoid Undo/Redo stacks reset when needed
  58. var self = this;
  59. }
  60. GanttMaster.prototype.init = function (place) {
  61. this.element = place;
  62. var self = this;
  63. //load templates
  64. $("#gantEditorTemplates").loadTemplates().remove();
  65. //create editor
  66. this.editor = new GridEditor(this);
  67. place.append(this.editor.gridified);
  68. //create gantt
  69. this.gantt = new Ganttalendar("m", new Date().getTime() - 3600000 * 24 * 2, new Date().getTime() + 3600000 * 24 * 15, this, place.width() * .6);
  70. //setup splitter
  71. self.splitter = $.splittify.init(place, this.editor.gridified, this.gantt.element, 60);
  72. self.splitter.firstBoxMinWidth = 5;
  73. self.splitter.secondBoxMinWidth = 20;
  74. //prepend buttons
  75. var ganttButtons = $.JST.createFromTemplate({}, "GANTBUTTONS");
  76. //hide buttons basing on permissions
  77. if (!self.permissions.canWrite)
  78. ganttButtons.find(".requireCanWrite").hide();
  79. if (!self.permissions.canAdd)
  80. ganttButtons.find(".requireCanAdd").hide();
  81. if (!self.permissions.canInOutdent)
  82. ganttButtons.find(".requireCanInOutdent").hide();
  83. if (!self.permissions.canMoveUpDown)
  84. ganttButtons.find(".requireCanMoveUpDown").hide();
  85. if (!self.permissions.canSeeCriticalPath)
  86. ganttButtons.find(".requireCanSeeCriticalPath").hide();
  87. if (!self.permissions.canAddIssue)
  88. ganttButtons.find(".requireCanAddIssue").hide();
  89. place.before(ganttButtons);
  90. //bindings
  91. place.bind("refreshTasks.gantt", function () {
  92. self.redrawTasks();
  93. }).bind("refreshTask.gantt", function (e, task) {
  94. self.drawTask(task);
  95. }).bind("deleteCurrentTask.gantt", function (e) {
  96. self.deleteCurrentTask();
  97. }).bind("addAboveCurrentTask.gantt", function () {
  98. self.addAboveCurrentTask();
  99. }).bind("addBelowCurrentTask.gantt", function () {
  100. self.addBelowCurrentTask();
  101. }).bind("indentCurrentTask.gantt", function () {
  102. self.indentCurrentTask();
  103. }).bind("outdentCurrentTask.gantt", function () {
  104. self.outdentCurrentTask();
  105. }).bind("moveUpCurrentTask.gantt", function () {
  106. self.moveUpCurrentTask();
  107. }).bind("moveDownCurrentTask.gantt", function () {
  108. self.moveDownCurrentTask();
  109. }).bind("collapseAll.gantt", function () {
  110. self.collapseAll();
  111. }).bind("expandAll.gantt", function () {
  112. self.expandAll();
  113. }).bind("zoomPlus.gantt", function () {
  114. self.gantt.zoomGantt(true);
  115. }).bind("zoomMinus.gantt", function () {
  116. self.gantt.zoomGantt(false);
  117. }).bind("addIssue.gantt", function () {
  118. self.addIssue();
  119. }).bind("undo.gantt", function () {
  120. if (!self.permissions.canWrite)
  121. return;
  122. self.undo();
  123. }).bind("redo.gantt", function () {
  124. if (!self.permissions.canWrite)
  125. return;
  126. self.redo();
  127. }).bind("resize.gantt", function () {
  128. self.resize();
  129. });
  130. //keyboard management bindings
  131. $("body").bind("keydown.body", function (e) {
  132. //console.debug(e.keyCode+ " "+e.target.nodeName, e.ctrlKey)
  133. var eventManaged = true;
  134. var isCtrl = e.ctrlKey || e.metaKey;
  135. var bodyOrSVG = e.target.nodeName.toLowerCase() == "body" || e.target.nodeName.toLowerCase() == "svg";
  136. var inWorkSpace=$(e.target).closest("#workSpace").length>0;
  137. //store focused field
  138. var focusedField=$(":focus");
  139. var focusedSVGElement = self.gantt.element.find(".focused.focused");// orrible hack for chrome that seems to keep in memory a cached object
  140. var isFocusedSVGElement=focusedSVGElement.length >0;
  141. if ((inWorkSpace ||isFocusedSVGElement) && isCtrl && e.keyCode == 37) { // CTRL+LEFT on the grid
  142. self.outdentCurrentTask();
  143. focusedField.focus();
  144. } else if (inWorkSpace && isCtrl && e.keyCode == 38) { // CTRL+UP on the grid
  145. self.moveUpCurrentTask();
  146. focusedField.focus();
  147. } else if (inWorkSpace && isCtrl && e.keyCode == 39) { //CTRL+RIGHT on the grid
  148. self.indentCurrentTask();
  149. focusedField.focus();
  150. } else if (inWorkSpace && isCtrl && e.keyCode == 40) { //CTRL+DOWN on the grid
  151. self.moveDownCurrentTask();
  152. focusedField.focus();
  153. } else if (isCtrl && e.keyCode == 89) { //CTRL+Y
  154. self.redo();
  155. } else if (isCtrl && e.keyCode == 90) { //CTRL+Y
  156. self.undo();
  157. } else if ( (isCtrl && inWorkSpace) && (e.keyCode == 8 || e.keyCode == 46) ) { //CTRL+DEL CTRL+BACKSPACE on grid
  158. self.deleteCurrentTask();
  159. } else if ( focusedSVGElement.is(".taskBox") && (e.keyCode == 8 || e.keyCode == 46) ) { //DEL BACKSPACE svg task
  160. self.deleteCurrentTask();
  161. } else if ( focusedSVGElement.is(".linkGroup") && (e.keyCode == 8 || e.keyCode == 46) ) { //DEL BACKSPACE svg link
  162. self.removeLink(focused.data("from"), focused.data("to"));
  163. } else {
  164. eventManaged=false;
  165. }
  166. if (eventManaged) {
  167. e.preventDefault();
  168. e.stopPropagation();
  169. }
  170. });
  171. //ask for comment input
  172. $("#saveGanttButton").after($('#LOG_CHANGES_CONTAINER'));
  173. //ask for comment management
  174. this.element.on("gantt.saveRequired",this.manageSaveRequired)
  175. };
  176. GanttMaster.messages = {
  177. "CANNOT_WRITE": "CANNOT_WRITE",
  178. "CHANGE_OUT_OF_SCOPE": "NO_RIGHTS_FOR_UPDATE_PARENTS_OUT_OF_EDITOR_SCOPE",
  179. "START_IS_MILESTONE": "START_IS_MILESTONE",
  180. "END_IS_MILESTONE": "END_IS_MILESTONE",
  181. "TASK_HAS_CONSTRAINTS": "TASK_HAS_CONSTRAINTS",
  182. "GANTT_ERROR_DEPENDS_ON_OPEN_TASK": "GANTT_ERROR_DEPENDS_ON_OPEN_TASK",
  183. "GANTT_ERROR_DESCENDANT_OF_CLOSED_TASK": "GANTT_ERROR_DESCENDANT_OF_CLOSED_TASK",
  184. "TASK_HAS_EXTERNAL_DEPS": "TASK_HAS_EXTERNAL_DEPS",
  185. "GANTT_ERROR_LOADING_DATA_TASK_REMOVED": "GANTT_ERROR_LOADING_DATA_TASK_REMOVED",
  186. "CIRCULAR_REFERENCE": "CIRCULAR_REFERENCE",
  187. "CANNOT_MOVE_TASK": "CANNOT_MOVE_TASK",
  188. "CANNOT_DEPENDS_ON_ANCESTORS": "CANNOT_DEPENDS_ON_ANCESTORS",
  189. "CANNOT_DEPENDS_ON_DESCENDANTS": "CANNOT_DEPENDS_ON_DESCENDANTS",
  190. "INVALID_DATE_FORMAT": "INVALID_DATE_FORMAT",
  191. "GANTT_QUARTER_SHORT": "GANTT_QUARTER_SHORT",
  192. "GANTT_SEMESTER_SHORT": "GANTT_SEMESTER_SHORT",
  193. "CANNOT_CLOSE_TASK_IF_OPEN_ISSUE": "CANNOT_CLOSE_TASK_IF_OPEN_ISSUE",
  194. "PLEASE_SAVE_PROJECT": "PLEASE_SAVE_PROJECT"
  195. };
  196. GanttMaster.prototype.createTask = function (id, name, code, level, start, duration) {
  197. var factory = new TaskFactory();
  198. return factory.build(id, name, code, level, start, duration);
  199. };
  200. GanttMaster.prototype.getOrCreateResource = function (id, name) {
  201. var res= this.getResource(id);
  202. if (!res && id && name) {
  203. res = this.createResource(id, name);
  204. }
  205. return res
  206. };
  207. GanttMaster.prototype.createResource = function (id, name) {
  208. var res = new Resource(id, name);
  209. this.resources.push(res);
  210. return res;
  211. };
  212. //update depends strings
  213. GanttMaster.prototype.updateDependsStrings = function () {
  214. //remove all deps
  215. for (var i = 0; i < this.tasks.length; i++) {
  216. this.tasks[i].depends = "";
  217. }
  218. for (var i = 0; i < this.links.length; i++) {
  219. var link = this.links[i];
  220. var dep = link.to.depends;
  221. link.to.depends = link.to.depends + (link.to.depends == "" ? "" : ",") + (link.from.getRow() + 1) + (link.lag ? ":" + link.lag : "");
  222. }
  223. };
  224. GanttMaster.prototype.removeLink = function (fromTask, toTask) {
  225. //console.debug("removeLink");
  226. if (!this.permissions.canWrite || (!fromTask.canWrite && !toTask.canWrite))
  227. return;
  228. this.beginTransaction();
  229. var found = false;
  230. for (var i = 0; i < this.links.length; i++) {
  231. if (this.links[i].from == fromTask && this.links[i].to == toTask) {
  232. this.links.splice(i, 1);
  233. found = true;
  234. break;
  235. }
  236. }
  237. if (found) {
  238. this.updateDependsStrings();
  239. if (this.updateLinks(toTask))
  240. this.changeTaskDates(toTask, toTask.start, toTask.end); // fake change to force date recomputation from dependencies
  241. }
  242. this.endTransaction();
  243. };
  244. GanttMaster.prototype.removeAllLinks = function (task, openTrans) {
  245. //console.debug("removeLink");
  246. if (!this.permissions.canWrite || (!task.canWrite && !task.canWrite))
  247. return;
  248. if (openTrans)
  249. this.beginTransaction();
  250. var found = false;
  251. for (var i = 0; i < this.links.length; i++) {
  252. if (this.links[i].from == task || this.links[i].to == task) {
  253. this.links.splice(i, 1);
  254. found = true;
  255. }
  256. }
  257. if (found) {
  258. this.updateDependsStrings();
  259. }
  260. if (openTrans)
  261. this.endTransaction();
  262. };
  263. //------------------------------------ ADD TASK --------------------------------------------
  264. GanttMaster.prototype.addTask = function (task, row) {
  265. //console.debug("master.addTask",task,row,this);
  266. if (!this.permissions.canWrite || !this.permissions.canAdd )
  267. return;
  268. task.master = this; // in order to access controller from task
  269. //replace if already exists
  270. var pos = -1;
  271. for (var i = 0; i < this.tasks.length; i++) {
  272. if (task.id == this.tasks[i].id) {
  273. pos = i;
  274. break;
  275. }
  276. }
  277. if (pos >= 0) {
  278. this.tasks.splice(pos, 1);
  279. row = parseInt(pos);
  280. }
  281. //add task in collection
  282. if (typeof(row) != "number") {
  283. this.tasks.push(task);
  284. } else {
  285. this.tasks.splice(row, 0, task);
  286. //recompute depends string
  287. this.updateDependsStrings();
  288. }
  289. //add Link collection in memory
  290. var linkLoops = !this.updateLinks(task);
  291. //set the status according to parent
  292. if (task.getParent())
  293. task.status = task.getParent().status;
  294. else
  295. task.status = "STATUS_ACTIVE";
  296. var ret = task;
  297. if (linkLoops || !task.setPeriod(task.start, task.end)) {
  298. //remove task from in-memory collection
  299. //console.debug("removing task from memory",task);
  300. this.tasks.splice(task.getRow(), 1);
  301. ret = undefined;
  302. } else {
  303. //append task to editor
  304. this.editor.addTask(task, row);
  305. //append task to gantt
  306. this.gantt.addTask(task);
  307. }
  308. //trigger addedTask event
  309. $(this.element).trigger("addedTask.gantt", task);
  310. return ret;
  311. };
  312. /**
  313. * a project contais tasks, resources, roles, and info about permisions
  314. * @param project
  315. */
  316. GanttMaster.prototype.loadProject = function (project) {
  317. //console.debug("loadProject", project)
  318. this.beginTransaction();
  319. this.serverClientTimeOffset = typeof project.serverTimeOffset !="undefined"? (parseInt(project.serverTimeOffset) + new Date().getTimezoneOffset() * 60000) : 0;
  320. this.resources = project.resources;
  321. this.roles = project.roles;
  322. this.permissions.canWrite = project.canWrite;
  323. this.permissions.canWriteOnParent = project.canWriteOnParent;
  324. this.permissions.cannotCloseTaskIfIssueOpen = project.cannotCloseTaskIfIssueOpen;
  325. if (project.minEditableDate)
  326. this.minEditableDate = computeStart(project.minEditableDate);
  327. else
  328. this.minEditableDate = -Infinity;
  329. if (project.maxEditableDate)
  330. this.maxEditableDate = computeEnd(project.maxEditableDate);
  331. else
  332. this.maxEditableDate = Infinity;
  333. //recover stored ccollapsed statuas
  334. var collTasks=this.loadCollapsedTasks();
  335. //shift dates in order to have client side the same hour (e.g.: 23:59) of the server side
  336. for (var i = 0; i < project.tasks.length; i++) {
  337. var task = project.tasks[i];
  338. task.start += this.serverClientTimeOffset;
  339. task.end += this.serverClientTimeOffset;
  340. //set initial collapsed status
  341. task.collapsed=collTasks.indexOf(task.id)>=0;
  342. }
  343. this.loadTasks(project.tasks, project.selectedRow);
  344. this.deletedTaskIds = [];
  345. //recover saved zoom level
  346. if (project.zoom){
  347. this.gantt.zoom = project.zoom;
  348. }
  349. this.endTransaction();
  350. var self = this;
  351. this.gantt.element.oneTime(200, function () {self.gantt.centerOnToday()});
  352. };
  353. GanttMaster.prototype.loadTasks = function (tasks, selectedRow) {
  354. //console.debug("GanttMaster.prototype.loadTasks")
  355. var factory = new TaskFactory();
  356. //reset
  357. this.reset();
  358. for (var i = 0; i < tasks.length; i++) {
  359. var task = tasks[i];
  360. if (!(task instanceof Task)) {
  361. var t = factory.build(task.id, task.name, task.code, task.level, task.start, task.duration, task.collapsed);
  362. for (var key in task) {
  363. if (key != "end" && key != "start")
  364. t[key] = task[key]; //copy all properties
  365. }
  366. task = t;
  367. }
  368. task.master = this; // in order to access controller from task
  369. this.tasks.push(task); //append task at the end
  370. }
  371. //var prof=new Profiler("gm_loadTasks_addTaskLoop");
  372. for (var i = 0; i < this.tasks.length; i++) {
  373. var task = this.tasks[i];
  374. var numOfError = this.__currentTransaction && this.__currentTransaction.errors ? this.__currentTransaction.errors.length : 0;
  375. //add Link collection in memory
  376. while (!this.updateLinks(task)) { // error on update links while loading can be considered as "warning". Can be displayed and removed in order to let transaction commits.
  377. if (this.__currentTransaction && numOfError != this.__currentTransaction.errors.length) {
  378. var msg = "ERROR:\n";
  379. while (numOfError < this.__currentTransaction.errors.length) {
  380. var err = this.__currentTransaction.errors.pop();
  381. msg = msg + err.msg + "\n\n";
  382. }
  383. alert(msg);
  384. }
  385. this.removeAllLinks(task, false);
  386. }
  387. if (!task.setPeriod(task.start, task.end)) {
  388. alert(GanttMaster.messages.GANNT_ERROR_LOADING_DATA_TASK_REMOVED + "\n" + task.name );
  389. //remove task from in-memory collection
  390. this.tasks.splice(task.getRow(), 1);
  391. } else {
  392. //append task to editor
  393. this.editor.addTask(task, null, true);
  394. //append task to gantt
  395. this.gantt.addTask(task);
  396. }
  397. }
  398. //this.editor.fillEmptyLines();
  399. //prof.stop();
  400. // re-select old row if tasks is not empty
  401. if (this.tasks && this.tasks.length > 0) {
  402. selectedRow = selectedRow ? selectedRow : 0;
  403. this.tasks[selectedRow].rowElement.click();
  404. }
  405. };
  406. GanttMaster.prototype.getTask = function (taskId) {
  407. var ret;
  408. for (var i = 0; i < this.tasks.length; i++) {
  409. var tsk = this.tasks[i];
  410. if (tsk.id == taskId) {
  411. ret = tsk;
  412. break;
  413. }
  414. }
  415. return ret;
  416. };
  417. GanttMaster.prototype.getResource = function (resId) {
  418. var ret;
  419. for (var i = 0; i < this.resources.length; i++) {
  420. var res = this.resources[i];
  421. if (res.id == resId) {
  422. ret = res;
  423. break;
  424. }
  425. }
  426. return ret;
  427. };
  428. GanttMaster.prototype.changeTaskDeps = function (task) {
  429. return task.moveTo(task.start);
  430. };
  431. GanttMaster.prototype.changeTaskDates = function (task, start, end) {
  432. return task.setPeriod(start, end);
  433. };
  434. GanttMaster.prototype.moveTask = function (task, newStart) {
  435. return task.moveTo(newStart, true);
  436. };
  437. GanttMaster.prototype.taskIsChanged = function () {
  438. //console.debug("taskIsChanged");
  439. var master = this;
  440. //refresh is executed only once every 50ms
  441. this.element.stopTime("gnnttaskIsChanged");
  442. //var profilerext = new Profiler("gm_taskIsChangedRequest");
  443. this.element.oneTime(50, "gnnttaskIsChanged", function () {
  444. //console.debug("task Is Changed real call to redraw");
  445. //var profiler = new Profiler("gm_taskIsChangedReal");
  446. master.editor.redraw();
  447. master.gantt.refreshGantt();
  448. master.element.trigger("gantt.refreshGanttCompleted");
  449. //profiler.stop();
  450. });
  451. //profilerext.stop();
  452. };
  453. GanttMaster.prototype.redraw = function () {
  454. this.editor.redraw();
  455. this.gantt.refreshGantt();
  456. };
  457. GanttMaster.prototype.reset = function () {
  458. //console.debug("GanttMaster.prototype.reset");
  459. this.tasks = [];
  460. this.links = [];
  461. this.deletedTaskIds = [];
  462. if (!this.__inUndoRedo) {
  463. this.__undoStack = [];
  464. this.__redoStack = [];
  465. } else { // don't reset the stacks if we're in an Undo/Redo, but restart the inUndoRedo control
  466. this.__inUndoRedo = false;
  467. }
  468. delete this.currentTask;
  469. this.editor.reset();
  470. this.gantt.reset();
  471. };
  472. GanttMaster.prototype.showTaskEditor = function (taskId) {
  473. var task = this.getTask(taskId);
  474. task.rowElement.find(".edit").click();
  475. };
  476. GanttMaster.prototype.saveProject = function () {
  477. return this.saveGantt(false);
  478. };
  479. GanttMaster.prototype.saveGantt = function (forTransaction) {
  480. //var prof = new Profiler("gm_saveGantt");
  481. var saved = [];
  482. for (var i = 0; i < this.tasks.length; i++) {
  483. var task = this.tasks[i];
  484. var cloned = task.clone();
  485. delete cloned.master;
  486. delete cloned.rowElement;
  487. delete cloned.ganttElement;
  488. //shift back to server side timezone
  489. if (!forTransaction) {
  490. cloned.start -= this.serverClientTimeOffset;
  491. cloned.end -= this.serverClientTimeOffset;
  492. }
  493. saved.push(cloned);
  494. }
  495. var ret = {tasks: saved};
  496. if (this.currentTask) {
  497. ret.selectedRow = this.currentTask.getRow();
  498. }
  499. ret.deletedTaskIds = this.deletedTaskIds; //this must be consistent with transactions and undo
  500. if (!forTransaction) {
  501. ret.resources = this.resources;
  502. ret.roles = this.roles;
  503. ret.canWrite = this.permissions.canWrite;
  504. ret.canWriteOnParent = this.permissions.canWriteOnParent;
  505. ret.zoom = this.gantt.zoom;
  506. //save collapsed tasks on localStorage
  507. this.storeCollapsedTasks();
  508. //mark un-changed task and assignments
  509. this.markUnChangedTasksAndAssignments(ret);
  510. //si aggiunge il commento al cambiamento di date/status
  511. ret.changesReasonWhy=$("#LOG_CHANGES").val();
  512. }
  513. //prof.stop();
  514. return ret;
  515. };
  516. GanttMaster.prototype.markUnChangedTasksAndAssignments=function(newProject){
  517. //console.debug("markUnChangedTasksAndAssignments");
  518. //si controlla che ci sia qualcosa di cambiato, ovvero che ci sia l'undo stack
  519. if (this.__undoStack.length>0){
  520. var oldProject=JSON.parse(ge.__undoStack[0]);
  521. //si looppano i "nuovi" task
  522. for (var i=0;i<newProject.tasks.length;i++){
  523. var newTask=newProject.tasks[i];
  524. //se è un task che c'erà già
  525. if (typeof (newTask.id)=="String" && !newTask.id.startsWith("tmp_")){
  526. //si recupera il vecchio task
  527. var oldTask;
  528. for (var j=0;j<oldProject.tasks.length;j++){
  529. if (oldProject.tasks[j].id==newTask.id){
  530. oldTask=oldProject.tasks[j];
  531. break;
  532. }
  533. }
  534. //si controlla se ci sono stati cambiamenti
  535. var taskChanged=
  536. oldTask.id != newTask.id ||
  537. oldTask.code != newTask.code ||
  538. oldTask.name != newTask.name ||
  539. oldTask.start != newTask.start ||
  540. oldTask.startIsMilestone != newTask.startIsMilestone ||
  541. oldTask.end != newTask.end ||
  542. oldTask.endIsMilestone != newTask.endIsMilestone ||
  543. oldTask.duration != newTask.duration ||
  544. oldTask.status != newTask.status ||
  545. oldTask.typeId != newTask.typeId ||
  546. oldTask.relevance != newTask.relevance ||
  547. oldTask.progress != newTask.progress ||
  548. oldTask.progressByWorklog != newTask.progressByWorklog ||
  549. oldTask.description != newTask.description ||
  550. oldTask.level != newTask.level||
  551. oldTask.depends != newTask.depends;
  552. newTask.unchanged=!taskChanged;
  553. //se ci sono assegnazioni
  554. if (newTask.assigs&&newTask.assigs.length>0){
  555. //se abbiamo trovato il vecchio task e questo aveva delle assegnazioni
  556. if (oldTask && oldTask.assigs && oldTask.assigs.length>0){
  557. for (var j=0;j<oldTask.assigs.length;j++){
  558. var oldAssig=oldTask.assigs[j];
  559. //si cerca la nuova assegnazione corrispondente
  560. var newAssig;
  561. for (var k=0;k<newTask.assigs.length;k++){
  562. if(oldAssig.id==newTask.assigs[k].id){
  563. newAssig=newTask.assigs[k];
  564. break;
  565. }
  566. }
  567. //se c'è una nuova assig corrispondente
  568. if(newAssig){
  569. //si confrontano i valori per vedere se è cambiata
  570. newAssig.unchanged=
  571. newAssig.resourceId==oldAssig.resourceId &&
  572. newAssig.roleId==oldAssig.roleId &&
  573. newAssig.effort==oldAssig.effort;
  574. }
  575. }
  576. }
  577. }
  578. }
  579. }
  580. }
  581. };
  582. GanttMaster.prototype.loadCollapsedTasks = function () {
  583. var collTasks=[];
  584. if (localStorage ) {
  585. if (localStorage.getObject("TWPGanttCollTasks"))
  586. collTasks = localStorage.getObject("TWPGanttCollTasks");
  587. return collTasks;
  588. }
  589. };
  590. GanttMaster.prototype.storeCollapsedTasks = function () {
  591. //console.debug("storeCollapsedTasks");
  592. if (localStorage) {
  593. var collTasks;
  594. if (!localStorage.getObject("TWPGanttCollTasks"))
  595. collTasks = [];
  596. else
  597. collTasks = localStorage.getObject("TWPGanttCollTasks");
  598. for (var i = 0; i < this.tasks.length; i++) {
  599. var task = this.tasks[i];
  600. var pos=collTasks.indexOf(task.id);
  601. if (task.collapsed){
  602. if (pos<0)
  603. collTasks.push(task.id);
  604. } else {
  605. if (pos>=0)
  606. collTasks.splice(pos,1);
  607. }
  608. }
  609. localStorage.setObject("TWPGanttCollTasks", collTasks);
  610. }
  611. };
  612. GanttMaster.prototype.updateLinks = function (task) {
  613. //console.debug("updateLinks",task);
  614. //var prof= new Profiler("gm_updateLinks");
  615. // defines isLoop function
  616. function isLoop(task, target, visited) {
  617. //var prof= new Profiler("gm_isLoop");
  618. //console.debug("isLoop :"+task.name+" - "+target.name);
  619. if (target == task) {
  620. return true;
  621. }
  622. var sups = task.getSuperiors();
  623. //my parent' superiors are my superiors too
  624. var p = task.getParent();
  625. while (p) {
  626. sups = sups.concat(p.getSuperiors());
  627. p = p.getParent();
  628. }
  629. //my children superiors are my superiors too
  630. var chs = task.getChildren();
  631. for (var i = 0; i < chs.length; i++) {
  632. sups = sups.concat(chs[i].getSuperiors());
  633. }
  634. var loop = false;
  635. //check superiors
  636. for (var i = 0; i < sups.length; i++) {
  637. var supLink = sups[i];
  638. if (supLink.from == target) {
  639. loop = true;
  640. break;
  641. } else {
  642. if (visited.indexOf(supLink.from.id + "x" + target.id) <= 0) {
  643. visited.push(supLink.from.id + "x" + target.id);
  644. if (isLoop(supLink.from, target, visited)) {
  645. loop = true;
  646. break;
  647. }
  648. }
  649. }
  650. }
  651. //check target parent
  652. var tpar = target.getParent();
  653. if (tpar) {
  654. if (visited.indexOf(task.id + "x" + tpar.id) <= 0) {
  655. visited.push(task.id + "x" + tpar.id);
  656. if (isLoop(task, tpar, visited)) {
  657. loop = true;
  658. }
  659. }
  660. }
  661. //prof.stop();
  662. return loop;
  663. }
  664. //remove my depends
  665. this.links = this.links.filter(function (link) {
  666. return link.to != task;
  667. });
  668. var todoOk = true;
  669. if (task.depends) {
  670. //cannot depend from an ancestor
  671. var parents = task.getParents();
  672. //cannot depend from descendants
  673. var descendants = task.getDescendant();
  674. var deps = task.depends.split(",");
  675. var newDepsString = "";
  676. var visited = [];
  677. for (var j = 0; j < deps.length; j++) {
  678. var dep = deps[j]; // in the form of row(lag) e.g. 2:3,3:4,5
  679. var par = dep.split(":");
  680. var lag = 0;
  681. if (par.length > 1) {
  682. lag = parseInt(par[1]);
  683. }
  684. var sup = this.tasks[parseInt(par[0] - 1)];
  685. if (sup) {
  686. if (parents && parents.indexOf(sup) >= 0) {
  687. this.setErrorOnTransaction("\""+task.name + "\"\n" + GanttMaster.messages.CANNOT_DEPENDS_ON_ANCESTORS + "\n\"" + sup.name+"\"");
  688. todoOk = false;
  689. } else if (descendants && descendants.indexOf(sup) >= 0) {
  690. this.setErrorOnTransaction("\""+task.name + "\"\n" + GanttMaster.messages.CANNOT_DEPENDS_ON_DESCENDANTS + "\n\"" + sup.name+"\"");
  691. todoOk = false;
  692. } else if (isLoop(sup, task, visited)) {
  693. todoOk = false;
  694. this.setErrorOnTransaction(GanttMaster.messages.CIRCULAR_REFERENCE + "\n\"" + task.name + "\" -> \"" + sup.name+"\"");
  695. } else {
  696. this.links.push(new Link(sup, task, lag));
  697. newDepsString = newDepsString + (newDepsString.length > 0 ? "," : "") + dep;
  698. }
  699. }
  700. }
  701. task.depends = newDepsString;
  702. }
  703. //prof.stop();
  704. return todoOk;
  705. };
  706. GanttMaster.prototype.moveUpCurrentTask = function () {
  707. var self = this;
  708. //console.debug("moveUpCurrentTask",self.currentTask)
  709. if (!self.permissions.canWrite || !self.currentTask.canWrite || !self.permissions.canMoveUpDown )
  710. return;
  711. if (self.currentTask) {
  712. self.beginTransaction();
  713. self.currentTask.moveUp();
  714. self.endTransaction();
  715. }
  716. };
  717. GanttMaster.prototype.moveDownCurrentTask = function () {
  718. var self = this;
  719. //console.debug("moveDownCurrentTask",self.currentTask)
  720. if (!self.permissions.canWrite || !self.currentTask.canWrite || !self.permissions.canMoveUpDown )
  721. return;
  722. if (self.currentTask) {
  723. self.beginTransaction();
  724. self.currentTask.moveDown();
  725. self.endTransaction();
  726. }
  727. };
  728. GanttMaster.prototype.outdentCurrentTask = function () {
  729. var self = this;
  730. if (!self.permissions.canWrite || !self.currentTask.canWrite || !self.permissions.canInOutdent)
  731. return;
  732. if (self.currentTask) {
  733. var par = self.currentTask.getParent();
  734. self.beginTransaction();
  735. self.currentTask.outdent();
  736. self.endTransaction();
  737. //[expand]
  738. if (par) self.editor.refreshExpandStatus(par);
  739. }
  740. };
  741. GanttMaster.prototype.indentCurrentTask = function () {
  742. var self = this;
  743. if (!self.permissions.canWrite || !self.currentTask.canWrite|| !self.permissions.canInOutdent)
  744. return;
  745. if (self.currentTask) {
  746. self.beginTransaction();
  747. self.currentTask.indent();
  748. self.endTransaction();
  749. }
  750. };
  751. GanttMaster.prototype.addBelowCurrentTask = function () {
  752. var self = this;
  753. if (!self.permissions.canWrite|| !self.permissions.canAdd)
  754. return;
  755. var factory = new TaskFactory();
  756. var ch;
  757. var row = 0;
  758. if (self.currentTask && self.currentTask.name) {
  759. ch = factory.build("tmp_" + new Date().getTime(), "", "", self.currentTask.level + 1, self.currentTask.start, 1);
  760. row = self.currentTask.getRow() + 1;
  761. if (row>0) {
  762. self.beginTransaction();
  763. var task = self.addTask(ch, row);
  764. if (task) {
  765. task.rowElement.click();
  766. task.rowElement.find("[name=name]").focus();
  767. }
  768. self.endTransaction();
  769. }
  770. }
  771. };
  772. GanttMaster.prototype.addAboveCurrentTask = function () {
  773. var self = this;
  774. if (!self.permissions.canWrite || !self.permissions.canAdd)
  775. return;
  776. var factory = new TaskFactory();
  777. var ch;
  778. var row = 0;
  779. if (self.currentTask && self.currentTask.name) {
  780. //cannot add brothers to root
  781. if (self.currentTask.level <= 0)
  782. return;
  783. ch = factory.build("tmp_" + new Date().getTime(), "", "", self.currentTask.level, self.currentTask.start, 1);
  784. row = self.currentTask.getRow();
  785. if (row > 0) {
  786. self.beginTransaction();
  787. var task = self.addTask(ch, row);
  788. if (task) {
  789. task.rowElement.click();
  790. task.rowElement.find("[name=name]").focus();
  791. }
  792. self.endTransaction();
  793. }
  794. }
  795. };
  796. GanttMaster.prototype.deleteCurrentTask = function () {
  797. //console.debug("deleteCurrentTask",this.currentTask , this.isMultiRoot)
  798. var self = this;
  799. if (!self.currentTask || !self.permissions.canWrite || !self.currentTask.canWrite)
  800. return;
  801. var row = self.currentTask.getRow();
  802. //20170526原来是row>0改为row>=0
  803. if (self.currentTask && (row >= 0 || self.isMultiRoot || self.currentTask.isNew()) ) {
  804. var par = self.currentTask.getParent();
  805. self.beginTransaction();
  806. self.currentTask.deleteTask();
  807. self.currentTask = undefined;
  808. //recompute depends string
  809. self.updateDependsStrings();
  810. //redraw
  811. self.redraw();
  812. //[expand]
  813. if (par) self.editor.refreshExpandStatus(par);
  814. //focus next row
  815. row = row > self.tasks.length - 1 ? self.tasks.length - 1 : row;
  816. if (row >= 0) {
  817. self.currentTask = self.tasks[row];
  818. self.currentTask.rowElement.click();
  819. self.currentTask.rowElement.find("[name=name]").focus();
  820. }
  821. self.endTransaction();
  822. }
  823. };
  824. GanttMaster.prototype.collapseAll = function () {
  825. //console.debug("collapseAll");
  826. if (this.currentTask){
  827. this.currentTask.collapsed=true;
  828. var desc = this.currentTask.getDescendant();
  829. for (var i=0; i<desc.length; i++) {
  830. if (desc[i].isParent()) // set collapsed only if is a parent
  831. desc[i].collapsed = true;
  832. desc[i].rowElement.hide();
  833. }
  834. this.redraw();
  835. //store collapse statuses
  836. this.storeCollapsedTasks();
  837. }
  838. };
  839. GanttMaster.prototype.expandAll = function () {
  840. //console.debug("expandAll");
  841. if (this.currentTask){
  842. this.currentTask.collapsed=false;
  843. var desc = this.currentTask.getDescendant();
  844. for (var i=0; i<desc.length; i++) {
  845. desc[i].collapsed = false;
  846. desc[i].rowElement.show();
  847. }
  848. this.redraw();
  849. //store collapse statuses
  850. this.storeCollapsedTasks();
  851. }
  852. };
  853. GanttMaster.prototype.collapse = function (task, all) {
  854. console.debug("collapse",task)
  855. task.collapsed=true;
  856. task.rowElement.addClass("collapsed");
  857. var descs = task.getDescendant();
  858. for (var i = 0; i < descs.length; i++)
  859. descs[i].rowElement.hide();
  860. this.gantt.refreshGantt();
  861. //store collapse statuses
  862. this.storeCollapsedTasks();
  863. };
  864. GanttMaster.prototype.expand = function (task,all) {
  865. console.debug("expand",task)
  866. task.collapsed=false;
  867. task.rowElement.removeClass("collapsed");
  868. var collapsedDescendant = this.getCollapsedDescendant();
  869. var descs = task.getDescendant();
  870. for (var i = 0; i < descs.length; i++) {
  871. var childTask = descs[i];
  872. if (collapsedDescendant.indexOf(childTask) >= 0) continue;
  873. childTask.rowElement.show();
  874. }
  875. this.gantt.refreshGantt();
  876. //store collapse statuses
  877. this.storeCollapsedTasks();
  878. };
  879. GanttMaster.prototype.getCollapsedDescendant = function () {
  880. var allTasks = this.tasks;
  881. var collapsedDescendant = [];
  882. for (var i = 0; i < allTasks.length; i++) {
  883. var task = allTasks[i];
  884. if (collapsedDescendant.indexOf(task) >= 0) continue;
  885. if (task.collapsed) collapsedDescendant = collapsedDescendant.concat(task.getDescendant());
  886. }
  887. return collapsedDescendant;
  888. }
  889. GanttMaster.prototype.addIssue = function () {
  890. var self = this;
  891. if (self.currentTask.isNew()){
  892. alert(GanttMaster.messages.PLEASE_SAVE_PROJECT);
  893. return;
  894. }
  895. if (!self.currentTask || !self.currentTask.canAddIssue)
  896. return;
  897. openIssueEditorInBlack('0',"AD","ISSUE_TASK="+self.currentTask.id);
  898. };
  899. //<%----------------------------- TRANSACTION MANAGEMENT ---------------------------------%>
  900. GanttMaster.prototype.beginTransaction = function () {
  901. if (!this.__currentTransaction) {
  902. this.__currentTransaction = {
  903. snapshot: JSON.stringify(this.saveGantt(true)),
  904. errors: []
  905. };
  906. } else {
  907. console.error("Cannot open twice a transaction");
  908. }
  909. return this.__currentTransaction;
  910. };
  911. //this function notify an error to a transaction -> transaction will rollback
  912. GanttMaster.prototype.setErrorOnTransaction = function (errorMessage, task) {
  913. if (this.__currentTransaction) {
  914. this.__currentTransaction.errors.push({msg: errorMessage, task: task});
  915. } else {
  916. console.error(errorMessage);
  917. }
  918. };
  919. GanttMaster.prototype.isTransactionInError = function () {
  920. if (!this.__currentTransaction) {
  921. console.error("Transaction never started.");
  922. return true;
  923. } else {
  924. return this.__currentTransaction.errors.length > 0
  925. }
  926. };
  927. GanttMaster.prototype.endTransaction = function () {
  928. if (!this.__currentTransaction) {
  929. console.error("Transaction never started.");
  930. return true;
  931. }
  932. var ret = true;
  933. //no error -> commit
  934. if (this.__currentTransaction.errors.length <= 0) {
  935. //console.debug("committing transaction");
  936. //put snapshot in undo
  937. this.__undoStack.push(this.__currentTransaction.snapshot);
  938. //clear redo stack
  939. this.__redoStack = [];
  940. //shrink gantt bundaries
  941. this.gantt.originalStartMillis = Infinity;
  942. this.gantt.originalEndMillis = -Infinity;
  943. for (var i = 0; i < this.tasks.length; i++) {
  944. var task = this.tasks[i];
  945. if (this.gantt.originalStartMillis > task.start)
  946. this.gantt.originalStartMillis = task.start;
  947. if (this.gantt.originalEndMillis < task.end)
  948. this.gantt.originalEndMillis = task.end;
  949. }
  950. this.taskIsChanged(); //enqueue for gantt refresh
  951. //error -> rollback
  952. } else {
  953. ret = false;
  954. //console.debug("rolling-back transaction");
  955. //compose error message
  956. var msg = "ERROR:\n";
  957. for (var i = 0; i < this.__currentTransaction.errors.length; i++) {
  958. var err = this.__currentTransaction.errors[i];
  959. msg = msg + err.msg + "\n\n";
  960. }
  961. alert(msg);
  962. //try to restore changed tasks
  963. var oldTasks = JSON.parse(this.__currentTransaction.snapshot);
  964. this.deletedTaskIds = oldTasks.deletedTaskIds;
  965. this.__inUndoRedo = true; // avoid Undo/Redo stacks reset
  966. this.loadTasks(oldTasks.tasks, oldTasks.selectedRow);
  967. this.redraw();
  968. }
  969. //reset transaction
  970. this.__currentTransaction = undefined;
  971. //show/hide save button
  972. this.saveRequired();
  973. //[expand]
  974. this.editor.refreshExpandStatus(this.currentTask);
  975. return ret;
  976. };
  977. // inhibit undo-redo
  978. GanttMaster.prototype.checkpoint = function () {
  979. //console.debug("GanttMaster.prototype.checkpoint");
  980. this.__undoStack = [];
  981. this.__redoStack = [];
  982. this.saveRequired();
  983. };
  984. //----------------------------- UNDO/REDO MANAGEMENT ---------------------------------%>
  985. GanttMaster.prototype.undo = function () {
  986. //console.debug("undo before:",this.__undoStack,this.__redoStack);
  987. if (this.__undoStack.length > 0) {
  988. var his = this.__undoStack.pop();
  989. this.__redoStack.push(JSON.stringify(this.saveGantt()));
  990. var oldTasks = JSON.parse(his);
  991. this.deletedTaskIds = oldTasks.deletedTaskIds;
  992. this.__inUndoRedo = true; // avoid Undo/Redo stacks reset
  993. this.loadTasks(oldTasks.tasks, oldTasks.selectedRow);
  994. //console.debug(oldTasks,oldTasks.deletedTaskIds)
  995. this.redraw();
  996. //show/hide save button
  997. this.saveRequired();
  998. //console.debug("undo after:",this.__undoStack,this.__redoStack);
  999. }
  1000. };
  1001. GanttMaster.prototype.redo = function () {
  1002. //console.debug("redo before:",undoStack,redoStack);
  1003. if (this.__redoStack.length > 0) {
  1004. var his = this.__redoStack.pop();
  1005. this.__undoStack.push(JSON.stringify(this.saveGantt()));
  1006. var oldTasks = JSON.parse(his);
  1007. this.deletedTaskIds = oldTasks.deletedTaskIds;
  1008. this.__inUndoRedo = true; // avoid Undo/Redo stacks reset
  1009. this.loadTasks(oldTasks.tasks, oldTasks.selectedRow);
  1010. this.redraw();
  1011. //console.debug("redo after:",undoStack,redoStack);
  1012. this.saveRequired();
  1013. }
  1014. };
  1015. GanttMaster.prototype.saveRequired = function () {
  1016. //show/hide save button
  1017. if(this.__undoStack.length>0 && this.permissions.canWrite) {
  1018. $("#saveGanttButton").removeClass("disabled");
  1019. $("form[alertOnChange] #Gantt").val(new Date().getTime()); // set a fake variable as dirty
  1020. this.element.trigger("gantt.saveRequired",[true]);
  1021. } else {
  1022. $("#saveGanttButton").addClass("disabled");
  1023. $("form[alertOnChange] #Gantt").updateOldValue(); // set a fake variable as clean
  1024. this.element.trigger("gantt.saveRequired",[false]);
  1025. }
  1026. };
  1027. GanttMaster.prototype.resize = function () {
  1028. //console.debug("GanttMaster.resize")
  1029. this.splitter.resize();
  1030. };
  1031. /**
  1032. * Compute the critical path using Backflow algorithm.
  1033. * Translated from Java code supplied by M. Jessup here http://stackoverflow.com/questions/2985317/critical-path-method-algorithm
  1034. *
  1035. * For each task computes:
  1036. * earlyStart, earlyFinish, latestStart, latestFinish, criticalCost
  1037. *
  1038. * A task on the critical path has isCritical=true
  1039. * A task not in critical path can float by latestStart-earlyStart days
  1040. *
  1041. * If you use critical path avoid usage of dependencies between different levels of tasks
  1042. *
  1043. * WARNNG: It ignore milestones!!!!
  1044. * @return {*}
  1045. */
  1046. GanttMaster.prototype.computeCriticalPath = function () {
  1047. if (!this.tasks)
  1048. return false;
  1049. // do not consider grouping tasks
  1050. var tasks = this.tasks.filter(function (t) {
  1051. //return !t.isParent()
  1052. return (t.getRow() > 0) && (!t.isParent() || (t.isParent() && !t.isDependent()));
  1053. });
  1054. // reset values
  1055. for (var i = 0; i < tasks.length; i++) {
  1056. var t = tasks[i];
  1057. t.earlyStart = -1;
  1058. t.earlyFinish = -1;
  1059. t.latestStart = -1;
  1060. t.latestFinish = -1;
  1061. t.criticalCost = -1;
  1062. t.isCritical = false;
  1063. }
  1064. // tasks whose critical cost has been calculated
  1065. var completed = [];
  1066. // tasks whose critical cost needs to be calculated
  1067. var remaining = tasks.concat(); // put all tasks in remaining
  1068. // Backflow algorithm
  1069. // while there are tasks whose critical cost isn't calculated.
  1070. while (remaining.length > 0) {
  1071. var progress = false;
  1072. // find a new task to calculate
  1073. for (var i = 0; i < remaining.length; i++) {
  1074. var task = remaining[i];
  1075. var inferiorTasks = task.getInferiorTasks();
  1076. if (containsAll(completed, inferiorTasks)) {
  1077. // all dependencies calculated, critical cost is max dependency critical cost, plus our cost
  1078. var critical = 0;
  1079. for (var j = 0; j < inferiorTasks.length; j++) {
  1080. var t = inferiorTasks[j];
  1081. if (t.criticalCost > critical) {
  1082. critical = t.criticalCost;
  1083. }
  1084. }
  1085. task.criticalCost = critical + task.duration;
  1086. // set task as calculated an remove
  1087. completed.push(task);
  1088. remaining.splice(i, 1);
  1089. // note we are making progress
  1090. progress = true;
  1091. }
  1092. }
  1093. // If we haven't made any progress then a cycle must exist in
  1094. // the graph and we wont be able to calculate the critical path
  1095. if (!progress) {
  1096. console.error("Cyclic dependency, algorithm stopped!");
  1097. return false;
  1098. }
  1099. }
  1100. // set earlyStart, earlyFinish, latestStart, latestFinish
  1101. computeMaxCost(tasks);
  1102. var initialNodes = initials(tasks);
  1103. calculateEarly(initialNodes);
  1104. calculateCritical(tasks);
  1105. return tasks;
  1106. function containsAll(set, targets) {
  1107. for (var i = 0; i < targets.length; i++) {
  1108. if (set.indexOf(targets[i]) < 0)
  1109. return false;
  1110. }
  1111. return true;
  1112. }
  1113. function computeMaxCost(tasks) {
  1114. var max = -1;
  1115. for (var i = 0; i < tasks.length; i++) {
  1116. var t = tasks[i];
  1117. if (t.criticalCost > max)
  1118. max = t.criticalCost;
  1119. }
  1120. //console.debug("Critical path length (cost): " + max);
  1121. for (var i = 0; i < tasks.length; i++) {
  1122. var t = tasks[i];
  1123. t.setLatest(max);
  1124. }
  1125. }
  1126. function initials(tasks) {
  1127. var initials = [];
  1128. for (var i = 0; i < tasks.length; i++) {
  1129. if (!tasks[i].depends || tasks[i].depends == "")
  1130. initials.push(tasks[i]);
  1131. }
  1132. return initials;
  1133. }
  1134. function calculateEarly(initials) {
  1135. for (var i = 0; i < initials.length; i++) {
  1136. var initial = initials[i];
  1137. initial.earlyStart = 0;
  1138. initial.earlyFinish = initial.duration;
  1139. setEarly(initial);
  1140. }
  1141. }
  1142. function setEarly(initial) {
  1143. var completionTime = initial.earlyFinish;
  1144. var inferiorTasks = initial.getInferiorTasks();
  1145. for (var i = 0; i < inferiorTasks.length; i++) {
  1146. var t = inferiorTasks[i];
  1147. if (completionTime >= t.earlyStart) {
  1148. t.earlyStart = completionTime;
  1149. t.earlyFinish = completionTime + t.duration;
  1150. }
  1151. setEarly(t);
  1152. }
  1153. }
  1154. function calculateCritical(tasks) {
  1155. for (var i = 0; i < tasks.length; i++) {
  1156. var t = tasks[i];
  1157. t.isCritical = (t.earlyStart == t.latestStart)
  1158. }
  1159. }
  1160. };
  1161. //------------------------------------------- MANAGE CHANGE LOG INPUT ---------------------------------------------------
  1162. GanttMaster.prototype.manageSaveRequired=function(ev, showSave) {
  1163. //console.debug("manageSaveRequired", showSave);
  1164. function checkChanges() {
  1165. var changes = false;
  1166. //there is somethin in the redo stack?
  1167. if (ge.__undoStack.length > 0) {
  1168. var oldProject = JSON.parse(ge.__undoStack[0]);
  1169. //si looppano i "nuovi" task
  1170. for (var i = 0; !changes && i < ge.tasks.length; i++) {
  1171. var newTask = ge.tasks[i];
  1172. //se è un task che c'erà già
  1173. if (typeof (newTask.id)=="String" && !newTask.id.startsWith("tmp_")) {
  1174. //si recupera il vecchio task
  1175. var oldTask;
  1176. for (var j = 0; j < oldProject.tasks.length; j++) {
  1177. if (oldProject.tasks[j].id == newTask.id) {
  1178. oldTask = oldProject.tasks[j];
  1179. break;
  1180. }
  1181. }
  1182. // chack only status or dateChanges
  1183. if (oldTask && (oldTask.status != newTask.status || oldTask.start != newTask.start || oldTask.end != newTask.end)) {
  1184. changes = true;
  1185. break;
  1186. }
  1187. }
  1188. }
  1189. }
  1190. $("#LOG_CHANGES_CONTAINER").css("display", changes ? "inline-block" : "none");
  1191. }
  1192. if (showSave) {
  1193. $("body").stopTime("gantt.manageSaveRequired").oneTime(200, "gantt.manageSaveRequired", checkChanges);
  1194. } else {
  1195. $("#LOG_CHANGES_CONTAINER").hide();
  1196. }
  1197. }