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