1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2008, 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) 2008, 2009, 2010, 2011, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * Creates the browser alert.
 26  * @class
 27  * Singleton alert class that alerts the user by flashing the favicon and document title.
 28  * 
 29  * @extends		ZmAlert
 30  */
 31 ZmBrowserAlert = function() {
 32 	ZmAlert.call(this);
 33 
 34 	this._originalTitle = null;
 35 	this.altTitle = null;   // Title to show when flashing.
 36 
 37 	// Keep track of focus on the app.
 38 	var focusListener = new AjxListener(this, this._focusListener);
 39 	DwtShell.getShell(window).addFocusListener(focusListener);
 40 	DwtShell.getShell(window).addBlurListener(focusListener);
 41 
 42 	// Use key & mouse down events to handle focus.
 43 	var globalEventListener = new AjxListener(this, this._globalEventListener);
 44 	DwtEventManager.addListener(DwtEvent.ONMOUSEDOWN, globalEventListener);
 45 	DwtEventManager.addListener(DwtEvent.ONKEYDOWN, globalEventListener);
 46 };
 47 
 48 ZmBrowserAlert.prototype = new ZmAlert;
 49 ZmBrowserAlert.prototype.constructor = ZmBrowserAlert;
 50 
 51 ZmBrowserAlert.prototype.toString =
 52 function() {
 53 	return "ZmBrowserAlert";
 54 };
 55 
 56 /**
 57  * Gets an instance of the browser alert.
 58  * 
 59  * @return	{ZmBrowserAlert}	the browser alert
 60  */
 61 ZmBrowserAlert.getInstance =
 62 function() {
 63 	return ZmBrowserAlert.INSTANCE = ZmBrowserAlert.INSTANCE || new ZmBrowserAlert();
 64 };
 65 
 66 /**
 67  * Starts the alert.
 68  * 
 69  * @param	{String}	altTitle		the alternate title
 70  */
 71 ZmBrowserAlert.prototype.start =
 72 function(altTitle) {
 73 	if (this._isLooping) {
 74 		return;
 75 	}
 76 	this.altTitle = altTitle || ZmMsg.newMessage;
 77 	if (!this._clientHasFocus) {
 78 		if (!this._favIcon) {
 79 			this._favIcon = appContextPath + "/img/logo/favicon.ico";
 80 			this._blankIcon = appContextPath + "/img/logo/blank.ico";
 81 		}
 82 		this._startLoop();
 83 	}
 84 };
 85 
 86 /**
 87  * Stops the alert.
 88  * 
 89  */
 90 ZmBrowserAlert.prototype.stop =
 91 function() {
 92 	this._stopLoop();
 93 };
 94 
 95 ZmBrowserAlert.prototype._update =
 96 function(status) {
 97 	// Update the favicon.
 98     // bug: 52080 - disable flashing of favicon
 99 	//Dwt.setFavIcon(status ? this._blankIcon : this._favIcon);
100 
101 	// Update the title.
102 	var doc = document;
103 	if (status) {
104 		this._origTitle = doc.title;
105 		doc.title = this.altTitle;
106 	} else {
107 		if (doc.title == this.altTitle) {
108 			doc.title = this._origTitle;
109 		}
110 		// else if someone else changed the title, just leave it.
111 	}
112 };
113 
114 ZmBrowserAlert.prototype._focusListener =
115 function(ev) {
116 	this._clientHasFocus = ev.state == DwtFocusEvent.FOCUS;
117 	if (this._clientHasFocus) {
118 		this.stop();
119 	}
120 };
121 
122 ZmBrowserAlert.prototype._globalEventListener =
123 function() {
124 	this._clientHasFocus = true;
125 	this.stop();
126 };
127