UNPKG

primevue

Version:

PrimeVue is an open source UI library for Vue featuring a rich set of 80+ components, a theme designer, various theme alternatives such as Material, Bootstrap, Tailwind, premium templates and professional support. In addition, it integrates with PrimeBloc

611 lines (602 loc) 23.2 kB
import { cn } from '@primeuix/utils'; import { isTouchDevice, absolutePosition, getOuterWidth, addStyle, focus, find, findSingle } from '@primeuix/utils/dom'; import { ZIndex } from '@primeuix/utils/zindex'; import { ConnectedOverlayScrollHandler } from '@primevue/core/utils'; import OverlayEventBus from 'primevue/overlayeventbus'; import Portal from 'primevue/portal'; import BaseComponent from '@primevue/core/basecomponent'; import MenuStyle from 'primevue/menu/style'; import { resolve } from '@primeuix/utils/object'; import Ripple from 'primevue/ripple'; import { mergeProps, resolveDirective, createElementBlock, createCommentVNode, openBlock, createElementVNode, withDirectives, createBlock, resolveDynamicComponent, normalizeClass, toDisplayString, resolveComponent, withCtx, createVNode, Transition, renderSlot, Fragment, renderList, createTextVNode } from 'vue'; var script$2 = { name: 'BaseMenu', "extends": BaseComponent, props: { popup: { type: Boolean, "default": false }, model: { type: Array, "default": null }, appendTo: { type: [String, Object], "default": 'body' }, autoZIndex: { type: Boolean, "default": true }, baseZIndex: { type: Number, "default": 0 }, tabindex: { type: Number, "default": 0 }, ariaLabel: { type: String, "default": null }, ariaLabelledby: { type: String, "default": null } }, style: MenuStyle, provide: function provide() { return { $pcMenu: this, $parentInstance: this }; } }; var script$1 = { name: 'Menuitem', hostName: 'Menu', "extends": BaseComponent, inheritAttrs: false, emits: ['item-click', 'item-mousemove'], props: { item: null, templates: null, id: null, focusedOptionId: null, index: null }, methods: { getItemProp: function getItemProp(processedItem, name) { return processedItem && processedItem.item ? resolve(processedItem.item[name]) : undefined; }, getPTOptions: function getPTOptions(key) { return this.ptm(key, { context: { item: this.item, index: this.index, focused: this.isItemFocused(), disabled: this.disabled() } }); }, isItemFocused: function isItemFocused() { return this.focusedOptionId === this.id; }, onItemClick: function onItemClick(event) { var command = this.getItemProp(this.item, 'command'); command && command({ originalEvent: event, item: this.item.item }); this.$emit('item-click', { originalEvent: event, item: this.item, id: this.id }); }, onItemMouseMove: function onItemMouseMove(event) { this.$emit('item-mousemove', { originalEvent: event, item: this.item, id: this.id }); }, visible: function visible() { return typeof this.item.visible === 'function' ? this.item.visible() : this.item.visible !== false; }, disabled: function disabled() { return typeof this.item.disabled === 'function' ? this.item.disabled() : this.item.disabled; }, label: function label() { return typeof this.item.label === 'function' ? this.item.label() : this.item.label; }, getMenuItemProps: function getMenuItemProps(item) { return { action: mergeProps({ "class": this.cx('itemLink'), tabindex: '-1' }, this.getPTOptions('itemLink')), icon: mergeProps({ "class": [this.cx('itemIcon'), item.icon] }, this.getPTOptions('itemIcon')), label: mergeProps({ "class": this.cx('itemLabel') }, this.getPTOptions('itemLabel')) }; } }, computed: { dataP: function dataP() { return cn({ focus: this.isItemFocused(), disabled: this.disabled() }); } }, directives: { ripple: Ripple } }; var _hoisted_1$1 = ["id", "aria-label", "aria-disabled", "data-p-focused", "data-p-disabled", "data-p"]; var _hoisted_2$1 = ["data-p"]; var _hoisted_3$1 = ["href", "target"]; var _hoisted_4 = ["data-p"]; var _hoisted_5 = ["data-p"]; function render$1(_ctx, _cache, $props, $setup, $data, $options) { var _directive_ripple = resolveDirective("ripple"); return $options.visible() ? (openBlock(), createElementBlock("li", mergeProps({ key: 0, id: $props.id, "class": [_ctx.cx('item'), $props.item["class"]], role: "menuitem", style: $props.item.style, "aria-label": $options.label(), "aria-disabled": $options.disabled(), "data-p-focused": $options.isItemFocused(), "data-p-disabled": $options.disabled() || false, "data-p": $options.dataP }, $options.getPTOptions('item')), [createElementVNode("div", mergeProps({ "class": _ctx.cx('itemContent'), onClick: _cache[0] || (_cache[0] = function ($event) { return $options.onItemClick($event); }), onMousemove: _cache[1] || (_cache[1] = function ($event) { return $options.onItemMouseMove($event); }), "data-p": $options.dataP }, $options.getPTOptions('itemContent')), [!$props.templates.item ? withDirectives((openBlock(), createElementBlock("a", mergeProps({ key: 0, href: $props.item.url, "class": _ctx.cx('itemLink'), target: $props.item.target, tabindex: "-1" }, $options.getPTOptions('itemLink')), [$props.templates.itemicon ? (openBlock(), createBlock(resolveDynamicComponent($props.templates.itemicon), { key: 0, item: $props.item, "class": normalizeClass(_ctx.cx('itemIcon')) }, null, 8, ["item", "class"])) : $props.item.icon ? (openBlock(), createElementBlock("span", mergeProps({ key: 1, "class": [_ctx.cx('itemIcon'), $props.item.icon], "data-p": $options.dataP }, $options.getPTOptions('itemIcon')), null, 16, _hoisted_4)) : createCommentVNode("", true), createElementVNode("span", mergeProps({ "class": _ctx.cx('itemLabel'), "data-p": $options.dataP }, $options.getPTOptions('itemLabel')), toDisplayString($options.label()), 17, _hoisted_5)], 16, _hoisted_3$1)), [[_directive_ripple]]) : $props.templates.item ? (openBlock(), createBlock(resolveDynamicComponent($props.templates.item), { key: 1, item: $props.item, label: $options.label(), props: $options.getMenuItemProps($props.item) }, null, 8, ["item", "label", "props"])) : createCommentVNode("", true)], 16, _hoisted_2$1)], 16, _hoisted_1$1)) : createCommentVNode("", true); } script$1.render = render$1; function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } var script = { name: 'Menu', "extends": script$2, inheritAttrs: false, emits: ['show', 'hide', 'focus', 'blur'], data: function data() { return { overlayVisible: false, focused: false, focusedOptionIndex: -1, selectedOptionIndex: -1 }; }, target: null, outsideClickListener: null, scrollHandler: null, resizeListener: null, container: null, list: null, mounted: function mounted() { if (!this.popup) { this.bindResizeListener(); this.bindOutsideClickListener(); } }, beforeUnmount: function beforeUnmount() { this.unbindResizeListener(); this.unbindOutsideClickListener(); if (this.scrollHandler) { this.scrollHandler.destroy(); this.scrollHandler = null; } this.target = null; if (this.container && this.autoZIndex) { ZIndex.clear(this.container); } this.container = null; }, methods: { itemClick: function itemClick(event) { var item = event.item; if (this.disabled(item)) { return; } if (item.command) { item.command(event); } if (this.overlayVisible) this.hide(); if (!this.popup && this.focusedOptionIndex !== event.id) { this.focusedOptionIndex = event.id; } }, itemMouseMove: function itemMouseMove(event) { if (this.focused) { this.focusedOptionIndex = event.id; } }, onListFocus: function onListFocus(event) { this.focused = true; !this.popup && this.changeFocusedOptionIndex(0); this.$emit('focus', event); }, onListBlur: function onListBlur(event) { this.focused = false; this.focusedOptionIndex = -1; this.$emit('blur', event); }, onListKeyDown: function onListKeyDown(event) { switch (event.code) { case 'ArrowDown': this.onArrowDownKey(event); break; case 'ArrowUp': this.onArrowUpKey(event); break; case 'Home': this.onHomeKey(event); break; case 'End': this.onEndKey(event); break; case 'Enter': case 'NumpadEnter': this.onEnterKey(event); break; case 'Space': this.onSpaceKey(event); break; case 'Escape': if (this.popup) { focus(this.target); this.hide(); } case 'Tab': this.overlayVisible && this.hide(); break; } }, onArrowDownKey: function onArrowDownKey(event) { var optionIndex = this.findNextOptionIndex(this.focusedOptionIndex); this.changeFocusedOptionIndex(optionIndex); event.preventDefault(); }, onArrowUpKey: function onArrowUpKey(event) { if (event.altKey && this.popup) { focus(this.target); this.hide(); event.preventDefault(); } else { var optionIndex = this.findPrevOptionIndex(this.focusedOptionIndex); this.changeFocusedOptionIndex(optionIndex); event.preventDefault(); } }, onHomeKey: function onHomeKey(event) { this.changeFocusedOptionIndex(0); event.preventDefault(); }, onEndKey: function onEndKey(event) { this.changeFocusedOptionIndex(find(this.container, 'li[data-pc-section="item"][data-p-disabled="false"]').length - 1); event.preventDefault(); }, onEnterKey: function onEnterKey(event) { var element = findSingle(this.list, "li[id=\"".concat("".concat(this.focusedOptionIndex), "\"]")); var anchorElement = element && findSingle(element, 'a[data-pc-section="itemlink"]'); this.popup && focus(this.target); anchorElement ? anchorElement.click() : element && element.click(); event.preventDefault(); }, onSpaceKey: function onSpaceKey(event) { this.onEnterKey(event); }, findNextOptionIndex: function findNextOptionIndex(index) { var links = find(this.container, 'li[data-pc-section="item"][data-p-disabled="false"]'); var matchedOptionIndex = _toConsumableArray(links).findIndex(function (link) { return link.id === index; }); return matchedOptionIndex > -1 ? matchedOptionIndex + 1 : 0; }, findPrevOptionIndex: function findPrevOptionIndex(index) { var links = find(this.container, 'li[data-pc-section="item"][data-p-disabled="false"]'); var matchedOptionIndex = _toConsumableArray(links).findIndex(function (link) { return link.id === index; }); return matchedOptionIndex > -1 ? matchedOptionIndex - 1 : 0; }, changeFocusedOptionIndex: function changeFocusedOptionIndex(index) { var links = find(this.container, 'li[data-pc-section="item"][data-p-disabled="false"]'); var order = index >= links.length ? links.length - 1 : index < 0 ? 0 : index; order > -1 && (this.focusedOptionIndex = links[order].getAttribute('id')); }, toggle: function toggle(event) { if (this.overlayVisible) this.hide();else this.show(event); }, show: function show(event) { this.overlayVisible = true; this.target = event.currentTarget; }, hide: function hide() { this.overlayVisible = false; this.target = null; }, onEnter: function onEnter(el) { addStyle(el, { position: 'absolute', top: '0' }); this.alignOverlay(); this.bindOutsideClickListener(); this.bindResizeListener(); this.bindScrollListener(); if (this.autoZIndex) { ZIndex.set('menu', el, this.baseZIndex + this.$primevue.config.zIndex.menu); } if (this.popup) { focus(this.list); } this.$emit('show'); }, onLeave: function onLeave() { this.unbindOutsideClickListener(); this.unbindResizeListener(); this.unbindScrollListener(); this.$emit('hide'); }, onAfterLeave: function onAfterLeave(el) { if (this.autoZIndex) { ZIndex.clear(el); } }, alignOverlay: function alignOverlay() { absolutePosition(this.container, this.target); var targetWidth = getOuterWidth(this.target); if (targetWidth > getOuterWidth(this.container)) { this.container.style.minWidth = getOuterWidth(this.target) + 'px'; } }, bindOutsideClickListener: function bindOutsideClickListener() { var _this = this; if (!this.outsideClickListener) { this.outsideClickListener = function (event) { var isOutsideContainer = _this.container && !_this.container.contains(event.target); var isOutsideTarget = !(_this.target && (_this.target === event.target || _this.target.contains(event.target))); if (_this.overlayVisible && isOutsideContainer && isOutsideTarget) { _this.hide(); } else if (!_this.popup && isOutsideContainer && isOutsideTarget) { _this.focusedOptionIndex = -1; } }; document.addEventListener('click', this.outsideClickListener, true); } }, unbindOutsideClickListener: function unbindOutsideClickListener() { if (this.outsideClickListener) { document.removeEventListener('click', this.outsideClickListener, true); this.outsideClickListener = null; } }, bindScrollListener: function bindScrollListener() { var _this2 = this; if (!this.scrollHandler) { this.scrollHandler = new ConnectedOverlayScrollHandler(this.target, function () { if (_this2.overlayVisible) { _this2.hide(); } }); } this.scrollHandler.bindScrollListener(); }, unbindScrollListener: function unbindScrollListener() { if (this.scrollHandler) { this.scrollHandler.unbindScrollListener(); } }, bindResizeListener: function bindResizeListener() { var _this3 = this; if (!this.resizeListener) { this.resizeListener = function () { if (_this3.overlayVisible && !isTouchDevice()) { _this3.hide(); } }; window.addEventListener('resize', this.resizeListener); } }, unbindResizeListener: function unbindResizeListener() { if (this.resizeListener) { window.removeEventListener('resize', this.resizeListener); this.resizeListener = null; } }, visible: function visible(item) { return typeof item.visible === 'function' ? item.visible() : item.visible !== false; }, disabled: function disabled(item) { return typeof item.disabled === 'function' ? item.disabled() : item.disabled; }, label: function label(item) { return typeof item.label === 'function' ? item.label() : item.label; }, onOverlayClick: function onOverlayClick(event) { OverlayEventBus.emit('overlay-click', { originalEvent: event, target: this.target }); }, containerRef: function containerRef(el) { this.container = el; }, listRef: function listRef(el) { this.list = el; } }, computed: { focusedOptionId: function focusedOptionId() { return this.focusedOptionIndex !== -1 ? this.focusedOptionIndex : null; }, dataP: function dataP() { return cn({ popup: this.popup }); } }, components: { PVMenuitem: script$1, Portal: Portal } }; var _hoisted_1 = ["id", "data-p"]; var _hoisted_2 = ["id", "tabindex", "aria-activedescendant", "aria-label", "aria-labelledby"]; var _hoisted_3 = ["id"]; function render(_ctx, _cache, $props, $setup, $data, $options) { var _component_PVMenuitem = resolveComponent("PVMenuitem"); var _component_Portal = resolveComponent("Portal"); return openBlock(), createBlock(_component_Portal, { appendTo: _ctx.appendTo, disabled: !_ctx.popup }, { "default": withCtx(function () { return [createVNode(Transition, mergeProps({ name: "p-connected-overlay", onEnter: $options.onEnter, onLeave: $options.onLeave, onAfterLeave: $options.onAfterLeave }, _ctx.ptm('transition')), { "default": withCtx(function () { return [(_ctx.popup ? $data.overlayVisible : true) ? (openBlock(), createElementBlock("div", mergeProps({ key: 0, ref: $options.containerRef, id: _ctx.$id, "class": _ctx.cx('root'), onClick: _cache[3] || (_cache[3] = function () { return $options.onOverlayClick && $options.onOverlayClick.apply($options, arguments); }), "data-p": $options.dataP }, _ctx.ptmi('root')), [_ctx.$slots.start ? (openBlock(), createElementBlock("div", mergeProps({ key: 0, "class": _ctx.cx('start') }, _ctx.ptm('start')), [renderSlot(_ctx.$slots, "start")], 16)) : createCommentVNode("", true), createElementVNode("ul", mergeProps({ ref: $options.listRef, id: _ctx.$id + '_list', "class": _ctx.cx('list'), role: "menu", tabindex: _ctx.tabindex, "aria-activedescendant": $data.focused ? $options.focusedOptionId : undefined, "aria-label": _ctx.ariaLabel, "aria-labelledby": _ctx.ariaLabelledby, onFocus: _cache[0] || (_cache[0] = function () { return $options.onListFocus && $options.onListFocus.apply($options, arguments); }), onBlur: _cache[1] || (_cache[1] = function () { return $options.onListBlur && $options.onListBlur.apply($options, arguments); }), onKeydown: _cache[2] || (_cache[2] = function () { return $options.onListKeyDown && $options.onListKeyDown.apply($options, arguments); }) }, _ctx.ptm('list')), [(openBlock(true), createElementBlock(Fragment, null, renderList(_ctx.model, function (item, i) { return openBlock(), createElementBlock(Fragment, { key: $options.label(item) + i.toString() }, [item.items && $options.visible(item) && !item.separator ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [item.items ? (openBlock(), createElementBlock("li", mergeProps({ key: 0, id: _ctx.$id + '_' + i, "class": [_ctx.cx('submenuLabel'), item["class"]], role: "none", ref_for: true }, _ctx.ptm('submenuLabel')), [renderSlot(_ctx.$slots, _ctx.$slots.submenulabel ? 'submenulabel' : 'submenuheader', { item: item }, function () { return [createTextVNode(toDisplayString($options.label(item)), 1)]; })], 16, _hoisted_3)) : createCommentVNode("", true), (openBlock(true), createElementBlock(Fragment, null, renderList(item.items, function (child, j) { return openBlock(), createElementBlock(Fragment, { key: child.label + i + '_' + j }, [$options.visible(child) && !child.separator ? (openBlock(), createBlock(_component_PVMenuitem, { key: 0, id: _ctx.$id + '_' + i + '_' + j, item: child, templates: _ctx.$slots, focusedOptionId: $options.focusedOptionId, unstyled: _ctx.unstyled, onItemClick: $options.itemClick, onItemMousemove: $options.itemMouseMove, pt: _ctx.pt }, null, 8, ["id", "item", "templates", "focusedOptionId", "unstyled", "onItemClick", "onItemMousemove", "pt"])) : $options.visible(child) && child.separator ? (openBlock(), createElementBlock("li", mergeProps({ key: 'separator' + i + j, "class": [_ctx.cx('separator'), item["class"]], style: child.style, role: "separator", ref_for: true }, _ctx.ptm('separator')), null, 16)) : createCommentVNode("", true)], 64); }), 128))], 64)) : $options.visible(item) && item.separator ? (openBlock(), createElementBlock("li", mergeProps({ key: 'separator' + i.toString(), "class": [_ctx.cx('separator'), item["class"]], style: item.style, role: "separator", ref_for: true }, _ctx.ptm('separator')), null, 16)) : (openBlock(), createBlock(_component_PVMenuitem, { key: $options.label(item) + i.toString(), id: _ctx.$id + '_' + i, item: item, index: i, templates: _ctx.$slots, focusedOptionId: $options.focusedOptionId, unstyled: _ctx.unstyled, onItemClick: $options.itemClick, onItemMousemove: $options.itemMouseMove, pt: _ctx.pt }, null, 8, ["id", "item", "index", "templates", "focusedOptionId", "unstyled", "onItemClick", "onItemMousemove", "pt"]))], 64); }), 128))], 16, _hoisted_2), _ctx.$slots.end ? (openBlock(), createElementBlock("div", mergeProps({ key: 1, "class": _ctx.cx('end') }, _ctx.ptm('end')), [renderSlot(_ctx.$slots, "end")], 16)) : createCommentVNode("", true)], 16, _hoisted_1)) : createCommentVNode("", true)]; }), _: 3 }, 16, ["onEnter", "onLeave", "onAfterLeave"])]; }), _: 3 }, 8, ["appendTo", "disabled"]); } script.render = render; export { script as default }; //# sourceMappingURL=index.mjs.map