]> git.mxchange.org Git - friendica.git/blob - view/js/filebrowser.js
Merge pull request #7988 from friendica/MrPetovan-notice
[friendica.git] / view / 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 var FileBrowser = {
56         nickname : "",
57         type : "",
58         event: "",
59         id : null,
60
61         init: function(nickname, type) {
62                 FileBrowser.nickname = nickname;
63                 FileBrowser.type = type;
64                 FileBrowser.event = "fbrowser."+type;
65                 if (location['hash']!=="") {
66                         var h = location['hash'].replace("#","");
67                         FileBrowser.event = FileBrowser.event + "." + h.split("-")[0];
68                         FileBrowser.id = h.split("-")[1];
69                 }
70
71                 console.log("FileBrowser:", nickname, type,FileBrowser.event, FileBrowser.id );
72
73                 $(".error a.close").on("click", function(e) {
74                         e.preventDefault();
75                         $(".error").addClass("hidden");
76                 });
77
78                 $(".folders a, .path a").on("click", function(e){
79                         e.preventDefault();
80                         var url = baseurl + "/fbrowser/" + FileBrowser.type + "/" + this.dataset.folder + "?mode=minimal" + location['hash'];
81                         location.href = url;
82                 });
83
84                 $(".photo-album-photo-link").on('click', function(e){
85                         e.preventDefault();
86
87                         var embed = "";
88                         if (FileBrowser.type == "image") {
89                                 embed = "[url="+this.dataset.link+"][img="+this.dataset.img+"][/img][/url]";
90                         }
91                         if (FileBrowser.type=="file") {
92                                 // attachment links are "baseurl/attach/id"; we need id
93                                 embed = "[attachment]"+this.dataset.link.split("/").pop()+"[/attachment]";
94                         }
95                         console.log(FileBrowser.event, this.dataset.filename, embed, FileBrowser.id);
96                         parent.$("body").trigger(FileBrowser.event, [
97                                 this.dataset.filename,
98                                 embed,
99                                 FileBrowser.id
100                         ]);
101
102                 });
103
104                 if ($("#upload-image").length)
105                         var image_uploader = new window.AjaxUpload(
106                                 'upload-image',
107                                 { action: 'wall_upload/'+FileBrowser.nickname+'?response=json',
108                                         name: 'userfile',
109                                         responseType: 'json',
110                                         onSubmit: function(file,ext) { $('#profile-rotator').show(); $(".error").addClass('hidden'); },
111                                         onComplete: function(file,response) {
112                                                 if (response['error']!= undefined) {
113                                                         $(".error span").html(response['error']);
114                                                         $(".error").removeClass('hidden');
115                                                         $('#profile-rotator').hide();
116                                                         return;
117                                                 }
118                                                 location = baseurl + "/fbrowser/image/?mode=minimal"+location['hash'];
119                                                 location.reload(true);
120                                         }
121                                 }
122                         );
123
124                 if ($("#upload-file").length)
125                         var file_uploader = new window.AjaxUpload(
126                                 'upload-file',
127                                 { action: 'wall_attach/'+FileBrowser.nickname+'?response=json',
128                                         name: 'userfile',
129                                         onSubmit: function(file,ext) { $('#profile-rotator').show(); $(".error").addClass('hidden'); },
130                                         onComplete: function(file,response) {
131                                                 if (response['error']!= undefined) {
132                                                         $(".error span").html(response['error']);
133                                                         $(".error").removeClass('hidden');
134                                                         $('#profile-rotator').hide();
135                                                         return;
136                                                 }
137                                                 location = baseurl + "/fbrowser/file/?mode=minimal"+location['hash'];
138                                                 location.reload(true);
139                                         }
140                                 }
141                 );
142         }
143 };
144 // @license-end