]> jfr.im git - z_archive/KronOS.git/blob - public/js/application.js
fd7db581d1565ca7a1981d84cedf54a3b2242d84
[z_archive/KronOS.git] / public / js / application.js
1 var state
2 var wos
3
4 (function( $ ) {
5 $.fn.pageConstruct = function( initvar ) {
6
7 this.buildPage = function() {
8 state = {}
9 this.loadMenu();
10 this.loadContainer();
11 }
12
13 this.loadDefaults = function() {
14 var self = this;
15 $.getJSON("/backend/logged_in", function(resp) {
16 if (!resp.contents) {
17 self.hideMenu();
18 self.hideBackground();
19 self.loadLogin();
20 } else {
21 self.loadUsername();
22 self.loadBackground();
23 }
24 });
25 }
26
27 this.showError = function(e, t, c) {
28 var n = $(".alert").length;
29 if (n>2) {
30 $(".alert").first().remove();
31 }
32 error = '<div class="alert alert-block alert-' + t + '"><button type="button" class="close" data-dismiss="alert">&times;</button>' + e + '</div>'
33 $(c).append(error);
34 }
35
36 this.loadMenu = function() {
37 $('body').append('<div class="navbar navbar-inverse navbar-fixed-top"></div>');
38 $('.navbar').append('<div class="navbar-inner"><div class="container-fluid"></div></div>');
39 $('.container-fluid').append('<a class="brand" href="#">WebOS Dev</a>');
40 $('.container-fluid').append('<div class="nav-collapse collapse menudiv"></div>');
41 $('.menudiv').append('<div class="menuitems"></div>');
42 $('.menudiv').append('<p class="navbar-text pull-right">Logged in as <a href="#" class="navbar-link" id="username"><em>unauthenticated</em></a></p>');
43 };
44
45 this.loadContainer = function() {
46 $('body').append('<div id="desktop" class="container-fluid"></div>');
47 $('desktop').append('<div class="row"></div>');
48 };
49
50 this.loadMenuItems = function() {
51 $.getJSON("/backend/get_menu", function(resp) {
52 if (resp.success) {
53 var menuitems = []
54 $.each(resp.contents, function(key, val) {
55 menuitems.push('<li><a href="' + val + '">' + key + '</li>');
56 });
57 $('<ul/>', {
58 'class': 'nav',
59 html: menuitems.join('')
60 }).appendTo('.menuitems');
61 } else {
62 throwError(resp.error, 'error', '#desktop');
63 }
64 });
65 };
66
67 this.hideMenu = function() {
68 $('.menuitems').empty();
69 }
70
71 this.loadLogin = function() {
72 $.ajax({
73 url: "/backend/login_modal",
74 success: function (data) {
75 $('body').append(data);
76 $('#loginModal').modal({
77 backdrop: 'static',
78 keyboard: false,
79 });
80 $('#loginModal').modal('show');
81 },
82 dataType: 'html'
83 });
84 }
85
86 this.loadBackground = function() {
87 background = '<style>body { background-image:url(\'/public/img/default-background.jpg\'); background-position: center top; } </style>'
88 $('body').append(background);
89 };
90
91 this.hideBackground = function() {
92 $('style').remove();
93 }
94
95 this.loadUsername = function() {
96 $("#username").html(state.name);
97 }
98
99 this.submitLogin = function() {
100 loginData = {
101 'username': $('#inputUsername').val(),
102 'password': $('#inputPassword').val()
103 };
104 $.post('/account/login', loginData, function(resp) {
105 if (resp.success) {
106 for (key in resp.data) {
107 state[key] = resp.data[key]
108 }
109 this.loadUsername();
110 this.loadMenuItems();
111 this.loadBackground();
112
113 $('#loginModal').modal('hide');
114 $('#inputUsername').val("")
115 $(".alert").remove();
116 } else {
117 this.showError(resp.error, 'error', '.messagebody');
118 }
119
120 $('#inputPassword').val("")
121 }, "json");
122 }
123
124 this.logout = function() {
125 state = {}
126 this.loadDefaults();
127 }
128
129 return this;
130 };
131 })( jQuery );
132
133 $(function () {
134 wos = $('document.body').pageConstruct();
135
136 wos.buildPage();
137 wos.loadDefaults();
138 });