Skip to content

Commit

Permalink
feat(CookieLawComponent): multiple components support
Browse files Browse the repository at this point in the history
multiple components support with attribute name; improved modularization; Angular2 version to
2.4.10; documentation updated.

closes #7
  • Loading branch information
andreasonny83 committed Apr 2, 2017
1 parent bf7d16d commit 640a655
Show file tree
Hide file tree
Showing 11 changed files with 486 additions and 233 deletions.
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,12 @@ to see the application running.
If set to a valid absolute or relative URL, it will render an extra 'learn more' link pointing to the link.
eg.
###### Example
```html
<cookie-law learnMore="/learn-more"></cookie-law>
```
###### Output
![output with link](http://i.imgur.com/0nvb6sP.png)
### target
Expand Down Expand Up @@ -201,6 +200,22 @@ Possible values are: `"bottom"` and `"top"`.
<cookie-law position="top" learnMore="/learn-more" target="_self"></cookie-law>
```
### name
| Type | Default value |
| --- | --- |
| string | "cookieLawSeen" |
Allows you to decide which name will be used for storing the cookie in the client's browser.
###### Example
```html
<cookie-law name="myShinyCookieLaw"></cookie-law>
```
The previous example will generate a `myShinyCookieLaw=true` as soon as the user dismiss the banner.
## Properties
| Name | Type | Description |
Expand Down Expand Up @@ -306,9 +321,9 @@ library starting from the version >=4.
Make sure to include the `BrowserAnimationsModule` in your App module like in the following example:
```ts
import { NgModule } from '@angular/core';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
Expand All @@ -319,7 +334,7 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
],
imports: [
BrowserModule,
BrowserAnimationsModule, // BrowserAnimationsModule
BrowserAnimationsModule, // Angular 4 Only
],
})
export class AppModule { }
Expand Down
12 changes: 6 additions & 6 deletions demo/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
</a>
</div>
<cookie-law position="top">
<cookie-law position="top" name="topCookieLaw">
Allo! This is my awesome cookie-law message.
<a href="https://github.com/andreasonny83/angular2-cookie-law">
Click here for more info
Expand All @@ -52,15 +52,15 @@ import {
`]
})
export class AppComponent implements OnInit {
@ViewChild('cookieLaw')
private cookieLawEl: any;
private cookieLawSeen: boolean;
cookieLawSeen: boolean;

@ViewChild('cookieLaw') private cookieLawEl: any;

ngOnInit() {
this.cookieLawSeen = this.cookieLawEl.cookieLawSeen;
this.cookieLawSeen = this.cookieLawEl.cookieLawSeen();
}

public dismiss(): void {
dismiss(): void {
this.cookieLawEl.dismiss();
}
}
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@
"@angular/platform-browser": ">=2.0.0"
},
"devDependencies": {
"@angular/common": "2.4.7",
"@angular/compiler": "2.4.7",
"@angular/core": "2.4.7",
"@angular/platform-browser": "2.4.7",
"@angular/platform-browser-dynamic": "2.4.7",
"@angular/compiler-cli": "2.4.7",
"@angular/common": "2.4.10",
"@angular/compiler": "2.4.10",
"@angular/core": "2.4.10",
"@angular/platform-browser": "2.4.10",
"@angular/platform-browser-dynamic": "2.4.10",
"@angular/compiler-cli": "2.4.10",
"@types/jasmine": "2.5.46",
"@types/node": "7.0.5",
"@types/selenium-webdriver": "2.53.39",
Expand Down
122 changes: 122 additions & 0 deletions src/cookie-law-element.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import {
Component,
OnInit,
ViewEncapsulation,
Input,
Output,
EventEmitter,
AnimationTransitionEvent,
trigger,
state,
style,
animate,
transition,
} from '@angular/core';

import {
DomSanitizer,
SafeHtml,
} from '@angular/platform-browser';

import {
closeIcon,
} from './icons';

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

@Component({
selector: 'cookie-law-el',
animations: [
trigger('state', [
state('bottomOut', style({ transform: 'translateY(100%)' })),
state('topOut', style({ transform: 'translateY(-100%)' })),
state('*', style({ transform: 'translateY(0)' })),

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

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

transition('* => *', animate('1000ms ease-out')),
])
],
styleUrls: [ './cookie-law.css' ],
templateUrl: './cookie-law.html',
encapsulation: ViewEncapsulation.None,
host: {
'[class.cookie-law]': 'true'
}
})
export class CookieLawElementComponent implements OnInit {
animation: CookieLawAnimation;
closeSvg: SafeHtml;
currentStyles: any;

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

@Input()
get target() { return this._target; }
set target(value: CookieLawTarget) {
this._target = (value !== null && `${value}` !== 'false' &&
(`${value}` === '_blank' || `${value}` === '_self')
) ? value : '_blank';
}

@Input()
get position() { return this._position; }
set position(value: CookieLawPosition) {
this._position = (value !== null && `${value}` !== 'false' &&
(`${value}` === 'top' || `${value}` === 'bottom')
) ? value : 'bottom';
}

@Output() isSeen = new EventEmitter<boolean>();

private _learnMore: string;
private _target: CookieLawTarget;
private _position: CookieLawPosition;

constructor(
private domSanitizer: DomSanitizer,
) {
this.animation = 'bottomIn';
this._position = 'bottom';
}

ngOnInit(): void {
this.animation = this.position === 'bottom' ? 'bottomIn' : 'topIn';

this.closeSvg = this.domSanitizer.bypassSecurityTrustHtml(closeIcon);

this.currentStyles = {
'top': this.position === 'top' ? '0' : null,
'bottom': this.position === 'top' ? 'initial' : null,
};
}

afterDismissAnimation(evt: AnimationTransitionEvent): void {
if (evt.toState === 'topOut' ||
evt.toState === 'bottomOut') {
this.isSeen.emit(true);
}
}

dismiss(evt?: MouseEvent): void {
if (evt) {
evt.preventDefault();
}

this.animation = this.position === 'top' ? 'topOut' : 'bottomOut';
}
}
Loading

0 comments on commit 640a655

Please sign in to comment.