1 // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPLv3-or-later
3 * Filebrowser - Friendica Communications Server
5 * Copyright (c) 2010-2021, the Friendica project
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.
12 * This code handle user interaction for image/file upload/browser dialog.
13 * Is loaded from filebrowser_plain.tpl
15 * To load filebrowser in colorbox, call
17 * Dialog.doImageBrowser(eventname, id);
21 * Dialog.doFileBrowser(eventname, id);
25 * eventname: event name to catch return value
26 * id: id returned to event handler
28 * When user select an item, an event in fired in parent page, on body element
31 * fbrowser.<type>.[<eventname>]
33 * <type> will be one of "image" or "file", and the event handler will
34 * get the following params:
36 * filemane: filename of item choosed by user
37 * embed: bbcode to embed element into posts
38 * id: id from caller code
42 * // open dialog for select an image for a textarea with id "myeditor"
44 * Dialog.doImageBrowser("example", id);
46 * // setup event handler to get user selection
47 * $("body").on("fbrowser.image.example", function(event, filename, bbcode, id) {
50 * // replace textxarea text with bbcode
51 * $(id).value = bbcode;
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];
71 console.log("FileBrowser:", nickname, type,FileBrowser.event, FileBrowser.id );
73 $(".error a.close").on("click", function(e) {
75 $(".error").addClass("hidden");
78 $(".folders a, .path a").on("click", function(e){
80 location.href = baseurl + "/fbrowser/" + FileBrowser.type + "/" + encodeURIComponent(this.dataset.folder) + "?mode=minimal" + location['hash'];
83 $(".photo-album-photo-link").on('click', function(e){
87 if (FileBrowser.type == "image") {
88 embed = "[url="+this.dataset.link+"][img="+this.dataset.img+"]"+this.dataset.alt+"[/img][/url]";
90 if (FileBrowser.type=="file") {
91 // attachment links are "baseurl/attach/id"; we need id
92 embed = "[attachment]"+this.dataset.link.split("/").pop()+"[/attachment]";
94 console.log(FileBrowser.event, this.dataset.filename, embed, FileBrowser.id);
95 parent.$("body").trigger(FileBrowser.event, [
96 this.dataset.filename,
103 if ($("#upload-image").length)
104 var image_uploader = new window.AjaxUpload(
106 { action: 'profile/' + FileBrowser.nickname + '/photos/upload?response=json',
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();
117 location = baseurl + "/fbrowser/image/?mode=minimal"+location['hash'];
118 location.reload(true);
123 if ($("#upload-file").length)
124 var file_uploader = new window.AjaxUpload(
126 { action: 'profile/' + FileBrowser.nickname + '/attachment/upload?response=json',
128 responseType: 'json',
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();
137 location = baseurl + "/fbrowser/file/?mode=minimal"+location['hash'];
138 location.reload(true);