]> jfr.im git - z_archive/KronOS.git/blob - public/js/application.js
092fc6e8006dcb5912d4e8c006c8e1c7820ddc34
[z_archive/KronOS.git] / public / js / application.js
1 (function( $ ) {
2 $.fn.pageConstruct = function( initvar ) {
3
4 this.buildPage = function() {
5 document.cookie = "session_id=0;expires=0";
6 this.loadMenu();
7 this.loadContainer();
8 }
9
10 this.loadDefaults = function() {
11 var self = this;
12 // $.getJSON("backend/logged_in", function(resp) {
13 // if (!resp.contents) {
14 self.hideMenu();
15 self.hideBackground();
16 self.setUsername('<em>unauthenticated</em>');
17 self.loadLogin();
18 // } else {
19 // self.loadUsername();
20 // self.loadBackground();
21 // }
22 // });
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">&times;</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>');
37 $('.container-fluid').append('<a class="brand" href="#">KronOS</a>');
38 $('.container-fluid').append('<div class="menuitems"></div>');
39 $('.container-fluid').append('<p class="navbar-text pull-right">Logged in as <a href="#" class="navbar-link" id="username"><em>unauthenticated</em></a> &bull; <span style="font-family:monospace;"><span id="clock"></span></span></p>');
40 $('#clock').jclock({
41 format: '%H:%M',
42 });
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 navStr = '<ul class="nav">';
54 $.each(resp.contents, function(key, val) {
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">';
56 innerStr = '';
57 closeStr = '</ul></li>';
58 $.each(val, function(ikey, ival) {
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 }
68 });
69 navStr += openStr+innerStr+closeStr;
70 });
71 navStr += '</ul>';
72 $('.menuitems').html(navStr);
73 } else {
74 throwError(resp.error, 'error', '#desktop');
75 }
76 });
77 };
78
79 this.hideMenu = function() {
80 $('.menuitems').empty();
81 }
82
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 }
97
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 };
102
103 this.hideBackground = function() {
104 $('style').remove();
105 }
106
107 this.setUsername = function(name) {
108 $("#username").html(name);
109 }
110
111 this.submitLogin = function() {
112 loginData = {
113 'username': $('#inputUsername').val(),
114 'password': $('#inputPassword').val()
115 };
116 self = this;
117 $.post('account/login', loginData, function(resp) {
118 if (resp.success) {
119 document.cookie = "session_id="+resp.data.sid+";expires=0";
120 self.setUsername(resp.data.name);
121 self.loadMenuItems();
122 self.loadBackground();
123
124 $('#loginModal').modal('hide');
125 $('#inputUsername').val("")
126 $(".alert").remove();
127 } else {
128 self.showError(resp.error, 'error', '.messagebody');
129 }
130
131 $('#inputPassword').val("")
132 }, "json");
133 }
134
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">&times;</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
162 this.logout = function() {
163 document.cookie = "session_id=0;expires=0";
164 this.loadDefaults();
165 }
166
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+repl.id;
172
173 apps[repl.name] = {aid: appid, instance: repl.id, title: repl.title, target: target};
174
175 $('body').append('<div id="'+repl.name+repl.id+'" class="app modal hide fade"></div>');
176
177 $(target).append('<div class="modal-header"></div>');
178 $(target).append('<div class="modal-body"></div>');
179 $(target).append('<div class="modal-footer"></div>');
180
181 $(target+'>.modal-header').append('<button type="button" class="close" aria-hidden="true" data-dismiss="modal" onClick="wos.closeApp(\'#'+repl.name+'\');void(0);">&times;</button>');
182 $(target+'>.modal-header').append('<h3 class="appLabel">'+repl.title+'</h3>');
183
184 $(target+'>.modal-body').append(repl.interior);
185
186 $(target).modal({ backdrop: false });
187 $(target).modal('show');
188 } else {
189 throwError(resp.error, 'error', '#desktop');
190 }
191 });
192 }
193
194 this.openCoreApp = function(appname) {
195 }
196
197 this.closeApp = function(target) {
198 $(target).modal('hide');
199 $(target).remove();
200 }
201
202 return this;
203 };
204 })( jQuery );
205
206 $(function () {
207 apps = {}
208 wos = $('document.body').pageConstruct();
209
210 wos.buildPage();
211 wos.loadDefaults();
212 });