@nativescript-community/ui-popover
Version:
Popover plugn
69 lines • 2.92 kB
JavaScript
import { showPopover } from '..';
import { View } from '@nativescript/core';
const modalStack = [];
let sequentialCounter = 0;
function serializeModalOptions(options) {
if (process.env.NODE_ENV === 'production') {
return null;
}
const allowed = ['anchor', 'vertPos', 'horPos', 'x', 'y', 'fitInScreen'];
return (Object.keys(options)
.filter((key) => allowed.includes(key))
.map((key) => `${key}: ${options[key]}`)
.concat(`uid: ${++sequentialCounter}`)
.join(', ') + '_Popover');
}
const PopoverPlugin = {
install(Vue) {
Vue.prototype.$showPopover = function (component, options) {
return new Promise((resolve) => {
const resolved = false;
let navEntryInstance = new Vue({
name: 'PopoverEntry',
parent: this.$root,
render: (h) => h(component, {
props: options.props,
key: serializeModalOptions(options)
})
});
const anchorView = options.anchor instanceof View ? options.anchor : options.anchor.nativeView;
navEntryInstance.$mount();
// if (!(modalView instanceof GestureRootView)) {
// const gestureView = new GestureRootView();
// gestureView.height = modalView.height;
// gestureView.addChild(modalView);
// modalView = gestureView;
// }
return new Promise(async (resolve, reject) => {
let resolved = false;
const closeCallback = (result) => {
if (resolved)
return;
resolved = true;
modalStack.pop();
options.onDismiss?.();
resolve(result);
navEntryInstance.$emit('popover:close');
navEntryInstance.$destroy(); // don't let an exception in destroy kill the promise callback
navEntryInstance = null;
};
try {
modalStack.push(showPopover(navEntryInstance.nativeView, { ...options, anchor: anchorView, onDismiss: closeCallback }));
}
catch (err) {
console.error(err);
reject(err);
}
});
});
};
Vue.prototype.$closePopover = async function (result) {
const modalPageInstanceInfo = modalStack[modalStack.length - 1];
if (modalPageInstanceInfo) {
return modalPageInstanceInfo.close(result);
}
};
}
};
export default PopoverPlugin;
//# sourceMappingURL=index.js.map