2020-03-07 00:41:41 +08:00
|
|
|
import Controller, { inject as controller } from "@ember/controller";
|
2020-05-14 04:23:41 +08:00
|
|
|
import I18n from "I18n";
|
2017-06-29 04:56:44 +08:00
|
|
|
import WatchedWord from "admin/models/watched-word";
|
2019-07-22 19:59:56 +08:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2020-08-27 00:57:13 +08:00
|
|
|
import bootbox from "bootbox";
|
2019-11-08 05:38:28 +08:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2019-07-22 19:59:56 +08:00
|
|
|
import { fmt } from "discourse/lib/computed";
|
2019-10-31 04:28:29 +08:00
|
|
|
import { or } from "@ember/object/computed";
|
2019-10-30 21:48:24 +08:00
|
|
|
import { schedule } from "@ember/runloop";
|
2019-08-02 17:53:03 +08:00
|
|
|
import showModal from "discourse/lib/show-modal";
|
2017-06-29 04:56:44 +08:00
|
|
|
|
2019-10-24 01:06:54 +08:00
|
|
|
export default Controller.extend({
|
2020-03-07 00:41:41 +08:00
|
|
|
adminWatchedWords: controller(),
|
2017-06-29 04:56:44 +08:00
|
|
|
actionNameKey: null,
|
2019-07-22 19:59:56 +08:00
|
|
|
downloadLink: fmt(
|
|
|
|
"actionNameKey",
|
2021-04-01 14:14:17 +08:00
|
|
|
"/admin/customize/watched_words/action/%@/download"
|
2019-07-22 19:59:56 +08:00
|
|
|
),
|
2021-05-28 00:20:26 +08:00
|
|
|
showWordsList: or("adminWatchedWords.showWords", "adminWatchedWords.filter"),
|
2017-06-29 04:56:44 +08:00
|
|
|
|
|
|
|
findAction(actionName) {
|
2021-05-28 00:20:26 +08:00
|
|
|
return (this.adminWatchedWords.model || []).findBy("nameKey", actionName);
|
2017-06-29 04:56:44 +08:00
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("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
|
|
|
|
2021-05-28 00:42:43 +08:00
|
|
|
@discourseComputed("currentAction.words.[]")
|
|
|
|
regexpError(words) {
|
|
|
|
for (const { regexp, word } of words) {
|
|
|
|
try {
|
|
|
|
RegExp(regexp);
|
|
|
|
} catch {
|
|
|
|
return I18n.t("admin.watched_words.invalid_regex", { word });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("actionNameKey")
|
2017-06-29 04:56:44 +08:00
|
|
|
actionDescription(actionNameKey) {
|
|
|
|
return I18n.t("admin.watched_words.action_descriptions." + actionNameKey);
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
recordAdded(arg) {
|
2021-05-28 00:20:26 +08:00
|
|
|
const action = this.findAction(this.actionNameKey);
|
|
|
|
if (!action) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-29 04:56:44 +08:00
|
|
|
|
2021-05-28 00:20:26 +08:00
|
|
|
action.words.unshiftObject(arg);
|
|
|
|
schedule("afterRender", () => {
|
|
|
|
// remove from other actions lists
|
|
|
|
let match = null;
|
|
|
|
this.adminWatchedWords.model.forEach((otherAction) => {
|
|
|
|
if (match) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (otherAction.nameKey !== this.actionNameKey) {
|
|
|
|
match = otherAction.words.findBy("id", arg.id);
|
|
|
|
if (match) {
|
|
|
|
otherAction.words.removeObject(match);
|
2017-06-29 04:56:44 +08:00
|
|
|
}
|
2021-05-28 00:20:26 +08:00
|
|
|
}
|
2017-06-29 04:56:44 +08:00
|
|
|
});
|
2021-05-28 00:20:26 +08:00
|
|
|
});
|
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);
|
2017-06-29 04:56:44 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
uploadComplete() {
|
|
|
|
WatchedWord.findAll().then((data) => {
|
2021-05-28 00:20:26 +08:00
|
|
|
this.adminWatchedWords.set("model", data);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
test() {
|
|
|
|
WatchedWord.findAll().then((data) => {
|
|
|
|
this.adminWatchedWords.set("model", data);
|
|
|
|
showModal("admin-watched-word-test", {
|
|
|
|
admin: true,
|
|
|
|
model: this.currentAction,
|
|
|
|
});
|
2017-06-29 04:56:44 +08:00
|
|
|
});
|
2019-07-22 19:59:56 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
clearAll() {
|
|
|
|
const actionKey = this.actionNameKey;
|
|
|
|
bootbox.confirm(
|
2021-03-10 20:48:13 +08:00
|
|
|
I18n.t("admin.watched_words.clear_all_confirm", {
|
|
|
|
action: I18n.t("admin.watched_words.actions." + actionKey),
|
|
|
|
}),
|
2019-07-22 19:59:56 +08:00
|
|
|
I18n.t("no_value"),
|
|
|
|
I18n.t("yes_value"),
|
|
|
|
(result) => {
|
|
|
|
if (result) {
|
2021-04-01 14:14:17 +08:00
|
|
|
ajax(`/admin/customize/watched_words/action/${actionKey}.json`, {
|
2020-03-27 04:00:10 +08:00
|
|
|
type: "DELETE",
|
2019-07-22 19:59:56 +08:00
|
|
|
}).then(() => {
|
|
|
|
const action = this.findAction(actionKey);
|
|
|
|
if (action) {
|
2021-05-28 00:20:26 +08:00
|
|
|
action.set("words", []);
|
2019-07-22 19:59:56 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2019-08-02 17:53:03 +08:00
|
|
|
},
|
2017-06-29 04:56:44 +08:00
|
|
|
},
|
|
|
|
});
|