1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/components/accessSchedule/accessSchedule.js

98 lines
3.2 KiB
JavaScript
Raw Normal View History

2020-06-04 20:32:39 +03:00
/* eslint-disable indent */
/**
* Module for controlling user parental control from.
* @module components/accessSchedule/accessSchedule
*/
import dialogHelper from 'dialogHelper';
import datetime from 'datetime';
import globalize from 'globalize';
import 'emby-select';
import 'paper-icon-button-light';
import 'formDialogStyle';
2018-10-23 01:05:09 +03:00
function getDisplayTime(hours) {
2020-06-04 20:32:39 +03:00
let minutes = 0;
const pct = hours % 1;
2019-10-09 18:49:41 +03:00
if (pct) {
minutes = parseInt(60 * pct);
}
2019-10-15 20:15:22 +03:00
return datetime.getDisplayTime(new Date(2000, 1, 1, hours, minutes, 0, 0));
2018-10-23 01:05:09 +03:00
}
function populateHours(context) {
2020-06-04 20:32:39 +03:00
let html = '';
2019-10-09 18:49:41 +03:00
2020-06-04 20:32:39 +03:00
for (let i = 0; i < 24; i++) {
html += `<option value="${i}">${getDisplayTime(i)}</option>`;
2019-10-09 18:49:41 +03:00
}
2020-06-04 20:32:39 +03:00
html += `<option value="24">${getDisplayTime(0)}</option>`;
2020-05-04 12:44:12 +02:00
context.querySelector('#selectStart').innerHTML = html;
context.querySelector('#selectEnd').innerHTML = html;
2018-10-23 01:05:09 +03:00
}
2020-06-04 20:32:39 +03:00
function loadSchedule(context, {DayOfWeek, StartHour, EndHour}) {
context.querySelector('#selectDay').value = DayOfWeek || 'Sunday';
context.querySelector('#selectStart').value = StartHour || 0;
context.querySelector('#selectEnd').value = EndHour || 0;
2018-10-23 01:05:09 +03:00
}
function submitSchedule(context, options) {
2020-06-04 20:32:39 +03:00
const updatedSchedule = {
2020-05-04 12:44:12 +02:00
DayOfWeek: context.querySelector('#selectDay').value,
StartHour: context.querySelector('#selectStart').value,
EndHour: context.querySelector('#selectEnd').value
2018-10-23 01:05:09 +03:00
};
2019-10-09 18:49:41 +03:00
if (parseFloat(updatedSchedule.StartHour) >= parseFloat(updatedSchedule.EndHour)) {
return void alert(globalize.translate('ErrorStartHourGreaterThanEnd'));
2019-10-09 18:49:41 +03:00
}
context.submitted = true;
options.schedule = Object.assign(options.schedule, updatedSchedule);
dialogHelper.close(context);
2018-10-23 01:05:09 +03:00
}
2019-10-09 18:49:41 +03:00
2020-06-04 20:32:39 +03:00
export function show(options) {
return new Promise((resolve, reject) => {
2020-07-25 13:42:03 +02:00
import('text!./accessSchedule.template.html').then(({default: template}) => {
2020-06-04 20:32:39 +03:00
const dlg = dialogHelper.createDialog({
removeOnClose: true,
size: 'small'
});
dlg.classList.add('formDialog');
let html = '';
2020-07-18 09:21:15 +01:00
html += globalize.translateHtml(template);
2020-06-04 20:32:39 +03:00
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
/* eslint-enable indent */
2019-10-09 18:49:41 +03:00
2020-06-04 20:32:39 +03:00
export default {
show: show
};