]>
Commit | Line | Data |
---|---|---|
f255c3e2 | 1 | var state |
db626555 | 2 | var wos |
9d2ed0ce | 3 | |
db626555 CS |
4 | (function( $ ) { |
5 | $.fn.pageConstruct = function( initvar ) { | |
6 | ||
7 | this.buildPage = function() { | |
8 | state = {} | |
9 | this.loadMenu(); | |
10 | this.loadContainer(); | |
9d2ed0ce | 11 | } |
db626555 CS |
12 | |
13 | this.loadDefaults = function() { | |
f8fb5549 | 14 | var self = this; |
db626555 CS |
15 | $.getJSON("/backend/logged_in", function(resp) { |
16 | if (!resp.contents) { | |
f8fb5549 CS |
17 | self.hideMenu(); |
18 | self.hideBackground(); | |
19 | self.loadLogin(); | |
db626555 | 20 | } else { |
f8fb5549 CS |
21 | self.loadUsername(); |
22 | self.loadBackground(); | |
db626555 | 23 | } |
f255c3e2 | 24 | }); |
db626555 CS |
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">×</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>'); | |
3372001f | 39 | $('.container-fluid').append('<a class="brand" href="#">KronOS</a>'); |
db626555 CS |
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 | } | |
4d35980c | 64 | }); |
db626555 CS |
65 | }; |
66 | ||
67 | this.hideMenu = function() { | |
68 | $('.menuitems').empty(); | |
4d35980c | 69 | } |
90b2985c | 70 | |
db626555 CS |
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 | } | |
90b2985c | 85 | |
db626555 CS |
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 | }; | |
42e9adcf | 90 | |
db626555 CS |
91 | this.hideBackground = function() { |
92 | $('style').remove(); | |
93 | } | |
94 | ||
95 | this.loadUsername = function() { | |
96 | $("#username").html(state.name); | |
f255c3e2 | 97 | } |
af724f74 | 98 | |
db626555 CS |
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 | } | |
f8fb5549 CS |
109 | this.loadUsername(); |
110 | this.loadMenuItems(); | |
111 | this.loadBackground(); | |
4d35980c | 112 | |
db626555 CS |
113 | $('#loginModal').modal('hide'); |
114 | $('#inputUsername').val("") | |
115 | $(".alert").remove(); | |
116 | } else { | |
f8fb5549 | 117 | this.showError(resp.error, 'error', '.messagebody'); |
db626555 | 118 | } |
7d24cfef | 119 | |
db626555 CS |
120 | $('#inputPassword').val("") |
121 | }, "json"); | |
122 | } | |
123 | ||
124 | this.logout = function() { | |
125 | state = {} | |
126 | this.loadDefaults(); | |
127 | } | |
128 | ||
129 | return this; | |
130 | }; | |
131 | })( jQuery ); | |
4d35980c | 132 | |
db626555 | 133 | $(function () { |
f8fb5549 | 134 | wos = $('document.body').pageConstruct(); |
f255c3e2 | 135 | |
db626555 CS |
136 | wos.buildPage(); |
137 | wos.loadDefaults(); | |
f255c3e2 | 138 | }); |