UNPKG

carbon-components

Version:

Carbon Components is a component library for IBM Cloud

220 lines (186 loc) • 9.13 kB
(function (global, factory) { if (typeof define === "function" && define.amd) { define(['exports', '../../globals/js/settings', '../../globals/js/misc/mixin', '../../globals/js/mixins/create-component', '../../globals/js/mixins/init-component-by-search', '../../globals/js/mixins/evented-show-hide-state', '../../globals/js/mixins/handles', '../floating-menu/floating-menu', '../../globals/js/misc/get-launching-details', '../../globals/js/misc/on'], factory); } else if (typeof exports !== "undefined") { factory(exports, require('../../globals/js/settings'), require('../../globals/js/misc/mixin'), require('../../globals/js/mixins/create-component'), require('../../globals/js/mixins/init-component-by-search'), require('../../globals/js/mixins/evented-show-hide-state'), require('../../globals/js/mixins/handles'), require('../floating-menu/floating-menu'), require('../../globals/js/misc/get-launching-details'), require('../../globals/js/misc/on')); } else { var mod = { exports: {} }; factory(mod.exports, global.settings, global.mixin, global.createComponent, global.initComponentBySearch, global.eventedShowHideState, global.handles, global.floatingMenu, global.getLaunchingDetails, global.on); global.overflowMenu = mod.exports; } })(this, function (exports, _settings, _mixin2, _createComponent, _initComponentBySearch, _eventedShowHideState, _handles, _floatingMenu, _getLaunchingDetails, _on) { 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _settings2 = _interopRequireDefault(_settings); var _mixin3 = _interopRequireDefault(_mixin2); var _createComponent2 = _interopRequireDefault(_createComponent); var _initComponentBySearch2 = _interopRequireDefault(_initComponentBySearch); var _eventedShowHideState2 = _interopRequireDefault(_eventedShowHideState); var _handles2 = _interopRequireDefault(_handles); var _floatingMenu2 = _interopRequireDefault(_floatingMenu); var _getLaunchingDetails2 = _interopRequireDefault(_getLaunchingDetails); var _on2 = _interopRequireDefault(_on); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var OverflowMenu = function (_mixin) { _inherits(OverflowMenu, _mixin); /** * Overflow menu. * @extends CreateComponent * @extends InitComponentBySearch * @extends Handles * @param {HTMLElement} element The element working as a modal dialog. * @param {Object} [options] The component options. * @param {string} [options.selectorOptionMenu] The CSS selector to find the menu. * @param {string} [options.classShown] The CSS class for the shown state, for the trigger UI. * @param {string} [options.classMenuShown] The CSS class for the shown state, for the menu. * @param {string} [options.classMenuFlip] The CSS class for the flipped state of the menu. * @param {Object} [options.objMenuOffset] The offset locating the menu for the non-flipped state. * @param {Object} [options.objMenuOffsetFlip] The offset locating the menu for the flipped state. */ function OverflowMenu(element, options) { _classCallCheck(this, OverflowMenu); var _this = _possibleConstructorReturn(this, (OverflowMenu.__proto__ || Object.getPrototypeOf(OverflowMenu)).call(this, element, options)); _this.manage((0, _on2.default)(_this.element.ownerDocument, 'click', function (event) { _this._handleDocumentClick(event); _this.wasOpenBeforeClick = undefined; })); _this.manage((0, _on2.default)(_this.element.ownerDocument, 'keypress', function (event) { _this._handleKeyPress(event); })); _this.manage((0, _on2.default)(_this.element, 'mousedown', function () { _this.wasOpenBeforeClick = element.classList.contains(_this.options.classShown); })); return _this; } /** * Changes the shown/hidden state. * @param {string} state The new state. * @param {Object} detail The detail of the event trigging this action. * @param {Function} callback Callback called when change in state completes. */ _createClass(OverflowMenu, [{ key: 'changeState', value: function changeState(state, detail, callback) { if (!this.optionMenu) { var optionMenu = this.element.querySelector(this.options.selectorOptionMenu); if (!optionMenu) { throw new Error('Cannot find the target menu.'); } // Lazily create a component instance for menu this.optionMenu = _floatingMenu2.default.create(optionMenu, { refNode: this.element, classShown: this.options.classMenuShown, classRefShown: this.options.classShown, offset: this.options.objMenuOffset }); this.children.push(this.optionMenu); } if (this.optionMenu.element.classList.contains(this.options.classMenuFlip)) { this.optionMenu.options.offset = this.options.objMenuOffsetFlip; } // Delegates the action of changing state to the menu. // (And thus the before/after shown/hidden events are fired from the menu) this.optionMenu.changeState(state, Object.assign(detail, { delegatorNode: this.element }), callback); } }, { key: '_handleDocumentClick', value: function _handleDocumentClick(event) { var element = this.element; var isOfSelf = element.contains(event.target); var shouldBeOpen = isOfSelf && !this.wasOpenBeforeClick; var state = shouldBeOpen ? 'shown' : 'hidden'; if (isOfSelf) { if (element.tagName === 'A') { event.preventDefault(); } event.delegateTarget = element; // eslint-disable-line no-param-reassign } this.changeState(state, (0, _getLaunchingDetails2.default)(event)); } }, { key: '_handleKeyPress', value: function _handleKeyPress(event) { var key = event.which; if (key === 13) { var element = this.element; var isOfSelf = element.contains(event.target); var shouldBeOpen = isOfSelf && !element.classList.contains(this.options.classShown); var state = shouldBeOpen ? 'shown' : 'hidden'; if (isOfSelf) { if (element.tagName === 'A') { event.preventDefault(); } event.delegateTarget = element; // eslint-disable-line no-param-reassign } this.changeState(state, (0, _getLaunchingDetails2.default)(event)); } } }], [{ key: 'options', get: function get() { var prefix = _settings2.default.prefix; return { selectorInit: '[data-overflow-menu]', selectorOptionMenu: '.' + prefix + '--overflow-menu-options', classShown: prefix + '--overflow-menu--open', classMenuShown: prefix + '--overflow-menu-options--open', classMenuFlip: prefix + '--overflow-menu--flip', objMenuOffset: { top: 3, left: 61 }, objMenuOffsetFlip: { top: 3, left: -61 } }; } }]); return OverflowMenu; }((0, _mixin3.default)(_createComponent2.default, _initComponentBySearch2.default, _eventedShowHideState2.default, _handles2.default)); OverflowMenu.components = new WeakMap(); exports.default = OverflowMenu; });