1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2005, 2006, 2007, 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) 2005, 2006, 2007, 2009, 2010, 2011, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 ZmImageAttachmentObjectHandler = function() {
 25 	ZmObjectHandler.call(this, ZmImageAttachmentObjectHandler.TYPE);
 26 	this._imageHash = {};
 27 }
 28 
 29 ZmImageAttachmentObjectHandler.prototype = new ZmObjectHandler;
 30 ZmImageAttachmentObjectHandler.prototype.constructor = ZmImageAttachmentObjectHandler;
 31 
 32 ZmImageAttachmentObjectHandler.TYPE = "imageAttachemnt";
 33 
 34 ZmImageAttachmentObjectHandler.THUMB_SIZE = 'width="320" height="240"';
 35 ZmImageAttachmentObjectHandler.THUMB_SIZE_MAX = 320;
 36 	
 37 // already htmlencoded!!
 38 ZmImageAttachmentObjectHandler.prototype._getHtmlContent =
 39 function(html, idx, obj, context) {
 40 	html[idx++] = obj; //AjxStringUtil.htmlEncode(obj, true);
 41 	return idx;
 42 }
 43 
 44 ZmImageAttachmentObjectHandler.prototype.getToolTipText =
 45 function(url, context) {
 46 	var image = this._imageHash[context.url];
 47 	if (!image) {
 48 		image = {id:Dwt.getNextId()};
 49 	}
 50 	if (!image.el || (image.el.src !== context.url)) {
 51 		this._imageHash[context.url] = image;
 52 		this._preload(context.url, image.id);	
 53 	}
 54 	
 55 	var el = document.getElementById(image.id);
 56 	if (el && !image.el) {
 57 		image.el = el;
 58 	}
 59 	if (image.el) {
 60 		return image.el.xml || image.el.outerHTML;
 61 	}
 62 	return '<img id="'+ image.id +'" style="visibility:hidden;"/>';
 63 };
 64 
 65 ZmImageAttachmentObjectHandler.prototype.getActionMenu =
 66 function(obj) {
 67 	return null;
 68 };
 69 
 70 ZmImageAttachmentObjectHandler.prototype._preload =
 71 function(url, id) {
 72 	var tmpImage = new Image();
 73 	tmpImage.onload = AjxCallback.simpleClosure(this._setSize, this, id, tmpImage);
 74 	tmpImage.src = url;
 75 };
 76 
 77 ZmImageAttachmentObjectHandler.prototype._setSize =
 78 function(id, tmpImage) {
 79 	var elm = document.getElementById(id);
 80 	if(elm) {
 81 		var width = tmpImage.width;
 82 		var height = tmpImage.height;
 83 		if(width > ZmImageAttachmentObjectHandler.THUMB_SIZE_MAX && width >= height) {
 84 			elm.width = ZmImageAttachmentObjectHandler.THUMB_SIZE_MAX;
 85 		} else if (height > ZmImageAttachmentObjectHandler.THUMB_SIZE_MAX && height > width) {
 86 			elm.height = ZmImageAttachmentObjectHandler.THUMB_SIZE_MAX;
 87 		} else {
 88 			elm.width = width;
 89 			elm.width = height;
 90 		}
 91 		elm.src = tmpImage.src;
 92 		elm.style.visibility = "visible";
 93 	}
 94 	tmpImage.onload = null;
 95 	tmpImage = null;
 96 };
 97