add-metadata.component.ts 7.88 KB
Newer Older
naji's avatar
naji committed
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
/*
Copyright © CINES - Centre Informatique National pour l'Enseignement Supérieur (2020)

[dad@cines.fr]

This software is a computer program whose purpose is to provide
a web application to create, edit, import and export archive
profiles based on the french SEDA standard
(https://redirect.francearchives.fr/seda/).


This software is governed by the CeCILL-C  license under French law and
abiding by the rules of distribution of free software.  You can  use,
modify and/ or redistribute the software under the terms of the CeCILL-C
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".

As a counterpart to the access to the source code and  rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty  and the software's author,  the holder of the
economic rights,  and the successive licensors  have only  limited
liability.

In this respect, the user's attention is drawn to the risks associated
with loading,  using,  modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean  that it is complicated to manipulate,  and  that  also
therefore means  that it is reserved for developers  and  experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and,  more generally, to use and operate it in the
same conditions as regards security.

The fact that you are presently reading this means that you have had
knowledge of the CeCILL-C license and that you accept its terms.
*/
naji's avatar
naji committed
38
39
40
import { Component, OnInit, Pipe, PipeTransform, TemplateRef } from '@angular/core';
import { MatDialogRef } from '@angular/material/dialog';
import { Subscription } from 'rxjs';
naji's avatar
naji committed
41
import { FileService } from '../../core/services/file.service';
naji's avatar
naji committed
42
import { PopupService } from '../../core/services/popup.service';
naji's avatar
naji committed
43
import { SedaService } from '../../core/services/seda.service';
naji's avatar
naji committed
44
45
import { FileNode } from '../../models/file-node';
import { SedaCardinalityConstants, SedaData, SedaElementConstants } from '../../models/seda-data';
naji's avatar
naji committed
46
import { PastisDialogData } from '../../shared/pastis-dialog/classes/pastis-dialog-data';
naji's avatar
naji committed
47
import { PastisDialogConfirmComponent } from '../../shared/pastis-dialog/pastis-dialog-confirm/pastis-dialog-confirm.component';
naji's avatar
naji committed
48
import { PastisPopupMetadataLanguageService } from '../../shared/pastis-popup-metadata-language/pastis-popup-metadata-language.service';
Baptiste Toulemonde's avatar
Baptiste Toulemonde committed
49
import { ProfileService } from '../../core/services/profile.service';
naji's avatar
naji committed
50
51
52
53
54
55
56
57
58
59

@Component({
  selector: 'pastis-user-action-add-metadata',
  templateUrl: './add-metadata.component.html',
  styleUrls: ['./add-metadata.component.scss']
})
export class UserActionAddMetadataComponent implements OnInit {

  btnIsDisabled: boolean;

naji's avatar
naji committed
60
61
62
  sedaData: SedaData;
  allowedChildren: SedaData[];
  filterName: string;
naji's avatar
naji committed
63
64
  namesFiltered: any = [];
  sedaNodeFound: SedaData;
naji's avatar
naji committed
65
66
  selectedSedaNode: SedaData;
  addedItems: SedaData[] = [];
naji's avatar
naji committed
67
68
  dialogData: PastisDialogData;

naji's avatar
naji committed
69
70
  atLeastOneIsSelected: boolean;
  customTemplate: TemplateRef<any>;
naji's avatar
naji committed
71
72
73
74
75
76
  fileNode: FileNode;
  sedaLanguage: boolean;
  sedaLanguageSub: Subscription;


  constructor(public dialogRef: MatDialogRef<PastisDialogConfirmComponent>,
naji's avatar
naji committed
77
              private fileService: FileService, private sedaService: SedaService,
78
              private popUpService: PopupService, private sedaLanguageService: PastisPopupMetadataLanguageService, private profileService: ProfileService) { }
naji's avatar
naji committed
79
80
81
82
83
84
85

