step.component.ts 7.81 KB
Newer Older
1
import { Component, Input, OnInit, SimpleChanges } from '@angular/core';
2
import { MatDialog } from '@angular/material/dialog';
Romain DELEAU's avatar
Romain DELEAU committed
3 4
import { Mission } from 'src/app/class/mission/mission';
import { Role } from 'src/app/class/role/role';
5
import { Step } from 'src/app/class/step/step';
Romain DELEAU's avatar
Romain DELEAU committed
6
import { PieceDetailsService } from 'src/app/services/piece-details/piece-details.service';
7 8
import { SuppressDialogComponent } from 'src/app/components/dialogs/suppress-dialog/suppress-dialog.component';
import { CleanDialogComponent } from 'src/app/components/dialogs/clean-dialog/clean-dialog.component';
9
import { TooltipService } from 'src/app/services/tooltip/tooltip.service';
10 11
import { Trace } from 'src/app/class/trace/trace';
import { Scenario } from 'src/app/class/scenario/scenario';
12
import { MinimapService } from 'src/app/services/minimap/minimap.service';
13
import { TranslateService } from '@ngx-translate/core';
Romain DELEAU's avatar
Romain DELEAU committed
14 15 16 17 18 19 20 21

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

22 23
  @Input() scenario: Scenario = new Scenario();
  @Input() step: Step = new Step();
Romain DELEAU's avatar
Romain DELEAU committed
24 25 26
  @Input() parent!: Mission | Role;
  @Input() index!: number;
  @Input() mission!: Mission;
27 28
  @Input() roleIndex!: number;
  @Input() missionIndex: number = 0;
29

30
  displayMenu: string = 'hide';
Romain DELEAU's avatar
Romain DELEAU committed
31
  pieceWidth: number = 400;
32
  urlIcon: string = 'url("./assets/background-images/step.png")';
33

34
  constructor(protected pieceDetailsService: PieceDetailsService, public dialog: MatDialog, protected tooltipService: TooltipService, private minimapService: MinimapService, protected translate: TranslateService) { }
Romain DELEAU's avatar
Romain DELEAU committed
35 36

  ngOnInit(): void {
Romain DELEAU's avatar
Romain DELEAU committed
37 38
    this.setPieceWidth();
    this.mission.equalizeLengths();
39
    this.minimapService.reset();
Romain DELEAU's avatar
Romain DELEAU committed
40
  }
Romain DELEAU's avatar
Romain DELEAU committed
41 42 43 44 45 46 47 48 49 50

  getStepNumber(): number {
    let number: number = 1;
    for(let i = 0; i < this.index; i++) {
      if (this.parent.chronologie[i] instanceof Step) {
        number++;
      }
    }
    return number;
  }
Romain DELEAU's avatar
Romain DELEAU committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  
  durationChange(): void {
    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.parent.chronologie[this.index+k+1] instanceof Step)) {
          this.parent.chronologie.splice(this.index+k+1, 1);
        }
      }
    }
    // Decrease
    if (afterWidth < beforeWidth) {
      difference = (beforeWidth/afterWidth)-1
      for (let k = 0; k < difference; k++) {
        this.parent.chronologie.splice(this.index+k+1, 0, null);
      }
    }
    this.mission.equalizeLengths();
74
    this.minimapService.reset();
Romain DELEAU's avatar
Romain DELEAU committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88
  }

  setPieceWidth(): void {
    if(this.step.durationUnit === 'UT') {
      if(this.step.duration >= 1 && this.step.duration <= 10) {
        this.pieceWidth = (this.step.duration*400);
      } else if(this.step.duration > 10) {
        this.pieceWidth = 4000;
      } else {
        this.pieceWidth = 400;
      }
    } else {
      this.pieceWidth = 400;
    }
Romain DELEAU's avatar
Romain DELEAU committed
89 90
  }

91
  onClickPiece(): void {
Romain DELEAU's avatar
Romain DELEAU committed
92
    this.pieceDetailsService.piece = this.step;
Romain DELEAU's avatar
Romain DELEAU committed
93 94 95 96 97 98 99
    this.pieceDetailsService.missionIndex = this.missionIndex;
    if (this.parent instanceof Mission) {
      this.pieceDetailsService.roleIndex = undefined;
    } else {
      this.pieceDetailsService.roleIndex = this.roleIndex;
    }
    this.pieceDetailsService.pieceIndex = this.index;
Romain DELEAU's avatar
Romain DELEAU committed
100 101
  }

