| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {NgZone} from '@angular/core/src/zone/ng_zone'; | 
					
						
							| 
									
										
										
										
											2017-03-02 12:12:46 -08:00
										 |  |  | import {beforeEach, describe, expect, it} from '@angular/core/testing/src/testing_internal'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter'; | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  | import {DomEventsPlugin} from '@angular/platform-browser/src/dom/events/dom_events'; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | import {EventManager, EventManagerPlugin} from '@angular/platform-browser/src/dom/events/event_manager'; | 
					
						
							| 
									
										
										
										
											2017-03-02 12:12:46 -08:00
										 |  |  | import {el} from '../../../testing/src/browser_util'; | 
					
						
							| 
									
										
										
										
											2015-02-09 15:11:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |   let domEventPlugin: DomEventsPlugin; | 
					
						
							| 
									
										
										
										
											2017-02-14 16:14:40 -08:00
										 |  |  |   let doc: any; | 
					
						
							| 
									
										
										
										
											2017-07-13 15:36:11 -07:00
										 |  |  |   let zone: NgZone; | 
					
						
							| 
									
										
										
										
											2015-02-19 19:10:16 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-09 15:11:31 +01:00
										 |  |  |   describe('EventManager', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-14 16:14:40 -08:00
										 |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       doc = getDOM().supportsDOMEvents() ? document : getDOM().createHtmlDocument(); | 
					
						
							| 
									
										
										
										
											2017-07-13 15:36:11 -07:00
										 |  |  |       zone = new NgZone({}); | 
					
						
							|  |  |  |       domEventPlugin = new DomEventsPlugin(doc, zone); | 
					
						
							| 
									
										
										
										
											2017-02-14 16:14:40 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-02-10 16:54:32 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-04 14:23:13 -07:00
										 |  |  |     it('should delegate event bindings to plugins that are passed in from the most generic one to the most specific one', | 
					
						
							|  |  |  |        () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |          const element = el('<div></div>'); | 
					
						
							|  |  |  |          const handler = (e: any /** TODO #9100 */) => e; | 
					
						
							| 
									
										
										
										
											2017-02-14 16:14:40 -08:00
										 |  |  |          const plugin = new FakeEventManagerPlugin(doc, ['click']); | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |          const manager = new EventManager([domEventPlugin, plugin], new FakeNgZone()); | 
					
						
							| 
									
										
										
										
											2015-09-04 14:23:13 -07:00
										 |  |  |          manager.addEventListener(element, 'click', handler); | 
					
						
							| 
									
										
										
										
											2016-09-19 17:15:57 -07:00
										 |  |  |          expect(plugin.eventHandler['click']).toBe(handler); | 
					
						
							| 
									
										
										
										
											2015-09-04 14:23:13 -07:00
										 |  |  |        }); | 
					
						
							| 
									
										
										
										
											2015-02-09 15:11:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should delegate event bindings to the first plugin supporting the event', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const element = el('<div></div>'); | 
					
						
							|  |  |  |       const clickHandler = (e: any /** TODO #9100 */) => e; | 
					
						
							|  |  |  |       const dblClickHandler = (e: any /** TODO #9100 */) => e; | 
					
						
							| 
									
										
										
										
											2017-02-14 16:14:40 -08:00
										 |  |  |       const plugin1 = new FakeEventManagerPlugin(doc, ['dblclick']); | 
					
						
							|  |  |  |       const plugin2 = new FakeEventManagerPlugin(doc, ['click', 'dblclick']); | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const manager = new EventManager([plugin2, plugin1], new FakeNgZone()); | 
					
						
							| 
									
										
										
										
											2015-02-09 15:11:31 +01:00
										 |  |  |       manager.addEventListener(element, 'click', clickHandler); | 
					
						
							|  |  |  |       manager.addEventListener(element, 'dblclick', dblClickHandler); | 
					
						
							| 
									
										
										
										
											2016-09-19 17:15:57 -07:00
										 |  |  |       expect(plugin2.eventHandler['click']).toBe(clickHandler); | 
					
						
							|  |  |  |       expect(plugin1.eventHandler['dblclick']).toBe(dblClickHandler); | 
					
						
							| 
									
										
										
										
											2015-02-09 15:11:31 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-19 19:10:16 -08:00
										 |  |  |     it('should throw when no plugin can handle the event', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const element = el('<div></div>'); | 
					
						
							| 
									
										
										
										
											2017-02-14 16:14:40 -08:00
										 |  |  |       const plugin = new FakeEventManagerPlugin(doc, ['dblclick']); | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const manager = new EventManager([plugin], new FakeNgZone()); | 
					
						
							| 
									
										
										
										
											2017-03-24 09:59:41 -07:00
										 |  |  |       expect(() => manager.addEventListener(element, 'click', null !)) | 
					
						
							| 
									
										
										
										
											2015-05-26 09:25:39 -07:00
										 |  |  |           .toThrowError('No event manager plugin found for event click'); | 
					
						
							| 
									
										
										
										
											2015-02-19 19:10:16 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-01 08:52:54 -07:00
										 |  |  |     it('events are caught when fired from a child', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const element = el('<div><div></div></div>'); | 
					
						
							| 
									
										
										
										
											2015-02-19 19:10:16 -08:00
										 |  |  |       // Workaround for https://bugs.webkit.org/show_bug.cgi?id=122755
 | 
					
						
							| 
									
										
										
										
											2017-02-14 16:14:40 -08:00
										 |  |  |       getDOM().appendChild(doc.body, element); | 
					
						
							| 
									
										
										
										
											2015-02-19 19:10:16 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const child = getDOM().firstChild(element); | 
					
						
							|  |  |  |       const dispatchedEvent = getDOM().createMouseEvent('click'); | 
					
						
							|  |  |  |       let receivedEvent: any /** TODO #9100 */ = null; | 
					
						
							|  |  |  |       const handler = (e: any /** TODO #9100 */) => { receivedEvent = e; }; | 
					
						
							|  |  |  |       const manager = new EventManager([domEventPlugin], new FakeNgZone()); | 
					
						
							| 
									
										
										
										
											2015-09-01 08:52:54 -07:00
										 |  |  |       manager.addEventListener(element, 'click', handler); | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  |       getDOM().dispatchEvent(child, dispatchedEvent); | 
					
						
							| 
									
										
										
										
											2015-02-19 19:10:16 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-09 15:11:31 +01:00
										 |  |  |       expect(receivedEvent).toBe(dispatchedEvent); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-04-02 15:56:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-01 08:52:54 -07:00
										 |  |  |     it('should add and remove global event listeners', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const element = el('<div><div></div></div>'); | 
					
						
							| 
									
										
										
										
											2017-02-14 16:14:40 -08:00
										 |  |  |       getDOM().appendChild(doc.body, element); | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const dispatchedEvent = getDOM().createMouseEvent('click'); | 
					
						
							|  |  |  |       let receivedEvent: any /** TODO #9100 */ = null; | 
					
						
							|  |  |  |       const handler = (e: any /** TODO #9100 */) => { receivedEvent = e; }; | 
					
						
							|  |  |  |       const manager = new EventManager([domEventPlugin], new FakeNgZone()); | 
					
						
							| 
									
										
										
										
											2015-04-02 15:56:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const remover = manager.addGlobalEventListener('document', 'click', handler); | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  |       getDOM().dispatchEvent(element, dispatchedEvent); | 
					
						
							| 
									
										
										
										
											2015-04-02 15:56:58 +02:00
										 |  |  |       expect(receivedEvent).toBe(dispatchedEvent); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       receivedEvent = null; | 
					
						
							|  |  |  |       remover(); | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  |       getDOM().dispatchEvent(element, dispatchedEvent); | 
					
						
							| 
									
										
										
										
											2015-04-02 15:56:58 +02:00
										 |  |  |       expect(receivedEvent).toBe(null); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-02-09 15:11:31 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 17:15:57 -07:00
										 |  |  | /** @internal */ | 
					
						
							| 
									
										
										
										
											2015-02-09 15:11:31 +01:00
										 |  |  | class FakeEventManagerPlugin extends EventManagerPlugin { | 
					
						
							| 
									
										
										
										
											2016-09-19 17:15:57 -07:00
										 |  |  |   eventHandler: {[event: string]: Function} = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-14 16:14:40 -08:00
										 |  |  |   constructor(doc: any, public supportedEvents: string[]) { super(doc); } | 
					
						
							| 
									
										
										
										
											2015-02-09 15:11:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 17:15:57 -07:00
										 |  |  |   supports(eventName: string): boolean { return this.supportedEvents.indexOf(eventName) > -1; } | 
					
						
							| 
									
										
										
										
											2015-02-09 15:11:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 17:15:57 -07:00
										 |  |  |   addEventListener(element: any, eventName: string, handler: Function) { | 
					
						
							|  |  |  |     this.eventHandler[eventName] = handler; | 
					
						
							|  |  |  |     return () => { delete (this.eventHandler[eventName]); }; | 
					
						
							| 
									
										
										
										
											2015-02-09 15:11:31 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-08 13:33:22 -07:00
										 |  |  | class FakeNgZone extends NgZone { | 
					
						
							| 
									
										
										
										
											2015-05-26 09:25:39 -07:00
										 |  |  |   constructor() { super({enableLongStackTrace: false}); } | 
					
						
							| 
									
										
										
										
											2017-08-02 16:30:50 -07:00
										 |  |  |   run<T>(fn: (...args: any[]) => T, applyThis?: any, applyArgs?: any[]): T { return fn(); } | 
					
						
							| 
									
										
										
										
											2016-09-19 17:15:57 -07:00
										 |  |  |   runOutsideAngular(fn: Function) { return fn(); } | 
					
						
							| 
									
										
										
										
											2015-02-09 15:11:31 +01:00
										 |  |  | } |