2023-02-23 10:32:53 -05:00
|
|
|
import { action } from "@ember/object";
|
|
|
|
import { classNames, tagName } from "@ember-decorators/component";
|
|
|
|
import { inject as service } from "@ember/service";
|
|
|
|
import { equal, not } from "@ember/object/computed";
|
2023-02-24 04:50:52 -05:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
|
|
|
import { observes } from "@ember-decorators/object";
|
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";
|
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
|
|
|
|
2023-02-23 10:32:53 -05:00
|
|
|
@tagName("form")
|
|
|
|
@classNames("watched-word-form")
|
|
|
|
export default class WatchedWordForm extends Component {
|
|
|
|
@service dialog;
|
|
|
|
|
|
|
|
formSubmitted = false;
|
|
|
|
actionKey = null;
|
|
|
|
showMessage = false;
|
|
|
|
selectedTags = null;
|
|
|
|
isCaseSensitive = false;
|
|
|
|
|
|
|
|
@not("word") submitDisabled;
|
|
|
|
|
|
|
|
@equal("actionKey", "replace") canReplace;
|
|
|
|
|
|
|
|
@equal("actionKey", "tag") canTag;
|
|
|
|
|
|
|
|
@equal("actionKey", "link") canLink;
|
2021-06-02 01:36:49 -04:00
|
|
|
|
|
|
|
didInsertElement() {
|
2023-02-23 10:32:53 -05:00
|
|
|
super.didInsertElement(...arguments);
|
2021-06-02 01:36:49 -04:00
|
|
|
this.set("selectedTags", []);
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2021-02-25 07:00:58 -05:00
|
|
|
|
2021-05-27 12:20:26 -04:00
|
|
|
@discourseComputed("siteSettings.watched_words_regular_expressions")
|
|
|
|
placeholderKey(watchedWordsRegularExpressions) {
|
|
|
|
if (watchedWordsRegularExpressions) {
|
|
|
|
return "admin.watched_words.form.placeholder_regexp";
|
|
|
|
} else {
|
|
|
|
return "admin.watched_words.form.placeholder";
|
|
|
|
}
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2017-09-27 15:48:57 -04:00
|
|
|
|
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
|
|
|
}
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
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
|
|
|
|
);
|
2022-08-05 06:18:17 -04:00
|
|
|
return filtered.every((content) => {
|
|
|
|
if (content.case_sensitive === true) {
|
|
|
|
return content.word !== word;
|
|
|
|
}
|
|
|
|
return content.word.toLowerCase() !== word.toLowerCase();
|
|
|
|
});
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2018-05-18 04:11:08 -04:00
|
|
|
|
2022-09-27 14:47:13 -04:00
|
|
|
focusInput() {
|
|
|
|
schedule("afterRender", () => this.element.querySelector("input").focus());
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
changeSelectedTags(tags) {
|
|
|
|
this.setProperties({
|
|
|
|
selectedTags: tags,
|
|
|
|
replacement: tags.join(","),
|
|
|
|
});
|
|
|
|
}
|
2022-09-27 14:47:13 -04:00
|
|
|
|
2023-02-23 10:32:53 -05:00
|
|
|
@action
|
|
|
|
submitForm() {
|
|
|
|
if (!this.isUniqueWord) {
|
2021-06-02 01:36:49 -04:00
|
|
|
this.setProperties({
|
2023-02-23 10:32:53 -05:00
|
|
|
showMessage: true,
|
|
|
|
message: I18n.t("admin.watched_words.form.exists"),
|
2021-06-02 01:36:49 -04:00
|
|
|
});
|
2023-02-23 10:32:53 -05:00
|
|
|
return;
|
|
|
|
}
|
2021-06-02 01:36:49 -04:00
|
|
|
|
2023-02-23 10:32:53 -05:00
|
|
|
if (!this.formSubmitted) {
|
|
|
|
this.set("formSubmitted", true);
|
2018-05-18 04:11:08 -04:00
|
|
|
|
2023-02-23 10:32:53 -05:00
|
|
|
const watchedWord = WatchedWord.create({
|
|
|
|
word: this.word,
|
|
|
|
replacement:
|
|
|
|
this.canReplace || this.canTag || this.canLink
|
|
|
|
? this.replacement
|
|
|
|
: null,
|
|
|
|
action: this.actionKey,
|
|
|
|
isCaseSensitive: this.isCaseSensitive,
|
|
|
|
});
|
2018-06-15 11:03:24 -04:00
|
|
|
|
2023-02-23 10:32:53 -05:00
|
|
|
watchedWord
|
|
|
|
.save()
|
|
|
|
.then((result) => {
|
|
|
|
this.setProperties({
|
|
|
|
word: "",
|
|
|
|
replacement: "",
|
|
|
|
formSubmitted: false,
|
|
|
|
selectedTags: [],
|
|
|
|
showMessage: true,
|
|
|
|
message: I18n.t("admin.watched_words.form.success"),
|
|
|
|
isCaseSensitive: false,
|
2018-06-15 11:03:24 -04:00
|
|
|
});
|
2023-02-23 10:32:53 -05:00
|
|
|
this.action(WatchedWord.create(result));
|
|
|
|
this.focusInput();
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
this.set("formSubmitted", false);
|
|
|
|
const message = e.jqXHR.responseJSON?.errors
|
|
|
|
? I18n.t("generic_error_with_reason", {
|
|
|
|
error: e.jqXHR.responseJSON.errors.join(". "),
|
|
|
|
})
|
|
|
|
: I18n.t("generic_error");
|
|
|
|
this.dialog.alert({
|
|
|
|
message,
|
|
|
|
didConfirm: () => this.focusInput(),
|
|
|
|
didCancel: () => this.focusInput(),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|