1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013, 2014, 2016 Synacor, Inc.
  5  *
  6  * The contents of this file are subject to the Common Public Attribution License Version 1.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at: https://www.zimbra.com/license
  9  * The License is based on the Mozilla Public License Version 1.1 but Sections 14 and 15
 10  * have been added to cover use of software over a computer network and provide for limited attribution
 11  * for the Original Developer. In addition, Exhibit A has been modified to be consistent with Exhibit B.
 12  *
 13  * Software distributed under the License is distributed on an "AS IS" basis,
 14  * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 15  * See the License for the specific language governing rights and limitations under the License.
 16  * The Original Code is Zimbra Open Source Web Client.
 17  * The Initial Developer of the Original Code is Zimbra, Inc.  All rights to the Original Code were
 18  * transferred by Zimbra, Inc. to Synacor, Inc. on September 14, 2015.
 19  *
 20  * All portions of the code are Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * @overview
 26  */
 27 
 28 /**
 29  * Creates an action menu with the given menu items.
 30  * @class
 31  * This class represents an action menu, which is a popup menu with a few added features.
 32  * It can be easily created using a set of standard operations, and/or custom menu items
 33  * can be provided. This class is designed for use with items ({@link ZmItem}), so it can for
 34  * example contain a tab submenu. See also {@link ZmButtonToolBar}.
 35  *
 36  * @author Conrad Damon
 37  *
 38  * @param {hash}	params		a hash of parameters
 39  * @param {DwtComposite}	params.parent		the containing widget
 40  * @param {ZmController}	params.controller	the owning controller
 41  * @param {array}	params.menuItems	a list of operation IDs
 42  * @param {hash}	params.overrides	a hash of overrides by op ID
 43  * @param {string}	params.context		the context (used to create ID)
 44  * @param {constant}	params.menuType		the menu type (used to generate menu item IDs)
 45  * 
 46  * @extends		ZmPopupMenu
 47  */
 48 ZmActionMenu = function(params) {
 49 
 50     var id = params.id || (params.context ? ZmId.getMenuId(params.context, params.menuType) : null);
 51 	ZmPopupMenu.call(this, params.parent, null, id, params.controller);
 52 
 53 	// standard menu items default to Tag/Print/Delete
 54 	var menuItems = params.menuItems;
 55 	if (!menuItems) {
 56 		menuItems = [ZmOperation.TAG_MENU, ZmOperation.PRINT, ZmOperation.DELETE];
 57 	} else if (menuItems == ZmOperation.NONE) {
 58 		menuItems = null;
 59 	}
 60 	// weed out disabled ops, save list of ones that make it
 61 	this.opList = ZmOperation.filterOperations(menuItems);
 62 	this._context = params.context;
 63 	this._menuType = params.menuType;
 64 
 65 	this._menuItems = ZmOperation.createOperations(this, this.opList, params.overrides);
 66 };
 67 
 68 ZmActionMenu.prototype = new ZmPopupMenu;
 69 ZmActionMenu.prototype.constructor = ZmActionMenu;
 70 
 71 ZmActionMenu.prototype.isZmActionMenu = true;
 72 ZmActionMenu.prototype.toString = function() { return "ZmActionMenu"; };
 73 
 74 
 75 // Public methods
 76 
 77 
 78 /**
 79  * Creates a menu item and adds its operation ID as data.
 80  * 
 81  * @param {String}	id			the name of the operation
 82  * @param	{hash}	params		a hash of parameters
 83  * @param  {string}	params.text			the menu item text
 84  * @param {string}	params.image			the icon class for the menu item
 85  * @param {string}	params.disImage		the disabled version of icon
 86  * @param {boolean}	params.enabled		if <code>true</code>, menu item is enabled
 87  * @param {constant}	params.style			the menu item style
 88  * @param {string}	params.radioGroupId	the ID of radio group for this menu item
 89  * @param {constant}	params.shortcut		the shortcut ID (from {@link ZmKeyMap}) for showing hint
 90  * 
 91  * @private
 92  */
 93 ZmActionMenu.prototype.createOp =
 94 function(id, params, elementId) {
 95 	params.id = this._context ? ZmId.getMenuItemId(this._context, id, this._menuType) : null;
 96 	var mi = this.createMenuItem(id, params, elementId);
 97 	mi.setData(ZmOperation.KEY_ID, id);
 98 
 99 	return mi;
100 };
101 
102 ZmActionMenu.prototype.addOp =
103 function(id) {
104 	ZmOperation.addOperation(this, id, this._menuItems);
105 };
106 
107 ZmActionMenu.prototype.removeOp =
108 function(id) {
109 	ZmOperation.removeOperation(this, id, this._menuItems);
110 };
111 
112 /**
113  * Gets the menu item with the given ID.
114  *
115  * @param {constant}	id		an operation ID
116  * @return	{DwtMenuItem}	the menu item
117  * @see		ZmOperation
118  */
119 ZmActionMenu.prototype.getOp =
120 function(id) {
121 	return this.getMenuItem(id);
122 };
123 
124 /**
125  * Gets the menu tag sub-menu (if any).
126  * 
127  * @return	{DwtMenu}		the menu
128  */
129 ZmActionMenu.prototype.getTagMenu =
130 function() {
131 	var menuItem = this.getMenuItem(ZmOperation.TAG_MENU);
132 	if (menuItem) {
133 		return menuItem.getMenu();
134 	}
135 };
136 
137 
138 // Private methods
139 
140 // Returns the ID for the given menu item.
141 ZmActionMenu.prototype._menuItemId =
142 function(menuItem) {
143 	return menuItem.getData(ZmOperation.KEY_ID);
144 };
145 
146 ZmActionMenu.prototype.removeMenuItemById =
147 function(menuItemId) {
148     var mi = this.getMenuItem(menuItemId);
149     this.removeMenuItem(mi);
150 };
151 
152 ZmActionMenu.prototype.removeMenuItem =
153 function(menuItem) {
154     if (!menuItem) {return};
155     this.removeChild(menuItem);
156     menuItem.dispose();
157 };
158 
159