2 * Filebrowser - Friendica Communications Server
4 * Copyright (c) 2010-2015 the Friendica Project
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This code handle user interaction for image/file upload/browser dialog.
12 * Is loaded from filebrowser_plain.tpl
14 * To load filebrowser in colorbox, call
16 * Dialog.doImageBrowser(eventname, id);
20 * Dialog.doFileBrowser(eventname, id);
24 * eventname: event name to catch return value
25 * id: id returned to event handler
27 * When user select an item, an event in fired in parent page, on body element
30 * fbrowser.<type>.[<eventname>]
32 * <type> will be one of "image" or "file", and the event handler will
33 * get the following params:
35 * filemane: filename of item choosed by user
36 * embed: bbcode to embed element into posts
37 * id: id from caller code
41 * // open dialog for select an image for a textarea with id "myeditor"
43 * Dialog.doImageBrowser("example", id);
45 * // setup event handler to get user selection
46 * $("body").on("fbrowser.image.example", function(event, filename, bbcode, id) {
49 * // replace textxarea text with bbcode
50 * $(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:
74 init: function(nickname, type, hash) {
75 FileBrowser.nickname = nickname;
76 FileBrowser.type = type;
77 FileBrowser.event = "fbrowser."+type;
80 var h = hash.replace("#","");
81 var destination = h.split("-")[0];
82 FileBrowser.id = h.split("-")[1];
83 FileBrowser.event = FileBrowser.event + "." + destination;
84 if (destination === "comment") {
85 // Get the comment textimput field
86 var commentElm = document.getElementById("comment-edit-text-" + FileBrowser.id);
90 console.log("FileBrowser: " + nickname, type, FileBrowser.event, FileBrowser.id);
92 FileBrowser.postLoad();
94 $(".error .close").on("click", function(e) {
96 $(".error").addClass("hidden");
99 // Click on album link
100 $(".fbrowser").on("click", ".folders a, .path a", function(e) {
102 var url = baseurl + "/fbrowser/" + FileBrowser.type + "/" + encodeURIComponent(this.dataset.folder) + "?mode=none&theme=frio";
103 FileBrowser.folder = this.dataset.folder;
105 FileBrowser.loadContent(url);
109 $(".fbrowser").on('click', ".photo-album-photo-link", function(e) {
113 if (FileBrowser.type === "image") {
114 embed = "[url=" + this.dataset.link + "][img=" + this.dataset.img + "][/img][/url]";
116 if (FileBrowser.type === "file") {
117 // attachment links are "baseurl/attach/id"; we need id
118 embed = "[attachment]" + this.dataset.link.split("/").pop() + "[/attachment]";
121 // Delete prefilled Text of the comment input
122 // Note: not the best solution but function commentOpenUI don't
123 // work as expected (we need a way to wait until commentOpenUI would be finished).
124 // As for now we insert pieces of this function here
125 if ((commentElm !== null) && (typeof commentElm !== "undefined")) {
126 if (commentElm.value === "") {
127 $("#comment-edit-text-" + FileBrowser.id).addClass("comment-edit-text-full").removeClass("comment-edit-text-empty");
128 $("#comment-edit-submit-wrapper-" + FileBrowser.id).show();
129 $("#comment-edit-text-" + FileBrowser.id).attr('tabindex','9');
130 $("#comment-edit-submit-" + FileBrowser.id).attr('tabindex','10');
135 console.log(FileBrowser.event, this.dataset.filename, embed, FileBrowser.id);
137 $("body").trigger(FileBrowser.event, [
138 this.dataset.filename,
145 $('#modal').modal('hide');
146 // Update autosize for this textarea
147 autosize.update($(".text-autosize"));
150 // EventListener for switching between image and file mode
151 $(".fbrowser").on('click', ".fbswitcher .btn", function(e) {
153 FileBrowser.type = this.getAttribute("data-mode");
154 $(".fbrowser").removeClass().addClass("fbrowser " + FileBrowser.type);
155 url = baseurl + "/fbrowser/" + FileBrowser.type + "?mode=none&theme=frio";
157 FileBrowser.loadContent(url);
161 // Initialize the AjaxUpload for the upload buttons
162 uploadButtons: function() {
163 if ($("#upload-image").length) {
164 //AjaxUpload for images
165 var image_uploader = new window.AjaxUpload(
168 action: 'wall_upload/' + FileBrowser.nickname + '?response=json&album=' + encodeURIComponent(FileBrowser.folder),
170 responseType: 'json',
171 onSubmit: function(file, ext) {
172 $(".fbrowser-content").hide();
173 $(".fbrowser .profile-rotator-wrapper").show();
174 $(".error").addClass('hidden');
176 onComplete: function(file,response) {
177 if (response['error'] != undefined) {
178 $(".error span").html(response['error']);
179 $(".error").removeClass('hidden');
180 $(".fbrowser .profile-rotator-wrapper").hide();
181 $(".fbrowser-content").show();
185 // load new content to fbrowser window
186 FileBrowser.loadContent(baseurl + '/fbrowser/' + FileBrowser.type + '/' + encodeURIComponent(FileBrowser.folder) + '?mode=none&theme=frio');
192 if ($("#upload-file").length) {
193 //AjaxUpload for files
194 var file_uploader = new window.AjaxUpload(
196 { action: 'wall_attach/' + FileBrowser.nickname + '?response=json',
198 onSubmit: function(file, ext) {
199 $(".fbrowser-content").hide();
200 $(".fbrowser .profile-rotator-wrapper").show();
201 $(".error").addClass('hidden');
203 onComplete: function(file,response) {
204 if (response['error']!= undefined) {
205 $(".error span").html(response['error']);
206 $(".error").removeClass('hidden');
207 $('#profile-rotator').hide();
208 $(".fbrowser-content").show();
212 var url = baseurl + "/fbrowser/" + FileBrowser.type + "?mode=none&theme=frio";
213 // Load new content to fbrowser window
214 FileBrowser.loadContent(url);
221 // Stuff which should be executed if ne content was loaded
222 postLoad: function() {
223 FileBrowser.initGallery();
224 $(".fbrowser .fbswitcher .btn").removeClass("active");
225 $(".fbrowser .fbswitcher [data-mode=" + FileBrowser.type + "]").addClass("active");
226 // We need to add the AjaxUpload to the button
227 FileBrowser.uploadButtons();
230 // Load new content (e.g. change photo album)
231 loadContent: function(url) {
232 $(".fbrowser-content").hide();
233 $(".fbrowser .profile-rotator-wrapper").show();
235 // load new content to fbrowser window
236 $(".fbrowser").load(url, function(responseText, textStatus) {
237 $(".profile-rotator-wrapper").hide();
238 if (textStatus === 'success') {
239 $(".fbrowser_content").show();
240 FileBrowser.postLoad();
245 // Initialize justified Gallery
246 initGallery: function() {
247 $(".fbrowser.image .fbrowser-content-container").justifiedGallery({