jellyfin-web/dashboard-ui/components/channelmapper/channelmapper.js

200 lines
6.3 KiB
JavaScript
Raw Normal View History

2016-06-10 10:34:08 -07:00
define(['dialogHelper', 'loading', 'connectionManager', 'globalize', 'actionsheet', 'paper-checkbox', 'emby-input', 'paper-icon-button-light', 'emby-button', 'listViewStyle', 'material-icons'],
2016-06-09 23:54:03 -07:00
function (dialogHelper, loading, connectionManager, globalize, actionsheet) {
2016-06-08 14:04:52 -07:00
2016-06-09 23:54:03 -07:00
return function (options) {
2016-06-08 14:04:52 -07:00
2016-06-09 23:54:03 -07:00
var self = this;
2016-06-09 09:13:25 -07:00
2016-06-09 23:54:03 -07:00
var currentMappingOptions;
2016-06-09 09:13:25 -07:00
2016-06-09 23:54:03 -07:00
function parentWithClass(elem, className) {
2016-06-09 09:13:25 -07:00
2016-06-09 23:54:03 -07:00
while (!elem.classList || !elem.classList.contains(className)) {
elem = elem.parentNode;
2016-06-09 09:13:25 -07:00
2016-06-09 23:54:03 -07:00
if (!elem) {
return null;
}
}
2016-06-09 09:13:25 -07:00
2016-06-09 23:54:03 -07:00
return elem;
}
2016-06-09 09:13:25 -07:00
2016-06-10 10:34:08 -07:00
function mapChannel(button, tunerChannelNumber, providerChannelNumber) {
2016-06-09 09:13:25 -07:00
2016-06-10 10:34:08 -07:00
loading.show();
var providerId = options.providerId;
var apiClient = connectionManager.getApiClient(options.serverId);
apiClient.ajax({
type: 'POST',
url: ApiClient.getUrl('LiveTv/ChannelMappings'),
data: {
providerId: providerId,
tunerChannelNumber: tunerChannelNumber,
providerChannelNumber: providerChannelNumber
},
dataType: 'json'
}).then(function (mapping) {
var listItem = parentWithClass(button, 'listItem');
button.setAttribute('data-providernumber', mapping.ProviderChannelNumber);
listItem.querySelector('.secondary').innerHTML = getMappingSecondaryName(mapping, currentMappingOptions.ProviderName);
loading.hide();
});
2016-06-09 09:13:25 -07:00
}
2016-06-09 23:54:03 -07:00
function onChannelsElementClick(e) {
2016-06-09 09:13:25 -07:00
2016-06-09 23:54:03 -07:00
var btnMap = parentWithClass(e.target, 'btnMap');
2016-06-09 09:13:25 -07:00
2016-06-09 23:54:03 -07:00
if (!btnMap) {
return;
}
2016-06-09 09:13:25 -07:00
2016-06-10 10:34:08 -07:00
var tunerChannelNumber = btnMap.getAttribute('data-number');
var providerChannelNumber = btnMap.getAttribute('data-providernumber');
2016-06-09 09:13:25 -07:00
2016-06-09 23:54:03 -07:00
var menuItems = currentMappingOptions.ProviderChannels.map(function (m) {
2016-06-08 14:04:52 -07:00
2016-06-09 23:54:03 -07:00
return {
name: m.Name,
id: m.Id,
2016-06-10 10:34:08 -07:00
selected: m.Id.toLowerCase() == providerChannelNumber.toLowerCase()
2016-06-09 23:54:03 -07:00
};
});
2016-06-08 14:04:52 -07:00
2016-06-09 23:54:03 -07:00
actionsheet.show({
positionTo: btnMap,
items: menuItems
2016-06-09 09:13:25 -07:00
2016-06-09 23:54:03 -07:00
}).then(function (newChannelNumber) {
2016-06-10 10:34:08 -07:00
mapChannel(btnMap, tunerChannelNumber, newChannelNumber);
2016-06-09 23:54:03 -07:00
});
}
2016-06-09 09:13:25 -07:00
2016-06-09 23:54:03 -07:00
function getChannelMappingOptions(serverId, providerId) {
2016-06-09 09:13:25 -07:00
2016-06-10 10:34:08 -07:00
var apiClient = connectionManager.getApiClient(serverId);
return apiClient.getJSON(apiClient.getUrl('LiveTv/ChannelMappingOptions', {
2016-06-09 23:54:03 -07:00
providerId: providerId
}));
}
2016-06-08 14:04:52 -07:00
2016-06-10 10:34:08 -07:00
function getMappingSecondaryName(mapping, providerName) {
return (mapping.ProviderChannelNumber || '') + ' ' + (mapping.ProviderChannelName || '') + ' - ' + providerName;
}
2016-06-09 23:54:03 -07:00
function getTunerChannelHtml(channel, providerName) {
2016-06-08 14:04:52 -07:00
2016-06-09 23:54:03 -07:00
var html = '';
2016-06-09 09:13:25 -07:00
2016-06-09 23:54:03 -07:00
html += '<div class="listItem">';
2016-06-08 14:04:52 -07:00
2016-07-21 10:23:25 -07:00
html += '<i class="md-icon listItemIcon">dvr</i>';
2016-06-08 14:04:52 -07:00
2016-06-26 21:19:10 -07:00
html += '<div class="listItemBody two-line">';
2016-06-09 23:54:03 -07:00
html += '<h3>';
html += channel.Name;
html += '</h3>';
2016-06-08 14:04:52 -07:00
2016-06-27 21:47:30 -07:00
html += '<div class="secondary">';
2016-06-09 23:54:03 -07:00
if (channel.ProviderChannelNumber || channel.ProviderChannelName) {
2016-06-10 10:34:08 -07:00
html += getMappingSecondaryName(channel, providerName);
2016-06-09 23:54:03 -07:00
}
2016-06-27 21:47:30 -07:00
html += '</div>';
2016-06-08 14:04:52 -07:00
2016-06-09 23:54:03 -07:00
html += '</div>';
2016-06-18 22:26:52 -07:00
html += '<button class="btnMap autoSize" is="paper-icon-button-light" type="button" data-number="' + channel.Number + '" data-providernumber="' + channel.ProviderChannelNumber + '"><i class="md-icon">mode_edit</i></button>';
2016-06-09 23:54:03 -07:00
html += '</div>';
return html;
}
function getEditorHtml() {
var html = '';
html += '<div class="dialogContent">';
html += '<div class="dialogContentInner centeredContent">';
html += '<form style="margin:auto;">';
html += '<h1>' + globalize.translate('HeaderChannels') + '</h1>';
html += '<div class="channels paperList">';
html += '</div>';
html += '</form>';
html += '</div>';
html += '</div>';
return html;
}
function initEditor(dlg, options) {
getChannelMappingOptions(options.serverId, options.providerId).then(function (result) {
currentMappingOptions = result;
var channelsElement = dlg.querySelector('.channels');
channelsElement.innerHTML = result.TunerChannels.map(function (channel) {
return getTunerChannelHtml(channel, result.ProviderName);
}).join('');
channelsElement.addEventListener('click', onChannelsElementClick);
});
}
2016-06-08 14:04:52 -07:00
2016-06-09 23:54:03 -07:00
self.show = function () {
2016-06-08 14:04:52 -07:00
var dialogOptions = {
removeOnClose: true
};
dialogOptions.size = 'small';
var dlg = dialogHelper.createDialog(dialogOptions);
dlg.classList.add('formDialog');
dlg.classList.add('ui-body-a');
dlg.classList.add('background-theme-a');
var html = '';
var title = globalize.translate('MapChannels');
html += '<div class="dialogHeader" style="margin:0 0 2em;">';
2016-07-07 20:23:40 -07:00
html += '<button is="paper-icon-button-light" class="btnCancel autoSize" tabindex="-1"><i class="md-icon">&#xE5C4;</i></button>';
2016-06-08 14:04:52 -07:00
html += '<div class="dialogHeaderTitle">';
html += title;
html += '</div>';
html += '</div>';
html += getEditorHtml();
dlg.innerHTML = html;
document.body.appendChild(dlg);
2016-06-09 09:13:25 -07:00
initEditor(dlg, options);
2016-06-08 14:04:52 -07:00
dlg.querySelector('.btnCancel').addEventListener('click', function () {
dialogHelper.close(dlg);
});
return new Promise(function (resolve, reject) {
dlg.addEventListener('close', resolve);
dialogHelper.open(dlg);
});
};
};
});