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