From 7de03f837e3a7aff170a035b9ed5e2af887a2576 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 27 Aug 2015 14:29:35 -0400 Subject: [PATCH] UX: Auto size the height of the drop down panel --- .../discourse/components/menu-panel.js.es6 | 38 +++++++++++++------ .../templates/components/menu-panel.hbs | 6 ++- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/discourse/components/menu-panel.js.es6 b/app/assets/javascripts/discourse/components/menu-panel.js.es6 index ae644826bf2..8b88fb9bcaa 100644 --- a/app/assets/javascripts/discourse/components/menu-panel.js.es6 +++ b/app/assets/javascripts/discourse/components/menu-panel.js.es6 @@ -1,14 +1,20 @@ import { default as computed, on, observes } from 'ember-addons/ember-computed-decorators'; +const PANEL_BODY_MARGIN = 30; + export default Ember.Component.extend({ classNameBindings: [':menu-panel', 'visible::hidden', 'viewMode'], - attributeBindings: ['style'], showClose: Ember.computed.equal('viewMode', 'slide-in'), - @computed('viewMode', 'visible') - style(viewMode) { + _resizeComponent() { + if (!this.get('visible')) { return; } + + const viewMode = this.get('viewMode'); + const $panelBody = this.$('.panel-body'); + if (viewMode === 'drop-down') { + // adjust panel position const $buttonPanel = $('header ul.icons'); if ($buttonPanel.length === 0) { return; } @@ -18,19 +24,27 @@ export default Ember.Component.extend({ const posTop = parseInt(buttonPanelPos.top + $buttonPanel.height() - $('header.d-header').offset().top); const posLeft = parseInt(buttonPanelPos.left + $buttonPanel.width() - myWidth); - return `left: ${posLeft}px; top: ${posTop}px`.htmlSafe(); + this.$().css({ left: posLeft + "px", top: posTop + "px" }); + + // adjust panel height + let contentHeight = parseInt($('.panel-body-contents').height()); + const fullHeight = parseInt($(window).height()); + + const offsetTop = this.$().offset().top; + if (contentHeight + offsetTop + PANEL_BODY_MARGIN > fullHeight) { + contentHeight = fullHeight - (offsetTop - $(window).scrollTop()) - PANEL_BODY_MARGIN; + } + $panelBody.height(contentHeight); } else { + $panelBody.height('auto'); + const headerHeight = parseInt($('header.d-header').height() + 3); - return `top: ${headerHeight}px`.htmlSafe(); + this.$().css({ left: "auto", top: headerHeight + "px" }); } }, - @computed('viewMode', 'visible') - bodyStyle(viewMode) { - if (viewMode === 'drop-down') { - const height = parseInt($(window).height() * 0.8) - return `height: ${height}px`.htmlSafe(); - } + _needsResize() { + Ember.run.scheduleOnce('afterRender', this, this._resizeComponent); }, @computed('force') @@ -65,6 +79,7 @@ export default Ember.Component.extend({ } $('html').off('click.close-menu-panel'); } + this._needsResize(); }, @computed() @@ -104,6 +119,7 @@ export default Ember.Component.extend({ // Recompute styles on resize $(window).on('resize.discourse-menu-panel', () => { this.propertyDidChange('viewMode'); + this._needsResize(); }); }, diff --git a/app/assets/javascripts/discourse/templates/components/menu-panel.hbs b/app/assets/javascripts/discourse/templates/components/menu-panel.hbs index 2a708126447..e937c49983b 100644 --- a/app/assets/javascripts/discourse/templates/components/menu-panel.hbs +++ b/app/assets/javascripts/discourse/templates/components/menu-panel.hbs @@ -4,7 +4,9 @@ {{fa-icon 'times'}} {{/if}} -
- {{yield}} +
+
+ {{yield}} +
{{/if}}