bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/timer/service.js

231 lines
4.5 KiB
JavaScript
Raw Normal View History

2023-05-11 04:03:20 +08:00
import Timer from '/imports/api/timer';
import Auth from '/imports/ui/services/auth';
2020-04-26 03:03:35 +08:00
import { Session } from 'meteor/session';
import { ACTIONS, PANELS } from '../layout/enums';
2023-05-11 04:03:20 +08:00
const TIMER_CONFIG = window.meetingClientSettings.public.timer;
const OFFSET_INTERVAL = TIMER_CONFIG.interval.offset;
2020-04-26 03:03:35 +08:00
const MILLI_IN_HOUR = 3600000;
const MILLI_IN_MINUTE = 60000;
const MILLI_IN_SECOND = 1000;
2023-05-11 04:03:20 +08:00
2020-04-27 01:44:01 +08:00
const MAX_HOURS = 23;
const TRACKS = [
'noTrack',
'track1',
'track2',
'track3',
];
const isMusicEnabled = () => TIMER_CONFIG.music.enabled;
const getCurrentTrack = () => {
const timer = Timer.findOne(
{ meetingId: Auth.meetingID },
{ fields: { track: 1 } },
);
if (timer) return isMusicEnabled() && timer.track;
return false;
};
const isEnabled = () => TIMER_CONFIG.enabled;
2020-04-27 01:44:01 +08:00
const getMaxHours = () => MAX_HOURS;
2020-06-13 02:00:24 +08:00
const isAlarmEnabled = () => isEnabled() && TIMER_CONFIG.alarm;
2020-04-27 04:18:43 +08:00
const isMusicActive = () => getCurrentTrack() !== TRACKS[0];
const getMusicVolume = () => TIMER_CONFIG.music.volume;
const getMusicTrack = () => TIMER_CONFIG.music.track;
2020-06-13 02:00:24 +08:00
const isActive = () => {
2023-05-11 04:03:20 +08:00
const timer = Timer.findOne(
{ meetingId: Auth.meetingID },
{ fields: { active: 1 } },
);
if (timer) return timer.active;
return false;
};
2020-04-26 03:03:35 +08:00
const getDefaultTime = () => TIMER_CONFIG.time * MILLI_IN_MINUTE;
const getInterval = () => TIMER_CONFIG.interval.clock;
2020-04-26 03:03:35 +08:00
2023-05-11 04:03:20 +08:00
const isRunning = () => {
const timer = Timer.findOne(
{ meetingId: Auth.meetingID },
{ fields: { running: 1 } },
);
if (timer) return timer.running;
return false;
};
const isStopwatch = () => {
const timer = Timer.findOne(
{ meetingId: Auth.meetingID },
{ fields: { stopwatch: 1 } },
);
if (timer) return timer.stopwatch;
return false;
2020-04-26 03:03:35 +08:00
};
2023-05-11 04:03:20 +08:00
2020-06-13 02:50:42 +08:00
const getTimeOffset = () => {
const timeOffset = Session.get('timeOffset');
if (timeOffset) return timeOffset;
return 0;
};
const getElapsedTime = (running, timestamp, timeOffset, accumulated) => {
if (!running) return accumulated;
const now = Date.now();
2020-06-13 02:50:42 +08:00
return accumulated + Math.abs(now - timestamp + timeOffset);
};
2023-05-11 04:03:20 +08:00
2023-05-16 03:46:44 +08:00
const getStopwatch = () => {
const timer = Timer.findOne(
{ meetingId: Auth.meetingID },
{ fields: { stopwatch: 1 } },
);
if (timer) return timer.stopwatch;
return true;
};
2023-05-11 04:03:20 +08:00
const getTimer = () => {
const timer = Timer.findOne(
{ meetingId: Auth.meetingID },
{
fields:
2023-05-11 04:03:20 +08:00
{
stopwatch: 1,
running: 1,
time: 1,
accumulated: 1,
timestamp: 1,
},
},
);
if (timer) {
const {
stopwatch,
running,
time,
accumulated,
timestamp,
} = timer;
return {
stopwatch,
running,
time,
accumulated,
timestamp,
};
2023-05-11 04:03:20 +08:00
}
return {
stopwatch: true,
running: false,
2020-04-26 03:03:35 +08:00
time: getDefaultTime(),
2023-05-11 04:03:20 +08:00
accumulated: 0,
timestamp: 0,
};
2020-04-26 03:03:35 +08:00
};
2023-05-11 04:03:20 +08:00
const getTimeAsString = (time) => {
const milliseconds = time;
2023-05-11 04:03:20 +08:00
const hours = Math.floor(milliseconds / MILLI_IN_HOUR);
2020-04-26 03:03:35 +08:00
const mHours = hours * MILLI_IN_HOUR;
const minutes = Math.floor((milliseconds - mHours) / MILLI_IN_MINUTE);
2020-04-26 03:03:35 +08:00
const mMinutes = minutes * MILLI_IN_MINUTE;
const seconds = Math.floor((milliseconds - mHours - mMinutes) / MILLI_IN_SECOND);
2020-04-26 03:03:35 +08:00
2020-04-27 01:44:01 +08:00
let timeAsString = '';
2020-04-26 03:03:35 +08:00
2023-05-16 03:46:44 +08:00
if (hours < 10) {
timeAsString += `0${hours}:`;
} else {
timeAsString += `${hours}:`;
2020-04-26 03:03:35 +08:00
}
2023-05-16 03:46:44 +08:00
if (minutes < 10) {
timeAsString += `0${minutes}:`;
} else {
timeAsString += `${minutes}:`;
2020-04-26 03:03:35 +08:00
}
if (seconds < 10) {
2020-04-27 01:44:01 +08:00
timeAsString += `0${seconds}`;
2020-04-26 03:03:35 +08:00
} else {
2020-04-27 01:44:01 +08:00
timeAsString += `${seconds}`;
2020-04-26 03:03:35 +08:00
}
2020-04-27 01:44:01 +08:00
return timeAsString;
2020-04-26 03:03:35 +08:00
};
2020-04-27 01:44:01 +08:00
const closePanel = (layoutContextDispatch) => {
layoutContextDispatch({
type: ACTIONS.SET_SIDEBAR_CONTENT_IS_OPEN,
value: false,
});
layoutContextDispatch({
type: ACTIONS.SET_SIDEBAR_CONTENT_PANEL,
value: PANELS.NONE,
});
};
2020-04-26 03:03:35 +08:00
const togglePanel = (sidebarContentPanel, layoutContextDispatch) => {
layoutContextDispatch({
type: ACTIONS.SET_SIDEBAR_CONTENT_IS_OPEN,
value: sidebarContentPanel !== PANELS.TIMER,
});
layoutContextDispatch({
type: ACTIONS.SET_SIDEBAR_CONTENT_PANEL,
value: sidebarContentPanel === PANELS.TIMER
? PANELS.NONE
: PANELS.TIMER,
});
};
2023-05-11 04:03:20 +08:00
export default {
OFFSET_INTERVAL,
TRACKS,
2023-05-11 04:03:20 +08:00
isActive,
isEnabled,
isMusicEnabled,
isMusicActive,
getCurrentTrack,
getMusicVolume,
getMusicTrack,
2023-05-11 04:03:20 +08:00
isRunning,
isStopwatch,
2020-04-27 04:18:43 +08:00
isAlarmEnabled,
2020-06-13 02:50:42 +08:00
getTimeOffset,
getElapsedTime,
2023-05-11 04:03:20 +08:00
getInterval,
2020-04-27 01:44:01 +08:00
getMaxHours,
2023-05-16 03:46:44 +08:00
getStopwatch,
2023-05-11 04:03:20 +08:00
getTimer,
getTimeAsString,
2020-04-27 01:44:01 +08:00
closePanel,
2020-04-26 03:03:35 +08:00
togglePanel,
2023-05-11 04:03:20 +08:00
};