2020-07-09 16:20:32 +01:00
|
|
|
import appHost from 'apphost';
|
|
|
|
import appSettings from 'appSettings';
|
|
|
|
import dom from 'dom';
|
|
|
|
import connectionManager from 'connectionManager';
|
|
|
|
import loading from 'loading';
|
|
|
|
import layoutManager from 'layoutManager';
|
2020-07-24 10:43:03 +02:00
|
|
|
import libraryMenu from 'libraryMenu';
|
2020-07-09 16:20:32 +01:00
|
|
|
import browser from 'browser';
|
|
|
|
import globalize from 'globalize';
|
|
|
|
import 'cardStyle';
|
|
|
|
import 'emby-checkbox';
|
|
|
|
|
|
|
|
/* eslint-disable indent */
|
2018-10-23 01:05:09 +03:00
|
|
|
|
2020-07-15 09:29:15 +01:00
|
|
|
const enableFocusTransform = !browser.slow && !browser.edge;
|
2019-11-11 12:28:27 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
function authenticateUserByName(page, apiClient, username, password) {
|
2019-04-06 23:07:02 -07:00
|
|
|
loading.show();
|
2020-01-22 03:29:52 +03:00
|
|
|
apiClient.authenticateUserByName(username, password).then(function (result) {
|
2019-04-06 23:07:02 -07:00
|
|
|
var user = result.User;
|
|
|
|
loading.hide();
|
2020-07-12 04:24:45 +09:00
|
|
|
|
2019-04-06 23:07:02 -07:00
|
|
|
Dashboard.onServerChanged(user.Id, result.AccessToken, apiClient);
|
2020-07-12 04:24:45 +09:00
|
|
|
Dashboard.navigate('home.html');
|
2020-01-22 03:29:52 +03:00
|
|
|
}, function (response) {
|
2020-05-04 12:44:12 +02:00
|
|
|
page.querySelector('#txtManualName').value = '';
|
|
|
|
page.querySelector('#txtManualPassword').value = '';
|
2019-04-06 23:07:02 -07:00
|
|
|
loading.hide();
|
2020-01-22 03:29:52 +03:00
|
|
|
|
2020-04-13 16:41:24 -04:00
|
|
|
const UnauthorizedOrForbidden = [401, 403];
|
|
|
|
if (UnauthorizedOrForbidden.includes(response.status)) {
|
2020-07-09 16:20:32 +01:00
|
|
|
import('toast').then(({default: toast}) => {
|
2020-05-04 12:44:12 +02:00
|
|
|
const messageKey = response.status === 401 ? 'MessageInvalidUser' : 'MessageUnauthorizedUser';
|
2020-04-26 14:44:01 +02:00
|
|
|
toast(globalize.translate(messageKey));
|
2019-04-06 23:07:02 -07:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Dashboard.alert({
|
2020-05-04 12:44:12 +02:00
|
|
|
message: globalize.translate('MessageUnableToConnectToServer'),
|
|
|
|
title: globalize.translate('HeaderConnectionFailure')
|
2019-04-06 23:07:02 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function showManualForm(context, showCancel, focusPassword) {
|
2020-05-04 12:44:12 +02:00
|
|
|
context.querySelector('.chkRememberLogin').checked = appSettings.enableAutoLogin();
|
|
|
|
context.querySelector('.manualLoginForm').classList.remove('hide');
|
|
|
|
context.querySelector('.visualLoginForm').classList.add('hide');
|
|
|
|
context.querySelector('.btnManual').classList.add('hide');
|
2020-01-22 03:29:52 +03:00
|
|
|
|
|
|
|
if (focusPassword) {
|
2020-05-04 12:44:12 +02:00
|
|
|
context.querySelector('#txtManualPassword').focus();
|
2020-01-22 03:29:52 +03:00
|
|
|
} else {
|
2020-05-04 12:44:12 +02:00
|
|
|
context.querySelector('#txtManualName').focus();
|
2020-01-22 03:29:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (showCancel) {
|
2020-05-04 12:44:12 +02:00
|
|
|
context.querySelector('.btnCancel').classList.remove('hide');
|
2020-01-22 03:29:52 +03:00
|
|
|
} else {
|
2020-05-04 12:44:12 +02:00
|
|
|
context.querySelector('.btnCancel').classList.add('hide');
|
2020-01-22 03:29:52 +03:00
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-07-15 09:29:15 +01:00
|
|
|
const metroColors = ['#6FBD45', '#4BB3DD', '#4164A5', '#E12026', '#800080', '#E1B222', '#008040', '#0094FF', '#FF00C7', '#FF870F', '#7F0037'];
|
2019-04-06 23:07:02 -07:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
function getRandomMetroColor() {
|
2020-07-15 09:29:15 +01:00
|
|
|
const index = Math.floor(Math.random() * (metroColors.length - 1));
|
2019-04-06 23:07:02 -07:00
|
|
|
return metroColors[index];
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getMetroColor(str) {
|
|
|
|
if (str) {
|
2020-07-15 09:29:15 +01:00
|
|
|
const character = String(str.substr(0, 1).charCodeAt());
|
|
|
|
let sum = 0;
|
2020-01-22 03:29:52 +03:00
|
|
|
|
2020-07-15 09:29:15 +01:00
|
|
|
for (let i = 0; i < character.length; i++) {
|
2019-04-06 23:07:02 -07:00
|
|
|
sum += parseInt(character.charAt(i));
|
|
|
|
}
|
2020-01-22 03:29:52 +03:00
|
|
|
|
2020-07-15 09:29:15 +01:00
|
|
|
const index = String(sum).substr(-1);
|
2019-04-06 23:07:02 -07:00
|
|
|
return metroColors[index];
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2020-01-22 03:29:52 +03:00
|
|
|
|
2019-04-06 23:07:02 -07:00
|
|
|
return getRandomMetroColor();
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function loadUserList(context, apiClient, users) {
|
2020-07-15 09:29:15 +01:00
|
|
|
let html = '';
|
2020-01-22 03:29:52 +03:00
|
|
|
|
2020-07-15 09:29:15 +01:00
|
|
|
for (let i = 0; i < users.length; i++) {
|
|
|
|
const user = users[i];
|
2019-11-11 12:28:27 +03:00
|
|
|
|
|
|
|
// TODO move card creation code to Card component
|
2020-07-15 09:29:15 +01:00
|
|
|
let cssClass = 'card squareCard scalableCard squareCard-scalable';
|
2019-11-11 12:28:27 +03:00
|
|
|
|
|
|
|
if (layoutManager.tv) {
|
2020-05-04 12:44:12 +02:00
|
|
|
cssClass += ' show-focus';
|
2019-11-11 12:28:27 +03:00
|
|
|
|
|
|
|
if (enableFocusTransform) {
|
2020-05-04 12:44:12 +02:00
|
|
|
cssClass += ' show-animation';
|
2019-11-11 12:28:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 09:29:15 +01:00
|
|
|
const cardBoxCssClass = 'cardBox cardBox-bottompadded';
|
2019-11-11 12:28:27 +03:00
|
|
|
html += '<button type="button" class="' + cssClass + '">';
|
|
|
|
html += '<div class="' + cardBoxCssClass + '">';
|
2019-04-06 23:07:02 -07:00
|
|
|
html += '<div class="cardScalable">';
|
|
|
|
html += '<div class="cardPadder cardPadder-square"></div>';
|
|
|
|
html += '<div class="cardContent" data-haspw="' + user.HasPassword + '" data-username="' + user.Name + '" data-userid="' + user.Id + '">';
|
2020-07-15 09:29:15 +01:00
|
|
|
let imgUrl;
|
2020-01-22 03:29:52 +03:00
|
|
|
|
2019-04-06 23:07:02 -07:00
|
|
|
if (user.PrimaryImageTag) {
|
|
|
|
imgUrl = apiClient.getUserImageUrl(user.Id, {
|
|
|
|
width: 300,
|
|
|
|
tag: user.PrimaryImageTag,
|
2020-05-04 12:44:12 +02:00
|
|
|
type: 'Primary'
|
2019-04-06 23:07:02 -07:00
|
|
|
});
|
|
|
|
html += '<div class="cardImageContainer coveredImage coveredImage-noScale" style="background-image:url(\'' + imgUrl + "');\"></div>";
|
|
|
|
} else {
|
2020-07-15 09:29:15 +01:00
|
|
|
const background = getMetroColor(user.Id);
|
2020-05-04 12:44:12 +02:00
|
|
|
imgUrl = 'assets/img/avatar.png';
|
2019-04-06 23:07:02 -07:00
|
|
|
html += '<div class="cardImageContainer coveredImage coveredImage-noScale" style="background-image:url(\'' + imgUrl + "');background-color:" + background + ';"></div>';
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2020-01-22 03:29:52 +03:00
|
|
|
|
2020-05-04 12:44:12 +02:00
|
|
|
html += '</div>';
|
|
|
|
html += '</div>';
|
2019-04-06 23:07:02 -07:00
|
|
|
html += '<div class="cardFooter visualCardBox-cardFooter">';
|
2020-05-04 12:44:12 +02:00
|
|
|
html += '<div class="cardText singleCardText cardTextCentered">' + user.Name + '</div>';
|
|
|
|
html += '</div>';
|
|
|
|
html += '</div>';
|
|
|
|
html += '</button>';
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2020-01-22 03:29:52 +03:00
|
|
|
|
2020-05-04 12:44:12 +02:00
|
|
|
context.querySelector('#divUsers').innerHTML = html;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-04-06 23:07:02 -07:00
|
|
|
|
2020-07-09 16:20:32 +01:00
|
|
|
export default function (view, params) {
|
2018-10-23 01:05:09 +03:00
|
|
|
function getApiClient() {
|
2020-07-15 09:29:15 +01:00
|
|
|
const serverId = params.serverid;
|
2020-01-22 03:29:52 +03:00
|
|
|
|
|
|
|
if (serverId) {
|
|
|
|
return connectionManager.getOrCreateApiClient(serverId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ApiClient;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function showVisualForm() {
|
2020-05-04 12:44:12 +02:00
|
|
|
view.querySelector('.visualLoginForm').classList.remove('hide');
|
|
|
|
view.querySelector('.manualLoginForm').classList.add('hide');
|
|
|
|
view.querySelector('.btnManual').classList.remove('hide');
|
2019-11-02 20:38:58 +03:00
|
|
|
|
2020-07-09 16:20:32 +01:00
|
|
|
import('autoFocuser').then(({default: autoFocuser}) => {
|
2019-11-02 20:38:58 +03:00
|
|
|
autoFocuser.autoFocus(view);
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-04-06 23:07:02 -07:00
|
|
|
|
2020-05-04 12:44:12 +02:00
|
|
|
view.querySelector('#divUsers').addEventListener('click', function (e) {
|
2020-07-15 09:29:15 +01:00
|
|
|
const card = dom.parentWithClass(e.target, 'card');
|
|
|
|
const cardContent = card ? card.querySelector('.cardContent') : null;
|
2020-01-22 03:29:52 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
if (cardContent) {
|
2020-07-15 09:29:15 +01:00
|
|
|
const context = view;
|
|
|
|
const id = cardContent.getAttribute('data-userid');
|
|
|
|
const name = cardContent.getAttribute('data-username');
|
|
|
|
const haspw = cardContent.getAttribute('data-haspw');
|
2020-01-22 03:29:52 +03:00
|
|
|
|
2019-04-06 23:07:02 -07:00
|
|
|
if (id === 'manual') {
|
2020-05-04 12:44:12 +02:00
|
|
|
context.querySelector('#txtManualName').value = '';
|
2019-04-06 23:07:02 -07:00
|
|
|
showManualForm(context, true);
|
|
|
|
} else if (haspw == 'false') {
|
2020-05-04 12:44:12 +02:00
|
|
|
authenticateUserByName(context, getApiClient(), name, '');
|
2019-04-06 23:07:02 -07:00
|
|
|
} else {
|
2020-05-04 12:44:12 +02:00
|
|
|
context.querySelector('#txtManualName').value = name;
|
|
|
|
context.querySelector('#txtManualPassword').value = '';
|
2019-04-06 23:07:02 -07:00
|
|
|
showManualForm(context, true, true);
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-04-06 23:07:02 -07:00
|
|
|
});
|
2020-05-04 12:44:12 +02:00
|
|
|
view.querySelector('.manualLoginForm').addEventListener('submit', function (e) {
|
|
|
|
appSettings.enableAutoLogin(view.querySelector('.chkRememberLogin').checked);
|
2020-07-15 09:29:15 +01:00
|
|
|
const apiClient = getApiClient();
|
2020-05-04 12:44:12 +02:00
|
|
|
authenticateUserByName(view, apiClient, view.querySelector('#txtManualName').value, view.querySelector('#txtManualPassword').value);
|
2019-04-06 23:07:02 -07:00
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
});
|
2020-05-04 12:44:12 +02:00
|
|
|
view.querySelector('.btnForgotPassword').addEventListener('click', function () {
|
|
|
|
Dashboard.navigate('forgotpassword.html');
|
2019-04-06 23:07:02 -07:00
|
|
|
});
|
2020-05-04 12:44:12 +02:00
|
|
|
view.querySelector('.btnCancel').addEventListener('click', showVisualForm);
|
|
|
|
view.querySelector('.btnManual').addEventListener('click', function () {
|
|
|
|
view.querySelector('#txtManualName').value = '';
|
2019-04-06 23:07:02 -07:00
|
|
|
showManualForm(view, true);
|
|
|
|
});
|
2020-05-04 12:44:12 +02:00
|
|
|
view.querySelector('.btnSelectServer').addEventListener('click', function () {
|
2020-02-01 23:55:07 +03:00
|
|
|
Dashboard.selectServer();
|
|
|
|
});
|
2020-05-04 12:44:12 +02:00
|
|
|
view.addEventListener('viewshow', function (e) {
|
2018-10-23 01:05:09 +03:00
|
|
|
loading.show();
|
2020-07-19 04:36:47 +02:00
|
|
|
libraryMenu.setTransparentMenu(true);
|
2020-01-22 03:29:52 +03:00
|
|
|
|
2019-04-06 23:18:47 -07:00
|
|
|
if (!appHost.supports('multiserver')) {
|
2020-05-04 12:44:12 +02:00
|
|
|
view.querySelector('.btnSelectServer').classList.add('hide');
|
2019-04-06 23:18:47 -07:00
|
|
|
}
|
2020-01-22 03:29:52 +03:00
|
|
|
|
2020-07-15 09:29:15 +01:00
|
|
|
const apiClient = getApiClient();
|
2020-01-22 03:29:52 +03:00
|
|
|
apiClient.getPublicUsers().then(function (users) {
|
2019-02-27 22:05:53 +00:00
|
|
|
if (users.length) {
|
2019-04-26 20:39:43 -07:00
|
|
|
showVisualForm();
|
|
|
|
loadUserList(view, apiClient, users);
|
2019-02-27 22:05:53 +00:00
|
|
|
} else {
|
2020-05-04 12:44:12 +02:00
|
|
|
view.querySelector('#txtManualName').value = '';
|
2019-02-27 22:05:53 +00:00
|
|
|
showManualForm(view, false, false);
|
|
|
|
}
|
2020-01-22 03:29:52 +03:00
|
|
|
}).catch().then(function () {
|
2019-02-27 22:05:53 +00:00
|
|
|
loading.hide();
|
2019-02-05 03:50:16 +09:00
|
|
|
});
|
2020-05-04 12:44:12 +02:00
|
|
|
apiClient.getJSON(apiClient.getUrl('Branding/Configuration')).then(function (options) {
|
|
|
|
view.querySelector('.disclaimer').textContent = options.LoginDisclaimer || '';
|
2019-02-05 03:50:16 +09:00
|
|
|
});
|
|
|
|
});
|
2020-07-19 04:36:47 +02:00
|
|
|
view.addEventListener('viewhide', function (e) {
|
|
|
|
libraryMenu.setTransparentMenu(false);
|
|
|
|
});
|
2020-07-24 10:43:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* eslint-enable indent */
|