forked from ixs/weaveclient-chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
137 lines (119 loc) · 3.98 KB
/
options.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
var background = chrome.extension.getBackgroundPage();
var Weave = background.Weave;
var options = Weave.Chromium.options;
function fillFormFields () {
// Initialize form fields with stored values
var optionElementIds = ["server", "user", "password", "passphrase"];
optionElementIds.forEach(function (id) {
var input = document.getElementById(id);
if (options[id]) {
input.value = options[id];
} else {
input.value = '';
}
input.addEventListener("blur", onInputBlur, "blurred");
});
jQuery('#clientGUID').text(options.client.id);
}
function onLoad () {
fillFormFields();
background.addEventListener("WeaveSyncConnecting", onConnecting, false);
background.addEventListener("WeaveSyncConnected", onConnected, false);
background.addEventListener("WeaveSyncDisconnected", onDisconnected, false);
background.addEventListener("WeaveSyncSyncBegin", onSyncBegin, false);
background.addEventListener("WeaveSyncSyncEnd", onSyncEnd, false);
// Make sure the UI reflects what the client is doing at the moment.
switch (Weave.Chromium.status) {
case "disconnected":
onDisconnected();
break;
case "connecting":
onConnecting();
break;
case "connected":
onConnected();
break;
case "syncing":
onSyncBegin();
break;
}
}
// Event handlers when buttons are pressed
function onConnectButton () {
switch (Weave.Chromium.status) {
case "disconnected":
Weave.Chromium.connect();
return;
case "connected":
Weave.Chromium.disconnect();
return;
default:
// Shouldn't get here, but you never know
return;
}
}
function onResetButton () {
if (Weave.Chromium.status !== "disconnected") {
return;
}
Weave.Chromium.reset();
options = Weave.Chromium.options;
fillFormFields();
}
function onClearCacheButton () {
Weave.Chromium.clearCache();
options = Weave.Chromium.options;
fillFormFields();
}
function onSyncButton () {
if (Weave.Chromium.status !== "connected") {
return;
}
Weave.Chromium.sync();
}
// Event handlers for updating the UI when Weave.Chromium does something
function onConnecting () {
var button = jQuery('#connect-button');
button.attr('disabled', 'disabled');
button.text('Connecting...');
}
function onConnected () {
var button = jQuery('#connect-button');
button.removeAttr('disabled');
button.text('Disconnect');
jQuery('#sync-button').removeAttr('disabled');
}
function onDisconnected () {
var button = jQuery('#connect-button');
button.removeAttr('disabled');
button.text('Connect');
jQuery('#sync-button').attr('disabled', 'disabled');
}
function onSyncBegin () {
var button = jQuery('#sync-button');
button.attr('disabled', 'disabled');
button.text('Syncing...');
jQuery('#connect-button').attr('disabled', 'disabled');
}
function onSyncEnd () {
var button = jQuery('#sync-button');
button.removeAttr('disabled');
button.text('Sync');
jQuery('#sync-button').removeAttr('disabled');
jQuery('#connect-button').removeAttr('disabled');
}
function onInputBlur (event) {
if (Weave.Chromium.status !== "disconnected") {
return;
}
options[event.target.id] = event.target.value;
//XXX is this a good idea? we could be syncing atm...
Weave.Chromium.saveOptions();
}
document.addEventListener('DOMContentLoaded', function () {
document.querySelector('button#connect-button').addEventListener('click', onConnectButton);
document.querySelector('button#reset-button').addEventListener('click', onResetButton);
document.querySelector('button#clear-button').addEventListener('click', onClearCacheButton);
document.querySelector('button#sync-button').addEventListener('click', onSyncButton);
onLoad();
});