GeminiToggleMenuBar.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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(['./MenuWithTick', 'underscore'], function (MenuWithTick, _) {
  8. 'use strict';
  9. var ToggleMenuBar = null;
  10. /**
  11. * Represents a drop down menu that can be added to an app bar
  12. */
  13. ToggleMenuBar = MenuWithTick.extend({
  14. itemMap: {},
  15. sCheckedItemName: null,
  16. updateLabel: false,
  17. /**
  18. * Creates a new item
  19. *
  20. * @param spec -
  21. * The item spec
  22. * @param root -
  23. * A reference to the root menu item, where items go if the app bar is collapsed horizontally
  24. */
  25. init: function init(spec) {
  26. _.extend(this, spec);
  27. ToggleMenuBar.inherited('init', this, arguments);
  28. },
  29. toggleHandler: function toggleHandler(name) {
  30. this.updateSelectedItem(name);
  31. },
  32. /**
  33. * Make sure only one item is selected
  34. */
  35. updateSelectedItem: function updateSelectedItem(name) {
  36. var item = this.itemMap[name];
  37. if (!item || !item.children) {
  38. // if we haven't selected an item, we do not update the tick
  39. return;
  40. } //uncheck current item
  41. if (this.sCheckedItemName) {
  42. //update tick mark on menu item
  43. this.updateMenuItemTick(this.itemMap[this.sCheckedItemName], false);
  44. } //update check item name
  45. this.sCheckedItemName = name; //update tick mark on menu item
  46. this.updateMenuItemTick(this.itemMap[this.sCheckedItemName], true);
  47. if (this.updateLabel) {
  48. if (this.$menuLabel.length > 0 && item.children) {
  49. this.$menuLabel.text(item.children('a').attr('aria-label'));
  50. }
  51. }
  52. },
  53. render: function render() {
  54. var promise = ToggleMenuBar.inherited('render', this, arguments);
  55. if (this.updateLabel) {
  56. _.each(this.items, function (item) {
  57. if (item.checked) {
  58. this.updateSelectedItem(item.name);
  59. }
  60. }.bind(this));
  61. if (!this.sCheckedItemName) {
  62. this.updateSelectedItem(this.items[0].name);
  63. }
  64. }
  65. return promise;
  66. },
  67. _performAction: function _performAction(id) {
  68. this.updateSelectedItem(this._getItemNameFromID(id));
  69. ToggleMenuBar.inherited('_performAction', this, arguments);
  70. },
  71. _getItemNameFromID: function _getItemNameFromID(id) {
  72. var prefix = this.viewId + '_';
  73. return id.substr(prefix.length);
  74. },
  75. _buildDropDown: function _buildDropDown() {
  76. var promise = ToggleMenuBar.inherited('_buildDropDown', this, arguments); //populate itemMap to reference elements by name
  77. _.each(this.items, function (item) {
  78. var id = this._getItemId(item.name);
  79. this.itemMap[item.name] = this.$menu.find('#' + id);
  80. if (item.checked) {
  81. this.sCheckedItemName = item.name;
  82. }
  83. }.bind(this));
  84. this.updateMenuItemTick(this.itemMap[this.sCheckedItemName], true);
  85. return promise;
  86. },
  87. remove: function remove() {
  88. _.each(this.itemMap, function (item) {
  89. if (item.remove) {
  90. item.remove();
  91. }
  92. });
  93. ToggleMenuBar.inherited('remove', this, arguments);
  94. }
  95. });
  96. return ToggleMenuBar;
  97. });