Skip to content

Commit

Permalink
feat(cdk): updated code styles and platform
Browse files Browse the repository at this point in the history
  • Loading branch information
pimenovoleg committed May 11, 2018
1 parent 7024a4b commit f29ff9f
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 44 deletions.
26 changes: 13 additions & 13 deletions src/cdk/collections/selection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ describe('SelectionModel', () => {

describe('onChange event', () => {
it('should return the model that dispatched the event', () => {
let model = new SelectionModel();
let spy = jasmine.createSpy('SelectionModel change event');
const model = new SelectionModel();
const spy = jasmine.createSpy('SelectionModel change event');

model.onChange!.subscribe(spy);
model.select(1);
Expand All @@ -99,25 +99,25 @@ describe('SelectionModel', () => {
});

it('should return both the added and removed values', () => {
let model = new SelectionModel();
let spy = jasmine.createSpy('SelectionModel change event');
const model = new SelectionModel();
const spy = jasmine.createSpy('SelectionModel change event');

model.select(1);

model.onChange!.subscribe(spy);

model.select(2);

let event = spy.calls.mostRecent().args[0];
const event = spy.calls.mostRecent().args[0];

expect(spy).toHaveBeenCalled();
expect(event.removed).toEqual([1]);
expect(event.added).toEqual([2]);
});

it('should have updated the selected value before emitting the change event', () => {
let model = new SelectionModel(true);
let spy = jasmine.createSpy('SelectionModel change event');
const model = new SelectionModel(true);
const spy = jasmine.createSpy('SelectionModel change event');

// Note: this assertion is only here to run the getter.
expect(model.selected).toEqual([]);
Expand All @@ -142,7 +142,7 @@ describe('SelectionModel', () => {
it('should emit an event when a value is selected', () => {
model.select(1);

let event = spy.calls.mostRecent().args[0];
const event = spy.calls.mostRecent().args[0];

expect(spy).toHaveBeenCalled();
expect(event.added).toEqual([1]);
Expand Down Expand Up @@ -193,7 +193,7 @@ describe('SelectionModel', () => {
it('should emit a single event when clearing all of the selected options', () => {
model.clear();

let event = spy.calls.mostRecent().args[0];
const event = spy.calls.mostRecent().args[0];

expect(spy).toHaveBeenCalledTimes(1);
expect(event.removed).toEqual([1, 2, 3]);
Expand Down Expand Up @@ -223,7 +223,7 @@ describe('SelectionModel', () => {
});

it('should be able to determine whether it is empty', () => {
let model = new SelectionModel();
const model = new SelectionModel();

expect(model.isEmpty()).toBe(true);

Expand All @@ -233,7 +233,7 @@ describe('SelectionModel', () => {
});

it('should be able to determine whether it has a value', () => {
let model = new SelectionModel();
const model = new SelectionModel();

expect(model.hasValue()).toBe(false);

Expand All @@ -243,7 +243,7 @@ describe('SelectionModel', () => {
});

it('should be able to toggle an option', () => {
let model = new SelectionModel();
const model = new SelectionModel();

model.toggle(1);
expect(model.isSelected(1)).toBe(true);
Expand All @@ -253,7 +253,7 @@ describe('SelectionModel', () => {
});

it('should be able to clear the selected options', () => {
let model = new SelectionModel(true);
const model = new SelectionModel(true);

model.select(1);
model.select(2);
Expand Down
12 changes: 9 additions & 3 deletions src/cdk/collections/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,15 @@ export class SelectionModel<T> {
/** Selects a value. */
private _markSelected(value: T) {
if (!this.isSelected(value)) {
if (!this._multiple) { this._unmarkAll(); }
if (!this._multiple) {
this._unmarkAll();
}

this._selection.add(value);

if (this._emitChanges) { this._selectedToEmit.push(value); }
if (this._emitChanges) {
this._selectedToEmit.push(value);
}
}
}

Expand All @@ -136,7 +140,9 @@ export class SelectionModel<T> {
if (this.isSelected(value)) {
this._selection.delete(value);

if (this._emitChanges) { this._deselectedToEmit.push(value); }
if (this._emitChanges) {
this._deselectedToEmit.push(value);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/cdk/collections/unique-selection-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type UniqueSelectionDispatcherListener = (id: string, name: string) => vo
* This service does not *store* any IDs and names because they may change at any time, so it is
* less error-prone if they are simply passed through when the events occur.
*/
@Injectable()
@Injectable({providedIn: 'root'})
export class UniqueSelectionDispatcher implements OnDestroy {
private _listeners: UniqueSelectionDispatcherListener[] = [];

Expand Down
124 changes: 112 additions & 12 deletions src/cdk/keycodes/keycodes.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,119 @@
export const UP_ARROW = 38;
export const DOWN_ARROW = 40;
export const RIGHT_ARROW = 39;
export const LEFT_ARROW = 37;

export const MAC_ENTER = 3;
export const BACKSPACE = 8;
export const TAB = 9;
export const NUM_CENTER = 12;
export const ENTER = 13;
export const SHIFT = 16;
export const CONTROL = 17;
export const ALT = 18;
export const PAUSE = 19;
export const CAPS_LOCK = 20;
export const ESCAPE = 27;
export const SPACE = 32;
export const PAGE_UP = 33;
export const PAGE_DOWN = 34;
export const HOME = 36;
export const END = 35;
export const ENTER = 13;
export const SPACE = 32;
export const TAB = 9;
export const ESCAPE = 27;
export const BACKSPACE = 8;
export const HOME = 36;
export const LEFT_ARROW = 37;
export const UP_ARROW = 38;
export const RIGHT_ARROW = 39;
export const DOWN_ARROW = 40;
export const PLUS_SIGN = 43;
export const PRINT_SCREEN = 44;
export const INSERT = 45;
export const DELETE = 46;
export const A = 65;
export const Z = 90;
export const ZERO = 48;
export const ONE = 49;
export const TWO = 50;
export const THREE = 51;
export const FOUR = 52;
export const FIVE = 53;
export const SIX = 54;
export const SEVEN = 55;
export const EIGHT = 56;
export const NINE = 57;
export const FF_SEMICOLON = 59; // Firefox (Gecko) fires this for semicolon instead of 186
export const FF_EQUALS = 61; // Firefox (Gecko) fires this for equals instead of 187
export const QUESTION_MARK = 63;
export const AT_SIGN = 64;
export const A = 65;
export const B = 66;
export const C = 67;
export const D = 68;
export const E = 69;
export const F = 70;
export const G = 71;
export const H = 72;
export const I = 73;
export const J = 74;
export const K = 75;
export const L = 76;
export const M = 77;
export const N = 78;
export const O = 79;
export const P = 80;
export const Q = 81;
export const R = 82;
export const S = 83;
export const T = 84;
export const U = 85;
export const V = 86;
export const W = 87;
export const X = 88;
export const Y = 89;
export const Z = 90;
export const META = 91; // WIN_KEY_LEFT
export const MAC_WK_CMD_LEFT = 91;
export const MAC_WK_CMD_RIGHT = 93;
export const CONTEXT_MENU = 93;
export const NUMPAD_ZERO = 96;
export const NUMPAD_ONE = 97;
export const NUMPAD_TWO = 98;
export const NUMPAD_THREE = 99;
export const NUMPAD_FOUR = 100;
export const NUMPAD_FIVE = 101;
export const NUMPAD_SIX = 102;
export const NUMPAD_SEVEN = 103;
export const NUMPAD_EIGHT = 104;
export const NUMPAD_NINE = 105;
export const NUMPAD_MULTIPLY = 106;
export const NUMPAD_PLUS = 107;
export const NUMPAD_MINUS = 109;
export const NUMPAD_PERIOD = 110;
export const NUMPAD_DIVIDE = 111;
export const F1 = 112;
export const F2 = 113;
export const F3 = 114;
export const F4 = 115;
export const F5 = 116;
export const F6 = 117;
export const F7 = 118;
export const F8 = 119;
export const F9 = 120;
export const F10 = 121;
export const F11 = 122;
export const F12 = 123;
export const NUM_LOCK = 144;
export const SCROLL_LOCK = 145;
export const FIRST_MEDIA = 166;
export const FF_MINUS = 173;
export const MUTE = 173; // Firefox (Gecko) fires 181 for MUTE
export const VOLUME_DOWN = 174; // Firefox (Gecko) fires 182 for VOLUME_DOWN
export const VOLUME_UP = 175; // Firefox (Gecko) fires 183 for VOLUME_UP
export const FF_MUTE = 181;
export const FF_VOLUME_DOWN = 182;
export const LAST_MEDIA = 183;
export const FF_VOLUME_UP = 183;
export const SEMICOLON = 186; // Firefox (Gecko) fires 59 for SEMICOLON
export const EQUALS = 187; // Firefox (Gecko) fires 61 for EQUALS
export const COMMA = 188;
export const DASH = 189; // Firefox (Gecko) fires 173 for DASH/MINUS
export const SLASH = 191;
export const APOSTROPHE = 192;
export const TILDE = 192;
export const OPEN_SQUARE_BRACKET = 219;
export const BACKSLASH = 220;
export const CLOSE_SQUARE_BRACKET = 221;
export const SINGLE_QUOTE = 222;
export const MAC_META = 224;
9 changes: 2 additions & 7 deletions src/cdk/platform/platform-module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@

import { NgModule } from '@angular/core';
import { Platform } from './platform';


@NgModule({
providers: [Platform]
})
export class PlatformModule {
}
@NgModule()
export class PlatformModule {}
24 changes: 16 additions & 8 deletions src/cdk/platform/platform.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@

import { Injectable } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { Inject, Injectable, Optional, PLATFORM_ID } from '@angular/core';


// Whether the current platform supports the V8 Break Iterator. The V8 check
// is necessary to detect all Blink based browsers.
const hasV8BreakIterator = (typeof(Intl) !== 'undefined' && (Intl as any).v8BreakIterator);
const hasV8BreakIterator = (typeof Intl !== 'undefined' && (Intl as any).v8BreakIterator);

/**
* Service to detect the current platform by comparing the userAgent strings and
* checking browser-specific global properties.
*/
@Injectable()
@Injectable({providedIn: 'root'})
export class Platform {
/** Whether the Angular application is being rendered in the browser. */
isBrowser: boolean = typeof document === 'object' && !!document;
/**
* Whether the Angular application is being rendered in the browser.
* We want to use the Angular platform check because if the Document is shimmed
* without the navigator, the following checks will fail. This is preferred because
* sometimes the Document may be shimmed without the user's knowledge or intention
*/
isBrowser: boolean = this._platformId ?
isPlatformBrowser(this._platformId) : typeof document === 'object' && !!document;

/** Whether the current browser is Microsoft Edge. */
EDGE: boolean = this.isBrowser && /(edge)/i.test(navigator.userAgent);
Expand All @@ -23,8 +29,8 @@ export class Platform {

/** Whether the current rendering engine is Blink. */
// EdgeHTML and Trident mock Blink specific things and need to be excluded from this check.
BLINK: boolean = this.isBrowser &&
(!!((window as any).chrome || hasV8BreakIterator) && !!CSS && !this.EDGE && !this.TRIDENT);
BLINK: boolean = this.isBrowser && (!!((window as any).chrome || hasV8BreakIterator) &&
typeof CSS !== 'undefined' && !this.EDGE && !this.TRIDENT);

/** Whether the current rendering engine is WebKit. */
// Webkit is part of the userAgent in EdgeHTML, Blink and Trident. Therefore we need to
Expand Down Expand Up @@ -52,4 +58,6 @@ export class Platform {
// this and just place the Safari keyword in the userAgent. To be more safe about Safari every
// Safari browser should also use Webkit as its layout engine.
SAFARI: boolean = this.isBrowser && /safari/i.test(navigator.userAgent) && this.WEBKIT;

constructor(@Optional() @Inject(PLATFORM_ID) private _platformId?: Object) {}
}
2 changes: 2 additions & 0 deletions src/cdk/testing/event-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export function createKeyboardEvent(type: string, keyCode: number, target?: Elem
// IE won't set `defaultPrevented` on synthetic events so we need to do it manually.
event.preventDefault = function() {
Object.defineProperty(event, 'defaultPrevented', { get: () => true });

return originalPreventDefault.apply(this, arguments);
};

Expand All @@ -70,5 +71,6 @@ export function createKeyboardEvent(type: string, keyCode: number, target?: Elem
export function createFakeEvent(type: string, canBubble = false, cancelable = true) {
const event = document.createEvent('Event');
event.initEvent(type, canBubble, cancelable);

return event;
}

0 comments on commit f29ff9f

Please sign in to comment.