]> git.mxchange.org Git - friendica.git/blob - view/js/filebrowser.js
License :)
[friendica.git] / view / js / filebrowser.js
1 // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPLv3-or-later
2 /**
3  * Filebrowser - Friendica Communications Server
4  *
5  * Copyright (c) 2010-2021, the Friendica project
6  *
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.
11  *
12  * This code handle user interaction for image/file upload/browser dialog.
13  * Is loaded from filebrowser_plain.tpl
14  *
15  * To load filebrowser in colorbox, call
16  *
17  *      Dialog.doImageBrowser(eventname, id);
18  *
19  * or
20  *
21  *      Dialog.doFileBrowser(eventname, id);
22  *
23  * where:
24  *
25  *              eventname: event name to catch return value
26  *              id: id returned to event handler
27  *
28  * When user select an item, an event in fired in parent page, on body element
29  * The event is named
30  *
31  *              fbrowser.<type>.[<eventname>]
32  *
33  * <type> will be one of "image" or "file", and the event handler will
34  * get the following params:
35  *
36  *              filename: filename of item chosen by user
37  *              embed: bbcode to embed element into posts
38  *              id: id from caller code
39  *
40  * example:
41  *
42  *              // open dialog for select an image for a textarea with id "myeditor"
43  *              var id="myeditor";
44  *              Dialog.doImageBrowser("example", id);
45  *
46  *              // setup event handler to get user selection
47  *              $("body").on("fbrowser.image.example", function(event, filename, bbcode, id) {
48  *                      // close colorbox
49  *                      $.colorbox.close();
50  *                      // replace textarea text with bbcode
51  *                      $(id).value = bbcode;
52  *              });
53  **/
54 const FileBrowser = {
55         nickname: '',
56         type: '',
57         event: '',
58         id: null,
59
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];
68                 }
69
70                 console.log('FileBrowser:', nickname, type, FileBrowser.event, FileBrowser.id);
71
72                 $('.error a.close').on('click', function (e) {
73                         e.preventDefault();
74                         $('.error').addClass('hidden');
75                 });
76
77                 $('.folders a, .path a').on('click', function (e) {
78                         e.preventDefault();
79                         location.href = FileBrowser._getUrl("minimal", location['hash'], this.dataset.folder);
80                         location.reload();
81                 });
82
83                 $(".photo-album-photo-link").on('click', function (e) {
84                         e.preventDefault();
85
86                         let embed = '';
87                         if (FileBrowser.type === "photos") {
88                                 embed = '[url=' + this.dataset.link + '][img=' + this.dataset.img + ']' + this.dataset.alt + '[/img][/url]';
89                         }
90                         if (FileBrowser.type === "attachment") {
91                                 embed = '[attachment]' + this.dataset.link + '[/attachment]';
92                         }
93                         console.log(FileBrowser.event, this.dataset.filename, embed, FileBrowser.id);
94                         parent.$('body').trigger(FileBrowser.event, [
95                                 this.dataset.filename,
96                                 embed,
97                                 FileBrowser.id
98                         ]);
99
100                 });
101
102                 if ($('#upload-photos').length)
103                 {
104                         new window.AjaxUpload(
105                                 'upload-photos',
106                                 {
107                                         action: 'profile/' + FileBrowser.nickname + '/photos/upload?response=json',
108                                         name: 'userfile',
109                                         responseType: 'json',
110                                         onSubmit: function (file, ext) {
111                                                 $('#profile-rotator').show();
112                                                 $('.error').addClass('hidden');
113                                         },
114                                         onComplete: function (file, response) {
115                                                 if (response['error'] !== undefined) {
116                                                         $('.error span').html(response['error']);
117                                                         $('.error').removeClass('hidden');
118                                                         $('#profile-rotator').hide();
119                                                         return;
120                                                 }
121                                                 location.href = FileBrowser._getUrl("minimal", location['hash']);
122                                                 location.reload();
123                                         }
124                                 }
125                         );
126                 }
127
128                 if ($('#upload-attachment').length)
129                 {
130                         new window.AjaxUpload(
131                                 'upload-attachment',
132                                 {
133                                         action: 'profile/' + FileBrowser.nickname + '/attachment/upload?response=json',
134                                         name: 'userfile',
135                                         responseType: 'json',
136                                         onSubmit: function (file, ext) {
137                                                 $('#profile-rotator').show();
138                                                 $('.error').addClass('hidden');
139                                         },
140                                         onComplete: function (file, response) {
141                                                 if (response['error'] !== undefined) {
142                                                         $('.error span').html(response['error']);
143                                                         $('.error').removeClass('hidden');
144                                                         $('#profile-rotator').hide();
145                                                         return;
146                                                 }
147                                                 location.href = FileBrowser._getUrl("minimal", location['hash']);
148                                                 location.reload();
149                                         }
150                                 }
151                                 );
152                         }
153         },
154
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;
159         }
160 };
161 // @license-end