+ $(clickToHideElement).click(function() {
+ $(blockElement).slideUp();
+ $(clickToHideElement).addClass("hidden");
+ $(clickToShowElement).removeClass("hidden");
+ });
+ }
+
+ /* ID of the image currently being edited. */
+ let editingImageId = null;
+
+ /**
+ * Shows the form for editing an image.
+ *
+ * @param imageId The ID of the image to edit.
+ */
+ function editImage(imageId) {
+ if (editingImageId !== imageId) {
+ cancelImageEditing();
+ } else {
+ return;
+ }
+ editingImageId = imageId;
+ $(".show-data", getImage(imageId)).hide();
+ $(".edit-data", getImage(imageId)).show();
+ $(document).bind("click.sone", function(event) {
+ if ($(event.target).closest("#image-" + imageId).size() === 0) {
+ cancelImageEditing();
+ }
+ });
+ }
+
+ /**
+ * Cancels all image editing.
+ */
+ function cancelImageEditing() {
+ $(".image .show-data").show();
+ $(".image .edit-data").hide();
+ $("form.edit-image").each(function() {
+ this.reset();
+ });
+ $(document).unbind("click.sone");
+ editingImageId = null;
+ }
+
+ /**
+ * Returns the image element with the given ID.
+ *
+ * @param imageId The ID of the image
+ * @return The image element
+ */
+ function getImage(imageId) {
+ return $("#sone .image .image-id:contains('" + imageId + "')").closest(".image");
+ }
+
+ /**
+ * Swaps two images.
+ *
+ * @param sourceId The ID of the source image
+ * @param destinationId The ID of the destionation image
+ */
+ function swapImage(sourceId, destinationId) {
+ const sourceElement = getImage(sourceId);
+ const destinationElement = getImage(destinationId);
+ const sourceParent = sourceElement.closest(".image-row");
+ const sourcePrevSibling = sourceElement.prev();
+ sourceElement.detach();
+ destinationElement.before(sourceElement);
+ if (sourcePrevSibling.get(0) !== destinationElement.get(0)) {
+ destinationElement.detach();
+ (sourcePrevSibling.size() > 0) ? sourcePrevSibling.after(destinationElement) : sourceParent.prepend(destinationElement);
+ }
+ if ($("button[name='moveLeft']", sourceElement).hasClass("hidden") !== $("button[name='moveLeft']", destinationElement).hasClass("hidden")) {
+ $("button[name='moveLeft']", sourceElement).toggleClass("hidden");
+ $("button[name='moveLeft']", destinationElement).toggleClass("hidden");
+ }
+ if ($("button[name='moveRight']", sourceElement).hasClass("hidden") !== $("button[name='moveRight']", destinationElement).hasClass("hidden")) {
+ $("button[name='moveRight']", sourceElement).toggleClass("hidden");
+ $("button[name='moveRight']", destinationElement).toggleClass("hidden");
+ }
+ }
+
+ /**
+ * Prepare all images for inline editing.
+ */
+ function prepareImages() {
+ $(".image").each(function() {
+ const imageId = $(this).closest(".image").find(".image-id").text();
+ (function(element, imageId) {
+ $(".show-data", element).click(function() {
+ editImage(imageId);
+ });
+ $("button[name='moveLeft'], button[name='moveRight']", element).click(function() {
+ ajaxGet("editImage.ajax", { "formPassword": getFormPassword(), "image": imageId, "moveLeft": this.name === "moveLeft", "moveRight": this.name === "moveRight" }, function(data) {
+ if (data && data.success) {
+ swapImage(data.sourceImageId, data.destinationImageId);
+ }
+ });
+ return false;
+ });
+ $("button[name='submit']", element).click(function() {
+ const title = $(":input[name='title']:enabled", this.form).val();
+ const description = $(":input[name='description']:enabled", this.form).val();
+ ajaxGet("editImage.ajax", { "formPassword": getFormPassword(), "image": imageId, "title": title, "description": description }, function(data) {
+ const imageElement = getImage(data.imageId);
+ const imageTitleInput = imageElement.find(":input[name='title']");
+ const imageDescriptionInput = imageElement.find(":input[name='description']");
+ if (data && data.success) {
+ imageElement.find(".image-title").text(data.title);
+ imageElement.find(".image-description").html(data.parsedDescription);
+ imageTitleInput.prop("defaultValue", data.title);
+ imageDescriptionInput.prop("defaultValue", data.description);
+ cancelImageEditing();
+ } else if (data && !data.success) {
+ imageTitleInput.prop("value", imageTitleInput.prop("defaultValue"));
+ imageDescriptionInput.prop("value", imageDescriptionInput.prop("defaultValue"));
+ cancelImageEditing();
+ }
+ });
+ return false;
+ });
+ })(this, imageId);
+ });
+ }
+
+ /* ID of the album currently being edited. */
+ let editingAlbumId = null;
+
+ /**
+ * Shows the form for editing an album.
+ *
+ * @param albumId The ID of the album to edit.
+ */
+ function editAlbum(albumId) {
+ if (editingAlbumId !== albumId) {
+ if (editingAlbumId != null) {
+ cancelAlbumEditing();
+ }
+ } else {
+ console.log("already editing " + albumId);
+ return;
+ }
+ editingAlbumId = albumId;
+ $(".show-data", getAlbum(albumId)).hide();
+ $(".edit-data", getAlbum(albumId)).show();
+ console.log(getAlbum(albumId));
+ $(document).bind("click.sone", function(event) {
+ if ($(event.target).closest("#album-" + albumId).size() === 0) {
+ cancelAlbumEditing();
+ }