jquery_gantt_rails 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +74 -0
  3. data/.idea/encodings.xml +5 -0
  4. data/.idea/jquery_gantt_rails.iml +9 -0
  5. data/.idea/misc.xml +22 -0
  6. data/.idea/modules.xml +9 -0
  7. data/.idea/scopes/scope_settings.xml +5 -0
  8. data/.idea/vcs.xml +7 -0
  9. data/.ruby-version.rb +1 -0
  10. data/Gemfile +3 -0
  11. data/README.md +41 -0
  12. data/Rakefile +1 -0
  13. data/jquery_gantt_rails.gemspec +20 -0
  14. data/lib/jquery_gantt_rails/version.rb +3 -0
  15. data/lib/jquery_gantt_rails/version.rb~ +3 -0
  16. data/lib/jquery_gantt_rails.rb +9 -0
  17. data/vendor/assets/javascripts/jquery_gantt/README.md +28 -0
  18. data/vendor/assets/javascripts/jquery_gantt/add.gif +0 -0
  19. data/vendor/assets/javascripts/jquery_gantt/alert.gif +0 -0
  20. data/vendor/assets/javascripts/jquery_gantt/closeBig.png +0 -0
  21. data/vendor/assets/javascripts/jquery_gantt/del.gif +0 -0
  22. data/vendor/assets/javascripts/jquery_gantt/edit.gif +0 -0
  23. data/vendor/assets/javascripts/jquery_gantt/gantt.css +323 -0
  24. data/vendor/assets/javascripts/jquery_gantt/gantt.html +547 -0
  25. data/vendor/assets/javascripts/jquery_gantt/ganttDrawer.js +752 -0
  26. data/vendor/assets/javascripts/jquery_gantt/ganttGridEditor.js +518 -0
  27. data/vendor/assets/javascripts/jquery_gantt/ganttMaster.js +702 -0
  28. data/vendor/assets/javascripts/jquery_gantt/ganttTask.js +947 -0
  29. data/vendor/assets/javascripts/jquery_gantt/ganttUtilities.js +237 -0
  30. data/vendor/assets/javascripts/jquery_gantt/gantt_compact.css +323 -0
  31. data/vendor/assets/javascripts/jquery_gantt/hasExternalDeps.png +0 -0
  32. data/vendor/assets/javascripts/jquery_gantt/libs/JST/jquery.JST.js +167 -0
  33. data/vendor/assets/javascripts/jquery_gantt/libs/date.js +584 -0
  34. data/vendor/assets/javascripts/jquery_gantt/libs/dateField/images/next.png +0 -0
  35. data/vendor/assets/javascripts/jquery_gantt/libs/dateField/images/prev.png +0 -0
  36. data/vendor/assets/javascripts/jquery_gantt/libs/dateField/jquery.dateField.css +88 -0
  37. data/vendor/assets/javascripts/jquery_gantt/libs/dateField/jquery.dateField.js +212 -0
  38. data/vendor/assets/javascripts/jquery_gantt/libs/i18nJs.js +140 -0
  39. data/vendor/assets/javascripts/jquery_gantt/libs/jquery.livequery.min.js +11 -0
  40. data/vendor/assets/javascripts/jquery_gantt/libs/jquery.timers.js +142 -0
  41. data/vendor/assets/javascripts/jquery_gantt/libs/platform.js +954 -0
  42. data/vendor/assets/javascripts/jquery_gantt/linkArrow.png +0 -0
  43. data/vendor/assets/javascripts/jquery_gantt/milestone.png +0 -0
  44. data/vendor/assets/javascripts/jquery_gantt/platform.css +346 -0
  45. data/vendor/assets/javascripts/jquery_gantt/teamwork-regular-webfont.eot +0 -0
  46. data/vendor/assets/javascripts/jquery_gantt/teamwork-regular-webfont.otf +0 -0
  47. data/vendor/assets/javascripts/jquery_gantt/teamwork-regular-webfont.svg +152 -0
  48. data/vendor/assets/javascripts/jquery_gantt/teamwork-regular-webfont.ttf +0 -0
  49. data/vendor/assets/javascripts/jquery_gantt/teamwork-regular-webfont.woff +0 -0
  50. data/vendor/assets/javascripts/jquery_gantt/teamworkFont.css +16 -0
  51. data/vendor/assets/javascripts/jquery_gantt/twGanttSmall.png +0 -0
  52. metadata +107 -0
