jsbeans

jsbeans  1.0.0

jsbeans > jsbeans > Ticker.js (source view)
Search:
 
Filters
/*!
 * Copyright (c) 2009 Francesco Mele jsbeans@francescomele.com
 *
 * This Software is licenced under the LGPL Licence (GNU Lesser General 
 * Public License).
 * In addition to the LGPL Licence the Software is subject to the 
 * following conditions:
 * 
 * 	i	every modification must be public and comunicated to the Author
 * 	ii	every "jsbean" added to this library must be self consistent 
 * 		except for the dependence from jsbeans-x.x.x.js
 * 	iii	copyright notice and this permission notice shall be included 
 * 		in all copies or substantial portions of the Software
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * <code class="class">jsbeans.Ticker</code> is a news ticker with fading affect.
 * @namespace jsbeans
 * @class Ticker
 * @constructor
 * @param id {String} id of the news container.
 * @param [options] {JSON}
 * <pre>
 * pause: &lt;Integer>, // interval in seconds
 * fadeSpeed: &lt;Integer>, // fading speed
 * fadeTo: &lt;String>, // only "black" is valid, otherwise it fades to white
 * </pre>
 * */
jsbeans.Ticker = function(divId) {
	this.list = null;
	this.tickerObj = null;
	this.divId = divId;
	this.timeoutId = divId + "_tickerTimeout";
	this.count = 0,
	this.hex = 255;
	this.pause = 3;// interval in seconds
	this.fadeSpeed = 40;
	var _printError = function(error) {
		alert("The script could not run because you have errors:\n\n" + error);
		return false;
	};
	this.tickerObj = document.getElementById(divId);
	if (!this.tickerObj) {
		return _printError("Could not find a div element with id \"" + divId + "\"");
	}
	this.list = this.tickerObj.childNodes;
	if (this.list.length <= 0) {
		return _printError("The div element \"" + divId + "\" does not have any children");
	}
	if (arguments.length == 2) {
		this.pause = arguments[1].pause || 3;
		this.fadeSpeed = arguments[1].fadeSpeed || 40;
		this.fadeTo = arguments[1].fadeTo || "black";// only "black" is valid, otherwise it fades to white
	}
	if (this.fadeTo != "black") {
		this.hex = 0;
	}
	for (var i = 0; i < this.list.length; i++) {
		var node = this.list[i];
		if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) {
			this.tickerObj.removeChild(node);
		}
	}
	for (var i = 0; i < this.tickerObj.childNodes.length; i++) {
		this.tickerObj.childNodes[i].style.display = "none";
	}
	jsbeans.TickerRunner.addTicker(divId, this);
};

/**
 * Adds a new item that will be shown on ticker
 * @method addItem
 * @param node {DOM} the DOM containing the news
 * */
jsbeans.Ticker.prototype.addItem = function(domObject) {
	this.tickerObj.appendChild(domObject);
};

/**
 * @method _fadeText
 * @private
 * */
jsbeans.Ticker.prototype._fadeText = function() {
	if (this.tickerObj) {
		if (this.fadeTo == "black") {
			if (this.hex > 0) {
				this.hex -= 5; // increase color darkness
				this.tickerObj.style.color = "rgb(" + this.hex + "," + this.hex + "," + this.hex + ")";
				this.timeoutId = setTimeout("jsbeans.TickerRunner.fade('" + this.divId + "')", this.fadeSpeed);
			} 
			else {
				this.hex = 255; //reset hex value
			}
		} 
		else {
			if (this.hex > 255) {
				this.hex = 0; //reset hex value
			} 
			else {
				this.hex += 5; // decrease color darkness
				this.tickerObj.style.color = "rgb(" + this.hex + "," + this.hex + "," + this.hex + ")";
				this.timeoutId = setTimeout("jsbeans.TickerRunner.fade('" + this.divId + "')", this.fadeSpeed);
			}
		}
	}
};

/**
 * Starts showing news.
 * @method run
 * */
jsbeans.Ticker.prototype.run = function() {
	if (this.list.length <= 0) {
		return ;
	}
	this._fadeText();
	this.list[this.count].style.display = "block";
	if (this.count > 0) {
		this.list[this.count-1].style.display = "none";
	} 
	else {
		this.list[this.list.length-1].style.display = "none";
	}
	this.count++;
	if (this.count == this.list.length) {
		this.count = 0;
	}
	window.setTimeout("jsbeans.TickerRunner.run('" + this.divId + "')", this.pause * 1000);
};

/**
 * Stops animation. This method does NOT wait until the animation is complete, it just stops ticker.
 * @method stop
 * */
jsbeans.Ticker.prototype.stop = function() {
	jsbeans.TickerRunner.removeTicker(this);
};

/**
 * A string containing information on <code class="this">this</code> <code class="class">Ticker</code>
 * @method toString
 * @return {String}
 * */
jsbeans.Ticker.prototype.toString = function() {
	return "Ticker on \"" + this.divId +"\", fading at speed " + this.fadeSpeed + " and pausing for " + this.pause + " seconds.";
};

/**
 * Static class for jsbeans.Ticker.<br/>
 * FOR INTERNAL USE ONLY. DO NOT CALL IT DIRECTLY.
 * @namespace jsbeans
 * @class TickerRunner
 * @static
 * */
jsbeans.TickerRunner = {
	/**
	 * @property tickers
	 * @type Array
	 * @static
	 * @private
	 * */
	tickers: new Array(),
	/**
	 * @method fade
	 * @private
	 * @param id {String}
	 * @static
	 * */
	fade: function(divId) {
		if (this.tickers[divId] == null || typeof(this.tickers[divId]) == "undefined") {
			try {
				window.clearTimeout(divId + "_tickerTimeout");
			} 
			catch(e) {}
		} 
		else {
			this.tickers[divId]._fadeText();
		}
	},
	/**
	 * @method run
	 * @private
	 * @param id {String}
	 * @static
	 * */
	run: function(divId) {
		if (this.tickers[divId] == null || typeof(this.tickers[divId]) == "undefined") {
			try {
				window.clearTimeout(divId + "_tickerTimeout");
			} 
			catch(e) {}
		} 
		else {
			this.tickers[divId].run();
		}
	},
	/**
	 * @method addTicker
	 * @param id {String}
	 * @param object {DOM}
	 * @private
	 * @static
	 * */
	addTicker: function(divId, object) {
		this.tickers[divId] = object;
	},
	/**
	 * @method removeTicker
	 * @param ticker {jsbeans.Ticker}
	 * @private
	 * @static
	 * */
	removeTicker: function(t) {
		try {
			window.clearTimeout(t.timeoutId);
			this.tickers[t.divId] = null;
		} 
		catch(e) {}
	}
};

Copyright © 2016 Francesco Mele. All rights reserved.