102
  onClickErase(): void {
103
    const dialogRef = this.dialog.open(CleanDialogComponent, { data: this.translate.instant('step_clean') });
104 105 106 107
    dialogRef.afterClosed().subscribe(result => {
      if (result == true) {
        this.step.description = '';
        this.step.durationUnit = 'UT';
108 109 110 111 112 113 114 115 116 117 118 119
        this.step.duration = 1;
        if (this.parent instanceof Mission) {
          this.scenario.traces.push(new Trace(this.scenario.traces.length,'erase',this.missionIndex,undefined,'all','Step_m_['+this.index+']','#ACC9FC'));
        } else if (this.parent instanceof Role) {
          this.scenario.traces.push(new Trace(this.scenario.traces.length,'erase',this.missionIndex,this.roleIndex,'all','Step_r_['+this.index+']','#ACC9FC'));
        }
      } else {
        if (this.parent instanceof Mission) {
          this.scenario.traces.push(new Trace(this.scenario.traces.length,'cancel_erase',this.missionIndex,undefined,'all','Step_m_['+this.index+']','#ACC9FC'));
        } else if (this.parent instanceof Role) {
          this.scenario.traces.push(new Trace(this.scenario.traces.length,'cancel_erase',this.missionIndex,this.roleIndex,'all','Step_r_['+this.index+']','#ACC9FC'));
        }
120 121
      }
    });
Romain DELEAU's avatar
Romain DELEAU committed
122 123
  }

124
  onClickDelete(): void {
125
    const dialogRef = this.dialog.open(SuppressDialogComponent, { data: this.translate.instant('step_delete')+' '+(this.parent instanceof Mission ? this.translate.instant('mission') : this.translate.instant('role_title')) });
126 127 128 129 130 131 132 133
    dialogRef.afterClosed().subscribe(result => {
      if (result == true) {
        if (this.parent instanceof Mission) {
          this.parent.removeChronologieStep(this.index);
        } else if (this.parent instanceof Role) {
          this.parent.removeChronologieStep(this.index);
        }
        this.mission.equalizeLengths();       
134 135 136 137 138 139 140 141 142 143 144
        if (this.parent instanceof Mission) {
          this.scenario.traces.push(new Trace(this.scenario.traces.length,'delete',this.missionIndex,undefined,'all','Step_m_['+this.index+']','#ACC9FC'));
        } else if (this.parent instanceof Role) {
          this.scenario.traces.push(new Trace(this.scenario.traces.length,'erase',this.missionIndex,this.roleIndex,'all','Step_r_['+this.index+']','#ACC9FC'));
        }
      } else {
        if (this.parent instanceof Mission) {
          this.scenario.traces.push(new Trace(this.scenario.traces.length,'cancel_erase',this.missionIndex,undefined,'all','Step_m_['+this.index+']','#ACC9FC'));
        } else if (this.parent instanceof Role) {
          this.scenario.traces.push(new Trace(this.scenario.traces.length,'cancel_erase',this.missionIndex,this.roleIndex,'all','Step_r_['+this.index+']','#ACC9FC'));
        }
145 146
      }
    });
Romain DELEAU's avatar
Romain DELEAU committed
147 148
  }

149 150 151 152 153
  moveStep(direction: string) {
    this.parent.moveStep(this.index, direction);
    this.displayMenu = 'hide';
    this.mission.equalizeLengths();
  }
154 155 156 157 158 159 160 161 162

  FirstStepIndex(): number {
    for(let i = 0; i < this.parent.chronologie.length; i++) {
      if (this.parent.chronologie[i] instanceof Step) {
        return i;
      }
    }
    return 0;
  }
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

  editTrace(event: any, source: string): void {
    if (event.target.value != '') {
      if (this.parent instanceof Mission) {
        this.scenario.traces.push(new Trace(this.scenario.traces.length,'write',this.missionIndex,undefined,source,'Step_m_['+this.index+']','#ACC9FC'));
      } else if (this.parent instanceof Role) {
        this.scenario.traces.push(new Trace(this.scenario.traces.length,'write',this.missionIndex,this.roleIndex,source,'Step_r_['+this.index+']','#ACC9FC'));
      }
    } else {
      if (this.parent instanceof Mission) {
        this.scenario.traces.push(new Trace(this.scenario.traces.length,'erase',this.missionIndex,undefined,source,'Step_m_['+this.index+']','#ACC9FC'));
      } else if (this.parent instanceof Role) {
        this.scenario.traces.push(new Trace(this.scenario.traces.length,'erase',this.missionIndex,this.roleIndex,source,'Step_r_['+this.index+']','#ACC9FC'));
      }
    }
  }

  editMoveTrace(event: any, source: string): void {
    if (event.target.value != '') {
      if (this.parent instanceof Mission) {
        this.scenario.traces.push(new Trace(this.scenario.traces.length,'move',this.missionIndex,undefined,source,'Step_m_['+this.index+']','#ACC9FC'));
      } else if (this.parent instanceof Role) {
        this.scenario.traces.push(new Trace(this.scenario.traces.length,'move',this.missionIndex,this.roleIndex,source,'Step_r_['+this.index+']','#ACC9FC'));
      }
    }
  }
Romain DELEAU's avatar
Romain DELEAU committed
189
}