      1 /**
      2  * Namespace for entire Xquared classes
      3  */
      4 var xq = {
      5 	majorVersion: '0.1',
      6 	minorVersion: '2007119'
      7 };
      8
      9 /**
     10  * Make given object as event source
     11  *
     12  * @param {Object} object target object
     13  * @param {String} prefix prefix for generated functions
     14  * @param {Array} events array of string which contains name of events
     15  */
     16 xq.asEventSource = function(object, prefix, events) {
     17 	object._listeners = []
     18 	object._registerEventFirer = function(prefix, name) {
     19 		this["_fireOn" + name] = function() {
     20 			for(var i = 0; i < this._listeners.length; i++) {
     21 				var listener = this._listeners[i];
     22 				var func = listener["on" + prefix + name];
     23 				if(func) func.apply(listener, $A(arguments));
     24 			}
     25 		}
     26 	}
     27 	object.addListener = function(l) {
     28 		this._listeners.push(l);
     29 	}
     30
     31 	for(var i = 0; i < events.length; i++) {
     32 		object._registerEventFirer(prefix, events[i]);
     33 	}
     34 }
     35
     36 /**
     37  * Returns the index of given element
     38  *
     39  * @returns {Number} index or -1
     40  */
     41 Array.prototype.indexOf = function(n) {
     42 	for(var i = 0; i < this.length; i++) {
     43 		if(this[i] == n) return i;
     44 	}
     45
     46 	return -1;
     47 }
     48
     49 Date.preset = null;
     50 Date.pass = function(msec) {
     51 	if(Date.preset == null) return;
     52 	Date.preset = new Date(Date.preset.getTime() + msec);
     53 }
     54 Date.get = function() {
     55 	return Date.preset == null ? new Date() : Date.preset;
     56 }
     57 Date.prototype.elapsed = function(msec) {
     58 	return Date.get().getTime() - this.getTime() >= msec;
     59 }
     60
     61 String.prototype.merge = function(data) {
     62 	var newString = this;
     63 	for(k in data) {
     64 		newString = newString.replace("{" + k + "}", data[k]);
     65 	}
     66 	return newString;
     67 }
     68
     69 String.prototype.parseURL = function() {
     70 	var m = this.match(/((((\w+):\/\/(((([^@:]+)(:([^@]+))?)@)?([^:\/\?#]+)?(:(\d+))?))?([^\?#]+)?)(\?([^#]+))?)(#(.+))?/);
     71
     72 	var includeAnchor = m[0];
     73 	var includeQuery = m[1] || undefined;
     74 	var includePath = m[2] || undefined;
     75 	var includeHost = m[3] || undefined;
     76 	var includeBase = null;
     77 	var protocol = m[4] || undefined;
     78 	var user = m[8] || undefined;
     79 	var password = m[10] || undefined;
     80 	var domain = m[11] || undefined;
     81 	var port = m[13] || undefined;
     82 	var path = m[14] || undefined;
     83 	var query = m[16] || undefined;
     84 	var anchor = m[18] || undefined;
     85
     86 	if(!path || path == '/') {
     87 		includeBase = includeHost + '/';
     88 	} else {
     89 		var index = path.lastIndexOf('/');
     90 		includeBase = includeHost + path.substring(0, index + 1);
     91 	}
     92
     93 	return {
     94 		includeAnchor: includeAnchor,
     95 		includeQuery: includeQuery,
     96 		includePath: includePath,
     97 		includeBase: includeBase,
     98 		includeHost: includeHost,
     99 		protocol: protocol,
    100 		user: user,
    101 		password: password,
    102 		domain: domain,
    103 		port: port,
    104 		path: path,
    105 		query: query,
    106 		anchor: anchor
    107 	};
    108 }
    109
    110 xq.findXquaredScript = function() {
    111     return $A(document.getElementsByTagName("script")).find(function(script) {
    112     	return script.src && script.src.match(/xquared\.js/i);
    113     });
    114 }
    115 xq.shouldLoadOthers = function() {
    116 	var script = xq.findXquaredScript();
    117     return script && !!script.src.match(/xquared\.js\?load_others=1/i);
    118 }
    119 xq.loadScript = function(url) {
    120     document.write('<script type="text/javascript" src="' + url + '"></script>');
    121 }
    122 xq.loadOthers = function() {
    123 	var script = xq.findXquaredScript();
    124 	var basePath = script.src.match(/(.*\/)xquared\.js.*/i)[1];
    125 	var others = [
    126 		'Editor.js',
    127 		'Browser.js',
    128 		'Shortcut.js',
    129 		'DomTree.js',
    130 		'RichDom.js',
    131 		'RichDomW3.js',
    132 		'RichDomGecko.js',
    133 		'RichDomWebkit.js',
    134 		'RichDomTrident.js',
    135 		'RichTable.js',
    136 		'Validator.js',
    137 		'ValidatorW3.js',
    138 		'ValidatorGecko.js',
    139 		'ValidatorWebkit.js',
    140 		'ValidatorTrident.js',
    141 		'EditHistory.js',
    142 		'Controls.js',
    143 		'_ui_templates.js'
    144 	];
    145 	others.each(function(name) {
    146 		xq.loadScript(basePath + name);
    147 	});
    148 }
    149
    150 if(xq.shouldLoadOthers()) xq.loadOthers();