@@ -0,0 +1,702 @@
1
+ /*
2
+ Copyright (c) 2012-2013 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
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+ function GanttMaster() {
24
+ this.tasks = [];
25
+ this.deletedTaskIds=[];
26
+ this.links = [];
27
+
28
+ this.editor; //element for editor
29
+ this.gantt; //element for gantt
30
+
31
+ this.element;
32
+
33
+
34
+ this.resources; //list of resources
35
+ this.roles; //list of roles
36
+
37
+ this.minEditableDate = 0;
38
+ this.maxEditableDate = Infinity;
39
+
40
+ this.canWriteOnParent=true;
41
+ this.canWrite=true;
42
+
43
+ this.firstDayOfWeek = Date.firstDayOfWeek;
44
+
45
+ this.currentTask; // task currently selected;
46
+
47
+ this.__currentTransaction; // a transaction object holds previous state during changes
48
+ this.__undoStack = [];
49
+ this.__redoStack = [];
50
+
51
+ var self = this;
52
+ }
53
+
54
+ GanttMaster.prototype.init = function(place) {
55
+ this.element = place;
56
+
57
+ var self=this;
58
+
59
+ //load templates
60
+ $("#gantEditorTemplates").loadTemplates().remove(); // TODO: Remove inline jquery, this should be injected
61
+
62
+ //create editor
63
+ this.editor = new GridEditor(this);
64
+ this.editor.element.width(place.width() * .9 - 10);
65
+ place.append(this.editor.element);
66
+
67
+ //create gantt
68
+ this.gantt = new Ganttalendar("m", new Date().getTime() - 3600000 * 24 * 2, new Date().getTime() + 3600000 * 24 * 15, this, place.width() * .6);
69
+
70
+ //setup splitter
71
+ var splitter = $.splittify.init(place, this.editor.element, this.gantt.element, 70);
72
+ splitter.secondBox.css("overflow-y", "auto").scroll(function() {
73
+ splitter.firstBox.scrollTop(splitter.secondBox.scrollTop());
74
+ });
75
+
76
+
77
+ //prepend buttons
78
+ place.before($.JST.createFromTemplate({}, "GANTBUTTONS"));
79
+
80
+
81
+ //bindings
82
+ place.bind("refreshTasks.gantt", function() {
83
+ self.redrawTasks();
84
+ }).bind("refreshTask.gantt", function(e, task) {
85
+ self.drawTask(task);
86
+
87
+ }).bind("deleteCurrentTask.gantt", function(e) {
88
+ var row = self.currentTask.getRow();
89
+ if (self.currentTask && (row>0 || self.currentTask.isNew())) {
90
+ self.beginTransaction();
91
+
92
+ self.currentTask.deleteTask();
93
+
94
+ self.currentTask = undefined;
95
+ //recompute depends string
96
+ self.updateDependsStrings();
97
+
98
+ //redraw
99
+ self.redraw();
100
+
101
+ //focus next row
102
+ row = row > self.tasks.length - 1 ? self.tasks.length - 1 : row;
103
+ if (row >= 0) {
104
+ self.currentTask = self.tasks[row];
105
+ self.currentTask.rowElement.click();
106
+ self.currentTask.rowElement.find("[name=name]").focus();
107
+ }
108
+ self.endTransaction();
109
+ }
110
+
111
+
112
+ }).bind("addAboveCurrentTask.gantt", function() {
113
+ var factory = new TaskFactory();
114
+
115
+ var ch;
116
+ var row = 0;
117
+ if (self.currentTask) {
118
+ //cannot add brothers to root
119
+ if (self.currentTask.level<=0)
120
+ return;
121
+
122
+ ch = factory.build("tmp_" + new Date().getTime(), "", "", self.currentTask.level, self.currentTask.start, 1);
123
+ row = self.currentTask.getRow();
124
+ } else {
125
+ ch = factory.build("tmp_" + new Date().getTime(), "", "", 0, new Date().getTime(), 1);
126
+ }
127
+ self.beginTransaction();
128
+ var task = self.addTask(ch, row);
129
+ if (task) {
130
+ task.rowElement.click();
131
+ task.rowElement.find("[name=name]").focus();
132
+ }
133
+ self.endTransaction();
134
+
135
+ }).bind("addBelowCurrentTask.gantt", function() {
136
+ var factory = new TaskFactory();
137
+ self.beginTransaction();
138
+ var ch;
139
+ var row = 0;
140
+ if (self.currentTask) {
141
+ ch = factory.build("tmp_" + new Date().getTime(), "", "", self.currentTask.level + 1, self.currentTask.start, 1);
142
+ row = self.currentTask.getRow() + 1;
143
+ } else {
144
+ ch = factory.build("tmp_" + new Date().getTime(), "", "", 0, new Date().getTime(), 1);
145
+ }
146
+ var task = self.addTask(ch, row);
147
+ if (task) {
148
+ task.rowElement.click();
149
+ task.rowElement.find("[name=name]").focus();
150
+ }
151
+ self.endTransaction();
152
+
153
+
154
+ }).bind("indentCurrentTask.gantt", function() {
155
+ if (self.currentTask) {
156
+ self.beginTransaction();
157
+ self.currentTask.indent();
158
+ self.endTransaction();
159
+ }
160
+
161
+ }).bind("outdentCurrentTask.gantt", function() {
162
+ if (self.currentTask) {
163
+ self.beginTransaction();
164
+ self.currentTask.outdent();
165
+ self.endTransaction();
166
+ }
167
+
168
+ }).bind("moveUpCurrentTask.gantt", function() {
169
+ if (self.currentTask) {
170
+ self.beginTransaction();
171
+ self.currentTask.moveUp();
172
+ self.endTransaction();
173
+ }
174
+ }).bind("moveDownCurrentTask.gantt", function() {
175
+ if (self.currentTask) {
176
+ self.beginTransaction();
177
+ self.currentTask.moveDown();
178
+ self.endTransaction();
179
+ }
180
+
181
+ }).bind("zoomPlus.gantt", function() {
182
+ self.gantt.zoomGantt(true);
183
+ }).bind("zoomMinus.gantt", function() {
184
+ self.gantt.zoomGantt(false);
185
+
186
+ }).bind("undo.gantt", function() {
187
+ self.undo();
188
+ }).bind("redo.gantt", function() {
189
+ self.redo();
190
+ });
191
+ };
192
+
193
+ GanttMaster.messages = {
194
+ "CHANGE_OUT_OF_SCOPE": "NO_RIGHTS_FOR_UPDATE_PARENTS_OUT_OF_EDITOR_SCOPE",
195
+ "START_IS_MILESTONE": "START_IS_MILESTONE",
196
+ "END_IS_MILESTONE": "END_IS_MILESTONE",
197
+ "TASK_HAS_CONSTRAINTS": "TASK_HAS_CONSTRAINTS",
198
+ "GANTT_ERROR_DEPENDS_ON_OPEN_TASK": "GANTT_ERROR_DEPENDS_ON_OPEN_TASK",
199
+ "GANTT_ERROR_DESCENDANT_OF_CLOSED_TASK":"GANTT_ERROR_DESCENDANT_OF_CLOSED_TASK",
200
+ "TASK_HAS_EXTERNAL_DEPS": "TASK_HAS_EXTERNAL_DEPS",
201
+ "GANTT_ERROR_LOADING_DATA_TASK_REMOVED":"GANTT_ERROR_LOADING_DATA_TASK_REMOVED",
202
+ "CIRCULAR_REFERENCE": "CIRCULAR_REFERENCE",
203
+ "ERROR_SETTING_DATES": "ERROR_SETTING_DATES",
204
+ "CANNOT_DEPENDS_ON_ANCESTORS": "CANNOT_DEPENDS_ON_ANCESTORS",
205
+ "CANNOT_DEPENDS_ON_DESCENDANTS": "CANNOT_DEPENDS_ON_DESCENDANTS",
206
+ "INVALID_DATE_FORMAT": "INVALID_DATE_FORMAT",
207
+ "GANTT_QUARTER_SHORT": "GANTT_QUARTER_SHORT",
208
+ "GANTT_SEMESTER_SHORT":"GANTT_SEMESTER_SHORT"
209
+ };
210
+
211
+
212
+ GanttMaster.prototype.createTask = function (id, name, code, level, start, duration) {
213
+ var factory = new TaskFactory();
214
+
215
+ return factory.build(id, name, code, level, start, duration);
216
+ };
217
+
218
+
219
+ GanttMaster.prototype.createResource = function (id, name) {
220
+ var res = new Resource(id, name);
221
+ return res;
222
+ };
223
+
224
+
225
+ //update depends strings
226
+ GanttMaster.prototype.updateDependsStrings = function() {
227
+ //remove all deps
228
+ for (var i=0;i<this.tasks.length;i++) {
229
+ this.tasks[i].depends = "";
230
+ }
231
+
232
+ for (var i=0;i<this.links.length;i++) {
233
+ var link = this.links[i];
234
+ var dep = link.to.depends;
235
+ link.to.depends = link.to.depends + (link.to.depends == "" ? "" : ",") + (link.from.getRow() + 1) + (link.lag ? ":" + link.lag : "");
236
+ }
237
+
238
+ };
239
+
240
+ //------------------------------------ ADD TASK --------------------------------------------
241
+ GanttMaster.prototype.addTask = function(task, row) {
242
+ //console.debug("master.addTask",task,row,this);
243
+ task.master = this; // in order to access controller from task
244
+
245
+ //replace if already exists
246
+ var pos = -1;
247
+ for (var i=0;i<this.tasks.length;i++) {
248
+ if (task.id == this.tasks[i].id) {
249
+ pos = i;
250
+ break;
251
+ }
252
+ }
253
+
254
+ if (pos >= 0) {
255
+ this.tasks.splice(pos, 1);
256
+ row = parseInt(pos);
257
+ }
258
+
259
+ //add task in collection
260
+ if (typeof(row) != "number") {
261
+ this.tasks.push(task);
262
+ } else {
263
+ this.tasks.splice(row, 0, task);
264
+
265
+ //recompute depends string
266
+ this.updateDependsStrings();
267
+ }
268
+
269
+ //add Link collection in memory
270
+ var linkLoops = !this.updateLinks(task);
271
+
272
+ //set the status according to parent
273
+ if (task.getParent())
274
+ task.status=task.getParent().status;
275
+ else
276
+ task.status="STATUS_ACTIVE";
277
+
278
+
279
+ var ret = task;
280
+ if (linkLoops || !task.setPeriod(task.start, task.end)) {
281
+ //remove task from in-memory collection
282
+ //console.debug("removing task from memory",task);
283
+ this.tasks.splice(task.getRow(), 1);
284
+ ret = undefined;
285
+ } else {
286
+ //append task to editor
287
+ this.editor.addTask(task, row);
288
+ //append task to gantt
289
+ this.gantt.addTask(task);
290
+ }
291
+ return ret;
292
+ };
293
+
294
+
295
+ /**
296
+ * a project contais tasks, resources, roles, and info about permisions
297
+ * @param project
298
+ */
299
+ GanttMaster.prototype.loadProject = function(project) {
300
+ this.beginTransaction();
301
+ this.resources = project.resources;
302
+ this.roles = project.roles;
303
+ this.canWrite = project.canWrite;
304
+ this.canWriteOnParent = project.canWriteOnParent;
305
+
306
+ if (project.minEditableDate)
307
+ this.minEditableDate = computeStart(project.minEditableDate);
308
+ else
309
+ this.minEditableDate =-Infinity;
310
+
311
+ if (project.maxEditableDate)
312
+ this.maxEditableDate =computeEnd(project.maxEditableDate);
313
+ else
314
+ this.maxEditableDate =Infinity;
315
+
316
+ this.loadTasks(project.tasks, project.selectedRow);
317
+ this.deletedTaskIds=[];
318
+ this.endTransaction();
319
+ var self=this;
320
+ this.gantt.element.oneTime(200,function(){self.gantt.centerOnToday()});
321
+ };
322
+
323
+
324
+ GanttMaster.prototype.loadTasks = function(tasks, selectedRow) {
325
+ var factory = new TaskFactory();
326
+ //reset
327
+ this.reset();
328
+
329
+ for (var i=0;i<tasks.length;i++){
330
+ var task = tasks[i];
331
+ if (!(task instanceof Task)) {
332
+ var t = factory.build(task.id, task.name, task.code, task.level, task.start, task.duration);
333
+ for (var key in task) {
334
+ if (key!="end" && key!="start")
335
+ t[key] = task[key]; //copy all properties
336
+ }
337
+ task = t;
338
+ }
339
+ task.master = this; // in order to access controller from task
340
+
341
+ /*//replace if already exists
342
+ var pos = -1;
343
+ for (var i=0;i<this.tasks.length;i++) {
344
+ if (task.id == this.tasks[i].id) {
345
+ pos = i;
346
+ break;
347
+ }
348
+ }*/
349
+
350
+ this.tasks.push(task); //append task at the end
351
+ }
352
+
353
+ //var prof=new Profiler("gm_loadTasks_addTaskLoop");
354
+ for (var i=0;i<this.tasks.length;i++) {
355
+ var task = this.tasks[i];
356
+
357
+ //add Link collection in memory
358
+ var linkLoops = !this.updateLinks(task);
359
+
360
+ if (linkLoops || !task.setPeriod(task.start, task.end)) {
361
+ alert(GanttMaster.messages.GANNT_ERROR_LOADING_DATA_TASK_REMOVED+"\n" + task.name+"\n"+
362
+ (linkLoops?GanttMaster.messages.CIRCULAR_REFERENCE:GanttMaster.messages.ERROR_SETTING_DATES));
363
+
364
+ //remove task from in-memory collection
365
+ this.tasks.splice(task.getRow(), 1);
366
+ } else {
367
+ //append task to editor
368
+ this.editor.addTask(task);
369
+ //append task to gantt
370
+ this.gantt.addTask(task);
371
+ }
372
+ }
373
+
374
+ this.editor.fillEmptyLines();
375
+ //prof.stop();
376
+
377
+ // re-select old row if tasks is not empty
378
+ if (this.tasks && this.tasks.length>0){
379
+ selectedRow = selectedRow ? selectedRow : 0;
380
+ this.tasks[selectedRow].rowElement.click();
381
+ }
382
+
383
+ };
384
+
385
+
386
+ GanttMaster.prototype.getTask = function(taskId) {
387
+ var ret;
388
+ for (var i=0;i<this.tasks.length;i++) {
389
+ var tsk = this.tasks[i];
390
+ if (tsk.id == taskId) {
391
+ ret = tsk;
392
+ break;
393
+ }
394
+ }
395
+ return ret;
396
+ };
397
+
398
+
399
+ GanttMaster.prototype.getResource = function(resId) {
400
+ var ret;
401
+ for (var i=0;i<this.resources.length;i++) {
402
+ var res = this.resources[i];
403
+ if (res.id == resId) {
404
+ ret = res;
405
+ break;
406
+ }
407
+ }
408
+ return ret;
409
+ };
410
+
411
+
412
+ GanttMaster.prototype.changeTaskDates = function(task, start, end) {
413
+ return task.setPeriod(start, end);
414
+ };
415
+
416
+
417
+ GanttMaster.prototype.moveTask = function(task, newStart) {
418
+ return task.moveTo(newStart, true);
419
+ };
420
+
421
+
422
+ GanttMaster.prototype.taskIsChanged = function() {
423
+ //console.debug("taskIsChanged");
424
+ var master=this;
425
+
426
+ //refresh is executed only once every 50ms
427
+ this.element.stopTime("gnnttaskIsChanged");
428
+ //var profilerext = new Profiler("gm_taskIsChangedRequest");
429
+ this.element.oneTime(50, "gnnttaskIsChanged", function() {
430
+ //console.debug("task Is Changed real call to redraw");
431
+ //var profiler = new Profiler("gm_taskIsChangedReal");
432
+ master.editor.redraw();
433
+ master.gantt.refreshGantt();
434
+ //profiler.stop();
435
+ });
436
+ //profilerext.stop();
437
+ };
438
+
439
+
440
+ GanttMaster.prototype.redraw = function() {
441
+ this.editor.redraw();
442
+ this.gantt.refreshGantt();
443
+ };
444
+
445
+ GanttMaster.prototype.reset = function() {
446
+ this.tasks = [];
447
+ this.links = [];
448
+ this.deletedTaskIds=[];
449
+ this.__undoStack = [];
450
+ this.__redoStack = [];
451
+ delete this.currentTask;
452
+
453
+ this.editor.reset();
454
+ this.gantt.reset();
455
+ };
456
+
457
+
458
+ GanttMaster.prototype.showTaskEditor = function(taskId) {
459
+ var task = this.getTask(taskId);
460
+ task.rowElement.find(".edit").click();
461
+ };
462
+
463
+ GanttMaster.prototype.saveProject = function() {
464
+ return this.saveGantt(false);
465
+ };
466
+
467
+ GanttMaster.prototype.saveGantt = function(forTransaction) {
468
+ //var prof = new Profiler("gm_saveGantt");
469
+ var saved = [];
470
+ for (var i=0;i<this.tasks.length;i++) {
471
+ var task = this.tasks[i];
472
+ var cloned = task.clone();
473
+ delete cloned.master;
474
+ delete cloned.rowElement;
475
+ delete cloned.ganttElement;
476
+
477
+ saved.push(cloned);
478
+ }
479
+
480
+ var ret = {tasks:saved};
481
+ if (this.currentTask) {
482
+ ret.selectedRow = this.currentTask.getRow();
483
+ }
484
+
485
+ ret.deletedTaskIds=this.deletedTaskIds; //this must be consistent with transactions and undo
486
+
487
+ if (!forTransaction) {
488
+ ret.resources = this.resources;
489
+ ret.roles = this.roles;
490
+ ret.canWrite = this.canWrite;
491
+ ret.canWriteOnParent = this.canWriteOnParent;
492
+ }
493
+
494
+ //prof.stop();
495
+ return ret;
496
+ };
497
+
498
+
499
+ GanttMaster.prototype.updateLinks = function(task) {
500
+ //console.debug("updateLinks");
501
+ //var prof= new Profiler("gm_updateLinks");
502
+
503
+ // defines isLoop function
504
+ function isLoop(task, target, visited) {
505
+ if (target == task) {
506
+ return true;
507
+ }
508
+
509
+ var sups = task.getSuperiors();
510
+ var loop = false;
511
+ for (var i=0;i<sups.length;i++) {
512
+ var supLink = sups[i];
513
+ if (supLink.from == target) {
514
+ loop = true;
515
+ break;
516
+ } else {
517
+ if (visited.indexOf(supLink.from) <= 0) {
518
+ visited.push(supLink.from);
519
+ if (isLoop(supLink.from, target, visited)) {
520
+ loop = true;
521
+ break;
522
+ }
523
+ }
524
+ }
525
+ }
526
+ return loop;
527
+ }
528
+
529
+ //remove my depends
530
+ this.links = this.links.filter(function(link) {
531
+ return link.to != task;
532
+ });
533
+
534
+ var todoOk = true;
535
+ if (task.depends) {
536
+
537
+ //cannot depend from an ancestor
538
+ var parents = task.getParents();
539
+ //cannot depend from descendants
540
+ var descendants=task.getDescendant();
541
+
542
+ var deps = task.depends.split(",");
543
+ var newDepsString = "";
544
+
545
+ var visited = [];
546
+ for (var j=0;j<deps.length;j++) {
547
+ var dep = deps[j]; // in the form of row(lag) e.g. 2:3,3:4,5
548
+ var par = dep.split(":");
549
+ var lag = 0;
550
+
551
+ if (par.length > 1) {
552
+ lag = parseInt(par[1]);
553
+ }
554
+
555
+ var sup = this.tasks[parseInt(par[0] - 1)];
556
+
557
+ if (sup) {
558
+ if (parents && parents.indexOf(sup) >= 0) {
559
+ this.setErrorOnTransaction(task.name + "\n"+GanttMaster.messages.CANNOT_DEPENDS_ON_ANCESTORS+"\n" + sup.name);
560
+ todoOk = false;
561
+
562
+ } else if (descendants && descendants.indexOf(sup) >= 0) {
563
+ this.setErrorOnTransaction(task.name + "\n"+GanttMaster.messages.CANNOT_DEPENDS_ON_DESCENDANTS+"\n" + sup.name);
564
+ todoOk = false;
565
+
566
+ } else if (isLoop(sup, task, visited)) {
567
+ todoOk = false;
568
+ this.setErrorOnTransaction(GanttMaster.messages.CIRCULAR_REFERENCE+"\n" + task.name + " -> " + sup.name);
569
+ } else {
570
+ this.links.push(new Link(sup, task, lag));
571
+ newDepsString = newDepsString + (newDepsString.length > 0 ? "," : "") + dep;
572
+ }
573
+ }
574
+ }
575
+
576
+ if (todoOk) {
577
+ task.depends = newDepsString;
578
+ }
579
+
580
+ }
581
+
582
+ //prof.stop();
583
+
584
+ return todoOk;
585
+ };
586
+
587
+
588
+ //<%----------------------------- TRANSACTION MANAGEMENT ---------------------------------%>
589
+ GanttMaster.prototype.beginTransaction = function() {
590
+ if (!this.__currentTransaction) {
591
+ this.__currentTransaction = {
592
+ snapshot:JSON.stringify(this.saveGantt(true)),
593
+ errors:[]
594
+ };
595
+ } else {
596
+ console.error("Cannot open twice a transaction");
597
+ }
598
+ return this.__currentTransaction;
599
+ };
600
+
601
+
602
+ GanttMaster.prototype.endTransaction = function() {
603
+ if (!this.__currentTransaction) {
604
+ console.error("Transaction never started.");
605
+ return true;
606
+ }
607
+
608
+ var ret = true;
609
+
610
+ //no error -> commit
611
+ if (this.__currentTransaction.errors.length <= 0) {
612
+ //console.debug("committing transaction");
613
+
614
+ //put snapshot in undo
615
+ this.__undoStack.push(this.__currentTransaction.snapshot);
616
+ //clear redo stack
617
+ this.__redoStack = [];
618
+
619
+ //shrink gantt bundaries
620
+ this.gantt.originalStartMillis = Infinity;
621
+ this.gantt.originalEndMillis = -Infinity;
622
+ for (var i=0;i<this.tasks.length;i++) {
623
+ var task = this.tasks[i];
624
+ if (this.gantt.originalStartMillis > task.start)
625
+ this.gantt.originalStartMillis = task.start;
626
+ if (this.gantt.originalEndMillis < task.end)
627
+ this.gantt.originalEndMillis = task.end;
628
+
629
+ }
630
+ this.taskIsChanged(); //enqueue for gantt refresh
631
+
632
+
633
+ //error -> rollback
634
+ } else {
635
+ ret = false;
636
+ //console.debug("rolling-back transaction");
637
+ //try to restore changed tasks
638
+ var oldTasks = JSON.parse(this.__currentTransaction.snapshot);
639
+ this.deletedTaskIds=oldTasks.deletedTaskIds;
640
+ this.loadTasks(oldTasks.tasks, oldTasks.selectedRow);
641
+ this.redraw();
642
+
643
+ //compose error message
644
+ var msg = "";
645
+ for (var i=0;i<this.__currentTransaction.errors.length;i++) {
646
+ var err = this.__currentTransaction.errors[i];
647
+ msg = msg + err.msg + "\n\n";
648
+ }
649
+ alert(msg);
650
+ }
651
+ //reset transaction
652
+ this.__currentTransaction = undefined;
653
+
654
+ return ret;
655
+ };
656
+
657
+ //this function notify an error to a transaction -> transaction will rollback
658
+ GanttMaster.prototype.setErrorOnTransaction = function(errorMessage, task) {
659
+ if (this.__currentTransaction) {
660
+ this.__currentTransaction.errors.push({msg:errorMessage,task:task});
661
+ } else {
662
+ console.error(errorMessage);
663
+ }
664
+ };
665
+
666
+ // inhibit undo-redo
667
+ GanttMaster.prototype.checkpoint= function() {
668
+ this.__undoStack = [];
669
+ this.__redoStack = [];
670
+ };
671
+
672
+ //----------------------------- UNDO/REDO MANAGEMENT ---------------------------------%>
673
+
674
+ GanttMaster.prototype.undo = function() {
675
+ //console.debug("undo before:",undoStack,redoStack);
676
+ if (this.__undoStack.length > 0) {
677
+ var his = this.__undoStack.pop();
678
+ this.__redoStack.push(JSON.stringify(this.saveGantt()));
679
+
680
+ var oldTasks = JSON.parse(his);
681
+ this.deletedTaskIds=oldTasks.deletedTaskIds;
682
+ this.loadTasks(oldTasks.tasks, oldTasks.selectedRow);
683
+ //console.debug(oldTasks,oldTasks.deletedTaskIds)
684
+ this.redraw();
685
+ //console.debug("undo after:",undoStack,redoStack);
686
+ }
687
+ };
688
+
689
+ GanttMaster.prototype.redo = function() {
690
+ //console.debug("redo before:",undoStack,redoStack);
691
+ if (this.__redoStack.length > 0) {
692
+ var his = this.__redoStack.pop();
693
+ this.__undoStack.push(JSON.stringify(this.saveGantt()));
694
+
695
+ var oldTasks = JSON.parse(his);
696
+ this.deletedTaskIds=oldTasks.deletedTaskIds;
697
+ this.loadTasks(oldTasks.tasks, oldTasks.selectedRow);
698
+ this.redraw();
699
+ //console.debug("redo after:",undoStack,redoStack);
700
+ }
701
+ };
702
+