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 list view for location suggestions
 26  * @constructor
 27  * @class
 28  *
 29  *  @author Vince Bellows
 30  *
 31  * @param parent			[ZmScheduleAssistantView]	the smart scheduler view
 32  * @param controller		[ZmApptComposeController]	the appt compose controller
 33  * @param apptEditView		[ZmApptEditView]	        the appt edit view
 34  */
 35 ZmLocationSuggestionView = function(parent, controller, apptEditView, className) {
 36     ZmSuggestionsView.call(this, parent, controller, apptEditView, ZmId.VIEW_SUGGEST_LOCATION_PANE, false, className);
 37     this._warning = false;
 38     this._emailToDivIdMap = {};
 39 };
 40 
 41 ZmLocationSuggestionView.prototype = new ZmSuggestionsView;
 42 ZmLocationSuggestionView.prototype.constuctor = ZmLocationSuggestionView;
 43 
 44 ZmLocationSuggestionView.prototype.toString =
 45 function() {
 46 	return "ZmLocationSuggestionView";
 47 }
 48 
 49 ZmLocationSuggestionView.prototype._createItemHtml =
 50 function (item) {
 51     var id = this.associateItemWithElement(item, null, null, null);
 52 
 53     var params = {
 54         id: id,
 55         locationName: item.name,
 56         locationDescription: item.description
 57     };
 58     return AjxTemplate.expand("calendar.Appointment#LocationSuggestion", params);
 59 };
 60 
 61 ZmLocationSuggestionView.prototype._getItemId =
 62 function(item) {
 63     var id;
 64     if (item && item.email) {
 65         id = this._emailToDivIdMap[item.email];
 66         if (!id) {
 67             // No email->id mapping - first time accessed, so generate an id and create a mapping.
 68             // Return the id, which will be used as the id of the containing div.
 69             id = ZmListView.prototype._getItemId.call(this, item);
 70             this._emailToDivIdMap[item.email] = id;
 71         }
 72     }
 73     return id;
 74 };
 75 
 76 ZmLocationSuggestionView.prototype.set =
 77 function(params) {
 78     this._emailToDivIdMap = {};
 79     this._items = params.locationInfo.locations;
 80     ZmListView.prototype.set.call(this, params.locationInfo.locations);
 81 };
 82 
 83 ZmLocationSuggestionView.prototype.handleLocationOverflow =
 84 function() {
 85     var locTxt = this._locSelect.getText();
 86     if(locTxt && locTxt.length > 15) {
 87         locTxt = locTxt.substring(0, 15) + '...';
 88         this._locSelect.setText(locTxt);
 89     }
 90 };
 91 
 92 ZmLocationSuggestionView.prototype._itemSelected =
 93 function(itemDiv, ev) {
 94     ZmListView.prototype._itemSelected.call(this, itemDiv, ev);
 95 
 96     var locationInfo = this.getItemFromElement(itemDiv);
 97     if(locationInfo != null) {
 98         var locationObj = locationInfo.locationObj;
 99         var locationStr = locationInfo.email;
100         this._editView.updateLocation(locationObj, locationStr);
101         this.setToolTipContent(null);
102     }
103 };
104 
105 ZmLocationSuggestionView.prototype._setNoResultsHtml =
106 function() {
107     var	div = document.createElement("div");
108     var elText = document.createTextNode(ZmMsg.noLocations);
109     div.appendChild(elText);
110     this._addRow(div);
111 };
112 
113 ZmLocationSuggestionView.prototype.setWarning =
114 function(warning) {
115     this._warning = warning;
116 }
117 
118 
119 ZmLocationSuggestionView.prototype._renderList =
120 function(list, noResultsOk, doAdd) {
121     var warningHtml = "";
122     if (this._warning) {
123         warningHtml = AjxTemplate.expand("calendar.Appointment#LocationSuggestion-Warning");
124     }
125     ZmSuggestionsView.prototype._renderList.call(this, list, noResultsOk, doAdd, warningHtml);
126 }
127 
128 ZmLocationSuggestionView.prototype.getToolTipContent =
129 function(ev) {
130     var tooltip = "";
131     var div = this.getTargetItemDiv(ev);
132     if (div) {
133         var item = this.getItemFromElement(div);
134         if(item) {
135             tooltip = AjxTemplate.expand("calendar.Appointment#LocationSuggestionTooltip",
136                         {name:        item.name,
137                          description: item.description,
138                          contactMail: item.contactMail,
139                          capacity:    item.capacity
140                         });
141         }
142     }
143     var consoleText = tooltip;
144     if (!consoleText) {
145         consoleText = "None";
146     } else if(consoleText.length > 15) {
147         consoleText = consoleText.substring(0, 15) + '...';
148     }
149     console.log("getToolTipContent, div = " + (div ? div.id : "null") + ", text = " + consoleText);
150     return tooltip;
151 };
152