Source: _loader_atom.js

"use strict";

// var log = require("xbrowser_debug").log;

var Xmit = require("xmit/src/xmit"),
	Channel = require("xmit/src/channel_xform"),

	// CacheItem = require("data_cache").Item,
	_load = require("./__methods").load,
	EVS = require("./_events");

/**  
 * @class _LoaderAtom 
 * @description 
 *   Non assertive version of the Loader class. ( not all methods appearing in docs.. methods are same as Loader)
 *   
 * @param {string} url
 * @description default url to be loaded by this class.
 * @param {function} method (optional)
 * @descrition the method to be used . default is GET
 * @param {object} XmitType (optional)
 * @description Allows overriding of the standard Xmit type
 * @param {object} ChannelType (optional)
 * @description Allows overriding of the standard Channel type
 * @memberof _LoaderAtom
 */

function _LoaderAtom(url, method, XmitType, ChannelType) { 
	var ts = this;
	ts.name = url.replace("http://", "") +"_loader";
	ts._url = url;
	ts._method = (method && method.toUpperCase()==="POST") ? "POST" : "GET";
	ts._setupXmit(url, XmitType||Xmit, ChannelType||Channel);
}

Object.defineProperties(_LoaderAtom.prototype, {
	"TYPE":{ 
		value:"LOADER_ATOM"
	},
	"FAMILY":{ 
		value:"LOADER"
	},
	/** 
	 * @function _setupXmit
	 * @description instantiates the Xmit instance 
	 * @param {object} XmitType
	 * @description the Xmit type to be instantiated
	 * @param {object} ChannelType
	 * @description the Channel Type to be instantiated
	 * @returns {void}
	 * @memberof _LoaderAtom
	 */
	"_setupXmit":{ 
		value:function(url, XmitType, ChannelType) { 
			var ts = this,
				xm = ts._xm = new Xmit(ts, "loader_" + 
				(url.slice(4, 8)==="://") ?
				url.slice(8) : url,
				ChannelType)
				.add(EVS.ON_LOAD)
				.add(EVS.ON_ERROR)
				.add(EVS.ON_REQUEST)
				.on(EVS.ON_LOAD, function(msg) { ts._on_load(msg)});
			return xm;
		}
	},
	/**
	 * @function fetch
	 * @description loads a file
     * @param {string} url (optional)
     * @description the url to be loaded.  if none, uses this._url
     * @param {string} method (optional) 
     * @description the method to use when loading.  If none, uses this._method
     * @returns {object} Promise
     * @memberof _LoaderAtom
     */
	"fetch":{ 
		value:function(url, method) {
			var ts = this;
			if (ts._locked===true) return;
			return _load(method||ts._method, url||ts._url, ts._xm);
		}
	},
	/** 
	 * @function _on_load
 	 * @description internal use function to unlock the loader if 
 	 * a fetch (load) is completed.
 	 * @memberof Bleh
 	 */
	"_on_load":{ 
		value:function(msg) { 
			this._locked = false;
		}
	},
	/** 
	 * @function _on_error
 	 * @description internal use function to unlock the loader if 
 	 * a fetch (load) is throws an error.
 	 * @memberof _LoaderAtom
 	 */
	"_on_error":{ 
		value:function(msg) { 
			this._locked = false;
		}
	},
	/**
	 * @function on
	 * @description proxies the xmit.on function to the loader API 
     * @param {*} channel_name
     * @description channel_name should be either a number or a string
     * @param {function} lambda (optional) 
     * @description the calback method 
     * @returns {object} loader instance for continuations\
     * @memberof _LoaderAtom
     */
	"on":{ 
		value:function(channel_name, lambda) { 
			this._xm.on(channel_name, lambda);
			return this
		}
	},
	/**
	 * @function loaded
	 * @description proxies xmit.on(EVS.ON_LOAD) to the loader API
     * @param {function} lambda (optional) 
     * @description the calback method 
     * @returns {object} loader instance for continuations
     * @memberof _LoaderAtom
     */
	"loaded":{ 
		value:function(lambda) { 
			this._xm.on(EVS.ON_LOAD, lambda)
		}
	},
	/**
	 * @function error
	 * @description proxies xmit.on(EVS.ON_ERROR) to the loader API
     * @param {function} lambda (optional) 
     * @description the calback method 
     * @returns {object} loader instance for continuations
     * @memberof LoaderAtom
     */
	"error":{
		value:function(lambda) { 
			this._xm.on(EVS.ON_ERROR, lambda);
		}
	}
	}
);
module.exports = _LoaderAtom;