2020-04-03 16:01:53 +00:00
|
|
|
/**
|
|
|
|
* Emojies settings sections
|
|
|
|
*
|
|
|
|
* @author Pierre Hubert
|
|
|
|
*/
|
|
|
|
|
|
|
|
class EmojiesSection {
|
|
|
|
|
|
|
|
|
|
|
|
static Open(args, target) {
|
|
|
|
|
|
|
|
//Create a box
|
|
|
|
const box = createElem2({
|
|
|
|
appendTo: target,
|
|
|
|
type: "div",
|
|
|
|
class: "box box-primary box-emojis-settings"
|
|
|
|
});
|
|
|
|
|
|
|
|
//Add box header
|
|
|
|
const boxHead = createElem2({
|
|
|
|
appendTo: box,
|
|
|
|
type: "div",
|
|
|
|
class: "box-header",
|
|
|
|
});
|
|
|
|
const boxTitle = createElem2({
|
|
|
|
appendTo: boxHead,
|
|
|
|
type: "h3",
|
|
|
|
class: "box-title",
|
|
|
|
innerHTML: "Custom emojis settings"
|
|
|
|
});
|
|
|
|
|
|
|
|
//Create box body
|
|
|
|
const boxBody = createElem2({
|
|
|
|
appendTo: box,
|
|
|
|
type: "div",
|
|
|
|
class: "box-body"
|
|
|
|
});
|
|
|
|
|
|
|
|
const emojiesList = createElem2({
|
|
|
|
appendTo: boxBody,
|
|
|
|
type: "table",
|
|
|
|
class: "table table-hover",
|
|
|
|
innerHTML: "<tbody />"
|
|
|
|
})
|
|
|
|
|
2020-04-03 16:32:11 +00:00
|
|
|
const refreshFunction = () => this.RefreshList(emojiesList.querySelector("tbody"))
|
|
|
|
refreshFunction();
|
|
|
|
|
|
|
|
// Add create form
|
|
|
|
this.AddUploadForm(target, refreshFunction)
|
2020-04-03 16:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {HTMLElement} target
|
|
|
|
*/
|
|
|
|
static async RefreshList(target) {
|
|
|
|
try {
|
|
|
|
target.innerHTML = "";
|
|
|
|
|
|
|
|
const emojies = (await userInfo(userID(), true)).customEmojis;
|
|
|
|
|
|
|
|
if(emojies.length == 0) {
|
2020-04-03 16:02:43 +00:00
|
|
|
target.innerHTML = "<tr><td>There is no custom emojis yet</td></tr>"
|
2020-04-03 16:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for(const e of emojies) {
|
|
|
|
|
|
|
|
const line = createElem2({
|
|
|
|
type: "tr",
|
|
|
|
appendTo: target,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
line.innerHTML += "<td><img class='e' src='"+e.url+"' /></td>" +
|
2020-04-03 16:43:23 +00:00
|
|
|
"<td>"+e.shorcut+"</td><td></td>"
|
2020-04-03 16:01:53 +00:00
|
|
|
|
|
|
|
|
2020-04-03 16:43:23 +00:00
|
|
|
// Add delete button
|
|
|
|
const deleteBtnTarget = line.querySelector("td:last-of-type")
|
2020-04-03 16:01:53 +00:00
|
|
|
|
2020-04-03 16:43:23 +00:00
|
|
|
const deleteButtonLink = createElem2({
|
|
|
|
appendTo: deleteBtnTarget,
|
|
|
|
type: "a",
|
|
|
|
innerHTML: "<i class='fa fa-trash'></i>"
|
|
|
|
});
|
|
|
|
|
|
|
|
deleteButtonLink.addEventListener("click", () => {
|
|
|
|
|
|
|
|
ComunicWeb.common.messages.confirm("Do you really want to delete this emoji ?", async (confirm) => {
|
|
|
|
if(!confirm)
|
|
|
|
return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await SettingsInterface.deleteEmoji(e.id)
|
|
|
|
|
|
|
|
this.RefreshList(target);
|
|
|
|
} catch(e) {
|
|
|
|
console.error(e);
|
|
|
|
notify("Could not delete emoji!", "danger")
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
2020-04-03 16:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
target.appendChild(ComunicWeb.common.messages.createCalloutElem(
|
|
|
|
"Error",
|
|
|
|
"Could not refresh the list of emojis!",
|
|
|
|
"danger"
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 16:32:11 +00:00
|
|
|
/**
|
|
|
|
* Add a form to upload new custom emojies
|
|
|
|
*
|
|
|
|
* @param {HTMLElment} target The target for the form
|
|
|
|
* @param cb Callback function to call in case of success
|
|
|
|
*/
|
|
|
|
static AddUploadForm(target, cb) {
|
|
|
|
|
|
|
|
//Create a box
|
|
|
|
const box = createElem2({
|
|
|
|
appendTo: target,
|
|
|
|
type: "div",
|
|
|
|
class: "box box-primary box-emojis-settings"
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create the form
|
|
|
|
const form = createElem2({
|
|
|
|
appendTo: box,
|
|
|
|
type: "form",
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
//Add box header
|
|
|
|
const boxHead = createElem2({
|
|
|
|
appendTo: form,
|
|
|
|
type: "div",
|
|
|
|
class: "box-header",
|
|
|
|
});
|
|
|
|
const boxTitle = createElem2({
|
|
|
|
appendTo: boxHead,
|
|
|
|
type: "h3",
|
|
|
|
class: "box-title",
|
|
|
|
innerHTML: "Add a new custom emoji"
|
|
|
|
});
|
|
|
|
|
|
|
|
//Create box body
|
|
|
|
const boxBody = createElem2({
|
|
|
|
appendTo: form,
|
|
|
|
type: "div",
|
|
|
|
class: "box-body"
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const shorcutInput = createFormGroup({
|
|
|
|
target: boxBody,
|
|
|
|
label: "Shorcut (starting and ending with a semicolon)",
|
|
|
|
placeholder: ":myEmoticon:",
|
|
|
|
type: "text",
|
|
|
|
name: "shorcut"
|
|
|
|
})
|
|
|
|
|
|
|
|
const associatedImage = createFormGroup({
|
|
|
|
target: boxBody,
|
|
|
|
label: "Associated image",
|
|
|
|
type: "file"
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// Create box footer
|
|
|
|
const boxFooter = createElem2({
|
|
|
|
appendTo: form,
|
|
|
|
type: "div",
|
|
|
|
class: "box-footer"
|
|
|
|
})
|
|
|
|
|
|
|
|
const submitButton = createElem2({
|
|
|
|
appendTo: boxFooter,
|
|
|
|
type: "input",
|
|
|
|
elemType: "submit",
|
|
|
|
value: "Upload custom emoji",
|
|
|
|
class: "btn btn-primary"
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
form.addEventListener("submit", async (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
try {
|
|
|
|
const shorcut = shorcutInput.value
|
|
|
|
if(!checkEmojiCode(shorcut)) {
|
|
|
|
return notify("Invalid shorcut!", "danger");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(associatedImage.files.length != 1) {
|
|
|
|
return notify("Please specify a file to upload!", "danger")
|
|
|
|
}
|
|
|
|
|
|
|
|
const fd = new FormData()
|
|
|
|
fd.append("shorcut", shorcut)
|
|
|
|
fd.append("image", associatedImage.files[0])
|
|
|
|
|
2020-04-03 16:43:23 +00:00
|
|
|
await SettingsInterface.uploadEmoji(fd)
|
2020-04-03 16:32:11 +00:00
|
|
|
|
|
|
|
cb()
|
|
|
|
|
2020-04-03 18:32:47 +00:00
|
|
|
// Recreate form
|
|
|
|
box.remove();
|
|
|
|
this.AddUploadForm(target, cb);
|
|
|
|
|
2020-04-03 16:32:11 +00:00
|
|
|
} catch(e) {
|
|
|
|
console.error(e);
|
|
|
|
notify("Could not upload new emoji!", "danger");
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-04-03 16:01:53 +00:00
|
|
|
}
|