]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/js/filebrowser.js
Move mod/fbrowser to src\Modules\Attachment|Photos\Browser
[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                                 // attachment links are "baseurl/attach/id"; we need id
117                                 embed = '[attachment]' + this.dataset.link + '[/attachment]';
118                         }
119
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');
132                                 }
133                         }
134
135                         console.log(FileBrowser.event, this.dataset.filename, embed, FileBrowser.id);
136
137                         $('body').trigger(FileBrowser.event, [this.dataset.filename, embed, FileBrowser.id, this.dataset.img]);
138
139                         // Close model
140                         $('#modal').modal('hide');
141                         // Update autosize for this textarea
142                         autosize.update($('.text-autosize'));
143                 });
144
145                 // EventListener for switching between photo and file mode
146                 $('.fbrowser').on('click', '.fbswitcher .btn', function (e) {
147                         e.preventDefault();
148                         FileBrowser.type = this.getAttribute('data-mode');
149                         $('.fbrowser')
150                                 .removeClass()
151                                 .addClass('fbrowser ' + FileBrowser.type);
152
153                         FileBrowser.loadContent(FileBrowser._getUrl("none"));
154                 });
155         },
156
157         // Initialize the AjaxUpload for the upload buttons
158         uploadButtons: function () {
159                 if ($('#upload-photos').length) {
160                         //AjaxUpload for photos
161                         new window.AjaxUpload(
162                                 'upload-photos',
163                                 {
164                                         action: 'profile/' + FileBrowser.nickname + '/photos/upload?response=json&album=' + encodeURIComponent(FileBrowser.folder),
165                                         name: 'userfile',
166                                         responseType: 'json',
167                                         onSubmit: function (file, ext) {
168                                                 $('.fbrowser-content').hide();
169                                                 $('.fbrowser .profile-rotator-wrapper').show();
170                                                 $('.error').addClass('hidden');
171                                         },
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();
178                                                         return;
179                                                 }
180                                                 // load new content to fbrowser window
181                                                 FileBrowser.loadContent(FileBrowser._getUrl("none"));
182                                         },
183                                 });
184                 }
185
186                 if ($('#upload-attachment').length) {
187                         //AjaxUpload for files
188                         new window.AjaxUpload(
189                                 'upload-attachment',
190                                 {
191                                         action: 'profile/' + FileBrowser.nickname + '/attachment/upload?response=json',
192                                         name: 'userfile',
193                                         responseType: 'json',
194                                         onSubmit: function (file, ext) {
195                                                 $('.fbrowser-content').hide();
196                                                 $('.fbrowser .profile-rotator-wrapper').show();
197                                                 $('.error').addClass('hidden');
198                                         },
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();
205                                                         return;
206                                                 }
207                                                 // Load new content to fbrowser window
208                                                 FileBrowser.loadContent(FileBrowser._getUrl("none"));
209                                         },
210                                 });
211                 }
212         },
213
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();
221         },
222
223         // Load new content (e.g. change photo album)
224         loadContent: function (url) {
225                 $('.fbrowser-content').hide();
226                 $('.fbrowser .profile-rotator-wrapper').show();
227
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();
234                         }
235                 });
236         },
237
238         // Initialize justified Gallery
239         initGallery: function () {
240                 $('.fbrowser.photos .fbrowser-content-container').justifiedGallery({
241                         rowHeight: 80,
242                         margins: 4,
243                         border: 0,
244                 });
245         },
246
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";
251         }
252 };
253 // @license-end