jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/actionsheet/actionsheet.js

276 lines
8.3 KiB
JavaScript
Raw Normal View History

2016-07-18 11:58:17 -07:00
define(['dialogHelper', 'layoutManager', 'globalize', 'browser', 'dom', 'emby-button', 'css!./actionsheet', 'material-icons', 'scrollStyles'], function (dialogHelper, layoutManager, globalize, browser, dom) {
2016-02-21 13:39:14 -07:00
2016-03-13 00:34:17 -07:00
function getOffsets(elems) {
var doc = document;
var results = [];
if (!doc) {
return results;
}
var box;
var elem;
for (var i = 0, length = elems.length; i < length; i++) {
elem = elems[i];
// Support: BlackBerry 5, iOS 3 (original iPhone)
// If we don't have gBCR, just use 0,0 rather than error
if (elem.getBoundingClientRect) {
box = elem.getBoundingClientRect();
} else {
box = { top: 0, left: 0 };
}
results[i] = {
top: box.top,
left: box.left
};
}
return results;
}
2016-04-10 21:24:16 -07:00
function getPosition(options, dlg) {
2016-02-21 13:39:14 -07:00
2016-08-11 20:23:12 -07:00
var windowSize = dom.getWindowSize();
var windowHeight = windowSize.innerHeight;
var windowWidth = windowSize.innerWidth;
2016-02-21 13:39:14 -07:00
if (windowHeight < 540) {
return null;
}
2016-03-13 00:34:17 -07:00
var pos = getOffsets([options.positionTo])[0];
2016-02-21 13:39:14 -07:00
2016-03-13 00:34:17 -07:00
pos.top += options.positionTo.offsetHeight / 2;
pos.left += options.positionTo.offsetWidth / 2;
2016-02-21 13:39:14 -07:00
2016-06-09 23:54:03 -07:00
var height = dlg.offsetHeight || 300;
var width = dlg.offsetWidth || 160;
2016-04-10 21:24:16 -07:00
// Account for popup size
2016-06-09 23:54:03 -07:00
pos.top -= height / 2;
pos.left -= width / 2;
2016-02-21 13:39:14 -07:00
// Avoid showing too close to the bottom
2016-06-09 23:54:03 -07:00
var overflowX = pos.left + width - windowWidth;
var overflowY = pos.top + height - windowHeight;
if (overflowX > 0) {
pos.left -= (overflowX + 20);
}
if (overflowY > 0) {
pos.top -= (overflowY + 20);
}
2016-02-21 13:39:14 -07:00
// Do some boundary checking
2016-03-13 00:34:17 -07:00
pos.top = Math.max(pos.top, 10);
pos.left = Math.max(pos.left, 10);
2016-02-21 13:39:14 -07:00
return pos;
}
2016-07-26 22:19:56 -07:00
function centerFocus(elem, horiz, on) {
2016-02-29 09:23:30 -07:00
require(['scrollHelper'], function (scrollHelper) {
2016-07-26 22:19:56 -07:00
var fn = on ? 'on' : 'off';
scrollHelper.centerFocus[fn](elem, horiz);
2016-02-29 09:23:30 -07:00
});
}
2016-02-21 13:39:14 -07:00
function show(options) {
// items
// positionTo
// showCancel
// title
var dialogOptions = {
removeOnClose: true,
2016-05-12 23:22:02 -07:00
enableHistory: options.enableHistory,
scrollY: false
2016-02-21 13:39:14 -07:00
};
var backButton = false;
2016-08-04 19:03:15 -07:00
var isFullscreen;
2016-02-21 13:39:14 -07:00
if (layoutManager.tv) {
dialogOptions.size = 'fullscreen';
2016-08-04 19:03:15 -07:00
isFullscreen = true;
2016-02-21 13:39:14 -07:00
backButton = true;
dialogOptions.autoFocus = true;
} else {
dialogOptions.modal = false;
2016-07-17 09:59:14 -07:00
dialogOptions.entryAnimationDuration = 140;
dialogOptions.exitAnimationDuration = 180;
2016-02-21 13:39:14 -07:00
dialogOptions.autoFocus = false;
}
2016-03-23 12:03:17 -07:00
var dlg = dialogHelper.createDialog(dialogOptions);
2016-02-21 13:39:14 -07:00
2016-08-04 19:03:15 -07:00
if (isFullscreen) {
dlg.classList.add('actionsheet-fullscreen');
}
2016-04-10 21:24:16 -07:00
if (!layoutManager.tv) {
2016-08-01 22:55:52 -07:00
dlg.classList.add('actionsheet-extraSpacing');
2016-04-10 21:24:16 -07:00
}
2016-02-21 13:39:14 -07:00
dlg.classList.add('actionSheet');
var html = '';
2016-06-09 23:54:03 -07:00
var scrollType = layoutManager.desktop ? 'smoothScrollY' : 'hiddenScrollY';
2016-06-29 12:25:09 -07:00
var style = (browser.noFlex || browser.firefox) ? 'max-height:400px;' : '';
// Admittedly a hack but right now the scrollbar is being factored into the width which is causing truncation
if (options.items.length > 20) {
2016-08-11 20:23:12 -07:00
var minWidth = dom.getWindowSize().innerWidth >= 300 ? 240 : 200;
2016-07-01 19:16:05 -07:00
style += "min-width:" + minWidth + "px;";
2016-06-29 12:25:09 -07:00
}
2016-02-21 13:39:14 -07:00
2016-05-21 19:29:03 -07:00
var i, length, option;
var renderIcon = false;
for (i = 0, length = options.items.length; i < length; i++) {
2016-04-10 21:24:16 -07:00
2016-05-21 19:29:03 -07:00
option = options.items[i];
2016-06-09 23:54:03 -07:00
option.icon = option.selected ? 'check' : null;
2016-05-21 19:29:03 -07:00
2016-06-09 23:54:03 -07:00
if (option.icon) {
2016-05-21 19:29:03 -07:00
renderIcon = true;
}
}
2016-02-21 13:39:14 -07:00
// If any items have an icon, give them all an icon just to make sure they're all lined up evenly
2016-05-21 19:29:03 -07:00
var center = options.title && (!renderIcon /*|| itemsWithIcons.length != options.items.length*/);
2016-02-21 13:39:14 -07:00
if (center) {
2016-08-01 22:55:52 -07:00
html += '<div class="actionSheetContent actionSheetContent-centered">';
} else {
html += '<div class="actionSheetContent">';
2016-02-21 13:39:14 -07:00
}
2016-08-01 22:55:52 -07:00
if (options.title) {
if (layoutManager.tv) {
html += '<h1 class="actionSheetTitle">';
html += options.title;
html += '</h1>';
} else {
html += '<h2 class="actionSheetTitle">';
html += options.title;
html += '</h2>';
}
}
if (options.text) {
html += '<p class="actionSheetText">';
html += options.text;
html += '</p>';
}
html += '<div class="actionSheetScroller ' + scrollType + '" style="' + style + '">';
2016-08-09 11:22:19 -07:00
var menuItemClass = browser.noFlex || browser.firefox ? 'actionSheetMenuItem actionSheetMenuItem-noflex' : 'actionSheetMenuItem';
2016-02-21 13:39:14 -07:00
2016-05-21 19:29:03 -07:00
for (i = 0, length = options.items.length; i < length; i++) {
2016-02-21 13:39:14 -07:00
2016-05-21 19:29:03 -07:00
option = options.items[i];
2016-02-21 13:39:14 -07:00
var autoFocus = option.selected ? ' autoFocus' : '';
2016-08-09 11:22:19 -07:00
html += '<button' + autoFocus + ' is="emby-button" type="button" class="' + menuItemClass + '" data-id="' + (option.id || option.value) + '">';
2016-02-21 13:39:14 -07:00
2016-06-09 23:54:03 -07:00
if (option.icon) {
html += '<i class="actionSheetItemIcon md-icon">' + option.icon + '</i>';
2016-02-21 13:39:14 -07:00
}
else if (renderIcon && !center) {
2016-06-09 23:54:03 -07:00
html += '<i class="actionSheetItemIcon md-icon" style="visibility:hidden;">check</i>';
2016-02-21 13:39:14 -07:00
}
2016-05-21 19:29:03 -07:00
html += '<div class="actionSheetItemText">' + (option.name || option.textContent || option.innerText) + '</div>';
2016-08-09 11:22:19 -07:00
html += '</button>';
2016-02-21 13:39:14 -07:00
}
if (options.showCancel) {
html += '<div class="buttons">';
2016-06-04 17:17:35 -07:00
html += '<button is="emby-button" type="button" class="btnCancel">' + globalize.translate('sharedcomponents#ButtonCancel') + '</button>';
2016-02-21 13:39:14 -07:00
html += '</div>';
}
html += '</div>';
dlg.innerHTML = html;
2016-02-29 09:23:30 -07:00
if (layoutManager.tv) {
2016-07-26 22:19:56 -07:00
centerFocus(dlg.querySelector('.actionSheetScroller'), false, true);
2016-02-29 09:23:30 -07:00
}
2016-04-14 09:30:37 -07:00
if (options.showCancel) {
dlg.querySelector('.btnCancel').addEventListener('click', function () {
dialogHelper.close(dlg);
});
}
2016-02-21 13:39:14 -07:00
document.body.appendChild(dlg);
// Seeing an issue in some non-chrome browsers where this is requiring a double click
//var eventName = browser.firefox ? 'mousedown' : 'click';
2016-05-22 09:47:14 -07:00
var selectedId;
2016-02-21 13:39:14 -07:00
2016-05-22 09:47:14 -07:00
dlg.addEventListener('click', function (e) {
2016-02-21 13:39:14 -07:00
2016-07-18 11:58:17 -07:00
var actionSheetMenuItem = dom.parentWithClass(e.target, 'actionSheetMenuItem');
2016-02-21 13:39:14 -07:00
2016-05-22 09:47:14 -07:00
if (actionSheetMenuItem) {
selectedId = actionSheetMenuItem.getAttribute('data-id');
dialogHelper.close(dlg);
}
2016-02-21 13:39:14 -07:00
2016-05-22 09:47:14 -07:00
});
2016-02-21 13:39:14 -07:00
2016-07-19 23:23:18 -07:00
var timeout;
if (options.timeout) {
timeout = setTimeout(function () {
dialogHelper.close(dlg);
}, options.timeout);
}
2016-05-22 09:47:14 -07:00
return new Promise(function (resolve, reject) {
2016-02-21 13:39:14 -07:00
2016-05-22 09:47:14 -07:00
dlg.addEventListener('close', function () {
2016-02-21 13:39:14 -07:00
2016-07-26 22:19:56 -07:00
if (layoutManager.tv) {
centerFocus(dlg.querySelector('.actionSheetScroller'), false, false);
}
2016-07-19 23:23:18 -07:00
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
2016-05-23 10:54:38 -07:00
if (selectedId != null) {
2016-05-22 09:47:14 -07:00
if (options.callback) {
options.callback(selectedId);
}
2016-02-21 13:39:14 -07:00
2016-05-22 09:47:14 -07:00
resolve(selectedId);
2016-07-19 23:23:18 -07:00
} else {
reject();
2016-02-21 13:39:14 -07:00
}
});
2016-03-23 12:03:17 -07:00
dialogHelper.open(dlg);
2016-03-13 00:34:17 -07:00
2016-04-10 21:24:16 -07:00
var pos = options.positionTo ? getPosition(options, dlg) : null;
2016-03-13 00:34:17 -07:00
if (pos) {
dlg.style.position = 'fixed';
2016-03-22 10:46:57 -07:00
dlg.style.margin = 0;
2016-03-13 00:34:17 -07:00
dlg.style.left = pos.left + 'px';
dlg.style.top = pos.top + 'px';
}
2016-02-21 13:39:14 -07:00
});
}
return {
show: show
};
});