~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to lib/yuilib/3.9.1/build/get-nodejs/get-nodejs.js

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

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