Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
getDefaultFoundation() {
return new MDCMenuSurfaceFoundation({
addClass: (className: string) => {
this.root.addClass(className);
},
removeClass: (className: string) => {
this.root.removeClass(className);
},
hasClass: (className: string) =>
className === 'mdc-menu-surface' ? true : this.root.hasClass(className),
hasAnchor: () => !!this.anchorElement,
notifyClose: () => {
this.emit('onClose', {});
this.deregisterBodyClickListener();
// an annoying hack... this is the only
// place to catch the normal close and bodyClick handler
// and correct it if we still want to be open.
if (this.props.open) {
init() {
const anchorEl = this.adapter_.getAnchorElement();
if (anchorEl) {
this.adapter_.setMenuAnchorElement(anchorEl);
this.adapter_.setMenuAnchorCorner(Corner.BOTTOM_START);
}
this.adapter_.setMenuWrapFocus(false);
const value = this.getValue();
if (value) {
this.setValue(value);
}
// Initially sync floating label
this.updateLabel_();
}
init() {
const anchorEl = this.adapter_.getAnchorElement();
if (anchorEl) {
this.adapter_.setMenuAnchorElement(anchorEl);
this.adapter_.setMenuAnchorCorner(Corner.BOTTOM_START);
}
this.adapter_.setMenuWrapFocus(false);
const value = this.getValue();
if (value) {
this.setValue(value);
}
// Initially sync floating label
this.updateLabel_();
}
componentDidMount() {
const {
anchorCorner,
anchorMargin,
coordinates,
fixed,
quickOpen,
} = this.props;
this.handleWindowClick = (evt: MouseEvent) => this.foundation.handleBodyClick(evt);
this.registerWindowClickListener = () =>
window.addEventListener('click', this.handleWindowClick!);
this.deregisterWindowClickListener = () =>
window.removeEventListener('click', this.handleWindowClick!);
this.foundation = new MDCMenuSurfaceFoundation(this.adapter);
this.foundation.init();
// this deviates from the mdc web version.
// here we force the menu to hoist, and require either
// this.props.(x,y) or this.props.anchorElement.
this.foundation.setIsHoisted(true);
this.foundation.setFixedPosition(fixed!);
if (coordinates) {
this.setCoordinates();
}
if (anchorCorner) {
this.foundation.setAnchorCorner(anchorCorner);
}
if (anchorMargin) {
this.foundation.setAnchorMargin(anchorMargin);
}
if (quickOpen) {
? this.$refs.helpertextEl.foundation
: void 0,
leadingIcon: this.$refs.leadingIconEl
? this.$refs.leadingIconEl.foundation
: undefined,
},
);
if (this.menu_) {
this.menu_.listen(
MDCMenuSurfaceFoundation.strings.CLOSED_EVENT,
this.handleMenuClosed,
);
this.menu_.listen(
MDCMenuSurfaceFoundation.strings.OPENED_EVENT,
this.handleMenuOpened,
);
this.menu_.listen(
MDCMenuFoundation.strings.SELECTED_EVENT,
this.handleMenuItemAction,
);
}
this.foundation.init();
// this.foundation.handleChange(false);
// initial sync with DOM
this.refreshIndex();
// this.slotObserver = new MutationObserver(() => this.refreshIndex());
// this.slotObserver.observe(this.$refs.native_control, {
// childList: true,
// will return false if the menu is still closing and this method listens to the closed event which
// occurs after the menu is already closed.
_this.isMenuOpen_ = false;
_this.selectedText_.removeAttribute('aria-expanded');
if (document.activeElement !== _this.selectedText_) {
_this.foundation_.handleBlur();
}
};
this.targetElement_.addEventListener('change', this.handleChange_);
this.targetElement_.addEventListener('focus', this.handleFocus_);
this.targetElement_.addEventListener('blur', this.handleBlur_);
this.targetElement_.addEventListener('click', this.handleClick_);
if (this.menuElement_) {
this.selectedText_.addEventListener('keydown', this.handleKeydown_);
this.menu_.listen(menuSurfaceConstants.strings.CLOSED_EVENT, this.handleMenuClosed_);
this.menu_.listen(menuSurfaceConstants.strings.OPENED_EVENT, this.handleMenuOpened_);
this.menu_.listen(menuConstants.strings.SELECTED_EVENT, this.handleMenuSelected_);
if (this.hiddenInput_ && this.hiddenInput_.value) {
// If the hidden input already has a value, use it to restore the select's value.
// This can happen e.g. if the user goes back or (in some browsers) refreshes the page.
var enhancedAdapterMethods = this.getEnhancedSelectAdapterMethods_();
enhancedAdapterMethods.setValue(this.hiddenInput_.value);
}
else if (this.menuElement_.querySelector(strings.SELECTED_ITEM_SELECTOR)) {
// If an element is selected, the select should set the initial selected text.
var enhancedAdapterMethods = this.getEnhancedSelectAdapterMethods_();
enhancedAdapterMethods.setValue(enhancedAdapterMethods.getValue());
}
}
// Initially sync floating label
this.foundation_.handleChange(/* didChange */ false);
if (this.root_.classList.contains(cssClasses.DISABLED)
};
const element = this.nativeControl_ ? this.nativeControl_ : this.selectedText_;
element.addEventListener('change', this.handleChange_);
element.addEventListener('focus', this.handleFocus_);
element.addEventListener('blur', this.handleBlur_);
['mousedown', 'touchstart'].forEach((evtType) => {
element.addEventListener(evtType, this.handleClick_);
});
if (this.menuElement_) {
this.selectedText_.addEventListener('keydown', this.handleKeydown_);
this.menu_.listen(menuSurfaceConstants.strings.CLOSED_EVENT, this.handleMenuClosed_);
this.menu_.listen(menuSurfaceConstants.strings.OPENED_EVENT, this.handleMenuOpened_);
this.menu_.listen(menuConstants.strings.SELECTED_EVENT, this.handleMenuSelected_);
if (this.hiddenInput_ && this.hiddenInput_.value) {
// If the hidden input already has a value, use it to restore the select's value.
// This can happen e.g. if the user goes back or (in some browsers) refreshes the page.
const enhancedAdapterMethods = this.getEnhancedSelectAdapterMethods_();
enhancedAdapterMethods.setValue(this.hiddenInput_.value);
} else if (this.menuElement_.querySelector(strings.SELECTED_ITEM_SELECTOR)) {
// If an element is selected, the select should set the initial selected text.
const enhancedAdapterMethods = this.getEnhancedSelectAdapterMethods_();
enhancedAdapterMethods.setValue(enhancedAdapterMethods.getValue());
}
}
// Initially sync floating label
this.foundation_.handleChange(/* didChange */ false);
componentDidMount() {
if (this.root.ref) {
const anchor = closest(
this.root.ref,
`.${MDCMenuSurfaceFoundation.cssClasses.ANCHOR}`
);
anchor && (this.anchorElement = anchor);
}
// this has to be run AFTER we try to get our anchor
super.componentDidMount();
}
this.adapter_.notifySelected({index});
this.adapter_.closeSurface();
const checkbox = this.adapter_.getCheckboxAtIndex(index);
if (checkbox) {
this.adapter_.toggleCheckbox(/** @type {!HTMLElement} */ (checkbox));
}
// Wait for the menu to close before adding/removing classes that affect styles.
this.closeAnimationEndTimerId_ = setTimeout(() => {
const selectionGroup = this.getSelectionGroup_(listItem);
if (selectionGroup !== null) {
this.handleSelectionGroup_(/** @type {!HTMLElement} */ (selectionGroup), index);
}
}, MDCMenuSurfaceFoundation.numbers.TRANSITION_CLOSE_DURATION);
}
MDCMenuFoundation.prototype.handleItemAction = function (listItem) {
var _this = this;
var index = this.adapter_.getElementIndex(listItem);
if (index < 0) {
return;
}
this.adapter_.notifySelected({ index: index });
this.adapter_.closeSurface();
// Wait for the menu to close before adding/removing classes that affect styles.
this.closeAnimationEndTimerId_ = setTimeout(function () {
// Recompute the index in case the menu contents have changed.
var recomputedIndex = _this.adapter_.getElementIndex(listItem);
if (_this.adapter_.isSelectableItemAtIndex(recomputedIndex)) {
_this.setSelectedIndex(recomputedIndex);
}
}, MDCMenuSurfaceFoundation.numbers.TRANSITION_CLOSE_DURATION);
};
MDCMenuFoundation.prototype.handleMenuSurfaceOpened = function () {