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

327 lines
10 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-10-01 23:46:32 -07:00
'use strict';
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,
2016-09-25 11:39:13 -07:00
left: box.left,
width: box.width,
height: box.height
2016-03-13 00:34:17 -07:00
};
}
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-10-01 23:46:32 -07:00
if (options.positionY !== 'top') {
2016-09-25 11:39:13 -07:00
pos.top += (pos.height || 0) / 2;
2016-08-31 12:17:11 -07:00
}
2016-09-25 11:39:13 -07:00
pos.left += (pos.width || 0) / 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
2016-08-31 12:17:11 -07:00
pos.top += (options.offsetTop || 0);
pos.left += (options.offsetLeft || 0);
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,
2016-10-14 09:22:04 -07:00
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-10-14 22:32:06 -07:00
dialogOptions.entryAnimation = options.entryAnimation;
dialogOptions.exitAnimation = options.exitAnimation;
dialogOptions.entryAnimationDuration = options.entryAnimationDuration || 140;
2016-10-16 10:11:32 -07:00
dialogOptions.exitAnimationDuration = options.exitAnimationDuration || 160;
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-09-22 23:57:24 -07:00
} else {
dlg.classList.add('actionsheet-not-fullscreen');
2016-08-04 19:03:15 -07:00
}
2016-09-03 10:58:23 -07:00
var extraSpacing = !layoutManager.tv;
if (extraSpacing) {
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');
2016-08-31 12:17:11 -07:00
if (options.dialogClass) {
dlg.classList.add(options.dialogClass);
}
2016-02-21 13:39:14 -07:00
var html = '';
2016-06-09 23:54:03 -07:00
var scrollType = layoutManager.desktop ? 'smoothScrollY' : 'hiddenScrollY';
var style = (browser.firefox) ? 'max-height:400px;' : '';
2016-06-29 12:25:09 -07:00
// 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
2016-09-07 14:08:40 -07:00
if (layoutManager.tv) {
html += '<button is="paper-icon-button-light" class="btnCloseActionSheet" tabindex="-1"><i class="md-icon">&#xE5C4;</i></button>';
}
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 + '">';
var menuItemClass = browser.firefox ? 'actionSheetMenuItem actionSheetMenuItem-noflex' : 'actionSheetMenuItem';
2016-02-21 13:39:14 -07:00
2016-08-31 12:17:11 -07:00
if (options.menuItemClass) {
menuItemClass += ' ' + options.menuItemClass;
}
2016-09-03 10:58:23 -07:00
var actionSheetItemTextClass = 'actionSheetItemText';
if (extraSpacing) {
actionSheetItemTextClass += ' actionSheetItemText-extraspacing';
}
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-09-03 10:58:23 -07:00
html += '<div class="' + actionSheetItemTextClass + '">' + (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-09-07 14:08:40 -07:00
html += '<button is="emby-button" type="button" class="btnCloseActionSheet">' + 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-09-07 14:08:40 -07:00
var btnCloseActionSheet = dlg.querySelector('.btnCloseActionSheet');
if (btnCloseActionSheet) {
dlg.querySelector('.btnCloseActionSheet').addEventListener('click', function () {
2016-04-14 09:30:37 -07:00
dialogHelper.close(dlg);
});
}
2016-02-21 13:39:14 -07:00
// 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-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-09-04 22:39:14 -07:00
var isResolved;
dlg.addEventListener('click', function (e) {
var actionSheetMenuItem = dom.parentWithClass(e.target, 'actionSheetMenuItem');
if (actionSheetMenuItem) {
selectedId = actionSheetMenuItem.getAttribute('data-id');
if (options.resolveOnClick) {
2016-09-08 13:32:30 -07:00
2016-09-04 22:39:14 -07:00
resolve(selectedId);
isResolved = true;
}
dialogHelper.close(dlg);
}
});
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-09-04 22:39:14 -07:00
if (!isResolved) {
if (selectedId != null) {
if (options.callback) {
options.callback(selectedId);
}
2016-02-21 13:39:14 -07:00
2016-09-04 22:39:14 -07:00
resolve(selectedId);
} 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-10-16 10:11:32 -07:00
// Make sure the above open has completed so that we can query offsetWidth and offsetHeight
// This was needed in safari, but in chrome this is causing the dialog to change position while animating
var setPositions = function () {
var pos = options.positionTo && dialogOptions.size !== 'fullscreen' ? getPosition(options, dlg) : null;
2016-04-10 21:24:16 -07:00
2016-10-16 10:11:32 -07:00
if (pos) {
dlg.style.position = 'fixed';
dlg.style.margin = 0;
dlg.style.left = pos.left + 'px';
dlg.style.top = pos.top + 'px';
}
};
if (browser.safari) {
setTimeout(setPositions, 0);
} else {
setPositions();
2016-03-13 00:34:17 -07:00
}
2016-02-21 13:39:14 -07:00
});
}
return {
show: show
};
});