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