jellyfin-web/dashboard-ui/scripts/actionsheet.js

131 lines
4.3 KiB
JavaScript
Raw Normal View History

2015-06-18 21:23:55 -07:00
(function () {
function show(options) {
2015-06-19 15:01:47 -07:00
require(['paperbuttonstyle'], function () {
2015-06-19 09:36:51 -07:00
// items
// positionTo
// showCancel
// title
var id = 'dlg' + new Date().getTime();
var html = '';
2015-06-19 15:01:47 -07:00
var style = "";
2015-06-20 17:49:42 -07:00
var windowHeight = $(window).height();
// If the window height is under a certain amount, don't bother trying to position
// based on an element.
2015-06-23 21:38:46 -07:00
if (options.positionTo && windowHeight >= 540) {
2015-06-19 15:01:47 -07:00
var pos = $(options.positionTo).offset();
2015-06-29 11:45:42 -07:00
pos.top += $(options.positionTo).innerHeight() / 2;
pos.left += $(options.positionTo).innerWidth() / 2;
2015-06-19 15:01:47 -07:00
// Account for margins
pos.top -= 24;
pos.left -= 24;
// Account for popup size - we can't predict this yet so just estimate
2015-06-20 17:49:42 -07:00
pos.top -= (55 * options.items.length) / 2;
2015-06-19 15:01:47 -07:00
pos.left -= 80;
// Account for scroll position
pos.top -= $(window).scrollTop();
pos.left -= $(window).scrollLeft();
// Avoid showing too close to the bottom
pos.top = Math.min(pos.top, $(window).height() - 300);
pos.left = Math.min(pos.left, $(window).width() - 300);
// Do some boundary checking
pos.top = Math.max(pos.top, 0);
pos.left = Math.max(pos.left, 0);
style += 'position:fixed;top:' + pos.top + 'px;left:' + pos.left + 'px';
}
html += '<paper-dialog id="' + id + '" entry-animation="fade-in-animation" exit-animation="fade-out-animation" with-backdrop style="' + style + '">';
2015-06-19 09:36:51 -07:00
if (options.title) {
html += '<h2>';
html += options.title;
html += '</h2>';
}
2015-06-20 17:49:42 -07:00
// There seems to be a bug with this in safari causing it to immediately roll up to 0 height
var isScrollable = !$.browser.safari;
if (isScrollable) {
html += '<paper-dialog-scrollable>';
}
2015-06-27 12:53:36 -07:00
// If any items have an icon, give them all an icon just to make sure they're all lined up evenly
var renderIcon = options.items.filter(function (o) {
return o.ironIcon;
}).length;
2015-06-19 09:36:51 -07:00
for (var i = 0, length = options.items.length; i < length; i++) {
var option = options.items[i];
2015-06-19 15:01:47 -07:00
html += '<paper-button class="block menuButton ripple btnOption" data-id="' + option.id + '" style="margin:0;">';
if (option.ironIcon) {
html += '<iron-icon icon="' + option.ironIcon + '"></iron-icon>';
}
2015-06-27 12:53:36 -07:00
else if (renderIcon) {
html += '<iron-icon></iron-icon>';
}
2015-06-19 11:34:21 -07:00
html += '<span>' + option.name + '</span>';
html += '</paper-button>';
2015-06-19 09:36:51 -07:00
}
2015-06-26 20:27:38 -07:00
if (isScrollable) {
html += '</paper-dialog-scrollable>';
}
2015-06-18 21:23:55 -07:00
2015-06-19 09:36:51 -07:00
if (options.showCancel) {
html += '<div class="buttons">';
html += '<paper-button dialog-dismiss>' + Globalize.translate('ButtonCancel') + '</paper-button>';
html += '</div>';
}
2015-06-26 20:27:38 -07:00
html += '</paper-dialog>';
2015-06-19 09:36:51 -07:00
2015-06-20 17:49:42 -07:00
$(document.body).append(html);
2015-06-19 09:36:51 -07:00
setTimeout(function () {
var dlg = document.getElementById(id);
2015-06-23 21:38:46 -07:00
2015-06-19 09:36:51 -07:00
dlg.open();
// Has to be assigned a z-index after the call to .open()
2015-06-27 12:53:36 -07:00
$(dlg).on('iron-overlay-closed', function () {
$(this).remove();
});
2015-06-19 09:36:51 -07:00
$('.btnOption', dlg).on('click', function () {
2015-06-19 15:01:47 -07:00
var selectedId = this.getAttribute('data-id');
// Add a delay here to allow the click animation to finish, for nice effect
setTimeout(function () {
dlg.close();
if (options.callback) {
options.callback(selectedId);
}
}, 100);
2015-06-19 09:36:51 -07:00
});
}, 100);
});
2015-06-18 21:23:55 -07:00
}
2015-06-19 09:36:51 -07:00
window.ActionSheetElement = {
2015-06-18 21:23:55 -07:00
show: show
};
})();