jsbeans

jsbeans  1.0.0

jsbeans > jsbeans > PleaseWait.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.
 */
/**
 * Disables buttons until response has come back and/or for an user-defined interval. Useful for AJAX form's submition.
 * @namespace jsbeans
 * @class PleaseWait
 * @static
 */
jsbeans.PleaseWait = {
	/**
	 * @property obj
	 * @type DOM
	 * @private
	 * */
	obj: null,
	/**
	 * @method init
	 * @param [options] {JSON}
	 * <pre>
	 * enablingClassName: String, // tells which style class will be used when button is re-enabled. Optional
	 * disablingClassName: String, // tells which style class will be used when button is disabled. Optional
	 * disablingCursor: String, // tells what to do with cursor's style. Optional with default "wait"
	 * waitingTime: Integer // number of seconds until method stop is fired. If -1 it won't be fired. Optional with default -1.
	 * </pre>
	 * @static
	 * */
	init: function(options) {
		var o = options || {};
		this._options.enablingClassName = o.enablingClassName || this._options.enablingClassName;
		this._options.disablingClassName = o.disablingClassName || this._options.disablingClassName;
		this._options.disablingCursor = o.disablingCursor || this._options.disablingCursor;
		this._options.waitingTime = o.waitingTime || this._options.waitingTime;
	},
	/**
	 * @property _options
	 * @type JSON
	 * @private
	 * @static
	 * */
	_options: {
		enablingClassName: null,
		disableInput: true,
		disablingClassName: null,
		disablingCursor: "wait",
		waitingTime: -1,
		onStart: null,
		onStop: null
	},
	/**
	 * Whan fired {@param} <code class="param">node</code>, usually a button or an input submit, is disabled and predefined style class are applied.<br/>
	 * If {@param} <code class="param">node</code> is <code>null</code> or <code>undefined</code> this method will exit silently.
	 * @method start
	 * @param node {DOM} the disabled DOM object
	 * @param [options] {JSON} same as <code class="methd">init</code> plus <code>onStart</code> and <code>onStop</code> functions called just before disabling and after re-enabling calls.  
	 * @static
	 * */
	start: function(obj) {
		if (obj == null || typeof(obj) == "undefined") {
			return;
		} 
		else {
			var o = arguments[1] || this._options;
			o.enablingClassName = o.enablingClassName || this._options.enablingClassName;
			o.disablingClassName = o.disablingClassName || this._options.disablingClassName;
			o.disablingCursor = o.disablingCursor || this._options.disablingCursor;
			o.waitingTime = o.waitingTime || this._options.waitingTime;
			o.disableInput = typeof(o.disableInput) == "undefined" ? this._options.disableInput : o.disableInput;
			o.onStart = o.onStart || this._options.onStart;
			o.onStop = o.onStop || this._options.onStop;

			var cnSplitted = obj.className.split(" ");
			var renewClassName = "";
			for (var i = 0, c; c = cnSplitted[i]; i++) {
				if (c != o.disablingClassName) {
					renewClassName += c;
				}
			}
			if (o.onStart != null && jsbeans.PleaseWait._isFun(o.onStart)) {
				o.onStart(obj, o);
			}
			obj.className = renewClassName + " " + o.disablingClassName;
			
			obj.disabled = o.disableInput ? "disabled" : "";
			document.body.style.cursor = o.disablingCursor;
			if (o.waitingTime > 0) {
				setTimeout("jsbeans.PleaseWait.stop('" + obj.id + "')", o.waitingTime * 1000);
			}
			jsbeans.PleaseWait._CACHE.push({id: obj.id, object: obj, options: o});
		}
	},
	/**
	 * Re-enables the last disabled object or the one indicated by its {@param} <code class="param">id</code>.
	 * @method stop
	 * @param [id] {String}
	 * @static
	 * */
	stop: function(id) {
		var item = null;
		if (id == null || typeof(id) == "undefined") {
			item = jsbeans.PleaseWait._CACHE.pop();
		} 
		else {
			var items = jsbeans.PleaseWait._CACHE;
			for (var i = 0; i < items.length; i++) {
				if (items[i].id == id) {
					item = items[i];
					break;
				}
			}
		}
		if (item == null || typeof(item) == "undefined") {
			return;
		}
		var obj = item.object;
		obj.disabled = false;
		
		var cnSplitted = obj.className.split(" ");
		var renewClassName = "";
		// always remove the disablingClassName
		for (var i = 0, c; c = cnSplitted[i]; i++) {
			if (c != this._options.disablingClassName) {
				renewClassName += c;
			}
		}
		obj.className = renewClassName + " " + item.options.enablingClassName != null ? item.options.enablingClassName : "";

		document.body.style.cursor = "default";
		if (item.options.onStop != null && jsbeans.PleaseWait._isFun(item.options.onStop)) {
			item.options.onStop(obj, item.options);
		}
	},
	/**
	 * @property _CACHE
	 * @type Array
	 * @private
	 * @static
	 * */
	_CACHE: new Array(),
	/**
	 * @method _isFun
	 * @param fn {Object}
	 * @return boolean
	 * @private
	 * @static
	 * */
	_isFun: function(fn) {
		return !!fn && typeof fn != "string" && !fn.nodeName && 
		fn.constructor != Array && /function/i.test( fn + "" );
	}
};

Copyright © 2016 Francesco Mele. All rights reserved.