refactor(core): move EventManager from core to platform/dom
Closes #5465
This commit is contained in:
@@ -1,34 +0,0 @@
|
||||
import {
|
||||
AsyncTestCompleter,
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
describe,
|
||||
el,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
it,
|
||||
xit,
|
||||
beforeEachBindings,
|
||||
SpyObject,
|
||||
} from 'angular2/testing_internal';
|
||||
|
||||
// import {MapWrapper} from 'angular2/src/facade/collection';
|
||||
// import {DOM} from 'angular2/src/platform/dom/dom_adapter';
|
||||
|
||||
// import {DomTestbed, TestRootView, elRef} from './dom_testbed';
|
||||
|
||||
// import {
|
||||
// ViewDefinition,
|
||||
// RenderDirectiveMetadata,
|
||||
// RenderViewRef,
|
||||
// ViewEncapsulation
|
||||
// } from 'angular2/src/core/render/api';
|
||||
|
||||
export function main() {
|
||||
describe('DomRenderer integration', () => {
|
||||
it('should work', () => {
|
||||
// TODO
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
import {
|
||||
describe,
|
||||
ddescribe,
|
||||
it,
|
||||
iit,
|
||||
xit,
|
||||
xdescribe,
|
||||
expect,
|
||||
beforeEach,
|
||||
el
|
||||
} from 'angular2/testing_internal';
|
||||
import {EventManager, EventManagerPlugin} from 'angular2/core';
|
||||
import {DomEventsPlugin} from 'angular2/src/platform/dom/events/dom_events';
|
||||
import {NgZone} from 'angular2/src/core/zone/ng_zone';
|
||||
import {ListWrapper, Map, MapWrapper} from 'angular2/src/facade/collection';
|
||||
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
|
||||
|
||||
export function main() {
|
||||
var domEventPlugin;
|
||||
|
||||
beforeEach(() => { domEventPlugin = new DomEventsPlugin(); });
|
||||
|
||||
describe('EventManager', () => {
|
||||
|
||||
it('should delegate event bindings to plugins that are passed in from the most generic one to the most specific one',
|
||||
() => {
|
||||
var element = el('<div></div>');
|
||||
var handler = (e) => e;
|
||||
var plugin = new FakeEventManagerPlugin(['click']);
|
||||
var manager = new EventManager([domEventPlugin, plugin], new FakeNgZone());
|
||||
manager.addEventListener(element, 'click', handler);
|
||||
expect(plugin._eventHandler.get('click')).toBe(handler);
|
||||
});
|
||||
|
||||
it('should delegate event bindings to the first plugin supporting the event', () => {
|
||||
var element = el('<div></div>');
|
||||
var clickHandler = (e) => e;
|
||||
var dblClickHandler = (e) => e;
|
||||
var plugin1 = new FakeEventManagerPlugin(['dblclick']);
|
||||
var plugin2 = new FakeEventManagerPlugin(['click', 'dblclick']);
|
||||
var manager = new EventManager([plugin2, plugin1], new FakeNgZone());
|
||||
manager.addEventListener(element, 'click', clickHandler);
|
||||
manager.addEventListener(element, 'dblclick', dblClickHandler);
|
||||
expect(plugin1._eventHandler.has('click')).toBe(false);
|
||||
expect(plugin2._eventHandler.get('click')).toBe(clickHandler);
|
||||
expect(plugin2._eventHandler.has('dblclick')).toBe(false);
|
||||
expect(plugin1._eventHandler.get('dblclick')).toBe(dblClickHandler);
|
||||
});
|
||||
|
||||
it('should throw when no plugin can handle the event', () => {
|
||||
var element = el('<div></div>');
|
||||
var plugin = new FakeEventManagerPlugin(['dblclick']);
|
||||
var manager = new EventManager([plugin], new FakeNgZone());
|
||||
expect(() => manager.addEventListener(element, 'click', null))
|
||||
.toThrowError('No event manager plugin found for event click');
|
||||
});
|
||||
|
||||
it('events are caught when fired from a child', () => {
|
||||
var element = el('<div><div></div></div>');
|
||||
// Workaround for https://bugs.webkit.org/show_bug.cgi?id=122755
|
||||
DOM.appendChild(DOM.defaultDoc().body, element);
|
||||
|
||||
var child = DOM.firstChild(element);
|
||||
var dispatchedEvent = DOM.createMouseEvent('click');
|
||||
var receivedEvent = null;
|
||||
var handler = (e) => { receivedEvent = e; };
|
||||
var manager = new EventManager([domEventPlugin], new FakeNgZone());
|
||||
manager.addEventListener(element, 'click', handler);
|
||||
DOM.dispatchEvent(child, dispatchedEvent);
|
||||
|
||||
expect(receivedEvent).toBe(dispatchedEvent);
|
||||
});
|
||||
|
||||
it('should add and remove global event listeners', () => {
|
||||
var element = el('<div><div></div></div>');
|
||||
DOM.appendChild(DOM.defaultDoc().body, element);
|
||||
var dispatchedEvent = DOM.createMouseEvent('click');
|
||||
var receivedEvent = null;
|
||||
var handler = (e) => { receivedEvent = e; };
|
||||
var manager = new EventManager([domEventPlugin], new FakeNgZone());
|
||||
|
||||
var remover = manager.addGlobalEventListener("document", 'click', handler);
|
||||
DOM.dispatchEvent(element, dispatchedEvent);
|
||||
expect(receivedEvent).toBe(dispatchedEvent);
|
||||
|
||||
receivedEvent = null;
|
||||
remover();
|
||||
DOM.dispatchEvent(element, dispatchedEvent);
|
||||
expect(receivedEvent).toBe(null);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class FakeEventManagerPlugin extends EventManagerPlugin {
|
||||
_eventHandler = new Map<string, Function>();
|
||||
constructor(public _supports: string[]) { super(); }
|
||||
|
||||
supports(eventName: string): boolean { return ListWrapper.contains(this._supports, eventName); }
|
||||
|
||||
addEventListener(element, eventName: string, handler: Function) {
|
||||
this._eventHandler.set(eventName, handler);
|
||||
return () => { this._eventHandler.delete(eventName); };
|
||||
}
|
||||
}
|
||||
|
||||
class FakeNgZone extends NgZone {
|
||||
constructor() { super({enableLongStackTrace: false}); }
|
||||
run(fn) { fn(); }
|
||||
|
||||
runOutsideAngular(fn) { return fn(); }
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
import {
|
||||
describe,
|
||||
ddescribe,
|
||||
it,
|
||||
iit,
|
||||
xit,
|
||||
xdescribe,
|
||||
expect,
|
||||
beforeEach,
|
||||
el
|
||||
} from 'angular2/testing_internal';
|
||||
import {KeyEventsPlugin} from 'angular2/src/platform/dom/events/key_events';
|
||||
|
||||
export function main() {
|
||||
describe('KeyEvents', () => {
|
||||
|
||||
it('should ignore unrecognized events', () => {
|
||||
expect(KeyEventsPlugin.parseEventName('keydown')).toEqual(null);
|
||||
expect(KeyEventsPlugin.parseEventName('keyup')).toEqual(null);
|
||||
expect(KeyEventsPlugin.parseEventName('keydown.unknownmodifier.enter')).toEqual(null);
|
||||
expect(KeyEventsPlugin.parseEventName('keyup.unknownmodifier.enter')).toEqual(null);
|
||||
expect(KeyEventsPlugin.parseEventName('unknownevent.control.shift.enter')).toEqual(null);
|
||||
expect(KeyEventsPlugin.parseEventName('unknownevent.enter')).toEqual(null);
|
||||
});
|
||||
|
||||
it('should correctly parse event names', () => {
|
||||
// key with no modifier
|
||||
expect(KeyEventsPlugin.parseEventName('keydown.enter'))
|
||||
.toEqual({'domEventName': 'keydown', 'fullKey': 'enter'});
|
||||
expect(KeyEventsPlugin.parseEventName('keyup.enter'))
|
||||
.toEqual({'domEventName': 'keyup', 'fullKey': 'enter'});
|
||||
|
||||
// key with modifiers:
|
||||
expect(KeyEventsPlugin.parseEventName('keydown.control.shift.enter'))
|
||||
.toEqual({'domEventName': 'keydown', 'fullKey': 'control.shift.enter'});
|
||||
expect(KeyEventsPlugin.parseEventName('keyup.control.shift.enter'))
|
||||
.toEqual({'domEventName': 'keyup', 'fullKey': 'control.shift.enter'});
|
||||
|
||||
// key with modifiers in a different order:
|
||||
expect(KeyEventsPlugin.parseEventName('keydown.shift.control.enter'))
|
||||
.toEqual({'domEventName': 'keydown', 'fullKey': 'control.shift.enter'});
|
||||
expect(KeyEventsPlugin.parseEventName('keyup.shift.control.enter'))
|
||||
.toEqual({'domEventName': 'keyup', 'fullKey': 'control.shift.enter'});
|
||||
|
||||
// key that is also a modifier:
|
||||
expect(KeyEventsPlugin.parseEventName('keydown.shift.control'))
|
||||
.toEqual({'domEventName': 'keydown', 'fullKey': 'shift.control'});
|
||||
expect(KeyEventsPlugin.parseEventName('keyup.shift.control'))
|
||||
.toEqual({'domEventName': 'keyup', 'fullKey': 'shift.control'});
|
||||
|
||||
expect(KeyEventsPlugin.parseEventName('keydown.control.shift'))
|
||||
.toEqual({'domEventName': 'keydown', 'fullKey': 'control.shift'});
|
||||
expect(KeyEventsPlugin.parseEventName('keyup.control.shift'))
|
||||
.toEqual({'domEventName': 'keyup', 'fullKey': 'control.shift'});
|
||||
|
||||
});
|
||||
|
||||
it('should alias esc to escape', () => {
|
||||
expect(KeyEventsPlugin.parseEventName('keyup.control.esc'))
|
||||
.toEqual(KeyEventsPlugin.parseEventName('keyup.control.escape'));
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
import {
|
||||
AsyncTestCompleter,
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
xdescribe,
|
||||
describe,
|
||||
el,
|
||||
dispatchEvent,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
beforeEachBindings,
|
||||
it,
|
||||
xit,
|
||||
SpyObject,
|
||||
proxy
|
||||
} from 'angular2/testing_internal';
|
||||
|
||||
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
|
||||
import {DomSharedStylesHost} from 'angular2/src/platform/dom/shared_styles_host';
|
||||
|
||||
export function main() {
|
||||
describe('DomSharedStylesHost', () => {
|
||||
var doc;
|
||||
var ssh: DomSharedStylesHost;
|
||||
var someHost: Element;
|
||||
beforeEach(() => {
|
||||
doc = DOM.createHtmlDocument();
|
||||
doc.title = '';
|
||||
ssh = new DomSharedStylesHost(doc);
|
||||
someHost = DOM.createElement('div');
|
||||
});
|
||||
|
||||
it('should add existing styles to new hosts', () => {
|
||||
ssh.addStyles(['a {};']);
|
||||
ssh.addHost(someHost);
|
||||
expect(DOM.getInnerHTML(someHost)).toEqual('<style>a {};</style>');
|
||||
});
|
||||
|
||||
it('should add new styles to hosts', () => {
|
||||
ssh.addHost(someHost);
|
||||
ssh.addStyles(['a {};']);
|
||||
expect(DOM.getInnerHTML(someHost)).toEqual('<style>a {};</style>');
|
||||
});
|
||||
|
||||
it('should add styles only once to hosts', () => {
|
||||
ssh.addStyles(['a {};']);
|
||||
ssh.addHost(someHost);
|
||||
ssh.addStyles(['a {};']);
|
||||
expect(DOM.getInnerHTML(someHost)).toEqual('<style>a {};</style>');
|
||||
});
|
||||
|
||||
it('should use the document head as default host', () => {
|
||||
ssh.addStyles(['a {};', 'b {};']);
|
||||
expect(doc.head).toHaveText('a {};b {};');
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user