]> jfr.im git - z_archive/KronOS.git/blob - public/js/application.js
Added ability for apps to define javascript -- static method scripts -- not core...
[z_archive/KronOS.git] / public / js / application.js
1 (function( $ ) {
2 $.fn.pageConstruct = function() {
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 id="menu" class="container-fluid"></div></div>');
37 $('#menu').append('<a class="brand dropdown-toggle" href="#" tabindex="-1" data-toggle="dropdown">KronOS <b class="caret"></b></a><ul id="coreapps" class="dropdown-menu"></ul>');
38 $('#menu').append('<div class="menuitems"></div>');
39 $('#menu').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 $('#coreapps').append('<li><a tabindex="-1" href="javascript:wos.openCoreApp(\'credits\');void(0);">Credits</a></li>');
45 };
46
47 this.loadContainer = function() {
48 $('body').append('<div id="desktop" class="container-fluid"></div>');
49 $('desktop').append('<div class="row"></div>');
50 };
51
52 this.loadMenuItems = function() {
53 $.getJSON("backend/get_menu", function(resp) {
54 if (resp.success) {
55 navStr = '<ul class="nav">';
56 $.each(resp.contents, function(key, val) {
57 openStr = '<li class="dropdown"><a tabindex="-1" class="dropdown-toggle" data-toggle="dropdown" href="#">'+key+' <b class="caret"></b></a><ul class="dropdown-menu">';
58 innerStr = '';
59 closeStr = '</ul></li>';
60 $.each(val, function(ikey, ival) {
61 if (typeof ival == "object") {
62 innerStr += '<li class="dropdown-submenu"><a tabindex="-1" href="#">'+ikey+'</a><ul class="dropdown-menu">';
63 $.each(ival, function(iikey, iival) {
64 innerStr += '<li><a tabindex="-1" href=\''+iival+'\'>'+iikey+'</a></li>';
65 });
66 innerStr += '</ul></li>';
67 } else {
68 innerStr += '<li><a tabindex="-1" href=\''+ival+'\'>'+ikey+'</a></li>';
69 }
70 });
71 navStr += openStr+innerStr+closeStr;
72 });
73 navStr += '</ul>';
74 $('.menuitems').html(navStr);
75 } else {
76 throwError(resp.error, 'error', '#desktop');
77 }
78 });
79 };
80
81 this.hideMenu = function() {
82 $('.menuitems').empty();
83 }
84
85 this.loadLogin = function() {
86 $.ajax({
87 url: "backend/login_modal",
88 success: function (data) {
89 $('body').append(data);
90 $('#loginModal').modal({
91 backdrop: 'static',
92 keyboard: false,
93 });
94 $('#loginModal').modal('show');
95 },
96 dataType: 'html'
97 });
98 }
99
100 this.loadBackground = function() {
101 background = '<style>body { background-image:url(\'/public/img/default-background.jpg\'); background-position: center top; } </style>'
102 $('body').append(background);
103 };
104
105 this.hideBackground = function() {
106 $('style').remove();
107 }
108
109 this.setUsername = function(name) {
110 $("#username").html(name);
111 }
112
113 this.submitLogin = function() {
114 loginData = {
115 'username': $('#inputUsername').val(),
116 'password': $('#inputPassword').val()
117 };
118 self = this;
119 $.post('account/login', loginData, function(resp) {
120 if (resp.success) {
121 document.cookie = "session_id="+resp.data.sid+";expires=0";
122 self.setUsername(resp.data.name);
123 self.loadMenuItems();
124 self.loadBackground();
125
126 $('#loginModal').modal('hide');
127 $('#inputUsername').val("")
128 $(".alert").remove();
129 } else {
130 self.showError(resp.error, 'error', '.messagebody');
131 }
132
133 $('#inputPassword').val("")
134 }, "json");
135 }
136
137 this.credits = function() {
138 $('body').append('<div id="credits" class="modal hide fade"></div>');
139
140 $('#credits').append('<div class="modal-header">');
141 $('#credits').append('<div class="modal-body">');
142 $('#credits').append('<div class="modal-footer">');
143
144 $('').append('');
145 $('#credits>.modal-header').append('<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>');
146 $('#credits>.modal-header').append('<h3 id="creditsLabel">About KronOS</h3>');
147
148 $('#credits>.modal-body').append('<h4>Committers</h4>');
149 $('#credits>.modal-body').append('<ul><li>BiohZn</li><li>DimeCadmium</li><li>Oscar</li><li>hyster</li><li>DarkDeviL</li></ul>');
150
151 $('#credits>.modal-footer').append('<button class="btn" aria-hidden="true" onClick="wos.hideCredits();">Close</button>');
152
153 $('#credits').modal({
154 backdrop: false
155 });
156 $('#credits').modal('show');
157 }
158
159 this.hideCredits = function() {
160 $('#credits').modal('hide');
161 $('#credits').remove();
162 }
163
164 this.logout = function() {
165 document.cookie = "session_id=0;expires=0";
166 this.loadDefaults();
167 }
168
169 this.openApp = function(appid) {
170 $.getJSON("control/open/"+appid, function(resp) {
171 if (resp.success) {
172 var repl = resp.contents
173 var target = 'div#'+repl.name+repl.id;
174
175 wos.apps[repl.id] = {aid: appid, instance: repl.id, title: repl.title, target: target};
176
177 $.getScript('control/scripts/'+appid, function() {
178 wos.appscripts[appid].load(target);
179 });
180
181 $('body').append('<div id="'+repl.name+repl.id+'" class="app modal hide fade"></div>');
182
183 $(target).append('<div class="modal-header"></div>');
184 $(target).append('<div class="modal-body"></div>');
185 $(target).append('<div class="modal-footer"></div>');
186
187 $(target+'>.modal-header').append('<button type="button" class="close" aria-hidden="true" data-dismiss="modal" onClick="wos.closeApp(\'#'+repl.name+repl.id+'\');void(0);">&times;</button>');
188 $(target+'>.modal-header').append('<h3 class="appLabel">'+repl.title+'</h3>');
189
190 $(target+'>.modal-body').append(repl.interior);
191
192 $(target).modal({ backdrop: false });
193 $(target).modal('show');
194 } else {
195 throwError(resp.error, 'error', '#desktop');
196 }
197 });
198 }
199
200 this.openCoreApp = function(appname) {
201 this.openApp("-1/"+appname);
202 }
203
204 this.closeApp = function(target) {
205 $(target).modal('hide');
206 $(target).remove();
207 }
208
209 this.apps = {};
210 this.appscripts = {}
211
212 return this;
213 };
214 })( jQuery );
215
216 $(function () {
217 wos = $('document.body').pageConstruct();
218
219 wos.buildPage();
220 wos.loadDefaults();
221 });