2020-03-26 14:25:16 +01:00
|
|
|
define(["dialogHelper", "datetime", "emby-select", "paper-icon-button-light", "formDialogStyle", "globalize"], function (dialogHelper, datetime, globalize) {
|
2018-10-23 01:05:09 +03:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
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) {
|
2019-10-09 18:49:41 +03:00
|
|
|
var html = "";
|
|
|
|
|
|
|
|
for (var i = 0; i < 24; i++) {
|
|
|
|
html += '<option value="' + i + '">' + getDisplayTime(i) + "</option>";
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2019-10-09 18:49:41 +03: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 = {
|
|
|
|
DayOfWeek: context.querySelector("#selectDay").value,
|
|
|
|
StartHour: context.querySelector("#selectStart").value,
|
|
|
|
EndHour: context.querySelector("#selectEnd").value
|
|
|
|
};
|
2019-10-09 18:49:41 +03:00
|
|
|
|
|
|
|
if (parseFloat(updatedSchedule.StartHour) >= parseFloat(updatedSchedule.EndHour)) {
|
2020-03-26 14:25:16 +01: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();
|
|
|
|
xhr.open("GET", "components/accessschedule/accessschedule.template.html", true);
|
|
|
|
|
|
|
|
xhr.onload = function (e) {
|
|
|
|
var template = this.response;
|
|
|
|
var dlg = dialogHelper.createDialog({
|
|
|
|
removeOnClose: true,
|
|
|
|
size: "small"
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
dlg.classList.add("formDialog");
|
|
|
|
var html = "";
|
2019-10-09 18:49:41 +03:00
|
|
|
html += Globalize.translateDocument(template);
|
|
|
|
dlg.innerHTML = html;
|
|
|
|
populateHours(dlg);
|
|
|
|
loadSchedule(dlg, options.schedule);
|
|
|
|
dialogHelper.open(dlg);
|
|
|
|
dlg.addEventListener("close", function () {
|
|
|
|
if (dlg.submitted) {
|
|
|
|
resolve(options.schedule);
|
|
|
|
} else {
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
dlg.querySelector(".btnCancel").addEventListener("click", function (e) {
|
|
|
|
dialogHelper.close(dlg);
|
|
|
|
});
|
|
|
|
dlg.querySelector("form").addEventListener("submit", function (e) {
|
|
|
|
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
|
|
|
};
|
|
|
|
});
|