-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesklet.js
153 lines (133 loc) · 5.13 KB
/
desklet.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
const Desklet = imports.ui.desklet;
const St = imports.gi.St;
const Settings = imports.ui.settings;
const MainLoop = imports.mainloop;
const Lang = imports.lang;
const UUID = "deadlines@shadowthink";
const MILLISEC_IN_MINUTE = 60 * 1000;
const MILLISEC_IN_HOUR = MILLISEC_IN_MINUTE * 60;
const MILLISEC_IN_DAY = MILLISEC_IN_HOUR * 24;
const MILLISEC_IN_WEEK = MILLISEC_IN_DAY * 7;
const TEXT_SIZE = 20;
const TASK_LEFT_PAD = 20;
const WIN_BORDER = 10;
function DeadlinesDesklet(metadata, desklet_id) {
this._init(metadata, desklet_id);
}
DeadlinesDesklet.prototype = {
__proto__: Desklet.Desklet.prototype,
_init: function(metadata, desklet_id) {
this.deadlineLst = [];
try {
Desklet.Desklet.prototype._init.call(this, metadata, desklet_id);
this.settings = new Settings.DeskletSettings(this, UUID, desklet_id);
this.settings.bindProperty(Settings.BindingDirection.ONE_WAY, "listLength", "listLength", this.refresh, null);
this.settings.bindProperty(Settings.BindingDirection.ONE_WAY, "transparency", "transparency", this.refresh, null);
this.settings.bindProperty(Settings.BindingDirection.ONE_WAY, "zoom", "zoom", this.refresh, null);
this.settings.bindProperty(Settings.BindingDirection.ONE_WAY, "textcolor", "textcolor", this.refresh, null);
this.settings.bindProperty(Settings.BindingDirection.ONE_WAY, "bgcolor", "bgcolor", this.refresh, null);
this.settings.bindProperty(Settings.BindingDirection.ONE_WAY, "deadlines", "deadlines", this.refresh, null);
this.setHeader("Deadlines");
this.setupUI();
}
catch (e) {
global.logError(e);
}
},
setupUI: function() {
this._parseDeadlines();
this._createWindow();
this.display();
this.refreshLoop();
},
_parseDeadlines: function() {
var i = 0, lst = this.deadlines.split(';');
var now = Date.now();
this.deadlineLst = [];
for(; i < lst.length; i++) {
var item = lst[i].trim().split(',');
var timediff = this._getTimeDiff(now, item[0]);
this.deadlineLst.push([timediff[0], timediff[1], item[1]]);
}
this.deadlineLst.sort(function(a, b) {
return a[0] - b[0];
});
},
_getTimeDiff: function(now, future) {
if (typeof future == "string") future = new Date(future);
var diff = future - now;
if (diff <= 0) return [0, "0 weeks 0 days 00:00"];
var week = parseInt(diff / MILLISEC_IN_WEEK);
diff = diff - week * MILLISEC_IN_WEEK;
var day = parseInt(diff / MILLISEC_IN_DAY);
diff = diff - day * MILLISEC_IN_DAY;
var hour = parseInt(diff / MILLISEC_IN_HOUR);
diff = diff - hour * MILLISEC_IN_HOUR;
var minute = parseInt(diff / MILLISEC_IN_MINUTE);
var diffstr = week + " weeks " + day + " days " + this._zeroPadTimeDiff(hour, 2) + ":" + this._zeroPadTimeDiff(minute, 2);
return [future - now, diffstr];
},
_zeroPadTimeDiff: function(diff, size) {
var s = "00000000" + diff;
return s.substr(s.length - size);
},
_createWindow: function() {
this.window = new St.BoxLayout({"vertical": true});
this.container = new St.BoxLayout({"vertical": false, "x_align": 2, "y_align": 2});
this.countDown = new St.BoxLayout({"vertical": true, "y_align": 2});
this.task = new St.BoxLayout({"vertical": true, "y_align": 2});
var i;
this.count_down_lst = [];
this.task_lst = [];
for (i = 0; i < this.listLength; i++) {
var count_down = new St.Label();
var task_caption = new St.Label();
this.countDown.add_actor(count_down);
this.task.add_actor(task_caption);
this.count_down_lst.push(count_down);
this.task_lst.push(task_caption);
}
this.container.add_actor(this.countDown);
this.container.add_actor(this.task);
this.window.add_actor(this.container);
this.setContent(this.window);
},
display: function() {
var i;
for (i = 0; i < this.deadlineLst.length; i++) {
if (i >= this.listLength) break;
this.count_down_lst[i].text = this.deadlineLst[i][1];
this.count_down_lst[i].style = "font-size: " + TEXT_SIZE * this.zoom + "px";
this.task_lst[i].text = this.deadlineLst[i][2];
this.task_lst[i].style = "font-size: " + TEXT_SIZE * this.zoom + "px; padding-left: " + TASK_LEFT_PAD * this.zoom + "px";
}
this.window.set_style_class_name('desklet');
this.window.style = "border: " + WIN_BORDER + "px solid; background-color: " + (this.bgcolor.replace(")", ","+this.transparency+")")).replace("rgb", "rgba") + "; color: " + this.textcolor;
},
refresh: function() {
this.clear();
this._parseDeadlines();
this.display();
},
clear: function() {
var i;
for (i = 0; i < this.deadlineLst.length; i++) {
if (i >= this.listLength) break;
this.count_down_lst[i].text = "";
this.task_lst[i].text = "";
}
},
on_desklet_removed: function() {
MainLoop.source_remove(this.timeout);
},
refreshLoop: function() {
this.refresh();
var now = new Date();
this.timeout = MainLoop.timeout_add_seconds(
60 - parseInt(now % (60 * 1000) / 1000),
Lang.bind(this, this.refreshLoop));
},
};
function main(metadata, desklet_id) {
return new DeadlinesDesklet(metadata, desklet_id);
}