-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPomodoro.js
193 lines (179 loc) · 7.66 KB
/
Pomodoro.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
function Pomodoro(options) {
this._INACTIVE = 0;
this._ACTIVITY = 1;
this._BREAK = 2;
var notifications = require("sdk/notifications");
var tmr = require('sdk/timers');
var events = require("sdk/system/events");
var pageWorker = require("sdk/page-worker");
var data = require("sdk/self").data;
this._LABEL_ADDON_NAME = "PomodoroFox";
this._LABEL_START_SESSION = "Start pomodoro session";
this._LABEL_TIME_REMAINING_ACTIVITY = " minutes of activity remaining. Click to end session.";
this._LABEL_TIME_REMAINING_BREAK = " minutes of break remaining. Click to end session.";
this._NOTIFICATION_TIMEOUT_ACTIVITY = "Time for a break";
this._NOTIFICATION_TIMEOUT_BREAK = "Time to go back to work";
this._NOTIFICATION_SESSION_STARTED = "Session started";
this._NOTIFICATION_SESSION_STOPPED = "Session stopped";
this._NOTIFICATION_SESSION_FINISHED = "Session finished";
this._NOTIFICATION_SESSION_CANNOT_STOP = "Strict mode is enabled. You can't stop periods manually!";
this._ERROR_BAD_FORMAT_DOMAIN_LIST = "Cannot read blocked domain list. Please check the configuration.";
this.settings = {
numCyclesPerSession: options.numCyclesPerSession !== null ? options.numCyclesPerSession : 5,
timeActivityPeriod: options.timeActivityPeriod !== null ? options.timeActivityPeriod : 25,
timeBreakPeriod: options.timeBreakPeriod !== null ? options.timeBreakPeriod : 5,
strictMode: options.strictMode !== null ? options.strictMode : false,
notificationIconURLActive: options.notificationIconURLActive !== null ? options.notificationIconURLActive : null,
notificationIconURLBreak: options.notificationIconURLBreak !== null ? options.notificationIconURLBreak : null
};
this.sessionActive = true;
this.currentPeriod = this._INACTIVE;
this.timerIsOn = false;
this.timerInstance = null;
this.interval = null;
this.activityPeriodsCounter = 0;
this.breakPeriodsCounter = 0;
this.cycleCounter = 0;
this.blockerObject = null;
this.onTimeOut = function(caller, blockerObj){
switch (this.currentPeriod) {
case this._ACTIVITY:
this.toast(this._NOTIFICATION_TIMEOUT_ACTIVITY);
break;
case this._BREAK:
this.toast(this._NOTIFICATION_TIMEOUT_BREAK);
break;
}
this.stopTimer(caller, blockerObj);
this.play();
};
this.startTimer = function(caller, time, periodType) {
this.currentPeriod = periodType;
this.timerIsOn = true;
var instance = this;
this.settings.timeActivityPeriod = time;
var accumulated = 1;
var currentLabel = '';
switch (periodType) {
case this._ACTIVITY:
currentLabel = this._LABEL_TIME_REMAINING_ACTIVITY;
break;
case this._BREAK:
currentLabel = this._LABEL_TIME_REMAINING_BREAK;
break;
}
caller.label = time + currentLabel;
//instance.toggleIcon(caller, instance.currentPeriod, (time - accumulated));
this.interval = tmr.setInterval(function(){
console.log("peich");
caller.tooltiptext = instance._LABEL_ADDON_NAME + ': ' +(time - accumulated) + currentLabel;
console.log(instance.currentPeriod);
instance.toggleIcon(caller, instance.currentPeriod, (time - accumulated));
accumulated++;
}, 60 * 1000);
this.timerInstance = tmr.setTimeout(function() {
instance.onTimeOut(caller, instance.blockerObject);
}, parseInt(this.settings.timeActivityPeriod) * 60 * 1000);
};
this.stopTimer = function(caller, blockerObj) {
console.log("stoptimer");
if (this.cycleCounter < this.settings.numCyclesPerSession) {
if (this.currentPeriod == this._ACTIVITY) {
console.log("finished activity " + this.activityPeriodsCounter);
this.activityPeriodsCounter++;
//start break
console.log("starting break " + this.breakPeriodsCounter);
this.startTimer(caller, this.settings.timeBreakPeriod, this._BREAK, blockerObj);
} else {
console.log("finished break " + this.breakPeriodsCounter);
this.breakPeriodsCounter++;
console.log("finished cycle " + this.cycleCounter);
this.cycleCounter++;
console.log("starting cycle " + this.cycleCounter);
//start activity
console.log("starting activity " + this.activityPeriodsCounter);
this.startTimer(caller, this.settings.timeActivityPeriod, this._ACTIVITY, blockerObj);
}
} else {
console.log("else. going to stop timer");
tmr.clearTimeout(this.timerInstance);
tmr.clearInterval(this.interval);
this.timerIsOn = false;
this.sessionActive = false;
blockerObj.destroy();
this.breakPeriodsCounter = 0;
this.activityPeriodsCounter = 0;
this.breakPeriodsCounter = 0;
this.cycleCounter = 0;
this.toast(this._NOTIFICATION_SESSION_FINISHED);
console.log("finished session");
this.toggleIcon(caller, this._INACTIVE, 0);
}
};
this.toggleIcon = function(caller, status, minutes)
{
switch (status) {
case this._ACTIVITY:
caller.badge = minutes;
caller.type = "activity";
caller.tooltiptext = this._LABEL_ADDON_NAME + ': ' + minutes + this._LABEL_TIME_REMAINING_ACTIVITY;
break;
case this._BREAK:
caller.badge = minutes;
caller.type = "break";
caller.tooltiptext = this._LABEL_ADDON_NAME + ': ' + minutes + this._LABEL_TIME_REMAINING_BREAK;
break;
case this._INACTIVE:
caller.badge = null;
caller.type = "inactive";
caller.tooltiptext = this._LABEL_START_SESSION;
}
console.log(caller.type);
};
this.toast = function(message)
{
notifications.notify({
title: this._LABEL_ADDON_NAME,
text: message,
iconURL: this.settings.notificationIconURLActive
});
};
this.play = function()
{
if (!require('sdk/simple-prefs').prefs['timeoutSound']) return;
var path = "alert.wav";
var worker = pageWorker.Page({
contentScript: "var audio = new Audio('" + path + "'); audio.play();",
contentURL: data.url("sound.html"),
onMessage: function(arr) {
worker.destroy();
}
});
};
this.startSession = function(caller, time, blockerObject)
{
this.blockerObject = blockerObject;
this.sessionActive = true;
this.breakPeriodsCounter = 0;
this.activityPeriodsCounter = 0;
this.breakPeriodsCounter = 0;
this.cycleCounter = 0;
console.log("starting session");
console.log("starting cycle " + this.cycleCounter);
//start activity
console.log("starting activity " + this.activityPeriodsCounter);
this.startTimer(caller, time, this._ACTIVITY);
};
this.stopSession = function(caller)
{
this.breakPeriodsCounter = 0;
this.activityPeriodsCounter = 0;
this.breakPeriodsCounter = 0;
this.cycleCounter = 0;
this.stopTimer(caller);
//this.blockerObject.destroy();
this.sessionActive = false;
this.toast(this._NOTIFICATION_SESSION_FINISHED);
};
}
exports.Pomodoro = Pomodoro;