]> git.mxchange.org Git - friendica.git/blob - js/filebrowser.js
0fe501d548906379693703ac1e47b286b5f41139
[friendica.git] / js / filebrowser.js
1 /**\r
2  * Filebrowser - Friendica Communications Server\r
3  *\r
4  * Copyright (c) 2010-2015 the Friendica Project\r
5  *\r
6  * This program is free software: you can redistribute it and/or modify\r
7  * it under the terms of the GNU Affero General Public License as published by\r
8  * the Free Software Foundation, either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * This code handle user interaction for image/file upload/browser dialog.\r
12  * Is loaded from filebrowser_plain.tpl\r
13  *\r
14  * To load filebrowser in colorbox, call\r
15  *\r
16  *      Dialog.doImageBrowser(eventname, id);\r
17  *\r
18  * or\r
19  *\r
20  *      Dialog.doFileBrowser(eventname, id);\r
21  *\r
22  * where:\r
23  *\r
24  *              eventname: event name to catch return value\r
25  *              id: id returned to event handler\r
26  *\r
27  * When user select an item, an event in fired in parent page, on body element\r
28  * The event is named\r
29  *\r
30  *              fbrowser.<type>.[<eventname>]\r
31  *\r
32  * <type> will be one of "image" or "file", and the event handler will\r
33  * get the following params:\r
34  *\r
35  *              filemane: filename of item choosed by user\r
36  *              embed: bbcode to embed element into posts\r
37  *              id: id from caller code\r
38  *\r
39  * example:\r
40  *\r
41  *              // open dialog for select an image for a textarea with id "myeditor"\r
42  *              var id="myeditor";\r
43  *              Dialog.doImageBrowser("example", id);\r
44  *\r
45  *              // setup event handler to get user selection\r
46  *              $("body").on("fbrowser.image.example", function(event, filename, bbcode, id) {\r
47  *                      // close colorbox\r
48  *                      $.colorbox.close();\r
49  *                      // replace textxarea text with bbcode\r
50  *                      $(id).value = bbcode;\r
51  *              });\r
52  **/\r
53 \r
54 \r
55 /*\r
56  * IMPORTANT\r
57  * \r
58  *  This is a modified version to work with \r
59  *  the frio theme.and bootstrap modals\r
60  *  \r
61  *  The origninal file is under:\r
62  *  js/filebrowser.js\r
63  * \r
64  */\r
65 \r
66 \r
67 var FileBrowser = {\r
68         nickname : "",\r
69         type : "",\r
70         event: "",\r
71         id : null,\r
72 \r
73         init: function(nickname, type, hash) {\r
74                 FileBrowser.nickname = nickname;\r
75                 FileBrowser.type = type;\r
76                 FileBrowser.event = "fbrowser."+type;\r
77 \r
78                 if (hash!=="") {\r
79                         var h = hash.replace("#","");\r
80                         var destination = h.split("-")[0];\r
81                         FileBrowser.id = h.split("-")[1];\r
82                         FileBrowser.event = FileBrowser.event + "." + destination;\r
83                         if (destination == "comment") {\r
84                                 // get the comment textimput field\r
85                                 var commentElm = document.getElementById("comment-edit-text-" + FileBrowser.id);\r
86                         }\r
87                 };\r
88 \r
89                 console.log("FileBrowser:", nickname, type,FileBrowser.event, FileBrowser.id );\r
90 \r
91                 $(".error a.close").on("click", function(e) {\r
92                         e.preventDefault();\r
93                         $(".error").addClass("hidden");\r
94                 });\r
95 \r
96                 $(".folders a, .path a").on("click", function(e){\r
97                         e.preventDefault();\r
98                         var url = baseurl + "/fbrowser/" + FileBrowser.type + "/" + this.dataset.folder + "?mode=none";\r
99 \r
100                         // load new content to fbrowser window\r
101                         $(".fbrowser").load(url,function(){\r
102                                 $(function() {FileBrowser.init(nickname, type, hash);});\r
103                         });\r
104                 });\r
105 \r
106                 //embed on click\r
107                 $(".photo-album-photo-link").on('click', function(e){\r
108                         e.preventDefault();\r
109 \r
110                         var embed = "";\r
111                         if (FileBrowser.type == "image") {\r
112                                 embed = "[url="+this.dataset.link+"][img]"+this.dataset.img+"[/img][/url]";\r
113                         }\r
114                         if (FileBrowser.type=="file") {\r
115                                 // attachment links are "baseurl/attach/id"; we need id\r
116                                 embed = "[attachment]"+this.dataset.link.split("/").pop()+"[/attachment]";\r
117                         }\r
118 \r
119                         // Delete prefilled Text of the comment input\r
120                         // Note: not the best solution but function commentOpenUI don't\r
121                         // work as expected (we need a way to wait until commentOpenUI would be finished).\r
122                         // As for now we insert pieces of this function here\r
123                         if ((commentElm !== null) && (typeof commentElm !== "undefined")) {\r
124                                 if (commentElm.value == aStr.comment){\r
125                                         commentElm.value = "";\r
126                                         $("#comment-edit-text-" + FileBrowser.id).addClass("comment-edit-text-full").removeClass("comment-edit-text-empty");\r
127                                         $("#comment-edit-submit-wrapper-" + FileBrowser.id).show();\r
128                                         $("#comment-edit-text-" + FileBrowser.id).attr('tabindex','9');\r
129                                         $("#comment-edit-submit-" + FileBrowser.id).attr('tabindex','10');\r
130                                 }\r
131 \r
132                         }\r
133                         console.log(FileBrowser.event, this.dataset.filename, embed, FileBrowser.id);\r
134                         parent.$("body").trigger(FileBrowser.event, [\r
135                                 this.dataset.filename,\r
136                                 embed,\r
137                                 FileBrowser.id,\r
138                                 this.dataset.img\r
139                         ]);\r
140 \r
141                         // close model\r
142                         $('#modal').modal('hide');\r
143 //                      if (id!=="") {\r
144 //                              commentExpand(FileBrowser.id);\r
145 //                              //$("#comment-edit-text-558").empty();\r
146 //                      };\r
147 \r
148                 });\r
149 \r
150                 if ($("#upload-image").length)\r
151                         var image_uploader = new window.AjaxUpload(\r
152                                 'upload-image',\r
153                                 { action: 'wall_upload/'+FileBrowser.nickname+'?response=json',\r
154                                         name: 'userfile',\r
155                                         responseType: 'json',\r
156                                         onSubmit: function(file,ext) { $('#profile-rotator').show(); $(".error").addClass('hidden'); },\r
157                                         onComplete: function(file,response) {\r
158                                                 if (response['error']!= undefined) {\r
159                                                         $(".error span").html(response['error']);\r
160                                                         $(".error").removeClass('hidden');\r
161                                                         $('#profile-rotator').hide();\r
162                                                         return;\r
163                                                 }\r
164 //                                              location = baseurl + "/fbrowser/image/?mode=none"+location['hash'];\r
165 //                                              location.reload(true);\r
166 \r
167                                                 var url = baseurl + "/fbrowser/" + FileBrowser.type + "?mode=none"\r
168                                                 // load new content to fbrowser window\r
169                                                 $(".fbrowser").load(url,function(){\r
170                                                         $(function() {FileBrowser.init(nickname, type, hash);});\r
171                                                 });\r
172                                         }\r
173                                 }\r
174                         );\r
175 \r
176                 if ($("#upload-file").length)\r
177                         var file_uploader = new window.AjaxUpload(\r
178                                 'upload-file',\r
179                                 { action: 'wall_attach/'+FileBrowser.nickname+'?response=json',\r
180                                         name: 'userfile',\r
181                                         onSubmit: function(file,ext) { $('#profile-rotator').show(); $(".error").addClass('hidden'); },\r
182                                         onComplete: function(file,response) {\r
183                                                 if (response['error']!= undefined) {\r
184                                                         $(".error span").html(response['error']);\r
185                                                         $(".error").removeClass('hidden');\r
186                                                         $('#profile-rotator').hide();\r
187                                                         return;\r
188                                                 }\r
189 //                                              location = baseurl + "/fbrowser/file/?mode=none"+location['hash'];\r
190 //                                              location.reload(true);\r
191 \r
192                                                 var url = baseurl + "/fbrowser/" + FileBrowser.type + "?mode=none"\r
193                                                 // load new content to fbrowser window\r
194                                                 $(".fbrowser").load(url,function(){\r
195                                                         $(function() {FileBrowser.init(nickname, type, hash);});\r
196                                                 });\r
197                                         }\r
198                                 }\r
199                 );\r
200         },\r
201 };\r
202 \r