]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/js/filebrowser.js
Apply suggestions from code review
[friendica.git] / view / theme / frio / 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 photo/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
55 /*
56  * IMPORTANT
57  *
58  *  This is a modified version to work with
59  *  the frio theme.and bootstrap modals
60  *
61  *  The original file is under:
62  *  js/filebrowser.js
63  *
64  */
65
66 var FileBrowser = {
67         nickname: '',
68         type: '',
69         event: '',
70         folder: '',
71         id: null,
72
73         init: function (nickname, type, hash) {
74                 FileBrowser.nickname = nickname;
75                 FileBrowser.type = type;
76                 FileBrowser.event = 'fbrowser.' + type;
77
78                 if (hash !== '') {
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);
86                         }
87                 }
88
89                 console.log('FileBrowser: ' + nickname, type, FileBrowser.event, FileBrowser.id);
90
91                 FileBrowser.postLoad();
92
93                 $('.error .close').on('click', function (e) {
94                         e.preventDefault();
95                         $('.error').addClass('hidden');
96                 });
97
98                 // Click on album link
99                 $('.fbrowser').on('click', '.folders button, .path button', function (e) {
100                         e.preventDefault();
101                         let url = FileBrowser._getUrl("none", this.dataset.folder);
102                         FileBrowser.folder = this.dataset.folder;
103
104                         FileBrowser.loadContent(url);
105                 });
106
107                 //Embed on click
108                 $('.fbrowser').on('click', '.photo-album-photo-link', function (e) {
109                         e.preventDefault();
110
111                         let embed = '';
112                         if (FileBrowser.type === 'photos') {
113                                 embed = '[url=' + this.dataset.link + '][img=' + this.dataset.img + ']' + this.dataset.alt + '[/img][/url]';
114                         }
115                         if (FileBrowser.type === 'attachment') {
116                                 embed = '[attachment]' + this.dataset.link + '[/attachment]';
117                         }
118
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');
131                                 }
132                         }
133
134                         console.log(FileBrowser.event, this.dataset.filename, embed, FileBrowser.id);
135
136                         $('body').trigger(FileBrowser.event, [this.dataset.filename, embed, FileBrowser.id, this.dataset.img]);
137
138                         // Close model
139                         $('#modal').modal('hide');
140                         // Update autosize for this textarea
141                         autosize.update($('.text-autosize'));
142                 });
143
144                 // EventListener for switching between photo and file mode
145                 $('.fbrowser').on('click', '.fbswitcher .btn', function (e) {
146                         e.preventDefault();
147                         FileBrowser.type = this.getAttribute('data-mode');
148                         $('.fbrowser')
149                                 .removeClass()
150                                 .addClass('fbrowser ' + FileBrowser.type);
151
152                         FileBrowser.loadContent(FileBrowser._getUrl("none"));
153                 });
154         },
155
156         // Initialize the AjaxUpload for the upload buttons
157         uploadButtons: function () {
158                 if ($('#upload-photos').length) {
159                         //AjaxUpload for photos
160                         new window.AjaxUpload(
161                                 'upload-photos',
162                                 {
163                                         action: 'profile/' + FileBrowser.nickname + '/photos/upload?response=json&album=' + encodeURIComponent(FileBrowser.folder),
164                                         name: 'userfile',
165                                         responseType: 'json',
166                                         onSubmit: function (file, ext) {
167                                                 $('.fbrowser-content').hide();
168                                                 $('.fbrowser .profile-rotator-wrapper').show();
169                                                 $('.error').addClass('hidden');
170                                         },
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();
177                                                         return;
178                                                 }
179                                                 // load new content to fbrowser window
180                                                 FileBrowser.loadContent(FileBrowser._getUrl("none"));
181                                         },
182                                 });
183                 }
184
185                 if ($('#upload-attachment').length) {
186                         //AjaxUpload for files
187                         new window.AjaxUpload(
188                                 'upload-attachment',
189                                 {
190                                         action: 'profile/' + FileBrowser.nickname + '/attachment/upload?response=json',
191                                         name: 'userfile',
192                                         responseType: 'json',
193                                         onSubmit: function (file, ext) {
194                                                 $('.fbrowser-content').hide();
195                                                 $('.fbrowser .profile-rotator-wrapper').show();
196                                                 $('.error').addClass('hidden');
197                                         },
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();
204                                                         return;
205                                                 }
206                                                 // Load new content to fbrowser window
207                                                 FileBrowser.loadContent(FileBrowser._getUrl("none"));
208                                         },
209                                 });
210                 }
211         },
212
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();
220         },
221
222         // Load new content (e.g. change photo album)
223         loadContent: function (url) {
224                 $('.fbrowser-content').hide();
225                 $('.fbrowser .profile-rotator-wrapper').show();
226
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();
233                         }
234                 });
235         },
236
237         // Initialize justified Gallery
238         initGallery: function () {
239                 $('.fbrowser.photos .fbrowser-content-container').justifiedGallery({
240                         rowHeight: 80,
241                         margins: 4,
242                         border: 0,
243                 });
244         },
245
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";
250         }
251 };
252 // @license-end