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

90 lines
3.3 KiB
JavaScript
Raw Normal View History

2020-05-04 12:44:12 +02:00
define(['dialogHelper', 'datetime', 'globalize', 'emby-select', 'paper-icon-button-light', 'formDialogStyle'], function (dialogHelper, datetime, globalize) {
'use strict';
2018-10-23 01:05:09 +03:00
function getDisplayTime(hours) {
2019-10-09 18:49:41 +03:00
var minutes = 0;
var pct = hours % 1;
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-05-04 12:44:12 +02:00
var html = '';
2019-10-09 18:49:41 +03:00
for (var i = 0; i < 24; i++) {
2020-05-04 12:44:12 +02:00
html += '<option value="' + i + '">' + getDisplayTime(i) + '</option>';
2019-10-09 18:49:41 +03:00
}
2020-05-04 12:44:12 +02:00
html += '<option value="24">' + getDisplayTime(0) + '</option>';
context.querySelector('#selectStart').innerHTML = html;
context.querySelector('#selectEnd').innerHTML = html;
2018-10-23 01:05:09 +03:00
}
function loadSchedule(context, schedule) {
2020-05-04 12:44:12 +02:00
context.querySelector('#selectDay').value = schedule.DayOfWeek || 'Sunday';
context.querySelector('#selectStart').value = schedule.StartHour || 0;
context.querySelector('#selectEnd').value = schedule.EndHour || 0;
2018-10-23 01:05:09 +03:00
}
function submitSchedule(context, options) {
var 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)) {
2020-05-04 12:44:12 +02:00
return void alert(globalize.translate('ErrorMessageStartHourGreaterThanEnd'));
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
2018-10-23 01:05:09 +03:00
return {
2019-10-09 18:49:41 +03:00
show: function (options) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
2020-05-04 12:44:12 +02:00
xhr.open('GET', 'components/accessschedule/accessschedule.template.html', true);
2019-10-09 18:49:41 +03:00
xhr.onload = function (e) {
var template = this.response;
var dlg = dialogHelper.createDialog({
removeOnClose: true,
2020-05-04 12:44:12 +02:00
size: 'small'
2019-10-09 18:49:41 +03:00
});
2020-05-04 12:44:12 +02:00
dlg.classList.add('formDialog');
var html = '';
html += globalize.translateDocument(template);
2019-10-09 18:49:41 +03:00
dlg.innerHTML = html;
populateHours(dlg);
loadSchedule(dlg, options.schedule);
dialogHelper.open(dlg);
2020-05-04 12:44:12 +02:00
dlg.addEventListener('close', function () {
2019-10-09 18:49:41 +03:00
if (dlg.submitted) {
resolve(options.schedule);
} else {
reject();
}
});
2020-05-04 12:44:12 +02:00
dlg.querySelector('.btnCancel').addEventListener('click', function (e) {
2019-10-09 18:49:41 +03:00
dialogHelper.close(dlg);
});
2020-05-04 12:44:12 +02:00
dlg.querySelector('form').addEventListener('submit', function (e) {
2019-10-09 18:49:41 +03:00
submitSchedule(dlg, options);
e.preventDefault();
return false;
});
};
xhr.send();
});
2018-10-23 01:05:09 +03:00
}
2019-10-09 18:49:41 +03:00
};
});