  ngOnInit() {
    this.sedaLanguageSub = this.sedaLanguageService.sedaLanguage.subscribe(
      (value: boolean) => {
        this.sedaLanguage = value;
      },
      (error) => {
naji's avatar
naji committed
86
        console.log(error);
naji's avatar
naji committed
87
88
      }
    );
naji's avatar
naji committed
89
    this.fileService.nodeChange.subscribe(fileNode => {this.fileNode = fileNode; });
naji's avatar
naji committed
90
91
92
93
    this.sedaData = this.sedaService.sedaRules[0];

    this.sedaNodeFound = this.fileNode.sedaData;

Baptiste Toulemonde's avatar
Baptiste Toulemonde committed
94
95
96
97
98
99
100
101
102
103
104
    if (this.profileService.profileMode === "PA") {
      this.allowedChildren = this.sedaService.findSelectableElementList(this.sedaNodeFound, this.fileNode)
      .filter(e => e.Element !== SedaElementConstants.attribute);
    } else if (this.profileService.profileMode === "PUA" ) {
      if ( this.sedaNodeFound.Name === "ArchiveUnit"){
        this.allowedChildren = this.sedaNodeFound.Children
        .filter(e => e.Name === "Management" || e.Name === "ArchiveUnitProfile");
      } else {
        this.allowedChildren = this.sedaNodeFound.Children
      }
    }
naji's avatar
naji committed
105
106
    // Subscribe observer to button status and
    // set the inital state of the ok button to disabled
naji's avatar
naji committed
107
    this.popUpService.btnYesShoudBeDisabled.subscribe(status => {
naji's avatar
naji committed
108
      this.btnIsDisabled = status;
naji's avatar
naji committed
109
    });
naji's avatar
naji committed
110
111
  }

naji's avatar
naji committed
112
  selectSedaElement(selectedElements: string[]) {
naji's avatar
naji committed
113
    if (selectedElements.length) {
naji's avatar
naji committed
114
      this.selectedSedaNode = this.sedaService.getSedaNode(this.sedaData, selectedElements[0]);
naji's avatar
naji committed
115
116
117
    }
  }

naji's avatar
naji committed
118
119
  isElementSelected(element: SedaData) {
    if (this.addedItems) {
naji's avatar
naji committed
120
121
122
123
      return this.addedItems.includes(element);
    }
  }

naji's avatar
naji committed
124
125
  onRemoveSelectedElement(element: SedaData) {
    const indexOfElement = this.addedItems.indexOf(element);
naji's avatar
naji committed
126
127
128
129
130
131
132
    if (indexOfElement >= 0) {
      this.addedItems.splice(indexOfElement, 1);
    }
    if (element.Cardinality !== (SedaCardinalityConstants.zeroOrMore || SedaCardinalityConstants.oreOrMore)) {
      this.allowedChildren.push(element);
      this.allowedChildren = this.allowedChildren.slice(0, this.allowedChildren.length);
    }
naji's avatar
naji committed
133
    const orderedNames = Object.values(this.allowedChildren);
naji's avatar
naji committed
134
    this.allowedChildren.sort((a, b) => {
naji's avatar
naji committed
135
136
137
      return orderedNames.indexOf(a) - orderedNames.indexOf(b);
    });
    this.addedItems.length > 0 ? this.atLeastOneIsSelected = true : this.atLeastOneIsSelected = false;
naji's avatar
naji committed
138
139
140
    this.upateButtonStatusAndDataToSend();
  }

naji's avatar
naji committed
141
  onAddSelectedElement(element: SedaData) {
naji's avatar
naji committed
142
143
144
145
146
      this.addedItems.push(element);

      if (element.Cardinality !== (SedaCardinalityConstants.zeroOrMore || SedaCardinalityConstants.oreOrMore)) {
        this.allowedChildren = this.allowedChildren.filter(e => e != element);
      }
naji's avatar
naji committed
147
148
      this.addedItems.length > 0 ? this.atLeastOneIsSelected = true : this.atLeastOneIsSelected = false;
      this.upateButtonStatusAndDataToSend();
naji's avatar
naji committed
149
150
  }

naji's avatar
naji committed
151
  upateButtonStatusAndDataToSend() {
naji's avatar
naji committed
152
    this.popUpService.setPopUpDataOnClose(this.addedItems);
naji's avatar
naji committed
153
    this.popUpService.disableYesButton(!this.atLeastOneIsSelected);
naji's avatar
naji committed
154
155
  }

naji's avatar
naji committed
156
  onAllItemsAdded() {
naji's avatar
naji committed
157
158
159
    return this.allowedChildren.length === this.addedItems.length;
  }

naji's avatar
naji committed
160
161
  isElementComplex(element: SedaData) {
    if (element) {
naji's avatar
naji committed
162
163
164
165
      return element.Element === SedaElementConstants.complex;
    }
  }

naji's avatar
naji committed
166
  getDefinition(element: SedaData): string {
naji's avatar
naji committed
167
168
169
170
    return element ? element.Definition : '';
  }

  onYesClick(): void {
naji's avatar
naji committed
171
    console.log('Clicked ok on dialog : %o' , this.selectedSedaNode);
naji's avatar
naji committed
172
173
174
175
176
177
178
179
180

  }
  onNoClick(): void {
    this.dialogRef.close();
  }

  public onSearchSubmit(search: string): void {
    this.filterName = search;
  }
naji's avatar
naji committed
181
182
  onResolveName(element: SedaData): string {
    if (this.sedaLanguage) {
naji's avatar
naji committed
183
      return element.Name;
naji's avatar
naji committed
184
185
    } else {
      if (element.NameFr) {
naji's avatar
naji committed
186
187
188
189
190
191
        return element.NameFr;
      }
    }
    return element.Name;
  }
  ngOnDestroy(): void {
naji's avatar
naji committed
192
    if (this.sedaLanguageSub != null) {
naji's avatar
naji committed
193
194
195
196
197
198
199
200
201
      this.sedaLanguageSub.unsubscribe();
    }
  }

}

@Pipe({name: 'filterByName'})
export class FilterByNamePipe implements PipeTransform {
  transform(listOfElements: SedaData[], nameToFilter: string, sedaLanguage: boolean): SedaData[] {
naji's avatar
naji committed
202
203
204
    if (!listOfElements) { return null; }
    if (!nameToFilter) { return listOfElements; }
    if (sedaLanguage) {
naji's avatar
naji committed
205
      return listOfElements.filter(element => element.Name != undefined).filter(element => element.Name.toLowerCase().indexOf(nameToFilter.toLowerCase()) >= 0);
naji's avatar
naji committed
206
    } else {
naji's avatar
naji committed
207
208
209
210
211
      return listOfElements.filter(element => element.NameFr != undefined).filter(element => element.NameFr.toLowerCase().indexOf(nameToFilter.toLowerCase()) >= 0);
    }

  }
}