]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/js/filebrowser.js
Merge remote-tracking branch 'friendica/stable' into develop
[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 + "/" + encodeURIComponent(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                         //AjaxUpload for images
165                         var image_uploader = new window.AjaxUpload(
166                                 'upload-image',
167                                 {
168                                         action: 'wall_upload/' + FileBrowser.nickname + '?response=json&album=' + encodeURIComponent(FileBrowser.folder),
169                                         name: 'userfile',
170                                         responseType: 'json',
171                                         onSubmit: function(file, ext) {
172                                                 $(".fbrowser-content").hide();
173                                                 $(".fbrowser .profile-rotator-wrapper").show();
174                                                 $(".error").addClass('hidden');
175                                         },
176                                         onComplete: function(file,response) {
177                                                 if (response['error'] != undefined) {
178                                                         $(".error span").html(response['error']);
179                                                         $(".error").removeClass('hidden');
180                                                         $(".fbrowser .profile-rotator-wrapper").hide();
181                                                         $(".fbrowser-content").show();
182                                                         return;
183                                                 }
184
185                                                 // load new content to fbrowser window
186                                                 FileBrowser.loadContent(baseurl + '/fbrowser/' + FileBrowser.type + '/' + encodeURIComponent(FileBrowser.folder) + '?mode=none&theme=frio');
187                                         }
188                                 }
189                         );
190                 }
191
192                 if ($("#upload-file").length) {
193                         //AjaxUpload for files
194                         var file_uploader = new window.AjaxUpload(
195                                 'upload-file',
196                                 {       action: 'wall_attach/' + FileBrowser.nickname + '?response=json',
197                                         name: 'userfile',
198                                         onSubmit: function(file, ext) {
199                                                 $(".fbrowser-content").hide();
200                                                 $(".fbrowser .profile-rotator-wrapper").show();
201                                                 $(".error").addClass('hidden');
202                                         },
203                                         onComplete: function(file,response) {
204                                                 if (response['error']!= undefined) {
205                                                         $(".error span").html(response['error']);
206                                                         $(".error").removeClass('hidden');
207                                                         $('#profile-rotator').hide();
208                                                         $(".fbrowser-content").show();
209                                                         return;
210                                                 }
211
212                                                 var url = baseurl + "/fbrowser/" + FileBrowser.type + "?mode=none&theme=frio";
213                                                 // Load new content to fbrowser window
214                                                 FileBrowser.loadContent(url);
215                                         }
216                                 }
217                         );
218                 }
219         },
220
221         // Stuff which should be executed if ne content was loaded
222         postLoad: function() {
223                 FileBrowser.initGallery();
224                 $(".fbrowser .fbswitcher .btn").removeClass("active");
225                 $(".fbrowser .fbswitcher [data-mode=" + FileBrowser.type + "]").addClass("active");
226                 // We need to add the AjaxUpload to the button
227                 FileBrowser.uploadButtons();
228         },
229
230         // Load new content (e.g. change photo album)
231         loadContent: function(url) {
232                 $(".fbrowser-content").hide();
233                 $(".fbrowser .profile-rotator-wrapper").show();
234
235                 // load new content to fbrowser window
236                 $(".fbrowser").load(url, function(responseText, textStatus) {
237                         $(".profile-rotator-wrapper").hide();
238                         if (textStatus === 'success') {
239                                 $(".fbrowser_content").show();
240                                 FileBrowser.postLoad();
241                         }
242                 });
243         },
244
245         // Initialize justified Gallery
246         initGallery: function() {
247                 $(".fbrowser.image .fbrowser-content-container").justifiedGallery({
248                         'rowHeight': 80,
249                         'margins': 4,
250                         'border': 0
251                 });
252         }
253 };