]> git.mxchange.org Git - friendica.git/blob - view/js/module/media/browser.js
Remove DI dependency from Module\Contact\Profile
[friendica.git] / view / js / module / media / browser.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 Browser = {
55         nickname: '',
56         type: '',
57         event: '',
58         id: null,
59
60         init: function (nickname, type) {
61                 Browser.nickname = nickname;
62                 Browser.type = type;
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];
68                 }
69
70                 $('.error a.close').on('click', function (e) {
71                         e.preventDefault();
72                         $('.error').addClass('hidden');
73                 });
74
75                 $('.folders a, .path a').on('click', function (e) {
76                         e.preventDefault();
77                         location.href = Browser._getUrl("minimal", location['hash'], this.dataset.folder);
78                         location.reload();
79                 });
80
81                 $(".photo-album-photo-link").on('click', function (e) {
82                         e.preventDefault();
83
84                         let embed = '';
85                         if (Browser.type === "photo") {
86                                 embed = '[url=' + this.dataset.link + '][img=' + this.dataset.img + ']' + this.dataset.alt + '[/img][/url]';
87                         }
88                         if (Browser.type === "attachment") {
89                                 embed = '[attachment]' + this.dataset.link + '[/attachment]';
90                         }
91                         parent.$('body').trigger(Browser.event, [
92                                 this.dataset.filename,
93                                 embed,
94                                 Browser.id
95                         ]);
96
97                 });
98
99                 if ($('#upload-photo').length) {
100                         new window.AjaxUpload(
101                                 'upload-photo',
102                                 {
103                                         action: 'media/photo/upload?response=json',
104                                         name: 'userfile',
105                                         responseType: 'json',
106                                         onSubmit: function (file, ext) {
107                                                 $('#profile-rotator').show();
108                                                 $('.error').addClass('hidden');
109                                         },
110                                         onComplete: function (file, response) {
111                                                 if (response['error'] !== undefined) {
112                                                         $('.error span').html(response['error']);
113                                                         $('.error').removeClass('hidden');
114                                                         $('#profile-rotator').hide();
115                                                         return;
116                                                 }
117                                                 location.href = Browser._getUrl("minimal", location['hash']);
118                                                 location.reload();
119                                         }
120                                 }
121                         );
122                 }
123
124                 if ($('#upload-attachment').length)     {
125                         new window.AjaxUpload(
126                                 'upload-attachment',
127                                 {
128                                         action: 'media/attachment/upload?response=json',
129                                         name: 'userfile',
130                                         responseType: 'json',
131                                         onSubmit: function (file, ext) {
132                                                 $('#profile-rotator').show();
133                                                 $('.error').addClass('hidden');
134                                         },
135                                         onComplete: function (file, response) {
136                                                 if (response['error'] !== undefined) {
137                                                         $('.error span').html(response['error']);
138                                                         $('.error').removeClass('hidden');
139                                                         $('#profile-rotator').hide();
140                                                         return;
141                                                 }
142                                                 location.href = Browser._getUrl("minimal", location['hash']);
143                                                 location.reload();
144                                         }
145                                 }
146                         );
147                 }
148         },
149
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;
154         }
155 };
156 // @license-end