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