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 + "/" + 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 // To get the albumname we need to convert it from hex
165 var albumname = hex2bin(FileBrowser.folder);
166 //AjaxUpload for images
167 var image_uploader = new window.AjaxUpload(
169 { action: 'wall_upload/' + FileBrowser.nickname + '?response=json&album=' + albumname,
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 var url = baseurl + "/fbrowser/" + FileBrowser.type + "/" + FileBrowser.folder + "?mode=none&theme=frio";
187 // load new content to fbrowser window
188 FileBrowser.loadContent(url);
194 if ($("#upload-file").length) {
195 //AjaxUpload for files
196 var file_uploader = new window.AjaxUpload(
198 { action: 'wall_attach/' + FileBrowser.nickname + '?response=json',
200 onSubmit: function(file, ext) {
201 $(".fbrowser-content").hide();
202 $(".fbrowser .profile-rotator-wrapper").show();
203 $(".error").addClass('hidden');
205 onComplete: function(file,response) {
206 if (response['error']!= undefined) {
207 $(".error span").html(response['error']);
208 $(".error").removeClass('hidden');
209 $('#profile-rotator').hide();
210 $(".fbrowser-content").show();
214 var url = baseurl + "/fbrowser/" + FileBrowser.type + "?mode=none&theme=frio";
215 // Load new content to fbrowser window
216 FileBrowser.loadContent(url);
223 // Stuff which should be executed if ne content was loaded
224 postLoad: function() {
225 FileBrowser.initGallery();
226 $(".fbrowser .fbswitcher .btn").removeClass("active");
227 $(".fbrowser .fbswitcher [data-mode=" + FileBrowser.type + "]").addClass("active");
228 // We need to add the AjaxUpload to the button
229 FileBrowser.uploadButtons();
232 // Load new content (e.g. change photo album)
233 loadContent: function(url) {
234 $(".fbrowser-content").hide();
235 $(".fbrowser .profile-rotator-wrapper").show();
237 // load new content to fbrowser window
238 $(".fbrowser").load(url, function(responseText, textStatus) {
239 $(".profile-rotator-wrapper").hide();
240 if (textStatus === 'success') {
241 $(".fbrowser_content").show();
242 FileBrowser.postLoad();
247 // Initialize justified Gallery
248 initGallery: function() {
249 $(".fbrowser.image .fbrowser-content-container").justifiedGallery({