Skip to content

Commit

Permalink
feat(aio): add survey link
Browse files Browse the repository at this point in the history
Closes #21094
  • Loading branch information
petebacondarwin committed Jan 8, 2018
1 parent db55e86 commit e70ddaa
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 10 deletions.
32 changes: 23 additions & 9 deletions aio/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,30 @@
<mat-progress-bar mode="indeterminate" color="warn"></mat-progress-bar>
</div>


<mat-toolbar color="primary" class="app-toolbar" [class.transitioning]="isTransitioning">
<button mat-button class="hamburger" (click)="sidenav.toggle()" title="Docs menu">
<mat-icon svgIcon="menu"></mat-icon>
</button>
<a class="nav-link home" href="/" [ngSwitch]="isSideBySide">
<img *ngSwitchCase="true" src="assets/images/logos/angular/logo-nav@2x.png" width="150" height="40" title="Home" alt="Home">
<img *ngSwitchDefault src="assets/images/logos/angular/shield-large.svg" width="37" height="40" title="Home" alt="Home">
</a>
<aio-top-menu *ngIf="isSideBySide" [nodes]="topMenuNodes"></aio-top-menu>
<aio-search-box class="search-container" #searchBox (onSearch)="doSearch($event)" (onFocus)="doSearch($event)"></aio-search-box>
<mat-toolbar-row class="notification-container">
<aio-notification
icon="insert_comment"
iconLabel="Survey"
buttonText="Go to survey"
actionUrl="https://bit.ly/angular-survey-2018"
notificationId="survey-january-2018"
expirationDate="2018-01-20">
Help Angular by taking a <strong>1 minute survey</strong>!
</aio-notification>
</mat-toolbar-row>
<mat-toolbar-row>
<button mat-button class="hamburger" (click)="sidenav.toggle()" title="Docs menu">
<mat-icon svgIcon="menu"></mat-icon>
</button>
<a class="nav-link home" href="/" [ngSwitch]="isSideBySide">
<img *ngSwitchCase="true" src="assets/images/logos/angular/logo-nav@2x.png" width="150" height="40" title="Home" alt="Home">
<img *ngSwitchDefault src="assets/images/logos/angular/shield-large.svg" width="37" height="40" title="Home" alt="Home">
</a>
<aio-top-menu *ngIf="isSideBySide" [nodes]="topMenuNodes"></aio-top-menu>
<aio-search-box class="search-container" #searchBox (onSearch)="doSearch($event)" (onFocus)="doSearch($event)"></aio-search-box>
</mat-toolbar-row>
</mat-toolbar>
<aio-search-results #searchResultsView *ngIf="showSearchResults" [searchResults]="searchResults | async" (resultSelected)="hideSearchResults()"></aio-search-results>

Expand Down
6 changes: 6 additions & 0 deletions aio/src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { SearchService } from 'app/search/search.service';
import { SelectComponent } from 'app/shared/select/select.component';
import { TocComponent } from 'app/layout/toc/toc.component';
import { TocItem, TocService } from 'app/shared/toc.service';
import { WindowToken } from 'app/shared/window';

const sideBySideBreakPoint = 992;
const hideToCBreakPoint = 800;
Expand Down Expand Up @@ -1134,10 +1135,15 @@ function createTestingModule(initialUrl: string, mode: string = 'stable') {
deployment.mode = mode;
return deployment;
}},
{ provide: WindowToken, useClass: MockWindow },
]
});
}

class MockWindow {
localStorage = jasmine.createSpyObj('localStorage', ['getItem', 'setItem']);
}

class TestEmbedComponentsService {
embedInto = jasmine.createSpy('embedInto').and.returnValue(of([]));
}
Expand Down
28 changes: 28 additions & 0 deletions aio/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ import { NavItemComponent } from 'app/layout/nav-item/nav-item.component';
import { ScrollService } from 'app/shared/scroll.service';
import { ScrollSpyService } from 'app/shared/scroll-spy.service';
import { SearchBoxComponent } from 'app/search/search-box/search-box.component';
import { NotificationComponent } from 'app/layout/notification/notification.component';
import { TocComponent } from 'app/layout/toc/toc.component';
import { TocService } from 'app/shared/toc.service';
import { CurrentDateToken, currentDateProvider } from 'app/shared/current-date';
import { WindowToken, windowProvider } from 'app/shared/window';

