Skip to content

Commit

Permalink
Only setup jQuery if jQuery is not disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
cibernox committed May 26, 2018
1 parent e682a5a commit bae56f9
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 74 deletions.
8 changes: 4 additions & 4 deletions packages/@ember/application/tests/application_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { libraries } from 'ember-metal';
import { getDebugFunction, setDebugFunction } from '@ember/debug';
import Application from '..';
import { Router, NoneLocation, Route as EmberRoute } from 'ember-routing';
import { jQuery } from 'ember-views';
import { jQueryDisabled, jQuery } from 'ember-views';
import { _loaded } from '@ember/application';
import Controller from '@ember/controller';
import { Object as EmberObject } from 'ember-runtime';
Expand Down Expand Up @@ -369,11 +369,11 @@ moduleFor(
this.runTask(() => this.createApplication());

assert.equal(messages[1], 'Ember : ' + VERSION);
if (jQuery) {
assert.equal(messages[2], 'jQuery : ' + jQuery().jquery);
if (jQueryDisabled) {
assert.equal(messages[2], 'my-lib : ' + '2.0.0a');
assert.equal(messages[3], 'my-lib : ' + '2.0.0a');
} else {
assert.equal(messages[2], 'my-lib : ' + '2.0.0a');
assert.equal(messages[2], 'jQuery : ' + jQuery().jquery);
}

libraries.deRegister('my-lib');
Expand Down
133 changes: 66 additions & 67 deletions packages/ember-views/lib/system/event_dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import jQuery, { jQueryDisabled } from './jquery';
import ActionManager from './action_manager';
import fallbackViewRegistry from '../compat/fallback-view-registry';

const HAS_JQUERY = jQueryDisabled === false;
const ROOT_ELEMENT_CLASS = 'ember-application';
const ROOT_ELEMENT_SELECTOR = `.${ROOT_ELEMENT_CLASS}`;

Expand Down Expand Up @@ -143,32 +142,7 @@ export default EmberObject.extend({

let rootElementSelector = get(this, 'rootElement');
let rootElement;
if (HAS_JQUERY) {
rootElement = jQuery(rootElementSelector);
assert(
`You cannot use the same root element (${rootElement.selector ||
rootElement[0].tagName}) multiple times in an Ember.Application`,
!rootElement.is(ROOT_ELEMENT_SELECTOR)
);
assert(
'You cannot make a new Ember.Application using a root element that is a descendent of an existing Ember.Application',
!rootElement.closest(ROOT_ELEMENT_SELECTOR).length
);
assert(
'You cannot make a new Ember.Application using a root element that is an ancestor of an existing Ember.Application',
!rootElement.find(ROOT_ELEMENT_SELECTOR).length
);

rootElement.addClass(ROOT_ELEMENT_CLASS);

if (!rootElement.is(ROOT_ELEMENT_SELECTOR)) {
throw new TypeError(
`Unable to add '${ROOT_ELEMENT_CLASS}' class to root element (${rootElement.selector ||
rootElement[0]
.tagName}). Make sure you set rootElement to the body or an element in the body.`
);
}
} else {
if (jQueryDisabled) {
if (typeof rootElementSelector !== 'string') {
rootElement = rootElementSelector;
} else {
Expand Down Expand Up @@ -207,6 +181,31 @@ export default EmberObject.extend({
rootElement.tagName}). Make sure you set rootElement to the body or an element in the body.`,
rootElement.classList.contains(ROOT_ELEMENT_CLASS)
);
} else {
rootElement = jQuery(rootElementSelector);
assert(
`You cannot use the same root element (${rootElement.selector ||
rootElement[0].tagName}) multiple times in an Ember.Application`,
!rootElement.is(ROOT_ELEMENT_SELECTOR)
);
assert(
'You cannot make a new Ember.Application using a root element that is a descendent of an existing Ember.Application',
!rootElement.closest(ROOT_ELEMENT_SELECTOR).length
);
assert(
'You cannot make a new Ember.Application using a root element that is an ancestor of an existing Ember.Application',
!rootElement.find(ROOT_ELEMENT_SELECTOR).length
);

rootElement.addClass(ROOT_ELEMENT_CLASS);

if (!rootElement.is(ROOT_ELEMENT_SELECTOR)) {
throw new TypeError(
`Unable to add '${ROOT_ELEMENT_CLASS}' class to root element (${rootElement.selector ||
rootElement[0]
.tagName}). Make sure you set rootElement to the body or an element in the body.`
);
}
}

let viewRegistry = this._getViewRegistry();
Expand Down Expand Up @@ -238,43 +237,7 @@ export default EmberObject.extend({
return;
}

if (HAS_JQUERY) {
rootElement.on(`${event}.ember`, '.ember-view', function(evt) {
let view = viewRegistry[this.id];
let result = true;

if (view) {
result = view.handleEvent(eventName, evt);
}

return result;
});

rootElement.on(`${event}.ember`, '[data-ember-action]', evt => {
let attributes = evt.currentTarget.attributes;
let handledActions = [];

for (let i = 0; i < attributes.length; i++) {
let attr = attributes.item(i);
let attrName = attr.name;

if (attrName.lastIndexOf('data-ember-action-', 0) !== -1) {
let action = ActionManager.registeredActions[attr.value];

// We have to check for action here since in some cases, jQuery will trigger
// an event on `removeChild` (i.e. focusout) after we've already torn down the
// action handlers for the view.
if (action && action.eventName === eventName && handledActions.indexOf(action) === -1) {
action.handler(evt);
// Action handlers can mutate state which in turn creates new attributes on the element.
// This effect could cause the `data-ember-action` attribute to shift down and be invoked twice.
// To avoid this, we keep track of which actions have been handled.
handledActions.push(action);
}
}
}
});
} else {
if (jQueryDisabled) {
let viewHandler = (target, event) => {
let view = viewRegistry[target.id];
let result = true;
Expand Down Expand Up @@ -347,6 +310,42 @@ export default EmberObject.extend({
});

rootElement.addEventListener(event, handleEvent);
} else {
rootElement.on(`${event}.ember`, '.ember-view', function(evt) {
let view = viewRegistry[this.id];
let result = true;

if (view) {
result = view.handleEvent(eventName, evt);
}

return result;
});

rootElement.on(`${event}.ember`, '[data-ember-action]', evt => {
let attributes = evt.currentTarget.attributes;
let handledActions = [];

for (let i = 0; i < attributes.length; i++) {
let attr = attributes.item(i);
let attrName = attr.name;

if (attrName.lastIndexOf('data-ember-action-', 0) !== -1) {
let action = ActionManager.registeredActions[attr.value];

// We have to check for action here since in some cases, jQuery will trigger
// an event on `removeChild` (i.e. focusout) after we've already torn down the
// action handlers for the view.
if (action && action.eventName === eventName && handledActions.indexOf(action) === -1) {
action.handler(evt);
// Action handlers can mutate state which in turn creates new attributes on the element.
// This effect could cause the `data-ember-action` attribute to shift down and be invoked twice.
// To avoid this, we keep track of which actions have been handled.
handledActions.push(action);
}
}
}
});
}
},

Expand All @@ -370,12 +369,12 @@ export default EmberObject.extend({
return;
}

if (HAS_JQUERY) {
jQuery(rootElementSelector).off('.ember', '**');
} else {
if (jQueryDisabled) {
for (let event in this._eventHandlers) {
rootElement.removeEventListener(event, this._eventHandlers[event]);
}
} else {
jQuery(rootElementSelector).off('.ember', '**');
}

rootElement.classList.remove(ROOT_ELEMENT_CLASS);
Expand Down
4 changes: 3 additions & 1 deletion packages/ember/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,9 @@ Object.defineProperty(Ember, 'TEMPLATES', {
Ember.VERSION = VERSION;

// ****ember-views****
Ember.$ = views.jQuery;
if (!views.jQueryDisabled) {
Ember.$ = views.jQuery;
}
Ember.ViewUtils = {
isSimpleClick: views.isSimpleClick,
getViewElement: views.getViewElement,
Expand Down
5 changes: 3 additions & 2 deletions packages/ember/tests/reexports_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Ember from '../index';
import { FEATURES } from '@ember/canary-features';
import { confirmExport } from 'internal-test-helpers';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';
import { jQueryDisabled } from '../../ember-views';

moduleFor(
'ember reexports',
Expand Down Expand Up @@ -168,7 +169,7 @@ let allExports = [
['Logger', 'ember-console', 'default'],

// ember-views
['$', 'ember-views', 'jQuery'],
!jQueryDisabled && ['$', 'ember-views', 'jQuery'],
['ViewUtils.isSimpleClick', 'ember-views', 'isSimpleClick'],
['ViewUtils.getViewElement', 'ember-views', 'getViewElement'],
['ViewUtils.getViewBounds', 'ember-views', 'getViewBounds'],
Expand Down Expand Up @@ -302,4 +303,4 @@ let allExports = [
// ember-extension-support
['DataAdapter', 'ember-extension-support'],
['ContainerDebugAdapter', 'ember-extension-support'],
];
].filter(Boolean);

0 comments on commit bae56f9

Please sign in to comment.