1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 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) 2010, 2011, 2012, 2013, 2014, 2015, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 ZmColorButton = function(params) {
 25 
 26     if (arguments.length == 0) {
 27 	    return;
 28     }
 29 
 30     DwtButton.call(this, params);
 31     var menu = new ZmColorMenu({parent:this,hideNone:params.hideNone});
 32     menu.addSelectionListener(new AjxListener(this, this._handleSelection));
 33     this.setMenu(menu);
 34     this._colorMenu = menu;
 35 	this._labelId = params.labelId;
 36 };
 37 
 38 ZmColorButton.prototype = new DwtButton;
 39 ZmColorButton.prototype.constructor = ZmColorButton;
 40 
 41 ZmColorButton.prototype.isZmColorButton = true;
 42 ZmColorButton.prototype.toString = function() { return "ZmColorButton"; };
 43 
 44 //
 45 // Public methods
 46 //
 47 
 48 ZmColorButton.prototype.setImage = function(image, skipMenu) {
 49     DwtButton.prototype.setImage.apply(this, arguments);
 50     if (!skipMenu) {
 51         this._colorMenu.setImage(image);
 52     }
 53 };
 54 
 55 ZmColorButton.prototype.setValue = function(color) {
 56 
 57 	var standardColorCode = ZmOrganizer.getStandardColorNumber(color),
 58 		colorMenuItemId;
 59 
 60 	if (standardColorCode !== -1) {
 61 		this._color = standardColorCode;
 62 		colorMenuItemId = 'COLOR_' + standardColorCode;
 63 	}
 64 	else {
 65         this._color = color;
 66 	}
 67     var image = this.getImage();
 68     if (image) {
 69         image = image.replace(/,.*$/, "");
 70 		var displayColor = this._color || ZmOrganizer.COLOR_VALUES[ZmOrganizer.ORG_DEFAULT_COLOR]; //default to gray
 71         this.setImage([image, this._color].join(",color="), true);
 72     }
 73     this.setText(this._colorMenu.getTextForColor(this._color));
 74 
 75 	if (colorMenuItemId) {
 76 		this.removeAttribute('aria-label');
 77 		this.setAttribute('aria-labelledby', [ this._labelId, colorMenuItemId ].join(' '));
 78 	}
 79 };
 80 
 81 
 82 ZmColorButton.prototype.getValue = function() {
 83     return this._color;
 84 };
 85 
 86 //
 87 // Protected methods
 88 //
 89 
 90 ZmColorButton.prototype._handleSelection = function(evt) {
 91     this.setValue(evt.item.getData(ZmOperation.MENUITEM_ID)); 
 92 };
 93