-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpopup.js
130 lines (111 loc) · 3.59 KB
/
popup.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
var background = chrome.extension.getBackgroundPage();
var Weave = background.Weave;
var days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
function onLoad () {
jQuery('#connect-button').click(onItemClick(onConnectButton));
jQuery('#sync-button').click(onItemClick(onSyncButton));
jQuery('#options').click(openPage('options.html'));
jQuery('#tabs').click(openPage('tabs.html'));
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;
}
updateSyncTime();
}
// Open a page within this extension
function openPage (page) {
page = chrome.extension.getURL(page);
return function () {
window.close();
chrome.tabs.create({url: page});
};
}
// Handler that swallows click events if the menu item is disabled
function onItemClick (handler) {
return function () {
if (this.getAttribute("disabled") === "disabled") {
return;
}
handler();
}
}
// 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 onSyncButton () {
if (Weave.Chromium.status !== "connected") {
return;
}
Weave.Chromium.sync();
}
// Helper functions
function updateSyncTime () {
if (localStorage["lastSync"] !== null) {
date = new Date(localStorage["lastSync"]);
jQuery('time').text(this.days[date.getDay()] + " " + date.getHours() + ":" + date.getMinutes());
} else {
jQuery('time').text("never");
}
}
// 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 now');
jQuery('#sync-button').removeAttr('disabled');
jQuery('#connect-button').removeAttr('disabled');
updateSyncTime();
}
document.addEventListener('DOMContentLoaded', function () {
onLoad();
});