Project

General

Profile

1
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
2
import {ActivatedRoute, Router} from '@angular/router';
3

    
4
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
5

    
6
import {Session} from '../../openaireLibrary/login/utils/helper.class';
7
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
8
import {DomSanitizer, Title} from '@angular/platform-browser';
9
import {CustomizationOptions} from '../../openaireLibrary/connect/community/CustomizationOptions';
10
import {StringUtils} from '../../openaireLibrary/utils/string-utils.class';
11
import {LayoutService} from '../../openaireLibrary/services/layout.service';
12
import {AlertModal} from '../../openaireLibrary/utils/modal/alert';
13
import {properties} from '../../../environments/environment';
14

    
15
@Component({
16
  selector: 'customization',
17
  templateUrl: './customization.component.html',
18
})
19

    
20
export class CustomizationComponent implements OnInit {
21

    
22
  menuSelected = 'identity';
23
  color = 'white';
24
  publishedCustomizationOptions: CustomizationOptions = null;
25
  draftCustomizationOptions: CustomizationOptions = null;
26
  appliedCustomizationOptions: CustomizationOptions = null;
27
  previewUrl = null;
28
  previewCustomization = null;
29
  buttonDarkBackgroundPreview;
30
  buttonLightBackgroundPreview;
31
  linkDarkBackgroundPreview;
32
  linkLightBackgroundPreview;
33
  hoveredText;
34
  public showLoading = true;
35
  public errorMessage = '';
36
  public successfulSaveMessage = '';
37

    
38

    
39
  public communityId = null;
40

    
41
  public properties: EnvProperties = null;
42

    
43
  public enabled = true;
44
  @ViewChild('backAlert') backAlert: AlertModal;
45
  @ViewChild('exitAlert') exitAlert: AlertModal;
46

    
47
  constructor(private element: ElementRef,
48
              private route: ActivatedRoute,
49
              private _router: Router,
50
              private title: Title,
51
              private sanitizer: DomSanitizer,
52
              private layoutService: LayoutService) {
53
  }
54

    
55

    
56
  ngOnInit() {
57
    this.properties = properties;
58
    if (!Session.isLoggedIn()) {
59
      this._router.navigate(['/user-info'], {
60
        queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
61
      });
62
    } else {
63
      this.route.params.subscribe((params) => {
64
        this.communityId = params['community'];
65
        this.title.setTitle('Administration Dashboard | Customization');
66
        this.showLoading = true;
67
        this.errorMessage = '';
68
        this.successfulSaveMessage = '';
69
        this.layoutService.getLayout(this.properties, this.communityId).subscribe(layout => {
70
          this.publishedCustomizationOptions = (layout?layout:new CustomizationOptions());
71
          this.initializeCustomizationOptions(true);
72
        }, error => {
73
          this.publishedCustomizationOptions = new CustomizationOptions();
74
          this.initializeCustomizationOptions(true);
75
          this.errorMessage = "An error occured fetching customizations options"
76
        });
77
      });
78
    }
79

    
80

    
81
  }
82
  hasChanges(object1,object2):boolean{
83
    return JSON.stringify(object1) != JSON.stringify(object2);
84
  }
85
  saveLayout() {
86
    if (!Session.isLoggedIn()) {
87
      this._router.navigate(['/user-info'], {
88
        queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
89
      });
90
    }
91
    this.layoutService.saveLayout(this.properties, this.communityId, this.draftCustomizationOptions).subscribe(layout => {
92
      this.publishedCustomizationOptions = layout;
93
      this.initializeCustomizationOptions(JSON.stringify(this.publishedCustomizationOptions) != this.previewCustomization);
94
      this.successfulSaveMessage = "Customization Options saved!"
95
    }, error => {
96
      this.errorMessage = "An error occured on save"
97
    });
98
  }
99

    
100
  initializeCustomizationOptions(updatePreviewUrl) {
101
    this.draftCustomizationOptions = this.copyObject(this.publishedCustomizationOptions);
102
    this.appliedCustomizationOptions = this.copyObject(this.publishedCustomizationOptions);
103

    
104
    if(updatePreviewUrl){
105
      this.previewUrl = this.getCommunityUrlSatinized(JSON.stringify(this.appliedCustomizationOptions));
106
    }
107

    
108

    
109
    this.buttonDarkBackgroundPreview = this.changeStyle(this.draftCustomizationOptions.buttons.darkBackground);
110
    this.buttonLightBackgroundPreview = this.changeStyle(this.draftCustomizationOptions.buttons.lightBackground);
111
    this.linkDarkBackgroundPreview = this.changeFontsStyle(this.draftCustomizationOptions.links.darkBackground, this.draftCustomizationOptions.links.darkBackground.color);
112
    this.linkLightBackgroundPreview = this.changeStyle(this.draftCustomizationOptions.links.lightBackground, this.draftCustomizationOptions.links.lightBackground.color);
113
  }
114

    
115
  applyLayout() {
116
    this.appliedCustomizationOptions = this.copyObject(this.draftCustomizationOptions);
117
    if(JSON.stringify(this.appliedCustomizationOptions) != this.previewCustomization){
118
      this.previewUrl = this.getCommunityUrlSatinized(JSON.stringify(this.appliedCustomizationOptions));
119
    }
120
  }
121

    
122
  resetLayout() {
123

    
124
    if (this.menuSelected == 'backgrounds') {
125
      this.resetBackgrounds();
126
    } else if (this.menuSelected == 'fonts') {
127
      this.resetFonts();
128
    } else if (this.menuSelected == 'buttons') {
129
      this.resetButtons();
130
    } else if (this.menuSelected == 'identity') {
131
      this.resetIdentity();
132
    } else {
133
      this.draftCustomizationOptions = this.copyObject(this.publishedCustomizationOptions);
134

    
135
      this.appliedCustomizationOptions = this.copyObject(this.publishedCustomizationOptions);
136
    }
137
    // console.log(JSON.stringify(this.appliedCustomizationOptions))
138
    // console.log(this.previewCustomization)
139
    this.initializeCustomizationOptions(JSON.stringify(this.appliedCustomizationOptions) != this.previewCustomization);
140

    
141
  }
142

    
143
  resetBackgrounds() {
144
    this.draftCustomizationOptions.panel.onDarkBackground = this.publishedCustomizationOptions.panel.onDarkBackground;
145
    this.draftCustomizationOptions.panel.background.color = this.publishedCustomizationOptions.panel.background.color;
146

    
147
    this.appliedCustomizationOptions.panel.onDarkBackground = this.publishedCustomizationOptions.panel.onDarkBackground;
148
    this.appliedCustomizationOptions.panel.background.color = this.publishedCustomizationOptions.panel.background.color;
149
  }
150

    
151
  resetFonts() {
152
    this.resetFontsBig();
153
    this.resetFontsSmall();
154
    this.resetFontsLinksDark();
155
    this.resetFontsLinksLight();
156

    
157
  }
158

    
159
  resetFontsBig() {
160
    this.draftCustomizationOptions.panel.title = this.copyObject(this.publishedCustomizationOptions.panel.title);
161

    
162
    this.appliedCustomizationOptions.panel.title = this.copyObject(this.publishedCustomizationOptions.panel.title);
163
  }
164

    
165
  resetFontsSmall() {
166
    this.draftCustomizationOptions.panel.fonts = this.copyObject(this.publishedCustomizationOptions.panel.fonts);
167

    
168
    this.appliedCustomizationOptions.panel.fonts = this.copyObject(this.publishedCustomizationOptions.panel.fonts);
169

    
170

    
171
  }
172

    
173
  resetFontsLinksDark() {
174
    this.draftCustomizationOptions.links.darkBackground = this.copyObject(this.publishedCustomizationOptions.links.darkBackground);
175

    
176
    this.appliedCustomizationOptions.links.darkBackground = this.copyObject(this.publishedCustomizationOptions.links.darkBackground);
177

    
178
    this.linkDarkBackgroundPreview=this.changeFontsStyle(this.publishedCustomizationOptions.links.darkBackground, this.publishedCustomizationOptions.links.darkBackground.color);
179
  }
180

    
181
  resetFontsLinksLight() {
182
    this.draftCustomizationOptions.panel.title = this.copyObject(this.publishedCustomizationOptions.panel.title);
183
    this.draftCustomizationOptions.links.lightBackground = this.copyObject(this.publishedCustomizationOptions.links.lightBackground);
184

    
185
    this.appliedCustomizationOptions.panel.title = this.copyObject(this.publishedCustomizationOptions.panel.title);
186
    this.appliedCustomizationOptions.links.lightBackground = this.copyObject(this.publishedCustomizationOptions.links.lightBackground);
187

    
188
    this.linkLightBackgroundPreview=this.changeFontsStyle(this.publishedCustomizationOptions.links.lightBackground, this.publishedCustomizationOptions.links.lightBackground.color);
189
  }
190

    
191

    
192
  resetButtons() {
193
    this.resetButtonsDark();
194
    this.resetButtonsLight();
195
  }
196

    
197
  resetButtonsDark() {
198
    this.draftCustomizationOptions.buttons.darkBackground = this.copyObject(this.publishedCustomizationOptions.buttons.darkBackground);
199

    
200
    this.appliedCustomizationOptions.buttons.darkBackground = this.copyObject(this.publishedCustomizationOptions.buttons.darkBackground);
201

    
202
    this.buttonDarkBackgroundPreview=this.changeStyle(this.publishedCustomizationOptions.buttons.darkBackground);
203
  }
204

    
205
  resetButtonsLight() {
206
    this.draftCustomizationOptions.buttons.lightBackground = this.copyObject(this.publishedCustomizationOptions.buttons.lightBackground);
207

    
208
    this.appliedCustomizationOptions.buttons.lightBackground = this.copyObject(this.publishedCustomizationOptions.buttons.lightBackground);
209

    
210
    this.buttonLightBackgroundPreview=this.changeStyle(this.publishedCustomizationOptions.buttons.lightBackground);
211
  }
212

    
213
  resetIdentity() {
214
    this.draftCustomizationOptions.mainColor = this.publishedCustomizationOptions.mainColor;
215
    this.draftCustomizationOptions.secondaryColor = this.publishedCustomizationOptions.secondaryColor;
216

    
217
    this.appliedCustomizationOptions.mainColor = this.publishedCustomizationOptions.mainColor;
218
    this.appliedCustomizationOptions.secondaryColor = this.publishedCustomizationOptions.secondaryColor;
219
  }
220

    
221
  changeStyle(colorOptions, borderOptions = null) {
222
    let style = '';
223
    if (!borderOptions) {
224
      borderOptions = colorOptions;
225
    }
226
    if (colorOptions.color) {
227
      style = style.concat('; color:' + colorOptions.color);
228
    }
229
    if (colorOptions.backgroundColor) {
230
      style = style.concat( '; background-color:' + colorOptions.backgroundColor);
231
    }
232
    if (colorOptions.borderColor) {
233
      style = style.concat('; border-color:' + colorOptions.borderColor);
234
    }
235
    if (borderOptions.borderStyle) {
236
      style = style.concat('; border-style:' + borderOptions.borderStyle);
237
    }
238
    if (borderOptions.borderWidth) {
239
      style = style.concat( '; border-width:' + borderOptions.borderWidth + 'px');
240
    }
241
    if (borderOptions.borderRadius) {
242
      style = style.concat('; border-radius:' + borderOptions.borderRadius + 'px');
243
    }
244

    
245
    return this.sanitizer.bypassSecurityTrustStyle(style);
246
  }
247

    
248

    
249
  changeFontsStyle(options, color = null) {
250
    let style = '';
251

    
252

    
253
    if (options.family) {
254
      style = style.concat('; font-family:' + options.family);
255
    }
256

    
257
    if (options.size) {
258
      style = style.concat('; font-size:' + options.size + 'px');
259
    }
260

    
261
    if (options.weight) {
262
      style = style.concat('; font-weight:' + options.weight);
263
    }
264

    
265
    if (color) {
266
      style = style.concat('; color:' + color);
267
    }
268

    
269
    return this.sanitizer.bypassSecurityTrustStyle(style);
270
  }
271

    
272
  getCommunityUrlSatinized(layout: string) {
273
    return this.sanitizer.bypassSecurityTrustResourceUrl(this.getCommunityUrlNewLayout(layout));
274
  }
275
  getCommunityUrl() {
276
    return 'https://'+ (this.properties.environment == 'production'?'':'beta.')+this.communityId+'.openaire.eu';
277
  }
278

    
279
  getCommunityUrlNewLayout(layout: string) {
280
    this.previewCustomization = layout;
281
      return this.getCommunityUrl()+'/?' + 'layout=' + StringUtils.URIEncode(layout);
282
  }
283

    
284
  copyObject(obj) {
285
    return JSON.parse(JSON.stringify(obj));
286
  }
287

    
288
  back() {
289
    if (JSON.stringify(this.draftCustomizationOptions) == JSON.stringify(this.publishedCustomizationOptions) ||
290
      JSON.stringify(this.draftCustomizationOptions) == JSON.stringify(this.appliedCustomizationOptions)) {
291
      this.menuSelected = 'main';
292
    } else {
293
      this.backAlert.okButtonText = 'Yes';
294
      this.backAlert.cancelButtonText = 'No';
295
      this.backAlert.alertTitle = '';
296
      this.backAlert.okButtonLeft = true;
297
      this.backAlert.open();
298
    }
299

    
300
  }
301

    
302
  resetToAppliedOptions() {
303
    this.draftCustomizationOptions = this.copyObject(this.appliedCustomizationOptions);
304
    this.menuSelected = 'main';
305
  }
306

    
307
  exit() {
308
    if (JSON.stringify(this.appliedCustomizationOptions) == JSON.stringify(this.publishedCustomizationOptions)) {
309
      this.exitCustomization();
310
    } else {
311
      this.exitAlert.okButtonText = 'Yes';
312
      this.exitAlert.cancelButtonText = 'No';
313
      this.exitAlert.alertTitle = '';
314
      this.exitAlert.okButtonLeft = true;
315
      this.exitAlert.open();
316
    }
317

    
318
  }
319

    
320
  exitCustomization() {
321
    this._router.navigate(['/dashboard'], {
322
      queryParams: {'communityId': this.communityId}
323
    });
324
  }
325
  downloadCustomization(){
326
    this.errorMessage = "";
327
    let options= JSON.parse(JSON.stringify(this.publishedCustomizationOptions));
328
    delete options['_id'];
329
    console.info("Here!" +JSON.stringify(options));
330
    if(typeof document !== 'undefined') {
331
      let dataStr = JSON.stringify(options);
332
      let dataUri = 'data:application/json;charset=utf-8,' + encodeURIComponent(dataStr);
333

    
334
      let exportFileDefaultName = 'layoutOptions.json';
335

    
336
      let linkElement = document.createElement('a');
337
      linkElement.setAttribute('href', dataUri);
338
      linkElement.setAttribute('download', exportFileDefaultName);
339
      linkElement.click();
340
    }
341
  }
342
  fileChangeEvent(fileInput: any) {
343
    this.errorMessage = "";
344
     let filesToUpload = <Array<File>>fileInput.target.files;
345

    
346

    
347
      if (filesToUpload.length == 0) {
348
        this.errorMessage = "There is no selected file to upload.";
349
        return;
350
      } else {
351
        if (filesToUpload[0].name.indexOf(".json") == -1 ||
352
          (filesToUpload[0].type != "application/json" )) {
353
          this.errorMessage = "No valid file type. The required type is json "+filesToUpload[0].type;
354
          return;
355
        }
356
      }
357

    
358
      this.makeFileRequest(this.properties.utilsService + '/upload?type=json', [], filesToUpload).then((result) => {
359
       let layout:CustomizationOptions = JSON.parse(result.toString());
360
        if( layout && layout.panel && layout.links && layout.buttons){
361
          this.draftCustomizationOptions = layout;
362
          //put the same id to not have any difference
363
          if(this.publishedCustomizationOptions['_id']){
364
            this.draftCustomizationOptions['_id'] = this.publishedCustomizationOptions['_id'];
365
          }
366
        }else{
367
          this.errorMessage = "No valid file";
368
        }
369

    
370
      }, (error) => {
371
        this.errorMessage = "No valid file";
372
      });
373
    }
374

    
375

    
376
  makeFileRequest(url: string, params: Array<string>, files: Array<File>) {
377
    return new Promise((resolve, reject) => {
378
      const formData: any = new FormData();
379
      const xhr = new XMLHttpRequest();
380
      for (let i = 0; i < files.length; i++) {
381
        formData.append("uploads[]", files[i], files[i].name);
382
      }
383
      xhr.onreadystatechange = function () {
384
        if (xhr.readyState == 4) {
385
          if (xhr.status == 200) {
386
            resolve(xhr.response);
387
          } else {
388
            reject(xhr.response);
389
          }
390
        }
391
      }
392
      xhr.open("POST", url, true);
393
      xhr.send(formData);
394
    });
395
  }
396

    
397

    
398

    
399

    
400
}
(6-6/7)