2022-09-26 08:54:05 +02:00
|
|
|
/**
|
|
|
|
* EGroupware clientside API object
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link https://www.egroupware.org
|
|
|
|
* @author Ralf Becker <RalfBecker@outdoor-training.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
import './egw_core.js';
|
|
|
|
import {sprintf} from "../egw_action/egw_action_common";
|
|
|
|
|
|
|
|
egw.extend('timer', egw.MODULE_GLOBAL, function()
|
|
|
|
{
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Timer state
|
|
|
|
*/
|
|
|
|
let timer_start;
|
|
|
|
let timer_offset = 0;
|
|
|
|
let timer_paused = false;
|
|
|
|
|
|
|
|
const timer = document.querySelector('#topmenu_timer');
|
|
|
|
let timer_interval;
|
|
|
|
|
|
|
|
function setState(_state)
|
|
|
|
{
|
|
|
|
timer_start = _state.start ? new Date(_state.start) : undefined;
|
|
|
|
timer_offset = _state.offset || 0;
|
2022-09-29 18:04:57 +02:00
|
|
|
timer_paused = _state.paused || false
|
|
|
|
if (timer_paused)
|
2022-09-26 08:54:05 +02:00
|
|
|
{
|
2022-09-29 18:04:57 +02:00
|
|
|
startTimer(); // to show offset / paused time
|
|
|
|
stopTimer(true);
|
2022-09-26 08:54:05 +02:00
|
|
|
}
|
2022-09-29 18:04:57 +02:00
|
|
|
else if (timer_start)
|
2022-09-26 08:54:05 +02:00
|
|
|
{
|
2022-09-29 18:04:57 +02:00
|
|
|
startTimer(_state.start);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
startTimer(); // to show offset / stopped time
|
|
|
|
stopTimer();
|
2022-09-26 08:54:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getState()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
start: timer_start,
|
|
|
|
offset: timer_offset,
|
|
|
|
paused: timer_paused
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run timer action eg. start/stop
|
|
|
|
*
|
|
|
|
* @param {string} _action
|
|
|
|
*/
|
|
|
|
function timerAction(_action)
|
|
|
|
{
|
|
|
|
switch(_action)
|
|
|
|
{
|
|
|
|
case 'overall-start':
|
|
|
|
startTimer();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'overall-pause':
|
|
|
|
stopTimer(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'overall-stop':
|
|
|
|
stopTimer();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// persist state
|
|
|
|
let state = getState();
|
|
|
|
state.action = _action;
|
|
|
|
egw.request('timesheet.timesheet_bo.ajax_event', [state])
|
|
|
|
}
|
|
|
|
|
2022-09-29 18:04:57 +02:00
|
|
|
/**
|
|
|
|
* Enable/disable menu items based on timer state
|
|
|
|
*/
|
|
|
|
function setMenuState()
|
2022-09-26 08:54:05 +02:00
|
|
|
{
|
2022-09-29 18:04:57 +02:00
|
|
|
const menu = document.querySelector('et2-select#timer_selectbox').menu;
|
|
|
|
// disable not matching / available menu-items
|
|
|
|
menu.getAllItems('et2-selectbox#timer_selecbox sl-menu-item').forEach(item =>
|
2022-09-26 08:54:05 +02:00
|
|
|
{
|
2022-09-29 18:04:57 +02:00
|
|
|
// timer running: disable only start, enable pause and stop
|
|
|
|
if (timer_start)
|
2022-09-26 08:54:05 +02:00
|
|
|
{
|
2022-09-29 18:04:57 +02:00
|
|
|
item.disabled = item.value === 'overall-start';
|
2022-09-26 08:54:05 +02:00
|
|
|
}
|
2022-09-29 18:04:57 +02:00
|
|
|
// timer paused: disable pause, enable start and stop
|
2022-09-26 08:54:05 +02:00
|
|
|
else if (timer_paused)
|
|
|
|
{
|
2022-09-29 18:04:57 +02:00
|
|
|
item.disabled = item.value === 'overall-pause';
|
2022-09-26 08:54:05 +02:00
|
|
|
}
|
2022-09-29 18:04:57 +02:00
|
|
|
// timer stopped: disable stop and pause, enable start
|
2022-09-26 08:54:05 +02:00
|
|
|
else
|
|
|
|
{
|
2022-09-29 18:04:57 +02:00
|
|
|
item.disabled = item.value !== 'overall-start';
|
2022-09-26 08:54:05 +02:00
|
|
|
}
|
2022-09-29 18:04:57 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function startTimer(_time)
|
|
|
|
{
|
|
|
|
if (_time)
|
|
|
|
{
|
|
|
|
timer_start = new Date(_time);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
timer_start = new Date();
|
|
|
|
}
|
|
|
|
if (timer_offset > 0)
|
|
|
|
{
|
|
|
|
timer_start.setMilliseconds(timer_start.getMilliseconds()-timer_offset);
|
|
|
|
}
|
|
|
|
timer_offset = 0; // it's now set in start-time
|
|
|
|
timer_paused = false;
|
|
|
|
const update = () =>
|
|
|
|
{
|
|
|
|
let diff = Math.round(((new Date()).valueOf() - timer_start.valueOf())/1000.0);
|
|
|
|
const sep = diff % 2 ? ' ' : ':';
|
|
|
|
diff = Math.round(diff / 60.0);
|
|
|
|
timer.textContent = sprintf('%d%s%02d', Math.round(diff/60), sep, diff % 60);
|
2022-09-26 08:54:05 +02:00
|
|
|
}
|
|
|
|
timer.classList.add('running');
|
|
|
|
timer.classList.remove('paused');
|
|
|
|
update();
|
|
|
|
timer_interval = window.setInterval(update, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
function stopTimer(_pause)
|
|
|
|
{
|
|
|
|
if (timer_interval)
|
|
|
|
{
|
|
|
|
window.clearInterval(timer_interval);
|
|
|
|
}
|
|
|
|
timer.classList.remove('running');
|
|
|
|
timer.textContent = timer.textContent.replace(' ', ':');
|
|
|
|
|
|
|
|
if (_pause)
|
|
|
|
{
|
|
|
|
timer.classList.add('paused');
|
|
|
|
timer_paused = true;
|
|
|
|
}
|
2022-09-29 18:04:57 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
timer.classList.remove('paused');
|
|
|
|
timer_paused = false;
|
|
|
|
}
|
|
|
|
if (timer_start)
|
2022-09-26 08:54:05 +02:00
|
|
|
{
|
2022-09-29 18:04:57 +02:00
|
|
|
timer_offset = (new Date()).valueOf() - timer_start.valueOf();
|
2022-09-26 08:54:05 +02:00
|
|
|
timer_start = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
/**
|
|
|
|
* Create timer in top-menu
|
|
|
|
*
|
|
|
|
* @param {string} _parent parent to create selectbox in
|
|
|
|
*/
|
|
|
|
add_timer: function(_parent)
|
|
|
|
{
|
|
|
|
const timer_container = document.getElementById(_parent);
|
|
|
|
if (!timer_container) return;
|
|
|
|
|
|
|
|
// set state if given
|
|
|
|
const timer = document.getElementById('topmenu_timer');
|
|
|
|
if (timer && timer.getAttribute('data-state'))
|
|
|
|
{
|
|
|
|
setState(JSON.parse(timer.getAttribute('data-state')));
|
|
|
|
}
|
|
|
|
|
|
|
|
// create selectbox / menu
|
2022-09-29 18:04:57 +02:00
|
|
|
const select = document.createElement('et2-select');
|
|
|
|
select.id = 'timer_selectbox';
|
2022-09-26 08:54:05 +02:00
|
|
|
timer_container.append(select);
|
|
|
|
|
|
|
|
// bind change handler
|
|
|
|
select.addEventListener('change', () =>
|
|
|
|
{
|
|
|
|
if (select.value) timerAction(select.value);
|
|
|
|
select.value = '';
|
|
|
|
});
|
2022-09-26 13:36:10 +02:00
|
|
|
|
|
|
|
select.addEventListener('sl-hide', (e) => {
|
|
|
|
if (e.currentTarget.nodeName === 'ET2-SELECT')
|
|
|
|
{
|
|
|
|
e.stopImmediatePropagation();
|
|
|
|
}
|
|
|
|
});
|
2022-09-26 08:54:05 +02:00
|
|
|
// bind click handler
|
|
|
|
timer_container.addEventListener('click', (ev) =>
|
|
|
|
{
|
2022-09-26 13:36:10 +02:00
|
|
|
ev.stopImmediatePropagation();
|
|
|
|
if (select.dropdown.open)
|
|
|
|
{
|
|
|
|
select.dropdown.hide();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-09-29 18:04:57 +02:00
|
|
|
setMenuState();
|
2022-09-26 13:36:10 +02:00
|
|
|
select.dropdown.show();
|
|
|
|
}
|
2022-09-26 08:54:05 +02:00
|
|
|
});
|
|
|
|
// need to load timesheet translations for app-names
|
|
|
|
this.langRequire(window, [{app: 'timesheet', lang: this.preference('lang')}], () =>
|
|
|
|
{
|
|
|
|
select.select_options = [
|
|
|
|
{ value:'', label: this.lang('Select one...')},
|
|
|
|
{ value: 'overall-start', label: this.lang('Start working time')},
|
|
|
|
{ value: 'overall-pause', label: this.lang('Pause working time')},
|
|
|
|
{ value: 'overall-stop', label: this.lang('Stop working time')},
|
|
|
|
];
|
|
|
|
select.updateComplete.then(() =>
|
|
|
|
{
|
|
|
|
select.dropdown.trigger.style.visibility = 'hidden';
|
|
|
|
select.dropdown.trigger.style.height = '0px';
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|