|
| 1 | +import { getOwner } from '@ember/application'; |
| 2 | +import { assign } from '@ember/polyfills'; |
| 3 | +import { assert } from '@ember/debug'; |
| 4 | +import { get, set } from '@ember/object'; |
| 5 | +import Ember from 'ember'; |
| 6 | +import jQuery from 'jquery'; |
| 7 | + |
| 8 | +const ActionManager = Ember.__loader.require('@ember/-internals/views/lib/system/action_manager').default; |
| 9 | + |
| 10 | +const ROOT_ELEMENT_CLASS = 'ember-application'; |
| 11 | +const ROOT_ELEMENT_SELECTOR = `.${ROOT_ELEMENT_CLASS}`; |
| 12 | + |
| 13 | +export default Ember.EventDispatcher.extend({ |
| 14 | + |
| 15 | + rootElement: 'body', |
| 16 | + |
| 17 | + init() { |
| 18 | + this._super(); |
| 19 | + |
| 20 | + assert( |
| 21 | + 'EventDispatcher should never be instantiated in fastboot mode. Please report this as an Ember bug.', |
| 22 | + (() => { |
| 23 | + let owner = getOwner(this); |
| 24 | + let environment = owner.lookup('-environment:main'); |
| 25 | + |
| 26 | + return environment.isInteractive; |
| 27 | + })() |
| 28 | + ); |
| 29 | + |
| 30 | + this._eventHandlers = Object.create(null); |
| 31 | + }, |
| 32 | + |
| 33 | + setup(addedEvents, _rootElement) { |
| 34 | + let events = (this._finalEvents = assign({}, get(this, 'events'), addedEvents)); |
| 35 | + |
| 36 | + if (_rootElement !== undefined && _rootElement !== null) { |
| 37 | + set(this, 'rootElement', _rootElement); |
| 38 | + } |
| 39 | + |
| 40 | + let rootElementSelector = get(this, 'rootElement'); |
| 41 | + let rootElement = jQuery(rootElementSelector); |
| 42 | + assert( |
| 43 | + `You cannot use the same root element (${rootElement.selector || |
| 44 | + rootElement[0].tagName}) multiple times in an Ember.Application`, |
| 45 | + !rootElement.is(ROOT_ELEMENT_SELECTOR) |
| 46 | + ); |
| 47 | + assert( |
| 48 | + 'You cannot make a new Ember.Application using a root element that is a descendent of an existing Ember.Application', |
| 49 | + !rootElement.closest(ROOT_ELEMENT_SELECTOR).length |
| 50 | + ); |
| 51 | + assert( |
| 52 | + 'You cannot make a new Ember.Application using a root element that is an ancestor of an existing Ember.Application', |
| 53 | + !rootElement.find(ROOT_ELEMENT_SELECTOR).length |
| 54 | + ); |
| 55 | + |
| 56 | + rootElement.addClass(ROOT_ELEMENT_CLASS); |
| 57 | + |
| 58 | + if (!rootElement.is(ROOT_ELEMENT_SELECTOR)) { |
| 59 | + throw new TypeError( |
| 60 | + `Unable to add '${ROOT_ELEMENT_CLASS}' class to root element (${rootElement.selector || |
| 61 | + rootElement[0] |
| 62 | + .tagName}). Make sure you set rootElement to the body or an element in the body.` |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + let viewRegistry = this._getViewRegistry(); |
| 67 | + |
| 68 | + for (let event in events) { |
| 69 | + if (events.hasOwnProperty(event)) { |
| 70 | + this.setupHandler(rootElement, event, events[event], viewRegistry); |
| 71 | + } |
| 72 | + } |
| 73 | + }, |
| 74 | + |
| 75 | + setupHandler(rootElement, event, eventName, viewRegistry) { |
| 76 | + if (eventName === null) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + rootElement.on(`${event}.ember`, '.ember-view', function(evt) { |
| 81 | + let view = viewRegistry[this.id]; |
| 82 | + let result = true; |
| 83 | + |
| 84 | + if (view) { |
| 85 | + result = view.handleEvent(eventName, evt); |
| 86 | + } |
| 87 | + |
| 88 | + return result; |
| 89 | + }); |
| 90 | + |
| 91 | + rootElement.on(`${event}.ember`, '[data-ember-action]', evt => { |
| 92 | + let attributes = evt.currentTarget.attributes; |
| 93 | + let handledActions = []; |
| 94 | + |
| 95 | + for (let i = 0; i < attributes.length; i++) { |
| 96 | + let attr = attributes.item(i); |
| 97 | + let attrName = attr.name; |
| 98 | + |
| 99 | + if (attrName.lastIndexOf('data-ember-action-', 0) !== -1) { |
| 100 | + let action = ActionManager.registeredActions[attr.value]; |
| 101 | + |
| 102 | + // We have to check for action here since in some cases, jQuery will trigger |
| 103 | + // an event on `removeChild` (i.e. focusout) after we've already torn down the |
| 104 | + // action handlers for the view. |
| 105 | + if (action && action.eventName === eventName && handledActions.indexOf(action) === -1) { |
| 106 | + action.handler(evt); |
| 107 | + // Action handlers can mutate state which in turn creates new attributes on the element. |
| 108 | + // This effect could cause the `data-ember-action` attribute to shift down and be invoked twice. |
| 109 | + // To avoid this, we keep track of which actions have been handled. |
| 110 | + handledActions.push(action); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + }); |
| 115 | + }, |
| 116 | + |
| 117 | + destroy() { |
| 118 | + let rootElementSelector = get(this, 'rootElement'); |
| 119 | + let rootElement; |
| 120 | + if (rootElementSelector.nodeType) { |
| 121 | + rootElement = rootElementSelector; |
| 122 | + } else { |
| 123 | + rootElement = document.querySelector(rootElementSelector); |
| 124 | + } |
| 125 | + |
| 126 | + if (!rootElement) { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + jQuery(rootElementSelector).off('.ember', '**'); |
| 131 | + |
| 132 | + rootElement.classList.remove(ROOT_ELEMENT_CLASS); |
| 133 | + |
| 134 | + return this._super(...arguments); |
| 135 | + } |
| 136 | +}); |
0 commit comments