]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/js/filebrowser.js
[frio] Check for role="presentation" uses throughout the templates
[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 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  *              filemane: filename of item choosed 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 textxarea 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 origninal 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                         var h = hash.replace("#", "");
80                         var destination = h.split("-")[0];
81                         FileBrowser.id = h.split("-")[1];
82                         FileBrowser.event = FileBrowser.event + "." + destination;
83                         if (destination === "comment") {
84                                 // Get the comment textimput 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                         var url =
102                                 baseurl +
103                                 "/fbrowser/" +
104                                 FileBrowser.type +
105                                 "/" +
106                                 encodeURIComponent(this.dataset.folder) +
107                                 "?mode=none&theme=frio";
108                         FileBrowser.folder = this.dataset.folder;
109
110                         FileBrowser.loadContent(url);
111                 });
112
113                 //Embed on click
114                 $(".fbrowser").on("click", ".photo-album-photo-link", function (e) {
115                         e.preventDefault();
116
117                         var embed = "";
118                         if (FileBrowser.type === "image") {
119                                 embed = "[url=" + this.dataset.link + "][img=" + this.dataset.img + "][/img][/url]";
120                         }
121                         if (FileBrowser.type === "file") {
122                                 // attachment links are "baseurl/attach/id"; we need id
123                                 embed = "[attachment]" + this.dataset.link.split("/").pop() + "[/attachment]";
124                         }
125
126                         // Delete prefilled Text of the comment input
127                         // Note: not the best solution but function commentOpenUI don't
128                         // work as expected (we need a way to wait until commentOpenUI would be finished).
129                         // As for now we insert pieces of this function here
130                         if (commentElm !== null && typeof commentElm !== "undefined") {
131                                 if (commentElm.value === "") {
132                                         $("#comment-edit-text-" + FileBrowser.id)
133                                                 .addClass("comment-edit-text-full")
134                                                 .removeClass("comment-edit-text-empty");
135                                         $("#comment-edit-submit-wrapper-" + FileBrowser.id).show();
136                                         $("#comment-edit-text-" + FileBrowser.id).attr("tabindex", "9");
137                                         $("#comment-edit-submit-" + FileBrowser.id).attr("tabindex", "10");
138                                 }
139                         }
140
141                         console.log(FileBrowser.event, this.dataset.filename, embed, FileBrowser.id);
142
143                         $("body").trigger(FileBrowser.event, [this.dataset.filename, embed, FileBrowser.id, this.dataset.img]);
144
145                         // Close model
146                         $("#modal").modal("hide");
147                         // Update autosize for this textarea
148                         autosize.update($(".text-autosize"));
149                 });
150
151                 // EventListener for switching between image and file mode
152                 $(".fbrowser").on("click", ".fbswitcher .btn", function (e) {
153                         e.preventDefault();
154                         FileBrowser.type = this.getAttribute("data-mode");
155                         $(".fbrowser")
156                                 .removeClass()
157                                 .addClass("fbrowser " + FileBrowser.type);
158                         url = baseurl + "/fbrowser/" + FileBrowser.type + "?mode=none&theme=frio";
159
160                         FileBrowser.loadContent(url);
161                 });
162         },
163
164         // Initialize the AjaxUpload for the upload buttons
165         uploadButtons: function () {
166                 if ($("#upload-image").length) {
167                         //AjaxUpload for images
168                         var image_uploader = new window.AjaxUpload("upload-image", {
169                                 action:
170                                         "wall_upload/" +
171                                         FileBrowser.nickname +
172                                         "?response=json&album=" +
173                                         encodeURIComponent(FileBrowser.folder),
174                                 name: "userfile",
175                                 responseType: "json",
176                                 onSubmit: function (file, ext) {
177                                         $(".fbrowser-content").hide();
178                                         $(".fbrowser .profile-rotator-wrapper").show();
179                                         $(".error").addClass("hidden");
180                                 },
181                                 onComplete: function (file, response) {
182                                         if (response["error"] != undefined) {
183                                                 $(".error span").html(response["error"]);
184                                                 $(".error").removeClass("hidden");
185                                                 $(".fbrowser .profile-rotator-wrapper").hide();
186                                                 $(".fbrowser-content").show();
187                                                 return;
188                                         }
189
190                                         // load new content to fbrowser window
191                                         FileBrowser.loadContent(
192                                                 baseurl +
193                                                         "/fbrowser/" +
194                                                         FileBrowser.type +
195                                                         "/" +
196                                                         encodeURIComponent(FileBrowser.folder) +
197                                                         "?mode=none&theme=frio",
198                                         );
199                                 },
200                         });
201                 }
202
203                 if ($("#upload-file").length) {
204                         //AjaxUpload for files
205                         var file_uploader = new window.AjaxUpload("upload-file", {
206                                 action: "wall_attach/" + FileBrowser.nickname + "?response=json",
207                                 name: "userfile",
208                                 onSubmit: function (file, ext) {
209                                         $(".fbrowser-content").hide();
210                                         $(".fbrowser .profile-rotator-wrapper").show();
211                                         $(".error").addClass("hidden");
212                                 },
213                                 onComplete: function (file, response) {
214                                         if (response["error"] != undefined) {
215                                                 $(".error span").html(response["error"]);
216                                                 $(".error").removeClass("hidden");
217                                                 $("#profile-rotator").hide();
218                                                 $(".fbrowser-content").show();
219                                                 return;
220                                         }
221
222                                         var url = baseurl + "/fbrowser/" + FileBrowser.type + "?mode=none&theme=frio";
223                                         // Load new content to fbrowser window
224                                         FileBrowser.loadContent(url);
225                                 },
226                         });
227                 }
228         },
229
230         // Stuff which should be executed if ne content was loaded
231         postLoad: function () {
232                 FileBrowser.initGallery();
233                 $(".fbrowser .fbswitcher .btn").removeClass("active");
234                 $(".fbrowser .fbswitcher [data-mode=" + FileBrowser.type + "]").addClass("active");
235                 // We need to add the AjaxUpload to the button
236                 FileBrowser.uploadButtons();
237         },
238
239         // Load new content (e.g. change photo album)
240         loadContent: function (url) {
241                 $(".fbrowser-content").hide();
242                 $(".fbrowser .profile-rotator-wrapper").show();
243
244                 // load new content to fbrowser window
245                 $(".fbrowser").load(url, function (responseText, textStatus) {
246                         $(".profile-rotator-wrapper").hide();
247                         if (textStatus === "success") {
248                                 $(".fbrowser_content").show();
249                                 FileBrowser.postLoad();
250                         }
251                 });
252         },
253
254         // Initialize justified Gallery
255         initGallery: function () {
256                 $(".fbrowser.image .fbrowser-content-container").justifiedGallery({
257                         rowHeight: 80,
258                         margins: 4,
259                         border: 0,
260                 });
261         },
262 };
263 // @license-end