1
|
import {Component, Input} from "@angular/core";
|
2
|
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
|
3
|
import {EnvProperties} from "../../../../openaireLibrary/utils/properties/env-properties";
|
4
|
import {properties} from "../../../../../environments/environment";
|
5
|
import {CommunityInfo} from "../../../../openaireLibrary/connect/community/communityInfo";
|
6
|
import {Session, User} from "../../../../openaireLibrary/login/utils/helper.class";
|
7
|
import {CommunityService} from "../../../../openaireLibrary/connect/community/community.service";
|
8
|
import {UtilitiesService} from "../../../../openaireLibrary/services/utilities.service";
|
9
|
import {UserManagementService} from "../../../../openaireLibrary/services/user-management.service";
|
10
|
import {StringUtils} from "../../../../openaireLibrary/utils/string-utils.class";
|
11
|
import {Subscription} from "rxjs";
|
12
|
import {Option} from "../../../../openaireLibrary/sharedComponents/input/input.component";
|
13
|
|
14
|
declare var UIkit;
|
15
|
|
16
|
@Component({
|
17
|
selector: 'edit-community',
|
18
|
template: `
|
19
|
<div [ngStyle]="{'max-height': maxHeight}"
|
20
|
[class.uk-padding-small]="!paddingLarge" [class.uk-padding-large]="paddingLarge"
|
21
|
class="uk-overflow-auto uk-padding-remove-bottom">
|
22
|
<form *ngIf="communityFb" [formGroup]="communityFb">
|
23
|
<div class="uk-grid" [class.uk-margin-small-bottom]="!paddingLarge"
|
24
|
[class.uk-margin-large-bottom]="paddingLarge" uk-grid uk-height-match=".uk-form-hint">
|
25
|
<div dashboard-input class="uk-width-1-1" [formInput]="communityFb.get('name')" label="Name"
|
26
|
placeholder="Write a name..."
|
27
|
hint="Name of the community profile."></div>
|
28
|
<div dashboard-input class="uk-width-1-1" [formInput]="communityFb.get('shortName')"
|
29
|
hint="Short name of the community."
|
30
|
label="Short Name" placeholder="Write an acronym..."></div>
|
31
|
<!-- <div dashboard-input class="uk-width-1-1" [type]="'textarea'" placeholder="Write a description..."-->
|
32
|
<!-- hint="The description of the community."-->
|
33
|
<!-- [rows]="4" [formInput]="communityFb.get('description')" label="Description"></div>-->
|
34
|
|
35
|
<div class="uk-width-1-1">
|
36
|
<div class="uk-text-bold uk-form-label uk-margin-small-bottom">Description</div>
|
37
|
<div class="uk-margin-bottom uk-form-hint">The description of the community</div>
|
38
|
<ckeditor [readonly]="false"
|
39
|
debounce="500"
|
40
|
[formControl]="communityFb.get('description')"
|
41
|
[config]="{ extraAllowedContent: '* [uk-*](*) ; span', disallowedContent: 'script; *[on*]',
|
42
|
removeButtons: 'Save,NewPage,DocProps,Preview,Print,' +
|
43
|
'Form,Checkbox,Radio,TextField,Textarea,Select,Button,ImageButton,HiddenField,' +
|
44
|
'CreateDiv,Flash,PageBreak,' +
|
45
|
'Subscript,Superscript,Anchor,Smiley,Iframe,Styles,Font,About,Language',
|
46
|
extraPlugins: 'divarea'}">
|
47
|
</ckeditor>
|
48
|
</div>
|
49
|
|
50
|
<input #file id="photo" type="file" class="uk-hidden" (change)="fileChangeEvent($event)"/>
|
51
|
<div dashboard-input class="uk-width-1-1" type="logoURL" [hideControl]="communityFb.get('isUpload').value" flex="top"
|
52
|
hint="Upload or link the logo of the community." [placeholder]="'Write link to the logo'"
|
53
|
[formInput]="communityFb.get('logoUrl')" label="Logo">
|
54
|
<div *ngIf="!communityFb.get('isUpload').value" class="uk-width-2-5@l uk-width-1-1" style="margin-top: 7px;">
|
55
|
<div class="uk-grid uk-flex uk-flex-middle" uk-grid>
|
56
|
<div class="uk-width-3-4@l uk-width-1-1 uk-flex uk-flex-center">
|
57
|
<button class="uk-button uk-button-secondary uk-flex uk-flex-middle uk-flex-wrap"
|
58
|
(click)="file.click()">
|
59
|
<icon name="cloud_upload" [flex]="true"></icon>
|
60
|
<span class="uk-margin-small-left">Upload a file</span>
|
61
|
</button>
|
62
|
</div>
|
63
|
<div class="uk-text-center uk-text-bold uk-width-expand">
|
64
|
OR
|
65
|
</div>
|
66
|
</div>
|
67
|
</div>
|
68
|
<div *ngIf="communityFb.get('isUpload').value" class="uk-width-1-1 uk-flex uk-flex-middle">
|
69
|
<div class="uk-card uk-card-default uk-text-center uk-border-circle">
|
70
|
<img class="uk-position-center" [src]="photo">
|
71
|
</div>
|
72
|
<div class="uk-margin-left">
|
73
|
<button (click)="remove()" class="uk-button-secondary outlined uk-icon-button">
|
74
|
<icon name="remove"></icon>
|
75
|
</button>
|
76
|
</div>
|
77
|
<div class="uk-margin-small-left">
|
78
|
<button class="uk-button-secondary uk-icon-button" (click)="file.click()">
|
79
|
<icon name="edit"></icon>
|
80
|
</button>
|
81
|
</div>
|
82
|
</div>
|
83
|
</div>
|
84
|
<div *ngIf="uploadError" class="uk-text-danger uk-width-1-1">{{uploadError}}</div>
|
85
|
<div dashboard-input class="uk-width-1-1" [formInput]="communityFb.get('status')"
|
86
|
hint="Set the visibility status for your community's profile." type="select" [options]="statuses"
|
87
|
label="Status" placeholder="Choose a status"></div>
|
88
|
</div>
|
89
|
</form>
|
90
|
</div>`,
|
91
|
styleUrls: ['edit-community.component.css']
|
92
|
})
|
93
|
export class EditCommunityComponent {
|
94
|
@Input()
|
95
|
public maxHeight = 'none';
|
96
|
@Input()
|
97
|
public paddingLarge: boolean = false;
|
98
|
public communityFb: FormGroup;
|
99
|
public statuses: Option[] = [
|
100
|
{label: 'Visible', value: 'all'},
|
101
|
{label: 'Visible to managers', value: 'manager'},
|
102
|
{label: 'Hidden', value: 'hidden'}
|
103
|
]
|
104
|
public community: CommunityInfo;
|
105
|
public isNew: boolean;
|
106
|
public properties: EnvProperties = properties
|
107
|
;
|
108
|
private subscriptions: any[] = [];
|
109
|
/**
|
110
|
* Photo upload
|
111
|
* */
|
112
|
public file: File;
|
113
|
public photo: string | ArrayBuffer;
|
114
|
public uploadError: string;
|
115
|
public deleteCurrentPhoto: boolean = false;
|
116
|
private maxsize: number = 200 * 1024;
|
117
|
user: User;
|
118
|
|
119
|
constructor(private fb: FormBuilder,
|
120
|
private communityService: CommunityService,
|
121
|
private utilsService: UtilitiesService,
|
122
|
private userManagementService: UserManagementService) {
|
123
|
}
|
124
|
|
125
|
ngOnDestroy() {
|
126
|
this.reset();
|
127
|
}
|
128
|
|
129
|
public init(community: CommunityInfo, isNew: boolean = false) {
|
130
|
this.reset();
|
131
|
this.deleteCurrentPhoto = false;
|
132
|
this.community = community;
|
133
|
this.isNew = isNew;
|
134
|
this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => {
|
135
|
this.user = user;
|
136
|
this.communityFb = this.fb.group({
|
137
|
communityId: this.fb.control(this.community.communityId),
|
138
|
name: this.fb.control(this.community.title, Validators.required),
|
139
|
shortName: this.fb.control(this.community.shortTitle, Validators.required),
|
140
|
description: this.fb.control(this.community.description),
|
141
|
status: this.fb.control(this.community.status),
|
142
|
managers: this.fb.control(this.community.managers),
|
143
|
isUpload: this.fb.control(this.community.isUpload),
|
144
|
logoUrl: this.fb.control(this.community.logoUrl)
|
145
|
});
|
146
|
if (this.community.isUpload) {
|
147
|
this.communityFb.get('logoUrl').clearValidators();
|
148
|
this.communityFb.get('logoUrl').updateValueAndValidity();
|
149
|
} else {
|
150
|
this.communityFb.get('logoUrl').setValidators([StringUtils.urlValidator()]);
|
151
|
this.communityFb.get('logoUrl').updateValueAndValidity();
|
152
|
}
|
153
|
this.subscriptions.push(this.communityFb.get('isUpload').valueChanges.subscribe(value => {
|
154
|
if (value == true) {
|
155
|
this.communityFb.get('logoUrl').clearValidators();
|
156
|
this.communityFb.updateValueAndValidity();
|
157
|
} else {
|
158
|
this.communityFb.get('logoUrl').setValidators([StringUtils.urlValidator()]);
|
159
|
this.communityFb.updateValueAndValidity();
|
160
|
}
|
161
|
}));
|
162
|
this.initPhoto();
|
163
|
if (!isNew) {
|
164
|
if (!this.isAdmin) {
|
165
|
setTimeout(() => {
|
166
|
this.communityFb.get('shortName').disable();
|
167
|
}, 0);
|
168
|
}
|
169
|
}
|
170
|
}
|
171
|
));
|
172
|
}
|
173
|
|
174
|
public get isAdmin() {
|
175
|
return Session.isPortalAdministrator(this.user);
|
176
|
}
|
177
|
|
178
|
public get disabled(): boolean {
|
179
|
return (this.communityFb && this.communityFb.invalid) ||
|
180
|
(this.communityFb && this.communityFb.pristine && !this.isNew && !this.file) ||
|
181
|
(this.uploadError && this.uploadError.length > 0);
|
182
|
}
|
183
|
|
184
|
public get dirty(): boolean {
|
185
|
return this.communityFb && this.communityFb.dirty;
|
186
|
}
|
187
|
|
188
|
reset() {
|
189
|
this.uploadError = null;
|
190
|
this.communityFb = null;
|
191
|
this.subscriptions.forEach(subscription => {
|
192
|
if (subscription instanceof Subscription) {
|
193
|
subscription.unsubscribe();
|
194
|
}
|
195
|
});
|
196
|
}
|
197
|
|
198
|
public save(callback: Function, errorCallback: Function = null) {
|
199
|
if (this.file) {
|
200
|
this.communityFb.get('shortName').enable();
|
201
|
this.subscriptions.push(this.utilsService.uploadPhoto(this.properties.utilsService + "/upload/stakeholder/" + encodeURIComponent(this.communityFb.value.communityId), this.file).subscribe(res => {
|
202
|
this.deletePhoto();
|
203
|
this.removePhoto();
|
204
|
this.communityFb.get('logoUrl').setValue(res.filename);
|
205
|
this.saveCommunity(callback, errorCallback);
|
206
|
}, error => {
|
207
|
this.uploadError = "An error has been occurred during upload your image. Try again later";
|
208
|
this.saveCommunity(callback, errorCallback);
|
209
|
}));
|
210
|
} else if (this.deleteCurrentPhoto) {
|
211
|
this.deletePhoto();
|
212
|
this.saveCommunity(callback, errorCallback);
|
213
|
} else {
|
214
|
this.saveCommunity(callback, errorCallback);
|
215
|
}
|
216
|
}
|
217
|
|
218
|
public saveCommunity(callback: Function, errorCallback: Function = null) {
|
219
|
if (this.isNew) {
|
220
|
this.removePhoto();
|
221
|
this.subscriptions.push(this.communityService.updateCommunity(
|
222
|
this.properties.communityAPI + this.community.communityId, this.communityFb.value).subscribe(() => {
|
223
|
this.communityService.getCommunity(this.community.communityId, true).subscribe(community => {
|
224
|
UIkit.notification(community.shortTitle + ' has been <b>successfully created</b>', {
|
225
|
status: 'success',
|
226
|
timeout: 6000,
|
227
|
pos: 'bottom-right'
|
228
|
});
|
229
|
callback(community);
|
230
|
});
|
231
|
}, error => {
|
232
|
UIkit.notification('An error has occurred. Please try again later', {
|
233
|
status: 'danger',
|
234
|
timeout: 6000,
|
235
|
pos: 'bottom-right'
|
236
|
});
|
237
|
if (errorCallback) {
|
238
|
errorCallback(error)
|
239
|
}
|
240
|
}));
|
241
|
} else {
|
242
|
this.subscriptions.push(this.communityService.updateCommunity(this.properties.communityAPI + this.community.communityId, this.communityFb.value).subscribe(() => {
|
243
|
this.communityService.getCommunity(this.community.communityId, true).subscribe(community => {
|
244
|
UIkit.notification(community.shortTitle + ' has been <b>successfully saved</b>', {
|
245
|
status: 'success',
|
246
|
timeout: 6000,
|
247
|
pos: 'bottom-right'
|
248
|
});
|
249
|
callback(community);
|
250
|
});
|
251
|
}, error => {
|
252
|
UIkit.notification('An error has occurred. Please try again later', {
|
253
|
status: 'danger',
|
254
|
timeout: 6000,
|
255
|
pos: 'bottom-right'
|
256
|
});
|
257
|
}));
|
258
|
}
|
259
|
}
|
260
|
|
261
|
fileChangeEvent(event) {
|
262
|
if (event.target.files && event.target.files[0]) {
|
263
|
this.file = event.target.files[0];
|
264
|
if (this.file.type !== 'image/png' && this.file.type !== 'image/jpeg') {
|
265
|
this.uploadError = 'You must choose a file with type: image/png or image/jpeg!';
|
266
|
this.communityFb.get('isUpload').setValue(false);
|
267
|
this.communityFb.get('isUpload').markAsDirty();
|
268
|
this.removePhoto();
|
269
|
} else if (this.file.size > this.maxsize) {
|
270
|
this.uploadError = 'File exceeds size\'s limit! Maximum resolution is 256x256 pixels.';
|
271
|
this.communityFb.get('isUpload').setValue(false);
|
272
|
this.communityFb.get('isUpload').markAsDirty();
|
273
|
this.removePhoto();
|
274
|
} else {
|
275
|
this.uploadError = null;
|
276
|
const reader = new FileReader();
|
277
|
reader.readAsDataURL(this.file);
|
278
|
reader.onload = () => {
|
279
|
this.photo = reader.result;
|
280
|
this.communityFb.get('isUpload').setValue(true);
|
281
|
this.communityFb.get('isUpload').markAsDirty();
|
282
|
};
|
283
|
}
|
284
|
}
|
285
|
}
|
286
|
|
287
|
initPhoto() {
|
288
|
if (this.communityFb.value.isUpload) {
|
289
|
this.photo = this.properties.utilsService + "/download/" + this.communityFb.get('logoUrl').value;
|
290
|
}
|
291
|
}
|
292
|
|
293
|
removePhoto() {
|
294
|
if (this.file) {
|
295
|
if (typeof document != 'undefined') {
|
296
|
(<HTMLInputElement>document.getElementById("photo")).value = "";
|
297
|
}
|
298
|
this.initPhoto();
|
299
|
this.file = null;
|
300
|
}
|
301
|
}
|
302
|
|
303
|
remove() {
|
304
|
this.communityFb.get('isUpload').setValue(false);
|
305
|
this.communityFb.get('isUpload').markAsDirty();
|
306
|
this.removePhoto();
|
307
|
this.communityFb.get('logoUrl').setValue(null);
|
308
|
if (this.community.isUpload) {
|
309
|
this.deleteCurrentPhoto = true;
|
310
|
}
|
311
|
}
|
312
|
|
313
|
public deletePhoto() {
|
314
|
if (this.community.logoUrl) {
|
315
|
this.subscriptions.push(this.utilsService.deletePhoto(this.properties.utilsService + '/delete/stakeholder/' + this.community.logoUrl).subscribe());
|
316
|
}
|
317
|
}
|
318
|
}
|