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