Skip to content

Commit

Permalink
FIX: Tweak the mutationObserver to respect attributes.
Browse files Browse the repository at this point in the history
To avoid crashing Firefox, it checks that the values actually changed.
This eliminates a bug where whitespace sometimes appeared in an
expanded menu.
  • Loading branch information
eviltrout committed Nov 9, 2015
1 parent d7ee074 commit 6eb83a3
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions app/assets/javascripts/discourse/components/menu-panel.js.es6
Expand Up @@ -24,13 +24,19 @@ export default Ember.Component.extend({
const $panelBody = this.$('.panel-body');
let contentHeight = parseInt(this.$('.panel-body-contents').height());

// We use a mutationObserver to check for style changes, so it's important
// we don't set it if it doesn't change. Same goes for the $panelBody!
const style = this.$().prop('style');

if (viewMode === 'drop-down') {
const $buttonPanel = $('header ul.icons');
if ($buttonPanel.length === 0) { return; }

// These values need to be set here, not in the css file - this is to deal with the
// possibility of the window being resized and the menu changing from .slide-in to .drop-down.
this.$().css({ top: '100%', height: 'auto' });
if (style.top !== '100%' || style.height !== 'auto') {
this.$().css({ top: '100%', height: 'auto' });
}

// adjust panel height
const fullHeight = parseInt($window.height());
Expand All @@ -40,7 +46,9 @@ export default Ember.Component.extend({
if (contentHeight + (offsetTop - scrollTop) + PANEL_BODY_MARGIN > fullHeight) {
contentHeight = fullHeight - (offsetTop - scrollTop) - PANEL_BODY_MARGIN;
}
$panelBody.height(contentHeight);
if ($panelBody.height() !== contentHeight) {
$panelBody.height(contentHeight);
}
$('body').addClass('drop-down-visible');
} else {
const menuTop = headerHeight();
Expand All @@ -53,8 +61,12 @@ export default Ember.Component.extend({
height = winHeight - menuTop;
}

$panelBody.height('100%');
this.$().css({ top: menuTop + "px", height });
if ($panelBody.prop('style').height !== '100%') {
$panelBody.height('100%');
}
if (style.top !== menuTop + "px" || style.height !== height) {
this.$().css({ top: menuTop + "px", height });
}
$('body').removeClass('drop-down-visible');
}

Expand Down Expand Up @@ -127,7 +139,7 @@ export default Ember.Component.extend({
_watchSizeChanges() {
if (mutationSupport) {
this._observer.disconnect();
this._observer.observe(this.element, { childList: true, subtree: true });
this._observer.observe(this.element, { childList: true, subtree: true, characterData: true, attributes: true });
} else {
clearInterval(this._resizeInterval);
this._resizeInterval = setInterval(() => {
Expand Down Expand Up @@ -176,7 +188,7 @@ export default Ember.Component.extend({

if (mutationSupport) {
this._observer = new MutationObserver(() => {
Ember.run(() => this.performLayout());
Ember.run.debounce(this, this.performLayout, 50);
});
}

Expand Down

0 comments on commit 6eb83a3

Please sign in to comment.