-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeekPlan.ts
220 lines (191 loc) · 6.15 KB
/
WeekPlan.ts
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
/// <reference path=".\typings\chrome.d.ts" />
/// <reference path=".\typings\globals\jquery\index.d.ts" />
import {EventManager} from './EventManager';
class WeekPlan {
private static _instance: WeekPlan = undefined;
static port_name: string = "popup_port";
private _current_date: Date;
private _elements: {[name: string]: HTMLElement}
private constructor() { }
static getInstance(): WeekPlan {
if (!WeekPlan._instance) {
WeekPlan._instance = new WeekPlan();
}
return WeekPlan._instance;
}
init(): void {
this._elements = this._initHTMLElement();
this._initAppState();
}
private _initHTMLElement(): {[name: string]: HTMLElement} {
return {
"prev_date_btn": document.getElementById('prev'),
"next_date_btn": document.getElementById('next'),
"date_field": document.getElementById('date'),
"content": document.getElementById('content'),
"app": document.getElementById('app')
}
}
private _initAppState(): void {
this._current_date = new Date(); // TODO: This should be read from db
this._elements["date_field"].innerHTML = this._current_date.toDateString();
this._initEventListener();
}
private _initShortcut(): void {
chrome.commands.onCommand.addListener((command: string) => {
if (command === "add-plan-today") {
chrome.tabs.executeScript({
code: "window.getSelection().toString();"
}, (result: any[]) => {
let key = new Date().toDateString();
chrome.storage.sync.get(key, (items: {[key: string] : string}) => {
if (result[0] !== "") {
let selection = result[0];
let content: string = "";
if (typeof items[key] === 'undefined') {
content = selection;
} else {
content = items[key] + "\n" + selection;
}
this.save(key, content);
}
});
});
}
});
}
private _initEventListener(): void {
let onDateChanged = (event: Event) => {
if (event.target === this._elements["next_date_btn"]) {
this._forwardDate();
} else {
this._backwardDate();
}
this.load(new Date(this._elements["date_field"].innerHTML).toDateString(), this._show.bind(this));
};
let onKeyDown = (event: KeyboardEvent) => {
if (event.altKey) {
if (event.which === 37) {
this._backwardDate();
} else if (event.which === 39) {
this._forwardDate();
}
}
};
let onBlur = (event: Event) => {
this.save();
};
EventManager.register(this._elements["prev_date_btn"], "click", onDateChanged);
EventManager.register(this._elements["next_date_btn"], "click", onDateChanged);
EventManager.register(this._elements["app"], "keydown", onKeyDown);
EventManager.register(this._elements["content"], "blur", onBlur);
}
private _forwardDate(): void {
this._current_date.setDate(this._current_date.getDate() + 1);
this._elements["date_field"].innerHTML = this._current_date.toDateString();
this._updateDateChangeButtonState();
}
private _backwardDate(): void {
this._current_date.setDate(this._current_date.getDate() - 1);
this._elements["date_field"].innerHTML = this._current_date.toDateString();
this._updateDateChangeButtonState();
}
private _updateDateChangeButtonState() {
let current_day_str: string = this._current_date.toDateString();
let today: Date = new Date();
let six_days_later: Date = new Date();
six_days_later.setDate(today.getDate() + 6);
let today_str: string = today.toDateString();
let six_days_later_str: string = six_days_later.toDateString();
if (current_day_str === today_str) {
this._elements["prev_date_btn"].disabled = true;
} else if (current_day_str === six_days_later_str) {
this._elements["next_date_btn"].disabled = true;
} else {
this._elements["prev_date_btn"].disabled = false;
this._elements["next_date_btn"].disabled = false;
}
}
run(): void {
// Method returns a Port object, which will be disconnected when popup unloads
chrome.runtime.connect(<chrome.runtime.ConnectInfo> {
name: WeekPlan.port_name
});
this.load(new Date().toDateString(), this._show.bind(this));
this._updateDateChangeButtonState();
this.updateIcon();
}
removeOutdatePlan(): void {
let today: number = new Date().getTime();
chrome.storage.sync.get(null, (items: {[key: string]: string}) => {
for (let date_str in items) {
let day: number = new Date(date_str).getTime();
let time_diff: number = day - today;
if (time_diff < 0) {
delete items[date_str];
// TODO: Double check if key is really removed
}
}
});
}
updateIcon(): void {
let today_str: string = new Date().toDateString();
chrome.storage.sync.get(today_str, (items: {[key: string] : string}) => {
if (typeof items[today_str] === 'undefined' || items[today_str] === "") {
chrome.browserAction.setBadgeText({
text: ""
});
} else {
chrome.browserAction.setBadgeBackgroundColor({
color: "#F00"
});
chrome.browserAction.setBadgeText({
text: "!"
});
}
});
}
private _onMenuItemClicked(info: chrome.contextMenus.OnClickData, tab: chrome.tabs.Tab) {
let day_str: string = info.menuItemId;
chrome.storage.sync.get(day_str, (items: {[key: string] : string}) => {
if (!chrome.runtime.lastError) {
let content = items[day_str];
if (!content) {
content = "";
}
let add_content = "";
if (info.selectionText) {
add_content = "\n" + info.selectionText;
} else if (info.linkUrl) {
add_content = "\n" + info.linkUrl;
}
this.save(day_str, content + add_content);
}
});
}
load(date_str: string, callback: (content: string) => void): void {
chrome.storage.sync.get(date_str, (items: {[key: string] : string}) => {
if (!chrome.runtime.lastError) {
let daily_plan = items[date_str];
callback(daily_plan);
}
});
}
private _show(content: string): void {
let content_area = $(this._elements["content"]);
content_area.val(content);
content_area.focus();
}
save(date: string = this._current_date.toDateString(),
content: string = $(this._elements["content"]).val()): void {
let entry = {};
entry[date] = content;
chrome.storage.sync.set(entry, () => {
if (chrome.runtime.lastError) {
console.log("Runtime error when saving content");
}
this.updateIcon();
});
}
}
export {WeekPlan};