final-task.component.ts 9.97 KB
Newer Older
Romain DELEAU's avatar
Romain DELEAU committed
1 2
import { Component, Input, OnInit } from '@angular/core';
import { Mission } from 'src/app/class/mission/mission';
Romain DELEAU's avatar
Romain DELEAU committed
3 4 5
import { PrerequireRessource } from 'src/app/class/prerequires/prerequire-ressource/prerequire-ressource';
import { PrerequireTask } from 'src/app/class/prerequires/prerequire-task/prerequire-task';
import { Ressource } from 'src/app/class/ressource/ressource';
Romain DELEAU's avatar
Romain DELEAU committed
6
import { Role } from 'src/app/class/role/role';
Romain DELEAU's avatar
Romain DELEAU committed
7
import { Scenario } from 'src/app/class/scenario/scenario';
Romain DELEAU's avatar
Romain DELEAU committed
8
import { Task } from 'src/app/class/task/task';
Romain DELEAU's avatar
Romain DELEAU committed
9
import { PieceDetailsService } from 'src/app/services/piece-details/piece-details.service';
10
import { TooltipService } from 'src/app/services/tooltip/tooltip.service';
11 12 13
import { SuppressDialogComponent } from 'src/app/components/dialogs/suppress-dialog/suppress-dialog.component';
import { MatDialog } from '@angular/material/dialog';
import { CleanDialogComponent } from 'src/app/components/dialogs/clean-dialog/clean-dialog.component';
14 15
import { MatSnackBar } from '@angular/material/snack-bar';
import { IdentifierSnackbarComponent } from 'src/app/components/snackbars/identifier-snackbar/identifier-snackbar.component';
Romain DELEAU's avatar
Romain DELEAU committed
16 17 18 19 20 21 22 23 24 25 26 27

@Component({
  selector: 'app-final-task',
  templateUrl: './final-task.component.html',
  styleUrls: ['./final-task.component.scss']
})
export class FinalTaskComponent implements OnInit {

  displayMenu: string = 'hide';
  displaySymbolChoice: string = 'hide';
  displayPrequires: string = 'hide';

Romain DELEAU's avatar
Romain DELEAU committed
28
  pieceWidth: number = 400;
Romain DELEAU's avatar
Romain DELEAU committed
29

Romain DELEAU's avatar
Romain DELEAU committed
30
  @Input() task: Task = new Task('normal');
Romain DELEAU's avatar
Romain DELEAU committed
31
  @Input() scenario: Scenario = new Scenario();
Romain DELEAU's avatar
Romain DELEAU committed
32 33 34 35
  @Input() mission!: Mission;
  @Input() role!: Role;
  @Input() i!: number;
  @Input() j!: number;
Romain DELEAU's avatar
Romain DELEAU committed
36

37
  urlIcon: string = 'url("../../../../assets/background-images/final.png")';
38
  antecedent: boolean = false;
39

40 41
  constructor(protected pieceDetailsService: PieceDetailsService, protected tooltipService: TooltipService, public dialog: MatDialog,
    private _snackBar: MatSnackBar) { }
Romain DELEAU's avatar
Romain DELEAU committed
42 43

  ngOnInit(): void {
Romain DELEAU's avatar
Romain DELEAU committed
44 45
    this.setPieceWidth();
    this.mission.equalizeLengths();
Romain DELEAU's avatar
Romain DELEAU committed
46
  }
Romain DELEAU's avatar
Romain DELEAU committed
47
  
Romain DELEAU's avatar
Romain DELEAU committed
48
  durationChange(): void {
Romain DELEAU's avatar
Romain DELEAU committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    let beforeWidth: number = this.pieceWidth;
    this.setPieceWidth();
    let afterWidth: number = this.pieceWidth;
    let difference: number;
    // Increase
    if (beforeWidth < afterWidth) {
      difference = (afterWidth/beforeWidth)-1;
      for(let k = 0; k < difference; k++) {
        if (!(this.role.tasks[this.i][this.j+k+1] instanceof Task)) {
          this.role.tasks[this.i].splice(this.j+k+1, 1);
        }
      }
    }
    // Decrease
    if (afterWidth < beforeWidth) {
      difference = (beforeWidth/afterWidth)-1
      for (let k = 0; k < difference; k++) {
        this.role.tasks[this.i].splice(this.j+k+1, 0, null);
      }
    }
    this.mission.equalizeLengths();
  }

