This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
177 lines (148 loc) · 4.31 KB
/
main.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
'use babel';
import io from 'socket.io-client';
import settings from './settings.js';
import ServerProcess from './serverProcess.js';
import {secondsToTime, getRandomArrayItem} from './helpers.js';
import {statusTile, topPanelTemplate} from './templates.js';
let breakServerProcess = new ServerProcess(7455);
let socket = io(breakServerProcess.getUrl(), { autoConnect : false });
let breakPackage = {
config: settings.config,
activate(state) {
// start break server if not running
breakServerProcess.checkRunningState().then(
// server running
(response) => {
switch(response.statusCode){
case 200:
case 201:
case 202:
socket.open();
break;
default:
atom.notifications.addWarning('There was a problem connecting to Break;');
break;
}
},
// server not running
(error) =>{
breakServerProcess.startServer().on('message', function() {
socket.open();
});
}
);
// observe config changes
atom.config.onDidChange('break', (event) => {
socket.emit('reset-config-data', {
config: atom.config.get('break')
});
});
// add the break panel
this.topPanel = atom.workspace.addTopPanel({
item: topPanelTemplate,
visible: false
});
// set panel size and resize event
this._resizePanel();
window.onresize = this._resizePanel;
},
/**
* Sets the break status bar tile.
*/
consumeStatusBar(statusBar) {
this.statusBarTile = statusBar.addRightTile({
item: statusTile,
priority: 0
});
},
/**
* Calls the disconnected event when
* a window is closed.
*/
deactivate(){
socket.emit('client-disconnected', {});
},
serialize(){
return {};
},
/**
* Sets the top panels height. emitted
* once the atom window is resized.
*/
_resizePanel(){
let panel = document.getElementById('break-cover-window');
panel.style.height = window.innerHeight + 'px';
}
};
export default breakPackage;
/**
* Calls all the server side events needed
* on client connection attempt.
*/
socket.on('connect', function(){
// constructor event
socket.emit('client-connected', {
config: atom.config.get('break')
});
});
/**
* Event that controls multiple changes
* of the top panel element.
*
* @param {Object} data {type} must be set.
*/
socket.on('set-status-bar-tile', function(data){
let tileElement = document.getElementById('break-interval-timer');
let time = 0;
switch(data.type){
case 'Macro':
time = Math.round(data.interval - data.seconds);
tileElement.innerHTML = secondsToTime(time);
break;
case 'Micro':
time = Math.round(data.interval - data.seconds);
tileElement.innerHTML = data.amount + ' | ' + secondsToTime(time);
break;
case 'MacroNull':
tileElement.innerHTML = '00:00:00';
break;
case 'MicroNull':
tileElement.innerHTML = data.amount + ' | 00:00:00';
break;
case 'Null':
default:
tileElement.innerHTML = '00:00:00';
break;
}
});
/**
* Event that controls multiple changes
* of the top panel element.
*
* @param {Object} data {type} must be set
*/
socket.on('set-break-panel', function(data){
let processElement = document.getElementById('break-progress');
let quoteElement = document.getElementById('break-quote');
switch(data.type){
case 'Process':
processElement.value = Math.round((data.secondCounter / data.duration) * 100);
break;
case 'Show':
breakPackage.topPanel.show();
break;
case 'Hide':
breakPackage.topPanel.hide();
processElement.value = 0;
break;
case 'Quote':
quoteElement.innerHTML = getRandomArrayItem(settings.quotes);
break;
case 'Null':
default:
breakPackage.topPanel.hide();
processElement.value = 0;
break;
}
});
socket.on('debug', function(data){ console.log(data); });