Skip to content

Commit

Permalink
Merge pull request #129 from oscarni/master
Browse files Browse the repository at this point in the history
Register unique clickOutsideToClose click handlers
  • Loading branch information
lukemelia authored Oct 26, 2016
2 parents d5f9f94 + f3195f0 commit 8a8f262
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 6 deletions.
6 changes: 3 additions & 3 deletions addon/components/modal-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Ember from 'ember';
import layout from '../templates/components/modal-dialog';

const { dasherize } = Ember.String;
const { $, computed, inject } = Ember;
const { $, computed, guidFor, inject } = Ember;
const { oneWay } = computed;
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
const computedJoin = function(prop) {
Expand Down Expand Up @@ -61,14 +61,14 @@ export default Ember.Component.extend({
this.send('close');
}
};
const registerClick = () => $(document).on('click.ember-modal-dialog', handleClick);
const registerClick = () => $(document).on(`click.ember-modal-dialog-${guidFor(this)}`, handleClick);

// setTimeout needed or else the click handler will catch the click that spawned this modal dialog
setTimeout(registerClick);
this._super(...arguments);
},
willDestroyElement() {
$(document).off('click.ember-modal-dialog');
$(document).off(`click.ember-modal-dialog-${guidFor(this)}`);
this._super(...arguments);
},

Expand Down
3 changes: 2 additions & 1 deletion tests/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"andThen",
"currentURL",
"currentPath",
"currentRouteName"
"currentRouteName",
"nativeClick"
],
"node": false,
"browser": false,
Expand Down
17 changes: 17 additions & 0 deletions tests/acceptance/tether-dialog-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@ test('modal without overlay click outside to close', function(assert) {
dialogText: 'Without Overlay - Click Outside to Close',
closeSelector: '#example-without-overlay-click-outside-to-close'
});

assert.dialogOpensAndCloses({
openSelector: '#example-without-overlay-click-outside-to-close button:nth-of-type(2)',
dialogText: 'Without Overlay Another One - Click Outside to Close',
closeSelector: '#example-without-overlay-click-outside-to-close button:nth-of-type(1)'
});

assert.dialogOpensAndCloses({
openSelector: '#example-without-overlay-click-outside-to-close button:nth-of-type(2)',
dialogText: 'Without Overlay Another One - Click Outside to Close',
closeSelector: '#example-without-overlay-click-outside-to-close'
});

andThen(function() {
assert.equal(Ember.$(dialogSelector).length, 0, 'All modals are absent');
});

});

test('target - selector', function(assert) {
Expand Down
4 changes: 4 additions & 0 deletions tests/dummy/app/controllers/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default Ember.Controller.extend({
isShowingTranslucent: false,
isShowingWithoutOverlay: false,
isShowingWithoutOverlayClickOutsideToClose: false,
isShowingWithoutOverlayClickOutsideToCloseAnotherOne: false,
isShowingCustomStyles: false,
isShowingTargetSelector: false,
isShowingTargetView: false,
Expand Down Expand Up @@ -53,6 +54,9 @@ export default Ember.Controller.extend({
toggleWithoutOverlayClickOutsideToClose() {
this.toggleProperty('isShowingWithoutOverlayClickOutsideToClose');
},
toggleWithoutOverlayClickOutsideToCloseAnotherOne() {
this.toggleProperty('isShowingWithoutOverlayClickOutsideToCloseAnotherOne');
},
toggleCustomStyles() {
this.toggleProperty('isShowingCustomStyles');
},
Expand Down
13 changes: 13 additions & 0 deletions tests/dummy/app/templates/-tether-dialog.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
<div class='example' id='example-without-overlay-click-outside-to-close'>
<h2>Without Overlay - Click Outside to Close</h2>
<button {{action 'toggleWithoutOverlayClickOutsideToClose'}}>Do It</button>
<button id="without-overlay-another-one-button"
{{action 'toggleWithoutOverlayClickOutsideToCloseAnotherOne'}}>Another one</button>
{{code-snippet name='without-overlay-click-outside-to-close-tether-dialog.hbs'}}
{{#if isShowingWithoutOverlayClickOutsideToClose}}
{{!-- BEGIN-SNIPPET without-overlay-click-outside-to-close-tether-dialog --}}
Expand All @@ -60,6 +62,17 @@
{{/tether-dialog}}
{{!-- END-SNIPPET --}}
{{/if}}
{{#if isShowingWithoutOverlayClickOutsideToCloseAnotherOne}}
{{#tether-dialog close='toggleWithoutOverlayClickOutsideToCloseAnotherOne'
hasOverlay=false
clickOutsideToClose=true
offset='100px 0'}}
<h1>Stop! Another modal!</h1>
<p>Without Overlay Another One - Click Outside to Close</p>
<button {{action 'toggleWithoutOverlayClickOutsideToCloseAnotherOne'}}>Close</button>
{{/tether-dialog}}
{{/if}}

</div>

<div class='example' id='example-custom-styles'>
Expand Down
23 changes: 23 additions & 0 deletions tests/helpers/acceptance-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Ember from 'ember';

function fireNativeMouseEvent(eventType, selectorOrDomElement, context) {
let event = new window.Event(eventType, { bubbles: true, cancelable: true, view: window });
let target;
if (typeof selectorOrDomElement === 'string') {
target = Ember.$(selectorOrDomElement, context)[0];
} else {
target = selectorOrDomElement;
}
Ember.run(() => target.dispatchEvent(event));
}

export default function acceptanceTestHelpers() {

Ember.Test.registerAsyncHelper('nativeClick', function(app, selectorOrDomElement, context) {
fireNativeMouseEvent('click', selectorOrDomElement, context);
wait();
});

}

export default acceptanceTestHelpers();
4 changes: 2 additions & 2 deletions tests/helpers/modal-asserts.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ export default function registerAssertHelpers() {
message = message || `Dialog triggered by ${options.openSelector} failed to open and close`;
const dialogContent = [dialogSelector, `:contains(${options.dialogText})`].join('');
const self = this;
return click(options.openSelector, options.context).then(function() {
return nativeClick(options.openSelector, options.context).then(function() {
if (options.hasOverlay) {
self.isPresentOnce(overlaySelector);
}
self.isPresentOnce(dialogContent);
if (options.whileOpen) {
options.whileOpen();
}
return click(options.closeSelector, options.context).then(function() {
return nativeClick(options.closeSelector, options.context).then(function() {
self.isAbsent(overlaySelector);
self.isAbsent(dialogContent);
});
Expand Down
1 change: 1 addition & 0 deletions tests/helpers/start-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Ember from 'ember';
import Application from '../../app';
import config from '../../config/environment';
import registerAssertHelpers from './modal-asserts';
import './acceptance-helpers';

export default function startApp(attrs) {
let application;
Expand Down

0 comments on commit 8a8f262

Please sign in to comment.