]>
Commit | Line | Data |
---|---|---|
db626555 CS |
1 | (function( $ ) { |
2 | $.fn.pageConstruct = function( initvar ) { | |
3 | ||
4 | this.buildPage = function() { | |
2c98ae78 | 5 | document.cookie = "session_id=0;expires=0"; |
db626555 CS |
6 | this.loadMenu(); |
7 | this.loadContainer(); | |
9d2ed0ce | 8 | } |
db626555 CS |
9 | |
10 | this.loadDefaults = function() { | |
f8fb5549 | 11 | var self = this; |
2c98ae78 JR |
12 | // $.getJSON("/backend/logged_in", function(resp) { |
13 | // if (!resp.contents) { | |
f8fb5549 CS |
14 | self.hideMenu(); |
15 | self.hideBackground(); | |
2c98ae78 | 16 | self.setUsername('<em>unauthenticated</em>'); |
f8fb5549 | 17 | self.loadLogin(); |
2c98ae78 JR |
18 | // } else { |
19 | // self.loadUsername(); | |
20 | // self.loadBackground(); | |
21 | // } | |
22 | // }); | |
db626555 CS |
23 | } |
24 | ||
25 | this.showError = function(e, t, c) { | |
26 | var n = $(".alert").length; | |
27 | if (n>2) { | |
28 | $(".alert").first().remove(); | |
29 | } | |
30 | error = '<div class="alert alert-block alert-' + t + '"><button type="button" class="close" data-dismiss="alert">×</button>' + e + '</div>' | |
31 | $(c).append(error); | |
32 | } | |
33 | ||
34 | this.loadMenu = function() { | |
35 | $('body').append('<div class="navbar navbar-inverse navbar-fixed-top"></div>'); | |
36 | $('.navbar').append('<div class="navbar-inner"><div class="container-fluid"></div></div>'); | |
3372001f | 37 | $('.container-fluid').append('<a class="brand" href="#">KronOS</a>'); |
2c98ae78 | 38 | $('.container-fluid').append('<div class="menuitems"></div>'); |
80b30533 JR |
39 | $('.container-fluid').append('<p class="navbar-text pull-right">Logged in as <a href="#" class="navbar-link" id="username"><em>unauthenticated</em></a> • <span style="font-family:monospace;"><span id="clock"></span></span></p>'); |
40 | $('#clock').jclock({ | |
41 | format: '%H:%M', | |
42 | }); | |
db626555 CS |
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) { | |
2c98ae78 | 53 | navStr = '<ul class="nav">'; |
db626555 | 54 | $.each(resp.contents, function(key, val) { |
7b1c4752 | 55 | openStr = '<li class="dropdown"><a tabindex="-1" class="dropdown-toggle" data-toggle="dropdown" href="#">'+key+' <b class="caret"></b></a><ul class="dropdown-menu">'; |
2c98ae78 JR |
56 | innerStr = ''; |
57 | closeStr = '</ul></li>'; | |
58 | $.each(val, function(ikey, ival) { | |
ff79bc64 JR |
59 | if (typeof ival == "object") { |
60 | innerStr += '<li class="dropdown-submenu"><a tabindex="-1" href="#">'+ikey+'</a><ul class="dropdown-menu">'; | |
61 | $.each(ival, function(iikey, iival) { | |
62 | innerStr += '<li><a tabindex="-1" href=\''+iival+'\'>'+iikey+'</a></li>'; | |
63 | }); | |
64 | innerStr += '</ul></li>'; | |
65 | } else { | |
66 | innerStr += '<li><a tabindex="-1" href=\''+ival+'\'>'+ikey+'</a></li>'; | |
67 | } | |
2c98ae78 JR |
68 | }); |
69 | navStr += openStr+innerStr+closeStr; | |
db626555 | 70 | }); |
2c98ae78 JR |
71 | navStr += '</ul>'; |
72 | $('.menuitems').html(navStr); | |
db626555 CS |
73 | } else { |
74 | throwError(resp.error, 'error', '#desktop'); | |
75 | } | |
4d35980c | 76 | }); |
db626555 CS |
77 | }; |
78 | ||
79 | this.hideMenu = function() { | |
80 | $('.menuitems').empty(); | |
4d35980c | 81 | } |
90b2985c | 82 | |
db626555 CS |
83 | this.loadLogin = function() { |
84 | $.ajax({ | |
85 | url: "/backend/login_modal", | |
86 | success: function (data) { | |
87 | $('body').append(data); | |
88 | $('#loginModal').modal({ | |
89 | backdrop: 'static', | |
90 | keyboard: false, | |
91 | }); | |
92 | $('#loginModal').modal('show'); | |
93 | }, | |
94 | dataType: 'html' | |
95 | }); | |
96 | } | |
90b2985c | 97 | |
db626555 CS |
98 | this.loadBackground = function() { |
99 | background = '<style>body { background-image:url(\'/public/img/default-background.jpg\'); background-position: center top; } </style>' | |
100 | $('body').append(background); | |
101 | }; | |
42e9adcf | 102 | |
db626555 CS |
103 | this.hideBackground = function() { |
104 | $('style').remove(); | |
105 | } | |
106 | ||
2c98ae78 JR |
107 | this.setUsername = function(name) { |
108 | $("#username").html(name); | |
f255c3e2 | 109 | } |
af724f74 | 110 | |
db626555 CS |
111 | this.submitLogin = function() { |
112 | loginData = { | |
113 | 'username': $('#inputUsername').val(), | |
114 | 'password': $('#inputPassword').val() | |
115 | }; | |
7fba2acf | 116 | self = this; |
db626555 CS |
117 | $.post('/account/login', loginData, function(resp) { |
118 | if (resp.success) { | |
2c98ae78 JR |
119 | document.cookie = "session_id="+resp.data.sid+";expires=0"; |
120 | self.setUsername(resp.data.name); | |
7fba2acf CS |
121 | self.loadMenuItems(); |
122 | self.loadBackground(); | |
4d35980c | 123 | |
db626555 CS |
124 | $('#loginModal').modal('hide'); |
125 | $('#inputUsername').val("") | |
126 | $(".alert").remove(); | |
127 | } else { | |
7fba2acf | 128 | self.showError(resp.error, 'error', '.messagebody'); |
db626555 | 129 | } |
7d24cfef | 130 | |
db626555 CS |
131 | $('#inputPassword').val("") |
132 | }, "json"); | |
133 | } | |
134 | ||
09fed158 JR |
135 | this.credits = function() { |
136 | $('body').append('<div id="credits" class="modal hide fade"></div>'); | |
137 | ||
138 | $('#credits').append('<div class="modal-header">'); | |
139 | $('#credits').append('<div class="modal-body">'); | |
140 | $('#credits').append('<div class="modal-footer">'); | |
141 | ||
142 | $('').append(''); | |
143 | $('#credits>.modal-header').append('<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'); | |
144 | $('#credits>.modal-header').append('<h3 id="creditsLabel">About KronOS</h3>'); | |
145 | ||
146 | $('#credits>.modal-body').append('<h4>Committers</h4>'); | |
147 | $('#credits>.modal-body').append('<ul><li>BiohZn</li><li>DimeCadmium</li><li>Oscar</li><li>hyster</li><li>DarkDeviL</li></ul>'); | |
148 | ||
149 | $('#credits>.modal-footer').append('<button class="btn" aria-hidden="true" onClick="wos.hideCredits();">Close</button>'); | |
150 | ||
151 | $('#credits').modal({ | |
152 | backdrop: false | |
153 | }); | |
154 | $('#credits').modal('show'); | |
155 | } | |
156 | ||
157 | this.hideCredits = function() { | |
158 | $('#credits').modal('hide'); | |
159 | $('#credits').remove(); | |
160 | } | |
161 | ||
db626555 | 162 | this.logout = function() { |
2c98ae78 | 163 | document.cookie = "session_id=0;expires=0"; |
db626555 CS |
164 | this.loadDefaults(); |
165 | } | |
166 | ||
3cf8917d JR |
167 | this.openApp = function(appid) { |
168 | $.getJSON("/control/open/"+appid, function(resp) { | |
169 | if (resp.success) { | |
170 | var repl = resp.contents | |
171 | var target = 'div#'+repl.name; | |
172 | ||
173 | if ($(target).length != 0) { | |
174 | this.closeApp(target); | |
175 | } | |
176 | ||
177 | this.apps[repl.name] = {aid: appid, instance: repl.id, title: repl.title, target: target}; | |
178 | ||
179 | $('body').append('<div id="'+repl.name+'" class="modal hide fade"></div>'); | |
180 | ||
181 | $(target).append('<div class="modal-header"></div>'); | |
182 | $(target).append('<div class="modal-body"></div>'); | |
183 | $(target).append('<div class="modal-footer"></div>'); | |
184 | ||
185 | $(target+'>.modal-header').append('<button class="btn appclose" aria-hidden="true" onClick="wos.closeApp(\'#'+repl.name+'\');void(0);">×</button>'); | |
186 | $(target+'>.modal-header').append('<h3 class="appLabel">'+repl.title+'</h3>'); | |
187 | ||
188 | $(target+'>.modal-body').append(repl.interior); | |
189 | } else { | |
190 | throwError(resp.error, 'error', '#desktop'); | |
191 | } | |
192 | }); | |
193 | } | |
194 | ||
195 | this.openCoreApp = function(appname) { | |
196 | } | |
197 | ||
198 | this.closeApp = function(target) { | |
199 | $(target).modal('hide'); | |
200 | $(target).remove(); | |
201 | } | |
202 | ||
203 | return this; | |
db626555 CS |
204 | }; |
205 | })( jQuery ); | |
4d35980c | 206 | |
db626555 | 207 | $(function () { |
f8fb5549 | 208 | wos = $('document.body').pageConstruct(); |
f255c3e2 | 209 | |
db626555 CS |
210 | wos.buildPage(); |
211 | wos.loadDefaults(); | |
f255c3e2 | 212 | }); |