mirror of
https://github.com/flarum/framework.git
synced 2024-12-05 09:03:36 +08:00
c28307903b
HTMLBars goodness! Since there was some breakage and a lot of fiddling around to get some things working, I took this opportunity to do a big cleanup of the whole Ember app. I accidentally worked on some new features too :3 Note that the app is still broken right now, pending on https://github.com/emberjs/ember.js/issues/10401 Cleanup: - Restructuring of components - Consolidation of some stuff into mixins, cleanup of some APIs that will be public - Change all instances of .property() / .observes() / .on() to Ember.computed() / Ember.observer() / Ember.on() respectively (I think it is more readable) - More comments - Start conforming to a code style (2 spaces for indentation) New features: - Post hiding/restoring - Mark individual discussions as read by clicking - Clicking on a read discussion jumps to the end - Mark all discussions as read - Progressively mark the discussion as read as the page is scrolled - Unordered list post formatting - Post permalink popup Demo once that Ember regression is fixed!
126 lines
4.0 KiB
JavaScript
126 lines
4.0 KiB
JavaScript
import Ember from 'ember';
|
|
|
|
var $ = Ember.$;
|
|
|
|
/**
|
|
A stream 'item' represents one item in the post stream - this may be a
|
|
single post, or it may represent a gap of many posts which have not been
|
|
loaded.
|
|
*/
|
|
export default Ember.Component.extend({
|
|
classNames: ['item'],
|
|
classNameBindings: ['gap', 'loading', 'direction'],
|
|
attributeBindings: [
|
|
'start:data-start',
|
|
'end:data-end',
|
|
'time:data-time',
|
|
'number:data-number'
|
|
],
|
|
|
|
start: Ember.computed.alias('item.indexStart'),
|
|
end: Ember.computed.alias('item.indexEnd'),
|
|
number: Ember.computed.alias('item.content.number'),
|
|
loading: Ember.computed.alias('item.loading'),
|
|
direction: Ember.computed.alias('item.direction'),
|
|
gap: Ember.computed.not('item.content'),
|
|
|
|
time: Ember.computed('item.content.time', function() {
|
|
var time = this.get('item.content.time');
|
|
return time ? time.toString() : null;
|
|
}),
|
|
|
|
count: Ember.computed('start', 'end', function() {
|
|
return this.get('end') - this.get('start') + 1;
|
|
}),
|
|
|
|
loadingChanged: Ember.observer('loading', function() {
|
|
this.rerender();
|
|
}),
|
|
|
|
render: function(buffer) {
|
|
if (this.get('item.content')) {
|
|
return this._super(buffer);
|
|
}
|
|
|
|
buffer.push('<span>');
|
|
if (this.get('loading')) {
|
|
buffer.push(' ');
|
|
} else {
|
|
buffer.push(this.get('count')+' more post'+(this.get('count') !== 1 ? 's' : ''));
|
|
}
|
|
buffer.push('</span>');
|
|
},
|
|
|
|
didInsertElement: function() {
|
|
if (!this.get('gap')) {
|
|
return;
|
|
}
|
|
|
|
if (this.get('loading')) {
|
|
var view = this;
|
|
Ember.run.scheduleOnce('afterRender', function() {
|
|
view.$().spin('small');
|
|
});
|
|
} else {
|
|
var self = this;
|
|
this.$().hover(function(e) {
|
|
if (! self.get('loading')) {
|
|
var up = e.clientY > $(this).offset().top - $(document).scrollTop() + $(this).outerHeight(true) / 2;
|
|
self.set('direction', up ? 'up' : 'down');
|
|
}
|
|
});
|
|
}
|
|
},
|
|
|
|
load: function(relativeIndex) {
|
|
// If this item is not a gap, or if we're already loading its posts,
|
|
// then we don't need to do anything.
|
|
if (! this.get('gap') || this.get('loading')) {
|
|
return false;
|
|
}
|
|
|
|
// If new posts are being loaded in an upwards direction, then when
|
|
// they are rendered, the rest of the posts will be pushed down the
|
|
// page. If loaded in a downwards direction from the end of a
|
|
// discussion, the terminal gap will disappear and the page will
|
|
// scroll up a bit before the new posts are rendered. In order to
|
|
// maintain the current scroll position relative to the content
|
|
// before/after the gap, we need to find item directly after the gap
|
|
// and use it as an anchor.
|
|
var siblingFunc = this.get('direction') === 'up' ? 'nextAll' : 'prevAll';
|
|
var anchor = this.$()[siblingFunc]('.item:first');
|
|
|
|
// Immediately after the posts have been loaded (but before they
|
|
// have been rendered,) we want to grab the distance from the top of
|
|
// the viewport to the top of the anchor element.
|
|
this.get('stream').one('postsLoaded', function() {
|
|
if (anchor.length) {
|
|
var scrollOffset = anchor.offset().top - $(document).scrollTop();
|
|
}
|
|
|
|
// After they have been rendered, we scroll back to a position
|
|
// so that the distance from the top of the viewport to the top
|
|
// of the anchor element is the same as before. If there is no
|
|
// anchor (i.e. this gap is terminal,) then we'll scroll to the
|
|
// bottom of the document.
|
|
Ember.run.scheduleOnce('afterRender', function() {
|
|
$('body').scrollTop(anchor.length ? anchor.offset().top - scrollOffset : $('body').height());
|
|
});
|
|
});
|
|
|
|
// Tell the controller that we want to load the range of posts that this
|
|
// gap represents. We also specify which direction we want to load the
|
|
// posts from.
|
|
this.sendAction(
|
|
'loadRange',
|
|
this.get('start') + (relativeIndex || 0),
|
|
this.get('end'),
|
|
this.get('direction') === 'up'
|
|
);
|
|
},
|
|
|
|
click: function() {
|
|
this.load();
|
|
}
|
|
});
|