source: Dev/branches/rest-dojo-ui/client/dojox/gantt/GanttProjectItem.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 33.2 KB
Line 
1dojo.provide("dojox.gantt.GanttProjectItem");
2
3dojo.require("dojox.gantt.GanttTaskItem");
4dojo.require("dojo.date.locale");
5dojo.require("dijit.focus");            // dijit.focus()
6
7dojo.declare("dojox.gantt.GanttProjectControl", null, {
8        constructor: function(ganttChart, projectItem){
9                this.project = projectItem;
10                this.ganttChart = ganttChart;
11                this.descrProject = null;
12                this.projectItem = null;
13                this.projectNameItem = null;
14                this.posY = 0;
15                this.posX = 0;
16                this.nextProject = null;
17                this.previousProject = null;
18                this.arrTasks = [];
19                this.percentage = 0;
20                this.duration = 0;
21        },
22        checkWidthProjectNameItem: function(){
23                if(this.projectNameItem.offsetWidth + this.projectNameItem.offsetLeft > this.ganttChart.maxWidthTaskNames){
24                        var width = this.projectNameItem.offsetWidth + this.projectNameItem.offsetLeft - this.ganttChart.maxWidthTaskNames;
25                        var countChar = Math.round(width / (this.projectNameItem.offsetWidth / this.projectNameItem.firstChild.length));
26                        var pName = this.project.name.substring(0, this.projectNameItem.firstChild.length - countChar - 3);
27                        pName += "...";
28                        this.projectNameItem.innerHTML = pName;
29                }
30        },
31        refreshProjectItem: function(projectItem){
32                this.percentage = this.getPercentCompleted();
33                dojo.style(projectItem, {
34                        "left": this.posX + "px",
35                        "width": this.duration * this.ganttChart.pixelsPerWorkHour + "px"
36                });
37                var tblProjectItem = projectItem.firstChild;
38                var width = this.duration * this.ganttChart.pixelsPerWorkHour;
39                tblProjectItem.width = ((width == 0) ? 1 : width) + "px";
40                tblProjectItem.style.width = ((width == 0) ? 1 : width) + "px";
41                var rowprojectItem = tblProjectItem.rows[0];
42                if(this.percentage != -1){
43                        if(this.percentage != 0){
44                                var cellprojectItem = rowprojectItem.firstChild;
45                                cellprojectItem.width = this.percentage + "%";
46                                var imageProgress = cellprojectItem.firstChild;
47                                dojo.style(imageProgress, {
48                                        width: (!this.duration ? 1 : (this.percentage * this.duration * this.ganttChart.pixelsPerWorkHour / 100)) + "px",
49                                        height: this.ganttChart.heightTaskItem + "px"
50                                })
51                        }
52                        if(this.percentage != 100){
53                                var cellprojectItem = rowprojectItem.lastChild;
54                                cellprojectItem.width = (100 - this.percentage) + "%";
55                                var imageProgress = cellprojectItem.firstChild;
56                                dojo.style(imageProgress, {
57                                        width: (!this.duration ? 1 : ((100 - this.percentage) * this.duration * this.ganttChart.pixelsPerWorkHour / 100)) + "px",
58                                        height: this.ganttChart.heightTaskItem + "px"
59                                })
60                        }
61                }else{
62                        var cellprojectItem = rowprojectItem.firstChild;
63                        cellprojectItem.width = "1px";
64                        var imageProgress = cellprojectItem.firstChild;
65                        dojo.style(imageProgress, {
66                                width: "1px",
67                                height: this.ganttChart.heightTaskItem + "px"
68                        })
69                }
70                var divTaskInfo = projectItem.lastChild;
71                var tblTaskInfo = divTaskInfo.firstChild;
72                dojo.style(tblTaskInfo, {
73                        height: this.ganttChart.heightTaskItem + "px",
74                        width: (!this.duration ? 1 : (this.duration * this.ganttChart.pixelsPerWorkHour)) + "px"
75                });
76                var rowTaskInfo = tblTaskInfo.rows[0];
77                var cellTaskInfo = rowTaskInfo.firstChild;
78                cellTaskInfo.height = this.ganttChart.heightTaskItem + "px";
79                if(this.project.parentTasks.length == 0){
80                        projectItem.style.display = "none";
81                }
82                return projectItem;
83        },
84        refreshDescrProject: function(divDesc){
85                var posX = (this.posX + this.duration * this.ganttChart.pixelsPerWorkHour + 10);
86                dojo.style(divDesc, {
87                        "left": posX + "px"
88                });
89                if(this.project.parentTasks.length == 0){
90                        this.descrProject.style.visibility = 'hidden';
91                }
92                return divDesc;
93        },
94        postLoadData: function(){
95                //TODO e.g. project relative info...
96        },
97        refresh: function(){
98                var containerTasks = this.ganttChart.contentData.firstChild;
99                this.posX = (this.project.startDate - this.ganttChart.startDate) / (60 * 60 * 1000) * this.ganttChart.pixelsPerHour;
100                this.refreshProjectItem(this.projectItem[0]);
101                this.refreshDescrProject(this.projectItem[0].nextSibling);
102                return this;
103        },
104        create: function(){
105                var containerTasks = this.ganttChart.contentData.firstChild;
106                this.posX = (this.project.startDate - this.ganttChart.startDate) / (60 * 60 * 1000) * this.ganttChart.pixelsPerHour;
107                if(this.previousProject){
108                        if(this.previousProject.arrTasks.length > 0){
109                                var lastChildTask = this.ganttChart.getLastChildTask(this.previousProject.arrTasks[this.previousProject.arrTasks.length - 1]);
110                                this.posY = parseInt(lastChildTask.cTaskItem[0].style.top) + this.ganttChart.heightTaskItem + this.ganttChart.heightTaskItemExtra;
111                        }else{
112                                this.posY = parseInt(this.previousProject.projectItem[0].style.top) + this.ganttChart.heightTaskItem + this.ganttChart.heightTaskItemExtra;
113                        }
114                }else{
115                        this.posY = 6;
116                }
117                var containerNames = this.ganttChart.panelNames.firstChild;
118                this.projectNameItem = this.createProjectNameItem();
119                containerNames.appendChild(this.projectNameItem);
120                this.checkWidthProjectNameItem();
121                this.projectItem = [this.createProjectItem(), []];
122                containerTasks.appendChild(this.projectItem[0]);
123                containerTasks.appendChild(this.createDescrProject());
124                this.adjustPanelTime();
125        },
126        getTaskById: function(id){
127                for(var i = 0; i < this.arrTasks.length; i++){
128                        var aTask = this.arrTasks[i];
129                        var task = this.searchTaskInTree(aTask, id);
130                        if(task){
131                                return task;
132                        }
133                }
134                return null;
135        },
136        searchTaskInTree: function(task, id){
137                if(task.taskItem.id == id){
138                        return task;
139                }else{
140                        for(var i = 0; i < task.childTask.length; i++){
141                                var cTask = task.childTask[i];
142                                if(cTask.taskItem.id == id){
143                                        return cTask;
144                                }else{
145                                        if(cTask.childTask.length > 0){
146                                                var cTask = this.searchTaskInTree(cTask, id);
147                                                if(cTask){
148                                                        return cTask;
149                                                }
150                                        }
151                                }
152                        }
153                }
154                return null;
155        },
156        shiftProjectItem: function(){
157                var posItemL = null;
158                var posItemR = null;
159                var posProjectItemL = parseInt(this.projectItem[0].style.left);
160                var posProjectItemR = parseInt(this.projectItem[0].firstChild.style.width) + parseInt(this.projectItem[0].style.left);
161                var widthProjectItem = parseInt(this.projectItem[0].firstChild.style.width);
162                for(var i = 0; i < this.arrTasks.length; i++){
163                        var aTask = this.arrTasks[i];
164                        var tmpPosItemL = parseInt(aTask.cTaskItem[0].style.left);
165                        var tmpPosItemR = parseInt(aTask.cTaskItem[0].style.left) + parseInt(aTask.cTaskItem[0].firstChild.firstChild.width);
166                        if(!posItemL){
167                                posItemL = tmpPosItemL;
168                        }
169                        if(!posItemR){
170                                posItemR = tmpPosItemR;
171                        }
172                        if(posItemL > tmpPosItemL){
173                                posItemL = tmpPosItemL;
174                        }
175                        if(posItemR < tmpPosItemR){
176                                posItemR = tmpPosItemR;
177                        }
178                }
179                if(posItemL != posProjectItemL){
180                        this.project.startDate = new Date(this.ganttChart.startDate);
181                        this.project.startDate.setHours(this.project.startDate.getHours() + (posItemL / this.ganttChart.pixelsPerHour));
182                }
183                this.projectItem[0].style.left = posItemL + "px";
184                this.resizeProjectItem(posItemR - posItemL);
185                this.duration = Math.round(parseInt(this.projectItem[0].firstChild.width) / (this.ganttChart.pixelsPerWorkHour));
186                this.shiftDescrProject();
187                this.adjustPanelTime();
188        },
189        adjustPanelTime: function(){
190                var projectItem = this.projectItem[0];
191                var width = parseInt(projectItem.style.left) + parseInt(projectItem.firstChild.style.width) + this.ganttChart.panelTimeExpandDelta;
192                width += this.descrProject.offsetWidth;
193                this.ganttChart.adjustPanelTime(width);
194        },
195        resizeProjectItem: function(width){
196                var percentage = this.percentage,
197                        pItem = this.projectItem[0];
198                if(percentage > 0 && percentage < 100){
199                        pItem.firstChild.style.width = width + "px";
200                        pItem.firstChild.width = width + "px";
201                        pItem.style.width = width + "px";
202                        var firstRow = pItem.firstChild.rows[0];
203                        firstRow.cells[0].firstChild.style.width = Math.round(width * percentage / 100) + "px";
204                        firstRow.cells[0].firstChild.style.height = this.ganttChart.heightTaskItem + "px";
205                        firstRow.cells[1].firstChild.style.width = Math.round(width * (100 - percentage) / 100) + "px";
206                        firstRow.cells[1].firstChild.style.height = this.ganttChart.heightTaskItem + "px";
207                        pItem.lastChild.firstChild.width = width + "px";
208                }else if(percentage == 0 || percentage == 100){
209                        pItem.firstChild.style.width = width + "px";
210                        pItem.firstChild.width = width + "px";
211                        pItem.style.width = width + "px";
212                        var firstRow = pItem.firstChild.rows[0];
213                        firstRow.cells[0].firstChild.style.width = width + "px";
214                        firstRow.cells[0].firstChild.style.height = this.ganttChart.heightTaskItem + "px";
215                        pItem.lastChild.firstChild.width = width + "px";
216                }
217        },
218        shiftDescrProject: function(){
219                var posX = (parseInt(this.projectItem[0].style.left) + this.duration * this.ganttChart.pixelsPerWorkHour + 10);
220                this.descrProject.style.left = posX + "px";
221                this.descrProject.innerHTML = this.getDescStr();
222        },
223        showDescrProject: function(){
224                var posX = (parseInt(this.projectItem[0].style.left) + this.duration * this.ganttChart.pixelsPerWorkHour + 10);
225                this.descrProject.style.left = posX + "px";
226                this.descrProject.style.visibility = 'visible';
227                this.descrProject.innerHTML = this.getDescStr();
228        },
229        hideDescrProject: function(){
230                this.descrProject.style.visibility = 'hidden';
231        },
232        getDescStr: function(){
233                return this.duration/this.ganttChart.hsPerDay + " days,  " + this.duration + " hours";
234        },
235        createDescrProject: function(){
236                var posX = (this.posX + this.duration * this.ganttChart.pixelsPerWorkHour + 10);
237                var divDesc = dojo.create("div", {
238                        innerHTML: this.getDescStr(),
239                        className: "ganttDescProject"
240                });
241                dojo.style(divDesc, {
242                        left: posX + "px",
243                        top: this.posY + "px"
244                });
245                this.descrProject = divDesc;
246                if(this.project.parentTasks.length == 0){
247                        this.descrProject.style.visibility = 'hidden';
248                }
249                return divDesc;
250        },
251        createProjectItem: function(){
252                this.percentage = this.getPercentCompleted();
253                this.duration = this.getDuration();
254                var projectItem = dojo.create("div", {
255                        id: this.project.id,
256                        className: "ganttProjectItem"
257                });
258                dojo.style(projectItem, {
259                        left: this.posX + "px",
260                        top: this.posY + "px",
261                        width: this.duration * this.ganttChart.pixelsPerWorkHour + "px"
262                });
263                var tblProjectItem = dojo.create("table", {
264                        cellPadding: "0",
265                        cellSpacing: "0",
266                        className: "ganttTblProjectItem"
267                }, projectItem);
268                var width = this.duration * this.ganttChart.pixelsPerWorkHour;
269                tblProjectItem.width = ((width == 0) ? 1 : width) + "px";
270                tblProjectItem.style.width = ((width == 0) ? 1 : width) + "px";
271               
272                var rowprojectItem = tblProjectItem.insertRow(tblProjectItem.rows.length);
273                if(this.percentage != -1){
274                        if(this.percentage != 0){
275                                var cellprojectItem = dojo.create("td", {
276                                        width: this.percentage + "%"
277                                }, rowprojectItem);
278                                cellprojectItem.style.lineHeight = "1px";
279                                var imageProgress = dojo.create("div", {
280                                        className: "ganttImageProgressFilled"
281                                }, cellprojectItem);
282                                dojo.style(imageProgress, {
283                                        width: (this.percentage * this.duration * this.ganttChart.pixelsPerWorkHour) / 100 + "px",
284                                        height: this.ganttChart.heightTaskItem + "px"
285                                });
286                        }
287                        if(this.percentage != 100){
288                                var cellprojectItem = dojo.create("td", {
289                                        width: (100 - this.percentage) + "%"
290                                }, rowprojectItem);
291                                cellprojectItem.style.lineHeight = "1px";
292                                var imageProgress = dojo.create("div", {
293                                        className: "ganttImageProgressBg"
294                                }, cellprojectItem);
295                                dojo.style(imageProgress, {
296                                        width: ((100 - this.percentage) * this.duration * this.ganttChart.pixelsPerWorkHour) / 100 + "px",
297                                        height: this.ganttChart.heightTaskItem + "px"
298                                });
299                        }
300                }else{
301                        var cellprojectItem = dojo.create("td", {
302                                width: "1px"
303                        }, rowprojectItem);
304                        cellprojectItem.style.lineHeight = "1px";
305                        var imageProgress = dojo.create("div", {
306                                className: "ganttImageProgressBg"
307                        }, cellprojectItem);
308                        dojo.style(imageProgress, {
309                                width: "1px",
310                                height: this.ganttChart.heightTaskItem + "px"
311                        });
312                }
313                var divTaskInfo = dojo.create("div", {className: "ganttDivTaskInfo"});
314                var tblTaskInfo = dojo.create("table", {
315                        cellPadding: "0",
316                        cellSpacing: "0",
317                        height: this.ganttChart.heightTaskItem + "px",
318                        width: ((this.duration * this.ganttChart.pixelsPerWorkHour == 0) ? 1 : this.duration * this.ganttChart.pixelsPerWorkHour) + "px"
319                }, divTaskInfo);
320                var rowTaskInfo = tblTaskInfo.insertRow(0);
321                var cellTaskInfo = dojo.create("td", {
322                        align: "center",
323                        vAlign: "top",
324                        height: this.ganttChart.heightTaskItem + "px",
325                        className: "ganttMoveInfo"
326                }, rowTaskInfo);
327                projectItem.appendChild(divTaskInfo);
328                if(this.project.parentTasks.length == 0){
329                        projectItem.style.display = "none";
330                }
331                return projectItem;
332        },
333        createProjectNameItem: function(){
334                var divName = dojo.create("div", {
335                        className: "ganttProjectNameItem",
336                        innerHTML: this.project.name,
337                        title: this.project.name
338                });
339                dojo.style(divName, {
340                        left: "5px",
341                        top: this.posY + "px"
342                });
343                dojo.attr(divName, "tabIndex", 0);
344                if(this.ganttChart.isShowConMenu){
345                        this.ganttChart._events.push(
346                                dojo.connect(divName, "onmouseover", this, function(event){
347                                        dojo.addClass(divName, "ganttProjectNameItemHover");
348                                        clearTimeout(this.ganttChart.menuTimer);
349                                        this.ganttChart.tabMenu.clear();
350                                        this.ganttChart.tabMenu.show(event.target, this);
351                                })
352                        );
353                        this.ganttChart._events.push(
354                                dojo.connect(divName, "onkeydown", this, function(event){
355                                        if(event.keyCode == dojo.keys.ENTER){
356                                                this.ganttChart.tabMenu.clear();
357                                                this.ganttChart.tabMenu.show(event.target, this);
358                                        }
359                                        if(this.ganttChart.tabMenu.isShow && (event.keyCode == dojo.keys.LEFT_ARROW || event.keyCode == dojo.keys.RIGHT_ARROW)){
360                                                dijit.focus(this.ganttChart.tabMenu.menuPanel.firstChild.rows[0].cells[0]);
361                                        }
362                                        if(this.ganttChart.tabMenu.isShow && event.keyCode == dojo.keys.ESCAPE){
363                                                this.ganttChart.tabMenu.hide();
364                                        }
365                                })
366                        );
367                        this.ganttChart._events.push(
368                                dojo.connect(divName, "onmouseout", this, function(){
369                                        dojo.removeClass(divName, "ganttProjectNameItemHover");
370                                        clearTimeout(this.ganttChart.menuTimer);
371                                        this.ganttChart.menuTimer = setTimeout(dojo.hitch(this, function(){
372                                                this.ganttChart.tabMenu.hide();
373                                        }), 200);
374                                })
375                        );
376                        this.ganttChart._events.push(
377                                dojo.connect(this.ganttChart.tabMenu.menuPanel, "onmouseover", this, function(){
378                                        clearTimeout(this.ganttChart.menuTimer);
379                                })
380                        );
381                        this.ganttChart._events.push(
382                                dojo.connect(this.ganttChart.tabMenu.menuPanel, "onkeydown", this, function(event){
383                                        if(this.ganttChart.tabMenu.isShow && event.keyCode == dojo.keys.ESCAPE){
384                                                this.ganttChart.tabMenu.hide();
385                                        }
386                                })
387                        );
388                        this.ganttChart._events.push(
389                                dojo.connect(this.ganttChart.tabMenu.menuPanel, "onmouseout", this, function(){
390                                        clearTimeout(this.ganttChart.menuTimer);
391                                        this.ganttChart.menuTimer = setTimeout(dojo.hitch(this, function(){
392                                                this.ganttChart.tabMenu.hide();
393                                        }), 200);
394                                })
395                        );
396                }
397                return divName;
398        },
399        getPercentCompleted: function(){
400                var sum = 0, percentage = 0;
401                dojo.forEach(this.project.parentTasks, function(ppTask){
402                        sum += parseInt(ppTask.percentage);
403                }, this);
404                if(this.project.parentTasks.length != 0){
405                        return percentage = Math.round(sum / this.project.parentTasks.length);
406                }else{
407                        return percentage = -1;
408                }
409        },
410        getDuration: function(){
411                var duration = 0, tmpDuration = 0;
412                if(this.project.parentTasks.length > 0){
413                        dojo.forEach(this.project.parentTasks, function(ppTask){
414                                tmpDuration = ppTask.duration * 24 / this.ganttChart.hsPerDay + (ppTask.startTime - this.ganttChart.startDate) / (60 * 60 * 1000);
415                                if(tmpDuration > duration){
416                                        duration = tmpDuration;
417                                }
418                        }, this);
419                        return ((duration - this.posX) / 24) * this.ganttChart.hsPerDay;
420                }else{
421                        return 0;
422                }
423        },
424        deleteTask: function(id){
425                var task = this.getTaskById(id);
426                if(task){
427                        this.deleteChildTask(task);
428                        this.ganttChart.checkPosition();
429                }
430        },
431        setName: function(name){
432                if(name){
433                        this.project.name = name;
434                        this.projectNameItem.innerHTML = name;
435                        this.projectNameItem.title = name;
436                        this.checkWidthProjectNameItem();
437                       
438                        this.descrProject.innerHTML = this.getDescStr();
439                        this.adjustPanelTime();
440                }
441        },
442        setPercentCompleted: function(percentage){
443                percentage = parseInt(percentage);
444                if(isNaN(percentage) || percentage > 100 || percentage < 0){
445                        return false;
446                }
447                var prow = this.projectItem[0].firstChild.rows[0],
448                        rc0 = prow.cells[0], rc1 = prow.cells[1];
449                if((percentage > 0) && (percentage < 100) && (this.percentage > 0) && (this.percentage < 100)){
450                        rc0.width = parseInt(percentage) + "%";
451                        rc0.firstChild.style.width = (percentage * this.duration * this.ganttChart.pixelsPerWorkHour) / 100 + "px";
452                        rc1.width = (100 - parseInt(percentage)) + "%";
453                        rc1.firstChild.style.width = ((100 - percentage) * this.duration * this.ganttChart.pixelsPerWorkHour) / 100 + "px";
454                }else if(((percentage == 0) || (percentage == 100)) && (this.percentage > 0) && (this.percentage < 100)){
455                        if(percentage == 0){
456                                rc0.parentNode.removeChild(rc0);
457                                rc1.width = 100 + "%";
458                                rc1.firstChild.style.width = this.duration * this.ganttChart.pixelsPerWorkHour + "px";
459                        }else if(percentage == 100){
460                                rc1.parentNode.removeChild(rc1);
461                                rc0.width = 100 + "%";
462                                rc0.firstChild.style.width = this.duration * this.ganttChart.pixelsPerWorkHour + "px";
463                        }
464                }else if(((percentage == 0) || (percentage == 100)) && ((this.percentage == 0) || (this.percentage == 100))){
465                        if((percentage == 0) && (this.percentage == 100)){
466                                dojo.removeClass(rc0.firstChild, "ganttImageProgressFilled");
467                                dojo.addClass(rc0.firstChild, "ganttImageProgressBg");
468                        }else if((percentage == 100) && (this.percentage == 0)){
469                                dojo.removeClass(rc0.firstChild, "ganttImageProgressBg");
470                                dojo.addClass(rc0.firstChild, "ganttImageProgressFilled");
471                        }
472                }else if(((percentage > 0) || (percentage < 100)) && ((this.percentage == 0) || (this.percentage == 100))){
473                        rc0.parentNode.removeChild(rc0);
474                        var cellprojectItem = dojo.create("td", {
475                                width: percentage + "%"
476                        }, prow);
477                        cellprojectItem.style.lineHeight = "1px";
478                        var imageProgress = dojo.create("div", {
479                                className: "ganttImageProgressFilled"
480                        }, cellprojectItem);
481                        dojo.style(imageProgress, {
482                                width: (percentage * this.duration * this.ganttChart.pixelsPerWorkHour) / 100 + "px",
483                                height: this.ganttChart.heightTaskItem + "px"
484                        });
485                        cellprojectItem = dojo.create("td", {
486                                width: (100 - percentage) + "%"
487                        }, prow);
488                        cellprojectItem.style.lineHeight = "1px";
489                        imageProgress = dojo.create("div", {
490                                className: "ganttImageProgressBg"
491                        }, cellprojectItem);
492                        dojo.style(imageProgress, {
493                                width: ((100 - percentage) * this.duration * this.ganttChart.pixelsPerWorkHour) / 100 + "px",
494                                height: this.ganttChart.heightTaskItem + "px"
495                        });
496                }else if(this.percentage == -1){
497                        if(percentage == 100){
498                                dojo.removeClass(rc0.firstChild, "ganttImageProgressBg");
499                                dojo.addClass(rc0.firstChild, "ganttImageProgressFilled");
500                        }else if(percentage < 100 && percentage > 0){
501                                rc0.parentNode.removeChild(rc0);
502                                var cellprojectItem = dojo.create("td", {
503                                        width: percentage + "%"
504                                }, prow);
505                                cellprojectItem.style.lineHeight = "1px";
506                                imageProgress = dojo.create("div", {
507                                        className: "ganttImageProgressFilled"
508                                }, cellprojectItem);
509                                dojo.style(imageProgress, {
510                                        width: (percentage * this.duration * this.ganttChart.pixelsPerWorkHour) / 100 + "px",
511                                        height: this.ganttChart.heightTaskItem + "px"
512                                });
513                                cellprojectItem = dojo.create("td", {
514                                        width: (100 - percentage) + "%"
515                                }, prow);
516                                cellprojectItem.style.lineHeight = "1px";
517                                imageProgress = dojo.create("div", {
518                                        className: "ganttImageProgressBg"
519                                }, cellprojectItem);
520                                dojo.style(imageProgress, {
521                                        width: ((100 - percentage) * this.duration * this.ganttChart.pixelsPerWorkHour) / 100 + "px",
522                                        height: this.ganttChart.heightTaskItem + "px"
523                                });
524                        }
525                }
526                this.percentage = percentage;
527                this.descrProject.innerHTML = this.getDescStr();
528                return true;
529        },
530        deleteChildTask: function(task){
531                if(task){
532                        var tItem0 = task.cTaskItem[0], tNameItem0 = task.cTaskNameItem[0],
533                                tItem1 = task.cTaskItem[1], tNameItem1 = task.cTaskNameItem[1],
534                                tItem2 = task.cTaskItem[2], tNameItem2 = task.cTaskNameItem[2];
535                        if(tItem0.style.display == "none"){
536                                this.ganttChart.openTree(task.parentTask);
537                        }
538                        //delete of connecting lines
539                        if(task.childPredTask.length > 0){
540                                for(var i = 0; i < task.childPredTask.length; i++){
541                                        var cpTask = task.childPredTask[i];
542                                        for(var t = 0; t < cpTask.cTaskItem[1].length; t++){
543                                                cpTask.cTaskItem[1][t].parentNode.removeChild(cpTask.cTaskItem[1][t]);
544                                        }
545                                        cpTask.cTaskItem[1] = [];
546                                        cpTask.predTask = null;
547                                }
548                        }
549                        //delete child task
550                        if(task.childTask.length > 0){
551                                while(task.childTask.length > 0){
552                                        this.deleteChildTask(task.childTask[0]);
553                                }
554                        }
555                        //shift tasks
556                        var rowHeight = this.ganttChart.heightTaskItem + this.ganttChart.heightTaskItemExtra;
557                        if(tItem0.style.display != "none"){
558                                task.shiftCurrentTasks(task, -rowHeight);
559                        }
560                        //delete object task
561                        this.project.deleteTask(task.taskItem.id);
562                        //delete div and connecting lines from contentData
563                        if(tItem0){
564                                tItem0.parentNode.removeChild(tItem0);
565                        }
566                        task.descrTask.parentNode.removeChild(task.descrTask);
567                        if(tItem1.length > 0){
568                                for(var j = 0; j < tItem1.length; j++){
569                                        tItem1[j].parentNode.removeChild(tItem1[j]);
570                                }
571                        }
572                        //delete div and connecting lines from panelName
573                        if(tNameItem0){
574                                tNameItem0.parentNode.removeChild(tNameItem0);
575                        }
576                        if(task.cTaskNameItem[1]){
577                                for(var j = 0; j < tNameItem1.length; j++){
578                                        tNameItem1[j].parentNode.removeChild(tNameItem1[j]);
579                                }
580                        }
581                        if(tNameItem2 && tNameItem2.parentNode){
582                                tNameItem2.parentNode.removeChild(tNameItem2);
583                        }
584                        if(task.taskIdentifier){
585                                task.taskIdentifier.parentNode.removeChild(task.taskIdentifier);
586                                task.taskIdentifier = null;
587                        }
588                        //delete object task
589                        if(task.parentTask){
590                                if(task.previousChildTask){
591                                        if(task.nextChildTask){
592                                                task.previousChildTask.nextChildTask = task.nextChildTask;
593                                        }else{
594                                                task.previousChildTask.nextChildTask = null;
595                                        }
596                                }
597                                var parentTask = task.parentTask;
598                                for(var i = 0; i < parentTask.childTask.length; i++){
599                                        if(parentTask.childTask[i].taskItem.id == task.taskItem.id){
600                                                parentTask.childTask[i] = null;
601                                                parentTask.childTask.splice(i, 1);
602                                                break;
603                                        }
604                                }
605                                if(parentTask.childTask.length == 0){
606                                        if(parentTask.cTaskNameItem[2]){
607                                                parentTask.cTaskNameItem[2].parentNode.removeChild(parentTask.cTaskNameItem[2]);
608                                                parentTask.cTaskNameItem[2] = null;
609                                        }
610                                }
611                        }else{
612                                if(task.previousParentTask){
613                                        if(task.nextParentTask){
614                                                task.previousParentTask.nextParentTask = task.nextParentTask;
615                                        }else{
616                                                task.previousParentTask.nextParentTask = null;
617                                        }
618                                }
619                                var project = task.project;
620                                for(var i = 0; i < project.arrTasks.length; i++){
621                                        if(project.arrTasks[i].taskItem.id == task.taskItem.id){
622                                                project.arrTasks.splice(i, 1);
623                                        }
624                                }
625                        }
626                        if(task.predTask){
627                                var predTask = task.predTask;
628                                for(var i = 0; i < predTask.childPredTask.length; i++){
629                                        if(predTask.childPredTask[i].taskItem.id == task.taskItem.id){
630                                                predTask.childPredTask[i] = null;
631                                                predTask.childPredTask.splice(i, 1);
632                                        }
633                                }
634                        }
635                        if(task.project.arrTasks.length != 0){
636                                task.project.shiftProjectItem();
637                        }else{
638                                task.project.projectItem[0].style.display = "none";
639                                this.hideDescrProject();
640                        }
641                        this.ganttChart.contentDataHeight -= this.ganttChart.heightTaskItemExtra + this.ganttChart.heightTaskItem;
642                }
643        },
644       
645        insertTask: function(id, name, startTime, duration, percentage, previousTaskId, taskOwner, parentTaskId){
646                var task = null;
647                var _task = null;
648                if(this.project.getTaskById(id)){
649                        return false;
650                }
651                if((!duration) || (duration < this.ganttChart.minWorkLength)){
652                        duration = this.ganttChart.minWorkLength;
653                }
654                if((!name) || (name == "")){
655                        name = id;
656                }
657                if((!percentage) || (percentage == "")){
658                        percentage = 0;
659                       
660                }else{
661                        percentage = parseInt(percentage);
662                        if(percentage < 0 || percentage > 100){
663                                return false;
664                        }
665                }
666                var sortRequired = false;
667                if((parentTaskId) && (parentTaskId != "")){
668                        var parentTask = this.project.getTaskById(parentTaskId);
669                        if(!parentTask){
670                                return false;
671                        }
672                        startTime = startTime || parentTask.startTime;
673                        if(startTime < parentTask.startTime){
674                                return false;
675                        }
676                        task = new dojox.gantt.GanttTaskItem({
677                                id: id,
678                                name: name,
679                                startTime: startTime,
680                                duration: duration,
681                                percentage: percentage,
682                                previousTaskId: previousTaskId,
683                                taskOwner: taskOwner
684                        });
685                        if(!this.ganttChart.checkPosParentTask(parentTask, task)){
686                                return false;
687                        }
688                        task.parentTask = parentTask;
689                        var _parentTask = this.getTaskById(parentTask.id);
690                        var isHide = false;
691                        if(_parentTask.cTaskItem[0].style.display == "none"){
692                                isHide = true;
693                        }else if(_parentTask.cTaskNameItem[2]){
694                                if(!_parentTask.isExpanded){
695                                        isHide = true;
696                                }
697                        }
698                        if(isHide){
699                                if(_parentTask.childTask.length == 0){
700                                        this.ganttChart.openTree(_parentTask.parentTask);
701                                }else{
702                                        this.ganttChart.openTree(_parentTask);
703                                }
704                        }
705                        if(previousTaskId != ""){
706                                var predTask = this.project.getTaskById(previousTaskId);
707                                if(!predTask){
708                                        return false;
709                                }
710                                if(predTask.parentTask){
711                                        if(predTask.parentTask.id != task.parentTask.id){
712                                                return false;
713                                        }
714                                }else{
715                                        return false;
716                                }
717                                if(!this.ganttChart.checkPosPreviousTask(predTask, task)){
718                                        this.ganttChart.correctPosPreviousTask(predTask, task);
719                                }
720                                task.previousTask = predTask;
721                        }
722                        var isAdd = false;
723                        if(sortRequired) for(var i = 0; i < parentTask.cldTasks.length; i++){
724                                if(task.startTime < parentTask.cldTasks[i].startTime){
725                                        parentTask.cldTasks.splice(i, 0, task);
726                                        if(i > 0){
727                                                parentTask.cldTasks[i - 1].nextChildTask = parentTask.cldTasks[i];
728                                                parentTask.cldTasks[i].previousChildTask = parentTask.cldTasks[i - 1];
729                                        }
730                                        if(parentTask.cldTasks[i + 1]){
731                                                parentTask.cldTasks[i + 1].previousChildTask = parentTask.cldTasks[i];
732                                                parentTask.cldTasks[i].nextChildTask = parentTask.cldTasks[i + 1];
733                                        }
734                                        isAdd = true;
735                                        break;
736                                }
737                        }
738                        if(!isAdd){
739                                if(parentTask.cldTasks.length > 0){
740                                        parentTask.cldTasks[parentTask.cldTasks.length - 1].nextChildTask = task;
741                                        task.previousChildTask = parentTask.cldTasks[parentTask.cldTasks.length - 1];
742                                }
743                                parentTask.cldTasks.push(task);
744                        }
745                        if(parentTask.cldTasks.length == 1){
746                                var treeImg = _parentTask.createTreeImg();
747                                _parentTask.cTaskNameItem[2] = treeImg;
748                        }
749                        _task = new dojox.gantt.GanttTaskControl(task, this, this.ganttChart);
750                        _task.create();
751                        if(task.nextChildTask) _task.nextChildTask = _task.project.getTaskById(task.nextChildTask.id);
752                        _task.adjustPanelTime();
753                        var rowHeight = this.ganttChart.heightTaskItem + this.ganttChart.heightTaskItemExtra;
754                        _task.shiftCurrentTasks(_task, rowHeight);//23
755                }else{
756                        startTime = startTime || this.project.startDate;
757                        task = new dojox.gantt.GanttTaskItem({
758                                id: id,
759                                name: name,
760                                startTime: startTime,
761                                duration: duration,
762                                percentage: percentage,
763                                previousTaskId: previousTaskId,
764                                taskOwner: taskOwner
765                        });
766                        if(task.startTime <= this.ganttChart.startDate){
767                                return false;
768                        }
769                        if(previousTaskId != ""){
770                                var predTask = this.project.getTaskById(previousTaskId);
771                                if(!predTask){
772                                        return false;
773                                }
774                                if(!this.ganttChart.checkPosPreviousTask(predTask, task)){
775                                        this.ganttChart.correctPosPreviousTask(predTask, task);
776                                }
777                                if(predTask.parentTask){
778                                        return false;
779                                }
780                                task.previousTask = predTask;
781                        }
782                        var isAdd = false;
783                        if(sortRequired){
784                                for(var i = 0; i < this.project.parentTasks.length; i++){
785                                        var ppTask = this.project.parentTasks[i];
786                                        if(startTime < ppTask.startTime){
787                                                this.project.parentTasks.splice(i, 0, task);
788                                                if(i > 0){
789                                                        this.project.parentTasks[i - 1].nextParentTask = task;
790                                                        task.previousParentTask = this.project.parentTasks[i - 1];
791                                                }
792                                                if(this.project.parentTasks[i + 1]){
793                                                        this.project.parentTasks[i + 1].previousParentTask = task;
794                                                        task.nextParentTask = this.project.parentTasks[i + 1];
795                                                }
796                                                isAdd = true;
797                                                break;
798                                        }
799                                }
800                        }
801                        if(!isAdd){
802                                if(this.project.parentTasks.length > 0){
803                                        this.project.parentTasks[this.project.parentTasks.length - 1].nextParentTask = task;
804                                        task.previousParentTask = this.project.parentTasks[this.project.parentTasks.length - 1];
805                                }
806                                this.project.parentTasks.push(task);
807                        }
808                        _task = new dojox.gantt.GanttTaskControl(task, this, this.ganttChart);
809                        _task.create();
810                        if(task.nextParentTask) _task.nextParentTask = _task.project.getTaskById(task.nextParentTask.id);
811                        _task.adjustPanelTime();
812                        this.arrTasks.push(_task);
813                        var rowHeight = this.ganttChart.heightTaskItem + this.ganttChart.heightTaskItemExtra;
814                        _task.shiftCurrentTasks(_task, rowHeight);
815                        this.projectItem[0].style.display = "inline";
816                        this.setPercentCompleted(this.getPercentCompleted());
817                        this.shiftProjectItem();
818                        this.showDescrProject();
819                }
820                this.ganttChart.checkHeighPanelTasks();
821                this.ganttChart.checkPosition();
822                return _task;
823        },
824        shiftNextProject: function(project, height){
825                if(project.nextProject){
826                        project.nextProject.shiftProject(height);
827                        this.shiftNextProject(project.nextProject, height);
828                }
829        },
830        shiftProject: function(height){
831                this.posY = this.posY + height;
832                this.projectItem[0].style.top = parseInt(this.projectItem[0].style.top) + height + "px";
833                this.descrProject.style.top = parseInt(this.descrProject.style.top) + height + "px";
834                this.projectNameItem.style.top = parseInt(this.projectNameItem.style.top) + height + "px";
835                if(this.arrTasks.length > 0){
836                        this.shiftNextParentTask(this.arrTasks[0], height);
837                }
838        },
839        shiftTask: function(task, height){
840                task.posY = task.posY + height;
841                var tNameItem0 = task.cTaskNameItem[0], tNameItem1 = task.cTaskNameItem[1], tNameItem2 = task.cTaskNameItem[2],
842                        tItem0 = task.cTaskItem[0], tItem1 = task.cTaskItem[1], tItem2 = task.cTaskItem[2];
843                tNameItem0.style.top = parseInt(tNameItem0.style.top) + height + "px";
844                if(tNameItem2){
845                        tNameItem2.style.top = parseInt(tNameItem2.style.top) + height + "px";
846                }
847                if(task.parentTask){
848                        tNameItem1[0].style.top = parseInt(tNameItem1[0].style.top) + height + "px";
849                        tNameItem1[1].style.top = parseInt(tNameItem1[1].style.top) + height + "px";
850                }
851                task.cTaskItem[0].style.top = parseInt(task.cTaskItem[0].style.top) + height + "px";
852                task.descrTask.style.top = parseInt(task.descrTask.style.top) + height + "px";
853                if(tItem1[0]){
854                        tItem1[0].style.top = parseInt(tItem1[0].style.top) + height + "px";
855                        tItem1[1].style.top = parseInt(tItem1[1].style.top) + height + "px";
856                        tItem1[2].style.top = parseInt(tItem1[2].style.top) + height + "px";
857                }
858        },
859        shiftNextParentTask: function(task, height){
860                this.shiftTask(task, height);
861                this.shiftChildTasks(task, height);
862                if(task.nextParentTask){
863                        this.shiftNextParentTask(task.nextParentTask, height);
864                }
865        },
866        shiftChildTasks: function(task, height){
867                dojo.forEach(task.childTask, function(cTask){
868                        this.shiftTask(cTask, height);
869                        if(cTask.childTask.length > 0){
870                                this.shiftChildTasks(cTask, height);
871                        }
872                }, this);
873        }
874});
875
876
877dojo.declare("dojox.gantt.GanttProjectItem", null, {
878        constructor: function(configuration){
879                //id is required
880                this.id = configuration.id;
881                this.name = configuration.name || this.id;
882                this.startDate = configuration.startDate || new Date();
883                this.parentTasks = [];
884        },
885        getTaskById: function(id){
886                for(var i = 0; i < this.parentTasks.length; i++){
887                        var pTask = this.parentTasks[i];
888                        var task = this.getTaskByIdInTree(pTask, id);
889                        if(task){
890                                return task;
891                        }
892                }
893                return null;
894        },
895        getTaskByIdInTree: function(parentTask, id){
896                if(parentTask.id == id){
897                        return parentTask;
898                }else{
899                        for(var i = 0; i < parentTask.cldTasks.length; i++){
900                                var pcTask = parentTask.cldTasks[i];
901                                if(pcTask.id == id){
902                                        return pcTask;
903                                }
904                                if(pcTask.cldTasks.length > 0){
905                                        if(pcTask.cldTasks.length > 0){
906                                                var cTask = this.getTaskByIdInTree(pcTask, id);
907                                                if(cTask){
908                                                        return cTask;
909                                                }
910                                        }
911                                }
912                        }
913                }
914                return null;
915        },
916        addTask: function(task){
917                this.parentTasks.push(task);
918                task.setProject(this);
919        },
920        deleteTask: function(id){
921                var task = this.getTaskById(id);
922                if(!task){return;}
923                if(!task.parentTask){
924                        for(var i = 0; i < this.parentTasks.length; i++){
925                                var pTask = this.parentTasks[i];
926                                if(pTask.id == id){
927                                        if(pTask.nextParentTask){
928                                                if(pTask.previousParentTask){
929                                                        pTask.previousParentTask.nextParentTask = pTask.nextParentTask;
930                                                        pTask.nextParentTask.previousParentTask = pTask.previousParentTask;
931                                                }else{
932                                                        pTask.nextParentTask.previousParentTask = null;
933                                                }
934                                        }else{
935                                                if(pTask.previousParentTask){
936                                                        pTask.previousParentTask.nextParentTask = null;
937                                                }
938                                        }
939                                        pTask = null;
940                                        this.parentTasks.splice(i, 1);
941                                        break;
942                                }
943                        }
944                }else{
945                        var parentTask = task.parentTask;
946                        for(var i = 0; i < parentTask.cldTasks.length; i++){
947                                var pcTask = parentTask.cldTasks[i];
948                                if(pcTask.id == id){
949                                        if(pcTask.nextChildTask){
950                                                if(pcTask.previousChildTask){
951                                                        pcTask.previousChildTask.nextChildTask = pcTask.nextChildTask;
952                                                        pcTask.nextChildTask.previousChildTask = pcTask.previousChildTask;
953                                                }else{
954                                                        pcTask.nextChildTask.previousChildTask = null;
955                                                }
956                                        }else{
957                                                if(pcTask.previousChildTask){
958                                                        pcTask.previousChildTask.nextChildTask = null;
959                                                }
960                                        }
961                                        pcTask = null;
962                                        parentTask.cldTasks.splice(i, 1);
963                                        break;
964                                }
965                        }
966                }
967        }
968});
Note: See TracBrowser for help on using the repository browser.