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, Layout} 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
declare var UIkit;
15

    
16
@Component({
17
  selector: 'customization',
18
  templateUrl: './customization.component.html',
19
  styles:[`
20
  .refresh-indicator {
21
    background-color: rgba(0, 0, 0, 0.50);
22
    border-radius: 4px;
23
    position: absolute;
24
    color: white;
25
  }
26
  iframe, .refresh-indicator{
27
      height:900px;
28
  }
29
  `]
30
})
31

    
32
export class CustomizationComponent implements OnInit {
33

    
34
  menuSelected = 'identity';
35
  color = 'white';
36
  defaultCustomizationOptions:CustomizationOptions = new CustomizationOptions();
37
  publishedLayout: Layout = null;
38
  publishedCustomizationOptions: CustomizationOptions = null;
39
  draftCustomizationOptions: CustomizationOptions = null;
40
  appliedCustomizationOptions: CustomizationOptions = null;
41
  previewUrl = null;
42
  previewCustomization = null;
43

    
44

    
45
  public showLoading = true;
46

    
47

    
48
  public communityId = null;
49

    
50
  public properties: EnvProperties = null;
51

    
52
  public enabled = true;
53
/*  @ViewChild('backAlert') backAlert: AlertModal;
54
  @ViewChild('exitAlert') exitAlert: AlertModal;*/
55

    
56
  constructor(private element: ElementRef,
57
              private route: ActivatedRoute,
58
              private _router: Router,
59
              private title: Title,
60
              private sanitizer: DomSanitizer,
61
              private layoutService: LayoutService) {
62
  }
63

    
64

    
65
  ngOnInit() {
66
    this.properties = properties;
67
    if (!Session.isLoggedIn()) {
68
      this._router.navigate(['/user-info'], {
69
        queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
70
      });
71
    } else {
72
      this.route.params.subscribe((params) => {
73
        this.communityId = params['community'];
74
        if(this.communityId == "covid-19"){
75
          this.defaultCustomizationOptions= new CustomizationOptions(CustomizationOptions.getIdentity(this.communityId).mainColor,CustomizationOptions.getIdentity(this.communityId).secondaryColor);
76
        }
77
        this.title.setTitle('Administration Dashboard | Customization');
78
        this.showLoading = true;
79
         this.layoutService.getLayout(this.properties, this.communityId).subscribe(layout => {
80
         this.publishedLayout = (layout?layout:new Layout(this.communityId,this.defaultCustomizationOptions));
81
         this.publishedCustomizationOptions = (layout?CustomizationOptions.checkForObsoleteVersion(layout.layoutOptions,this.communityId):Object.assign({},this.defaultCustomizationOptions));
82
         this.initializeCustomizationOptions(true);
83
        }, error => {
84
          this.publishedCustomizationOptions = new CustomizationOptions(CustomizationOptions.getIdentity(this.communityId).mainColor,CustomizationOptions.getIdentity(this.communityId).secondaryColor);
85
          this.initializeCustomizationOptions(true);
86
           UIkit.notification("An error occured fetching customizations options", {
87
             status: 'danger',
88
             timeout: 6000,
89
             pos: 'bottom-right'
90
           });
91
        });
92
      });
93
    }
94

    
95

    
96
  }
97
  hasChanges(object1,object2):boolean{
98
    return JSON.stringify(object1) != JSON.stringify(object2);
99
  }
100
  saveLayout() {
101
    if (!Session.isLoggedIn()) {
102
      this._router.navigate(['/user-info'], {
103
        queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
104
      });
105
    }
106
    this.publishedLayout.layoutOptions = this.draftCustomizationOptions;
107
    this.layoutService.saveLayout(this.properties, this.communityId, this.publishedLayout).subscribe(layout => {
108
      this.publishedLayout = layout;
109
      this.publishedCustomizationOptions = layout.layoutOptions;
110
      this.initializeCustomizationOptions(JSON.stringify(this.publishedCustomizationOptions) != this.previewCustomization);
111
      UIkit.notification("Customizations was succesfully saved!", {
112
        status: 'success',
113
        timeout: 6000,
114
        pos: 'bottom-right'
115
      });
116
    }, error => {
117

    
118
      UIkit.notification("An error occured on save", {
119
        status: 'danger',
120
        timeout: 6000,
121
        pos: 'bottom-right'
122
      });
123
    });
124
  }
125

    
126
  initializeCustomizationOptions(updatePreviewUrl) {
127
    this.draftCustomizationOptions = this.copyObject(this.publishedCustomizationOptions);
128
    this.appliedCustomizationOptions = this.copyObject(this.publishedCustomizationOptions);
129
    if(updatePreviewUrl){
130
      this.previewUrl = this.getCommunityUrlSatinized(JSON.stringify(this.appliedCustomizationOptions));
131
    }
132

    
133
  }
134

    
135
  applyLayout() {
136
    this.appliedCustomizationOptions = this.copyObject(this.draftCustomizationOptions);
137
    if(JSON.stringify(this.appliedCustomizationOptions) != this.previewCustomization){
138
      this.previewUrl = this.getCommunityUrlSatinized(JSON.stringify(this.appliedCustomizationOptions));
139
    }
140
  }
141

    
142
  resetLayout() {
143
    this.initializeCustomizationOptions(JSON.stringify(this.appliedCustomizationOptions) != this.previewCustomization);
144
  }
145
  resetBackgroundsTo(backgrounds) {
146
    this.draftCustomizationOptions.backgrounds.dark.color = backgrounds.dark.color;
147
    this.draftCustomizationOptions.backgrounds.light.color = backgrounds.light.color;
148
    this.draftCustomizationOptions.backgrounds.form.color = backgrounds.form.color;
149

    
150
  }
151

    
152
  resetBackgroundsAndButtonsTo(c:CustomizationOptions){
153
    this.resetBackgroundsTo(c.backgrounds);
154
    this.resetButtonsTo(c.buttons);
155
   }
156
  resetBackgroundsAndButtonsToPublished(){
157
    this.resetBackgroundsAndButtonsTo(this.publishedCustomizationOptions);
158
  }
159
  resetBackgroundsAndButtonsToDefault(){
160
    this.resetBackgroundsAndButtonsTo(this.publishedCustomizationOptions);
161
    this.updateBackgroundsAndButtonsBasedOnIdentity();
162
  }
163

    
164
  resetButtonsTo(buttonsToRevert) {
165
    this.draftCustomizationOptions.buttons= this.copyObject(buttonsToRevert);
166
  }
167

    
168
  resetIdentityTo( original:CustomizationOptions) {
169
    this.draftCustomizationOptions.identity.mainColor = this.copyObject(original.identity.mainColor);
170
    this.draftCustomizationOptions.identity.secondaryColor = this.copyObject(original.identity.secondaryColor);
171
  }
172
  resetIdentityToPublished() {
173
    this.resetIdentityTo( this.publishedCustomizationOptions);
174
    this.updateBackgroundsAndButtonsBasedOnIdentity()
175
  }
176
  resetIdentityToDefault() {
177
    this.resetIdentityTo( this.defaultCustomizationOptions);
178
    this.draftCustomizationOptions.identityIsCustom = false;
179
    this.updateBackgroundsAndButtonsBasedOnIdentity()
180

    
181
  }
182
  updateBackgroundsAndButtonsBasedOnIdentity(){
183
    if(!this.draftCustomizationOptions.backgroundsAndButtonsIsCustom){
184
      let tmp =   new CustomizationOptions(this.draftCustomizationOptions.identity.mainColor, this.draftCustomizationOptions.identity.secondaryColor);
185
      this.resetBackgroundsAndButtonsTo(tmp);
186
    }
187
  }
188
  getCommunityUrlSatinized(layout: string) {
189
    return this.sanitizer.bypassSecurityTrustResourceUrl(this.getCommunityUrlNewLayout(layout));
190
  }
191
  getCommunityUrl() {
192
    // return 'http://spitoo.di.uoa.gr:4000';
193
    return 'https://'+ (this.properties.environment == 'production'?'':'beta.')+this.communityId+'.openaire.eu';
194
  }
195

    
196
  getCommunityUrlNewLayout(layout: string) {
197
    this.previewCustomization = layout;
198
      return this.getCommunityUrl()+'/preview/?' + 'layout=' + StringUtils.URIEncode(layout);
199
  }
200

    
201
  copyObject(obj) {
202
    return JSON.parse(JSON.stringify(obj));
203
  }
204

    
205
  /*back() {
206
    if (JSON.stringify(this.draftCustomizationOptions) == JSON.stringify(this.publishedCustomizationOptions) ||
207
      JSON.stringify(this.draftCustomizationOptions) == JSON.stringify(this.appliedCustomizationOptions)) {
208
      this.menuSelected = 'main';
209
    } else {
210
      this.backAlert.okButtonText = 'Yes';
211
      this.backAlert.cancelButtonText = 'No';
212
      this.backAlert.alertTitle = '';
213
      this.backAlert.okButtonLeft = true;
214
      this.backAlert.open();
215
    }
216

    
217
  }
218

    
219
  resetToAppliedOptions() {
220
    this.draftCustomizationOptions = this.copyObject(this.appliedCustomizationOptions);
221
    this.menuSelected = 'main';
222
  }
223

    
224
  exit() {
225
    if (JSON.stringify(this.appliedCustomizationOptions) == JSON.stringify(this.publishedCustomizationOptions)) {
226
      this.exitCustomization();
227
    } else {
228
      this.exitAlert.okButtonText = 'Yes';
229
      this.exitAlert.cancelButtonText = 'No';
230
      this.exitAlert.alertTitle = '';
231
      this.exitAlert.okButtonLeft = true;
232
      this.exitAlert.open();
233
    }
234

    
235
  }
236

    
237
  exitCustomization() {
238
    this._router.navigate(['/dashboard'], {
239
      queryParams: {'communityId': this.communityId}
240
    });
241
  }
242
  downloadCustomization(){
243
     let options= JSON.parse(JSON.stringify(this.publishedCustomizationOptions));
244
    delete options['_id'];
245
    console.info("Here!" +JSON.stringify(options));
246
    if(typeof document !== 'undefined') {
247
      let dataStr = JSON.stringify(options);
248
      let dataUri = 'data:application/json;charset=utf-8,' + encodeURIComponent(dataStr);
249

    
250
      let exportFileDefaultName = 'layoutOptions.json';
251

    
252
      let linkElement = document.createElement('a');
253
      linkElement.setAttribute('href', dataUri);
254
      linkElement.setAttribute('download', exportFileDefaultName);
255
      linkElement.click();
256
    }
257
  }
258
  fileChangeEvent(fileInput: any) {
259
      let filesToUpload = <Array<File>>fileInput.target.files;
260

    
261

    
262
      if (filesToUpload.length == 0) {
263
        UIkit.notification("There is no selected file to upload.", {
264
          status: 'danger',
265
          timeout: 6000,
266
          pos: 'bottom-right'
267
        });
268
        return;
269
      } else {
270
        if (filesToUpload[0].name.indexOf(".json") == -1 ||
271
          (filesToUpload[0].type != "application/json" )) {
272
           UIkit.notification("No valid file type. The required type is json "+filesToUpload[0].type, {
273
            status: 'danger',
274
            timeout: 6000,
275
            pos: 'bottom-right'
276
          });
277
          return;
278
        }
279
      }
280

    
281
      this.makeFileRequest(this.properties.utilsService + '/upload?type=json', [], filesToUpload).then((result) => {
282
       let layout:CustomizationOptions = JSON.parse(result.toString());
283
        if( layout && layout.backgrounds && layout.buttons){
284
          this.draftCustomizationOptions = layout;
285
          //put the same id to not have any difference
286
          if(this.publishedCustomizationOptions['_id']){
287
            this.draftCustomizationOptions['_id'] = this.publishedCustomizationOptions['_id'];
288
          }
289
        }else{
290
          UIkit.notification("No valid file", {
291
            status: 'danger',
292
            timeout: 6000,
293
            pos: 'bottom-right'
294
          });
295
        }
296

    
297
      }, (error) => {
298
        UIkit.notification("No valid file", {
299
          status: 'danger',
300
          timeout: 6000,
301
          pos: 'bottom-right'
302
        });
303
      });
304
    }
305

    
306

    
307
  makeFileRequest(url: string, params: Array<string>, files: Array<File>) {
308
    return new Promise((resolve, reject) => {
309
      const formData: any = new FormData();
310
      const xhr = new XMLHttpRequest();
311
      for (let i = 0; i < files.length; i++) {
312
        formData.append("uploads[]", files[i], files[i].name);
313
      }
314
      xhr.onreadystatechange = function () {
315
        if (xhr.readyState == 4) {
316
          if (xhr.status == 200) {
317
            resolve(xhr.response);
318
          } else {
319
            reject(xhr.response);
320
          }
321
        }
322
      }
323
      xhr.open("POST", url, true);
324
      xhr.send(formData);
325
    });
326
  }
327
*/
328
public isForLightBackground(color:string){
329
  return CustomizationOptions.isForLightBackground(color);
330
}
331

    
332

    
333
}
(6-6/11)