forked from robicch/jQueryGantt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ganttTask.js
1090 lines (876 loc) · 32.5 KB
/
ganttTask.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2012-2017 Open Lab
Written by Roberto Bicchierai and Silvia Chelazzi http://roberto.open-lab.com
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* A method to instantiate valid task models from
* raw data.
*/
function TaskFactory() {
/**
* Build a new Task
*/
this.build = function (id, name, code, level, start, duration, collapsed) {
// Set at beginning of day
var adjusted_start = computeStart(start);
var calculated_end = computeEndByDuration(adjusted_start, duration);
return new Task(id, name, code, level, adjusted_start, calculated_end, duration, collapsed);
};
}
function Task(id, name, code, level, start, end, duration, collapsed) {
this.id = id;
this.name = name;
this.progress = 0;
this.progressByWorklog = false;
this.relevance = 0;
this.type = "";
this.typeId = "";
this.description = "";
this.code = code;
this.level = level;
this.status = "STATUS_UNDEFINED";
this.depends = "";
this.canWrite = true; // by default all tasks are writeable
this.start = start;
this.duration = duration;
this.end = end;
this.startIsMilestone = false;
this.endIsMilestone = false;
this.collapsed = collapsed;
this.rowElement; //row editor html element
this.ganttElement; //gantt html element
this.master;
this.assigs = [];
}
Task.prototype.clone = function () {
var ret = {};
for (var key in this) {
if (typeof(this[key]) != "function") {
ret[key] = this[key];
}
}
return ret;
};
Task.prototype.getAssigsString = function () {
var ret = "";
for (var i = 0; i < this.assigs.length; i++) {
var ass = this.assigs[i];
var res = this.master.getResource(ass.resourceId);
if (res) {
ret = ret + (ret == "" ? "" : ", ") + res.name;
}
}
return ret;
};
Task.prototype.createAssignment = function (id, resourceId, roleId, effort) {
var assig = new Assignment(id, resourceId, roleId, effort);
this.assigs.push(assig);
return assig;
};
//<%---------- SET PERIOD ---------------------- --%>
Task.prototype.setPeriod = function (start, end) {
//console.debug("setPeriod ",this.code,this.name,new Date(start), new Date(end));
//var profilerSetPer = new Profiler("gt_setPeriodJS");
if (start instanceof Date) {
start = start.getTime();
}
if (end instanceof Date) {
end = end.getTime();
}
var originalPeriod = {
start: this.start,
end: this.end,
duration: this.duration
};
//compute legal start/end //todo mossa qui R&S 30/3/2016 perchè altrimenti il calcolo della durata, che è stato modificato sommando giorni, sbaglia
start = computeStart(start);
end=computeEnd(end);
var newDuration = recomputeDuration(start, end);
//if are equals do nothing and return true
if ( start == originalPeriod.start && end == originalPeriod.end && newDuration == originalPeriod.duration) {
//console.debug("Periods are identical!")
return true;
}
if (newDuration == this.duration) { // is shift
return this.moveTo(start, false);
}
//console.debug("setStart",date,date instanceof Date);
var wantedStartMillis = start;
//cannot start after end
if (start > end) {
start = end;
}
//set a legal start
//start = computeStart(start); //todo R&S 30/3/2016 messo in vetta
//if there are dependencies compute the start date and eventually moveTo
var startBySuperiors = this.computeStartBySuperiors(start);
if (startBySuperiors != start) {
return this.moveTo(startBySuperiors, false);
}
var somethingChanged = false;
if (this.start != start || this.start != wantedStartMillis) {
this.start = start;
somethingChanged = true;
}
//set end
var wantedEndMillis = end;
//end = computeEnd(end);//todo R&S 30/3/2016 messo in vetta
if (this.end != end || this.end != wantedEndMillis) {
this.end = end;
somethingChanged = true;
}
this.duration = recomputeDuration(this.start, this.end);
//profilerSetPer.stop();
//nothing changed exit
if (!somethingChanged)
return true;
//cannot write exit
if (!this.canWrite) {
this.master.setErrorOnTransaction("\"" + this.name + "\"\n" + GanttMaster.messages["CANNOT_WRITE"], this);
return false;
}
//external dependencies: exit with error
if (this.hasExternalDep) {
this.master.setErrorOnTransaction("\"" + this.name + "\"\n" + GanttMaster.messages["TASK_HAS_EXTERNAL_DEPS"], this);
return false;
}
var todoOk = true;
//I'm restricting
var deltaPeriod = originalPeriod.duration - this.duration;
var restricting = deltaPeriod > 0;
var restrictingStart = restricting && (originalPeriod.start < this.start);
var restrictingEnd = restricting && (originalPeriod.end > this.end);
//console.debug( " originalPeriod.duration "+ originalPeriod.duration +" deltaPeriod "+deltaPeriod+" "+"restricting "+restricting);
if (restricting) {
//loops children to get boundaries
var children = this.getChildren();
var bs = Infinity;
var be = 0;
for (var i = 0; i < children.length; i++) {
var ch = children[i];
//console.debug("restricting: test child "+ch.name+" "+ch.end)
if (restrictingEnd) {
be = Math.max(be, ch.end);
} else {
bs = Math.min(bs, ch.start);
}
}
if (restrictingEnd) {
//console.debug("restricting end ",be, this.end);
this.end = Math.max(be, this.end);
} else {
//console.debug("restricting start");
this.start = Math.min(bs, this.start);
}
this.duration = recomputeDuration(this.start, this.end);
} else {
//check global boundaries
if (this.start < this.master.minEditableDate || this.end > this.master.maxEditableDate) {
this.master.setErrorOnTransaction("\"" + this.name + "\"\n" +GanttMaster.messages["CHANGE_OUT_OF_SCOPE"], this);
todoOk = false;
}
//console.debug("set period: somethingChanged",this);
if (todoOk && !updateTree(this)) {
todoOk = false;
}
}
if (todoOk) {
todoOk = this.propagateToInferiors(end);
}
return todoOk;
};
//<%---------- MOVE TO ---------------------- --%>
Task.prototype.moveTo = function (start, ignoreMilestones) {
//console.debug("moveTo ",this.code,this.name,new Date(start),this.duration,ignoreMilestones);
//var profiler = new Profiler("gt_task_moveTo");
if (start instanceof Date) {
start = start.getTime();
}
var originalPeriod = {
start: this.start,
end: this.end
};
var wantedStartMillis = start;
//set a legal start
start = computeStart(start);
//if start is milestone cannot be move
if (!ignoreMilestones && this.startIsMilestone && start != this.start) {
//notify error
this.master.setErrorOnTransaction("\"" + this.name + "\"\n" +GanttMaster.messages["START_IS_MILESTONE"], this);
return false;
} else if (this.hasExternalDep) {
//notify error
this.master.setErrorOnTransaction("\"" + this.name + "\"\n" +GanttMaster.messages["TASK_HAS_EXTERNAL_DEPS"], this);
return false;
}
//if depends, start is set to max end + lag of superior
var startBySuperiors = this.computeStartBySuperiors(start);
//todo if there are dependencies the new start,end must be contained into parent dates
/*var parent=this.getParent();
if (start!=startBySuperiors){
var proposedEnd = computeEndByDuration(startBySuperiors, this.duration);
// if outside parent's scoce error
if (parent && (startBySuperiors<parent.start || proposedEnd>parent.end)) {
this.master.setErrorOnTransaction("\"" + this.name + "\"\n" +GanttMaster.messages["CANNOT_MOVE_TASK"], this);
return false;
} else {
start = startBySuperiors;
}
}*/
start = startBySuperiors;
var end = computeEndByDuration(start, this.duration);
if (this.start != start || this.start != wantedStartMillis) {
/*//in case of end is milestone it never changes, but recompute duration
if (!ignoreMilestones && this.endIsMilestone) {
end = this.end;
this.duration = recomputeDuration(start, end);
}*/
//in case of end is milestone it never changes!
if (!ignoreMilestones && this.endIsMilestone && end!=this.end) {
this.master.setErrorOnTransaction("\"" + this.name + "\"\n" +GanttMaster.messages["END_IS_MILESTONE"], this);
return false;
}
this.start = start;
this.end = end;
//profiler.stop();
//check global boundaries
if (this.start < this.master.minEditableDate || this.end > this.master.maxEditableDate) {
this.master.setErrorOnTransaction("\"" + this.name + "\"\n" +GanttMaster.messages["CHANGE_OUT_OF_SCOPE"], this);
return false;
}
// bicch 22/4/2016: quando si sposta un task con child a cavallo di holidays, i figli devono essere shiftati in workingDays, non in millisecondi, altrimenti si cambiano le durate
// when moving children you MUST consider WORKING days,
var panDeltaInWD = new Date(originalPeriod.start).distanceInWorkingDays(new Date(this.start));
//loops children to shift them
var children = this.getChildren();
for (var i = 0; i < children.length; i++) {
ch = children[i];
var chStart=new Date(ch.start).incrementDateByWorkingDays(panDeltaInWD);
if (!ch.moveTo(chStart, false)) {
//console.debug("esco")
return false;
}
}
//console.debug("set period: somethingChanged",this);
if (!updateTree(this)) {
return false;
}
return this.propagateToInferiors(end);
}
return true;
};
//<%---------- PROPAGATE TO INFERIORS ---------------------- --%>
Task.prototype.propagateToInferiors = function (end) {
//and now propagate to inferiors
var todoOk = true;
var infs = this.getInferiors();
if (infs && infs.length > 0) {
for (var i = 0; i < infs.length; i++) {
var link = infs[i];
if (!link.to.canWrite) {
this.master.setErrorOnTransaction(GanttMaster.messages["CANNOT_WRITE"] + "\n\"" + link.to.name + "\"", link.to);
break;
}
todoOk = link.to.moveTo(end, false); //this is not the right date but moveTo checks start
if (!todoOk)
break;
}
}
return todoOk;
};
//<%---------- COMPUTE START BY SUPERIORS ---------------------- --%>
Task.prototype.computeStartBySuperiors = function (proposedStart) {
//if depends -> start is set to max end + lag of superior
var supEnd=proposedStart;
var sups = this.getSuperiors();
if (sups && sups.length > 0) {
supEnd=0;
for (var i = 0; i < sups.length; i++) {
var link = sups[i];
supEnd = Math.max(supEnd, incrementDateByWorkingDays(link.from.end, link.lag));
}
supEnd+=1;
}
return computeStart(supEnd);
};
function updateTree(task) {
//console.debug("updateTree ",task.code,task.name);
var error;
//try to enlarge parent
var p = task.getParent();
//no parent:exit
if (!p)
return true;
var newStart = p.start;
var newEnd = p.end;
if (p.start > task.start) {
if (p.startIsMilestone) {
task.master.setErrorOnTransaction("\"" + p.name + "\"\n" + GanttMaster.messages["START_IS_MILESTONE"], task);
return false;
} else if (p.depends) {
task.master.setErrorOnTransaction("\"" + p.name + "\"\n" + GanttMaster.messages["TASK_HAS_CONSTRAINTS"], task);
return false;
}
newStart = task.start;
}
if (p.end < task.end) {
if (p.endIsMilestone) {
task.master.setErrorOnTransaction("\"" + p.name + "\"\n" + GanttMaster.messages["END_IS_MILESTONE"], task);
return false;
}
newEnd = task.end;
}
//propagate updates if needed
if (newStart != p.start || newEnd != p.end) {
//can write?
if (!p.canWrite) {
task.master.setErrorOnTransaction(GanttMaster.messages["CANNOT_WRITE"] + "\n" + p.name, task);
return false;
}
//has external deps ?
if (p.hasExternalDep) {
task.master.setErrorOnTransaction(GanttMaster.messages["TASK_HAS_EXTERNAL_DEPS"] + "\n\"" + p.name + "\"", task);
return false;
}
return p.setPeriod(newStart, newEnd);
}
return true;
}
//<%---------- CHANGE STATUS ---------------------- --%>
Task.prototype.changeStatus = function (newStatus,forceStatusCheck) {
//console.debug("changeStatus: "+this.name+" from "+this.status+" -> "+newStatus);
var cone = this.getDescendant();
function propagateStatus(task, newStatus, manuallyChanged, propagateFromParent, propagateFromChildren) {
//console.debug("propagateStatus",task.name, task.status,newStatus, manuallyChanged, propagateFromParent, propagateFromChildren);
var oldStatus = task.status;
//no changes exit
if (newStatus == oldStatus && !forceStatusCheck) {
return true;
}
var todoOk = true;
task.status = newStatus;
//xxxx -> STATUS_DONE may activate dependent tasks, both suspended and undefined. Will set to done all children.
//STATUS_FAILED -> STATUS_DONE do nothing if not forced by hand
if (newStatus == "STATUS_DONE") {
// cannot close task if open issues
if (task.master.permissions.cannotCloseTaskIfIssueOpen && task.openIssues > 0) {
task.master.setErrorOnTransaction(GanttMaster.messages["CANNOT_CLOSE_TASK_IF_OPEN_ISSUE"] + " \"" + task.name + "\"");
return false;
}
if ((manuallyChanged || oldStatus != "STATUS_FAILED")) { //cannot set failed task as closed for cascade - only if changed manually
//can be closed only if superiors are already done
var sups = task.getSuperiors();
for (var i = 0; i < sups.length; i++) {
if (sups[i].from.status != "STATUS_DONE" && cone.indexOf(sups[i].from)<0) { // è un errore se un predecessore è non chiuso ed è fuori dal cono
if (manuallyChanged || propagateFromParent) //genere un errore bloccante se è cambiato a mano o se il cambiamento arriva dal parent ed ho una dipendenza fuori dal cono (altrimenti avrei un attivo figlio di un chiuso
task.master.setErrorOnTransaction(GanttMaster.messages["GANTT_ERROR_DEPENDS_ON_OPEN_TASK"] + "\n\"" + sups[i].from.name + "\" -> \"" + task.name + "\"");
todoOk = false;
break;
}
}
if (todoOk) {
// set progress to 100% if needed by settings
if (task.master.set100OnClose && !task.progressByWorklog ){
task.progress=100;
}
//set children as done
propagateStatusToChildren(task,newStatus,false);
//set inferiors as active
propagateStatusToInferiors( task.getInferiors(), "STATUS_ACTIVE");
}
} else { // una propagazione tenta di chiudere un task fallito
todoOk = false;
}
// STATUS_UNDEFINED -> STATUS_ACTIVE all children become active, if they have no dependencies.
// STATUS_SUSPENDED -> STATUS_ACTIVE sets to active all children and their descendants that have no inhibiting dependencies.
// STATUS_DONE -> STATUS_ACTIVE all those that have dependencies must be set to suspended.
// STATUS_FAILED -> STATUS_ACTIVE nothing happens: child statuses must be reset by hand.
} else if (newStatus == "STATUS_ACTIVE") {
if ((manuallyChanged || oldStatus != "STATUS_FAILED")) { //cannot set failed task as closed for cascade - only if changed manually
//can be active only if superiors are already done, not only on this task, but also on ancestors superiors
var sups = task.getSuperiors();
for (var i = 0; i < sups.length; i++) {
if (sups[i].from.status != "STATUS_DONE") {
if (manuallyChanged || propagateFromChildren)
task.master.setErrorOnTransaction(GanttMaster.messages["GANTT_ERROR_DEPENDS_ON_OPEN_TASK"] + "\n\"" + sups[i].from.name + "\" -> \"" + task.name + "\"");
todoOk = false;
break;
}
}
// check if parent is already active
if (todoOk) {
var par = task.getParent();
if (par && par.status != "STATUS_ACTIVE") {
// todoOk = propagateStatus(par, "STATUS_ACTIVE", false, false, true); //todo abbiamo deciso di non far propagare lo status verso l'alto
todoOk = false;
}
}
if (todoOk) {
if (oldStatus == "STATUS_UNDEFINED" || oldStatus == "STATUS_SUSPENDED") {
//set children as active
propagateStatusToChildren(task,newStatus,true);
}
//set inferiors as suspended
propagateStatusToInferiors( task.getInferiors(), "STATUS_SUSPENDED");
}
} else {
todoOk = false;
}
// xxxx -> STATUS_SUSPENDED all active children and their active descendants become suspended. when not failed or forced
} else if (newStatus == "STATUS_SUSPENDED" ) {
if (manuallyChanged || oldStatus != "STATUS_FAILED") { //cannot set failed task as closed for cascade - only if changed manually
//check if parent if not active
var par = task.getParent();
if (par && (par.status != "STATUS_ACTIVE" && par.status != "STATUS_SUSPENDED")) {
todoOk = false;
}
if (todoOk) {
//set children as STATUS_SUSPENDED
propagateStatusToChildren(task, newStatus, true);
//set inferiors as STATUS_SUSPENDED
propagateStatusToInferiors( task.getInferiors(), newStatus);
}
} else {
todoOk = false;
}
// xxxx -> STATUS_FAILED children and dependent failed
// xxxx -> STATUS_UNDEFINED children and dependant become undefined.
} else if (newStatus == "STATUS_FAILED" || newStatus == "STATUS_UNDEFINED") {
//set children as failed or undefined
propagateStatusToChildren(task,newStatus,false);
//set inferiors as failed
propagateStatusToInferiors( task.getInferiors(), newStatus);
}
if (!todoOk) {
task.status = oldStatus;
//console.debug("status rolled back: "+task.name + " to " + oldStatus);
}
return todoOk;
}
/**
* A helper method to traverse an array of 'inferior' tasks
* and signal a status change.
*/
function propagateStatusToInferiors( infs, status) {
for (var i = 0; i < infs.length; i++) {
propagateStatus(infs[i].to, status, false, false, false);
}
}
/**
* A helper method to loop children and propagate new status
*/
function propagateStatusToChildren(task, newStatus, skipClosedTasks) {
var chds = task.getChildren();
for (var i = 0; i < chds.length; i++)
if (!(skipClosedTasks && chds[i].status == "STATUS_DONE") )
propagateStatus(chds[i], newStatus, false, true, false);
}
var manuallyChanged=true;
var oldStatus = this.status;
//first call
if (propagateStatus(this, newStatus, manuallyChanged, false, false)) {
return true;
} else {
this.status = oldStatus;
return false;
}
};
Task.prototype.synchronizeStatus = function () {
//console.debug("synchronizeStatus",this.name);
var oldS = this.status;
this.status = this.getParent()?this.getParent().status:"STATUS_UNDEFINED"; // di default si invalida lo stato mettendo quello del padre, in modo che inde/outd siano consistenti
return this.changeStatus(oldS,true);
};
Task.prototype.isLocallyBlockedByDependencies = function () {
var sups = this.getSuperiors();
var blocked = false;
for (var i = 0; i < sups.length; i++) {
if (sups[i].from.status != "STATUS_DONE") {
blocked = true;
break;
}
}
return blocked;
};
//<%---------- TASK STRUCTURE ---------------------- --%>
Task.prototype.getRow = function () {
ret = -1;
if (this.master)
ret = this.master.tasks.indexOf(this);
return ret;
};
Task.prototype.getParents = function () {
var ret;
if (this.master) {
var topLevel = this.level;
var pos = this.getRow();
ret = [];
for (var i = pos; i >= 0; i--) {
var par = this.master.tasks[i];
if (topLevel > par.level) {
topLevel = par.level;
ret.push(par);
}
}
}
return ret;
};
Task.prototype.getParent = function () {
var ret;
if (this.master) {
for (var i = this.getRow(); i >= 0; i--) {
var par = this.master.tasks[i];
if (this.level > par.level) {
ret = par;
break;
}
}
}
return ret;
};
Task.prototype.isParent = function () {
var ret = false;
if (this.master) {
var pos = this.getRow();
if (pos < this.master.tasks.length - 1)
ret = this.master.tasks[pos + 1].level > this.level;
}
return ret;
};
Task.prototype.getChildren = function () {
var ret = [];
if (this.master) {
var pos = this.getRow();
for (var i = pos + 1; i < this.master.tasks.length; i++) {
var ch = this.master.tasks[i];
if (ch.level == this.level + 1)
ret.push(ch);
else if (ch.level <= this.level) // exit loop if parent or brother
break;
}
}
return ret;
};
Task.prototype.getDescendant = function () {
var ret = [];
if (this.master) {
var pos = this.getRow();
for (var i = pos + 1; i < this.master.tasks.length; i++) {
var ch = this.master.tasks[i];
if (ch.level > this.level)
ret.push(ch);
else
break;
}
}
return ret;
};
Task.prototype.getSuperiors = function () {
var ret = [];
var task = this;
if (this.master) {
ret = this.master.links.filter(function (link) {
return link.to == task;
});
}
return ret;
};
Task.prototype.getSuperiorTasks = function () {
var ret = [];
var sups = this.getSuperiors();
for (var i = 0; i < sups.length; i++)
ret.push(sups[i].from);
return ret;
};
Task.prototype.getInferiors = function () {
var ret = [];
var task = this;
if (this.master) {
ret = this.master.links.filter(function (link) {
return link.from == task;
});
}
return ret;
};
Task.prototype.getInferiorTasks = function () {
var ret = [];
var infs = this.getInferiors();
for (var i = 0; i < infs.length; i++)
ret.push(infs[i].to);
return ret;
};
Task.prototype.deleteTask = function () {
//console.debug("deleteTask",this.name,this.master.deletedTaskIds)
//if is the current one remove it
if (this.master.currentTask && this.master.currentTask.id==this.id)
delete this.master.currentTask;
//delete both dom elements if exists
if (this.rowElement)
this.rowElement.remove();
if (this.ganttElement)
this.ganttElement.remove();
//remove children
var chd = this.getChildren();
for (var i = 0; i < chd.length; i++) {
//add removed child in list
chd[i].deleteTask();
}
if (!this.isNew())
this.master.deletedTaskIds.push(this.id);
//remove from in-memory collection
this.master.tasks.splice(this.getRow(), 1);
//remove from links
var task = this;
this.master.links = this.master.links.filter(function (link) {
return link.from != task && link.to != task;
});
};
Task.prototype.isNew = function () {
return (this.id + "").indexOf("tmp_") == 0;
};
Task.prototype.isDependent = function (t) {
//console.debug("isDependent",this.name, t.name)
var task = this;
var dep = this.master.links.filter(function (link) {
return link.from == task;
});
// is t a direct dependency?
for (var i = 0; i < dep.length; i++) {
if (dep[i].to == t)
return true;
}
// is t an indirect dependency
for (var i = 0; i < dep.length; i++) {
if (dep[i].to.isDependent(t)) {
return true;
}
}
return false;
};
Task.prototype.setLatest = function (maxCost) {
this.latestStart = maxCost - this.criticalCost;
this.latestFinish = this.latestStart + this.duration;
};
//<%------------------------------------------ INDENT/OUTDENT --------------------------------%>
Task.prototype.indent = function () {
//console.debug("indent", this);
//a row above must exist
var row = this.getRow();
//no row no party
if (row <= 0)
return false;
var ret = false;
var taskAbove = this.master.tasks[row - 1];
var newLev = this.level + 1;
if (newLev <= taskAbove.level + 1) {
ret = true;
//trick to get parents after indent
this.level++;
var futureParents = this.getParents();
this.level--;
var oldLevel = this.level;
for (var i = row; i < this.master.tasks.length; i++) {
var desc = this.master.tasks[i];
if (desc.level > oldLevel || desc == this) {
desc.level++;
//remove links from this and descendant to my parents
this.master.links = this.master.links.filter(function (link) {
var linkToParent = false;
if (link.to == desc)
linkToParent = futureParents.indexOf(link.from) >= 0;
else if (link.from == desc)
linkToParent = futureParents.indexOf(link.to) >= 0;
return !linkToParent;
});
//remove links from this and descendants to predecessors of parents in order to avoid loop
var predecessorsOfFutureParents=[];
for (var j=0;j<futureParents.length;j++)
predecessorsOfFutureParents=predecessorsOfFutureParents.concat(futureParents[j].getSuperiorTasks());
this.master.links = this.master.links.filter(function (link) {
var linkToParent = false;
if (link.from == desc)
linkToParent = predecessorsOfFutureParents.indexOf(link.to) >= 0;
return !linkToParent;
});
} else
break;
}
var parent = this.getParent();
// set start date to parent' start if no deps
if (parent && !this.depends) {
var new_end = computeEndByDuration(parent.start, this.duration);
this.master.changeTaskDates(this, parent.start, new_end);
}
//recompute depends string
this.master.updateDependsStrings();
//enlarge parent using a fake set period
updateTree(this);
//force status check starting from parent
this.getParent().synchronizeStatus();
}
return ret;
};
Task.prototype.outdent = function () {
//console.debug("outdent", this);
//a level must be >1 -> cannot escape from root
if (this.level <= 1)
return false;
var ret = false;
var oldLevel = this.level;
ret = true;
var row = this.getRow();
for (var i = row; i < this.master.tasks.length; i++) {
var desc = this.master.tasks[i];
if (desc.level > oldLevel || desc == this) {
desc.level--;
} else
break;
}
var task = this;
var chds = this.getChildren();
//remove links from me to my new children
this.master.links = this.master.links.filter(function (link) {
var linkExist = (link.to == task && chds.indexOf(link.from) >= 0 || link.from == task && chds.indexOf(link.to) >= 0);
return !linkExist;
});
//enlarge me if inherited children are larger
for (var i = 0; i < chds.length; i++) {
//remove links from me to my new children
chds[i].setPeriod(chds[i].start + 1, chds[i].end + 1);
}
//recompute depends string
this.master.updateDependsStrings();
//enlarge parent using a fake set period
this.setPeriod(this.start + 1, this.end + 1);
//force status check
this.synchronizeStatus();
return ret;
};
//<%------------------------------------------ MOVE UP / MOVE DOWN --------------------------------%>
Task.prototype.moveUp = function () {
//console.debug("moveUp", this);
var ret = false;
//a row above must exist
var row = this.getRow();
//no row no party
if (row <= 0)
return false;
//find new row
var newRow;
for (newRow = row - 1; newRow >= 0; newRow--) {
if (this.master.tasks[newRow].level <= this.level)
break;
}
//is a parent or a brother
if (this.master.tasks[newRow].level == this.level) {
ret = true;
//compute descendant
var descNumber = 0;
for (var i = row + 1; i < this.master.tasks.length; i++) {
var desc = this.master.tasks[i];
if (desc.level > this.level) {
descNumber++;
} else {
break;
}
}
//move in memory
var blockToMove = this.master.tasks.splice(row, descNumber + 1);
var top = this.master.tasks.splice(0, newRow);
this.master.tasks = [].concat(top, blockToMove, this.master.tasks);
//move on dom
var rows = this.master.editor.element.find("tr[taskid]");
var domBlockToMove = rows.slice(row, row + descNumber + 1);
rows.eq(newRow).before(domBlockToMove);
//recompute depends string
this.master.updateDependsStrings();
} else {
this.master.setErrorOnTransaction(GanttMaster.messages["TASK_MOVE_INCONSISTENT_LEVEL"], this);
ret = false;
}
return ret;
};