1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 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) 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * Sets up an iframe which displays content from Community (activity stream, notifications, chat).
 26  * @class
 27  * This class displays Community content in an iframe.
 28  *
 29  * @extends		ZmAppIframeView
 30  *
 31  * @author Conrad Damon
 32  */
 33 ZmCommunityView = function(params) {
 34 	ZmAppIframeView.apply(this, arguments);
 35 	this._createFrame(params);
 36 	this._setupMessageHandling();
 37 };
 38 
 39 ZmCommunityView.prototype = new ZmAppIframeView;
 40 ZmCommunityView.prototype.constructor = ZmCommunityView;
 41 
 42 ZmCommunityView.prototype.isZmCommunityView = true;
 43 ZmCommunityView.prototype.toString = function() { return "ZmCommunityView"; };
 44 
 45 ZmCommunityView.prototype._setupMessageHandling = function(params) {
 46 
 47 	// Set up to handle messages sent to us via postMessage()
 48 	var iframe = document.getElementById(this._iframeId);
 49 	if (iframe) {
 50 		var callback = ZmCommunityView.handleMessage.bind(null, this);
 51 		if (window.addEventListener) {
 52 			window.addEventListener('message', callback, false);
 53 		}
 54 		else if (window.attachEvent) {
 55 			window.attachEvent('onmessage', callback);
 56 		}
 57 	}
 58 };
 59 
 60 ZmCommunityView.prototype._getIframeId = function() {
 61 	return 'fragment-41812_iframe';     // this is what Community is expecting
 62 };
 63 
 64 /**
 65  * If Community tells us there is new content, turn the tab orange if it's not the current tab,
 66  * and refresh the content.
 67  *
 68  * @param view
 69  * @param event
 70  */
 71 ZmCommunityView.handleMessage = function(view, event) {
 72 
 73 	var iframe = document.getElementById(view._iframeId);
 74 	if (iframe && event.source === iframe.contentWindow) {
 75 		var data = AjxStringUtil.parseQueryString(event.data || '');
 76 		var isUnread = (data.unread && data.unread.toLowerCase() === 'true');
 77 		if (data.type === 'community-notification' && isUnread) {
 78 			appCtxt.getApp(view._appName).startAlert();
 79 			view.getUpdates();
 80 		}
 81 	}
 82 };
 83 
 84 /**
 85  * Sends a message to Community to refresh the content.
 86  */
 87 ZmCommunityView.prototype.getUpdates = function() {
 88 
 89 	var iframe = document.getElementById(this._iframeId)
 90 	if (iframe) {
 91 		iframe.contentWindow.postMessage('type=community-update', '*');
 92 	}
 93 };
 94 
 95 // Called when user switches to this tab.
 96 ZmCommunityView.prototype.activate = function(active) {
 97 	if (active) {
 98 		this.getUpdates();
 99 	}
100 };
101 
102 // Code to run when the user clicks the refresh (circle-arrow) button. Shouldn't really be
103 // needed, but doesn't hurt to have it.
104 ZmCommunityView.prototype.runRefresh = function() {
105 	this.getUpdates();
106 };
107