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 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 * 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;
60 init: function (nickname, type) {
61 Browser.nickname = nickname;
63 Browser.event = 'fbrowser.' + type;
64 if (location['hash'] !== '') {
65 const h = location['hash'].replace('#', '');
66 Browser.event = Browser.event + '.' + h.split('-')[0];
67 Browser.id = h.split('-')[1];
70 $('.error a.close').on('click', function (e) {
72 $('.error').addClass('hidden');
75 $('.folders a, .path a').on('click', function (e) {
77 location.href = Browser._getUrl("minimal", location['hash'], this.dataset.folder);
81 $(".photo-album-photo-link").on('click', function (e) {
85 if (Browser.type === "photo") {
86 embed = '[url=' + this.dataset.link + '][img=' + this.dataset.img + ']' + this.dataset.alt + '[/img][/url]';
88 if (Browser.type === "attachment") {
89 embed = '[attachment]' + this.dataset.link + '[/attachment]';
91 parent.$('body').trigger(Browser.event, [
92 this.dataset.filename,
99 if ($('#upload-photo').length) {
100 new window.AjaxUpload(
103 action: 'media/photo/upload?response=json',
105 responseType: 'json',
106 onSubmit: function (file, ext) {
107 $('#profile-rotator').show();
108 $('.error').addClass('hidden');
110 onComplete: function (file, response) {
111 if (response['error'] !== undefined) {
112 $('.error span').html(response['error']);
113 $('.error').removeClass('hidden');
114 $('#profile-rotator').hide();
117 location.href = Browser._getUrl("minimal", location['hash']);
124 if ($('#upload-attachment').length) {
125 new window.AjaxUpload(
128 action: 'media/attachment/upload?response=json',
130 responseType: 'json',
131 onSubmit: function (file, ext) {
132 $('#profile-rotator').show();
133 $('.error').addClass('hidden');
135 onComplete: function (file, response) {
136 if (response['error'] !== undefined) {
137 $('.error span').html(response['error']);
138 $('.error').removeClass('hidden');
139 $('#profile-rotator').hide();
142 location.href = Browser._getUrl("minimal", location['hash']);
150 _getUrl: function (mode, hash, folder) {
151 let folderValue = folder !== undefined ? folder : Browser.folder;
152 let folderUrl = folderValue !== undefined ? '/' + encodeURIComponent(folderValue) : '';
153 return 'media/' + Browser.type + '/browser' + folderUrl + '?mode=' + mode + hash;