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

248 lines
7.0 KiB
JavaScript
Raw Normal View History

2016-05-12 12:21:43 -07:00
define(['dialogHelper', 'layoutManager', 'globalize', 'paper-button', 'css!./actionsheet', 'html!./../icons/nav.html'], function (dialogHelper, layoutManager, globalize) {
2016-02-21 13:39:14 -07:00
function parentWithClass(elem, className) {
while (!elem.classList || !elem.classList.contains(className)) {
elem = elem.parentNode;
if (!elem) {
return null;
}
}
return elem;
}
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-03-13 00:34:17 -07:00
var windowHeight = window.innerHeight;
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-04-10 21:24:16 -07:00
// Account for popup size
pos.top -= ((dlg.offsetHeight || 300) / 2);
pos.left -= ((dlg.offsetWidth || 160) / 2);
2016-02-21 13:39:14 -07:00
// Avoid showing too close to the bottom
pos.top = Math.min(pos.top, windowHeight - 300);
2016-03-13 00:34:17 -07:00
pos.left = Math.min(pos.left, window.innerWidth - 300);
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-02-29 09:23:30 -07:00
function addCenterFocus(dlg) {
require(['scrollHelper'], function (scrollHelper) {
scrollHelper.centerFocus.on(dlg.querySelector('.actionSheetScroller'), false);
});
}
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;
if (layoutManager.tv) {
dialogOptions.size = 'fullscreen';
backButton = true;
dialogOptions.autoFocus = true;
} else {
dialogOptions.modal = false;
dialogOptions.entryAnimationDuration = 160;
dialogOptions.exitAnimationDuration = 200;
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-04-10 21:24:16 -07:00
if (!layoutManager.tv) {
dlg.classList.add('extraSpacing');
}
2016-02-21 13:39:14 -07:00
dlg.classList.add('actionSheet');
var html = '';
html += '<div class="actionSheetContent">';
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>';
}
}
html += '<div class="actionSheetScroller">';
2016-04-10 21:24:16 -07:00
options.items.forEach(function (o) {
2016-05-11 15:08:19 -07:00
o.ironIcon = o.selected ? 'nav:check' : null;
2016-04-10 21:24:16 -07:00
});
2016-02-21 13:39:14 -07:00
var itemsWithIcons = options.items.filter(function (o) {
return o.ironIcon;
});
// If any items have an icon, give them all an icon just to make sure they're all lined up evenly
var renderIcon = itemsWithIcons.length;
2016-02-22 12:32:35 -07:00
var center = options.title && (!itemsWithIcons.length /*|| itemsWithIcons.length != options.items.length*/);
2016-02-21 13:39:14 -07:00
if (center) {
dlg.classList.add('centered');
}
var enablePaperMenu = !layoutManager.tv;
2016-04-16 12:46:47 -07:00
enablePaperMenu = false;
2016-02-21 13:39:14 -07:00
var itemTagName = 'paper-button';
if (enablePaperMenu) {
html += '<paper-menu>';
itemTagName = 'paper-menu-item';
}
for (var i = 0, length = options.items.length; i < length; i++) {
var option = options.items[i];
var autoFocus = option.selected ? ' autoFocus' : '';
2016-04-16 12:22:09 -07:00
html += '<' + itemTagName + autoFocus + ' class="actionSheetMenuItem" data-id="' + option.id + '">';
2016-02-21 13:39:14 -07:00
if (option.ironIcon) {
html += '<iron-icon class="actionSheetItemIcon" icon="' + option.ironIcon + '"></iron-icon>';
}
else if (renderIcon && !center) {
2016-02-21 14:09:38 -07:00
html += '<iron-icon class="actionSheetItemIcon"></iron-icon>';
2016-02-21 13:39:14 -07:00
}
2016-04-16 12:22:09 -07:00
html += '<div class="actionSheetItemText">' + option.name + '</div>';
2016-02-21 13:39:14 -07:00
html += '</' + itemTagName + '>';
}
if (enablePaperMenu) {
html += '</paper-menu>';
}
if (options.showCancel) {
html += '<div class="buttons">';
2016-05-12 12:21:43 -07:00
html += '<paper-button class="btnCancel">' + globalize.translate('sharedcomponents#ButtonCancel') + '</paper-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) {
addCenterFocus(dlg);
}
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';
var eventName = 'click';
return new Promise(function (resolve, reject) {
dlg.addEventListener(eventName, function (e) {
var actionSheetMenuItem = parentWithClass(e.target, 'actionSheetMenuItem');
if (actionSheetMenuItem) {
var selectedId = actionSheetMenuItem.getAttribute('data-id');
2016-03-23 12:03:17 -07:00
dialogHelper.close(dlg);
2016-02-21 13:39:14 -07:00
// Add a delay here to allow the click animation to finish, for nice effect
setTimeout(function () {
if (options.callback) {
options.callback(selectedId);
}
resolve(selectedId);
}, 100);
}
});
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
};
});