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 photo/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 * filename: filename of item chosen 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 textarea 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 original file is under:
73 init: function (nickname, type, hash) {
74 FileBrowser.nickname = nickname;
75 FileBrowser.type = type;
76 FileBrowser.event = 'fbrowser.' + type;
79 const h = hash.replace('#', '');
80 const destination = h.split('-')[0];
81 FileBrowser.id = h.split('-')[1];
82 FileBrowser.event = FileBrowser.event + '.' + destination;
83 if (destination === 'comment') {
84 // Get the comment textinput 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) {
101 let url = FileBrowser._getUrl("none", this.dataset.folder);
102 FileBrowser.folder = this.dataset.folder;
104 FileBrowser.loadContent(url);
108 $('.fbrowser').on('click', '.photo-album-photo-link', function (e) {
112 if (FileBrowser.type === 'photos') {
113 embed = '[url=' + this.dataset.link + '][img=' + this.dataset.img + ']' + this.dataset.alt + '[/img][/url]';
115 if (FileBrowser.type === 'attachment') {
116 embed = '[attachment]' + this.dataset.link + '[/attachment]';
119 // Delete prefilled Text of the comment input
120 // Note: not the best solution but function commentOpenUI don't
121 // work as expected (we need a way to wait until commentOpenUI would be finished).
122 // As for now we insert pieces of this function here
123 if (commentElm !== null && typeof commentElm !== 'undefined') {
124 if (commentElm.value === '') {
125 $('#comment-edit-text-' + FileBrowser.id)
126 .addClass('comment-edit-text-full')
127 .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');
134 console.log(FileBrowser.event, this.dataset.filename, embed, FileBrowser.id);
136 $('body').trigger(FileBrowser.event, [this.dataset.filename, embed, FileBrowser.id, this.dataset.img]);
139 $('#modal').modal('hide');
140 // Update autosize for this textarea
141 autosize.update($('.text-autosize'));
144 // EventListener for switching between photo and file mode
145 $('.fbrowser').on('click', '.fbswitcher .btn', function (e) {
147 FileBrowser.type = this.getAttribute('data-mode');
150 .addClass('fbrowser ' + FileBrowser.type);
152 FileBrowser.loadContent(FileBrowser._getUrl("none"));
156 // Initialize the AjaxUpload for the upload buttons
157 uploadButtons: function () {
158 if ($('#upload-photos').length) {
159 //AjaxUpload for photos
160 new window.AjaxUpload(
163 action: 'profile/' + FileBrowser.nickname + '/photos/upload?response=json&album=' + encodeURIComponent(FileBrowser.folder),
165 responseType: 'json',
166 onSubmit: function (file, ext) {
167 $('.fbrowser-content').hide();
168 $('.fbrowser .profile-rotator-wrapper').show();
169 $('.error').addClass('hidden');
171 onComplete: function (file, response) {
172 if (response['error'] !== undefined) {
173 $('.error span').html(response['error']);
174 $('.error').removeClass('hidden');
175 $('.fbrowser .profile-rotator-wrapper').hide();
176 $('.fbrowser-content').show();
179 // load new content to fbrowser window
180 FileBrowser.loadContent(FileBrowser._getUrl("none"));
185 if ($('#upload-attachment').length) {
186 //AjaxUpload for files
187 new window.AjaxUpload(
190 action: 'profile/' + FileBrowser.nickname + '/attachment/upload?response=json',
192 responseType: 'json',
193 onSubmit: function (file, ext) {
194 $('.fbrowser-content').hide();
195 $('.fbrowser .profile-rotator-wrapper').show();
196 $('.error').addClass('hidden');
198 onComplete: function (file, response) {
199 if (response["error"] !== undefined) {
200 $('.error span').html(response['error']);
201 $('.error').removeClass('hidden');
202 $('.fbrowser .profile-rotator-wrapper').hide();
203 $('.fbrowser-content').show();
206 // Load new content to fbrowser window
207 FileBrowser.loadContent(FileBrowser._getUrl("none"));
213 // Stuff which should be executed if no content was loaded
214 postLoad: function () {
215 FileBrowser.initGallery();
216 $('.fbrowser .fbswitcher .btn').removeClass('active');
217 $('.fbrowser .fbswitcher [data-mode=' + FileBrowser.type + ']').addClass('active');
218 // We need to add the AjaxUpload to the button
219 FileBrowser.uploadButtons();
222 // Load new content (e.g. change photo album)
223 loadContent: function (url) {
224 $('.fbrowser-content').hide();
225 $('.fbrowser .profile-rotator-wrapper').show();
227 // load new content to fbrowser window
228 $('.fbrowser').load(url, function (responseText, textStatus) {
229 $('.profile-rotator-wrapper').hide();
230 if (textStatus === 'success') {
231 $(".fbrowser_content").show();
232 FileBrowser.postLoad();
237 // Initialize justified Gallery
238 initGallery: function () {
239 $('.fbrowser.photos .fbrowser-content-container').justifiedGallery({
246 _getUrl: function (mode, folder) {
247 let folderValue = folder !== undefined ? folder : FileBrowser.folder;
248 let folderUrl = folderValue !== undefined ? '/' + encodeURIComponent(folderValue) : '';
249 return 'profile/' + FileBrowser.nickname + '/' + FileBrowser.type + '/browser' + folderUrl + '?mode=' + mode + "&theme=frio";