jellyfish-web/src/components/tvproviders/schedulesdirect.js

298 lines
12 KiB
JavaScript
Raw Normal View History

2020-05-04 12:44:12 +02:00
define(['jQuery', 'loading', 'globalize', 'emby-checkbox', 'listViewStyle', 'emby-input', 'emby-select', 'emby-button', 'flexStyles'], function ($, loading, globalize) {
'use strict';
2019-10-09 19:13:07 +03:00
return function (page, providerId, options) {
2018-10-23 01:05:09 +03:00
function reload() {
2019-10-09 19:13:07 +03:00
loading.show();
2020-05-04 12:44:12 +02:00
ApiClient.getNamedConfiguration('livetv').then(function (config) {
2019-10-09 19:13:07 +03:00
var info = config.ListingProviders.filter(function (i) {
return i.Id === providerId;
2018-10-23 01:05:09 +03:00
})[0] || {};
2019-10-09 19:13:07 +03:00
listingsId = info.ListingsId;
2020-05-04 12:44:12 +02:00
$('#selectListing', page).val(info.ListingsId || '');
page.querySelector('.txtUser').value = info.Username || '';
page.querySelector('.txtPass').value = '';
page.querySelector('.txtZipCode').value = info.ZipCode || '';
2019-10-09 19:13:07 +03:00
if (info.Username && info.Password) {
2020-05-04 12:44:12 +02:00
page.querySelector('.listingsSection').classList.remove('hide');
2019-10-09 19:13:07 +03:00
} else {
2020-05-04 12:44:12 +02:00
page.querySelector('.listingsSection').classList.add('hide');
2019-10-09 19:13:07 +03:00
}
2020-05-04 12:44:12 +02:00
page.querySelector('.chkAllTuners').checked = info.EnableAllTuners;
2019-10-09 19:13:07 +03:00
if (info.EnableAllTuners) {
2020-05-04 12:44:12 +02:00
page.querySelector('.selectTunersSection').classList.add('hide');
2019-10-09 19:13:07 +03:00
} else {
2020-05-04 12:44:12 +02:00
page.querySelector('.selectTunersSection').classList.remove('hide');
2019-10-09 19:13:07 +03:00
}
setCountry(info);
refreshTunerDevices(page, info, config.TunerHosts);
});
2018-10-23 01:05:09 +03:00
}
function setCountry(info) {
2020-05-04 12:44:12 +02:00
ApiClient.getJSON(ApiClient.getUrl('LiveTv/ListingProviders/SchedulesDirect/Countries')).then(function (result) {
2019-10-09 19:13:07 +03:00
var i;
var length;
var countryList = [];
2018-10-23 01:05:09 +03:00
for (var region in result) {
var countries = result[region];
2019-10-09 19:13:07 +03:00
2020-05-04 12:44:12 +02:00
if (countries.length && 'ZZZ' !== region) {
2019-10-09 19:13:07 +03:00
for (i = 0, length = countries.length; i < length; i++) {
countryList.push({
name: countries[i].fullName,
value: countries[i].shortName
});
}
}
2018-10-23 01:05:09 +03:00
}
2019-10-09 19:13:07 +03:00
countryList.sort(function (a, b) {
if (a.name > b.name) {
return 1;
}
if (a.name < b.name) {
return -1;
}
return 0;
});
2020-05-04 12:44:12 +02:00
$('#selectCountry', page).html(countryList.map(function (c) {
return '<option value="' + c.value + '">' + c.name + '</option>';
}).join('')).val(info.Country || '');
$(page.querySelector('.txtZipCode')).trigger('change');
}, function () { // ApiClient.getJSON() error handler
2018-10-23 01:05:09 +03:00
Dashboard.alert({
2020-05-04 12:44:12 +02:00
message: globalize.translate('ErrorGettingTvLineups')
2019-10-09 19:13:07 +03:00
});
});
loading.hide();
2018-10-23 01:05:09 +03:00
}
function sha256(str) {
2019-10-09 19:13:07 +03:00
if (!self.TextEncoder) {
2020-05-04 12:44:12 +02:00
return Promise.resolve('');
2019-10-09 19:13:07 +03:00
}
2020-05-04 12:44:12 +02:00
var buffer = new TextEncoder('utf-8').encode(str);
return crypto.subtle.digest('SHA-256', buffer).then(function (hash) {
2019-10-09 19:13:07 +03:00
return hex(hash);
});
2018-10-23 01:05:09 +03:00
}
function hex(buffer) {
2019-10-09 19:13:07 +03:00
var hexCodes = [];
var view = new DataView(buffer);
2019-10-09 19:13:07 +03:00
2019-10-16 18:23:18 +03:00
for (var i = 0; i < view.byteLength; i += 4) {
2019-10-09 19:13:07 +03:00
var value = view.getUint32(i);
var stringValue = value.toString(16);
2020-05-04 12:44:12 +02:00
var paddedValue = ('00000000' + stringValue).slice(-'00000000'.length);
2019-10-09 19:13:07 +03:00
hexCodes.push(paddedValue);
2018-10-23 01:05:09 +03:00
}
2019-10-09 19:13:07 +03:00
2020-05-04 12:44:12 +02:00
return hexCodes.join('');
2018-10-23 01:05:09 +03:00
}
function submitLoginForm() {
2019-10-09 19:13:07 +03:00
loading.show();
2020-05-04 12:44:12 +02:00
sha256(page.querySelector('.txtPass').value).then(function (passwordHash) {
2018-10-23 01:05:09 +03:00
var info = {
2020-05-04 12:44:12 +02:00
Type: 'SchedulesDirect',
Username: page.querySelector('.txtUser').value,
2019-10-09 19:13:07 +03:00
EnableAllTuners: true,
Password: passwordHash,
2020-05-04 12:44:12 +02:00
Pw: page.querySelector('.txtPass').value
2019-10-09 19:13:07 +03:00
};
var id = providerId;
if (id) {
info.Id = id;
}
ApiClient.ajax({
2020-05-04 12:44:12 +02:00
type: 'POST',
url: ApiClient.getUrl('LiveTv/ListingProviders', {
2019-10-09 19:13:07 +03:00
ValidateLogin: true
2018-10-23 01:05:09 +03:00
}),
data: JSON.stringify(info),
2020-05-04 12:44:12 +02:00
contentType: 'application/json',
dataType: 'json'
2019-10-09 19:13:07 +03:00
}).then(function (result) {
Dashboard.processServerConfigurationUpdateResult();
providerId = result.Id;
reload();
}, function () {
Dashboard.alert({ // ApiClient.ajax() error handler
2020-05-04 12:44:12 +02:00
message: globalize.translate('ErrorSavingTvProvider')
2019-10-09 19:13:07 +03:00
});
});
});
2018-10-23 01:05:09 +03:00
}
function submitListingsForm() {
2020-05-04 12:44:12 +02:00
var selectedListingsId = $('#selectListing', page).val();
2019-10-09 19:13:07 +03:00
if (!selectedListingsId) {
return void Dashboard.alert({
2020-05-04 12:44:12 +02:00
message: globalize.translate('ErrorPleaseSelectLineup')
2019-10-09 19:13:07 +03:00
});
}
2018-10-23 01:05:09 +03:00
loading.show();
var id = providerId;
2020-05-04 12:44:12 +02:00
ApiClient.getNamedConfiguration('livetv').then(function (config) {
2019-10-09 19:13:07 +03:00
var info = config.ListingProviders.filter(function (i) {
return i.Id === id;
2018-10-23 01:05:09 +03:00
})[0];
2020-05-04 12:44:12 +02:00
info.ZipCode = page.querySelector('.txtZipCode').value;
info.Country = $('#selectCountry', page).val();
2019-10-09 19:13:07 +03:00
info.ListingsId = selectedListingsId;
2020-05-04 12:44:12 +02:00
info.EnableAllTuners = page.querySelector('.chkAllTuners').checked;
info.EnabledTuners = info.EnableAllTuners ? [] : $('.chkTuner', page).get().filter(function (i) {
2019-10-09 19:13:07 +03:00
return i.checked;
}).map(function (i) {
2020-05-04 12:44:12 +02:00
return i.getAttribute('data-id');
2019-10-09 19:13:07 +03:00
});
ApiClient.ajax({
2020-05-04 12:44:12 +02:00
type: 'POST',
url: ApiClient.getUrl('LiveTv/ListingProviders', {
2019-10-09 19:13:07 +03:00
ValidateListings: true
2018-10-23 01:05:09 +03:00
}),
data: JSON.stringify(info),
2020-05-04 12:44:12 +02:00
contentType: 'application/json'
2019-10-09 19:13:07 +03:00
}).then(function (result) {
loading.hide();
if (options.showConfirmation) {
2019-10-09 19:13:07 +03:00
Dashboard.processServerConfigurationUpdateResult();
}
2020-05-04 12:44:12 +02:00
Events.trigger(self, 'submitted');
2019-10-09 19:13:07 +03:00
}, function () {
loading.hide();
Dashboard.alert({
2020-05-04 12:44:12 +02:00
message: globalize.translate('ErrorAddingListingsToSchedulesDirect')
2019-10-09 19:13:07 +03:00
});
});
});
2018-10-23 01:05:09 +03:00
}
function refreshListings(value) {
2019-10-09 19:13:07 +03:00
if (!value) {
2020-05-04 12:44:12 +02:00
return void $('#selectListing', page).html('');
2019-10-09 19:13:07 +03:00
}
loading.show();
ApiClient.ajax({
2020-05-04 12:44:12 +02:00
type: 'GET',
url: ApiClient.getUrl('LiveTv/ListingProviders/Lineups', {
2018-10-23 01:05:09 +03:00
Id: providerId,
Location: value,
2020-05-04 12:44:12 +02:00
Country: $('#selectCountry', page).val()
2018-10-23 01:05:09 +03:00
}),
2020-05-04 12:44:12 +02:00
dataType: 'json'
2019-10-09 19:13:07 +03:00
}).then(function (result) {
2020-05-04 12:44:12 +02:00
$('#selectListing', page).html(result.map(function (o) {
return '<option value="' + o.Id + '">' + o.Name + '</option>';
2019-10-09 19:13:07 +03:00
}));
if (listingsId) {
2020-05-04 12:44:12 +02:00
$('#selectListing', page).val(listingsId);
2019-10-09 19:13:07 +03:00
}
loading.hide();
}, function (result) {
2018-10-23 01:05:09 +03:00
Dashboard.alert({
2020-05-04 12:44:12 +02:00
message: globalize.translate('ErrorGettingTvLineups')
2019-10-09 19:13:07 +03:00
});
2020-05-04 12:44:12 +02:00
refreshListings('');
2019-10-09 19:13:07 +03:00
loading.hide();
});
2018-10-23 01:05:09 +03:00
}
function getTunerName(providerId) {
switch (providerId = providerId.toLowerCase()) {
2020-05-04 12:44:12 +02:00
case 'm3u':
return 'M3U Playlist';
case 'hdhomerun':
return 'HDHomerun';
case 'satip':
return 'DVB';
2018-10-23 01:05:09 +03:00
default:
2020-05-04 12:44:12 +02:00
return 'Unknown';
2018-10-23 01:05:09 +03:00
}
}
function refreshTunerDevices(page, providerInfo, devices) {
2020-05-04 12:44:12 +02:00
var html = '';
2019-10-09 19:13:07 +03:00
for (var i = 0, length = devices.length; i < length; i++) {
2018-10-23 01:05:09 +03:00
var device = devices[i];
html += '<div class="listItem">';
2019-10-09 19:13:07 +03:00
var enabledTuners = providerInfo.EnabledTuners || [];
var isChecked = providerInfo.EnableAllTuners || -1 !== enabledTuners.indexOf(device.Id);
2020-05-04 12:44:12 +02:00
var checkedAttribute = isChecked ? ' checked' : '';
html += '<label class="checkboxContainer listItemCheckboxContainer"><input type="checkbox" is="emby-checkbox" data-id="' + device.Id + '" class="chkTuner" ' + checkedAttribute + '/><span></span></label>';
2019-10-09 19:13:07 +03:00
html += '<div class="listItemBody two-line">';
html += '<div class="listItemBodyText">';
html += device.FriendlyName || getTunerName(device.Type);
2020-05-04 12:44:12 +02:00
html += '</div>';
2019-10-09 19:13:07 +03:00
html += '<div class="listItemBodyText secondary">';
html += device.Url;
2020-05-04 12:44:12 +02:00
html += '</div>';
html += '</div>';
html += '</div>';
2018-10-23 01:05:09 +03:00
}
2019-10-09 19:13:07 +03:00
2020-05-04 12:44:12 +02:00
page.querySelector('.tunerList').innerHTML = html;
2018-10-23 01:05:09 +03:00
}
2019-10-09 19:13:07 +03:00
var listingsId;
var self = this;
self.submit = function () {
2020-05-04 12:44:12 +02:00
page.querySelector('.btnSubmitListingsContainer').click();
2019-10-09 19:13:07 +03:00
};
self.init = function () {
options = options || {};
// Only hide the buttons if explicitly set to false; default to showing if undefined or null
// FIXME: rename this option to clarify logic
var hideCancelButton = options.showCancelButton === false;
2020-05-04 12:44:12 +02:00
page.querySelector('.btnCancel').classList.toggle('hide', hideCancelButton);
2019-10-09 19:13:07 +03:00
var hideSubmitButton = options.showSubmitButton === false;
2020-05-04 12:44:12 +02:00
page.querySelector('.btnSubmitListings').classList.toggle('hide', hideSubmitButton);
2019-10-09 19:13:07 +03:00
2020-05-04 12:44:12 +02:00
$('.formLogin', page).on('submit', function () {
2019-10-09 19:13:07 +03:00
submitLoginForm();
return false;
});
2020-05-04 12:44:12 +02:00
$('.formListings', page).on('submit', function () {
2019-10-09 19:13:07 +03:00
submitListingsForm();
return false;
});
2020-05-04 12:44:12 +02:00
$('.txtZipCode', page).on('change', function () {
2019-10-09 19:13:07 +03:00
refreshListings(this.value);
});
2020-05-04 12:44:12 +02:00
page.querySelector('.chkAllTuners').addEventListener('change', function (e) {
2019-10-09 19:13:07 +03:00
if (e.target.checked) {
2020-05-04 12:44:12 +02:00
page.querySelector('.selectTunersSection').classList.add('hide');
2019-10-09 19:13:07 +03:00
} else {
2020-05-04 12:44:12 +02:00
page.querySelector('.selectTunersSection').classList.remove('hide');
2019-10-09 19:13:07 +03:00
}
});
2020-05-04 12:44:12 +02:00
$('.createAccountHelp', page).html(globalize.translate('MessageCreateAccountAt', '<a is="emby-linkbutton" class="button-link" href="http://www.schedulesdirect.org" target="_blank">http://www.schedulesdirect.org</a>'));
2019-10-09 19:13:07 +03:00
reload();
};
};
});