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