-
Notifications
You must be signed in to change notification settings - Fork 28
/
kimai.mobile-1.0.js
executable file
·352 lines (314 loc) · 8.21 KB
/
kimai.mobile-1.0.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/**
* Kimai - mobile web frontend
*
* @version 0.1
* @author Kevin Papst <[email protected]>
*/
/**
* global variable to store the currently running timer.
*/
lastTimer = null;
/**
* Running when page loads.
* Initializes the environment and attaches some action handler to buttons.
*/
$(function(){
var obj = $.mobile.path.parseUrl(location.href);
Kimai.setJsonApi(obj.domain + obj.directory + '../core/json.php');
$('#password').bind('change keydown keypress keyup', function(){
validateLoginButton();
});
$('#username').bind('change keydown keypress keyup', function(){
validateLoginButton();
});
// authentication logic
$('#btnLogin').button();
$('#btnLogin').bind('click', function() {
if (Kimai.authenticate($('#username').val(), $('#password').val())) {
$.mobile.showPageLoadingMsg();
$('#loginForm').hide();
$('.kimai-not-login').removeClass('kimai-not-login');
setProjects(Kimai.getProjects());
setTasks(Kimai.getTasks());
setActiveTask(Kimai.getRunningTask());
$.mobile.hidePageLoadingMsg();
} else {
$(this).parent().find('span.ui-icon').removeClass('ui-icon-check').addClass('ui-icon-alert');
}
});
// actions on start and stop tasks
$('#recorder').slider();
$('#recorder').bind('change', function() {
startStopRecord();
});
$('#tasks').selectmenu();
$('#tasks').bind('change', function() {
validateStartStopButton();
});
$('#projects').selectmenu();
$('#projects').bind('change', function() {
validateStartStopButton();
});
// trigger the state of the login and start/stop buttons
validateLoginButton();
});
function setWindowState(title, stateMsg)
{
document.title = title;
$('#duration').text(stateMsg);
}
/**
* This function cares about starting and stopping of an entry.
* Also updates the visual UI elements.
*/
function startStopRecord()
{
var task = Kimai.getRunningTask();
if (task != null) {
if (!Kimai.stop()) {
alert('Could not stop entry');
}
} else {
if (!Kimai.start($('#projects').val(),$('#tasks').val())) {
alert('Could not start entry');
}
}
setActiveTask(Kimai.getRunningTask());
}
/**
* Sets the active task and all UI components into their correct state.
*/
function setActiveTask(task)
{
if (task == null)
{
$('#projects').selectmenu('enable');
$('#tasks').selectmenu('enable');
$('#recorder').val('off');
window.clearTimeout(lastTimer);
setWindowState('Kimai Mobile: Easy mobile Time-Tracking v0.1', 'Please select the task you want to start');
}
else
{
$('#projects').val(task.zef_pctID);
$('#tasks').val(task.zef_evtID);
$('#projects').selectmenu('disable');
$('#tasks').selectmenu('disable');
$('#recorder').val('on');
updateTimer(task.zef_in);
}
$('#recorder').slider('refresh');
$('#projects').selectmenu('refresh');
$('#tasks').selectmenu('refresh');
}
function updateTimer(start)
{
var currently = new Date();
currently.setTime(currently.getTime() - (start*1000));
// @FIXME getUTCHours depends on server and client settings
var hours = currently.getUTCHours();
if (hours < 10) {
hours = "0" + hours;
}
var minutes = currently.getMinutes();
if (minutes < 10) {
minutes = "0" + minutes;
}
var seconds = currently.getSeconds();
if (seconds < 10) {
seconds = "0" + seconds;
}
var formTime = hours + ':' + minutes + ':' + seconds;
setWindowState(formTime, 'You are now working: ' + formTime);
lastTimer = window.setTimeout('updateTimer('+start+')', 1000);
}
function setProjects(projects)
{
if (projects == null) {
alert('Sorry, but the project list could not be loaded');
}
$.each(projects, function(index, theEntry) {
$('#projects').append(
$('<option></option>').val(theEntry.pct_ID).html(theEntry.pct_name + " ("+theEntry.knd_name+")")
);
});
$('#projects').selectmenu('refresh');
}
function setTasks(tasks)
{
if (tasks == null) {
alert('Sorry, but the task list could not be loaded');
}
$.each(tasks, function(index, theEntry) {
$('#tasks').append(
$('<option></option>').val(theEntry.evt_ID).html(theEntry.evt_name)
);
});
$('#tasks').selectmenu('refresh');
}
function validateStartStopButton()
{
var mySlider = $('#recorder');
var myProjects = $('#projects');
var myTasks = $('#tasks');
// switch the start/stop button
if (myTasks.val() != '' && myProjects.val() != '') {
mySlider.slider('enable');
mySlider.parent().find('span.ui-icon').removeClass('ui-icon-alert').addClass('ui-icon-check');
} else {
mySlider.slider('disable');
mySlider.parent().find('span.ui-icon').removeClass('ui-icon-check').addClass('ui-icon-alert');
}
mySlider.slider('refresh');
}
function validateLoginButton()
{
var myButton = $('#btnLogin');
if ($('#username').val() != '' && $('#password').val() != '') {
myButton.button('enable');
myButton.parent().find('span.ui-icon').removeClass('ui-icon-alert').addClass('ui-icon-check');
return true;
}
myButton.button('disable');
myButton.parent().find('span.ui-icon').removeClass('ui-icon-check').addClass('ui-icon-alert');
return false;
}
/**
* The global Kimai object gives access to the JSON API.
*/
var Kimai =
{
apiKey: '',
jsonApi: '',
/**
* Sets the URL where the JSON API is located.
*
* @param string url the url of the json api
*/
setJsonApi: function(url) {
this.jsonApi = url;
},
/**
* Creates a JSON API Call ID.
* Actually, the API does not care about it yet, we could also use
* a hardcoded string here, but its better nicer that way...
*
* @return string
*/
getApiCallID: function(apiCall) {
return apiCall; // @TODO improve me
},
/**
* Authenticates the user account and returns the API key on success.
* If user could not be authenticated null will be returned.
*
* @param string uid the username
* @param string pwd the password
* @return boolean
*/
authenticate: function(uid, pwd) {
if (this.jsonApi == '' || uid == '' || pwd == '') {
return false;
}
var key = null;
$.ajax({
type: 'POST',
url: this.jsonApi,
data: {method: 'authenticate', id: Kimai.getApiCallID('authenticate'), params: {username: uid, password: pwd}},
async: false,
success: function(data) {
if (typeof data != 'undefined' && typeof data.result != 'undefined' && data.result != '') {
key = data.result;
}
}
});
if (key !== null) {
this.apiKey = key;
return true;
}
return false;
},
/**
* Retrieve a list ob project objects.
*
* @return array
*/
getProjects: function() {
return this._doApiCall('getProjects');
},
/**
* Returns a list of all tasks for the current user.
*
* @return array
*/
getTasks: function() {
return this._doApiCall('getTasks');
},
/**
* Returns null if no record is processing or an array if one is running.
*
* @return null|array
*/
getRunningTask: function() {
var result = this._doApiCall('getActiveTask');
// only if a last entry is existing and its not stopped, return it
if (typeof result.zef_out != 'undefined' && result.zef_out == 0) {
return result;
}
return null;
},
/**
* Starts the given task within the project.
*
* @param integer prjId
* @param integer taskId
* @return boolean
*/
start: function(prjId, taskId) {
var result = null;
$.ajax({
type: 'POST',
url: this.jsonApi,
data: {method: 'startRecord', id: Kimai.getApiCallID('startRecord'), params: {apiKey: this.apiKey, projectId: prjId, eventId: taskId}},
async: false,
success: function(data) {
if (typeof data != 'undefined' && typeof data.result != 'undefined') {
result = data.result;
}
}
});
return result;
},
/**
* Stops the current running task.
*
* @return boolean
*/
stop: function() {
var stopped = this._doApiCall('stopRecord');
return stopped;
},
/**
* Calls the JSON Api method and returns the result.
* This method is only meant for calls with no parameters.
*
* @param string apimethod
* @access private
* @return mixed
*/
_doApiCall: function(apimethod) {
var result = null;
$.ajax({
type: 'POST',
url: this.jsonApi,
data: {method: apimethod, id: Kimai.getApiCallID(apimethod), params: {apiKey: this.apiKey}},
async: false,
success: function(data) {
if (typeof data != 'undefined' && typeof data.result != 'undefined') {
result = data.result;
}
}
});
return result;
}
};