]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/js/modal.js
Some more settings moved to the admin frontend
[friendica.git] / view / theme / frio / js / modal.js
1 // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPLv3-or-later
2 /**
3  * Contains functions for bootstrap modal handling.
4  */
5 $(document).ready(function () {
6         // Clear bs modal on close.
7         // We need this to prevent that the modal displays old content.
8         $("body, footer").on("hidden.bs.modal", ".modal", function () {
9                 $(this).removeData("bs.modal");
10                 $("#modal-title").empty();
11                 $("#modal-body").empty();
12                 // Remove the file browser from jot (else we would have problems
13                 // with AjaxUpload.
14                 $(".fbrowser").remove();
15                 // Remove the AjaxUpload element.
16                 $(".ajaxbutton-wrapper").remove();
17         });
18
19         // Clear bs modal on close.
20         // We need this to prevent that the modal displays old content.
21         $("body").on("hidden.bs.modal", "#jot-modal", function () {
22                 // Restore cached jot at its hidden position ("#jot-content").
23                 $("#jot-content").append(jotcache);
24                 // Clear the jotcache.
25                 jotcache = "";
26                 // Destroy the attachment linkPreview for Jot.
27                 if (typeof linkPreview === "object") {
28                         linkPreview.destroy();
29                 }
30         });
31
32         // Navbar login.
33         $("body").on("click", "#nav-login", function (e) {
34                 e.preventDefault();
35                 Dialog.show(this.href, this.dataset.originalTitle || this.title);
36         });
37
38         // Jot nav menu..
39         $("body").on("click", "#jot-modal .jot-nav li .jot-nav-lnk", function (e) {
40                 e.preventDefault();
41                 toggleJotNav(this);
42         });
43
44         // Bookmarklet page needs an jot modal which appears automatically.
45         if (window.location.pathname.indexOf("/bookmarklet") >= 0 && $("#jot-modal").length) {
46                 jotShow();
47         }
48
49         // Open filebrowser for elements with the class "image-select"
50         // The following part handles the filebrowser for field_fileinput.tpl.
51         $("body").on("click", ".image-select", function () {
52                 // Set a extra attribute to mark the clicked button.
53                 this.setAttribute("image-input", "select");
54                 Dialog.doImageBrowser("input");
55         });
56
57         // Insert filebrowser images into the input field (field_fileinput.tpl).
58         $("body").on("fbrowser.photo.input", function (e, filename, embedcode, id, img) {
59                 // Select the clicked button by it's attribute.
60                 var elm = $("[image-input='select']");
61                 // Select the input field which belongs to this button.
62                 var input = elm.parent(".input-group").children("input");
63                 // Remove the special indicator attribut from the button.
64                 elm.removeAttr("image-input");
65                 // Insert the link from the image into the input field.
66                 input.val(img);
67         });
68
69         // Generic delegated event to open an anchor URL in a modal.
70         // Used in the hovercard.
71         document.getElementsByTagName("body")[0].addEventListener("click", function (e) {
72                 var target = e.target;
73                 while (target) {
74                         if (target.matches && target.matches("a.add-to-modal")) {
75                                 addToModal(target.href);
76                                 e.preventDefault();
77                                 return false;
78                         }
79
80                         target = target.parentNode || null;
81                 }
82         });
83 });
84
85 // Overwrite Dialog.show from main js to load the filebrowser into a bs modal.
86 Dialog.show = function (url, title) {
87         if (typeof title === "undefined") {
88                 title = "";
89         }
90
91         var modal = $("#modal").modal();
92         modal.find("#modal-header h4").html(title);
93         modal.find("#modal-body").load(url, function (responseText, textStatus) {
94                 if (textStatus === "success" || textStatus === "notmodified") {
95                         modal.show();
96
97                         $(function () {
98                                 Dialog._load(url);
99                         });
100                 }
101         });
102 };
103
104 // Overwrite the function _get_url from main.js.
105 Dialog._get_url = function (type, name, id) {
106         var hash = name;
107         if (id !== undefined) hash = hash + "-" + id;
108         return 'media/' + type + '/browser?mode=none&theme=frio#' + hash;
109 };
110
111 // Does load the filebrowser into the jot modal.
112 Dialog.showJot = function () {
113         var type = "photo";
114         var name = "main";
115
116         var url = Dialog._get_url(type, name);
117         if ($(".modal-body #jot-fbrowser-wrapper .fbrowser").length < 1) {
118                 // Load new content to fbrowser window.
119                 $("#jot-fbrowser-wrapper").load(url, function (responseText, textStatus) {
120                         if (textStatus === "success" || textStatus === "notmodified") {
121                                 $(function () {
122                                         Dialog._load(url);
123                                 });
124                         }
125                 });
126         }
127 };
128
129 // Init the filebrowser after page load.
130 Dialog._load = function (url) {
131         // Get nickname & filebrowser type from the modal content.
132         let filebrowser = document.getElementById("filebrowser");
133
134         // Try to fetch the hash form the url.
135         let match = url.match(/media\/[a-z]+\/.*(#.*)/);
136         if (!filebrowser || match === null) {
137                 return; //not fbrowser
138         }
139
140         // Initialize the filebrowser.
141         loadScript("view/js/ajaxupload.js");
142         loadScript("view/theme/frio/js/module/media/browser.js", function () {
143                 Browser.init(filebrowser.dataset.nickname, filebrowser.dataset.type, match[1]);
144         });
145 };
146
147 /**
148  * Add first element with the class "heading" as modal title
149  *
150  * Note: this should be really done in the template
151  * and is the solution where we havent done it until this
152  * moment or where it isn't possible because of design
153  */
154 function loadModalTitle() {
155         // Clear the text of the title.
156         $("#modal-title").empty();
157
158         // Hide the first element with the class "heading" of the modal body.
159         $("#modal-body .heading").first().hide();
160
161         var title = "";
162
163         // Get the text of the first element with "heading" class.
164         title = $("#modal-body .heading").first().html();
165
166         // for event modals we need some special handling
167         if ($("#modal-body .event-wrapper .event-summary").length) {
168                 title = '<i class="fa fa-calendar" aria-hidden="true"></i>&nbsp;';
169                 var eventsum = $("#modal-body .event-wrapper .event-summary").html();
170                 title = title + eventsum;
171         }
172
173         // And append it to modal title.
174         if (title !== "") {
175                 $("#modal-title").append(title);
176         }
177 }
178
179 /**
180  * This function loads html content from a friendica page into a modal.
181  *
182  * @param {string} url The url with html content.
183  * @param {string} id The ID of a html element (can be undefined).
184  * @returns {void}
185  */
186 function addToModal(url, id) {
187         var char = qOrAmp(url);
188
189         url = url + char + "mode=none";
190         var modal = $("#modal").modal();
191
192         // Only search for an element if we have an ID.
193         if (typeof id !== "undefined") {
194                 url = url + " div#" + id;
195         }
196
197         modal.find("#modal-body").load(url, function (responseText, textStatus) {
198                 if (textStatus === "success" || textStatus === "notmodified") {
199                         modal.show();
200
201                         //Get first element with the class "heading"
202                         //and use it as title.
203                         loadModalTitle();
204
205                         // We need to initialize autosize again for new
206                         // modal content.
207                         autosize($(".modal .text-autosize"));
208                 }
209         });
210 }
211
212 // Add an element (by its id) to a bootstrap modal.
213 function addElmToModal(id) {
214         var elm = $(id).html();
215         var modal = $("#modal").modal();
216
217         modal.find("#modal-body").append(elm).modal.show;
218
219         loadModalTitle();
220 }
221
222 // Function to load the html from the edit post page into
223 // the jot modal.
224 function editpost(url) {
225         // Next to normel posts the post can be an event post. The event posts don't
226         // use the normal Jot modal. For event posts we will use a normal modal
227         // But first we have to test if the url links to an event. So we will split up
228         // the url in its parts.
229         var splitURL = parseUrl(url);
230         // Test if in the url path containing "calendar/event/show". If the path containing this
231         // expression then we will call the addToModal function and exit this function at
232         // this point.
233         if (splitURL.path.indexOf("calendar/event/show") > -1) {
234                 addToModal(splitURL.path);
235                 return;
236         }
237
238         var modal = $("#jot-modal").modal();
239         url = url + " #jot-sections";
240
241         $(".jot-nav .jot-perms-lnk").parent("li").addClass("hidden");
242
243         // For editpost we load the modal html of "jot-sections" of the edit page. So we would have two jot forms in
244         // the page html. To avoid js conflicts we store the original jot in the variable jotcache.
245         // After closing the modal original jot should be restored at its original position in the html structure.
246         jotcache = $("#jot-content > #jot-sections");
247
248         // Remove the original Jot as long as the edit Jot is open.
249         jotcache.detach();
250
251         // Add the class "edit" to the modal to have some kind of identifier to
252         // have the possibility to e.g. put special event-listener.
253         $("#jot-modal").addClass("edit-jot");
254
255         jotreset();
256
257         modal.find("#jot-modal-content").load(url, function (responseText, textStatus) {
258                 if (textStatus === "success" || textStatus === "notmodified") {
259                         // get the item type and hide the input for title and category if it isn't needed.
260                         var type = $(responseText).find("#profile-jot-form input[name='type']").val();
261                         if (type === "wall-comment" || type === "remote-comment") {
262                                 // Hide title and category input fields because we don't.
263                                 $("#profile-jot-form #jot-title-wrap").hide();
264                                 $("#profile-jot-form #jot-category-wrap").hide();
265                         }
266
267                         // To make dropzone fileupload work on editing a comment, we need to
268                         // attach a new dropzone to modal
269                         dzFactory.setupDropzone('#jot-text-wrap', 'profile-jot-text');
270
271                         modal.show();
272                         $("#jot-popup").show();
273                         linkPreview = $("#profile-jot-text").linkPreview();
274                 }
275         });
276 }
277
278 // Remove content from the jot modal.
279 function jotreset() {
280         // Clear bs modal on close.
281         // We need this to prevent that the modal displays old content.
282         $("body").on("hidden.bs.modal", "#jot-modal.edit-jot", function () {
283                 $(this).removeData("bs.modal");
284                 $(".jot-nav .jot-perms-lnk").parent("li").removeClass("hidden");
285                 $("#profile-jot-form #jot-title-wrap").show();
286                 $("#profile-jot-form #jot-category-wrap").show();
287
288                 // Remove the "edit-jot" class so we can the standard behavior on close.
289                 $("#jot-modal.edit-jot").removeClass("edit-jot");
290                 $("#jot-modal-content").empty();
291         });
292 }
293
294 // Give the active "jot-nav" list element the class "active".
295 function toggleJotNav(elm) {
296         // Get the ID of the tab panel which should be activated.
297         var tabpanel = elm.getAttribute("aria-controls");
298         var cls = hasClass(elm, "jot-nav-lnk-mobile");
299
300         // Select all li of jot-nav and remove the active class.
301         $(elm).parent("li").siblings("li").removeClass("active");
302         // Add the active class to the parent of the link which was selected.
303         $(elm).parent("li").addClass("active");
304
305         // Minimize all tab content wrapper and activate only the selected
306         // tab panel.
307         $("#profile-jot-form > [role=tabpanel]").addClass("minimize").attr("aria-hidden", "true");
308         $("#" + tabpanel)
309                 .removeClass("minimize")
310                 .attr("aria-hidden", "false");
311
312         // Set the aria-selected states
313         $("#jot-modal .modal-header .nav-tabs .jot-nav-lnk").attr("aria-selected", "false");
314         elm.setAttribute("aria-selected", "true");
315
316         // For some tab panels we need to execute other js functions.
317         if (tabpanel === "jot-preview-content") {
318                 preview_post();
319                 // Make Share button visible in preview
320                 $("#jot-preview-share").removeClass("minimize").attr("aria-hidden", "false");
321         } else if (tabpanel === "jot-fbrowser-wrapper") {
322                 $(function () {
323                         Dialog.showJot();
324                 });
325         }
326
327         // If element is a mobile dropdown nav menu we need to change the button text.
328         if (cls) {
329                 toggleDropdownText(elm);
330         }
331 }
332
333 // Wall Message needs a special handling because in some cases
334 // it redirects you to your own server. In such cases we can't
335 // load it into a modal.
336 function openWallMessage(url) {
337         // Split the url in its parts.
338         var parts = parseUrl(url);
339
340         // If the host isn't the same we can't load it in a modal.
341         // So we will go to to the url directly.
342         if ("host" in parts && parts.host !== window.location.host) {
343                 window.location.href = url;
344         } else {
345                 // Otherwise load the wall message into a modal.
346                 addToModal(url);
347         }
348 }
349
350 // This function load the content of the edit url into a modal.
351 /// @todo Rename this function because it can be used for more than events.
352 function eventEdit(url) {
353         var char = qOrAmp(url);
354         url = url + char + "mode=none";
355
356         $.get(url, function (data) {
357                 $("#modal-body").empty();
358                 $("#modal-body").append(data);
359         }).done(function () {
360                 loadModalTitle();
361         });
362 }
363 // @license-end