1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2008, 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) 2008, 2009, 2010, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * Abstract base class of flashing alerts.
 26  * @class
 27  * This is an abstract base class of flashing alerts.
 28  * 
 29  */
 30 ZmAlert = function() {
 31 	this._isLooping = false;
 32 };
 33 
 34 // Abstract methods.
 35 ZmAlert.prototype.start = null; // function ()
 36 ZmAlert.prototype.stop = null; // function ()
 37 ZmAlert.prototype._update = null; // function(status)
 38 
 39 ZmAlert.prototype._startLoop =
 40 function() {
 41 	if (!ZmAlertLoop.INSTANCE) {
 42 		ZmAlertLoop.INSTANCE = new ZmAlertLoop();
 43 	}
 44 	ZmAlertLoop.INSTANCE._add(this);
 45 	this._isLooping = true;
 46 };
 47 
 48 ZmAlert.prototype._stopLoop =
 49 function() {
 50 	//no need to do anything if already not looping
 51 	if (!this._isLooping) {
 52 		return;
 53 	}
 54 	if (ZmAlertLoop.INSTANCE) {
 55 		ZmAlertLoop.INSTANCE._remove(this);
 56 	}
 57 	this._isLooping = false;
 58 	this._update(false);
 59 };
 60 
 61 ///////////////////////////////////////////////////////////////////////////////////////
 62 ///////////////////////////////////////////////////////////////////////////////////////
 63 
 64 /**
 65  * Private class only used by ZmAlert.
 66  * Manages an interval that tells alerts when to flash icons and titles and such.
 67  * @class
 68  * @private 
 69  */
 70 ZmAlertLoop = function() {
 71 	this._alerts = new AjxVector();
 72 	this._flashOn = false;
 73 	if (appCtxt.multiAccounts) {
 74 		appCtxt.accountList.addActiveAcountListener(new AjxListener(this, this._accountChangeListener), 0);
 75 	}
 76 };
 77 
 78 ZmAlertLoop.prototype._add =
 79 function(alert) {
 80 	this._alerts.add(alert, 0, true);
 81 	if (!this._alertInterval) {
 82 		this._alertInterval = setInterval(AjxCallback.simpleClosure(this._alertTimerCallback, this), 1500);
 83 	}
 84 };
 85 
 86 ZmAlertLoop.prototype._remove =
 87 function(alert) {
 88 	this._alerts.remove(alert);
 89 	if (this._alertInterval && !this._alerts.size()) {
 90 		clearInterval(this._alertInterval);
 91 		this._alertInterval = 0;
 92 	}
 93 };
 94 
 95 ZmAlertLoop.prototype._alertTimerCallback =
 96 function() {
 97 	this._flashOn = !this._flashOn;
 98 	for (var i = 0, count = this._alerts.size(); i < count; i++) {
 99 	    this._alerts.get(i)._update(this._flashOn);
100 	}
101 };
102 
103 ZmAlertLoop.prototype._accountChangeListener =
104 function() {
105 	// Stop all flashing alerts.
106 	var array = this._alerts.getArray();
107 	var alert;
108 	while (alert = array.unshift()) {
109 		alert.stop();
110 	}
111 };
112 
113