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