1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2011, 2012, 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) 2011, 2012, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * Creates a tooltip manager.
 26  * @constructor
 27  * @class
 28  * This singleton class manages tooltip content generation and retrieval. Tooltips are
 29  * broken down by type based on what they show (for example information about
 30  * a person). Each type has a handler, which gets passed the appropriate data
 31  * and generates the tooltip content.
 32  *
 33  * @author Conrad Damon
 34  * 
 35  * @param {DwtComposite}	container	the containing shell
 36  * @param {ZmMailApp}		mailApp		the containing app
 37  */
 38 
 39 ZmToolTipMgr = function() {
 40 	this._registry = {};
 41 	this.registerToolTipHandler(ZmToolTipMgr.PERSON, this.getPersonToolTip);
 42 };
 43 
 44 ZmToolTipMgr.PERSON = "PERSON";
 45 
 46 /**
 47  * Associates a type of tooltip with a function to generate its content.
 48  * 
 49  * @param {constant}	type		type of tooltip
 50  * @param {function}	handler		function that returns tooltip content
 51  */
 52 ZmToolTipMgr.prototype.registerToolTipHandler =
 53 function(type, handler) {
 54 	this._registry[type] = handler;
 55 };
 56 
 57 /**
 58  * Returns tooltip content for the given type with the given data.
 59  * 
 60  * @param {constant}	type		type of tooltip
 61  * @param {hash}		params		arbitrary data to pass to tooltip function
 62  * @param {AjxCallback}	callback	callback to run with results (optional)
 63  */
 64 ZmToolTipMgr.prototype.getToolTip =
 65 function(type, params, callback) {
 66 	var handler = this._registry[type];
 67 	if (handler && AjxUtil.isFunction(handler)) {
 68 		return handler.apply(this, [params, callback]);
 69 	}
 70 };
 71 
 72 /**
 73  * Returns tooltip content for a person based on an email address or contact.
 74  * 
 75  * @param {hash}					params			hash of params:
 76  * @param {string|AjxEmailAddress}	address			email address
 77  * @param {ZmContact}				contact			contact - need either address or contact
 78  * @param {DwtMouseEvent}			ev				mouseover event
 79  * @param {boolean}					noRightClick	if true, don't show right click hint
 80  * @param {AjxCallback}				callback		callback to run with results (optional)
 81  */
 82 ZmToolTipMgr.prototype.getPersonToolTip =
 83 function(params, callback) {
 84 
 85 	if (!(params && (params.address || params.contact))) { return ""; }
 86 
 87 	var contact = params.contact;
 88 	var address = params.address || contact.getEmail();
 89 	if (!address.isAjxEmailAddress) {
 90 		address = new AjxEmailAddress(address);
 91 	}
 92 	
 93 	if (appCtxt.notifyZimlets("onHoverOverEmailInList", [address, params.ev, params.noRightClick])) {
 94 		// Zimlet framework is handling the tooltip
 95 		return "";
 96 	}
 97 	
 98 	var contactsApp = appCtxt.get(ZmSetting.CONTACTS_ENABLED) && appCtxt.getApp(ZmApp.CONTACTS);
 99 	if (!contact && !contactsApp) { return ""; }
100 
101 	var addr = address.getAddress();
102 
103 	if (callback) {
104 		if (contact) {
105 			this._handleResponseGetContact(address, callback, contact);
106 		}
107 		else {
108 			var respCallback = new AjxCallback(this, this._handleResponseGetContact, [address, callback]);
109 			contactsApp.getContactByEmail(addr, respCallback);
110 		}
111 	} else {
112 		contact = contact || contactsApp.getContactByEmail(addr);
113 		return this._handleResponseGetContact(address, null, contact);
114 	}
115 };
116 		
117 ZmToolTipMgr.prototype._handleResponseGetContact =
118 function(address, callback, contact) {
119 
120 	if (!address && !contact) { return ""; }
121 	
122 	var tooltip;
123 	if (contact) {
124 		tooltip = contact.getToolTip(address.getAddress());
125 	} else {
126 		tooltip = AjxTemplate.expand("abook.Contacts#TooltipNotInAddrBook", {addrstr:address.toString()});
127 	}
128 
129 	if (callback) {
130 		callback.run(tooltip);
131 	} else {
132 		return tooltip;
133 	}
134 };
135