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