]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/js/filebrowser.js
fix: undefined search tag #9789
[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-2015 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 /*
57  * IMPORTANT
58  *
59  *  This is a modified version to work with
60  *  the frio theme.and bootstrap modals
61  *
62  *  The origninal file is under:
63  *  js/filebrowser.js
64  *
65  */
66
67
68 var FileBrowser = {
69         nickname : "",
70         type : "",
71         event: "",
72         folder: "",
73         id : null,
74
75         init: function(nickname, type, hash) {
76                 FileBrowser.nickname = nickname;
77                 FileBrowser.type = type;
78                 FileBrowser.event = "fbrowser."+type;
79
80                 if (hash!=="") {
81                         var h = hash.replace("#","");
82                         var destination = h.split("-")[0];
83                         FileBrowser.id = h.split("-")[1];
84                         FileBrowser.event = FileBrowser.event + "." + destination;
85                         if (destination === "comment") {
86                                 // Get the comment textimput field
87                                 var commentElm = document.getElementById("comment-edit-text-" + FileBrowser.id);
88                         }
89                 };
90
91                 console.log("FileBrowser: " + nickname,  type, FileBrowser.event, FileBrowser.id);
92
93                 FileBrowser.postLoad();
94
95                 $(".error .close").on("click", function(e) {
96                         e.preventDefault();
97                         $(".error").addClass("hidden");
98                 });
99
100                 // Click on album link
101                 $(".fbrowser").on("click", ".folders a, .path a", function(e) {
102                         e.preventDefault();
103                         var url = baseurl + "/fbrowser/" + FileBrowser.type + "/" + encodeURIComponent(this.dataset.folder) + "?mode=none&theme=frio";
104                         FileBrowser.folder = this.dataset.folder;
105
106                         FileBrowser.loadContent(url);
107                 });
108
109                 //Embed on click
110                 $(".fbrowser").on('click', ".photo-album-photo-link", function(e) {
111                         e.preventDefault();
112
113                         var embed = "";
114                         if (FileBrowser.type === "image") {
115                                 embed = "[url=" + this.dataset.link + "][img=" + this.dataset.img + "][/img][/url]";
116                         }
117                         if (FileBrowser.type === "file") {
118                                 // attachment links are "baseurl/attach/id"; we need id
119                                 embed = "[attachment]" + this.dataset.link.split("/").pop() + "[/attachment]";
120                         }
121
122                         // Delete prefilled Text of the comment input
123                         // Note: not the best solution but function commentOpenUI don't
124                         // work as expected (we need a way to wait until commentOpenUI would be finished).
125                         // As for now we insert pieces of this function here
126                         if ((commentElm !== null) && (typeof commentElm !== "undefined")) {
127                                 if (commentElm.value === "") {
128                                         $("#comment-edit-text-" + FileBrowser.id).addClass("comment-edit-text-full").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
136                         console.log(FileBrowser.event, this.dataset.filename, embed, FileBrowser.id);
137
138                         $("body").trigger(FileBrowser.event, [
139                                 this.dataset.filename,
140                                 embed,
141                                 FileBrowser.id,
142                                 this.dataset.img
143                         ]);
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").removeClass().addClass("fbrowser " + FileBrowser.type);
156                         url = baseurl + "/fbrowser/" + FileBrowser.type + "?mode=none&theme=frio";
157
158                         FileBrowser.loadContent(url);
159                 });
160         },
161
162         // Initialize the AjaxUpload for the upload buttons
163         uploadButtons: function() {
164                 if ($("#upload-image").length) {
165                         //AjaxUpload for images
166                         var image_uploader = new window.AjaxUpload(
167                                 'upload-image',
168                                 {
169                                         action: 'wall_upload/' + FileBrowser.nickname + '?response=json&album=' + encodeURIComponent(FileBrowser.folder),
170                                         name: 'userfile',
171                                         responseType: 'json',
172                                         onSubmit: function(file, ext) {
173                                                 $(".fbrowser-content").hide();
174                                                 $(".fbrowser .profile-rotator-wrapper").show();
175                                                 $(".error").addClass('hidden');
176                                         },
177                                         onComplete: function(file,response) {
178                                                 if (response['error'] != undefined) {
179                                                         $(".error span").html(response['error']);
180                                                         $(".error").removeClass('hidden');
181                                                         $(".fbrowser .profile-rotator-wrapper").hide();
182                                                         $(".fbrowser-content").show();
183                                                         return;
184                                                 }
185
186                                                 // load new content to fbrowser window
187                                                 FileBrowser.loadContent(baseurl + '/fbrowser/' + FileBrowser.type + '/' + encodeURIComponent(FileBrowser.folder) + '?mode=none&theme=frio');
188                                         }
189                                 }
190                         );
191                 }
192
193                 if ($("#upload-file").length) {
194                         //AjaxUpload for files
195                         var file_uploader = new window.AjaxUpload(
196                                 'upload-file',
197                                 {       action: 'wall_attach/' + FileBrowser.nickname + '?response=json',
198                                         name: 'userfile',
199                                         onSubmit: function(file, ext) {
200                                                 $(".fbrowser-content").hide();
201                                                 $(".fbrowser .profile-rotator-wrapper").show();
202                                                 $(".error").addClass('hidden');
203                                         },
204                                         onComplete: function(file,response) {
205                                                 if (response['error']!= undefined) {
206                                                         $(".error span").html(response['error']);
207                                                         $(".error").removeClass('hidden');
208                                                         $('#profile-rotator').hide();
209                                                         $(".fbrowser-content").show();
210                                                         return;
211                                                 }
212
213                                                 var url = baseurl + "/fbrowser/" + FileBrowser.type + "?mode=none&theme=frio";
214                                                 // Load new content to fbrowser window
215                                                 FileBrowser.loadContent(url);
216                                         }
217                                 }
218                         );
219                 }
220         },
221
222         // Stuff which should be executed if ne content was loaded
223         postLoad: function() {
224                 FileBrowser.initGallery();
225                 $(".fbrowser .fbswitcher .btn").removeClass("active");
226                 $(".fbrowser .fbswitcher [data-mode=" + FileBrowser.type + "]").addClass("active");
227                 // We need to add the AjaxUpload to the button
228                 FileBrowser.uploadButtons();
229         },
230
231         // Load new content (e.g. change photo album)
232         loadContent: function(url) {
233                 $(".fbrowser-content").hide();
234                 $(".fbrowser .profile-rotator-wrapper").show();
235
236                 // load new content to fbrowser window
237                 $(".fbrowser").load(url, function(responseText, textStatus) {
238                         $(".profile-rotator-wrapper").hide();
239                         if (textStatus === 'success') {
240                                 $(".fbrowser_content").show();
241                                 FileBrowser.postLoad();
242                         }
243                 });
244         },
245
246         // Initialize justified Gallery
247         initGallery: function() {
248                 $(".fbrowser.image .fbrowser-content-container").justifiedGallery({
249                         'rowHeight': 80,
250                         'margins': 4,
251                         'border': 0
252                 });
253         }
254 };
255 // @license-end