]> jfr.im git - z_archive/KronOS.git/blob - public/js/application.js
Fixed login / logout
[z_archive/KronOS.git] / public / js / application.js
1 var state
2
3 function loadDefaults() {
4 jQuery.getJSON("/backend/logged_in", function(resp) {
5 if (!resp.contents) {
6 hideMenu();
7 hideBackground();
8 loadLoginModal();
9 } else {
10 loadUsername();
11 loadMenu();
12 loadBackground();
13 }
14 });
15 }
16
17 function loadBackground() {
18 background = '<style>body { background-image:url(\'/public/img/default-background.jpg\'); background-position: center top; } </style>'
19 jQuery('body').append(background);
20 }
21
22 function hideBackground() {
23 jQuery('style').remove();
24 }
25
26 function loadLoginModal() {
27 jQuery.ajax({
28 url: "/backend/login_modal",
29 success: function (data) {
30 jQuery('body').append(data);
31 jQuery('#loginModal').modal({
32 backdrop: 'static',
33 keyboard: false,
34 });
35 jQuery('#loginModal').modal('show');
36 },
37 dataType: 'html'
38 });
39
40 }
41
42 function loadUsername() {
43 jQuery("#username").html(state.name);
44 }
45
46 function loadMenu() {
47 jQuery.getJSON("/backend/get_menu", function(resp) {
48 if (resp.success) {
49 var menuitems = []
50 jQuery.each(resp.contents, function(key, val) {
51 menuitems.push('<li><a href="' + val + '">' + key + '</li>');
52 });
53 jQuery('<ul/>', {
54 'class': 'nav',
55 html: menuitems.join('')
56 }).appendTo('.menuitems');
57 } else {
58 throwError(resp.error, 'error', '#desktop');
59 }
60 });
61
62 }
63
64 function hideMenu() {
65 jQuery('.menuitems').empty();
66 }
67
68 function submitLogin() {
69 loginData = {
70 'username': jQuery('#inputUsername').val(),
71 'password': jQuery('#inputPassword').val()
72 };
73 jQuery.post('/account/login', loginData, function(resp) {
74 if (resp.success) {
75 for (key in resp.data) {
76 state[key] = resp.data[key]
77 }
78 jQuery('#loginModal').modal('hide');
79 loadUsername();
80 loadMenu();
81 loadBackground();
82 } else {
83 throwError(resp.error, 'error', '.messagebody');
84 }
85 }, "json");
86 }
87
88 function throwError(e, t, c) {
89 var n = jQuery(".alert").length;
90 if (n>2) {
91 jQuery(".alert").first().remove();
92 }
93
94 error = '<div class="alert alert-block alert-' + t + '"><button type="button" class="close" data-dismiss="alert">&times;</button>' + e + '</div>'
95 jQuery(c).append(error);
96 }
97
98 function logout() {
99 state = {}
100 loadDefaults();
101 }
102
103 jQuery(function () {
104 state = {}
105
106 jQuery.noConflict();
107 loadDefaults();
108 });