  setPieceWidth(): void {
Romain DELEAU's avatar
Romain DELEAU committed
73 74
    if(this.task.durationUnit === 'UT') {
      if(this.task.duration >= 1 && this.task.duration <= 10) {
Romain DELEAU's avatar
Romain DELEAU committed
75
        this.pieceWidth = (this.task.duration*400);
Romain DELEAU's avatar
Romain DELEAU committed
76
      } else if(this.task.duration > 10) {
Romain DELEAU's avatar
Romain DELEAU committed
77
        this.pieceWidth = 4000;
Romain DELEAU's avatar
Romain DELEAU committed
78
      } else {
Romain DELEAU's avatar
Romain DELEAU committed
79
        this.pieceWidth = 400;
Romain DELEAU's avatar
Romain DELEAU committed
80 81
      }
    } else {
Romain DELEAU's avatar
Romain DELEAU committed
82
      this.pieceWidth = 400;
Romain DELEAU's avatar
Romain DELEAU committed
83 84 85
    }
  }

86
  onClickErase(): void {
87 88 89 90 91 92 93 94 95
    const dialogRef = this.dialog.open(CleanDialogComponent, { data: 'Tâche finale' });
    dialogRef.afterClosed().subscribe(result => {
      if (result == true) {
        this.task.duration = 1;
        this.task.durationUnit = 'UT';
        this.task.identifier = '';
        this.task.objective = '';
        this.task.symbol.color = '';
        this.task.symbol.symbol = '';
96 97 98 99 100 101 102 103 104 105 106
        this.task.prerequireRessources = [];
        this.task.prerequireTasks = [];   
        this.role.tasks.forEach(inlineTasks => {
          inlineTasks.forEach(task => {
            task?.prerequireTasks.forEach((prerequire, index) => {
              if (prerequire.identifier == this.task.identifier) {
                task.prerequireTasks.splice(index, 1);
              }
            });
          });
        });
107 108
      }
    });
109
  } 
Romain DELEAU's avatar
Romain DELEAU committed
110

111
  onClickPiece(): void {
Romain DELEAU's avatar
Romain DELEAU committed
112
    this.pieceDetailsService.piece = this.task;
113
    this.pieceDetailsService.parent = this.role;
Romain DELEAU's avatar
Romain DELEAU committed
114 115
  }

Romain DELEAU's avatar
Romain DELEAU committed
116 117 118 119 120 121 122
  onClickChange(type: string): void {
    if (type == 'annexe') {
      this.task.symbol.color = '';
      this.task.symbol.symbol = '';
    }
    this.task.changeType(type);
    this.mission.equalizeLengths();
123 124 125
  }

  onClickDelete(): void {
126 127 128 129 130 131 132 133 134 135 136
    const dialogRef = this.dialog.open(SuppressDialogComponent, { data: 'cette Tâche finale' });
    dialogRef.afterClosed().subscribe(result => {
      if (result == true) {
        this.role.tasks.forEach(inlineTasks => {
          inlineTasks.forEach(task => {
            task?.prerequireTasks.forEach((prerequire, index) => {
              if (prerequire.identifier == this.task.identifier) {
                task.prerequireTasks.splice(index, 1);
              }
            });
          });
Romain DELEAU's avatar
Romain DELEAU committed
137
        });
138 139 140
        this.role.removeTask(this.i, this.j);
        this.mission.equalizeLengths();
      }
Romain DELEAU's avatar
Romain DELEAU committed
141
    });
Romain DELEAU's avatar
Romain DELEAU committed
142 143 144 145 146 147 148 149 150 151 152
  }

  changeDisplaySymbolChoice(): void {
    if(this.displaySymbolChoice == 'show') {
      this.displaySymbolChoice = 'hide';
    } else {
      this.displaySymbolChoice = 'show';
    }
  }

  setSymbol(symbol: string, symbolColor: string): void {
Romain DELEAU's avatar
Romain DELEAU committed
153 154
    this.task.symbol.symbol = symbol;
    this.task.symbol.color = symbolColor;
Romain DELEAU's avatar
Romain DELEAU committed
155 156 157 158 159 160 161 162 163 164 165
    this.displaySymbolChoice = 'hide';
  }

  changeDisplayPrerequires(): void {
    if(this.displayPrequires == 'show') {
      this.displayPrequires = 'hide';
    } else {
      this.displayPrequires = 'show';
    }
  }

Romain DELEAU's avatar
Romain DELEAU committed
166
  moveTask(direction: string): void {
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    if (direction == 'left' && this.canMoveTo('left')) {
      this.role.moveTask(this.i, this.j, direction);
      this.displayMenu = 'hide';
      this.displayPrequires = 'hide';
      this.displaySymbolChoice = 'hide';
      this.mission.equalizeLengths();
    } else if (direction == 'top' && this.canMoveTo('top')) {
      this.role.moveTask(this.i, this.j, direction);
      this.displayMenu = 'hide';
      this.displayPrequires = 'hide';
      this.displaySymbolChoice = 'hide';
      this.mission.equalizeLengths();
    } else if (direction == 'right' && this.canMoveTo('right')) {
      this.role.moveTask(this.i, this.j, direction);
      this.displayMenu = 'hide';
      this.displayPrequires = 'hide';
      this.displaySymbolChoice = 'hide';
      this.mission.equalizeLengths();
Romain DELEAU's avatar
Romain DELEAU committed
185
    } else if (direction == 'bottom') {
186 187 188 189 190 191
      this.role.moveTask(this.i, this.j, direction);
      this.displayMenu = 'hide';
      this.displayPrequires = 'hide';
      this.displaySymbolChoice = 'hide';
      this.mission.equalizeLengths();
    }
Romain DELEAU's avatar
Romain DELEAU committed
192 193 194 195 196 197 198 199
  }

