1 // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPLv3-or-later
3 * Contains functions for bootstrap modal handling.
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
14 $(".fbrowser").remove();
15 // Remove the AjaxUpload element.
16 $(".ajaxbutton-wrapper").remove();
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.
26 // Destroy the attachment linkPreview for Jot.
27 if (typeof linkPreview === "object") {
28 linkPreview.destroy();
33 $("body").on("click", "#nav-login", function (e) {
35 Dialog.show(this.href, this.dataset.originalTitle || this.title);
39 $("body").on("click", "#jot-modal .jot-nav li .jot-nav-lnk", function (e) {
44 // Bookmarklet page needs an jot modal which appears automatically.
45 if (window.location.pathname.indexOf("/bookmarklet") >= 0 && $("#jot-modal").length) {
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");
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.
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;
74 if (target.matches && target.matches("a.add-to-modal")) {
75 addToModal(target.href);
80 target = target.parentNode || null;
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") {
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") {
104 // Overwrite the function _get_url from main.js.
105 Dialog._get_url = function (type, name, id) {
107 if (id !== undefined) hash = hash + "-" + id;
108 return 'media/' + type + '/browser?mode=none&theme=frio#' + hash;
111 // Does load the filebrowser into the jot modal.
112 Dialog.showJot = function () {
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") {
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");
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
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]);
148 * Add first element with the class "heading" as modal title
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
154 function loadModalTitle() {
155 // Clear the text of the title.
156 $("#modal-title").empty();
158 // Hide the first element with the class "heading" of the modal body.
159 $("#modal-body .heading").first().hide();
163 // Get the text of the first element with "heading" class.
164 title = $("#modal-body .heading").first().html();
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> ';
169 var eventsum = $("#modal-body .event-wrapper .event-summary").html();
170 title = title + eventsum;
173 // And append it to modal title.
175 $("#modal-title").append(title);
180 * This function loads html content from a friendica page into a modal.
182 * @param {string} url The url with html content.
183 * @param {string} id The ID of a html element (can be undefined).
186 function addToModal(url, id) {
187 var char = qOrAmp(url);
189 url = url + char + "mode=none";
190 var modal = $("#modal").modal();
192 // Only search for an element if we have an ID.
193 if (typeof id !== "undefined") {
194 url = url + " div#" + id;
197 modal.find("#modal-body").load(url, function (responseText, textStatus) {
198 if (textStatus === "success" || textStatus === "notmodified") {
201 //Get first element with the class "heading"
202 //and use it as title.
205 // We need to initialize autosize again for new
207 autosize($(".modal .text-autosize"));
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();
217 modal.find("#modal-body").append(elm).modal.show;
222 // Function to load the html from the edit post page into
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
233 if (splitURL.path.indexOf("calendar/event/show") > -1) {
234 addToModal(splitURL.path);
238 var modal = $("#jot-modal").modal();
239 url = url + " #jot-sections";
241 $(".jot-nav .jot-perms-lnk").parent("li").addClass("hidden");
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");
248 // Remove the original Jot as long as the edit Jot is open.
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");
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();
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');
272 $("#jot-popup").show();
273 linkPreview = $("#profile-jot-text").linkPreview();
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();
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();
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");
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");
305 // Minimize all tab content wrapper and activate only the selected
307 $("#profile-jot-form > [role=tabpanel]").addClass("minimize").attr("aria-hidden", "true");
309 .removeClass("minimize")
310 .attr("aria-hidden", "false");
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");
316 // For some tab panels we need to execute other js functions.
317 if (tabpanel === "jot-preview-content") {
319 // Make Share button visible in preview
320 $("#jot-preview-share").removeClass("minimize").attr("aria-hidden", "false");
321 } else if (tabpanel === "jot-fbrowser-wrapper") {
327 // If element is a mobile dropdown nav menu we need to change the button text.
329 toggleDropdownText(elm);
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);
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;
345 // Otherwise load the wall message into a modal.
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";
356 $.get(url, function (data) {
357 $("#modal-body").empty();
358 $("#modal-body").append(data);
359 }).done(function () {