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