1 // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPLv3-or-later
3 * Filebrowser - Friendica Communications Server
5 * Copyright (c) 2010-2015 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;
59 * This is a modified version to work with
60 * the frio theme.and bootstrap modals
62 * The origninal file is under:
75 init: function(nickname, type, hash) {
76 FileBrowser.nickname = nickname;
77 FileBrowser.type = type;
78 FileBrowser.event = "fbrowser."+type;
81 var h = hash.replace("#","");
82 var destination = h.split("-")[0];
83 FileBrowser.id = h.split("-")[1];
84 FileBrowser.event = FileBrowser.event + "." + destination;
85 if (destination === "comment") {
86 // Get the comment textimput field
87 var commentElm = document.getElementById("comment-edit-text-" + FileBrowser.id);
91 console.log("FileBrowser: " + nickname, type, FileBrowser.event, FileBrowser.id);
93 FileBrowser.postLoad();
95 $(".error .close").on("click", function(e) {
97 $(".error").addClass("hidden");
100 // Click on album link
101 $(".fbrowser").on("click", ".folders a, .path a", function(e) {
103 var url = baseurl + "/fbrowser/" + FileBrowser.type + "/" + encodeURIComponent(this.dataset.folder) + "?mode=none&theme=frio";
104 FileBrowser.folder = this.dataset.folder;
106 FileBrowser.loadContent(url);
110 $(".fbrowser").on('click', ".photo-album-photo-link", function(e) {
114 if (FileBrowser.type === "image") {
115 embed = "[url=" + this.dataset.link + "][img=" + this.dataset.img + "][/img][/url]";
117 if (FileBrowser.type === "file") {
118 // attachment links are "baseurl/attach/id"; we need id
119 embed = "[attachment]" + this.dataset.link.split("/").pop() + "[/attachment]";
122 // Delete prefilled Text of the comment input
123 // Note: not the best solution but function commentOpenUI don't
124 // work as expected (we need a way to wait until commentOpenUI would be finished).
125 // As for now we insert pieces of this function here
126 if ((commentElm !== null) && (typeof commentElm !== "undefined")) {
127 if (commentElm.value === "") {
128 $("#comment-edit-text-" + FileBrowser.id).addClass("comment-edit-text-full").removeClass("comment-edit-text-empty");
129 $("#comment-edit-submit-wrapper-" + FileBrowser.id).show();
130 $("#comment-edit-text-" + FileBrowser.id).attr('tabindex','9');
131 $("#comment-edit-submit-" + FileBrowser.id).attr('tabindex','10');
136 console.log(FileBrowser.event, this.dataset.filename, embed, FileBrowser.id);
138 $("body").trigger(FileBrowser.event, [
139 this.dataset.filename,
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");
155 $(".fbrowser").removeClass().addClass("fbrowser " + FileBrowser.type);
156 url = baseurl + "/fbrowser/" + FileBrowser.type + "?mode=none&theme=frio";
158 FileBrowser.loadContent(url);
162 // Initialize the AjaxUpload for the upload buttons
163 uploadButtons: function() {
164 if ($("#upload-image").length) {
165 //AjaxUpload for images
166 var image_uploader = new window.AjaxUpload(
169 action: 'wall_upload/' + FileBrowser.nickname + '?response=json&album=' + encodeURIComponent(FileBrowser.folder),
171 responseType: 'json',
172 onSubmit: function(file, ext) {
173 $(".fbrowser-content").hide();
174 $(".fbrowser .profile-rotator-wrapper").show();
175 $(".error").addClass('hidden');
177 onComplete: function(file,response) {
178 if (response['error'] != undefined) {
179 $(".error span").html(response['error']);
180 $(".error").removeClass('hidden');
181 $(".fbrowser .profile-rotator-wrapper").hide();
182 $(".fbrowser-content").show();
186 // load new content to fbrowser window
187 FileBrowser.loadContent(baseurl + '/fbrowser/' + FileBrowser.type + '/' + encodeURIComponent(FileBrowser.folder) + '?mode=none&theme=frio');
193 if ($("#upload-file").length) {
194 //AjaxUpload for files
195 var file_uploader = new window.AjaxUpload(
197 { action: 'wall_attach/' + FileBrowser.nickname + '?response=json',
199 onSubmit: function(file, ext) {
200 $(".fbrowser-content").hide();
201 $(".fbrowser .profile-rotator-wrapper").show();
202 $(".error").addClass('hidden');
204 onComplete: function(file,response) {
205 if (response['error']!= undefined) {
206 $(".error span").html(response['error']);
207 $(".error").removeClass('hidden');
208 $('#profile-rotator').hide();
209 $(".fbrowser-content").show();
213 var url = baseurl + "/fbrowser/" + FileBrowser.type + "?mode=none&theme=frio";
214 // Load new content to fbrowser window
215 FileBrowser.loadContent(url);
222 // Stuff which should be executed if ne content was loaded
223 postLoad: function() {
224 FileBrowser.initGallery();
225 $(".fbrowser .fbswitcher .btn").removeClass("active");
226 $(".fbrowser .fbswitcher [data-mode=" + FileBrowser.type + "]").addClass("active");
227 // We need to add the AjaxUpload to the button
228 FileBrowser.uploadButtons();
231 // Load new content (e.g. change photo album)
232 loadContent: function(url) {
233 $(".fbrowser-content").hide();
234 $(".fbrowser .profile-rotator-wrapper").show();
236 // load new content to fbrowser window
237 $(".fbrowser").load(url, function(responseText, textStatus) {
238 $(".profile-rotator-wrapper").hide();
239 if (textStatus === 'success') {
240 $(".fbrowser_content").show();
241 FileBrowser.postLoad();
246 // Initialize justified Gallery
247 initGallery: function() {
248 $(".fbrowser.image .fbrowser-content-container").justifiedGallery({