discourse/app/assets/javascripts/admin/controllers/admin-watched-words-action.js.es6

79 lines
2.0 KiB
Plaintext
Raw Normal View History

2018-06-15 23:03:24 +08:00
import computed from "ember-addons/ember-computed-decorators";
import WatchedWord from "admin/models/watched-word";
export default Ember.Controller.extend({
actionNameKey: null,
adminWatchedWords: Ember.inject.controller(),
2018-06-15 23:03:24 +08:00
showWordsList: Ember.computed.or(
"adminWatchedWords.filtered",
"adminWatchedWords.showWords"
),
findAction(actionName) {
2018-06-15 23:03:24 +08:00
return (this.get("adminWatchedWords.model") || []).findBy(
"nameKey",
actionName
);
},
2018-06-15 23:03:24 +08:00
@computed("actionNameKey", "adminWatchedWords.model")
filteredContent(actionNameKey) {
2018-06-15 23:03:24 +08:00
if (!actionNameKey) {
return [];
}
const a = this.findAction(actionNameKey);
return a ? a.words : [];
},
2018-06-15 23:03:24 +08:00
@computed("actionNameKey")
actionDescription(actionNameKey) {
2018-06-15 23:03:24 +08:00
return I18n.t("admin.watched_words.action_descriptions." + actionNameKey);
},
2018-06-15 23:03:24 +08:00
@computed("actionNameKey", "adminWatchedWords.model")
wordCount(actionNameKey) {
const a = this.findAction(actionNameKey);
return a ? a.words.length : 0;
},
actions: {
recordAdded(arg) {
2018-06-15 23:03:24 +08:00
const a = this.findAction(this.get("actionNameKey"));
if (a) {
a.words.unshiftObject(arg);
2018-06-15 23:03:24 +08:00
a.incrementProperty("count");
2019-01-12 00:54:23 +08:00
Ember.run.schedule("afterRender", () => {
// remove from other actions lists
let match = null;
2018-06-15 23:03:24 +08:00
this.get("adminWatchedWords.model").forEach(action => {
if (match) return;
2018-06-15 23:03:24 +08:00
if (action.nameKey !== this.get("actionNameKey")) {
match = action.words.findBy("id", arg.id);
if (match) {
action.words.removeObject(match);
2018-06-15 23:03:24 +08:00
action.decrementProperty("count");
}
}
});
});
}
},
recordRemoved(arg) {
2018-06-15 23:03:24 +08:00
const a = this.findAction(this.get("actionNameKey"));
if (a) {
a.words.removeObject(arg);
2018-06-15 23:03:24 +08:00
a.decrementProperty("count");
}
},
uploadComplete() {
WatchedWord.findAll().then(data => {
2018-06-15 23:03:24 +08:00
this.set("adminWatchedWords.model", data);
});
}
}
});