import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend";
import { show as showNotification } from "WoltLabSuite/Core/Ui/Notification";
import * as FormBuilderManager from "WoltLabSuite/Core/Form/Builder/Manager";
-import WoltlabCoreFile from "WoltLabSuite/Core/Component/File/woltlab-core-file";
import { fire as fireEvent } from "WoltLabSuite/Core/Event/Handler";
import { getPhrase } from "WoltLabSuite/Core/Language";
import DomUtil from "WoltLabSuite/Core/Dom/Util";
import { escapeHTML } from "WoltLabSuite/Core/StringUtil";
+import { registerCallback } from "WoltLabSuite/Core/Form/Builder/Field/Controller/FileProcessor";
+import WoltlabCoreFile from "WoltLabSuite/Core/Component/File/woltlab-core-file";
type ResponseGetForm = {
dialog: string;
};
async function editCoverPhoto(button: HTMLElement): Promise<void> {
- const defaultCoverPhoto = button.dataset.defaultCoverPhoto;
const json = (await prepareRequest(button.dataset.editCoverPhoto!).get().fetchAsJson()) as ResponseGetForm;
const dialog = dialogFactory().fromHtml(json.dialog).withoutControls();
- const coverPhotoElement = getCoverPhotoElement();
- const coverPhotoNotice = document.getElementById("coverPhotoNotice");
- const oldCoverPhoto = getCoverPhotoUrl(coverPhotoElement);
dialog.addEventListener("afterClose", () => {
- const file = dialog.querySelector<WoltlabCoreFile>("woltlab-core-file");
- const coverPhotoUrl = file?.link ?? defaultCoverPhoto ?? "";
- const coverPhotoStyle = `url("${coverPhotoUrl}")`;
-
if (FormBuilderManager.hasForm(json.formId)) {
FormBuilderManager.unregisterForm(json.formId);
}
-
- if (oldCoverPhoto === coverPhotoUrl || oldCoverPhoto === coverPhotoStyle) {
- // nothing changed
- return;
- }
-
- if (coverPhotoElement instanceof HTMLImageElement && coverPhotoUrl) {
- coverPhotoElement.src = coverPhotoUrl;
- } else {
- // ACP cover photo management
- if (!coverPhotoElement && coverPhotoUrl) {
- coverPhotoNotice!.parentElement!.appendChild(
- DomUtil.createFragmentFromHtml(
- `<div id="coverPhotoPreview" style="background-image: ${escapeHTML(coverPhotoStyle)};"></div>`,
- ),
- );
- coverPhotoNotice!.remove();
- } else if (coverPhotoElement && !coverPhotoUrl) {
- coverPhotoElement.parentElement!.appendChild(
- DomUtil.createFragmentFromHtml(
- `<woltlab-core-notice id="coverPhotoNotice" type="info">${getPhrase("wcf.user.coverPhoto.noImage")}</woltlab-core-notice>`,
- ),
- );
- coverPhotoElement.remove();
- }
- }
-
- showNotification();
- fireEvent("com.woltlab.wcf.user", "coverPhoto", {
- url: coverPhotoUrl,
- });
});
dialog.show(json.title);
}
-function getCoverPhotoUrl(coverPhotoElement: HTMLElement | null): string | undefined {
- if (coverPhotoElement instanceof HTMLImageElement) {
- return coverPhotoElement.src;
- } else {
- return coverPhotoElement?.style.backgroundImage;
- }
-}
-
function getCoverPhotoElement(): HTMLElement | null {
return (
document.querySelector<HTMLElement>(".userProfileHeader__coverPhotoImage") ??
export function setup(): void {
wheneverFirstSeen("[data-edit-cover-photo]", (button) => {
+ const defaultCoverPhoto = button.dataset.defaultCoverPhoto;
+
+ registerCallback("wcf\\action\\UserCoverPhotoAction_coverPhotoFileID", (fileId: number | undefined) => {
+ const coverPhotoElement = getCoverPhotoElement();
+
+ if (coverPhotoElement && parseInt(coverPhotoElement.dataset.objectId!) === fileId) {
+ // nothing changed
+ return;
+ }
+
+ const file = document.querySelector<WoltlabCoreFile>(
+ `#wcf\\\\action\\\\UserCoverPhotoAction_coverPhotoFileIDContainer woltlab-core-file[file-id="${fileId}"]`,
+ );
+ const coverPhotoNotice = document.getElementById("coverPhotoNotice");
+ const coverPhotoUrl = file?.link ?? defaultCoverPhoto ?? "";
+ const coverPhotoStyle = `url("${coverPhotoUrl}")`;
+
+ if (coverPhotoElement instanceof HTMLImageElement && coverPhotoUrl) {
+ coverPhotoElement.src = coverPhotoUrl;
+
+ coverPhotoElement.dataset.objectId = fileId?.toString() || "";
+ } else {
+ // ACP cover photo management
+ if (!coverPhotoElement && coverPhotoUrl) {
+ coverPhotoNotice!.parentElement!.appendChild(
+ DomUtil.createFragmentFromHtml(
+ `<div id="coverPhotoPreview" data-object-id="${fileId}" style="background-image: ${escapeHTML(coverPhotoStyle)};"></div>`,
+ ),
+ );
+ coverPhotoNotice!.remove();
+ } else if (coverPhotoElement && !coverPhotoUrl) {
+ coverPhotoElement.parentElement!.appendChild(
+ DomUtil.createFragmentFromHtml(
+ `<woltlab-core-notice id="coverPhotoNotice" type="info">${getPhrase("wcf.user.coverPhoto.noImage")}</woltlab-core-notice>`,
+ ),
+ );
+ coverPhotoElement.remove();
+ } else if (coverPhotoElement && coverPhotoUrl) {
+ coverPhotoElement.style.backgroundImage = coverPhotoStyle;
+ coverPhotoElement.dataset.objectId = fileId?.toString() || "";
+ }
+ }
+
+ showNotification();
+ fireEvent("com.woltlab.wcf.user", "coverPhoto", {
+ url: coverPhotoUrl,
+ });
+ });
+
button.addEventListener(
"click",
promiseMutex(() => editCoverPhoto(button)),
* @since 6.2
* @woltlabExcludeBundle all
*/
-define(["require", "exports", "tslib", "WoltLabSuite/Core/Helper/PromiseMutex", "WoltLabSuite/Core/Helper/Selector", "WoltLabSuite/Core/Component/Dialog", "WoltLabSuite/Core/Ajax/Backend", "WoltLabSuite/Core/Ui/Notification", "WoltLabSuite/Core/Form/Builder/Manager", "WoltLabSuite/Core/Event/Handler", "WoltLabSuite/Core/Language", "WoltLabSuite/Core/Dom/Util", "WoltLabSuite/Core/StringUtil"], function (require, exports, tslib_1, PromiseMutex_1, Selector_1, Dialog_1, Backend_1, Notification_1, FormBuilderManager, Handler_1, Language_1, Util_1, StringUtil_1) {
+define(["require", "exports", "tslib", "WoltLabSuite/Core/Helper/PromiseMutex", "WoltLabSuite/Core/Helper/Selector", "WoltLabSuite/Core/Component/Dialog", "WoltLabSuite/Core/Ajax/Backend", "WoltLabSuite/Core/Ui/Notification", "WoltLabSuite/Core/Form/Builder/Manager", "WoltLabSuite/Core/Event/Handler", "WoltLabSuite/Core/Language", "WoltLabSuite/Core/Dom/Util", "WoltLabSuite/Core/StringUtil", "WoltLabSuite/Core/Form/Builder/Field/Controller/FileProcessor"], function (require, exports, tslib_1, PromiseMutex_1, Selector_1, Dialog_1, Backend_1, Notification_1, FormBuilderManager, Handler_1, Language_1, Util_1, StringUtil_1, FileProcessor_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.setup = setup;
FormBuilderManager = tslib_1.__importStar(FormBuilderManager);
Util_1 = tslib_1.__importDefault(Util_1);
async function editCoverPhoto(button) {
- const defaultCoverPhoto = button.dataset.defaultCoverPhoto;
const json = (await (0, Backend_1.prepareRequest)(button.dataset.editCoverPhoto).get().fetchAsJson());
const dialog = (0, Dialog_1.dialogFactory)().fromHtml(json.dialog).withoutControls();
- const coverPhotoElement = getCoverPhotoElement();
- const coverPhotoNotice = document.getElementById("coverPhotoNotice");
- const oldCoverPhoto = getCoverPhotoUrl(coverPhotoElement);
dialog.addEventListener("afterClose", () => {
- const file = dialog.querySelector("woltlab-core-file");
- const coverPhotoUrl = file?.link ?? defaultCoverPhoto ?? "";
- const coverPhotoStyle = `url("${coverPhotoUrl}")`;
if (FormBuilderManager.hasForm(json.formId)) {
FormBuilderManager.unregisterForm(json.formId);
}
- if (oldCoverPhoto === coverPhotoUrl || oldCoverPhoto === coverPhotoStyle) {
- // nothing changed
- return;
- }
- if (coverPhotoElement instanceof HTMLImageElement && coverPhotoUrl) {
- coverPhotoElement.src = coverPhotoUrl;
- }
- else {
- // ACP cover photo management
- if (!coverPhotoElement && coverPhotoUrl) {
- coverPhotoNotice.parentElement.appendChild(Util_1.default.createFragmentFromHtml(`<div id="coverPhotoPreview" style="background-image: ${(0, StringUtil_1.escapeHTML)(coverPhotoStyle)};"></div>`));
- coverPhotoNotice.remove();
- }
- else if (coverPhotoElement && !coverPhotoUrl) {
- coverPhotoElement.parentElement.appendChild(Util_1.default.createFragmentFromHtml(`<woltlab-core-notice id="coverPhotoNotice" type="info">${(0, Language_1.getPhrase)("wcf.user.coverPhoto.noImage")}</woltlab-core-notice>`));
- coverPhotoElement.remove();
- }
- }
- (0, Notification_1.show)();
- (0, Handler_1.fire)("com.woltlab.wcf.user", "coverPhoto", {
- url: coverPhotoUrl,
- });
});
dialog.show(json.title);
}
- function getCoverPhotoUrl(coverPhotoElement) {
- if (coverPhotoElement instanceof HTMLImageElement) {
- return coverPhotoElement.src;
- }
- else {
- return coverPhotoElement?.style.backgroundImage;
- }
- }
function getCoverPhotoElement() {
return (document.querySelector(".userProfileHeader__coverPhotoImage") ??
document.getElementById("coverPhotoPreview"));
}
function setup() {
(0, Selector_1.wheneverFirstSeen)("[data-edit-cover-photo]", (button) => {
+ const defaultCoverPhoto = button.dataset.defaultCoverPhoto;
+ (0, FileProcessor_1.registerCallback)("wcf\\action\\UserCoverPhotoAction_coverPhotoFileID", (fileId) => {
+ const coverPhotoElement = getCoverPhotoElement();
+ if (coverPhotoElement && parseInt(coverPhotoElement.dataset.objectId) === fileId) {
+ // nothing changed
+ return;
+ }
+ const file = document.querySelector(`#wcf\\\\action\\\\UserCoverPhotoAction_coverPhotoFileIDContainer woltlab-core-file[file-id="${fileId}"]`);
+ const coverPhotoNotice = document.getElementById("coverPhotoNotice");
+ const coverPhotoUrl = file?.link ?? defaultCoverPhoto ?? "";
+ const coverPhotoStyle = `url("${coverPhotoUrl}")`;
+ if (coverPhotoElement instanceof HTMLImageElement && coverPhotoUrl) {
+ coverPhotoElement.src = coverPhotoUrl;
+ coverPhotoElement.dataset.objectId = fileId?.toString() || "";
+ }
+ else {
+ // ACP cover photo management
+ if (!coverPhotoElement && coverPhotoUrl) {
+ coverPhotoNotice.parentElement.appendChild(Util_1.default.createFragmentFromHtml(`<div id="coverPhotoPreview" data-object-id="${fileId}" style="background-image: ${(0, StringUtil_1.escapeHTML)(coverPhotoStyle)};"></div>`));
+ coverPhotoNotice.remove();
+ }
+ else if (coverPhotoElement && !coverPhotoUrl) {
+ coverPhotoElement.parentElement.appendChild(Util_1.default.createFragmentFromHtml(`<woltlab-core-notice id="coverPhotoNotice" type="info">${(0, Language_1.getPhrase)("wcf.user.coverPhoto.noImage")}</woltlab-core-notice>`));
+ coverPhotoElement.remove();
+ }
+ else if (coverPhotoElement && coverPhotoUrl) {
+ coverPhotoElement.style.backgroundImage = coverPhotoStyle;
+ coverPhotoElement.dataset.objectId = fileId?.toString() || "";
+ }
+ }
+ (0, Notification_1.show)();
+ (0, Handler_1.fire)("com.woltlab.wcf.user", "coverPhoto", {
+ url: coverPhotoUrl,
+ });
+ });
button.addEventListener("click", (0, PromiseMutex_1.promiseMutex)(() => editCoverPhoto(button)));
});
}