-
Notifications
You must be signed in to change notification settings - Fork 21
/
script.js
273 lines (239 loc) · 7.57 KB
/
script.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* jshint esversion: 6 */
var time_remaining = 0;
var selected_user = null;
var valid_image = /.*\.(png|svg|jpg|jpeg|bmp)$/i;
///////////////////////////////////////////////
// CALLBACK API. Called by the webkit greeeter
///////////////////////////////////////////////
// called when the greeter asks to show a login prompt for a user
function show_prompt(text) {
var password_container = document.querySelector("#password_container");
var password_entry = document.querySelector("#password_entry");
if (!isVisible(password_container)) {
var users = document.querySelectorAll(".user");
var user_node = document.querySelector("#"+selected_user);
var rect = user_node.getClientRects()[0];
var parentRect = user_node.parentElement.getClientRects()[0];
var center = parentRect.width/2;
var left = center - rect.width/2 - rect.left;
var i = 0;
if (left < 5 && left > -5) {
left = 0;
}
for (i = 0; i < users.length; i++) {
var node = users[i];
setVisible(node, node.id === selected_user);
node.style.left= left;
}
setVisible(password_container, true);
password_entry.placeholder= text.replace(":", "");
}
password_entry.value= "";
password_entry.focus();
}
// called when the greeter asks to show a message
function show_message(text) {
var message = document.querySelector("#message_content");
message.innerHTML= text;
if (text) {
document.querySelector("#message").classList.remove("hidden");
} else {
document.querySelector("#message").classList.add("hidden");
}
message.classList.remove("error");
}
// called when the greeter asks to show an error
function show_error(text) {
show_message(text);
var message = document.querySelector("#message_content");
message.classList.add("error");
}
// called when the greeter is finished the authentication request
function authentication_complete() {
var container = document.querySelector("#session_container");
var children = container.querySelectorAll("input");
var i = 0;
var key = "";
for (i = 0; i < children.length; i++) {
var child = children[i];
if (child.checked) {
key = child.value;
break;
}
}
if (lightdm.is_authenticated) {
if (key === "") {
lightdm.login(lightdm.authentication_user, lightdm.default_session);
} else {
lightdm.login(lightdm.authentication_user, key);
}
} else {
show_error("Authentication Failed");
start_authentication(selected_user);
}
}
// called when the greeter wants us to perform a timed login
function timed_login(user) {
lightdm.login (lightdm.timed_login_user);
//setTimeout('throbber()', 1000);
}
//////////////////////////////
// Implementation
//////////////////////////////
function start_authentication(username) {
lightdm.cancel_timed_login();
selected_user = username;
var userObj = lightdm.users.filter((user) => (user.username)?user.username === username : user.name === username)[0];
if(typeof(userObj) !== "undefined" && userObj.session) {
Array.prototype.slice.call(document.querySelectorAll("input[type=radio][name=session]"), 0).forEach((radio) => {
radio.checked = false;
if(radio.value === userObj.session) radio.checked = true;
});
}
else {
Array.prototype.slice.call(document.querySelectorAll("input[type=radio][name=session]"), 0).forEach((radio) => {
radio.checked = false;
if(radio.value === lightdm.default_session.key) radio.checked = true;
});
}
lightdm.start_authentication(username);
}
function provide_secret() {
show_message("Logging in...");
entry = document.querySelector('#password_entry');
lightdm.respond(entry.value);
}
function initialize_sessions() {
var template = document.querySelector("#session_template");
var container = session_template.parentElement;
var i = 0;
container.removeChild(template);
for (i = 0; i < lightdm.sessions.length; i = i + 1) {
var session = lightdm.sessions[i];
var s = template.cloneNode(true);
s.id = "session_" + session.key;
var label = s.querySelector(".session_label");
var radio = s.querySelector("input");
label.innerHTML = session.name;
radio.value = session.key;
radio.addEventListener("click", function() {
if(document.getElementById("password_entry").value != "") document.querySelectorAll("form.password_prompt")[0].submit();
});
/*
var default_session = 'default' == lightdm.default_session && 0 === i;
if (session.key === lightdm.default_session || default_session) {
radio.checked = true;
}
*/
container.appendChild(s);
}
}
function show_users() {
var users = document.querySelectorAll(".user");
var i = 0;
for (i= 0; i < users.length; i++) {
setVisible(users[i], true);
users[i].style.left = 0;
}
setVisible(document.querySelector("#password_container"), false);
selected_user = null;
}
function user_clicked(event) {
if (selected_user !== null) {
selected_user = null;
lightdm.cancel_authentication();
show_users();
} else {
selected_user = event.currentTarget.id;
start_authentication(event.currentTarget.id);
}
show_message("");
event.stopPropagation();
return false;
}
function setVisible(element, visible) {
if (visible) {
element.classList.remove("hidden");
} else {
element.classList.add("hidden");
}
}
function isVisible(element) {
return !element.classList.contains("hidden");
}
function update_time() {
var time = document.querySelector("#current_time");
var date = new Date();
var hh = date.getHours();
var mm = date.getMinutes();
var ss = date.getSeconds();
var suffix= "AM";
if (hh > 12) {
hh = hh - 12;
suffix = "PM";
}
if (hh < 10) { hh = "0"+hh; }
if (mm < 10) { mm = "0"+mm; }
if (ss < 10) { ss = "0"+ss; }
time.innerHTML = hh+":"+mm + " " + suffix;
}
//////////////////////////////////
// Initialization
//////////////////////////////////
function initialize() {
show_message("");
initialize_users();
initialize_timer();
initialize_sessions();
/*
var bg_image_dir = config.get_str("background_images");
console.log("image_dir", bg_image_dir);
if(bg_image_dir) {
var backgrounds = greeter_util.dirlist(bg_image_dir);
console.log("bg list", backgrounds);
if(backgrounds.length) {
var chosen = backgrounds[Math.floor(Math.random()*backgrounds.length)];
document.querySelectorAll("body")[0].style.backgroundImage = chosen;
}
}
*/
}
function on_image_error(e) {
e.currentTarget.src = "img/avatar.svg";
}
function initialize_users() {
var template = document.querySelector("#user_template");
var parent = template.parentElement;
parent.removeChild(template);
for (i = 0; i < lightdm.users.length; i += 1) {
user = lightdm.users[i];
userNode = template.cloneNode(true);
var image = userNode.querySelectorAll(".user_image")[0];
var name = userNode.querySelectorAll(".user_name")[0];
name.innerHTML = user.display_name;
if (user.image) {
image.src = user.image;
image.onerror = on_image_error;
} else {
image.src = "img/avatar.svg";
}
userNode.id = user.name;
userNode.onclick = user_clicked;
parent.appendChild(userNode);
}
setTimeout(show_users, 400);
}
function initialize_timer() {
update_time();
setInterval(update_time, 1000);
}
function add_action(id, name, image, clickhandler, template, parent) {
action_node = template.cloneNode(true);
action_node.id = "action_" + id;
img_node = action_node.querySelectorAll(".action_image")[0];
label_node = action_node.querySelectorAll(".action_label")[0];
label_node.innerHTML = name;
img_node.src = image;
action_node.onclick = clickhandler;
parent.appendChild(action_node);
}