ContextMenu.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2014, 2017
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['jquery', '../../lib/@waca/dashboard-common/dist/ui/toolbar_components/Menu'], function ($, Menu) {
  8. var ContextMenuItem = null;
  9. /**
  10. * Represents a context down menu
  11. */
  12. ContextMenuItem = Menu.extend({
  13. init: function init(options) {
  14. this.oInvokedContext = null;
  15. this.nPreviousFocus = null;
  16. ContextMenuItem.inherited('init', this, arguments);
  17. if (options.selectedIndex) {
  18. this.selectedIndex = options.selectedIndex;
  19. }
  20. },
  21. _performAction: function _performAction(id) {
  22. var action = this.itemActions[id];
  23. var oInvokedContext = this.oInvokedContext;
  24. if (action) {
  25. // Delay the action slightly to allow bootstrap to close the menu before
  26. // the action is fired
  27. setTimeout(function () {
  28. action(oInvokedContext);
  29. }, 10);
  30. }
  31. this.hide();
  32. },
  33. render: function render() {
  34. this.$el.addClass('contextmenu').hide();
  35. var promise = ContextMenuItem.inherited('render', this, arguments);
  36. this.$toggle.addClass('sr-only');
  37. this.$menu.show();
  38. return promise;
  39. },
  40. show: function show(event) {
  41. this.oInvokedContext = event;
  42. this.nPreviousFocus = document.activeElement;
  43. var oMenuNode = this.$el.find('.dropdown-menu').first();
  44. var oWindow = $(window);
  45. //Bind our initial position to the bounds of the target node
  46. var iInitialX = event.pageX ? event.pageX : 0;
  47. var iInitialY = event.pageY ? event.pageY : 0;
  48. if (event.target) {
  49. var oTarget = $(event.target);
  50. var oTargetCoords = oTarget.offset();
  51. oTargetCoords.width = oTarget.width();
  52. oTargetCoords.height = oTarget.height();
  53. if (iInitialX < oTargetCoords.left || iInitialX > oTargetCoords.left + oTargetCoords.width) {
  54. iInitialX = oTargetCoords.left;
  55. }
  56. if (iInitialY < oTargetCoords.top || iInitialY > oTargetCoords.top + oTargetCoords.height) {
  57. iInitialY = oTargetCoords.top;
  58. }
  59. }
  60. this.$el.show().addClass('open').css({
  61. left: this._getPosition(iInitialX, oWindow.width(), oMenuNode.outerWidth()),
  62. top: this._getPosition(iInitialY, oWindow.height(), oMenuNode.outerHeight())
  63. });
  64. var selectedItem;
  65. if (this.selectedIndex) {
  66. selectedItem = this.$el.find('.dropdown-menu a').eq(this.selectedIndex);
  67. } else {
  68. selectedItem = this.$el.find('.dropdown-menu a').first();
  69. }
  70. selectedItem.focus();
  71. $(document).on('click.privateViewEvents' + this.viewId + 'visible', this.hide.bind(this));
  72. },
  73. hide: function hide() {
  74. this.oInvokedContext = null;
  75. if (this.nPreviousFocus) {
  76. this.nPreviousFocus.focus();
  77. }
  78. this.nPreviousFocus = null;
  79. this.$el.hide().removeClass('open');
  80. $(document).off('click.privateViewEvents' + this.viewId + 'visible');
  81. },
  82. _getPosition: function _getPosition(iInitialLocation, iLimit, iObjectSize) {
  83. var iFinalLocation = iInitialLocation;
  84. if (iInitialLocation + iObjectSize > iLimit) {
  85. if (iObjectSize <= iInitialLocation) {
  86. iFinalLocation = iInitialLocation - iObjectSize;
  87. } else {
  88. iFinalLocation = 0;
  89. }
  90. }
  91. return iFinalLocation;
  92. }
  93. });
  94. return ContextMenuItem;
  95. });
  96. //# sourceMappingURL=ContextMenu.js.map