2023-02-23 10:32:53 -05:00
|
|
|
import { classNameBindings } from "@ember-decorators/component";
|
2021-04-07 09:32:05 -04:00
|
|
|
import Component from "@ember/component";
|
|
|
|
import I18n from "I18n";
|
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
|
|
|
import { emojiUrlFor } from "discourse/lib/text";
|
|
|
|
import { action, set, setProperties } from "@ember/object";
|
2022-07-16 18:50:49 -04:00
|
|
|
import { schedule } from "@ember/runloop";
|
|
|
|
import discourseLater from "discourse-common/lib/later";
|
2021-04-07 09:32:05 -04:00
|
|
|
|
2023-02-23 10:32:53 -05:00
|
|
|
@classNameBindings(":value-list", ":emoji-list")
|
|
|
|
export default class EmojiValueList extends Component {
|
|
|
|
values = null;
|
|
|
|
validationMessage = null;
|
|
|
|
emojiPickerIsActive = false;
|
|
|
|
isEditorFocused = false;
|
2021-04-07 09:32:05 -04:00
|
|
|
|
|
|
|
@discourseComputed("values")
|
|
|
|
collection(values) {
|
|
|
|
values = values || "";
|
|
|
|
|
|
|
|
return values
|
|
|
|
.split("|")
|
|
|
|
.filter(Boolean)
|
|
|
|
.map((value) => {
|
|
|
|
return {
|
|
|
|
isEditable: true,
|
|
|
|
isEditing: false,
|
|
|
|
value,
|
|
|
|
emojiUrl: emojiUrlFor(value),
|
|
|
|
};
|
|
|
|
});
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2021-04-07 09:32:05 -04:00
|
|
|
|
|
|
|
@action
|
|
|
|
closeEmojiPicker() {
|
|
|
|
this.collection.setEach("isEditing", false);
|
|
|
|
this.set("emojiPickerIsActive", false);
|
|
|
|
this.set("isEditorFocused", false);
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2021-04-07 09:32:05 -04:00
|
|
|
|
|
|
|
@action
|
|
|
|
emojiSelected(code) {
|
|
|
|
if (!this._validateInput(code)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const item = this.collection.findBy("isEditing");
|
|
|
|
if (item) {
|
|
|
|
setProperties(item, {
|
|
|
|
value: code,
|
|
|
|
emojiUrl: emojiUrlFor(code),
|
|
|
|
isEditing: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
this._saveValues();
|
|
|
|
} else {
|
|
|
|
const newCollectionValue = {
|
|
|
|
value: code,
|
|
|
|
emojiUrl: emojiUrlFor(code),
|
|
|
|
isEditable: true,
|
|
|
|
isEditing: false,
|
|
|
|
};
|
|
|
|
this.collection.addObject(newCollectionValue);
|
|
|
|
this._saveValues();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set("emojiPickerIsActive", false);
|
|
|
|
this.set("isEditorFocused", false);
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2021-04-07 09:32:05 -04:00
|
|
|
|
|
|
|
@discourseComputed("collection")
|
|
|
|
showUpDownButtons(collection) {
|
|
|
|
return collection.length - 1 ? true : false;
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2021-04-07 09:32:05 -04:00
|
|
|
|
|
|
|
_splitValues(values) {
|
|
|
|
if (values && values.length) {
|
|
|
|
const emojiList = [];
|
|
|
|
const emojis = values.split("|").filter(Boolean);
|
|
|
|
emojis.forEach((emojiName) => {
|
|
|
|
const emoji = {
|
|
|
|
isEditable: true,
|
|
|
|
isEditing: false,
|
|
|
|
};
|
|
|
|
emoji.value = emojiName;
|
|
|
|
emoji.emojiUrl = emojiUrlFor(emojiName);
|
|
|
|
|
|
|
|
emojiList.push(emoji);
|
|
|
|
});
|
|
|
|
|
|
|
|
return emojiList;
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2021-04-07 09:32:05 -04:00
|
|
|
|
|
|
|
@action
|
|
|
|
editValue(index) {
|
|
|
|
this.closeEmojiPicker();
|
|
|
|
schedule("afterRender", () => {
|
|
|
|
if (parseInt(index, 10) >= 0) {
|
|
|
|
const item = this.collection[index];
|
|
|
|
if (item.isEditable) {
|
|
|
|
set(item, "isEditing", true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set("isEditorFocused", true);
|
2022-07-16 18:50:49 -04:00
|
|
|
discourseLater(() => {
|
2021-04-07 09:32:05 -04:00
|
|
|
if (this.element && !this.isDestroying && !this.isDestroyed) {
|
|
|
|
this.set("emojiPickerIsActive", true);
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
});
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2021-04-07 09:32:05 -04:00
|
|
|
|
|
|
|
@action
|
|
|
|
removeValue(value) {
|
|
|
|
this._removeValue(value);
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2021-04-07 09:32:05 -04:00
|
|
|
|
|
|
|
@action
|
|
|
|
shift(operation, index) {
|
|
|
|
let futureIndex = index + operation;
|
|
|
|
|
|
|
|
if (futureIndex > this.collection.length - 1) {
|
|
|
|
futureIndex = 0;
|
|
|
|
} else if (futureIndex < 0) {
|
|
|
|
futureIndex = this.collection.length - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const shiftedEmoji = this.collection[index];
|
|
|
|
this.collection.removeAt(index);
|
|
|
|
this.collection.insertAt(futureIndex, shiftedEmoji);
|
|
|
|
|
|
|
|
this._saveValues();
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2021-04-07 09:32:05 -04:00
|
|
|
|
|
|
|
_validateInput(input) {
|
|
|
|
this.set("validationMessage", null);
|
|
|
|
|
|
|
|
if (!emojiUrlFor(input)) {
|
|
|
|
this.set(
|
|
|
|
"validationMessage",
|
|
|
|
I18n.t("admin.site_settings.emoji_list.invalid_input")
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2021-04-07 09:32:05 -04:00
|
|
|
|
|
|
|
_removeValue(value) {
|
|
|
|
this.collection.removeObject(value);
|
|
|
|
this._saveValues();
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2021-04-07 09:32:05 -04:00
|
|
|
|
|
|
|
_replaceValue(index, newValue) {
|
|
|
|
const item = this.collection[index];
|
|
|
|
if (item.value === newValue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
set(item, "value", newValue);
|
|
|
|
this._saveValues();
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2021-04-07 09:32:05 -04:00
|
|
|
|
|
|
|
_saveValues() {
|
|
|
|
this.set("values", this.collection.mapBy("value").join("|"));
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
|
|
|
}
|