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 ?$.include.IEonload(css,onload,callback) :onload(callback);//other browsers do not support it
82
        },
83
        loadJS: function(url,onload,callback){
84
            var js=document.createElement('script');
85
            js.setAttribute('type','text/javascript');
86
            js.setAttribute('src',''+url);
87
            $.browser.msie
88
                ?$.include.IEonload(js,onload,callback)
89
                :js.onload = function(){onload(callback)};
90
            $('head').get(0).appendChild(js);
91
        },
92
        IEonload: function(elm,onload,callback){
93
            elm.onreadystatechange = 
94
                    function(){
95
                        if(this.readyState=='loaded'||this.readyState=='complete')
96
                            onload(callback);
97
                    }
98
        },
99
        exist: function(url){
100
            var fresh = false;
101
            $('head script').each(
102
                                function(){
103
                                    if(/.css$/.test(url)&&this.href==url)
104
                                            return fresh=true;
105
                                    else if(/.js$/.test(url)&&this.src==url)
106
                                            return fresh=true;
107
                                }
108
                            );
109
            return fresh;
110
        }
111
    }
112
);
113
//
114
})(jQuery);
(7-7/24)