Clean up clicking outside the hamburger, add test

This commit is contained in:
Robin Ward 2015-08-26 12:27:30 -04:00
parent e98c354c2d
commit 9760369e4c
3 changed files with 25 additions and 22 deletions

View File

@ -1,32 +1,23 @@
import { default as computed, on } from 'ember-addons/ember-computed-decorators'; import { default as computed, on, observes } from 'ember-addons/ember-computed-decorators';
export default Ember.Component.extend({ export default Ember.Component.extend({
classNameBindings: ['visible::slideright'], classNameBindings: ['visible::slideright'],
elementId: 'hamburger-menu', elementId: 'hamburger-menu',
visibilityChanged: function(){ @observes('visible')
if(this.get("visible")) { _catchClickOutside() {
$('html').on('click.close-humburger', (e) => { if (this.get('visible')) {
$('html').on('click.close-hamburger', (e) => {
if (this.get('isDestroyed')) {
$('html').off('click.close-humburger');
return true;
}
const $target = $(e.target); const $target = $(e.target);
if ($target.closest('.dropdown.categories').length > 0) { if ($target.closest('.dropdown.hamburger').length > 0) { return; }
return; if ($target.closest('#hamburger-menu').length > 0) { return; }
} this.set('visible', false);
this.set("visible", false);
$('html').off('click.close-humburger');
return true;
}); });
} else { } else {
$('html').off('click.close-humburger'); $('html').off('click.close-hamburger');
} }
}.observes("visible"), },
@computed() @computed()
showKeyboardShortcuts() { showKeyboardShortcuts() {
@ -59,13 +50,14 @@ export default Ember.Component.extend({
this.set('visible', false); this.set('visible', false);
} }
}); });
}, },
@on('willDestroyElement') @on('willDestroyElement')
_removeEvents() { _removeEvents() {
this.$().off('click.discourse-hamburger'); this.$().off('click.discourse-hamburger');
$('body').off('keydown.discourse-hambuger'); $('body').off('keydown.discourse-hambuger');
$('body').off('click.close-humburger'); $('html').off('click.close-hamburger');
}, },
@computed() @computed()

View File

@ -42,7 +42,7 @@
</a> </a>
{{/if}} {{/if}}
</li> </li>
<li class='categories dropdown'> <li class='hamburger categories dropdown'>
{{#if loginRequired}} {{#if loginRequired}}
<a class='icon' <a class='icon'
href href

View File

@ -17,6 +17,17 @@ test("Toggle Menu", (assert) => {
andThen(() => { andThen(() => {
assert.ok(exists("#hamburger-menu.slideright"), "clicking the X hides it"); assert.ok(exists("#hamburger-menu.slideright"), "clicking the X hides it");
}); });
click("#toggle-hamburger-menu");
andThen(() => {
assert.ok(!exists("#hamburger-menu.slideright"), "it opens again");
});
click('#main-outlet')
andThen(() => {
assert.ok(exists("#hamburger-menu.slideright"), "clicking the body hides the menu");
});
}); });
test("Menu Items", (assert) => { test("Menu Items", (assert) => {