1 // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPLv3-or-later
3 * Filebrowser - Friendica Communications Server
5 * Copyright (c) 2010-2021, the Friendica project
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This code handle user interaction for image/file upload/browser dialog.
13 * Is loaded from filebrowser_plain.tpl
15 * To load filebrowser in colorbox, call
17 * Dialog.doImageBrowser(eventname, id);
21 * Dialog.doFileBrowser(eventname, id);
25 * eventname: event name to catch return value
26 * id: id returned to event handler
28 * When user select an item, an event in fired in parent page, on body element
31 * fbrowser.<type>.[<eventname>]
33 * <type> will be one of "image" or "file", and the event handler will
34 * get the following params:
36 * filemane: filename of item choosed by user
37 * embed: bbcode to embed element into posts
38 * id: id from caller code
42 * // open dialog for select an image for a textarea with id "myeditor"
44 * Dialog.doImageBrowser("example", id);
46 * // setup event handler to get user selection
47 * $("body").on("fbrowser.image.example", function(event, filename, bbcode, id) {
50 * // replace textxarea text with bbcode
51 * $(id).value = bbcode;
58 * This is a modified version to work with
59 * the frio theme.and bootstrap modals
61 * The origninal file is under:
73 init: function (nickname, type, hash) {
74 FileBrowser.nickname = nickname;
75 FileBrowser.type = type;
76 FileBrowser.event = "fbrowser." + type;
79 var h = hash.replace("#", "");
80 var destination = h.split("-")[0];
81 FileBrowser.id = h.split("-")[1];
82 FileBrowser.event = FileBrowser.event + "." + destination;
83 if (destination === "comment") {
84 // Get the comment textimput field
85 var commentElm = document.getElementById("comment-edit-text-" + FileBrowser.id);
89 console.log("FileBrowser: " + nickname, type, FileBrowser.event, FileBrowser.id);
91 FileBrowser.postLoad();
93 $(".error .close").on("click", function (e) {
95 $(".error").addClass("hidden");
98 // Click on album link
99 $(".fbrowser").on("click", ".folders button, .path button", function (e) {
106 encodeURIComponent(this.dataset.folder) +
107 "?mode=none&theme=frio";
108 FileBrowser.folder = this.dataset.folder;
110 FileBrowser.loadContent(url);
114 $(".fbrowser").on("click", ".photo-album-photo-link", function (e) {
118 if (FileBrowser.type === "image") {
119 embed = "[url=" + this.dataset.link + "][img=" + this.dataset.img + "]" + this.dataset.alt + "[/img][/url]";
121 if (FileBrowser.type === "file") {
122 // attachment links are "baseurl/attach/id"; we need id
123 embed = "[attachment]" + this.dataset.link.split("/").pop() + "[/attachment]";
126 // Delete prefilled Text of the comment input
127 // Note: not the best solution but function commentOpenUI don't
128 // work as expected (we need a way to wait until commentOpenUI would be finished).
129 // As for now we insert pieces of this function here
130 if (commentElm !== null && typeof commentElm !== "undefined") {
131 if (commentElm.value === "") {
132 $("#comment-edit-text-" + FileBrowser.id)
133 .addClass("comment-edit-text-full")
134 .removeClass("comment-edit-text-empty");
135 $("#comment-edit-submit-wrapper-" + FileBrowser.id).show();
136 $("#comment-edit-text-" + FileBrowser.id).attr("tabindex", "9");
137 $("#comment-edit-submit-" + FileBrowser.id).attr("tabindex", "10");
141 console.log(FileBrowser.event, this.dataset.filename, embed, FileBrowser.id);
143 $("body").trigger(FileBrowser.event, [this.dataset.filename, embed, FileBrowser.id, this.dataset.img]);
146 $("#modal").modal("hide");
147 // Update autosize for this textarea
148 autosize.update($(".text-autosize"));
151 // EventListener for switching between image and file mode
152 $(".fbrowser").on("click", ".fbswitcher .btn", function (e) {
154 FileBrowser.type = this.getAttribute("data-mode");
157 .addClass("fbrowser " + FileBrowser.type);
158 url = baseurl + "/fbrowser/" + FileBrowser.type + "?mode=none&theme=frio";
160 FileBrowser.loadContent(url);
164 // Initialize the AjaxUpload for the upload buttons
165 uploadButtons: function () {
166 if ($("#upload-image").length) {
167 //AjaxUpload for images
168 var image_uploader = new window.AjaxUpload("upload-image", {
171 FileBrowser.nickname +
172 "/photos/upload?response=json&album=" +
173 encodeURIComponent(FileBrowser.folder),
175 responseType: "json",
176 onSubmit: function (file, ext) {
177 $(".fbrowser-content").hide();
178 $(".fbrowser .profile-rotator-wrapper").show();
179 $(".error").addClass("hidden");
181 onComplete: function (file, response) {
182 if (response["error"] != undefined) {
183 $(".error span").html(response["error"]);
184 $(".error").removeClass("hidden");
185 $(".fbrowser .profile-rotator-wrapper").hide();
186 $(".fbrowser-content").show();
190 // load new content to fbrowser window
191 FileBrowser.loadContent(
196 encodeURIComponent(FileBrowser.folder) +
197 "?mode=none&theme=frio",
203 if ($("#upload-file").length) {
204 //AjaxUpload for files
205 var file_uploader = new window.AjaxUpload("upload-file", {
206 action: "wall_attach/" + FileBrowser.nickname + "?response=json",
208 onSubmit: function (file, ext) {
209 $(".fbrowser-content").hide();
210 $(".fbrowser .profile-rotator-wrapper").show();
211 $(".error").addClass("hidden");
213 onComplete: function (file, response) {
214 if (response["error"] != undefined) {
215 $(".error span").html(response["error"]);
216 $(".error").removeClass("hidden");
217 $("#profile-rotator").hide();
218 $(".fbrowser-content").show();
222 var url = baseurl + "/fbrowser/" + FileBrowser.type + "?mode=none&theme=frio";
223 // Load new content to fbrowser window
224 FileBrowser.loadContent(url);
230 // Stuff which should be executed if ne content was loaded
231 postLoad: function () {
232 FileBrowser.initGallery();
233 $(".fbrowser .fbswitcher .btn").removeClass("active");
234 $(".fbrowser .fbswitcher [data-mode=" + FileBrowser.type + "]").addClass("active");
235 // We need to add the AjaxUpload to the button
236 FileBrowser.uploadButtons();
239 // Load new content (e.g. change photo album)
240 loadContent: function (url) {
241 $(".fbrowser-content").hide();
242 $(".fbrowser .profile-rotator-wrapper").show();
244 // load new content to fbrowser window
245 $(".fbrowser").load(url, function (responseText, textStatus) {
246 $(".profile-rotator-wrapper").hide();
247 if (textStatus === "success") {
248 $(".fbrowser_content").show();
249 FileBrowser.postLoad();
254 // Initialize justified Gallery
255 initGallery: function () {
256 $(".fbrowser.image .fbrowser-content-container").justifiedGallery({