]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/js/modal.js
Use visibility tags input for the default ACL selector
[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         var nickname = $("#fb-nickname").attr("value");
163         var type = $("#fb-type").attr("value");
164
165         // Try to fetch the hash form the url.
166         var match = url.match(/fbrowser\/[a-z]+\/.*(#.*)/);
167         if (match===null) return; //not fbrowser
168         var hash = match[1];
169
170         // Initialize the filebrowser.
171         var jsbrowser = function() {
172                 FileBrowser.init(nickname, type, hash);
173         };
174         loadScript("view/js/ajaxupload.js");
175         loadScript("view/theme/frio/js/filebrowser.js", jsbrowser);
176 };
177
178 /**
179  * @brief Add first element with the class "heading" as modal title
180  * 
181  * Note: this should be really done in the template
182  * and is the solution where we havent done it until this
183  * moment or where it isn't possible because of design
184  */
185 function loadModalTitle() {
186         // Clear the text of the title.
187         $("#modal-title").empty();
188
189         // Hide the first element with the class "heading" of the modal body.
190         $("#modal-body .heading").first().hide();
191
192         var title = "";
193
194         // Get the text of the first element with "heading" class.
195         title = $("#modal-body .heading").first().text();
196
197         // for event modals we need some speacial handling
198         if($("#modal-body .event-wrapper .event-summary").length) {
199                 title = '<i class="fa fa-calendar" aria-hidden="true"></i>&nbsp;';
200                 var eventsum = $("#modal-body .event-wrapper .event-summary").text();
201                 title = title + eventsum;
202         }
203
204         // And append it to modal title.
205         if (title!=="") {
206                 $("#modal-title").append(title);
207         }
208 }
209
210
211 /**
212  * This function loads html content from a friendica page into a modal.
213  * 
214  * @param {string} url The url with html content.
215  * @param {string} id The ID of a html element (can be undefined).
216  * @returns {void}
217  */
218 function addToModal(url, id) {
219         var char = qOrAmp(url);
220
221         url = url + char + 'mode=none';
222         var modal = $('#modal').modal();
223
224         // Only search for an element if we have an ID.
225         if (typeof id !== "undefined") {
226                 url = url + " div#" + id;
227         }
228
229         modal
230                 .find('#modal-body')
231                 .load(url, function (responseText, textStatus) {
232                         if ( textStatus === 'success' || 
233                                 textStatus === 'notmodified') 
234                         {
235                                 modal.show();
236
237                                 //Get first element with the class "heading"
238                                 //and use it as title.
239                                 loadModalTitle();
240
241                                 // We need to initialize autosize again for new
242                                 // modal conent.
243                                 autosize($('.modal .text-autosize'));
244                         }
245                 });
246 }
247
248 // Add an element (by its id) to a bootstrap modal.
249 function addElmToModal(id) {
250         var elm = $(id).html();
251         var modal = $('#modal').modal();
252
253         modal
254                 .find('#modal-body')
255                 .append(elm)
256                 .modal.show;
257
258         loadModalTitle();
259 }
260
261 // Function to load the html from the edit post page into
262 // the jot modal.
263 function editpost(url) {
264         // Next to normel posts the post can be an event post. The event posts don't
265         // use the normal Jot modal. For event posts we will use a normal modal
266         // But first we have to test if the url links to an event. So we will split up
267         // the url in its parts.
268         var splitURL = parseUrl(url);
269         // Test if in the url path containing "events/event". If the path containing this
270         // expression then we will call the addToModal function and exit this function at
271         // this point.
272         if (splitURL.path.indexOf('events/event') > -1) {
273                 addToModal(splitURL.path);
274                 return;
275         }
276
277         var modal = $('#jot-modal').modal();
278         url = url + " #jot-sections";
279
280         $(".jot-nav .jot-perms-lnk").parent("li").addClass("hidden");
281
282         // For editpost we load the modal html of "jot-sections" of the edit page. So we would have two jot forms in
283         // the page html. To avoid js conflicts we store the original jot in the variable jotcache.
284         // After closing the modal original jot should be restored at its orginal position in the html structure.
285         jotcache = $("#jot-content > #jot-sections");
286
287         // Remove the original Jot as long as the edit Jot is open.
288         jotcache.remove();
289
290         // Add the class "edit" to the modal to have some kind of identifier to
291         // have the possibility to e.g. put special event-listener.
292         $("#jot-modal").addClass("edit-jot");
293
294         jotreset();
295
296         modal
297                 .find('#jot-modal-content')
298                 .load(url, function (responseText, textStatus) {
299                         if ( textStatus === 'success' || 
300                                 textStatus === 'notmodified') 
301                         {
302                                 // get the item type and hide the input for title and category if it isn't needed.
303                                 var type = $(responseText).find("#profile-jot-form input[name='type']").val();
304                                 if(type === "wall-comment" || type === "remote-comment")
305                                 {
306                                         // Hide title and category input fields because we don't.
307                                         $("#profile-jot-form #jot-title-wrap").hide();
308                                         $("#profile-jot-form #jot-category-wrap").hide();
309                                 }
310
311                                 modal.show();
312                                 $("#jot-popup").show();
313                                 linkPreview = $('#profile-jot-text').linkPreview();
314                         }
315                 });
316 }
317
318 // Remove content from the jot modal.
319 function jotreset() {
320         // Clear bs modal on close.
321         // We need this to prevent that the modal displays old content.
322         $('body').on('hidden.bs.modal', '#jot-modal.edit-jot', function () {
323                 $(this).removeData('bs.modal');
324                 $(".jot-nav .jot-perms-lnk").parent("li").removeClass("hidden");
325                 $("#profile-jot-form #jot-title-wrap").show();
326                 $("#profile-jot-form #jot-category-wrap").show();
327
328                 // Remove the "edit-jot" class so we can the standard behavior on close.
329                 $("#jot-modal.edit-jot").removeClass("edit-jot");
330                 $("#jot-modal-content").empty();
331         });
332 }
333
334 // Give the active "jot-nav" list element the class "active".
335 function toggleJotNav (elm) {
336         // Get the ID of the tab panel which should be activated.
337         var tabpanel = elm.getAttribute("aria-controls");
338         var cls = hasClass(elm, "jot-nav-lnk-mobile");
339
340         // Select all li of jot-nav and remove the active class.
341         $(elm).parent("li").siblings("li").removeClass("active");
342         // Add the active class to the parent of the link which was selected.
343         $(elm).parent("li").addClass("active");
344
345         // Minimize all tab content wrapper and activate only the selected
346         // tab panel.
347         $('#profile-jot-form > [role=tabpanel]').addClass("minimize").attr("aria-hidden" ,"true");
348         $('#' + tabpanel).removeClass("minimize").attr("aria-hidden" ,"false");
349
350         // Set the aria-selected states
351         $("#jot-modal .modal-header .nav-tabs .jot-nav-lnk").attr("aria-selected", "false");
352         elm.setAttribute("aria-selected", "true");
353
354         // For some some tab panels we need to execute other js functions.
355         if (tabpanel === "jot-preview-content") {
356                 preview_post();
357                 // Make Share button visivle in preview
358                 $('#jot-preview-share').removeClass("minimize").attr("aria-hidden" ,"false");
359         } else if (tabpanel === "jot-fbrowser-wrapper") {
360                 $(function() {
361                         Dialog.showJot();
362                 });
363         }
364
365         // If element is a mobile dropdown nav menu we need to change the botton text.
366         if (cls) {
367                 toggleDropdownText(elm);
368         }
369 }
370
371 // Wall Message needs a special handling because in some cases
372 // it redirects you to your own server. In such cases we can't
373 // load it into a modal.
374 function openWallMessage(url) {
375         // Split the the url in its parts.
376         var parts = parseUrl(url);
377
378         // If the host isn't the same we can't load it in a modal.
379         // So we will go to to the url directly.
380         if( ("host" in parts) && (parts.host !== window.location.host)) {
381                 window.location.href = url;
382         } else {
383                 // Otherwise load the wall message into a modal.
384                 addToModal(url);
385         }
386 }
387
388 // This function load the content of the edit url into a modal.
389 /// @todo Rename this function because it can be used for more than events.
390 function eventEdit(url) {
391         var char = qOrAmp(url);
392         url = url + char + 'mode=none';
393
394         $.get(url, function(data) {
395                 $("#modal-body").empty();
396                 $("#modal-body").append(data);
397         }).done(function() {
398                 loadModalTitle();
399         });
400 }