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