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
  DomSanitizer,
11
  SafeHtml,
12
} from '@angular/platform-browser';
13

    
14
import {
15
  CookieLawService,
16
} from './cookie-law.service';
17
import { Component, EventEmitter, HostBinding, Input, OnInit, Output } from '@angular/core';
18
import { animate, AnimationEvent, state, style, transition, trigger } from '@angular/animations';
19

    
20
// import {
21
//   closeIcon,
22
// } from './icons';
23

    
24
export type CookieLawPosition = 'top' | 'bottom';
25
export type CookieLawAnimation = 'topIn' | 'bottomIn' | 'topOut' | 'bottomOut';
26
export type CookieLawTarget = '_blank' | '_self';
27

    
28
@Component({
29
  selector: 'cookie-law',
30
  // encapsulation: ViewEncapsulation.None,
31
  animations: [
32
    trigger('state', [
33
      state('bottomOut', style({ transform: 'translateY(100%)' })),
34
      state('topOut', style({ transform: 'translateY(-100%)' })),
35
      state('*', style({ transform: 'translateY(0)' })),
36

    
37
      transition('void => topIn', [
38
        style({ transform: 'translateY(-100%)' }),
39
        animate('1000ms ease-in-out'),
40
      ]),
41

    
42
      transition('void => bottomIn', [
43
        style({ transform: 'translateY(100%)' }),
44
        animate('1000ms ease-in-out'),
45
      ]),
46

    
47
      transition('* => *', animate('1000ms ease-out')),
48
    ])
49
  ],
50
  styleUrls: [ './cookie-law.css' ],
51
  templateUrl: './cookie-law.html',
52
})
53
export class CookieLawComponent implements OnInit {
54
  public cookieLawSeen: boolean;
55

    
56
  @Input('learnMore')
57
  get learnMore() { return this._learnMore; }
58
  set learnMore(value: string) {
59
    this._learnMore = (value !== null && `${value}` !== 'false') ? value : null;
60
  }
61

    
62
  @Input('target')
63
  get target() { return this._target; }
64
  set target(value: CookieLawTarget) {
65
    this._target = (value !== null && `${value}` !== 'false' &&
66
                      (`${value}` === '_blank' || `${value}` === '_self')
67
                     ) ? value : '_blank';
68
  }
69

    
70
  @Input('position')
71
  get position() { return this._position; }
72
  set position(value: CookieLawPosition) {
73
    this._position = (value !== null && `${value}` !== 'false' &&
74
                      (`${value}` === 'top' || `${value}` === 'bottom')
75
                     ) ? value : 'bottom';
76
  }
77

    
78
  @Output('isSeen')
79
  private isSeenEvt: EventEmitter<boolean>;
80

    
81
  @HostBinding('attr.seen')
82
  public isSeen: boolean;
83

    
84
  private animation: CookieLawAnimation;
85
  private closeSvg: SafeHtml;
86
  private currentStyles: {};
87
  private _learnMore: string;
88
  private _target: CookieLawTarget;
89
  private _position: CookieLawPosition;
90

    
91
  constructor(
92
    private _service: CookieLawService,
93
    private domSanitizer: DomSanitizer,
94
  ) {
95
    this.isSeenEvt = new EventEmitter<boolean>();
96
    this.animation = 'topIn';
97
    this._position = 'bottom';
98
    this.cookieLawSeen = this._service.seen();
99
  }
100

    
101
  ngOnInit(): void {
102
    if (typeof document !== 'undefined') {
103
      this.animation = this.position === 'bottom' ? 'bottomIn' : 'topIn';
104

    
105
      this.closeSvg = `
106
            <span class="clickable uk-icon">
107
              <svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" icon="close" ratio="1">
108
                <path fill="none" stroke="#000" stroke-width="1.06" d="M16,16 L4,4"></path>
109
                <path fill="none" stroke="#000" stroke-width="1.06" d="M16,4 L4,16"></path>
110
              </svg>
111
            </span>`;
112

    
113
      if (this.cookieLawSeen) {
114
        this.isSeen = true;
115
      }
116

    
117
      this.currentStyles = {
118
        'top': this.position === 'top' ? '0' : null,
119
        'bottom': this.position === 'top' ? 'initial' : null,
120
      };
121
    }
122
  }
123

    
124
  afterDismissAnimation(evt: AnimationEvent) {
125
    if (evt.toState === 'topOut' ||
126
        evt.toState === 'bottomOut') {
127
      this.isSeen = true;
128
      this.isSeenEvt.emit(this.isSeen);
129
    }
130
  }
131

    
132
  public dismiss(evt?: MouseEvent): void {
133
    if (evt) {
134
      evt.preventDefault();
135
    }
136

    
137
    this._service.storeCookie();
138
    this.animation = this.position === 'top' ? 'topOut' : 'bottomOut';
139
  }
140
}
(1-1/6)