Base application
[infodrom.org/touren.infodrom.org] / touren.js
diff --git a/touren.js b/touren.js
new file mode 100644 (file)
index 0000000..0229de1
--- /dev/null
+++ b/touren.js
@@ -0,0 +1,104 @@
+// Source http://stackoverflow.com/questions/210717/using-jquery-to-center-a-div-on-the-screen
+//
+(function($){
+    $.fn.center = function () {
+       this.css("position","absolute");
+       this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) +
+                                $(window).scrollTop()) + "px");
+       this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) +
+                                 $(window).scrollLeft()) + "px");
+       return this;
+    };
+})($);
+
+// type = ['error', 'warning', 'info']
+function show_message(text, type)
+{
+    if (!type) type = 'info';
+
+    if (type == 'error') {
+       var classname = 'alert-danger';
+    } else if (type == 'warning') {
+       var classname = 'alert-warning';
+    } else {
+       var classname = 'alert-success';
+    }
+
+    $('div#message').attr('class', 'alert ' + classname).text(text).center().show();
+    setTimeout(function(){$('div#message').hide();}, 3000);
+
+    return false;
+}
+
+function ajax_request(route, parms, callback)
+{
+    $.post(tour_base_url + route,
+          parms,
+          function(data){
+              if (!data.status) {
+                  if (data.error) {
+                      if (data.redirect_login) {
+                          show_message(data.error, 'error');
+                          setTimeout(function(){window.location.href = '/';}, 5000);
+                          return;
+                      } else
+                          return show_message(data.error, 'error');
+                  } else
+                      return show_message('Fehler im Backend zu "'+route+'" aufgetreten', 'error');
+              }
+              if (data.warning)
+                  show_message(data.warning, 'warning');
+              if (data.info)
+                  show_message(data.info, 'info');
+              if (typeof(callback) == 'function')
+                  callback(data);
+          });
+}
+
+$(function(){
+    $('input').on('keydown', function(e){
+       if (e.which === 13)
+           e.preventDefault();
+    });
+
+    // Login form
+    $('.lostpw a').click(function(e){
+       if (!$('.loginform form #login').val().length)
+           return show_message('Bitte E-Mail angeben', 'error');
+
+       ajax_request('account/lostpw', 'email='+$('.loginform form #login').val(), function(data){
+           return show_message('Eine Mail an wurde verschickt.');
+       });
+
+       return false;
+    });
+
+    // Passwort setzen
+    $('.lostpwform .btn').click(function(e){
+       if (!$('.lostpwform #password').val().length)
+           return show_message('Bitte ein neues Passwort setzen', 'error');
+
+       if ($('.lostpwform #password').val() != $('.lostpwform #password2').val())
+           return show_message('Die Passwörter stimmen nicht überein', 'error');
+
+       if ($('.lostpwform #password').val().length < 8)
+           return show_message('Das Passwort muss mindestens 8 Zeichen lang sein', 'error');
+
+       ajax_request('account/setpw', $('.lostpwform form').serialize(), function(data){
+           if (data.goto) {
+               show_message('Gespeichert. Bitte neu anmelden.');
+               setTimeout(function(){window.location.href = data.goto;}, 2000);
+           }
+       });
+
+       return false;
+    });
+
+    // Settings dialog
+    $('#settings_save').click(function(e){
+       ajax_request('index/settings', $('form[name="settings"]').serialize(), function(data){
+           show_message('Einstellungen gespeichert', 'info');
+           setTimeout(function(){window.location.href = tour_base_url + 'index/index';}, 4000);
+       });
+    });
+});