jellyfish-web/src/components/accessSchedule/accessSchedule.js

94 lines
2.8 KiB
JavaScript
Raw Normal View History

2020-06-04 20:32:39 +03:00
/**
* Module for controlling user parental control from.
* @module components/accessSchedule/accessSchedule
*/
2020-08-14 08:46:34 +02:00
import dialogHelper from '../dialogHelper/dialogHelper';
import datetime from '../../scripts/datetime';
import globalize from '../../scripts/globalize';
import '../../elements/emby-select/emby-select';
import '../../elements/emby-button/paper-icon-button-light';
2021-01-26 16:25:38 -05:00
import '../formdialog.scss';
2020-11-25 00:17:24 -05:00
import template from './accessSchedule.template.html';
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function getDisplayTime(hours) {
let minutes = 0;
const pct = hours % 1;
2019-10-09 18:49:41 +03:00
2023-04-19 01:56:05 -04:00
if (pct) {
minutes = parseInt(60 * pct, 10);
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
return datetime.getDisplayTime(new Date(2000, 1, 1, hours, minutes, 0, 0));
}
2019-10-09 18:49:41 +03:00
2023-04-19 01:56:05 -04:00
function populateHours(context) {
let html = '';
2019-10-09 18:49:41 +03:00
2023-04-19 01:56:05 -04:00
for (let i = 0; i < 24; i += 0.5) {
html += `<option value="${i}">${getDisplayTime(i)}</option>`;
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
html += `<option value="24">${getDisplayTime(0)}</option>`;
context.querySelector('#selectStart').innerHTML = html;
context.querySelector('#selectEnd').innerHTML = html;
}
function loadSchedule(context, { DayOfWeek, StartHour, EndHour }) {
context.querySelector('#selectDay').value = DayOfWeek || 'Sunday';
context.querySelector('#selectStart').value = StartHour || 0;
context.querySelector('#selectEnd').value = EndHour || 0;
}
function submitSchedule(context, options) {
const updatedSchedule = {
DayOfWeek: context.querySelector('#selectDay').value,
StartHour: context.querySelector('#selectStart').value,
EndHour: context.querySelector('#selectEnd').value
};
if (parseFloat(updatedSchedule.StartHour) >= parseFloat(updatedSchedule.EndHour)) {
alert(globalize.translate('ErrorStartHourGreaterThanEnd'));
return;
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
context.submitted = true;
options.schedule = Object.assign(options.schedule, updatedSchedule);
dialogHelper.close(context);
}
2019-10-09 18:49:41 +03:00
2023-04-19 01:56:05 -04:00
export function show(options) {
return new Promise((resolve, reject) => {
const dlg = dialogHelper.createDialog({
removeOnClose: true,
size: 'small'
2020-06-04 20:32:39 +03:00
});
2023-04-19 01:56:05 -04:00
dlg.classList.add('formDialog');
let html = '';
html += globalize.translateHtml(template);
dlg.innerHTML = html;
populateHours(dlg);
loadSchedule(dlg, options.schedule);
dialogHelper.open(dlg);
dlg.addEventListener('close', () => {
if (dlg.submitted) {
resolve(options.schedule);
} else {
reject();
}
});
dlg.querySelector('.btnCancel').addEventListener('click', () => {
dialogHelper.close(dlg);
});
dlg.querySelector('form').addEventListener('submit', event => {
submitSchedule(dlg, options);
event.preventDefault();
return false;
});
});
}
2019-10-09 18:49:41 +03:00
2020-06-04 20:32:39 +03:00
export default {
show: show
};