-
Notifications
You must be signed in to change notification settings - Fork 2
/
settings.js
116 lines (101 loc) · 3.14 KB
/
settings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* This file is part of Chronos. The LICENSE file at the top level of
* this repository contains the full copyright notices and license terms.
*/
var form = jQuery(document).find('form');
var inputURL = jQuery(document).find('#inputURL');
var inputDB = jQuery(document).find('#inputDB');
var inputUser = jQuery(document).find('#inputUser');
var pKey = jQuery(document).find('#key');
var buttonRegister = jQuery(document).find('#buttonRegister');
var buttonReset = jQuery(document).find('#buttonReset');
form.submit(submit);
buttonRegister.click(register);
buttonReset.click(reset);
initialize();
function initialize() {
var url = localStorage.getItem('url');
var db = localStorage.getItem('db');
var user = localStorage.getItem('user');
var key = localStorage.getItem('key');
if (!url && document.location.protocol.startsWith('http')) {
url = (
document.location.protocol + '//' + document.location.host);
}
inputURL.val(url);
inputDB.val(db);
inputUser.val(user);
var configured = Boolean(url && db && user);
inputURL.prop('readonly', configured);
inputDB.prop('readonly', configured);
inputUser.prop('readonly', configured);
if (key) {
pKey.text(key.slice(0, 8) + '...');
} else {
pKey.text('');
}
buttonRegister.prop('disabled', configured);
buttonReset.prop('disabled', !configured);
}
function submit(evt) {
evt.preventDefault();
}
function register() {
var btn = jQuery(this);
var url = inputURL.val();
var db = inputDB.val();
var user = inputUser.val();
if (!url || !db || !user) {
return;
}
btn.prop('disabled', true);
var register_url = (url + '/' + db + '/user/application/')
.replace(/([^:]\/)\/+/g, "$1");
var data = {
'user': user,
'application': 'timesheet'
};
jQuery.ajax({
'url': register_url,
'contentType': 'application/json',
'data': JSON.stringify(data),
'dataType': 'json',
'type': 'POST'})
.done(function(data) {
localStorage.setItem('url', url);
localStorage.setItem('db', db);
localStorage.setItem('user', user);
localStorage.setItem('key', data);
window.location.href = "key.html";
})
.fail(function(request, status, error) {
alert(error);
}).always(function() {
initialize();
});
}
function reset() {
var btn = jQuery(this);
var url = localStorage.getItem('url');
var db = localStorage.getItem('db');
var user = localStorage.getItem('user');
var key = localStorage.getItem('key');
btn.prop('disabled', true);
var reset_url = (url + '/' + db + '/user/application/')
.replace(/([^:]\/)\/+/g, "$1");
var data = {
'user': user,
'key': key,
'application': 'timesheet'
};
jQuery.ajax({
'url': reset_url,
'contentType': 'application/json',
'data': JSON.stringify(data),
'dataType': 'json',
'type': 'DELETE'})
.always(function() {
localStorage.clear();
initialize();
});
}