This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/lib/posts-with-placeholders.js.es6
2019-05-27 10:15:39 +02:00

85 lines
1.8 KiB
JavaScript

import { default as computed } from "ember-addons/ember-computed-decorators";
export function Placeholder(viewName) {
this.viewName = viewName;
}
export default Ember.Object.extend(Ember.Array, {
posts: null,
_appendingIds: null,
init() {
this._appendingIds = {};
},
@computed
length() {
return (
this.get("posts.length") + Object.keys(this._appendingIds || {}).length
);
},
nextObject(index) {
return this.objectAt(index);
},
_changeArray(cb, offset, removed, inserted) {
this.arrayContentWillChange(offset, removed, inserted);
cb();
this.arrayContentDidChange(offset, removed, inserted);
this.notifyPropertyChange("length");
},
clear(cb) {
this._changeArray(cb, 0, this.get("posts.length"), 0);
},
appendPost(cb) {
this._changeArray(cb, this.get("posts.length"), 0, 1);
},
removePost(cb) {
this._changeArray(cb, this.get("posts.length") - 1, 1, 0);
},
refreshAll(cb) {
const length = this.get("posts.length");
this._changeArray(cb, 0, length, length);
},
appending(postIds) {
this._changeArray(
() => {
const appendingIds = this._appendingIds;
postIds.forEach(pid => (appendingIds[pid] = true));
},
this.length,
0,
postIds.length
);
},
finishedAppending(postIds) {
this._changeArray(
() => {
const appendingIds = this._appendingIds;
postIds.forEach(pid => delete appendingIds[pid]);
},
this.get("posts.length") - postIds.length,
postIds.length,
postIds.length
);
},
finishedPrepending(postIds) {
this._changeArray(function() {}, 0, 0, postIds.length);
},
objectAt(index) {
const posts = this.posts;
return index < posts.length
? posts[index]
: new Placeholder("post-placeholder");
}
});