]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/js/filebrowser.js
Merge pull request #8131 from nupplaphil/task/cleanup_lock
[friendica.git] / view / theme / frio / js / filebrowser.js
1 /**
2  * Filebrowser - Friendica Communications Server
3  *
4  * Copyright (c) 2010-2015 the Friendica Project
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This code handle user interaction for image/file upload/browser dialog.
12  * Is loaded from filebrowser_plain.tpl
13  *
14  * To load filebrowser in colorbox, call
15  *
16  *      Dialog.doImageBrowser(eventname, id);
17  *
18  * or
19  *
20  *      Dialog.doFileBrowser(eventname, id);
21  *
22  * where:
23  *
24  *              eventname: event name to catch return value
25  *              id: id returned to event handler
26  *
27  * When user select an item, an event in fired in parent page, on body element
28  * The event is named
29  *
30  *              fbrowser.<type>.[<eventname>]
31  *
32  * <type> will be one of "image" or "file", and the event handler will
33  * get the following params:
34  *
35  *              filemane: filename of item choosed by user
36  *              embed: bbcode to embed element into posts
37  *              id: id from caller code
38  *
39  * example:
40  *
41  *              // open dialog for select an image for a textarea with id "myeditor"
42  *              var id="myeditor";
43  *              Dialog.doImageBrowser("example", id);
44  *
45  *              // setup event handler to get user selection
46  *              $("body").on("fbrowser.image.example", function(event, filename, bbcode, id) {
47  *                      // close colorbox
48  *                      $.colorbox.close();
49  *                      // replace textxarea text with bbcode
50  *                      $(id).value = bbcode;
51  *              });
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
67 var FileBrowser = {
68         nickname : "",
69         type : "",
70         event: "",
71         folder: "",
72         id : null,
73
74         init: function(nickname, type, hash) {
75                 FileBrowser.nickname = nickname;
76                 FileBrowser.type = type;
77                 FileBrowser.event = "fbrowser."+type;
78
79                 if (hash!=="") {
80                         var h = hash.replace("#","");
81                         var destination = h.split("-")[0];
82                         FileBrowser.id = h.split("-")[1];
83                         FileBrowser.event = FileBrowser.event + "." + destination;
84                         if (destination === "comment") {
85                                 // Get the comment textimput field
86                                 var commentElm = document.getElementById("comment-edit-text-" + FileBrowser.id);
87                         }
88                 };
89
90                 console.log("FileBrowser: " + nickname,  type, FileBrowser.event, FileBrowser.id);
91
92                 FileBrowser.postLoad();
93
94                 $(".error .close").on("click", function(e) {
95                         e.preventDefault();
96                         $(".error").addClass("hidden");
97                 });
98
99                 // Click on album link
100                 $(".fbrowser").on("click", ".folders a, .path a", function(e) {
101                         e.preventDefault();
102                         var url = baseurl + "/fbrowser/" + FileBrowser.type + "/" + this.dataset.folder + "?mode=none&theme=frio";
103                         FileBrowser.folder = this.dataset.folder;
104
105                         FileBrowser.loadContent(url);
106                 });
107
108                 //Embed on click
109                 $(".fbrowser").on('click', ".photo-album-photo-link", function(e) {
110                         e.preventDefault();
111
112                         var embed = "";
113                         if (FileBrowser.type === "image") {
114                                 embed = "[url=" + this.dataset.link + "][img=" + this.dataset.img + "][/img][/url]";
115                         }
116                         if (FileBrowser.type === "file") {
117                                 // attachment links are "baseurl/attach/id"; we need id
118                                 embed = "[attachment]" + this.dataset.link.split("/").pop() + "[/attachment]";
119                         }
120
121                         // Delete prefilled Text of the comment input
122                         // Note: not the best solution but function commentOpenUI don't
123                         // work as expected (we need a way to wait until commentOpenUI would be finished).
124                         // As for now we insert pieces of this function here
125                         if ((commentElm !== null) && (typeof commentElm !== "undefined")) {
126                                 if (commentElm.value === "") {
127                                         $("#comment-edit-text-" + FileBrowser.id).addClass("comment-edit-text-full").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
135                         console.log(FileBrowser.event, this.dataset.filename, embed, FileBrowser.id);
136
137                         $("body").trigger(FileBrowser.event, [
138                                 this.dataset.filename,
139                                 embed,
140                                 FileBrowser.id,
141                                 this.dataset.img
142                         ]);
143
144                         // Close model
145                         $('#modal').modal('hide');
146                         // Update autosize for this textarea
147                         autosize.update($(".text-autosize"));
148                 });
149
150                 // EventListener for switching between image and file mode
151                 $(".fbrowser").on('click', ".fbswitcher .btn", function(e) {
152                         e.preventDefault();
153                         FileBrowser.type = this.getAttribute("data-mode");
154                         $(".fbrowser").removeClass().addClass("fbrowser " + FileBrowser.type);
155                         url = baseurl + "/fbrowser/" + FileBrowser.type + "?mode=none&theme=frio";
156
157                         FileBrowser.loadContent(url);
158                 });
159         },
160
161         // Initialize the AjaxUpload for the upload buttons
162         uploadButtons: function() {
163                 if ($("#upload-image").length) {
164                         // To get the albumname we need to convert it from hex
165                         var albumname = hex2bin(FileBrowser.folder);
166                         //AjaxUpload for images
167                         var image_uploader = new window.AjaxUpload(
168                                 'upload-image',
169                                 {       action: 'wall_upload/' + FileBrowser.nickname + '?response=json&album=' + albumname,
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                                                 var url = baseurl + "/fbrowser/" + FileBrowser.type + "/" + FileBrowser.folder + "?mode=none&theme=frio";
187                                                 // load new content to fbrowser window
188                                                 FileBrowser.loadContent(url);
189                                         }
190                                 }
191                         );
192                 }
193
194                 if ($("#upload-file").length) {
195                         //AjaxUpload for files
196                         var file_uploader = new window.AjaxUpload(
197                                 'upload-file',
198                                 {       action: 'wall_attach/' + FileBrowser.nickname + '?response=json',
199                                         name: 'userfile',
200                                         onSubmit: function(file, ext) {
201                                                 $(".fbrowser-content").hide();
202                                                 $(".fbrowser .profile-rotator-wrapper").show();
203                                                 $(".error").addClass('hidden');
204                                         },
205                                         onComplete: function(file,response) {
206                                                 if (response['error']!= undefined) {
207                                                         $(".error span").html(response['error']);
208                                                         $(".error").removeClass('hidden');
209                                                         $('#profile-rotator').hide();
210                                                         $(".fbrowser-content").show();
211                                                         return;
212                                                 }
213
214                                                 var url = baseurl + "/fbrowser/" + FileBrowser.type + "?mode=none&theme=frio";
215                                                 // Load new content to fbrowser window
216                                                 FileBrowser.loadContent(url);
217                                         }
218                                 }
219                         );
220                 }
221         },
222
223         // Stuff which should be executed if ne content was loaded
224         postLoad: function() {
225                 FileBrowser.initGallery();
226                 $(".fbrowser .fbswitcher .btn").removeClass("active");
227                 $(".fbrowser .fbswitcher [data-mode=" + FileBrowser.type + "]").addClass("active");
228                 // We need to add the AjaxUpload to the button
229                 FileBrowser.uploadButtons();
230         },
231
232         // Load new content (e.g. change photo album)
233         loadContent: function(url) {
234                 $(".fbrowser-content").hide();
235                 $(".fbrowser .profile-rotator-wrapper").show();
236
237                 // load new content to fbrowser window
238                 $(".fbrowser").load(url, function(responseText, textStatus) {
239                         $(".profile-rotator-wrapper").hide();
240                         if (textStatus === 'success') {
241                                 $(".fbrowser_content").show();
242                                 FileBrowser.postLoad();
243                         }
244                 });
245         },
246
247         // Initialize justified Gallery
248         initGallery: function() {
249                 $(".fbrowser.image .fbrowser-content-container").justifiedGallery({
250                         'rowHeight': 80,
251                         'margins': 4,
252                         'border': 0
253                 });
254         }
255 };