source: trunk/prototype/plugins/lazy/jquery.lazy.source.js @ 5911

Revision 5911, 4.3 KB checked in by gustavo, 12 years ago (diff)

Ticket #2611 - Refatorar interface de criação de nova mensagem

Line 
1/**
2*       @name                                                   Lazy
3*       @descripton                                             Lazy is Jquery plugin that lazy loads Jquery plugins
4*       @version                                                1.5
5*       @requires                                               Jquery 1.2.6+
6*       @author                                                 Jan Jarfalk jan.jarfalk@unwrongest.com
7*
8*       @param {String} src                             Path to the plugin you want to load
9*       @param {String} name                    Function name of the plugin
10*       @param {Hash} dependencies              Hash with the keys js and css
11*               @param {Array} js                       Array of paths to javascript dependencies
12*               @param {Array} css                      Array of paths to css dependencies
13*       @param {Bool} cache                             Enable or disable caching
14*/
15
16(function($) {
17       
18        $.lazy = function(options)
19        {
20               
21                // Handle string name
22                if(typeof options.name == 'string'){
23                        options.name = [options.name];
24                }
25               
26                $.each(options.name, function(i){
27               
28                        // Create local variables
29                        var src = options.src,
30                                name = options.name[i],
31                                cache = options.cache || true,
32                                isFunction = options.isFunction || true,
33                                isMethod = options.isMethod || true,
34                                self, arg, object = {};
35                       
36                        // Add plugin to the archive
37                        $.lazy.archive[src] = {'status':'unloaded','que':[]};
38                       
39                       
40                        // Add a CSS file to the document
41                        function loadCSS(src,callback,self,name,arg){
42                               
43                                $.lazy.archive[src].status = "loading";
44                               
45                                var node = document.createElement('link');
46                                node.type = 'text/css';
47                                node.rel = 'stylesheet';
48                                node.href = src;
49                                node.media = 'screen';
50                                document.getElementsByTagName("head")[0].appendChild(node);
51                               
52                                $.lazy.archive[src].status = 'loaded';
53       
54                                if(callback)
55                                        callback(self,name,arg);
56                        }
57                       
58                        // Add a JS file to the document
59                        function loadJS(src,callback,self,name,arg){
60                               
61                                $.lazy.archive[src].status = "loading";
62                                       
63                                $.ajax({
64                                        type: "GET",
65                                        url: src,
66                                        cache: cache,
67                                        dataType: "script",
68                                        success: function(){
69                                                $.lazy.archive[src].status = 'loaded';
70                                                if(callback) {
71                                                        callback(self,name,arg);
72                                                }
73                                        }
74                                });
75                               
76                        }
77                       
78                        // Wrapper for loadJS for the actual plugin file
79                        function loadPlugin(self, name, arg){
80                               
81                                function callback(){
82                                        if(typeof self == 'object'){
83                                                if(arg.length > 0){
84                                                        $(self)[name].apply(self,arg);
85                                                } else {
86                                                        $(self)[name]();
87                                                }
88                                        } else {
89                                                $[name].apply(null,arg);
90                                        }
91                                       
92                                        $.each($.lazy.archive[src].que,function(i){
93                                                var queItem = $.lazy.archive[src].que[i];
94                                                object[queItem.name].apply(queItem.self,queItem.arguments);
95                                        });
96                                        $.lazy.archive[src].que = [];
97                                }
98                               
99                                loadJS(src,callback,self,name,arg);
100                        }
101                       
102                        // Proxy function
103                        function proxy() {
104       
105                                var self = this;
106                                arg = arguments;
107                                       
108                                if( $.lazy.archive[src].status === 'loaded' ) {
109                               
110                                        $.each(this,function(){
111                                                $(this)[name].apply(self,arg);
112                                        });
113                                       
114                                } else if ( $.lazy.archive[src].status === 'loading' ) {
115                                       
116                                        $.lazy.archive[src].que.push({'name':name,'self':self,'arguments':arg});
117                                                                                       
118                                } else {
119                               
120                                        $.lazy.archive[src].status = 'loading';
121                                       
122                                        if ( options.dependencies ) {
123                                               
124                                                var css = options.dependencies.css || [],
125                                                        js = options.dependencies.js || [];
126                                               
127                                                var total = css.length + js.length;
128                                               
129                                                function loadDependencies(array, callback, callbackCallback){
130                                                                               
131                                                        var length = array.length,
132                                                                src;
133                                                       
134                                                        array = array.reverse();
135                                                       
136                                                        while( length-- && total-- ){
137                                                               
138                                                               
139                                                                src = array[length];
140                                                               
141                                                                if(typeof $.lazy.archive[src] == 'undefined') {
142                                                                        $.lazy.archive[src] = {'status':'unloaded','que':[]};
143                                                                }
144                                                               
145                                                                if($.lazy.archive[src].status === 'unloaded'){
146                                                                       
147                                                                        if(!total) {
148                                                                       
149                                                                                callback(src,function(){
150                                                                                        loadPlugin(self,name,arg);
151                                                                                });
152                                                                               
153                                                                        } else {
154                                                                       
155                                                                                callback(src);
156                                                                               
157                                                                        }
158                                                                       
159                                                                } else if( !total ) {
160                                                               
161                                                                        loadPlugin(self,name,arg);
162                                                                       
163                                                                }
164                                                        }
165                                                }
166                                               
167                                                loadDependencies(css, loadCSS);
168                                                loadDependencies(js, loadJS);
169                                       
170                                        } else {
171                                                loadPlugin(self,name,arg);
172                                               
173                                        }
174                                }
175                                       
176                                return this;
177                        };
178                       
179                        object[name] = proxy;
180                       
181                        // Attach Methods to jQuery.fn object
182                        if(isMethod){
183                                jQuery.fn.extend(object);
184                        }
185                       
186                        // Attach Function to jQuery object
187                        if(isFunction){
188                                jQuery.extend(object);
189                        }
190                });
191        };
192       
193        $.lazy.archive = {};
194
195})(jQuery);
Note: See TracBrowser for help on using the repository browser.