2018-06-15 23:03:24 +08:00
|
|
|
import computed from "ember-addons/ember-computed-decorators";
|
|
|
|
import WatchedWord from "admin/models/watched-word";
|
2019-07-22 19:59:56 +08:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
import { fmt } from "discourse/lib/computed";
|
2019-08-02 17:53:03 +08:00
|
|
|
import showModal from "discourse/lib/show-modal";
|
2017-06-29 04:56:44 +08:00
|
|
|
|
|
|
|
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"
|
|
|
|
),
|
2019-07-22 19:59:56 +08:00
|
|
|
downloadLink: fmt(
|
|
|
|
"actionNameKey",
|
|
|
|
"/admin/logs/watched_words/action/%@/download"
|
|
|
|
),
|
2017-06-29 04:56:44 +08:00
|
|
|
|
|
|
|
findAction(actionName) {
|
2018-06-15 23:03:24 +08:00
|
|
|
return (this.get("adminWatchedWords.model") || []).findBy(
|
|
|
|
"nameKey",
|
|
|
|
actionName
|
|
|
|
);
|
2017-06-29 04:56:44 +08:00
|
|
|
},
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
@computed("actionNameKey", "adminWatchedWords.model")
|
2019-07-22 19:59:56 +08:00
|
|
|
currentAction(actionName) {
|
|
|
|
return this.findAction(actionName);
|
|
|
|
},
|
2017-06-29 04:56:44 +08:00
|
|
|
|
2019-07-22 19:59:56 +08:00
|
|
|
@computed("currentAction.words.[]", "adminWatchedWords.model")
|
|
|
|
filteredContent(words) {
|
|
|
|
return words || [];
|
2017-06-29 04:56:44 +08:00
|
|
|
},
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
@computed("actionNameKey")
|
2017-06-29 04:56:44 +08:00
|
|
|
actionDescription(actionNameKey) {
|
2018-06-15 23:03:24 +08:00
|
|
|
return I18n.t("admin.watched_words.action_descriptions." + actionNameKey);
|
2017-06-29 04:56:44 +08:00
|
|
|
},
|
|
|
|
|
2019-07-22 19:59:56 +08:00
|
|
|
@computed("currentAction.count")
|
|
|
|
wordCount(count) {
|
|
|
|
return count || 0;
|
2018-03-28 05:26:53 +08:00
|
|
|
},
|
|
|
|
|
2017-06-29 04:56:44 +08:00
|
|
|
actions: {
|
|
|
|
recordAdded(arg) {
|
2019-05-27 16:15:39 +08:00
|
|
|
const a = this.findAction(this.actionNameKey);
|
2017-06-29 04:56:44 +08:00
|
|
|
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", () => {
|
2017-06-29 04:56:44 +08:00
|
|
|
// remove from other actions lists
|
|
|
|
let match = null;
|
2018-06-15 23:03:24 +08:00
|
|
|
this.get("adminWatchedWords.model").forEach(action => {
|
2017-06-29 04:56:44 +08:00
|
|
|
if (match) return;
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
if (action.nameKey !== this.actionNameKey) {
|
2018-06-15 23:03:24 +08:00
|
|
|
match = action.words.findBy("id", arg.id);
|
2017-06-29 04:56:44 +08:00
|
|
|
if (match) {
|
|
|
|
action.words.removeObject(match);
|
2018-06-15 23:03:24 +08:00
|
|
|
action.decrementProperty("count");
|
2017-06-29 04:56:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
recordRemoved(arg) {
|
2019-07-22 19:59:56 +08:00
|
|
|
if (this.currentAction) {
|
|
|
|
this.currentAction.words.removeObject(arg);
|
|
|
|
this.currentAction.decrementProperty("count");
|
2017-06-29 04:56:44 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
uploadComplete() {
|
|
|
|
WatchedWord.findAll().then(data => {
|
2018-06-15 23:03:24 +08:00
|
|
|
this.set("adminWatchedWords.model", data);
|
2017-06-29 04:56:44 +08:00
|
|
|
});
|
2019-07-22 19:59:56 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2019-08-02 17:53:03 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
test() {
|
|
|
|
WatchedWord.findAll().then(data => {
|
|
|
|
this.set("adminWatchedWords.model", data);
|
|
|
|
showModal("admin-watched-word-test", {
|
|
|
|
admin: true,
|
|
|
|
model: this.currentAction
|
|
|
|
});
|
|
|
|
});
|
2017-06-29 04:56:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|