123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- "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(['./MenuWithTick', 'underscore'], function (MenuWithTick, _) {
- 'use strict';
- var ToggleMenuBar = null;
- /**
- * Represents a drop down menu that can be added to an app bar
- */
- ToggleMenuBar = MenuWithTick.extend({
- itemMap: {},
- sCheckedItemName: null,
- updateLabel: false,
- /**
- * Creates a new item
- *
- * @param spec -
- * The item spec
- * @param root -
- * A reference to the root menu item, where items go if the app bar is collapsed horizontally
- */
- init: function init(spec) {
- _.extend(this, spec);
- ToggleMenuBar.inherited('init', this, arguments);
- },
- toggleHandler: function toggleHandler(name) {
- this.updateSelectedItem(name);
- },
- /**
- * Make sure only one item is selected
- */
- updateSelectedItem: function updateSelectedItem(name) {
- var item = this.itemMap[name];
- if (!item || !item.children) {
- // if we haven't selected an item, we do not update the tick
- return;
- } //uncheck current item
- if (this.sCheckedItemName) {
- //update tick mark on menu item
- this.updateMenuItemTick(this.itemMap[this.sCheckedItemName], false);
- } //update check item name
- this.sCheckedItemName = name; //update tick mark on menu item
- this.updateMenuItemTick(this.itemMap[this.sCheckedItemName], true);
- if (this.updateLabel) {
- if (this.$menuLabel.length > 0 && item.children) {
- this.$menuLabel.text(item.children('a').attr('aria-label'));
- }
- }
- },
- render: function render() {
- var promise = ToggleMenuBar.inherited('render', this, arguments);
- if (this.updateLabel) {
- _.each(this.items, function (item) {
- if (item.checked) {
- this.updateSelectedItem(item.name);
- }
- }.bind(this));
- if (!this.sCheckedItemName) {
- this.updateSelectedItem(this.items[0].name);
- }
- }
- return promise;
- },
- _performAction: function _performAction(id) {
- this.updateSelectedItem(this._getItemNameFromID(id));
- ToggleMenuBar.inherited('_performAction', this, arguments);
- },
- _getItemNameFromID: function _getItemNameFromID(id) {
- var prefix = this.viewId + '_';
- return id.substr(prefix.length);
- },
- _buildDropDown: function _buildDropDown() {
- var promise = ToggleMenuBar.inherited('_buildDropDown', this, arguments); //populate itemMap to reference elements by name
- _.each(this.items, function (item) {
- var id = this._getItemId(item.name);
- this.itemMap[item.name] = this.$menu.find('#' + id);
- if (item.checked) {
- this.sCheckedItemName = item.name;
- }
- }.bind(this));
- this.updateMenuItemTick(this.itemMap[this.sCheckedItemName], true);
- return promise;
- },
- remove: function remove() {
- _.each(this.itemMap, function (item) {
- if (item.remove) {
- item.remove();
- }
- });
- ToggleMenuBar.inherited('remove', this, arguments);
- }
- });
- return ToggleMenuBar;
- });
|