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
Jarek Radosz fe588cc7f8
DEV: Fix function prototype deprecations (#8681)
* DEV: Fix the function prototype observers deprecation

DEPRECATION: Function prototype extensions have been deprecated, please migrate from function(){}.observes('foo') to observer('foo', function() {}). [deprecation id: function-prototype-extensions.observes] See https://deprecations.emberjs.com/v3.x/#toc_function-prototype-extensions-observes for more details.

* DEV: Fix the function prototype event listeners deprecation

DEPRECATION: Function prototype extensions have been deprecated, please migrate from function(){}.on('foo') to on('foo', function() {}). [deprecation id: function-prototype-extensions.on] See https://deprecations.emberjs.com/v3.x/#toc_function-prototype-extensions-on for more details.

* DEV: Simplify `default as` imports

Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
2020-01-16 18:56:53 +01:00

86 lines
1.8 KiB
JavaScript

import EmberObject from "@ember/object";
import discourseComputed from "discourse-common/utils/decorators";
export function Placeholder(viewName) {
this.viewName = viewName;
}
export default EmberObject.extend(Ember.Array, {
posts: null,
_appendingIds: null,
init() {
this._appendingIds = {};
},
@discourseComputed
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");
}
});