1
0
mirror of https://github.com/flarum/core.git synced 2025-07-29 04:30:56 +02:00

Clean up/refactor composer, add escape hotkey

This commit is contained in:
Toby Zerner
2015-02-03 16:59:53 +10:30
parent 74d8b0e377
commit 6544052da6
7 changed files with 259 additions and 211 deletions

View File

@@ -1,112 +1,66 @@
import Ember from 'ember';
import { PositionEnum } from '../controllers/composer';
import ActionButton from '../components/ui/controls/action-button';
import TaggedArray from '../utils/tagged-array';
var $ = Ember.$;
export default Ember.View.extend(Ember.Evented, {
classNames: ['composer'],
classNameBindings: [
'controller.showing:showing',
'controller.minimized:minimized',
'controller.fullScreen:fullscreen',
'minimized',
'fullscreen',
'active'
],
position: Ember.computed.alias('controller.position'),
visible: Ember.computed.alias('controller.visible'),
normal: Ember.computed.alias('controller.normal'),
minimized: Ember.computed.alias('controller.minimized'),
fullscreen: Ember.computed.alias('controller.fullscreen'),
// Calculate the composer's current height, based on the intended height
// (which is set when the resizing handle is dragged), and the composer's
// current state.
computedHeight: function() {
if (this.get('controller.minimized') || this.get('controller.fullScreen')) {
if (this.get('minimized')) {
return '';
} else if (this.get('fullscreen')) {
return $(window).height();
} else {
return Math.max(200, Math.min(this.get('height'), $(window).height() - $('#header').outerHeight()));
}
}.property('height', 'controller.minimized', 'controller.fullScreen'),
updateHeight: function() {
if (! this.$()) {
return;
}
var view = this;
Ember.run.scheduleOnce('afterRender', function() {
view.$().height(view.get('computedHeight'));
view.updateTextareaHeight();
// view.updateBottomPadding();
});
}.observes('computedHeight'),
showingChanged: function() {
if (! this.$()) {
return;
}
var view = this;
if (view.get('controller.showing')) {
view.$().show();
}
Ember.run.scheduleOnce('afterRender', function() {
view.$().css('bottom', view.get('controller.showing') ? -view.$().outerHeight() : 0);
view.$().animate({bottom: view.get('controller.showing') ? 0 : -view.$().outerHeight()}, 'fast', function() {
if (view.get('controller.showing')) {
view.focus();
} else {
view.$().hide();
}
});
view.updateBottomPadding(true);
});
}.observes('controller.showing'),
minimizedChanged: function() {
if (! this.$() || ! this.get('controller.showing')) {
return;
}
var view = this;
var oldHeight = this.$().height();
Ember.run.scheduleOnce('afterRender', function() {
var newHeight = view.$().height();
view.updateBottomPadding(true);
view.$().css('height', oldHeight).animate({height: newHeight}, 'fast', function() {
if (! view.get('controller.minimized')) {
view.focus();
}
});
});
}.observes('controller.minimized'),
fullScreenChanged: function() {
if (! this.$()) {
return;
}
var view = this;
Ember.run.scheduleOnce('afterRender', function() {
view.updateTextareaHeight();
});
}.observes('controller.fullScreen'),
}.property('height', 'minimized', 'fullscreen'),
didInsertElement: function() {
var view = this;
var controller = this.get('controller');
// Hide the composer to begin with.
this.set('height', this.$().height());
this.$().hide();
var controller = this.get('controller');
// If the composer is minimized, allow the user to click anywhere on
// it to show it.
this.$('.composer-content').click(function() {
if (controller.get('minimized')) {
if (view.get('minimized')) {
controller.send('show');
}
});
var view = this;
// Modulate the view's active property/class according to the focus
// state of any inputs.
this.$().on('focus', ':input', function() {
view.set('active', true);
}).on('blur', ':input', function() {
view.set('active', false);
});
this.set('height', this.$().height());
// Focus on the first input when the controller wants to focus.
controller.on('focus', this, this.focus);
// Set up the handle so that the composer can be resized.
$(window).on('resize', {view: this}, this.windowWasResized).resize();
var dragData = {view: this};
@@ -123,107 +77,36 @@ export default Ember.View.extend(Ember.Evented, {
$(document)
.on('mousemove', dragData, this.mouseWasMoved)
.on('mouseup', dragData, this.mouseWasReleased);
// When the escape key is pressed on any inputs, close the composer.
this.$().on('keydown', ':input', 'esc', function() {
controller.send('close');
});
},
willDestroyElement: function() {
$(window)
.off('resize', this.windowWasResized);
$(window).off('resize', this.windowWasResized);
$(document)
.off('mousemove', this.mouseWasMoved)
.off('mouseup', this.mouseWasReleased);
},
windowWasResized: function(event) {
var view = event.data.view;
view.notifyPropertyChange('computedHeight');
},
// Update the amount of padding-bottom on the body so that the page's
// content will still be visible above the composer when the page is
// scrolled right to the bottom.
updateBodyPadding: function(animate) {
// Before we change anything, work out if we're currently scrolled
// right to the bottom of the page. If we are, we'll want to anchor
// the body's scroll position to the bottom after we update the
// padding.
var anchorScroll = $(window).scrollTop() + $(window).height() >= $(document).height();
mouseWasMoved: function(event) {
if (! event.data.handle) {
return;
}
var view = event.data.view;
var func = animate ? 'animate' : 'css';
var paddingBottom = this.get('visible') ? this.get('computedHeight') - Ember.$('#footer').outerHeight(true) : 0;
$('#main')[func]({paddingBottom: paddingBottom}, 'fast');
var deltaPixels = event.data.mouseStart - event.clientY;
view.set('height', event.data.heightStart + deltaPixels);
view.updateTextareaHeight();
view.updateBottomPadding();
},
mouseWasReleased: function(event) {
if (! event.data.handle) {
return;
}
event.data.handle = null;
$('body').css('cursor', '');
},
refreshControls: function() {
var controlItems = TaggedArray.create();
this.trigger('populateControls', controlItems);
this.set('controlItems', controlItems);
}.observes('controller.minimized', 'controller.fullScreen'),
populateControls: function(controls) {
var controller = this.get('controller');
if (controller.get('fullScreen')) {
var exit = ActionButton.create({
icon: 'compress',
title: 'Exit Full Screen',
className: 'btn btn-icon btn-link',
action: function() {
controller.send('exitFullScreen');
}
});
controls.pushObjectWithTag(exit, 'exit');
} else {
if (! controller.get('minimized')) {
var minimize = ActionButton.create({
icon: 'minus',
title: 'Minimize',
className: 'btn btn-icon btn-link btn-minimize',
action: function() {
controller.send('minimize');
}
});
controls.pushObjectWithTag(minimize, 'minimize');
var fullScreen = ActionButton.create({
icon: 'expand',
title: 'Full Screen',
className: 'btn btn-icon btn-link',
action: function() {
controller.send('fullScreen');
}
});
controls.pushObjectWithTag(fullScreen, 'fullScreen');
}
var close = ActionButton.create({
icon: 'times',
title: 'Close',
className: 'btn btn-icon btn-link',
action: function() {
controller.send('close');
}
});
controls.pushObjectWithTag(close, 'close');
}
},
focus: function() {
this.$().find(':input:enabled:visible:first').focus();
},
updateBottomPadding: function(animate) {
var top = $(document).height() - $(window).height();
var isBottom = $(window).scrollTop() >= top;
$('#main')[animate ? 'animate' : 'css']({paddingBottom: this.get('controller.showing') ? this.$().outerHeight() - Ember.$('#footer').outerHeight(true) : 0}, 'fast');
if (isBottom) {
if (anchorScroll) {
if (animate) {
$('html, body').animate({scrollTop: $(document).height()}, 'fast');
} else {
@@ -232,13 +115,151 @@ export default Ember.View.extend(Ember.Evented, {
}
},
updateTextareaHeight: function() {
// Update the height of the stuff inside of the composer. There should be
// an element with the class .flexible-height — this element is intended
// to fill up the height of the composer, minus the space taken up by the
// composer's header/footer/etc.
updateContentHeight: function() {
var content = this.$('.composer-content');
this.$('textarea').height((this.get('computedHeight') || this.$().height())
this.$('.flexible-height').height(this.get('computedHeight')
- parseInt(content.css('padding-top'))
- parseInt(content.css('padding-bottom'))
- this.$('.composer-header').outerHeight(true)
- this.$('.text-editor-controls').outerHeight(true));
}
},
// ------------------------------------------------------------------------
// OBSERVERS
// ------------------------------------------------------------------------
// Whenever the composer is minimized or goes to/from fullscreen, we need
// to re-populate the control buttons, because their configuration depends
// on the composer's current state.
refreshControls: function() {
var controlItems = TaggedArray.create();
this.trigger('populateControls', controlItems);
this.set('controlItems', controlItems);
}.observes('minimized', 'fullscreen'),
// Whenever the composer's computed height changes, update the DOM to
// reflect it.
updateHeight: function() {
if (!this.$()) { return; }
var view = this;
Ember.run.scheduleOnce('afterRender', function() {
view.$().height(view.get('computedHeight'));
view.updateContentHeight();
});
}.observes('computedHeight'),
positionWillChange: function() {
this.set('oldPosition', this.get('position'));
}.observesBefore('position'),
// Whenever the composer's display state changes, update the DOM to slide
// it in or out.
positionDidChange: function() {
var $composer = this.$();
if (!$composer) { return; }
var view = this;
// At this stage, the position property has just changed, and the
// class name hasn't been altered in the DOM. So, we can grab the
// composer's current height which we might want to animate from.
// After the DOM has updated, we animate to its new height.
var oldHeight = $composer.height();
Ember.run.scheduleOnce('afterRender', function() {
var newHeight = $composer.height();
switch (view.get('position')) {
case PositionEnum.HIDDEN:
$composer.animate({bottom: -oldHeight}, 'fast', function() {
$composer.hide();
});
break;
case PositionEnum.NORMAL:
if (view.get('oldPosition') !== PositionEnum.FULLSCREEN) {
$composer.show();
$composer.css({height: oldHeight}).animate({bottom: 0, height: newHeight}, 'fast', function() {
view.focus();
});
}
break;
case PositionEnum.MINIMIZED:
$composer.css({height: oldHeight}).animate({height: newHeight}, 'fast', function() {
view.focus();
});
break;
}
if (view.get('position') !== PositionEnum.FULLSCREEN) {
view.updateBodyPadding(true);
}
view.updateContentHeight();
});
}.observes('position'),
// ------------------------------------------------------------------------
// LISTENERS
// ------------------------------------------------------------------------
windowWasResized: function(event) {
// Force a recalculation of the computed height, because its value
// depends on the window's height.
var view = event.data.view;
view.notifyPropertyChange('computedHeight');
},
mouseWasMoved: function(event) {
if (! event.data.handle) { return; }
var view = event.data.view;
// Work out how much the mouse has been moved, and set the height
// relative to the old one based on that. Then update the content's
// height so that it fills the height of the composer, and update the
// body's padding.
var deltaPixels = event.data.mouseStart - event.clientY;
view.set('height', event.data.heightStart + deltaPixels);
view.updateContentHeight();
view.updateBodyPadding();
},
mouseWasReleased: function(event) {
if (! event.data.handle) { return; }
event.data.handle = null;
$('body').css('cursor', '');
},
focus: function() {
this.$().find(':input:enabled:visible:first').focus();
},
populateControls: function(controls) {
var controller = this.get('controller');
var addControl = function(action, icon, title) {
var control = ActionButton.create({
icon: icon,
title: title,
className: 'btn btn-icon btn-link',
action: function() {
controller.send(action);
}
});
controls.pushObjectWithTag(control, action);
};
if (this.get('fullscreen')) {
addControl('exitFullscreen', 'compress', 'Exit Full Screen');
} else {
if (! this.get('minimized')) {
addControl('minimize', 'minus minimize', 'Minimize');
addControl('fullscreen', 'expand', 'Full Screen');
}
addControl('close', 'times', 'Close');
}
}
});