1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2005, 2006, 2007, 2009, 2010, 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) 2005, 2006, 2007, 2009, 2010, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 ZmAddressObjectHandler = function() {
 25 
 26 	ZmObjectHandler.call(this, ZmAddressObjectHandler.TYPE);
 27 }
 28 
 29 ZmAddressObjectHandler.prototype = new ZmObjectHandler;
 30 ZmAddressObjectHandler.prototype.constructor = ZmAddressObjectHandler;
 31 
 32 // Consts
 33 ZmAddressObjectHandler.TYPE = "address";
 34 // TODO This regex is very very simple.  It only matches single line simple addresses like:
 35 // 1234 Main St City CA 99999
 36 ZmAddressObjectHandler.ADDRESS_RE = /[\w]{3,}([A-Za-z]\.)?([ \w]*\#\d+)?(\r\n| )[ \w]{3,}\x20[A-Za-z]{2}\x20\d{5}(-\d{4})?\b/ig;
 37 
 38 // Y! maps geocoder, since Google doesn't have one.
 39 ZmAddressObjectHandler.URL = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=ZimbraMail&location=";
 40 
 41 // Make DOM safe id's
 42 ZmAddressObjectHandler.encodeId = function(s) {
 43 	return s.replace(/[^A-Za-z0-9]/g, "");
 44 };
 45 
 46 ZmAddressObjectHandler.CACHE = new Array();
 47 
 48 // Public methods
 49 
 50 ZmAddressObjectHandler.prototype.match =
 51 function(line, startIndex) {
 52 	ZmAddressObjectHandler.ADDRESS_RE.lastIndex = startIndex;
 53 	return ZmAddressObjectHandler.ADDRESS_RE.exec(line);
 54 };
 55 
 56 ZmAddressObjectHandler.prototype._getHtmlContent =
 57 function(html, idx, address, context) {
 58 	var	url = "http://maps.google.com/maps?q="+AjxStringUtil.htmlEncode(address);
 59     html[idx++] = '<a target="_blank" href="'+url+'">'+AjxStringUtil.htmlEncode(address)+'</a>';	
 60 	return idx;
 61 };
 62 
 63 ZmAddressObjectHandler.prototype.getToolTipText =
 64 function(obj, context) {
 65 	return '<div class="AddressContent" id="'+ZmAddressObjectHandler.encodeId(obj)+'"> </div>';
 66 };
 67 
 68 ZmAddressObjectHandler.prototype.populateToolTip =
 69 function(obj, context) {
 70 	if(ZmAddressObjectHandler.CACHE[obj+"lng"] && ZmAddressObjectHandler.CACHE[obj+"lat"]) {
 71 		ZmAddressObjectHandler.displayMap(ZmAddressObjectHandler.CACHE[obj+"lng"], 
 72 		                                  ZmAddressObjectHandler.CACHE[obj+"lat"], obj);
 73 	} else {
 74 		var url = "/service/proxy?target=" + AjxStringUtil.urlEncode(ZmAddressObjectHandler.URL + AjxStringUtil.htmlEncode(obj));
 75 		AjxRpc.invoke(null, url, null, new AjxCallback(this, this._callback, obj), true);
 76 	}
 77 };
 78 
 79 ZmAddressObjectHandler.prototype._callback = 
 80 function(arg0, arg1) {
 81 	// Quick RDF parser
 82 	var r = arg1.text;
 83 	var lg_s = r.indexOf("<Longitude>");
 84 	var lg_e = r.indexOf("</Longitude>");
 85 	var lt_s = r.indexOf("<Latitude>");
 86 	var lt_e = r.indexOf("</Latitude>");
 87 	ZmAddressObjectHandler.displayMap(r.substring(lg_s+11,lg_e), r.substring(lt_s+10,lt_e), arg0);
 88 };
 89 
 90 ZmAddressObjectHandler.displayMap = function(lng, lat, obj) {
 91 	DBG.println(AjxDebug.DBG2, "gMAPS: lat: " + lat + " long: " + lng + " obj: '" + obj + "'");
 92 	var element = document.getElementById(ZmAddressObjectHandler.encodeId(obj));
 93     var map = new GMap(element);
 94     var point = new GPoint(lng, lat);
 95     map.centerAndZoom(point, 4);
 96 	var marker = new GMarker(point);
 97 	map.addOverlay(marker);
 98 	marker.openInfoWindowHtml("<div id=\""+ZmAddressObjectHandler.encodeId(obj)+"tip\" style=\"width:255px\"><b>"+obj+"</b></div>");
 99     if(!ZmAddressObjectHandler.CACHE[obj+"lng"] || !ZmAddressObjectHandler.CACHE[obj+"lat"]) {
100 		DBG.println(AjxDebug.DBG2, "gMAPS: Adding to cache");
101 		ZmAddressObjectHandler.CACHE[obj+"lng"] = lng;
102 		ZmAddressObjectHandler.CACHE[obj+"lat"] = lat;
103 	}
104 };
105 
106 //ZmObjectManager.registerHandler("ZmAddressObjectHandler",ZmAddressObjectHandler.TYPE, 27);