import { EmbedComponentsModule } from 'app/embed-components/embed-components.module';
Expand Down Expand Up @@ -65,6 +67,30 @@ export const svgIconProviders = [
'viewBox="0 0 24 24"><path d="M3 18h18v-2H3v2zm0-5h18v-2H3v2zm0-7v2h18V6H3z"/></svg>'
},
multi: true
},
{
provide: SVG_ICONS,
useValue: {
name: 'insert_comment',
svgSource:
'<svg fill="#FFFFFF" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">' +
'<path d="M20 2H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h14l4 4V4c0-1.1-.9-2-2-2zm-2 12H6v-2h12v2zm0-3H6V9h12v2zm0-3H6V6h12v2z"/>' +
'<path d="M0 0h24v24H0z" fill="none"/>' +
'</svg>'
},
multi: true
},
{
provide: SVG_ICONS,
useValue: {
name: 'close',
svgSource:
'<svg fill="#ffffff" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">' +
'<path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/>' +
'<path d="M0 0h24v24H0z" fill="none"/>' +
'</svg>'
},
multi: true
}
];

Expand All @@ -91,6 +117,7 @@ export const svgIconProviders = [
NavMenuComponent,
NavItemComponent,
SearchBoxComponent,
NotificationComponent,
TocComponent,
TopMenuComponent,
],
Expand All @@ -109,6 +136,7 @@ export const svgIconProviders = [
SearchService,
svgIconProviders,
TocService,
{ provide: CurrentDateToken, useFactory: currentDateProvider },
{ provide: WindowToken, useFactory: windowProvider },

{
Expand Down
11 changes: 11 additions & 0 deletions aio/src/app/layout/notification/notification.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<span class="space"></span>

<a href="{{actionUrl}}" class="content">
<mat-icon class="icon" [svgIcon]="icon" [attr.aria-label]="iconLabel"></mat-icon>
<span class="message"><ng-content></ng-content></span>
<span class="action-button">{{buttonText}}</span>
</a>

<button mat-icon-button class="close-button" aria-label="Close">
<mat-icon svgIcon="close" aria-label="Dismiss notification"></mat-icon>
</button>
114 changes: 114 additions & 0 deletions aio/src/app/layout/notification/notification.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { Component, NO_ERRORS_SCHEMA } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { CurrentDateToken } from 'app/shared/current-date';
import { NotificationComponent } from './notification.component';
import { WindowToken } from 'app/shared/window';

describe('NotificationComponent', () => {
let element: HTMLElement;
let component: NotificationComponent;
let fixture: ComponentFixture<TestComponent>;

function configTestingModule(now = new Date('2018-01-20')) {
TestBed.configureTestingModule({
declarations: [TestComponent, NotificationComponent],
providers: [
{ provide: WindowToken, useClass: MockWindow },
{ provide: CurrentDateToken, useValue: now },
],
imports: [NoopAnimationsModule],
schemas: [NO_ERRORS_SCHEMA]
});
}

function createComponent() {
fixture = TestBed.createComponent(TestComponent);
const debugElement = fixture.debugElement.query(By.directive(NotificationComponent));
element = debugElement.nativeElement;
component = debugElement.componentInstance;
component.ngOnInit();
fixture.detectChanges();
}

it('should display the message', () => {
configTestingModule();
createComponent();
expect(fixture.nativeElement.innerHTML).toContain('Help Angular by taking a <strong>1 minute survey</strong>!');
});

it('should display an icon', () => {
configTestingModule();
createComponent();
const iconElement = fixture.debugElement.query(By.css('.icon'));
expect(iconElement.properties['svgIcon']).toEqual('insert_comment');
expect(iconElement.attributes['aria-label']).toEqual('Survey');
});

it('should display a button', () => {
configTestingModule();
createComponent();
const button = fixture.debugElement.query(By.css('.action-button'));
expect(button.nativeElement.textContent).toEqual('Go to survey');
});

it('should call hideNotification when the component is clicked', () => {
configTestingModule();
createComponent();
spyOn(component, 'hideNotification');
element.click();
fixture.detectChanges();
expect(component.hideNotification).toHaveBeenCalled();
});

it('should hide the notification when hideNotification is called', () => {
configTestingModule();
createComponent();
expect(component.showNotification).toBe(true);
component.hideNotification();
expect(component.showNotification).toBe(false);
});

it('should update localStorage key when hideNotification is called', () => {
configTestingModule();
createComponent();
const setItemSpy: jasmine.Spy = TestBed.get(WindowToken).localStorage.setItem;
component.hideNotification();
expect(setItemSpy).toHaveBeenCalledWith('survey-january-2018', 'hide');
});

it('should not show the notification if the date is after the expiry date', () => {
configTestingModule(new Date('2018-01-23'));
createComponent();
expect(component.showNotification).toBe(false);
});

it('should not show the notification if the there is a "hide" flag in localStorage', () => {
configTestingModule();
const getItemSpy: jasmine.Spy = TestBed.get(WindowToken).localStorage.getItem;
getItemSpy.and.returnValue('hide');
createComponent();
expect(getItemSpy).toHaveBeenCalledWith('survey-january-2018');
expect(component.showNotification).toBe(false);
});
});

@Component({
template: `
<aio-notification
icon="insert_comment"
iconLabel="Survey"
buttonText="Go to survey"
actionUrl="https://bit.ly/angular-survey-2018"
notificationId="survey-january-2018"
expirationDate="2018-01-22">
Help Angular by taking a <strong>1 minute survey</strong>!
</aio-notification>`
})
class TestComponent {
}

class MockWindow {
localStorage = jasmine.createSpyObj('localStorage', ['getItem', 'setItem']);
}
44 changes: 44 additions & 0 deletions aio/src/app/layout/notification/notification.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { animate, state, style, trigger, transition } from '@angular/animations';
import { Component, HostBinding, HostListener, Inject, Input, OnInit } from '@angular/core';
import { CurrentDateToken } from 'app/shared/current-date';
import { WindowToken } from 'app/shared/window';

@Component({
selector: 'aio-notification',
templateUrl: 'notification.component.html',
animations: [
trigger('accordion', [
state('true', style({height: '*'})),
state('false', style({height: 0})),
transition('true => false', animate(250))
])
]
})
export class NotificationComponent implements OnInit {
private get localStorage() { return this.window.localStorage; }

@Input() icon: string;
@Input() iconLabel: string;
@Input() buttonText: string;
@Input() actionUrl: string;
@Input() notificationId: string;
@Input() expirationDate: string;

@HostBinding('@accordion')
showNotification: boolean;

constructor(
@Inject(WindowToken) private window: Window,
@Inject(CurrentDateToken) private currentDate: Date
) {}

ngOnInit() {
this.showNotification = this.localStorage.getItem(this.notificationId) !== 'hide' && new Date(this.expirationDate) > this.currentDate;
}

@HostListener('click')
hideNotification() {
this.localStorage.setItem(this.notificationId, 'hide');
this.showNotification = false;
}
}
4 changes: 4 additions & 0 deletions aio/src/app/shared/current-date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { InjectionToken } from '@angular/core';

export const CurrentDateToken = new InjectionToken('CurrentDate');
export function currentDateProvider() { return new Date(); }
1 change: 0 additions & 1 deletion aio/src/styles/1-layouts/_top-menu.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ mat-toolbar.mat-toolbar {
right: 0;
left: 0;
z-index: 10;
padding: 0 16px 0 0;
box-shadow: 0 2px 5px 0 rgba(0,0,0,0.30);

mat-icon {
Expand Down
1 change: 1 addition & 0 deletions aio/src/styles/2-modules/_modules-dir.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@
@import 'toc';
@import 'select-menu';
@import 'deploy-theme';
@import 'notification';
49 changes: 49 additions & 0 deletions aio/src/styles/2-modules/_notification.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// we need to override some of the toolbar styling
mat-toolbar-row.notification-container {
padding: 0;
height: auto;
overflow: hidden;
}

aio-notification {
background: $darkgray;
display: flex;
align-items: center;
width: 100%;
height: 56px;

.space, .close-button {
// prevent the spacer and close button from growing to fill
// but allow them to disappear as the screen width gets narrow
flex: 0 1 auto;
width: 56px;
}

.content {
align-items: center;
justify-content: center;
flex: 1 1 auto; // allow the content to fill the space
display: flex;
text-transform: none;
padding: 0;

.icon {
margin-right: 10px;
@media (max-width: 400px) {
display: none;
}
}

.action-button {
margin-left: 10px;
background: $brightred;
border-radius: 15px;
text-transform: uppercase;
padding: 0 10px;
font-size: 12px;
@media (max-width: 780px) {
display: none;
}
}
}
}

0 comments on commit e70ddaa

Please sign in to comment.