]> git.mxchange.org Git - friendica.git/blob - js/filebrowser.js
Merge pull request #2026 from silke/vagrant-update
[friendica.git] / js / filebrowser.js
1 /**\r
2  * Filebrowser - Friendica Communications Server\r
3  *\r
4  * Copyright (c) 2010-2013 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  *     $.colorbox({href: ulr, iframe:true,innerWidth:'500px',innerHeight:'400px'})\r
17  *\r
18  * where url is:\r
19  *\r
20  *              <baseurl>/fbrowser/<type>/?mode=minimal[#<eventname>-<id>]\r
21  *\r
22  *              baseurl: baseurl from friendica\r
23  *              type: one of "image", "file"\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  *      with params:\r
33  *\r
34  *              filemane: filename of item choosed by user\r
35  *              embed: bbcode to embed element into posts\r
36  *              id: id from url\r
37  *\r
38  *  example:\r
39  *\r
40  *      // open dialog for select an image for a textarea with id "myeditor"\r
41  *              var id="myeditor";\r
42  *              $.colorbox({href: baseurl + "/fbrowser/image/?mode=minimal#example-"+id, iframe:true,innerWidth:'500px',innerHeight:'400px'})\r
43  *\r
44  *              // setup event handler to get user selection\r
45  *              $("body").on("fbrowser.image.example", function(event, filename, bbcode, id) {\r
46  *                      // close colorbox\r
47  *                      $.colorbox.close();\r
48  *                      // replace textxarea text with bbcode\r
49  *                      $(id).value = bbcode;\r
50  *              });\r
51  **/\r
52 \r
53 var FileBrowser = {\r
54         nickname : "",\r
55         type : "",\r
56         event: "",\r
57         id : null,\r
58 \r
59         init: function(nickname, type) {\r
60                 FileBrowser.nickname = nickname;\r
61                 FileBrowser.type = type;\r
62                 FileBrowser.event = "fbrowser."+type;\r
63                 if (location['hash']!=="") {\r
64                         var h = location['hash'].replace("#","");\r
65                         FileBrowser.event = FileBrowser.event + "." + h.split("-")[0];\r
66                         FileBrowser.id = h.split("-")[1];\r
67                 }\r
68 \r
69                 console.log("FileBrowser:", nickname, type,FileBrowser.event, FileBrowser.id );\r
70 \r
71                 $(".error a.close").on("click", function(e) {\r
72                         e.preventDefault();\r
73                         $(".error").addClass("hidden");\r
74                 });\r
75 \r
76                 $(".folders a, .path a").on("click", function(e){\r
77                         e.preventDefault();\r
78                         var url = baseurl + "/fbrowser/" + FileBrowser.type + "/" + this.dataset.folder + "?mode=minimal" + location['hash'];\r
79                         location.href = url;\r
80                 });\r
81 \r
82                 $(".photo-album-photo-link").on('click', function(e){\r
83                         e.preventDefault();\r
84 \r
85                         var embed = "";\r
86                         if (FileBrowser.type == "image") {\r
87                                 embed = "[url="+this.dataset.link+"][img]"+this.dataset.img+"[/img][/url]";\r
88                         }\r
89                         if (FileBrowser.type=="file") {\r
90                                 // attachment links are "baseurl/attach/id"; we need id\r
91                                 embed = "[attachment]"+this.dataset.link.split("/").pop()+"[/attachment]";\r
92                         }\r
93                         console.log(FileBrowser.event, this.dataset.filename, embed, FileBrowser.id);\r
94                         parent.$("body").trigger(FileBrowser.event, [\r
95                                 this.dataset.filename,\r
96                                 embed,\r
97                                 FileBrowser.id\r
98                         ]);\r
99 \r
100                 });\r
101 \r
102                 if ($("#upload-image").length)\r
103                         var image_uploader = new window.AjaxUpload(\r
104                                 'upload-image',\r
105                                 { action: 'wall_upload/'+FileBrowser.nickname+'?response=json',\r
106                                         name: 'userfile',\r
107                                         responseType: 'json',\r
108                                         onSubmit: function(file,ext) { $('#profile-rotator').show(); $(".error").addClass('hidden'); },\r
109                                         onComplete: function(file,response) {\r
110                                                 if (response['error']!= undefined) {\r
111                                                         $(".error span").html(response['error']);\r
112                                                         $(".error").removeClass('hidden');\r
113                                                         $('#profile-rotator').hide();\r
114                                                         return;\r
115                                                 }\r
116                                                 location = baseurl + "/fbrowser/image/?mode=minimal"+location['hash'];\r
117                                                 location.reload(true);\r
118                                         }\r
119                                 }\r
120                         );\r
121 \r
122                 if ($("#upload-file").length)\r
123                         var file_uploader = new window.AjaxUpload(\r
124                                 'upload-file',\r
125                                 { action: 'wall_attach/'+FileBrowser.nickname+'?response=json',\r
126                                         name: 'userfile',\r
127                                         onSubmit: function(file,ext) { $('#profile-rotator').show(); $(".error").addClass('hidden'); },\r
128                                         onComplete: function(file,response) {\r
129                                                 if (response['error']!= undefined) {\r
130                                                         $(".error span").html(response['error']);\r
131                                                         $(".error").removeClass('hidden');\r
132                                                         $('#profile-rotator').hide();\r
133                                                         return;\r
134                                                 }\r
135                                                 location = baseurl + "/fbrowser/file/?mode=minimal"+location['hash'];\r
136                                                 location.reload(true);\r
137                                         }\r
138                                 }\r
139                 );\r
140         }\r
141 };\r
142 \r