all files / canijs/ cani.js

98.68% Statements 75/76
79.17% Branches 38/48
100% Functions 11/11
100% Lines 66/66
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121               20× 17×                     11× 11×   11×                                               12× 12×                            
Eif(typeof require === 'function'){
    Q = require('q');
}
var Cani = Cani || {};
try{
    if(window) window.Cani = Cani;
}catch(e){}
 
Cani.core = (function(core){
    // core is just notifications and confirmations
    
    var config = {};
    core.config = config;
 
    var note = {};//notifications
 
    core.cast = function(asset, flush, params){
	if(typeof asset === 'string'){
	    if(typeof note[asset] === 'undefined') return;
	    for(var i=0; i<note[asset].length; ++i) note[asset][i](params);
	    if(flush) note[asset] = [];
	}else Eif(asset instanceof RegExp){
	    for(var ff in note){
		if(asset.test(ff)){
		    for(var i=0; i<note[ff].length; ++i) note[ff][i](params);
		    Eif(flush) note[ff] = [];
		}
	    }
	}
    };
 
    core.on = function(asset, tino){
	if(typeof note[asset] === 'undefined') note[asset] = [];
	note[asset].push(tino);
	// in order to achieve multi-asset casting, use the affirmation system
	// ie, when something loads, affirm. to wait on something, confirm().then
    };
 
    var assets = {};
    core.assets = assets;
 
    core.confirm = function(asset, prefix){
	if(!prefix) prefix = 'confirm';
	var deferred = Q.defer();
 
	if(typeof asset === 'string'){
	    if((prefix!=='defirm')&&(asset in assets)){
		deferred.resolve(assets[asset]);
	    }else{
		// register note for confirmation
		Eif(typeof note[prefix+': '+asset] === 'undefined'){
		    note[prefix+': '+asset] = [];
		}
		core.on(prefix+': '+asset, function(){
		    deferred.resolve(assets[asset]);
		});
	    }
  	    return deferred.promise;
 
	}else Eif(typeof asset === 'object'){
	    // check that all of the assets are present, return as collection
 
	    // cheeky way to do this is to find the first missing asset
	    //    then on it's load register the same confirm
	    // if everything is here send it all back
	    // technically this would work for arrays of arrays of assets. WoAh.
	    var everything = true;
	    for(var se in asset){
		var s = (asset.constructor == Array)? asset[se] : se;
		if(!(s in assets)){
		    everything = false;
		    core.confirm(s).then(function(singleAsset){
			core.confirm(asset).then(function(allAssets){
			    deferred.resolve(allAssets);
			});
		    });
		    break;
		}
	    }
	    if(everything){
		var ret = {};
		for(var se in asset){
		    var s = (asset.constructor == Array)? asset[se] : se;
		    ret[s] = assets[s];
		}
		deferred.resolve(ret);
	    }
	    return deferred.promise;
	}
    };
 
    core.disconfirm = function(asset){
	return core.confirm(asset, 'defirm');
    };
 
    core.affirm = function(asset, module){
	Eif(!(asset in assets)) assets[asset] = module;
	core.cast('confirm: '+asset, true);
    };
 
    core.defirm = function(asset, params){
	Eif(Object.keys(assets).indexOf(asset)>-1) delete assets[asset];
	core.cast('defirm: '+asset, true, params);
    };
 
    core.boot = function(conf){
	// any config requiring something else to register on that thing's cast
	// also, if the config file were to determine boot, this would be the place to read that
 
	config = conf;
	return core.cast(/config/, true, conf);
    };
 
    return core;
 
})(Cani.core||{});
 
Eif(typeof require === 'function'){
    module.exports = Cani;
}