friendica/view/theme/frio/js/filebrowser.js

265 lines
7.7 KiB
JavaScript
Raw Normal View History

// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPLv3-or-later
2019-07-29 22:41:19 +02:00
/**
* Filebrowser - Friendica Communications Server
*
2021-03-29 08:40:20 +02:00
* Copyright (c) 2010-2021, the Friendica project
2019-07-29 22:41:19 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This code handle user interaction for image/file upload/browser dialog.
* Is loaded from filebrowser_plain.tpl
*
* To load filebrowser in colorbox, call
*
* Dialog.doImageBrowser(eventname, id);
*
* or
*
* Dialog.doFileBrowser(eventname, id);
*
* where:
*
* eventname: event name to catch return value
* id: id returned to event handler
*
* When user select an item, an event in fired in parent page, on body element
* The event is named
*
* fbrowser.<type>.[<eventname>]
*
* <type> will be one of "image" or "file", and the event handler will
* get the following params:
*
* filemane: filename of item choosed by user
* embed: bbcode to embed element into posts
* id: id from caller code
*
* example:
*
* // open dialog for select an image for a textarea with id "myeditor"
* var id="myeditor";
* Dialog.doImageBrowser("example", id);
*
* // setup event handler to get user selection
* $("body").on("fbrowser.image.example", function(event, filename, bbcode, id) {
* // close colorbox
* $.colorbox.close();
* // replace textxarea text with bbcode
* $(id).value = bbcode;
* });
**/
/*
* IMPORTANT
*
* This is a modified version to work with
* the frio theme.and bootstrap modals
*
* The origninal file is under:
* js/filebrowser.js
*
*/
var FileBrowser = {
2021-01-22 14:38:44 +01:00
nickname: "",
type: "",
2019-07-29 22:41:19 +02:00
event: "",
folder: "",
2021-01-22 14:38:44 +01:00
id: null,
2019-07-29 22:41:19 +02:00
2021-01-22 14:38:44 +01:00
init: function (nickname, type, hash) {
2019-07-29 22:41:19 +02:00
FileBrowser.nickname = nickname;
FileBrowser.type = type;
2021-01-22 14:38:44 +01:00
FileBrowser.event = "fbrowser." + type;
2019-07-29 22:41:19 +02:00
2021-01-22 14:38:44 +01:00
if (hash !== "") {
var h = hash.replace("#", "");
2019-07-29 22:41:19 +02:00
var destination = h.split("-")[0];
FileBrowser.id = h.split("-")[1];
FileBrowser.event = FileBrowser.event + "." + destination;
if (destination === "comment") {
// Get the comment textimput field
var commentElm = document.getElementById("comment-edit-text-" + FileBrowser.id);
}
2021-01-22 14:38:44 +01:00
}
2019-07-29 22:41:19 +02:00
2021-01-22 14:38:44 +01:00
console.log("FileBrowser: " + nickname, type, FileBrowser.event, FileBrowser.id);
2019-07-29 22:41:19 +02:00
FileBrowser.postLoad();
2021-01-22 14:38:44 +01:00
$(".error .close").on("click", function (e) {
2019-07-29 22:41:19 +02:00
e.preventDefault();
$(".error").addClass("hidden");
});
// Click on album link
$(".fbrowser").on("click", ".folders button, .path button", function (e) {
2019-07-29 22:41:19 +02:00
e.preventDefault();
2021-01-22 14:38:44 +01:00
var url =
baseurl +
"/fbrowser/" +
FileBrowser.type +
"/" +
encodeURIComponent(this.dataset.folder) +
"?mode=none&theme=frio";
2019-07-29 22:41:19 +02:00
FileBrowser.folder = this.dataset.folder;
FileBrowser.loadContent(url);
});
//Embed on click
2021-01-22 14:38:44 +01:00
$(".fbrowser").on("click", ".photo-album-photo-link", function (e) {
2019-07-29 22:41:19 +02:00
e.preventDefault();
var embed = "";
if (FileBrowser.type === "image") {
embed = "[url=" + this.dataset.link + "][img=" + this.dataset.img + "]" + this.dataset.alt + "[/img][/url]";
2019-07-29 22:41:19 +02:00
}
if (FileBrowser.type === "file") {
// attachment links are "baseurl/attach/id"; we need id
embed = "[attachment]" + this.dataset.link.split("/").pop() + "[/attachment]";
}
// Delete prefilled Text of the comment input
// Note: not the best solution but function commentOpenUI don't
// work as expected (we need a way to wait until commentOpenUI would be finished).
// As for now we insert pieces of this function here
2021-01-22 14:38:44 +01:00
if (commentElm !== null && typeof commentElm !== "undefined") {
2019-07-29 22:41:19 +02:00
if (commentElm.value === "") {
2021-01-22 14:38:44 +01:00
$("#comment-edit-text-" + FileBrowser.id)
.addClass("comment-edit-text-full")
.removeClass("comment-edit-text-empty");
2019-07-29 22:41:19 +02:00
$("#comment-edit-submit-wrapper-" + FileBrowser.id).show();
2021-01-22 14:38:44 +01:00
$("#comment-edit-text-" + FileBrowser.id).attr("tabindex", "9");
$("#comment-edit-submit-" + FileBrowser.id).attr("tabindex", "10");
2019-07-29 22:41:19 +02:00
}
}
console.log(FileBrowser.event, this.dataset.filename, embed, FileBrowser.id);
2021-01-22 14:38:44 +01:00
$("body").trigger(FileBrowser.event, [this.dataset.filename, embed, FileBrowser.id, this.dataset.img]);
2019-07-29 22:41:19 +02:00
// Close model
2021-01-22 14:38:44 +01:00
$("#modal").modal("hide");
2019-07-29 22:41:19 +02:00
// Update autosize for this textarea
autosize.update($(".text-autosize"));
});
// EventListener for switching between image and file mode
2021-01-22 14:38:44 +01:00
$(".fbrowser").on("click", ".fbswitcher .btn", function (e) {
2019-07-29 22:41:19 +02:00
e.preventDefault();
FileBrowser.type = this.getAttribute("data-mode");
2021-01-22 14:38:44 +01:00
$(".fbrowser")
.removeClass()
.addClass("fbrowser " + FileBrowser.type);
2019-07-29 22:41:19 +02:00
url = baseurl + "/fbrowser/" + FileBrowser.type + "?mode=none&theme=frio";
FileBrowser.loadContent(url);
});
},
// Initialize the AjaxUpload for the upload buttons
2021-01-22 14:38:44 +01:00
uploadButtons: function () {
2019-07-29 22:41:19 +02:00
if ($("#upload-image").length) {
//AjaxUpload for images
2021-01-22 14:38:44 +01:00
var image_uploader = new window.AjaxUpload("upload-image", {
action:
2022-10-30 05:17:54 +01:00
"profile/" +
2021-01-22 14:38:44 +01:00
FileBrowser.nickname +
2022-10-30 05:17:54 +01:00
"/photos/upload?response=json&album=" +
2021-01-22 14:38:44 +01:00
encodeURIComponent(FileBrowser.folder),
name: "userfile",
responseType: "json",
onSubmit: function (file, ext) {
$(".fbrowser-content").hide();
$(".fbrowser .profile-rotator-wrapper").show();
$(".error").addClass("hidden");
},
onComplete: function (file, response) {
if (response["error"] != undefined) {
$(".error span").html(response["error"]);
$(".error").removeClass("hidden");
$(".fbrowser .profile-rotator-wrapper").hide();
$(".fbrowser-content").show();
return;
2019-07-29 22:41:19 +02:00
}
2021-01-22 14:38:44 +01:00
// load new content to fbrowser window
FileBrowser.loadContent(
baseurl +
"/fbrowser/" +
FileBrowser.type +
"/" +
encodeURIComponent(FileBrowser.folder) +
"?mode=none&theme=frio",
);
},
});
2019-07-29 22:41:19 +02:00
}
if ($("#upload-file").length) {
//AjaxUpload for files
2021-01-22 14:38:44 +01:00
var file_uploader = new window.AjaxUpload("upload-file", {
action: "profile/" + FileBrowser.nickname + "/attachment/upload?response=json",
2021-01-22 14:38:44 +01:00
name: "userfile",
responseType: "json",
2021-01-22 14:38:44 +01:00
onSubmit: function (file, ext) {
$(".fbrowser-content").hide();
$(".fbrowser .profile-rotator-wrapper").show();
$(".error").addClass("hidden");
},
onComplete: function (file, response) {
if (response["error"] != undefined) {
$(".error span").html(response["error"]);
$(".error").removeClass("hidden");
$(".fbrowser .profile-rotator-wrapper").hide();
2021-01-22 14:38:44 +01:00
$(".fbrowser-content").show();
return;
2019-07-29 22:41:19 +02:00
}
2021-01-22 14:38:44 +01:00
var url = baseurl + "/fbrowser/" + FileBrowser.type + "?mode=none&theme=frio";
// Load new content to fbrowser window
FileBrowser.loadContent(url);
},
});
2019-07-29 22:41:19 +02:00
}
},
// Stuff which should be executed if ne content was loaded
2021-01-22 14:38:44 +01:00
postLoad: function () {
2019-07-29 22:41:19 +02:00
FileBrowser.initGallery();
$(".fbrowser .fbswitcher .btn").removeClass("active");
$(".fbrowser .fbswitcher [data-mode=" + FileBrowser.type + "]").addClass("active");
// We need to add the AjaxUpload to the button
FileBrowser.uploadButtons();
},
// Load new content (e.g. change photo album)
2021-01-22 14:38:44 +01:00
loadContent: function (url) {
2019-07-29 22:41:19 +02:00
$(".fbrowser-content").hide();
$(".fbrowser .profile-rotator-wrapper").show();
// load new content to fbrowser window
2021-01-22 14:38:44 +01:00
$(".fbrowser").load(url, function (responseText, textStatus) {
2019-07-29 22:41:19 +02:00
$(".profile-rotator-wrapper").hide();
2021-01-22 14:38:44 +01:00
if (textStatus === "success") {
2019-07-29 22:41:19 +02:00
$(".fbrowser_content").show();
FileBrowser.postLoad();
}
});
},
// Initialize justified Gallery
2021-01-22 14:38:44 +01:00
initGallery: function () {
2019-07-29 22:41:19 +02:00
$(".fbrowser.image .fbrowser-content-container").justifiedGallery({
2021-01-22 14:38:44 +01:00
rowHeight: 80,
margins: 4,
border: 0,
2019-07-29 22:41:19 +02:00
});
2021-01-22 14:38:44 +01:00
},
2019-07-29 22:41:19 +02:00
};
// @license-end