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/actionSheet/actionSheet.js

339 lines
9.4 KiB
JavaScript
Raw Normal View History

2020-05-03 19:56:46 +05:30
import dialogHelper from 'dialogHelper';
import layoutManager from 'layoutManager';
import globalize from 'globalize';
import dom from 'dom';
import 'emby-button';
import 'css!./actionSheet';
2020-05-03 19:56:46 +05:30
import 'material-icons';
import 'scrollStyles';
import 'listViewStyle';
function getOffsets(elems) {
2020-05-10 14:46:28 +05:30
let results = [];
2020-05-03 20:00:32 +05:30
2020-05-03 23:59:23 +05:30
if (!document) {
2020-05-03 20:00:32 +05:30
return results;
}
for (const elem of elems) {
let box = elem.getBoundingClientRect();
2020-05-03 20:00:32 +05:30
2020-05-04 00:35:39 +05:30
results.push({
2020-05-03 20:00:32 +05:30
top: box.top,
left: box.left,
width: box.width,
height: box.height
2020-05-04 00:35:39 +05:30
});
2020-05-03 20:00:32 +05:30
}
return results;
2020-05-03 19:56:46 +05:30
}
function getPosition(options, dlg) {
2020-05-10 14:46:28 +05:30
const windowSize = dom.getWindowSize();
const windowHeight = windowSize.innerHeight;
const windowWidth = windowSize.innerWidth;
2020-05-03 19:56:46 +05:30
2020-05-10 14:46:28 +05:30
let pos = getOffsets([options.positionTo])[0];
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
if (options.positionY !== 'top') {
pos.top += (pos.height || 0) / 2;
}
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
pos.left += (pos.width || 0) / 2;
2020-05-03 19:56:46 +05:30
2020-05-10 14:46:28 +05:30
const height = dlg.offsetHeight || 300;
const width = dlg.offsetWidth || 160;
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
// Account for popup size
pos.top -= height / 2;
pos.left -= width / 2;
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
// Avoid showing too close to the bottom
2020-05-10 14:46:28 +05:30
const overflowX = pos.left + width - windowWidth;
const overflowY = pos.top + height - windowHeight;
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
if (overflowX > 0) {
pos.left -= (overflowX + 20);
}
if (overflowY > 0) {
pos.top -= (overflowY + 20);
}
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
pos.top += (options.offsetTop || 0);
pos.left += (options.offsetLeft || 0);
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
// Do some boundary checking
pos.top = Math.max(pos.top, 10);
pos.left = Math.max(pos.left, 10);
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
return pos;
2020-05-03 19:56:46 +05:30
}
function centerFocus(elem, horiz, on) {
2020-07-21 13:25:50 +01:00
import('scrollHelper').then(({default: scrollHelper}) => {
2020-05-10 14:46:28 +05:30
const fn = on ? 'on' : 'off';
2020-05-03 20:00:32 +05:30
scrollHelper.centerFocus[fn](elem, horiz);
});
2020-05-03 19:56:46 +05:30
}
export function show(options) {
2020-05-03 20:00:32 +05:30
// items
// positionTo
// showCancel
// title
2020-05-10 14:46:28 +05:30
let dialogOptions = {
2020-05-03 20:00:32 +05:30
removeOnClose: true,
enableHistory: options.enableHistory,
scrollY: false
};
2020-05-10 14:46:28 +05:30
let isFullscreen;
2020-05-03 20:00:32 +05:30
if (layoutManager.tv) {
dialogOptions.size = 'fullscreen';
isFullscreen = true;
dialogOptions.autoFocus = true;
} else {
dialogOptions.modal = false;
dialogOptions.entryAnimation = options.entryAnimation;
dialogOptions.exitAnimation = options.exitAnimation;
dialogOptions.entryAnimationDuration = options.entryAnimationDuration || 140;
dialogOptions.exitAnimationDuration = options.exitAnimationDuration || 100;
dialogOptions.autoFocus = false;
}
2020-05-10 14:46:28 +05:30
let dlg = dialogHelper.createDialog(dialogOptions);
2020-05-03 20:00:32 +05:30
if (isFullscreen) {
dlg.classList.add('actionsheet-fullscreen');
} else {
dlg.classList.add('actionsheet-not-fullscreen');
}
dlg.classList.add('actionSheet');
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
if (options.dialogClass) {
dlg.classList.add(options.dialogClass);
}
2020-05-10 22:58:14 +05:30
2020-05-10 14:46:28 +05:30
let html = '';
2020-05-10 22:58:14 +05:30
2020-05-10 14:46:28 +05:30
const scrollClassName = layoutManager.tv ? 'scrollY smoothScrollY hiddenScrollY' : 'scrollY';
let style = '';
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
// Admittedly a hack but right now the scrollbar is being factored into the width which is causing truncation
if (options.items.length > 20) {
2020-05-10 14:46:28 +05:30
const minWidth = dom.getWindowSize().innerWidth >= 300 ? 240 : 200;
2020-05-10 22:58:14 +05:30
style += 'min-width:' + minWidth + 'px;';
2020-05-03 20:00:32 +05:30
}
2020-05-03 19:56:46 +05:30
2020-05-10 14:46:28 +05:30
let renderIcon = false;
let icons = [];
let itemIcon;
for (const item of options.items) {
2020-05-03 19:56:46 +05:30
2020-05-03 23:59:23 +05:30
itemIcon = item.icon || (item.selected ? 'check' : null);
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
if (itemIcon) {
renderIcon = true;
}
icons.push(itemIcon || '');
}
if (layoutManager.tv) {
html += `<button is="paper-icon-button-light" class="btnCloseActionSheet hide-mouse-idle-tv" tabindex="-1">
<span class="material-icons arrow_back"></span>
</button>`;
2020-05-03 20:00:32 +05:30
}
// If any items have an icon, give them all an icon just to make sure they're all lined up evenly
2020-05-10 14:46:28 +05:30
const center = options.title && (!renderIcon /*|| itemsWithIcons.length != options.items.length*/);
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
if (center || layoutManager.tv) {
html += '<div class="actionSheetContent actionSheetContent-centered">';
} else {
html += '<div class="actionSheetContent">';
}
if (options.title) {
2020-05-10 14:46:28 +05:30
html += '<h1 class="actionSheetTitle">' + options.title + '</h1>';
2020-05-03 20:00:32 +05:30
}
if (options.text) {
2020-05-11 13:48:41 +05:30
html += '<p class="actionSheetText">' + options.text + '</p>';
2020-05-03 20:00:32 +05:30
}
2020-05-10 14:46:28 +05:30
let scrollerClassName = 'actionSheetScroller';
2020-05-03 20:00:32 +05:30
if (layoutManager.tv) {
scrollerClassName += ' actionSheetScroller-tv focuscontainer-x focuscontainer-y';
}
html += '<div class="' + scrollerClassName + ' ' + scrollClassName + '" style="' + style + '">';
2020-05-10 14:46:28 +05:30
let menuItemClass = 'listItem listItem-button actionSheetMenuItem';
2020-05-03 20:00:32 +05:30
if (options.border || options.shaded) {
menuItemClass += ' listItem-border';
}
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
if (options.menuItemClass) {
menuItemClass += ' ' + options.menuItemClass;
}
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
if (layoutManager.tv) {
menuItemClass += ' listItem-focusscale';
}
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
if (layoutManager.mobile) {
menuItemClass += ' actionsheet-xlargeFont';
}
2020-05-03 19:56:46 +05:30
2020-05-19 20:15:41 +03:00
// 'options.items' is HTMLOptionsCollection, so no fancy loops
for (let i = 0; i < options.items.length; i++) {
const item = options.items[i];
2020-05-03 19:56:46 +05:30
2020-05-03 23:59:23 +05:30
if (item.divider) {
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
html += '<div class="actionsheetDivider"></div>';
continue;
}
2020-05-03 19:56:46 +05:30
2020-05-10 14:46:28 +05:30
const autoFocus = item.selected && layoutManager.tv ? ' autoFocus' : '';
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
// Check for null in case int 0 was passed in
2020-05-10 14:46:28 +05:30
const optionId = item.id == null || item.id === '' ? item.value : item.id;
2020-05-03 20:00:32 +05:30
html += '<button' + autoFocus + ' is="emby-button" type="button" class="' + menuItemClass + '" data-id="' + optionId + '">';
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
itemIcon = icons[i];
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
if (itemIcon) {
html += `<span class="actionsheetMenuItemIcon listItemIcon listItemIcon-transparent material-icons ${itemIcon}"></span>`;
2020-05-03 20:00:32 +05:30
} else if (renderIcon && !center) {
2020-05-10 22:58:14 +05:30
html += '<span class="actionsheetMenuItemIcon listItemIcon listItemIcon-transparent material-icons check" style="visibility:hidden;"></span>';
2020-05-03 20:00:32 +05:30
}
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
html += '<div class="listItemBody actionsheetListItemBody">';
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
html += '<div class="listItemBodyText actionSheetItemText">';
2020-05-03 23:59:23 +05:30
html += (item.name || item.textContent || item.innerText);
2020-05-03 20:00:32 +05:30
html += '</div>';
2020-05-03 19:56:46 +05:30
2020-05-03 23:59:23 +05:30
if (item.secondaryText) {
html += `<div class="listItemBodyText secondary">${item.secondaryText}</div>`;
2020-05-03 20:00:32 +05:30
}
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
html += '</div>';
2020-05-03 19:56:46 +05:30
2020-05-03 23:59:23 +05:30
if (item.asideText) {
html += `<div class="listItemAside actionSheetItemAsideText">${item.asideText}</div>`;
2020-05-03 20:00:32 +05:30
}
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
html += '</button>';
}
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
if (options.showCancel) {
html += '<div class="buttons">';
html += `<button is="emby-button" type="button" class="btnCloseActionSheet">${globalize.translate('ButtonCancel')}</button>`;
2020-05-03 20:00:32 +05:30
html += '</div>';
}
html += '</div>';
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
dlg.innerHTML = html;
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
if (layoutManager.tv) {
centerFocus(dlg.querySelector('.actionSheetScroller'), false, true);
}
2020-05-03 19:56:46 +05:30
2020-05-10 14:46:28 +05:30
let btnCloseActionSheet = dlg.querySelector('.btnCloseActionSheet');
2020-05-03 20:00:32 +05:30
if (btnCloseActionSheet) {
2020-05-10 14:46:28 +05:30
btnCloseActionSheet.addEventListener('click', function () {
2020-05-03 20:00:32 +05:30
dialogHelper.close(dlg);
});
}
2020-05-03 19:56:46 +05:30
2020-05-10 14:46:28 +05:30
let selectedId;
2020-05-03 19:56:46 +05:30
2020-05-10 14:46:28 +05:30
let timeout;
2020-05-03 20:00:32 +05:30
if (options.timeout) {
timeout = setTimeout(function () {
dialogHelper.close(dlg);
}, options.timeout);
}
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
return new Promise(function (resolve, reject) {
2020-05-03 19:56:46 +05:30
2020-05-10 14:46:28 +05:30
let isResolved;
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
dlg.addEventListener('click', function (e) {
2020-05-03 19:56:46 +05:30
2020-05-10 14:46:28 +05:30
const actionSheetMenuItem = dom.parentWithClass(e.target, 'actionSheetMenuItem');
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
if (actionSheetMenuItem) {
selectedId = actionSheetMenuItem.getAttribute('data-id');
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
if (options.resolveOnClick) {
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
if (options.resolveOnClick.indexOf) {
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
if (options.resolveOnClick.indexOf(selectedId) !== -1) {
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
resolve(selectedId);
isResolved = true;
}
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
} else {
resolve(selectedId);
isResolved = true;
}
}
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
dialogHelper.close(dlg);
}
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
});
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
dlg.addEventListener('close', function () {
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
if (layoutManager.tv) {
centerFocus(dlg.querySelector('.actionSheetScroller'), false, false);
}
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
if (!isResolved) {
if (selectedId != null) {
if (options.callback) {
options.callback(selectedId);
}
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
resolve(selectedId);
} else {
reject();
}
}
});
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
dialogHelper.open(dlg);
2020-05-03 19:56:46 +05:30
2020-05-10 14:46:28 +05:30
const pos = options.positionTo && dialogOptions.size !== 'fullscreen' ? getPosition(options, dlg) : null;
2020-05-03 19:56:46 +05:30
2020-05-03 20:00:32 +05:30
if (pos) {
dlg.style.position = 'fixed';
dlg.style.margin = 0;
dlg.style.left = pos.left + 'px';
dlg.style.top = pos.top + 'px';
}
});
2020-05-03 19:56:46 +05:30
}
export default {
2020-05-03 20:00:32 +05:30
show: show
2020-05-03 19:56:46 +05:30
};