  canMoveTo(direction: string): boolean {
    let res: boolean = true;
    if (direction == 'left') {
      if (this.role.tasks[this.i][this.j-1] instanceof Task || this.j == 0) {
        res = false;
      }
Romain DELEAU's avatar
Romain DELEAU committed
200 201 202 203
    } else if (direction == 'top') {
      if (this.i == 0) {
        res = false;
      }
Romain DELEAU's avatar
Romain DELEAU committed
204 205 206 207
    }
    return res;
  }

208 209 210 211 212 213 214 215 216 217
  findFirstIndexOfTaskType(type: string): number[] {
    for(let i = 0; i < this.role.tasks.length; i++) {
      for(let j = 0; j < this.role.tasks[i].length; j++) {
        if (this.role.tasks[i][j] instanceof Task && this.role.tasks[i][j]?.type == type) {
          return [i, j];
        }
      }
    }
    return [0, 0];
  } 
Romain DELEAU's avatar
Romain DELEAU committed
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

  changeIdentifier(event: any): void {
    let value: string = event.target.value;
    if (value != '') {
      this.role.tasks.forEach(inlineTasks => {
        inlineTasks.forEach(task => {
          task?.prerequireTasks.forEach(prerequire => {
            if (prerequire.identifier == this.task.identifier) {
              prerequire.identifier = value;
            }
          });
        });
      });
    } else {
      this.role.tasks.forEach(inlineTasks => {
        inlineTasks.forEach(task => {
          task?.prerequireTasks.forEach((prerequire, index) => {
            if (prerequire.identifier == this.task.identifier) {
              task.prerequireTasks.splice(index, 1);
            }
          });
        });
      }); 
    }
    this.task.identifier = value;
243 244 245
    if (this.role.isAlreadyUsedIdentifier(this.task.identifier)) {
      this._snackBar.openFromComponent(IdentifierSnackbarComponent, { duration: 5000 });      
    }
Romain DELEAU's avatar
Romain DELEAU committed
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
  }

  checkboxChangedTask(event: any, task:(Task|null)): void {
    if (task instanceof Task) {
      if (event.target.checked) {
        this.onCheckTask(task);
      } else {
        this.onUncheckTask(task);
      }      
    }
  }

  isCheckedTask(task: (Task|null)): boolean {
    if (task instanceof Task) {
      return this.task.prerequireTasks.some(element => element.identifier == task.identifier);
    }
    return false;
  }

  onCheckTask(task: Task): void {
    this.task.prerequireTasks.push(new PrerequireTask(task.identifier));
  }

  onUncheckTask(task: Task): void {
    let i: number = this.task.prerequireTasks.findIndex(element => element.identifier == task.identifier);
    this.task.prerequireTasks.splice(i,1);
  }

  checkboxChangedRessource(event: any, ressource: Ressource): void {
    if (event.target.checked) {
      this.onCheckRessource(ressource);
    } else {
      this.onUncheckRessource(ressource);
    }
  }

  isCheckedRessource(ressource: Ressource): boolean {
    return this.task.prerequireRessources.some(element => element.ressource == ressource);
  }

  onCheckRessource(ressource: Ressource): void {
    this.task.prerequireRessources.push(new PrerequireRessource(ressource));
  }

  onUncheckRessource(ressource: Ressource): void {
    let i: number = this.task.prerequireRessources.findIndex(element => ressource == element.ressource);
    this.task.prerequireRessources.splice(i, 1);
  }

  getAssociatePrerequireRessource(ressource: Ressource): PrerequireRessource {
    let i: number = this.task.prerequireRessources.findIndex(element => ressource == element.ressource);
    return this.task.prerequireRessources[i];
  }
299 300 301 302 303 304 305 306 307 308 309 310
  
  hasPossibleAntecedents(): boolean {
    let res = false;
    this.role.tasks.forEach(inlineTask => {
      for(let i = 0; i < this.j; i++) {
        if (inlineTask[i]?.identifier && (this.task.identifier != inlineTask[i]?.identifier)) {
          res = true;
        }
      }
    });
    return res;
  }
Romain DELEAU's avatar
Romain DELEAU committed
311
}