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