]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/js/modal.js
b29b8d4caa6638a383511a5c639bc7fb4ee75e1e
[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         var modal = $('#modal').modal();
95         modal.find("#modal-header h4").html(title);
96         modal
97                 .find('#modal-body')
98                 .load(url, function (responseText, textStatus) {
99                         if ( textStatus === 'success' || 
100                                 textStatus === 'notmodified') 
101                         {
102                                 modal.show();
103
104                                 $(function() {Dialog._load(url);});
105                         }
106                 });
107 };
108
109 // overwrite the function _get_url from main.js
110 Dialog._get_url = function(type, name, id) {
111         var hash = name;
112         if (id !== undefined) hash = hash + "-" + id;
113         return "fbrowser/"+type+"/?mode=none#"+hash;
114 };
115
116 // does load the filebrowser into the jot modal
117 Dialog.showJot = function() {
118         var type = "image";
119         var name = "main";
120
121         var url = Dialog._get_url(type, name);
122         if(($(".modal-body #jot-fbrowser-wrapper .fbrowser").length) < 1 ) {
123                 // load new content to fbrowser window
124                 $("#jot-fbrowser-wrapper").load(url,function(responseText, textStatus){
125                         if ( textStatus === 'success' || 
126                                 textStatus === 'notmodified') 
127                         {
128                                 $(function() {Dialog._load(url);});
129                         }
130                 });
131         }
132 };
133
134 // init the filebrowser after page load
135 Dialog._load = function(url) {
136         // get nickname & filebrowser type from the modal content
137         var nickname = $("#fb-nickname").attr("value");
138         var type = $("#fb-type").attr("value");
139
140         // try to fetch the hash form the url
141         var match = url.match(/fbrowser\/[a-z]+\/\?mode=none(.*)/);
142         if (match===null) return; //not fbrowser
143         var hash = match[1];
144
145         // initialize the filebrowser
146         var jsbrowser = function() {
147                 FileBrowser.init(nickname, type, hash);
148         };
149         loadScript("view/theme/frio/js/filebrowser.js", jsbrowser);
150 };
151
152 /**
153  * @brief Add first element with the class "heading" as modal title
154  * 
155  * Note: this should be really done in the template
156  * and is the solution where we havent done it until this
157  * moment or where it isn't possible because of design
158  */
159 function loadModalTitle() {
160         // clear the text of the title
161         $("#modal-title").empty();
162
163         // hide the first element with the class "heading" of the modal body
164         $("#modal-body .heading").first().hide();
165
166         var title = "";
167
168         // get the text of the first element with "heading" class
169         title = $("#modal-body .heading").first().text();
170
171         // for event modals we need some speacial handling
172         if($("#modal-body .event-wrapper .event-summary").length) {
173                 title = '<i class="fa fa-calendar" aria-hidden="true"></i>&nbsp;';
174                 var eventsum = $("#modal-body .event-wrapper .event-summary").text();
175                 title = title + eventsum;
176         }
177
178         // and append it to modal title
179         if (title!=="") {
180                 $("#modal-title").append(title);
181         }
182 }
183
184 // This function loads html content from a friendica page
185 // into a modal
186 function addToModal(url) {
187         var char = qOrAmp(url);
188
189         url = url + char + 'mode=none';
190         var modal = $('#modal').modal();
191
192         modal
193                 .find('#modal-body')
194                 .load(url, function (responseText, textStatus) {
195                         if ( textStatus === 'success' || 
196                                 textStatus === 'notmodified') 
197                         {
198                                 modal.show();
199
200                                 //Get first element with the class "heading"
201                                 //and use it as title
202                                 loadModalTitle();
203                         }
204                 });
205 }
206
207 // Add a element (by it's id) to a bootstrap modal
208 function addElmToModal(id) {
209         var elm = $(id).html();
210         var modal = $('#modal').modal();
211
212         modal
213                 .find('#modal-body')
214                 .append(elm)
215                 .modal.show;
216
217         loadModalTitle();
218 }
219
220 // function to load the html from the edit post page into
221 // the jot modal
222 function editpost(url) {
223         // next to normel posts the post can be an event post. The event posts don't
224         // use the normal Jot modal. For event posts we will use a normal modal
225         // But first we have to test if the url links to an event. So we will split up
226         // the url in its parts
227         var splitURL = parseUrl(url);
228         // Test if in the url path containing "events/event". If the path containing this
229         // expression then we will call the addToModal function and exit this function at
230         // this point
231         if (splitURL.path.indexOf('events/event') > -1) {
232                 addToModal(splitURL.path);
233                 return;
234         }
235
236         var modal = $('#jot-modal').modal();
237         url = url + " #profile-jot-form";
238
239         //var rand_num = random_digits(12);
240         $(".jot-nav .jot-perms-lnk").parent("li").addClass("hidden");
241
242         // For editpost we load the modal html form the edit page. So we would have two jot forms in
243         // the page html. To avoid js conflicts we store the original jot in the variable jotcache.
244         // After closing the modal original jot should be restored at its orginal position in the html structure.
245         jotcache = $("#jot-content > #profile-jot-form");
246
247         // remove the original Jot as long as the edit Jot is open
248         jotcache.remove();
249
250         // add the class "edit" to the modal to have some kind of identifier to
251         // have the possibility to e.g. put special event-listener
252         $("#jot-modal").addClass("edit-jot");
253
254         jotreset();
255
256         modal
257                 .find('#jot-modal-content')
258                 .load(url, function (responseText, textStatus) {
259                         if ( textStatus === 'success' || 
260                                 textStatus === 'notmodified') 
261                         {
262                                 // get the item type and hide the input for title and category if it isn't needed
263                                 var type = $(responseText).find("#profile-jot-form input[name='type']").val();
264                                 if(type === "wall-comment" || type === "remote-comment")
265                                 {
266                                         // hide title and category input fields because we don't
267                                         $("#profile-jot-form #jot-title-wrap").hide();
268                                         $("#profile-jot-form #jot-category-wrap").hide();
269                                 }
270
271                                 modal.show();
272                                 $("#jot-popup").show();
273                         }
274                 });
275 }
276
277 // remove content from the jot modal
278 function jotreset() {
279         // Clear bs modal on close
280         // We need this to prevent that the modal displays old content
281         $('body').on('hidden.bs.modal', '#jot-modal.edit-jot', function () {
282                 $(this).removeData('bs.modal');
283                 $(".jot-nav .jot-perms-lnk").parent("li").removeClass("hidden");
284                 $("#profile-jot-form #jot-title-wrap").show();
285                 $("#profile-jot-form #jot-category-wrap").show();
286
287                 // the following was commented out because it is needed anymore
288                 // because we changed the behavior at an other place
289         //              var rand_num = random_digits(12);
290         //              $('#jot-title, #jot-category, #profile-jot-text').val("");
291         //              $( "#profile-jot-form input[name='type']" ).val("wall");
292         //              $( "#profile-jot-form input[name='post_id']" ).val("");
293         //              $( "#profile-jot-form input[name='post_id_random']" ).val(rand_num);
294
295                 // remove the "edit-jot" class so we can the standard behavior on close
296                 $("#jot-modal.edit-jot").removeClass("edit-jot");
297                 $("#jot-modal-content").empty();
298         });
299 }
300
301 // Give the active "jot-nav" list element the class "active"
302 function toggleJotNav (elm) {
303         // select all li of jot-nav and remove the active class
304         $(".jot-nav li").removeClass("active");
305         // add the active class to the parent of the link which was selected
306         $(elm).parent("li").addClass("active");
307 }
308
309 // Wall Message needs a special handling because in some cases
310 // it redirects you to your own server. In such cases we can't
311 // load it into a modal
312 function openWallMessage(url) {
313         // split the the url in its parts
314         var parts = parseUrl(url);
315
316         // If the host isn't the same we can't load it in a modal.
317         // So we will go to to the url directly
318         if( ("host" in parts) && (parts.host !== window.location.host)) {
319                 window.location.href = url;
320         } else {
321                 // otherwise load the wall message into a modal
322                 addToModal(url);
323         }
324 }