Touren application
[infodrom.org/touren.infodrom.org] / touren.js
1 // Source http://stackoverflow.com/questions/210717/using-jquery-to-center-a-div-on-the-screen
2 //
3 (function($){
4     $.fn.center = function () {
5         this.css("position","absolute");
6         this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) +
7                                  $(window).scrollTop()) + "px");
8         this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) +
9                                   $(window).scrollLeft()) + "px");
10         return this;
11     };
12 })($);
13
14 // type = ['error', 'warning', 'info']
15 function show_message(text, type)
16 {
17     if (!type) type = 'info';
18
19     if (type == 'error') {
20         var classname = 'alert-danger';
21     } else if (type == 'warning') {
22         var classname = 'alert-warning';
23     } else {
24         var classname = 'alert-success';
25     }
26
27     $('div#message').attr('class', 'alert ' + classname).text(text).center().show();
28     setTimeout(function(){$('div#message').hide();}, 3000);
29
30     return false;
31 }
32
33 function ajax_request(route, parms, callback)
34 {
35     $.post(tour_base_url + route,
36            parms,
37            function(data){
38                if (!data.status) {
39                    if (data.error) {
40                        if (data.redirect_login) {
41                            show_message(data.error, 'error');
42                            setTimeout(function(){window.location.href = '/';}, 5000);
43                            return;
44                        } else
45                            return show_message(data.error, 'error');
46                    } else
47                        return show_message('Fehler im Backend zu "'+route+'" aufgetreten', 'error');
48                }
49                if (data.warning)
50                    show_message(data.warning, 'warning');
51                if (data.info)
52                    show_message(data.info, 'info');
53                if (typeof(callback) == 'function')
54                    callback(data);
55            });
56 }
57
58 function load_dates()
59 {
60     ajax_request('tour/'+tour_key+'/dates', '', function(data){
61         $('#datelist').html(data.table);
62     });
63 }
64
65 function load_pov()
66 {
67     ajax_request('tour/'+tour_key+'/pov', '', function(data){
68         $('#povlist').html(data.table);
69
70         $('#povlist table button.btn-warning').click(function(e){
71             ajax_request('tour/'+tour_key+'/povmove',
72                          {'pov': $(this).parents('tr:first').attr('data-id'),
73                           'direction': $(this).attr('data-dir')},
74                          function(data){
75                              load_pov();
76                          });
77         });
78         $('#povlist table button.btn-warning:first').attr('disabled', 'disabled');
79         $('#povlist table button.btn-warning:last').attr('disabled', 'disabled');
80
81         $('#povlist table button.btn-danger').click (function(e){
82             ajax_request('tour/'+tour_key+'/povdel',
83                          {'pov': $(this).parents('tr:first').attr('data-id')},
84                          function(data){
85                              load_pov();
86                          });
87         });
88     });
89 }
90
91 $(function(){
92     $('input').on('keydown', function(e){
93         if (e.which === 13)
94             e.preventDefault();
95     });
96
97     // Login form
98     $('.lostpw a').click(function(e){
99         if (!$('.loginform form #login').val().length)
100             return show_message('Bitte E-Mail angeben', 'error');
101
102         ajax_request('account/lostpw', 'email='+$('.loginform form #login').val(), function(data){
103             return show_message('Eine Mail an wurde verschickt.');
104         });
105
106         return false;
107     });
108
109     // Passwort setzen
110     $('.lostpwform .btn').click(function(e){
111         if (!$('.lostpwform #password').val().length)
112             return show_message('Bitte ein neues Passwort setzen', 'error');
113
114         if ($('.lostpwform #password').val() != $('.lostpwform #password2').val())
115             return show_message('Die Passwörter stimmen nicht überein', 'error');
116
117         if ($('.lostpwform #password').val().length < 8)
118             return show_message('Das Passwort muss mindestens 8 Zeichen lang sein', 'error');
119
120         ajax_request('account/setpw', $('.lostpwform form').serialize(), function(data){
121             if (data.goto) {
122                 show_message('Gespeichert. Bitte neu anmelden.');
123                 setTimeout(function(){window.location.href = data.goto;}, 2000);
124             }
125         });
126
127         return false;
128     });
129
130     // Settings dialog
131     $('#settings_save').click(function(e){
132         ajax_request('index/settings', $('form[name="settings"]').serialize(), function(data){
133             show_message('Einstellungen gespeichert', 'info');
134             setTimeout(function(){window.location.href = tour_base_url + 'index/index';}, 4000);
135         });
136     });
137
138     // Create new tour
139     $('#newtour_save').click(function(e){
140         if (!$('form[name="newtour"] #name').val().length ||
141             !$('form[name="newtour"] #urlkey').val().length ||
142             !$('form[name="newtour"] #year').val().length ||
143             !$('form[name="newtour"] #duration').val().length ||
144             !$('form[name="newtour"] #leader').val().length)
145             return show_message('Alle Felder müssen ausgefüllt sein', 'error');
146
147         if ($('form[name="newtour"] #urlkey').val().indexOf(' ') != -1)
148             return show_message('Leerzeichen nicht erlaubt im URL-Key', 'error');
149
150         ajax_request('tour/newtour', $('form[name="newtour"]').serialize(), function(data){
151             show_message('Tour gespeichert', 'info');
152             setTimeout(function(){window.location.href = tour_base_url + 'index/index';}, 4000);
153         });
154     });
155
156     // Toggle status for date
157     $('div.tour table#plan td#date-status').click(function(e){
158         var cell = $(this);
159         ajax_request('tour/'+tour_key+'/togglestatus',
160                      'id='+cell.attr('data-id'),
161                      function(data){
162                          cell.attr('class', 'bg-'+data.status_key);
163                          cell.text(data.status_text);
164                      });
165     });
166
167     // Toggle status for date in matrix
168     $('div.tour table#matrix td#date-toggle').click(function(e){
169         var cell = $(this);
170         ajax_request('tour/'+tour_key+'/togglestatus',
171                      {id: cell.attr('data-date-id'),
172                       user: cell.attr('data-user-id')},
173                      function(data){
174                          cell.attr('class', 'bg-'+data.status_key);
175                      });
176     });
177
178     // Set tour member status
179     $('#tourmember_save').click(function(e){
180         ajax_request('tour/'+tour_key+'/tourmember', $('form[name="tourmember"]').serialize(), function(data){
181             show_message('Status gespeichert', 'info');
182         });
183     });
184
185     // Add new date to tour
186     $('#newdate_save').click(function(e){
187         if (!$('#start_date').val().length) return;
188         ajax_request('tour/'+tour_key+'/newdate', $('form[name="newdate"]').serialize(), function(data){
189             load_dates();
190             show_message('Termin gespeichert', 'info');
191         });
192     });
193
194     // Add new POV
195     $('#newpov_save').click(function(e){
196         if (!$('form[name="newpov"] #destination').val().length) return;
197         ajax_request('tour/'+tour_key+'/newpov', $('form[name="newpov"]').serialize(), function(data){
198             load_pov();
199             show_message('Zwischenziel gespeichert', 'info');
200             $('form[name="newpov"] #destination').val('');
201         });
202     });
203
204     // Add new member to tour
205     $('div.invite button.btn-primary').click(function(e){
206         var button = $(this);
207         if (!button.attr('data-id').length) return;
208         ajax_request('tour/'+tour_key+'/invite',
209                      'sys_user_id='+button.attr('data-id'),
210                      function(data){
211                          button.removeClass('btn-primary').addClass('btn-success').attr('disabled', 'disabled');
212                      });
213     });
214     // Create new biker and add new member to tour
215     $('div.invite button.btn-warning').click(function(e){
216         window.location.href = tour_base_url + 'tour/' + tour_key + '/newmember';
217     });
218     $('#newbiker_save').click(function(e){
219         if (!$('form[name="newbiker"] #name').val().length ||
220             !$('form[name="newbiker"] #email').val().length)
221             return show_message('Name und E-Mail müssen ausgefüllt sein', 'error');
222
223         if (typeof tour_key == 'string')
224             var backend = 'tour/'+tour_key+'/newbiker';
225         else
226             var backend = 'tour/newbiker';
227         ajax_request(backend, $('form[name="newbiker"]').serialize(), function(data){
228             show_message('Biker gespeichert', 'info');
229             if (typeof tour_key == 'string')
230                 setTimeout(function(){window.location.href = tour_base_url + 'tour/' + tour_key + '/invite';}, 4000);
231         });
232     });
233
234     // Notizen
235     $('.container.notes #btn_new').click(function(e){
236         window.location.href = tour_base_url + 'tour/'+tour_key+'/notenew';
237     });
238     $('.container.notes .alert-dark .alert-primary').click(function(e){
239         $('div#note_del').center().show();
240         $('div#note_del button').attr('data-id', $(this).attr('data-id'));
241     });
242     $('div#note_del button#btn_del').click(function(e){
243         var note_id = $(this).attr('data-id');
244
245         ajax_request('tour/'+tour_key+'/notedel', 'id='+note_id, function(data){
246             $('div#note_del').hide();
247             $('div.note[data-id="'+note_id+'"]').hide();
248             show_message('Notiz gelöscht', 'info');
249         });
250     });
251     $('div#note_del button#btn_close').click(function(e){
252         $('div#note_del').hide();
253     });
254     $('#newnote_save').click(function(e){
255         var dest_url = tour_base_url + 'tour/'+tour_key+'/notes';
256         if (!$('form[name="newnote"] #note').val().length) {
257             window.location.href = dest_url;
258             return;
259         }
260
261         ajax_request('tour/'+tour_key+'/notenew', $('form[name="newnote"]').serialize(), function(data){
262             show_message('Notiz gespeichert', 'info');
263                 setTimeout(function(){window.location.href = dest_url;}, 2000);
264         });
265     });
266     $('#newnote_second').click(function(e){
267         if (!$('form[name="newnote"] #note').val().length)
268             return;
269
270         $('form[name="newnote"]').submit();
271     });
272
273     // Administration
274     $('#tourplan_save').click(function(e){
275         if (!$('form[name="tourplan"] #name').val().length ||
276             !$('form[name="tourplan"] #urlkey').val().length ||
277             !$('form[name="tourplan"] #year').val().length ||
278             !$('form[name="tourplan"] #duration').val().length)
279             return show_message('Name, Key, Jahr und Dauer müssen ausgefüllt sein', 'error');
280
281         ajax_request('tour/'+tour_key+'/admin', $('form[name="tourplan"]').serialize(), function(data){
282             show_message('Informationen gespeichert', 'info');
283         });
284     });
285     $('#touradmin_save').click(function(e){
286         if (!$('form[name="touradmin"] #tour_member_id').val().length) return;
287
288         ajax_request('tour/'+tour_key+'/touradmin', $('form[name="touradmin"]').serialize(), function(data){
289             show_message('Tourleiter gesetzt', 'info');
290             $('form[name="touradmin"] #tour_member_id option[value="'+$('form[name="touradmin"] #tour_member_id').val()+'"]').remove();
291         });
292     });
293     $('#touradmin .btn-danger').click(function(e){
294         var button = $(this);
295         ajax_request('tour/'+tour_key+'/deladmin', 'id='+button.attr('data-id'), function(data){
296             show_message('Tourleiter gelöscht', 'info');
297             button.parents('div.form-row:first').hide();
298         });
299     });
300 });