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

122 lines
3.2 KiB
Plaintext
Raw Normal View History

import { or } from "@ember/object/computed";
import { schedule } from "@ember/runloop";
2019-10-30 01:31:44 +08:00
import { inject } from "@ember/controller";
import Controller from "@ember/controller";
2018-06-15 23:03:24 +08:00
import computed from "ember-addons/ember-computed-decorators";
import WatchedWord from "admin/models/watched-word";
import { ajax } from "discourse/lib/ajax";
import { fmt } from "discourse/lib/computed";
import showModal from "discourse/lib/show-modal";
export default Controller.extend({
actionNameKey: null,
adminWatchedWords: inject(),
showWordsList: or(
2018-06-15 23:03:24 +08:00
"adminWatchedWords.filtered",
"adminWatchedWords.showWords"
),
downloadLink: fmt(
"actionNameKey",
"/admin/logs/watched_words/action/%@/download"
),
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")
currentAction(actionName) {
return this.findAction(actionName);
},
@computed("currentAction.words.[]", "adminWatchedWords.model")
filteredContent(words) {
return 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);
},
@computed("currentAction.count")
wordCount(count) {
return count || 0;
},
actions: {
recordAdded(arg) {
const a = this.findAction(this.actionNameKey);
if (a) {
a.words.unshiftObject(arg);
2018-06-15 23:03:24 +08:00
a.incrementProperty("count");
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;
if (action.nameKey !== this.actionNameKey) {
2018-06-15 23:03:24 +08:00
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) {
if (this.currentAction) {
this.currentAction.words.removeObject(arg);
this.currentAction.decrementProperty("count");
}
},
uploadComplete() {
WatchedWord.findAll().then(data => {
2018-06-15 23:03:24 +08:00
this.set("adminWatchedWords.model", data);
});
},
clearAll() {
const actionKey = this.actionNameKey;
bootbox.confirm(
I18n.t(`admin.watched_words.clear_all_confirm_${actionKey}`),
I18n.t("no_value"),
I18n.t("yes_value"),
result => {
if (result) {
ajax(`/admin/logs/watched_words/action/${actionKey}.json`, {
method: "DELETE"
}).then(() => {
const action = this.findAction(actionKey);
if (action) {
action.setProperties({
words: [],
count: 0
});
}
});
}
}
);
},
test() {
WatchedWord.findAll().then(data => {
this.set("adminWatchedWords.model", data);
showModal("admin-watched-word-test", {
admin: true,
model: this.currentAction
});
});
}
}
});