Project

General

Profile

1
/**
2
 * angular2-cookie-law
3
 *
4
 * Copyright 2016-2017, @andreasonny83, All rights reserved.
5
 *
6
 * @author: @andreasonny83 <andreasonny83@gmail.com>
7
 */
8

    
9
import {
10
  Component,
11
  OnInit,
12
  ViewEncapsulation,
13
  HostBinding,
14
  Input,
15
  Output,
16
  EventEmitter,
17
  animate,
18
  state,
19
  trigger,
20
  style,
21
  transition,
22
  AnimationTransitionEvent,
23
} from '@angular/core';
24

    
25
import {
26
  DomSanitizer,
27
  SafeHtml,
28
} from '@angular/platform-browser';
29

    
30
import {
31
  CookieLawService,
32
} from './cookie-law.service';
33

    
34
// import {
35
//   closeIcon,
36
// } from './icons';
37

    
38
export type CookieLawPosition = 'top' | 'bottom';
39
export type CookieLawAnimation = 'topIn' | 'bottomIn' | 'topOut' | 'bottomOut';
40
export type CookieLawTarget = '_blank' | '_self';
41

    
42
@Component({
43
  selector: 'cookie-law',
44
  // encapsulation: ViewEncapsulation.None,
45
  animations: [
46
    trigger('state', [
47
      state('bottomOut', style({ transform: 'translateY(100%)' })),
48
      state('topOut', style({ transform: 'translateY(-100%)' })),
49
      state('*', style({ transform: 'translateY(0)' })),
50

    
51
      transition('void => topIn', [
52
        style({ transform: 'translateY(-100%)' }),
53
        animate('1000ms ease-in-out'),
54
      ]),
55

    
56
      transition('void => bottomIn', [
57
        style({ transform: 'translateY(100%)' }),
58
        animate('1000ms ease-in-out'),
59
      ]),
60

    
61
      transition('* => *', animate('1000ms ease-out')),
62
    ])
63
  ],
64
  styleUrls: [ './cookie-law.css' ],
65
  templateUrl: './cookie-law.html',
66
})
67
export class CookieLawComponent implements OnInit {
68
  public cookieLawSeen: boolean;
69

    
70
  @Input('learnMore')
71
  get learnMore() { return this._learnMore; }
72
  set learnMore(value: string) {
73
    this._learnMore = (value !== null && `${value}` !== 'false') ? value : null;
74
  }
75

    
76
  @Input('target')
77
  get target() { return this._target; }
78
  set target(value: CookieLawTarget) {
79
    this._target = (value !== null && `${value}` !== 'false' &&
80
                      (`${value}` === '_blank' || `${value}` === '_self')
81
                     ) ? value : '_blank';
82
  }
83

    
84
  @Input('position')
85
  get position() { return this._position; }
86
  set position(value: CookieLawPosition) {
87
    this._position = (value !== null && `${value}` !== 'false' &&
88
                      (`${value}` === 'top' || `${value}` === 'bottom')
89
                     ) ? value : 'bottom';
90
  }
91

    
92
  @Output('isSeen')
93
  private isSeenEvt: EventEmitter<boolean>;
94

    
95
  @HostBinding('attr.seen')
96
  public isSeen: boolean;
97

    
98
  private animation: CookieLawAnimation;
99
  private closeSvg: SafeHtml;
100
  private currentStyles: {};
101
  private _learnMore: string;
102
  private _target: CookieLawTarget;
103
  private _position: CookieLawPosition;
104

    
105
  constructor(
106
    private _service: CookieLawService,
107
    private domSanitizer: DomSanitizer,
108
  ) {
109
    this.isSeenEvt = new EventEmitter<boolean>();
110
    this.animation = 'topIn';
111
    this._position = 'bottom';
112
    this.cookieLawSeen = this._service.seen();
113
  }
114

    
115
  ngOnInit(): void {
116
    if (typeof document !== 'undefined') {
117
      this.animation = this.position === 'bottom' ? 'bottomIn' : 'topIn';
118

    
119
      this.closeSvg = '<span class="clickable uk-icon"><svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="close" ratio="1"><path fill="none" stroke="#000" stroke-width="1.06" d="M16,16 L4,4"></path><path fill="none" stroke="#000" stroke-width="1.06" d="M16,4 L4,16"></path></svg></span> ' ;
120

    
121
      if (this.cookieLawSeen) {
122
        this.isSeen = true;
123
      }
124

    
125
      this.currentStyles = {
126
        'top': this.position === 'top' ? '0' : null,
127
        'bottom': this.position === 'top' ? 'initial' : null,
128
      };
129
    }
130
  }
131

    
132
  afterDismissAnimation(evt: AnimationTransitionEvent) {
133
    if (evt.toState === 'topOut' ||
134
        evt.toState === 'bottomOut') {
135
      this.isSeen = true;
136
      this.isSeenEvt.emit(this.isSeen);
137
    }
138
  }
139

    
140
  public dismiss(evt?: MouseEvent): void {
141
    if (evt) {
142
      evt.preventDefault();
143
    }
144

    
145
    this._service.storeCookie();
146
    this.animation = this.position === 'top' ? 'topOut' : 'bottomOut';
147
  }
148
}
(1-1/6)