Project

General

Profile

1
(function($){
2
/*
3
 * includeMany 1.2.2
4
 *
5
 * Copyright (c) 2009 Arash Karimzadeh (arashkarimzadeh.com)
6
 * Licensed under the MIT (MIT-LICENSE.txt)
7
 * http://www.opensource.org/licenses/mit-license.php
8
 *
9
 * Date: Dec 03 2009
10
 */
11
$.chainclude = function(urls,finaly){
12
	var onload = function(callback,data){
13
						if(typeof urls.length!='undefined'){
14
							if(urls.length==0)
15
								return $.isFunction(finaly)
16
											?finaly(data)
17
											:null;
18
							urls.shift();
19
							return $.chainclude.load(urls,onload);
20
						}
21
						for(var item in urls){
22
							urls[item](data);
23
							delete urls[item];
24
							var count = 0;
25
							for(var i in urls)
26
								count++;
27
							return (count==0)
28
										?$.isFunction(finaly)?finaly(data):null
29
										:$.chainclude.load(urls,onload);
30
						}
31
					}
32
	$.chainclude.load(urls,onload);
33
};
34
$.chainclude.load = function(urls,onload){
35
	if(typeof urls=='object' && typeof urls.length=='undefined')
36
		for(var item in urls)
37
			return $.include.load(item,onload,urls[item].callback);
38
	urls = $.makeArray(urls);
39
	$.include.load(urls[0],onload,null);
40
};
41
$.include = function(urls,finaly){
42
	var luid = $.include.luid++;
43
	var onload = function(callback,data){
44
						if($.isFunction(callback))
45
							callback(data);
46
						if(--$.include.counter[luid]==0&&$.isFunction(finaly))
47
							finaly();
48
					}
49
	if(typeof urls=='object' && typeof urls.length=='undefined'){
50
		$.include.counter[luid] = 0;
51
		for(var item in urls)
52
			$.include.counter[luid]++;
53
		return $.each(urls,function(url,callback){$.include.load(url,onload,callback);});
54
	}
55
	urls = $.makeArray(urls);
56
	$.include.counter[luid] = urls.length;
57
	$.each(urls,function(){$.include.load(this,onload,null);});
58
}
59
$.extend(
60
	$.include,
61
	{
62
		luid: 0,
63
		counter: [],
64
		load: function(url,onload,callback){
65
			url = url.toString();
66
			if($.include.exist(url))
67
				return onload(callback);
68
			if(/.css$/.test(url))
69
				$.include.loadCSS(url,onload,callback);
70
			else if(/.js$/.test(url))
71
				$.include.loadJS(url,onload,callback);
72
			else
73
				$.get(url,function(data){onload(callback,data)});
74
		},
75
		loadCSS: function(url,onload,callback){
76
			var css=document.createElement('link');
77
			css.setAttribute('type','text/css');
78
			css.setAttribute('rel','stylesheet');
79
			css.setAttribute('href',''+url);
80
			$('head').get(0).appendChild(css);
81
			$.browser.msie
82
				?$.include.IEonload(css,onload,callback)
83
				:onload(callback);//other browsers do not support it
84
		},
85
		loadJS: function(url,onload,callback){
86
			var js=document.createElement('script');
87
			js.setAttribute('type','text/javascript');
88
			js.setAttribute('src',''+url);
89
			$.browser.msie
90
				?$.include.IEonload(js,onload,callback)
91
				:js.onload = function(){onload(callback)};
92
			$('head').get(0).appendChild(js);
93
		},
94
		IEonload: function(elm,onload,callback){
95
			elm.onreadystatechange = 
96
					function(){
97
						if(this.readyState=='loaded'||this.readyState=='complete')
98
							onload(callback);
99
					}
100
		},
101
		exist: function(url){
102
			var fresh = false;
103
			$('head script').each(
104
								function(){
105
									if(/.css$/.test(url)&&this.href==url)
106
											return fresh=true;
107
									else if(/.js$/.test(url)&&this.src==url)
108
											return fresh=true;
109
								}
110
							);
111
			return fresh;
112
		}
113
	}
114
);
115
//
116
})(jQuery);
(5-5/17)