-
Notifications
You must be signed in to change notification settings - Fork 532
Expand file tree
/
Copy pathadmin-edit-cms-metadata.component.ts
More file actions
168 lines (157 loc) · 4.97 KB
/
admin-edit-cms-metadata.component.ts
File metadata and controls
168 lines (157 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import {
AsyncPipe,
KeyValuePipe,
NgTemplateOutlet,
} from '@angular/common';
import {
Component,
OnInit,
} from '@angular/core';
import { FormsModule } from '@angular/forms';
import { NotificationsService } from '@dspace/core/notification-system/notifications.service';
import {
TranslateModule,
TranslateService,
} from '@ngx-translate/core';
import { Operation } from 'fast-json-patch';
import { BehaviorSubject } from 'rxjs';
import { BtnDisabledDirective } from 'src/app/shared/btn-disabled.directive';
import { environment } from '../../../environments/environment';
import { SiteDataService } from '../../core/data/site-data.service';
import { getFirstCompletedRemoteData } from '../../core/shared/operators';
import { Site } from '../../core/shared/site.model';
import { AlertComponent } from '../../shared/alert/alert.component';
/**
* Component representing the page to edit cms metadata for site.
*/
@Component({
selector: 'ds-edit-homepage-metadata',
templateUrl: './admin-edit-cms-metadata.component.html',
styleUrls: ['./admin-edit-cms-metadata.component.scss'],
imports: [
AlertComponent,
AsyncPipe,
BtnDisabledDirective,
FormsModule,
KeyValuePipe,
NgTemplateOutlet,
TranslateModule,
],
})
export class AdminEditCmsMetadataComponent implements OnInit {
/**
* default value of the select options
*/
selectedMetadata: string;
/**
* default true to show the select options
*/
editMode: BehaviorSubject<boolean> = new BehaviorSubject(false);
/**
* The map between language codes available and their label
*/
languageMap: Map<string, string> = new Map();
/**
* key value pair map with language and value of metadata
*/
selectedMetadataValues: Map<string, string> = new Map();
/**
* the owner object of the metadataList
*/
site: Site;
/**
* list of the metadata to be edited by the user
*/
metadataList: string[] = [];
constructor(
private siteService: SiteDataService,
private notificationsService: NotificationsService,
private translateService: TranslateService,
) {
}
ngOnInit(): void {
environment.languages.filter((language) => language.active).forEach((language) => {
this.languageMap.set(language.code, language.label);
});
environment.cms.metadataList.forEach((md) => {
// Do not allow the user to edit the top footer if it is not enabled
if (md === 'dspace.cms.footer' && !environment.homePage.showTopFooter) {
return;
}
if (md === 'dspace.cms.home-header' && !environment.homePage.editHomeHeader) {
return;
}
if (md === 'dspace.cms.home-news' && !environment.homePage.editHomeNews) {
return;
}
this.metadataList.push(md);
});
this.siteService.find().subscribe((site) => {
this.site = site;
});
}
/**
* Save metadata values
*/
saveMetadata() {
const operations = this.getOperationsToEditText();
this.siteService.patch(this.site, operations).pipe(getFirstCompletedRemoteData())
.subscribe((restResponse) => {
if (restResponse.hasSucceeded) {
this.site = restResponse.payload;
this.notificationsService.success(this.translateService.get('admin.edit-cms-metadata.success'));
this.selectedMetadata = undefined;
this.editMode.next(false);
} else {
this.notificationsService.error(this.translateService.get('admin.edit-cms-metadata.error'));
}
this.siteService.setStale();
this.siteService.find().subscribe((site) => {
this.site = site;
});
});
}
/**
* Reset metadata selection and go back to the select options
*/
back() {
this.selectedMetadata = undefined;
this.editMode.next(false);
}
/**
* Get the label for a language key using language map
* @param key Key of the language to get the label for
* @returns Label of the language if found, otherwise the key itself
*/
languageLabel(key: string) {
return this.languageMap.get(key) ?? key;
}
/**
* Start editing selected metadata
*/
editSelectedMetadata() {
if (this.selectedMetadata) {
this.languageMap.forEach((value: string, key: string) => {
const text = this.site.firstMetadataValue(this.selectedMetadata, { language: key });
this.selectedMetadataValues.set(key, text);
});
}
this.editMode.next(true);
}
/**
* Create a list of operations to send to backend to edit selected metadata
* @returns List of operations to send to backend to edit selected metadata
*/
private getOperationsToEditText(): Operation[] {
const entries = Array.from(this.selectedMetadataValues.entries());
// First entry should form a 'replace' operation, then the rest should be an 'add' operation
return entries.map(([language, text], index) => ({
op: index === 0 ? 'replace' : 'add',
path: `/metadata/${this.selectedMetadata}`,
value: {
value: text ?? '',
language: language,
},
}));
}
}