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 // attachment links are "baseurl/attach/id"; we need id
117 embed = '[attachment]' + this.dataset.link + '[/attachment]';
120 // Delete prefilled Text of the comment input
121 // Note: not the best solution but function commentOpenUI don't
122 // work as expected (we need a way to wait until commentOpenUI would be finished).
123 // As for now we insert pieces of this function here
124 if (commentElm !== null && typeof commentElm !== 'undefined') {
125 if (commentElm.value === '') {
126 $('#comment-edit-text-' + FileBrowser.id)
127 .addClass('comment-edit-text-full')
128 .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');
135 console.log(FileBrowser.event, this.dataset.filename, embed, FileBrowser.id);
137 $('body').trigger(FileBrowser.event, [this.dataset.filename, embed, FileBrowser.id, this.dataset.img]);
140 $('#modal').modal('hide');
141 // Update autosize for this textarea
142 autosize.update($('.text-autosize'));
145 // EventListener for switching between photo and file mode
146 $('.fbrowser').on('click', '.fbswitcher .btn', function (e) {
148 FileBrowser.type = this.getAttribute('data-mode');
151 .addClass('fbrowser ' + FileBrowser.type);
153 FileBrowser.loadContent(FileBrowser._getUrl("none"));
157 // Initialize the AjaxUpload for the upload buttons
158 uploadButtons: function () {
159 if ($('#upload-photos').length) {
160 //AjaxUpload for photos
161 new window.AjaxUpload(
164 action: 'profile/' + FileBrowser.nickname + '/photos/upload?response=json&album=' + encodeURIComponent(FileBrowser.folder),
166 responseType: 'json',
167 onSubmit: function (file, ext) {
168 $('.fbrowser-content').hide();
169 $('.fbrowser .profile-rotator-wrapper').show();
170 $('.error').addClass('hidden');
172 onComplete: function (file, response) {
173 if (response['error'] !== undefined) {
174 $('.error span').html(response['error']);
175 $('.error').removeClass('hidden');
176 $('.fbrowser .profile-rotator-wrapper').hide();
177 $('.fbrowser-content').show();
180 // load new content to fbrowser window
181 FileBrowser.loadContent(FileBrowser._getUrl("none"));
186 if ($('#upload-attachment').length) {
187 //AjaxUpload for files
188 new window.AjaxUpload(
191 action: 'profile/' + FileBrowser.nickname + '/attachment/upload?response=json',
193 responseType: 'json',
194 onSubmit: function (file, ext) {
195 $('.fbrowser-content').hide();
196 $('.fbrowser .profile-rotator-wrapper').show();
197 $('.error').addClass('hidden');
199 onComplete: function (file, response) {
200 if (response["error"] !== undefined) {
201 $('.error span').html(response['error']);
202 $('.error').removeClass('hidden');
203 $('.fbrowser .profile-rotator-wrapper').hide();
204 $('.fbrowser-content').show();
207 // Load new content to fbrowser window
208 FileBrowser.loadContent(FileBrowser._getUrl("none"));
214 // Stuff which should be executed if no content was loaded
215 postLoad: function () {
216 FileBrowser.initGallery();
217 $('.fbrowser .fbswitcher .btn').removeClass('active');
218 $('.fbrowser .fbswitcher [data-mode=' + FileBrowser.type + ']').addClass('active');
219 // We need to add the AjaxUpload to the button
220 FileBrowser.uploadButtons();
223 // Load new content (e.g. change photo album)
224 loadContent: function (url) {
225 $('.fbrowser-content').hide();
226 $('.fbrowser .profile-rotator-wrapper').show();
228 // load new content to fbrowser window
229 $('.fbrowser').load(url, function (responseText, textStatus) {
230 $('.profile-rotator-wrapper').hide();
231 if (textStatus === 'success') {
232 $(".fbrowser_content").show();
233 FileBrowser.postLoad();
238 // Initialize justified Gallery
239 initGallery: function () {
240 $('.fbrowser.photos .fbrowser-content-container').justifiedGallery({
247 _getUrl: function (mode, folder) {
248 let folderValue = folder !== undefined ? folder : FileBrowser.folder;
249 let folderUrl = folderValue !== undefined ? '/' + encodeURIComponent(folderValue) : '';
250 return 'profile/' + FileBrowser.nickname + '/' + FileBrowser.type + '/browser' + folderUrl + '?mode=' + mode + "&theme=frio";