discourse/app/assets/javascripts/admin/components/watched-word-form.js.es6

97 lines
2.5 KiB
Plaintext
Raw Normal View History

2018-06-15 23:03:24 +08:00
import WatchedWord from "admin/models/watched-word";
import {
default as computed,
on,
observes
} from "ember-addons/ember-computed-decorators";
export default Ember.Component.extend({
2018-06-15 23:03:24 +08:00
classNames: ["watched-word-form"],
formSubmitted: false,
actionKey: null,
showMessage: false,
2018-06-15 23:03:24 +08:00
@computed("regularExpressions")
placeholderKey(regularExpressions) {
2018-06-15 23:03:24 +08:00
return (
"admin.watched_words.form.placeholder" +
(regularExpressions ? "_regexp" : "")
);
},
2018-06-15 23:03:24 +08:00
@observes("word")
removeMessage() {
2018-06-15 23:03:24 +08:00
if (this.get("showMessage") && !Ember.isEmpty(this.get("word"))) {
this.set("showMessage", false);
}
},
2018-06-15 23:03:24 +08:00
@computed("word")
isUniqueWord(word) {
const words = this.get("filteredContent") || [];
2018-06-15 23:03:24 +08:00
const filtered = words.filter(
content => content.action === this.get("actionKey")
);
return filtered.every(
content => content.word.toLowerCase() !== word.toLowerCase()
);
},
actions: {
submit() {
if (!this.get("isUniqueWord")) {
2018-06-15 23:03:24 +08:00
this.setProperties({
showMessage: true,
message: I18n.t("admin.watched_words.form.exists")
});
return;
}
2018-06-15 23:03:24 +08:00
if (!this.get("formSubmitted")) {
this.set("formSubmitted", true);
2018-06-15 23:03:24 +08:00
const watchedWord = WatchedWord.create({
word: this.get("word"),
action: this.get("actionKey")
});
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")
});
this.sendAction("action", WatchedWord.create(result));
Ember.run.schedule("afterRender", () =>
this.$(".watched-word-input").focus()
);
})
.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");
bootbox.alert(msg, () => this.$(".watched-word-input").focus());
});
}
}
},
@on("didInsertElement")
_init() {
2018-06-15 23:03:24 +08:00
Ember.run.schedule("afterRender", () => {
this.$(".watched-word-input").keydown(e => {
if (e.keyCode === 13) {
2018-06-15 23:03:24 +08:00
this.send("submit");
}
});
});
}
});