FIX: Use topic progress widget for jumping to posts

This commit is contained in:
Robin Ward 2016-05-24 17:00:10 -04:00
parent 3c30fa628b
commit a6b2f5ddba
No known key found for this signature in database
GPG Key ID: 0E091E2B4ED1B83D
5 changed files with 66 additions and 40 deletions

View File

@ -2,19 +2,32 @@ import { default as computed, observes } from 'ember-addons/ember-computed-decor
export default Ember.Component.extend({ export default Ember.Component.extend({
elementId: 'topic-progress-wrapper', elementId: 'topic-progress-wrapper',
classNameBindings: ['docked'], classNameBindings: ['docked', 'hidden'],
expanded: false, expanded: false,
toPostIndex: null, toPostIndex: null,
docked: false, docked: false,
progressPosition: null, progressPosition: null,
postStream: Ember.computed.alias('topic.postStream'), postStream: Ember.computed.alias('topic.postStream'),
userWantsToJump: null,
init() { init() {
this._super(); this._super();
(this.get('delegated') || []).forEach(m => this.set(m, m)); (this.get('delegated') || []).forEach(m => this.set(m, m));
}, },
@computed('userWantsToJump')
hidden(userWantsToJump) {
return !userWantsToJump && !this.site.mobileView;
},
keyboardTrigger(kbdEvent) {
if (kbdEvent.type === 'jump') {
this.set('expanded', true);
this.set('userWantsToJump', true);
Ember.run.scheduleOnce('afterRender', () => this.$('.jump-form input').focus());
}
},
@computed('postStream.loaded', 'progressPosition', 'postStream.filteredPostsCount', 'postStream.highest_post_number') @computed('postStream.loaded', 'progressPosition', 'postStream.filteredPostsCount', 'postStream.highest_post_number')
streamPercentage(loaded, progressPosition, filteredPostsCount, highestPostNumber) { streamPercentage(loaded, progressPosition, filteredPostsCount, highestPostNumber) {
if (!loaded) { return 0; } if (!loaded) { return 0; }
@ -64,11 +77,8 @@ export default Ember.Component.extend({
.on("composer:resized", this, this._dock) .on("composer:resized", this, this._dock)
.on("composer:closed", this, this._dock) .on("composer:closed", this, this._dock)
.on("topic:scrolled", this, this._dock) .on("topic:scrolled", this, this._dock)
.on('topic:current-post-changed', postNumber => this.set('progressPosition', postNumber)); .on('topic:current-post-changed', postNumber => this.set('progressPosition', postNumber))
.on('topic-progress:keyboard-trigger', this, this.keyboardTrigger);
// Reflows are expensive. Cache the jQuery selector
// and the width when inserted into the DOM
this._$topicProgress = this.$('#topic-progress');
Ember.run.scheduleOnce('afterRender', this, this._updateProgressBar); Ember.run.scheduleOnce('afterRender', this, this._updateProgressBar);
}, },
@ -79,18 +89,22 @@ export default Ember.Component.extend({
.off("composer:resized", this, this._dock) .off("composer:resized", this, this._dock)
.off("composer:closed", this, this._dock) .off("composer:closed", this, this._dock)
.off('topic:scrolled', this, this._dock) .off('topic:scrolled', this, this._dock)
.off('topic:current-post-changed'); .off('topic:current-post-changed')
.off('topic-progress:keyboard-trigger');
}, },
_updateProgressBar() { _updateProgressBar() {
if (this.get('hidden')) { return; }
const $topicProgress = this.$('#topic-progress');
// speeds up stuff, bypass jquery slowness and extra checks // speeds up stuff, bypass jquery slowness and extra checks
if (!this._totalWidth) { if (!this._totalWidth) {
this._totalWidth = this._$topicProgress[0].offsetWidth; this._totalWidth = $topicProgress[0].offsetWidth;
} }
const totalWidth = this._totalWidth; const totalWidth = this._totalWidth;
const progressWidth = this.get('streamPercentage') * totalWidth; const progressWidth = this.get('streamPercentage') * totalWidth;
this._$topicProgress.find('.bg') $topicProgress.find('.bg')
.css("border-right-width", (progressWidth === totalWidth) ? "0px" : "1px") .css("border-right-width", (progressWidth === totalWidth) ? "0px" : "1px")
.width(progressWidth); .width(progressWidth);
}, },
@ -143,6 +157,7 @@ export default Ember.Component.extend({
this.send('jumpPost'); this.send('jumpPost');
} else if (e.keyCode === 27) { } else if (e.keyCode === 27) {
this.send('toggleExpansion'); this.send('toggleExpansion');
this.set('userWantsToJump', false);
} }
} }
}, },
@ -151,6 +166,7 @@ export default Ember.Component.extend({
toggleExpansion(opts) { toggleExpansion(opts) {
this.toggleProperty('expanded'); this.toggleProperty('expanded');
if (this.get('expanded')) { if (this.get('expanded')) {
this.set('userWantsToJump', false);
this.set('toPostIndex', this.get('progressPosition')); this.set('toPostIndex', this.get('progressPosition'));
if(opts && opts.highlight){ if(opts && opts.highlight){
// TODO: somehow move to view? // TODO: somehow move to view?
@ -175,18 +191,23 @@ export default Ember.Component.extend({
postIndex = this.get('postStream.filteredPostsCount'); postIndex = this.get('postStream.filteredPostsCount');
} }
this.set('toPostIndex', postIndex); this.set('toPostIndex', postIndex);
this.set('expanded', false); this._beforeJump();
this.sendAction('jumpToIndex', postIndex); this.sendAction('jumpToIndex', postIndex);
}, },
jumpTop() { jumpTop() {
this.set('expanded', false); this._beforeJump();
this.sendAction('jumpTop'); this.sendAction('jumpTop');
}, },
jumpBottom() { jumpBottom() {
this.set('expanded', false); this._beforeJump();
this.sendAction('jumpBottom'); this.sendAction('jumpBottom');
} }
},
_beforeJump() {
this.set('expanded', false);
this.set('userWantsToJump', false);
} }
}); });

View File

@ -21,6 +21,7 @@ export default Ember.Controller.extend(SelectedPostsCount, BufferedContent, {
enteredAt: null, enteredAt: null,
enteredIndex: null, enteredIndex: null,
retrying: false, retrying: false,
userTriggeredProgress: null,
topicDelegated: [ topicDelegated: [
'toggleMultiSelect', 'toggleMultiSelect',

View File

@ -159,7 +159,7 @@ export default {
}, },
toggleProgress() { toggleProgress() {
this.container.lookup('controller:topic-progress').send('toggleExpansion', {highlight: true}); this.appEvents.trigger('topic-progress:keyboard-trigger', { type: 'jump' });
}, },
toggleSearch(event) { toggleSearch(event) {

View File

@ -1,4 +1,5 @@
{{#if expanded}} {{#unless hidden}}
{{#if expanded}}
<nav id='topic-progress-expanded'> <nav id='topic-progress-expanded'>
{{d-button action="jumpTop" {{d-button action="jumpTop"
disabled=jumpTopDisabled disabled=jumpTopDisabled
@ -15,8 +16,8 @@
icon="caret-down" icon="caret-down"
label="topic.progress.go_bottom"}} label="topic.progress.go_bottom"}}
</nav> </nav>
{{/if}} {{/if}}
<nav id='topic-progress' title="{{i18n 'topic.progress.title'}}" class="{{if hideProgress 'hidden'}}"> <nav id='topic-progress' title="{{i18n 'topic.progress.title'}}" class="{{if hideProgress 'hidden'}}">
<div class='nums'> <div class='nums'>
<h4>{{progressPosition}}</h4><span class="{{if hugeNumberOfPosts 'hidden'}}"> <h4>{{progressPosition}}</h4><span class="{{if hugeNumberOfPosts 'hidden'}}">
<span>/</span> <span>/</span>
@ -24,4 +25,5 @@
</div> </div>
<i class="fa {{unless expanded 'fa-sort'}}"></i> <i class="fa {{unless expanded 'fa-sort'}}"></i>
<div class='bg'>&nbsp;</div> <div class='bg'>&nbsp;</div>
</nav> </nav>
{{/unless}}

View File

@ -77,9 +77,11 @@
loading=model.postStream.loading loading=model.postStream.loading
delegated=topicDelegated}} delegated=topicDelegated}}
{{else}} {{else}}
{{topic-progress topic=model delegated=topicDelegated}}
{{topic-admin-menu-button topic=model fixed="true" delegated=topicDelegated}} {{topic-admin-menu-button topic=model fixed="true" delegated=topicDelegated}}
{{/if}} {{/if}}
{{topic-progress topic=model delegated=topicDelegated}}
{{conditional-loading-spinner condition=model.postStream.loadingAbove}} {{conditional-loading-spinner condition=model.postStream.loadingAbove}}
{{plugin-outlet "topic-above-posts"}} {{plugin-outlet "topic-above-posts"}}