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
Mark VanLandingham c7475ee03b
DEV: Import EmberObject rather than global variable (#8256)
* DEV: Import ember/object rather than Ember.Object globally

* fixed broken object proxy import

* prettier on js

* added @ember/object/proxy to loader

* added unstaged file

* Fixed objet proxy reference is loader

* Linting!
2019-10-29 14:23:50 -05:00

86 lines
1.8 KiB
JavaScript

import EmberObject from "@ember/object";
import { default as computed } from "ember-addons/ember-computed-decorators";
export function Placeholder(viewName) {
this.viewName = viewName;
}
export default EmberObject.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");
}
});