'use strict'; /** * Licensed Materials - Property of IBM * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2014, 2017 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['jquery', '../../lib/@waca/dashboard-common/dist/ui/toolbar_components/Menu'], function ($, Menu) { var ContextMenuItem = null; /** * Represents a context down menu */ ContextMenuItem = Menu.extend({ init: function init(options) { this.oInvokedContext = null; this.nPreviousFocus = null; ContextMenuItem.inherited('init', this, arguments); if (options.selectedIndex) { this.selectedIndex = options.selectedIndex; } }, _performAction: function _performAction(id) { var action = this.itemActions[id]; var oInvokedContext = this.oInvokedContext; if (action) { // Delay the action slightly to allow bootstrap to close the menu before // the action is fired setTimeout(function () { action(oInvokedContext); }, 10); } this.hide(); }, render: function render() { this.$el.addClass('contextmenu').hide(); var promise = ContextMenuItem.inherited('render', this, arguments); this.$toggle.addClass('sr-only'); this.$menu.show(); return promise; }, show: function show(event) { this.oInvokedContext = event; this.nPreviousFocus = document.activeElement; var oMenuNode = this.$el.find('.dropdown-menu').first(); var oWindow = $(window); //Bind our initial position to the bounds of the target node var iInitialX = event.pageX ? event.pageX : 0; var iInitialY = event.pageY ? event.pageY : 0; if (event.target) { var oTarget = $(event.target); var oTargetCoords = oTarget.offset(); oTargetCoords.width = oTarget.width(); oTargetCoords.height = oTarget.height(); if (iInitialX < oTargetCoords.left || iInitialX > oTargetCoords.left + oTargetCoords.width) { iInitialX = oTargetCoords.left; } if (iInitialY < oTargetCoords.top || iInitialY > oTargetCoords.top + oTargetCoords.height) { iInitialY = oTargetCoords.top; } } this.$el.show().addClass('open').css({ left: this._getPosition(iInitialX, oWindow.width(), oMenuNode.outerWidth()), top: this._getPosition(iInitialY, oWindow.height(), oMenuNode.outerHeight()) }); var selectedItem; if (this.selectedIndex) { selectedItem = this.$el.find('.dropdown-menu a').eq(this.selectedIndex); } else { selectedItem = this.$el.find('.dropdown-menu a').first(); } selectedItem.focus(); $(document).on('click.privateViewEvents' + this.viewId + 'visible', this.hide.bind(this)); }, hide: function hide() { this.oInvokedContext = null; if (this.nPreviousFocus) { this.nPreviousFocus.focus(); } this.nPreviousFocus = null; this.$el.hide().removeClass('open'); $(document).off('click.privateViewEvents' + this.viewId + 'visible'); }, _getPosition: function _getPosition(iInitialLocation, iLimit, iObjectSize) { var iFinalLocation = iInitialLocation; if (iInitialLocation + iObjectSize > iLimit) { if (iObjectSize <= iInitialLocation) { iFinalLocation = iInitialLocation - iObjectSize; } else { iFinalLocation = 0; } } return iFinalLocation; } }); return ContextMenuItem; }); //# sourceMappingURL=ContextMenu.js.map