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 FileBrowser.nickname = nickname;
62 FileBrowser.type = type;
63 FileBrowser.event = "fbrowser." + type;
64 if (location['hash'] !== "") {
65 const h = location['hash'].replace('#', '');
66 FileBrowser.event = FileBrowser.event + "." + h.split("-")[0];
67 FileBrowser.id = h.split("-")[1];
70 console.log('FileBrowser:', nickname, type, FileBrowser.event, FileBrowser.id);
72 $('.error a.close').on('click', function (e) {
74 $('.error').addClass('hidden');
77 $('.folders a, .path a').on('click', function (e) {
79 location.href = FileBrowser._getUrl("minimal", location['hash'], this.dataset.folder);
83 $(".photo-album-photo-link").on('click', function (e) {
87 if (FileBrowser.type === "photos") {
88 embed = '[url=' + this.dataset.link + '][img=' + this.dataset.img + ']' + this.dataset.alt + '[/img][/url]';
90 if (FileBrowser.type === "attachment") {
91 embed = '[attachment]' + this.dataset.link + '[/attachment]';
93 console.log(FileBrowser.event, this.dataset.filename, embed, FileBrowser.id);
94 parent.$('body').trigger(FileBrowser.event, [
95 this.dataset.filename,
102 if ($('#upload-photos').length)
104 new window.AjaxUpload(
107 action: 'profile/' + FileBrowser.nickname + '/photos/upload?response=json',
109 responseType: 'json',
110 onSubmit: function (file, ext) {
111 $('#profile-rotator').show();
112 $('.error').addClass('hidden');
114 onComplete: function (file, response) {
115 if (response['error'] !== undefined) {
116 $('.error span').html(response['error']);
117 $('.error').removeClass('hidden');
118 $('#profile-rotator').hide();
121 location.href = FileBrowser._getUrl("minimal", location['hash']);
128 if ($('#upload-attachment').length)
130 new window.AjaxUpload(
133 action: 'profile/' + FileBrowser.nickname + '/attachment/upload?response=json',
135 responseType: 'json',
136 onSubmit: function (file, ext) {
137 $('#profile-rotator').show();
138 $('.error').addClass('hidden');
140 onComplete: function (file, response) {
141 if (response['error'] !== undefined) {
142 $('.error span').html(response['error']);
143 $('.error').removeClass('hidden');
144 $('#profile-rotator').hide();
147 location.href = FileBrowser._getUrl("minimal", location['hash']);
155 _getUrl: function (mode, hash, folder) {
156 let folderValue = folder !== undefined ? folder : FileBrowser.folder;
157 let folderUrl = folderValue !== undefined ? '/' + encodeURIComponent(folderValue) : '';
158 return 'profile/' + FileBrowser.nickname + '/' + FileBrowser.type + '/browser' + folderUrl + '?mode=' + mode + hash;