]> git.mxchange.org Git - friendica.git/blob - js/theme.js
open friendica photos from the stream with colorbox
[friendica.git] / js / theme.js
1 $(document).ready(function(){
2         //fade in/out based on scrollTop value
3         $(window).scroll(function () {
4                 if ($(this).scrollTop() > 1000) {
5                         $("#back-to-top").fadeIn();
6                 } else {
7                         $("#back-to-top").fadeOut();
8                 }
9         });
10  
11         // scroll body to 0px on click
12         $("#back-to-top").click(function () {
13                 $("body,html").animate({
14                         scrollTop: 0
15                 }, 400);
16                 return false;
17         });
18
19         // Clear bs modal on close
20         // We need this to prevent that the modal displays old content
21         $('body').on('hidden.bs.modal', '.modal', function () {
22                 $(this).removeData('bs.modal');
23                 $("#modal-title").empty();
24                 $('#modal-body').empty();
25         });
26
27         // add the class "selected" to group widges li if li > a does have the class group-selected
28         if( $("#sidebar-group-ul li a").hasClass("group-selected")) {
29                 $("#sidebar-group-ul li a.group-selected").parent("li").addClass("selected");
30         }
31
32         // add the class "selected" to forums widges li if li > a does have the class forum-selected
33         if( $("#forumlist-sidbar-ul li a").hasClass("forum-selected")) {
34                 $("#forumlist-sidbar-ul li a.forum-selected").parent("li").addClass("selected");
35         }
36
37         // add the class "active" to tabmenuli if li > a does have the class active
38         if( $("#tabmenu ul li a").hasClass("active")) {
39                 $("#tabmenu ul li a.active").parent("li").addClass("active");
40         }
41
42         // give select fields an boostrap classes
43         $(".field.select, .field.custom").addClass("form-group");
44         $(".field.select > select, .field.custom > select").addClass("form-control");
45
46         if( $("ul.tabs")) {
47                 $("ul.tabs").appendTo("#topbar-second > .container > #tabmenu");
48         }
49
50         // add Jot botton to the scecond navbar
51         if( $("section #jotOpen")) {
52                 $("section #jotOpen").appendTo("#topbar-second > .container > #navbar-button");
53                 if( $("#jot-popup").is(":hidden")) $("#topbar-second > .container > #navbar-button #jotOpen").hide();
54         }
55
56         // Loading remote bootstrap remote modals
57         // This is uses to load tradional friendica pages into bootstrap modals
58         // 
59         $('a[rel=modal]').on('click', function(evt) {
60                 evt.preventDefault();
61                 var modal = $('#modal').modal();
62                 modal
63                         .find('#modal-body')
64                         .load($(this).attr("href"), function (responseText, textStatus) {
65                                 if ( textStatus === 'success' || 
66                                         textStatus === 'notmodified') 
67                                 {
68                                         modal.show();
69
70                                         //Get first h3 element and use it as title
71                                         loadModalTitle();
72                                 }
73                         });
74         });
75
76         // Add Colorbox for viewing Network page images
77         //var cBoxClasses = new Array();
78         $(".wall-item-body a img").each(function(){
79                 var aElem = $(this).parent();
80                 var imgHref = aElem.attr("href");
81
82                 // We need to make sure we only put a Colorbox on links to Friendica images
83                 // We'll try to do this by looking for links of the form
84                 // .../photo/ab803d8eg08daf85023adfec08 (with nothing more following), in hopes
85                 // that that will be unique enough
86                 if(imgHref.match(/\/photo\/[a-fA-F0-9]+(-[0-9]\.[\w]+?)?$/)) {
87
88                         // Add a unique class to all the images of a certain post, to allow scrolling through
89                         var cBoxClass = $(this).closest(".wall-item-body").attr("id") + "-lightbox";
90                         $(this).addClass(cBoxClass);
91
92 //                      if( $.inArray(cBoxClass, cBoxClasses) < 0 ) {
93 //                              cBoxClasses.push(cBoxClass);
94 //                      }
95
96                         aElem.colorbox({
97                                 maxHeight: '90%',
98                                 photo: true, // Colorbox doesn't recognize a URL that don't end in .jpg, etc. as a photo
99                                 rel: cBoxClass //$(this).attr("class").match(/wall-item-body-[\d]+-lightbox/)[0]
100                         });
101                 }
102         });
103
104         // overwrite Dialog.show from main js to load the filebrowser into a bs modal
105         Dialog.show = function(url) {
106                 var modal = $('#modal').modal();
107                 modal
108                         .find('#modal-body')
109                         .load(url, function (responseText, textStatus) {
110                                 if ( textStatus === 'success' || 
111                                         textStatus === 'notmodified') 
112                                 {
113                                         modal.show();
114
115                                         // get nickname & filebrowser type from the modal content
116                                         var nickname = $("#fb-nickname").attr("value");
117                                         var type = $("#fb-type").attr("value");
118
119                                         // try to fetch the hash form the url
120                                         var match = url.match(/fbrowser\/[a-z]+\/\?mode=modal(.*)/);
121                                         var hash = match[1];
122
123                                         // initialize the filebrowser
124                                         var jsbrowser = function() {
125                                                 FileBrowser.init(nickname, type, hash);
126                                         }
127                                         loadScript("view/theme/frio/js/filebrowser.js", jsbrowser);
128                                 }
129                         });
130         };
131
132         // overwrite the function _get_url from main.js
133         Dialog._get_url = function(type, name, id) {
134                 var hash = name;
135                 if (id !== undefined) hash = hash + "-" + id;
136                 return "fbrowser/"+type+"/?mode=modal#"+hash;
137         };
138
139
140
141
142 });
143 //function commentOpenUI(obj, id) {
144 //      $(document).unbind( "click.commentOpen", handler );
145 //
146 //      var handler = function() {
147 //              if(obj.value == '{{$comment}}') {
148 //                      obj.value = '';
149 //                      $("#comment-edit-text-" + id).addClass("comment-edit-text-full").removeClass("comment-edit-text-empty");
150 //                      // Choose an arbitrary tab index that's greater than what we're using in jot (3 of them)
151 //                      // The submit button gets tabindex + 1
152 //                      $("#comment-edit-text-" + id).attr('tabindex','9');
153 //                      $("#comment-edit-submit-" + id).attr('tabindex','10');
154 //                      $("#comment-edit-submit-wrapper-" + id).show();
155 //              }
156 //      };
157 //
158 //      $(document).bind( "click.commentOpen", handler );
159 //}
160 //
161 //function commentCloseUI(obj, id) {
162 //      $(document).unbind( "click.commentClose", handler );
163 //
164 //      var handler = function() {
165 //              if(obj.value === '') {
166 //              obj.value = '{{$comment}}';
167 //                      $("#comment-edit-text-" + id).removeClass("comment-edit-text-full").addClass("comment-edit-text-empty");
168 //                      $("#comment-edit-text-" + id).removeAttr('tabindex');
169 //                      $("#comment-edit-submit-" + id).removeAttr('tabindex');
170 //                      $("#comment-edit-submit-wrapper-" + id).hide();
171 //              }
172 //      };
173 //
174 //      $(document).bind( "click.commentClose", handler );
175 //}
176
177 function openClose(theID) {
178         var elem = document.getElementById(theID);
179
180         if( $(elem).is(':visible')) {
181                 $(elem).slideUp(200);
182         }
183         else {
184                 $(elem).slideDown(200);
185         }
186 }
187
188 function showHide(theID) {
189         if(document.getElementById(theID).style.display == "block") {
190                 document.getElementById(theID).style.display = "none"
191         }
192         else {
193                 document.getElementById(theID).style.display = "block"
194         }
195 }
196
197
198 function showHideComments(id) {
199         if( $('#collapsed-comments-' + id).is(':visible')) {
200                 $('#collapsed-comments-' + id).slideUp();
201                 $('#hide-comments-' + id).html(window.showMore);
202                 $('#hide-comments-total-' + id).show();
203         }
204         else {
205                 $('#collapsed-comments-' + id).slideDown();
206                 $('#hide-comments-' + id).html(window.showFewer);
207                 $('#hide-comments-total-' + id).hide();
208         }
209 }
210
211
212 function justifyPhotos() {
213         justifiedGalleryActive = true;
214         $('#photo-album-contents').justifiedGallery({
215                 margins: 3,
216                 border: 0,
217                 sizeRangeSuffixes: {
218                         'lt100': '-2',
219                         'lt240': '-2',
220                         'lt320': '-2',
221                         'lt500': '',
222                         'lt640': '-1',
223                         'lt1024': '-0'
224                 }
225         }).on('jg.complete', function(e){ justifiedGalleryActive = false; });
226 }
227
228 function justifyPhotosAjax() {
229         justifiedGalleryActive = true;
230         $('#photo-album-contents').justifiedGallery('norewind').on('jg.complete', function(e){ justifiedGalleryActive = false; });
231 }
232
233 function loadScript(url, callback) {
234         // Adding the script tag to the head as suggested before
235         var head = document.getElementsByTagName('head')[0];
236         var script = document.createElement('script');
237         script.type = 'text/javascript';
238         script.src = url;
239
240         // Then bind the event to the callback function.
241         // There are several events for cross browser compatibility.
242         script.onreadystatechange = callback;
243         script.onload = callback;
244
245         // Fire the loading
246         head.appendChild(script);
247 }
248
249 /**
250  * @brief Add first h3 element as modal title
251  * 
252  * Note: this should be really done in the template
253  * and is the solution where we havent done it until this
254  * moment or where it isn't possible because of design
255  */
256 function loadModalTitle() {
257         // clear the text of the title
258         //$("#modal-title").empty();
259
260         // hide the first h3 child element of the modal body
261         $("#modal-body > h3").first().hide();
262
263         // get the text of the first h3 child element
264         var title = $("#modal-body > h3").first().text();
265
266         // and append it to modal title
267         if (title!=="") {
268                 $("#modal-title").append(title);
269         }
270 }
271
272 function editpost(url) {
273         var modal = $('#jot-modal').modal();
274         var url = url + " #profile-jot-form";
275         //var rand_num = random_digits(12);
276         $("#jot-perms-lnk").hide();
277
278         // rename the the original div jot-preview-content because the edit function
279         // does load the content for the modal from another source and preview won't work
280         // if this div would exist twice
281         // $("#jot-content #profile-jot-form").attr("id","#profile-jot-form-renamed");
282         // $("#jot-content #jot-preview-content").attr("id","#jot-preview-content-renamed");
283
284         // For editpost we load the modal html form the edit page. So we would have two jot forms in
285         // the page html. To avoid js conflicts we move the original jot to the end of the page
286         // so the editpost jot would be the first jot in html structure.
287         // After closing the modal we move the original jot back to it's orginal position in the html structure.
288         // 
289         // Note: For now it seems to work but this isn't optimal because we have doubled ID names for the jot div's.
290         // We need to have a better solution for this in the future. 
291         $("section #jot-content #profile-jot-form").appendTo("footer #cache-container");
292
293         jotreset();
294
295         modal
296                 .find('#jot-modal-body')
297                 .load(url, function (responseText, textStatus) {
298                         if ( textStatus === 'success' || 
299                                 textStatus === 'notmodified') 
300                         {
301                                 // get the item type and hide the input for title and category if it isn't needed
302                                 var type = $(responseText).find("#profile-jot-form input[name='type']").val();
303                                 if(type === "wall-comment" || type === "remote-comment")
304                                 {
305                                         $("#profile-jot-form #jot-title-wrap").hide();
306                                         $("#profile-jot-form #jot-category-wrap").hide();
307                                 }
308
309                                 modal.show();
310                                 $("#jot-popup").show();
311                         }
312                 });
313 }
314
315 function jotreset() {
316         // Clear bs modal on close
317         // We need this to prevent that the modal displays old content
318         $('body').on('hidden.bs.modal', '#jot-modal', function () {
319                 $(this).removeData('bs.modal');
320                 $("#jot-perms-lnk").show();
321                 $("#profile-jot-form #jot-title-wrap").show();
322                 $("#profile-jot-form #jot-category-wrap").show();
323
324                 // the following was commented out because it is needed anymore
325                 // because we changed the behavior at an other place
326 //              var rand_num = random_digits(12);
327 //              $('#jot-title, #jot-category, #profile-jot-text').val("");
328 //              $( "#profile-jot-form input[name='type']" ).val("wall");
329 //              $( "#profile-jot-form input[name='post_id']" ).val("");
330 //              $( "#profile-jot-form input[name='post_id_random']" ).val(rand_num);
331                 $("#jot-modal-body").empty();
332
333                 // rename the div #jot-preview-content-renamed back to it's original
334                 // name. Have a look at function editpost() for further explanation
335                 //$("#jot-content #profile-jot-form-renamed").attr("id","#profile-jot-form");
336                 //$("#jot-content #jot-preview-content-renamed").attr("id","#jot-preview-content");
337
338                 // Move the original jot back to it's old place in the html structure
339                 // For explaination have a look at function editpost()
340                 $("footer #cache-container #profile-jot-form").appendTo("section #jot-content");
341         });
342 }
343
344 function random_digits(digits) {
345         var rn = "";
346         var rnd = "";
347
348         for(var i = 0; i < digits; i++) {
349                 var rn = Math.round(Math.random() * (9));
350                 rnd += rn;
351         }
352
353         return rnd;
354 }