Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('get', function (Y, NAME) {
2
 
3
    /**
4
    * NodeJS specific Get module used to load remote resources.
5
    * It contains the same signature as the default Get module so there is no code change needed.
6
    * @module get-nodejs
7
    * @class GetNodeJS
8
    */
9
 
10
    var Module = require('module'),
11
 
12
        path = require('path'),
13
        fs = require('fs'),
14
        request = require('request'),
15
        end = function(cb, msg, result) {
16
            if (Y.Lang.isFunction(cb.onEnd)) {
17
                cb.onEnd.call(Y, msg, result);
18
            }
19
        }, pass = function(cb) {
20
            if (Y.Lang.isFunction(cb.onSuccess)) {
21
                cb.onSuccess.call(Y, cb);
22
            }
23
            end(cb, 'success', 'success');
24
        }, fail = function(cb, er) {
25
            er.errors = [er];
26
            if (Y.Lang.isFunction(cb.onFailure)) {
27
                cb.onFailure.call(Y, er, cb);
28
            }
29
            end(cb, er, 'fail');
30
        };
31
 
32
 
33
    Y.Get = function() {
34
    };
35
 
36
    //Setup the default config base path
37
    Y.config.base = path.join(__dirname, '../');
38
 
39
    YUI.require = require;
40
    YUI.process = process;
41
 
42
    /**
43
    * Takes the raw JS files and wraps them to be executed in the YUI context so they can be loaded
44
    * into the YUI object
45
    * @method _exec
46
    * @private
47
    * @param {String} data The JS to execute
48
    * @param {String} url The path to the file that was parsed
49
    * @param {Function} cb The callback to execute when this is completed
50
    * @param {Error} cb.err=null Error object
51
    * @param {String} cb.url The URL that was just parsed
52
    */
53
 
54
    Y.Get._exec = function(data, url, cb) {
55
        if (data.charCodeAt(0) === 0xFEFF) {
56
            data = data.slice(1);
57
        }
58
 
59
        var mod = new Module(url, module);
60
        mod.filename = url;
61
        mod.paths = Module._nodeModulePaths(path.dirname(url));
62
        if (typeof YUI._getLoadHook === 'function') {
63
            data = YUI._getLoadHook(data, url);
64
        }
65
        mod._compile('module.exports = function (YUI) {' +
66
            'return (function () {'+ data + '\n;return YUI;}).apply(global);' +
67
        '};', url);
68
 
69
        /*global YUI:true */
70
        YUI = mod.exports(YUI);
71
 
72
        mod.loaded = true;
73
 
74
        cb(null, url);
75
    };
76
 
77
    /**
78
    * Fetches the content from a remote URL or a file from disc and passes the content
79
    * off to `_exec` for parsing
80
    * @method _include
81
    * @private
82
    * @param {String} url The URL/File path to fetch the content from
83
    * @param {Function} cb The callback to fire once the content has been executed via `_exec`
84
    */
85
    Y.Get._include = function (url, cb) {
86
        var cfg,
87
            mod,
88
            self = this;
89
 
90
        if (url.match(/^https?:\/\//)) {
91
            cfg = {
92
                url: url,
93
                timeout: self.timeout
94
            };
95
            request(cfg, function (err, response, body) {
96
                if (err) {
97
                    cb(err, url);
98
                } else {
99
                    Y.Get._exec(body, url, cb);
100
                }
101
            });
102
        } else {
103
            try {
104
                // Try to resolve paths relative to the module that required yui.
105
                url = Module._findPath(url, Module._resolveLookupPaths(url, module.parent.parent)[1]);
106
 
107
                if (Y.config.useSync) {
108
                    //Needs to be in useSync
109
                    mod = fs.readFileSync(url,'utf8');
110
                } else {
111
                    fs.readFile(url, 'utf8', function (err, mod) {
112
                        if (err) {
113
                            cb(err, url);
114
                        } else {
115
                            Y.Get._exec(mod, url, cb);
116
                        }
117
                    });
118
                    return;
119
                }
120
            } catch (err) {
121
                cb(err, url);
122
                return;
123
            }
124
 
125
            Y.Get._exec(mod, url, cb);
126
        }
127
    };
128
 
129
 
130
    /**
131
    * Override for Get.script for loading local or remote YUI modules.
132
    * @method js
133
    * @param {Array|String} s The URL's to load into this context
134
    * @param {Object} options Transaction options
135
    */
136
    Y.Get.js = function(s, options) {
137
        var urls = Y.Array(s), url, i, l = urls.length, c= 0,
138
            check = function() {
139
                if (c === l) {
140
                    pass(options);
141
                }
142
            };
143
 
144
 
145
        /*jshint loopfunc: true */
146
        for (i=0; i<l; i++) {
147
            url = urls[i];
148
            if (Y.Lang.isObject(url)) {
149
                url = url.url;
150
            }
151
 
152
            url = url.replace(/'/g, '%27');
153
            Y.Get._include(url, function(err, url) {
154
                if (!Y.config) {
155
                    Y.config = {
156
                        debug: true
157
                    };
158
                }
159
                if (options.onProgress) {
160
                    options.onProgress.call(options.context || Y, url);
161
                }
162
                if (err) {
163
                    fail(options, err);
164
                } else {
165
                    c++;
166
                    check();
167
                }
168
            });
169
        }
170
 
171
        //Keeping Signature in the browser.
172
        return {
173
            execute: function() {}
174
        };
175
    };
176
 
177
    /**
178
    * Alias for `Y.Get.js`
179
    * @method script
180
    */
181
    Y.Get.script = Y.Get.js;
182
 
183
    //Place holder for SS Dom access
184
    Y.Get.css = function(s, cb) {
185
        pass(cb);
186
    };
187
 
188
 
189
 
190
}, '3.18.1');