2016-02-04 21:56:34 -07:00
|
|
|
|
define(['paperdialoghelper', 'browser', 'paper-menu', 'paper-dialog', 'scale-up-animation', 'fade-out-animation'], function (paperDialogHelper, browser) {
|
2015-06-18 21:23:55 -07:00
|
|
|
|
|
2016-01-30 21:04:00 -07:00
|
|
|
|
function parentWithClass(elem, className) {
|
2015-06-18 21:23:55 -07:00
|
|
|
|
|
2016-01-30 21:04:00 -07:00
|
|
|
|
while (!elem.classList || !elem.classList.contains(className)) {
|
|
|
|
|
elem = elem.parentNode;
|
|
|
|
|
|
|
|
|
|
if (!elem) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return elem;
|
2015-12-14 08:43:03 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-30 21:04:00 -07:00
|
|
|
|
function show(options) {
|
2015-12-14 08:43:03 -07:00
|
|
|
|
|
2015-10-18 14:41:39 -07:00
|
|
|
|
// items
|
|
|
|
|
// positionTo
|
|
|
|
|
// showCancel
|
|
|
|
|
// title
|
|
|
|
|
var html = '';
|
2015-06-19 09:36:51 -07:00
|
|
|
|
|
2016-01-30 21:04:00 -07:00
|
|
|
|
html += '<div style="margin:0;padding:.8em 1em;">';
|
|
|
|
|
|
2015-10-18 14:41:39 -07:00
|
|
|
|
var windowHeight = $(window).height();
|
2015-12-14 08:43:03 -07:00
|
|
|
|
var pos;
|
2015-06-20 17:49:42 -07:00
|
|
|
|
|
2015-10-18 14:41:39 -07:00
|
|
|
|
// If the window height is under a certain amount, don't bother trying to position
|
|
|
|
|
// based on an element.
|
|
|
|
|
if (options.positionTo && windowHeight >= 540) {
|
2015-06-19 15:01:47 -07:00
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
pos = $(options.positionTo).offset();
|
2015-06-19 15:01:47 -07:00
|
|
|
|
|
2015-10-18 14:41:39 -07:00
|
|
|
|
pos.top += $(options.positionTo).innerHeight() / 2;
|
|
|
|
|
pos.left += $(options.positionTo).innerWidth() / 2;
|
2015-06-19 15:01:47 -07:00
|
|
|
|
|
2015-10-18 14:41:39 -07:00
|
|
|
|
// Account for margins
|
|
|
|
|
pos.top -= 24;
|
|
|
|
|
pos.left -= 24;
|
2015-06-19 15:01:47 -07:00
|
|
|
|
|
2015-10-18 14:41:39 -07:00
|
|
|
|
// Account for popup size - we can't predict this yet so just estimate
|
|
|
|
|
pos.top -= (55 * options.items.length) / 2;
|
|
|
|
|
pos.left -= 80;
|
2015-06-19 15:01:47 -07:00
|
|
|
|
|
2015-10-18 14:41:39 -07:00
|
|
|
|
// Account for scroll position
|
|
|
|
|
pos.top -= $(window).scrollTop();
|
|
|
|
|
pos.left -= $(window).scrollLeft();
|
2015-06-19 15:01:47 -07:00
|
|
|
|
|
2015-10-18 14:41:39 -07:00
|
|
|
|
// 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);
|
2015-06-19 15:01:47 -07:00
|
|
|
|
|
2015-10-18 14:41:39 -07:00
|
|
|
|
// Do some boundary checking
|
|
|
|
|
pos.top = Math.max(pos.top, 0);
|
|
|
|
|
pos.left = Math.max(pos.left, 0);
|
|
|
|
|
}
|
2015-06-19 15:01:47 -07:00
|
|
|
|
|
2015-10-18 14:41:39 -07:00
|
|
|
|
if (options.title) {
|
2015-12-19 21:35:22 -07:00
|
|
|
|
html += '<h3>';
|
2015-10-18 14:41:39 -07:00
|
|
|
|
html += options.title;
|
2015-12-19 21:35:22 -07:00
|
|
|
|
html += '</h3>';
|
2015-10-18 14:41:39 -07:00
|
|
|
|
}
|
2015-06-19 09:36:51 -07:00
|
|
|
|
|
2016-01-05 09:48:51 -07:00
|
|
|
|
var itemsWithIcons = options.items.filter(function (o) {
|
2015-10-18 14:41:39 -07:00
|
|
|
|
return o.ironIcon;
|
2015-12-19 21:35:22 -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 = itemsWithIcons.length;
|
|
|
|
|
var center = options.title && (!itemsWithIcons.length || itemsWithIcons.length != options.items.length);
|
2015-06-27 12:53:36 -07:00
|
|
|
|
|
2015-12-19 21:35:22 -07:00
|
|
|
|
if (center) {
|
2015-12-14 08:43:03 -07:00
|
|
|
|
html += '<paper-menu style="text-align:center;">';
|
|
|
|
|
} else {
|
|
|
|
|
html += '<paper-menu>';
|
|
|
|
|
}
|
2015-10-18 14:41:39 -07:00
|
|
|
|
for (var i = 0, length = options.items.length; i < length; i++) {
|
2015-06-19 09:36:51 -07:00
|
|
|
|
|
2015-10-18 14:41:39 -07:00
|
|
|
|
var option = options.items[i];
|
2015-06-19 09:36:51 -07:00
|
|
|
|
|
2015-10-18 14:41:39 -07:00
|
|
|
|
html += '<paper-menu-item class="actionSheetMenuItem" data-id="' + option.id + '" style="display:block;">';
|
2015-06-19 15:01:47 -07:00
|
|
|
|
|
2015-10-18 14:41:39 -07:00
|
|
|
|
if (option.ironIcon) {
|
2015-12-19 21:35:22 -07:00
|
|
|
|
if (center) {
|
|
|
|
|
html += '<iron-icon style="margin-right:.5em;" icon="' + option.ironIcon + '"></iron-icon>';
|
|
|
|
|
} else {
|
|
|
|
|
html += '<iron-icon icon="' + option.ironIcon + '"></iron-icon>';
|
|
|
|
|
}
|
2015-06-19 09:36:51 -07:00
|
|
|
|
}
|
2015-12-19 21:35:22 -07:00
|
|
|
|
else if (renderIcon && !center) {
|
2015-10-18 14:41:39 -07:00
|
|
|
|
html += '<iron-icon></iron-icon>';
|
2015-06-26 20:27:38 -07:00
|
|
|
|
}
|
2015-10-18 14:41:39 -07:00
|
|
|
|
html += '<span>' + option.name + '</span>';
|
|
|
|
|
html += '</paper-menu-item>';
|
|
|
|
|
}
|
|
|
|
|
html += '</paper-menu>';
|
2016-01-30 21:04:00 -07:00
|
|
|
|
html += '</div>';
|
2015-10-18 14:41:39 -07:00
|
|
|
|
|
|
|
|
|
if (options.showCancel) {
|
|
|
|
|
html += '<div class="buttons">';
|
|
|
|
|
html += '<paper-button dialog-dismiss>' + Globalize.translate('ButtonCancel') + '</paper-button>';
|
|
|
|
|
html += '</div>';
|
|
|
|
|
}
|
2015-06-19 09:36:51 -07:00
|
|
|
|
|
2016-01-30 21:04:00 -07:00
|
|
|
|
var dlg = paperDialogHelper.createDialog({
|
|
|
|
|
modal: false,
|
|
|
|
|
entryAnimationDuration: 160,
|
2016-02-01 10:02:46 -07:00
|
|
|
|
exitAnimationDuration: 200,
|
|
|
|
|
enableHistory: options.enableHistory
|
2016-01-30 21:04:00 -07:00
|
|
|
|
});
|
2015-12-14 08:43:03 -07:00
|
|
|
|
dlg.innerHTML = html;
|
2015-06-19 09:36:51 -07:00
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
if (pos) {
|
|
|
|
|
dlg.style.position = 'fixed';
|
|
|
|
|
dlg.style.left = pos.left + 'px';
|
|
|
|
|
dlg.style.top = pos.top + 'px';
|
|
|
|
|
}
|
2016-01-28 14:08:09 -07:00
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
document.body.appendChild(dlg);
|
|
|
|
|
|
2016-01-30 21:04:00 -07:00
|
|
|
|
paperDialogHelper.open(dlg);
|
2015-06-19 09:36:51 -07:00
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
// Has to be assigned a z-index after the call to .open()
|
|
|
|
|
dlg.addEventListener('iron-overlay-closed', function () {
|
|
|
|
|
dlg.parentNode.removeChild(dlg);
|
|
|
|
|
});
|
2015-06-19 09:36:51 -07:00
|
|
|
|
|
2015-12-14 08:43:03 -07:00
|
|
|
|
// Seeing an issue in some non-chrome browsers where this is requiring a double click
|
2016-02-04 21:56:34 -07:00
|
|
|
|
var eventName = browser.firefox ? 'mousedown' : 'click';
|
2015-10-14 19:55:19 -07:00
|
|
|
|
|
2016-01-28 21:18:31 -07:00
|
|
|
|
dlg.addEventListener(eventName, function (e) {
|
2015-06-19 09:36:51 -07:00
|
|
|
|
|
2016-01-28 21:18:31 -07:00
|
|
|
|
var target = parentWithClass(e.target, 'actionSheetMenuItem');
|
|
|
|
|
if (target) {
|
2016-01-30 21:04:00 -07:00
|
|
|
|
|
2016-01-28 21:18:31 -07:00
|
|
|
|
var selectedId = target.getAttribute('data-id');
|
2015-06-19 15:01:47 -07:00
|
|
|
|
|
2016-01-30 21:04:00 -07:00
|
|
|
|
paperDialogHelper.close(dlg);
|
|
|
|
|
|
2016-01-28 21:18:31 -07:00
|
|
|
|
// Add a delay here to allow the click animation to finish, for nice effect
|
|
|
|
|
setTimeout(function () {
|
2015-06-19 15:01:47 -07:00
|
|
|
|
|
2016-01-28 21:18:31 -07:00
|
|
|
|
if (options.callback) {
|
|
|
|
|
options.callback(selectedId);
|
|
|
|
|
}
|
2015-06-19 15:01:47 -07:00
|
|
|
|
|
2016-01-28 21:18:31 -07:00
|
|
|
|
}, 100);
|
|
|
|
|
}
|
2015-12-14 08:43:03 -07:00
|
|
|
|
});
|
2015-06-18 21:23:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-30 21:04:00 -07:00
|
|
|
|
return {
|
2015-06-18 21:23:55 -07:00
|
|
|
|
show: show
|
|
|
|
|
};
|
2016-01-30 21:04:00 -07:00
|
|
|
|
});
|