2019-11-01 01:37:24 +08:00
|
|
|
import { isEmpty } from "@ember/utils";
|
2019-10-30 21:48:24 +08:00
|
|
|
import { schedule } from "@ember/runloop";
|
2019-10-24 00:30:52 +08:00
|
|
|
import Component from "@ember/component";
|
2018-06-15 23:03:24 +08:00
|
|
|
import WatchedWord from "admin/models/watched-word";
|
2020-01-17 01:56:53 +08:00
|
|
|
import discourseComputed, {
|
2018-06-15 23:03:24 +08:00
|
|
|
on,
|
|
|
|
observes
|
2019-11-08 05:38:28 +08:00
|
|
|
} from "discourse-common/utils/decorators";
|
2017-06-29 04:56:44 +08:00
|
|
|
|
2019-10-24 00:30:52 +08:00
|
|
|
export default Component.extend({
|
2018-06-15 23:03:24 +08:00
|
|
|
classNames: ["watched-word-form"],
|
2017-06-29 04:56:44 +08:00
|
|
|
formSubmitted: false,
|
|
|
|
actionKey: null,
|
2018-05-18 16:11:08 +08:00
|
|
|
showMessage: false,
|
2017-06-29 04:56:44 +08:00
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("regularExpressions")
|
2017-09-28 03:48:57 +08:00
|
|
|
placeholderKey(regularExpressions) {
|
2018-06-15 23:03:24 +08:00
|
|
|
return (
|
|
|
|
"admin.watched_words.form.placeholder" +
|
|
|
|
(regularExpressions ? "_regexp" : "")
|
|
|
|
);
|
2017-09-28 03:48:57 +08:00
|
|
|
},
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
@observes("word")
|
2018-05-18 16:11:08 +08:00
|
|
|
removeMessage() {
|
2019-11-01 01:37:24 +08:00
|
|
|
if (this.showMessage && !isEmpty(this.word)) {
|
2018-06-15 23:03:24 +08:00
|
|
|
this.set("showMessage", false);
|
2017-06-29 04:56:44 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("word")
|
2018-05-18 16:11:08 +08:00
|
|
|
isUniqueWord(word) {
|
2019-05-27 16:15:39 +08:00
|
|
|
const words = this.filteredContent || [];
|
2019-05-27 16:42:53 +08:00
|
|
|
const filtered = words.filter(content => content.action === this.actionKey);
|
2018-06-15 23:03:24 +08:00
|
|
|
return filtered.every(
|
|
|
|
content => content.word.toLowerCase() !== word.toLowerCase()
|
|
|
|
);
|
2018-05-18 16:11:08 +08:00
|
|
|
},
|
|
|
|
|
2017-06-29 04:56:44 +08:00
|
|
|
actions: {
|
|
|
|
submit() {
|
2019-05-27 16:15:39 +08:00
|
|
|
if (!this.isUniqueWord) {
|
2018-06-15 23:03:24 +08:00
|
|
|
this.setProperties({
|
|
|
|
showMessage: true,
|
|
|
|
message: I18n.t("admin.watched_words.form.exists")
|
|
|
|
});
|
2018-05-18 16:11:08 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
if (!this.formSubmitted) {
|
2018-06-15 23:03:24 +08:00
|
|
|
this.set("formSubmitted", true);
|
2017-06-29 04:56:44 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
const watchedWord = WatchedWord.create({
|
2019-05-27 16:15:39 +08:00
|
|
|
word: this.word,
|
|
|
|
action: this.actionKey
|
2017-06-29 04:56:44 +08:00
|
|
|
});
|
2018-06-15 23:03:24 +08:00
|
|
|
|
|
|
|
watchedWord
|
|
|
|
.save()
|
|
|
|
.then(result => {
|
|
|
|
this.setProperties({
|
|
|
|
word: "",
|
|
|
|
formSubmitted: false,
|
|
|
|
showMessage: true,
|
|
|
|
message: I18n.t("admin.watched_words.form.success")
|
|
|
|
});
|
2019-01-10 18:06:01 +08:00
|
|
|
this.action(WatchedWord.create(result));
|
2019-10-30 21:48:24 +08:00
|
|
|
schedule("afterRender", () =>
|
2019-07-16 18:45:15 +08:00
|
|
|
this.element.querySelector(".watched-word-input").focus()
|
2018-06-15 23:03:24 +08:00
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
this.set("formSubmitted", false);
|
|
|
|
const msg =
|
|
|
|
e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors
|
|
|
|
? I18n.t("generic_error_with_reason", {
|
|
|
|
error: e.jqXHR.responseJSON.errors.join(". ")
|
|
|
|
})
|
|
|
|
: I18n.t("generic_error");
|
2019-07-16 18:45:15 +08:00
|
|
|
bootbox.alert(msg, () =>
|
|
|
|
this.element.querySelector(".watched-word-input").focus()
|
|
|
|
);
|
2018-06-15 23:03:24 +08:00
|
|
|
});
|
2017-06-29 04:56:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
@on("didInsertElement")
|
|
|
|
_init() {
|
2019-10-30 21:48:24 +08:00
|
|
|
schedule("afterRender", () => {
|
2019-07-16 18:45:15 +08:00
|
|
|
$(this.element.querySelector(".watched-word-input")).keydown(e => {
|
2017-06-29 04:56:44 +08:00
|
|
|
if (e.keyCode === 13) {
|
2018-06-15 23:03:24 +08:00
|
|
|
this.send("submit");
|
2017-06-29 04:56:44 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|