Merge branch 'MDL-32509' of git://github.com/danpoltawski/moodle
[moodle.git] / lib / yui / 3.5.0 / build / get-nodejs / get-nodejs.js
blob7a739f924266fca8922cf6992e09fb43f562f0e4
1 /*
2 YUI 3.5.0 (build 5089)
3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
7 YUI.add('get-nodejs', function(Y) {
9     /**
10     * NodeJS specific Get module used to load remote resources. It contains the same signature as the default Get module so there is no code change needed.
11     * Note: There is an added method called Get.domScript, which is the same as Get.script in a browser, it simply loads the script into the dom tree
12     * so that you can call outerHTML on the document to print it to the screen.
13     * @module get-nodejs
14     */
16         var path = require('path');
18         Y.config.base = path.join(__dirname, '../');
19         console.log(Y.config);
21         YUI.add('get', function() { });
22         
23         var end = function(cb, msg, result) {
24             if (Y.Lang.isFunction(cb.onEnd)) {
25                 cb.onEnd.call(Y, msg, result);
26             }
27         }, pass = function(cb) {
28             if (Y.Lang.isFunction(cb.onSuccess)) {
29                 cb.onSuccess.call(Y, cb);
30             }
31             end(cb, 'success', 'success');
32         }, fail = function(cb, er) {
33             if (Y.Lang.isFunction(cb.onFailure)) {
34                 cb.onFailure.call(Y, er, cb);
35             }
36             end(cb, er, 'fail');
37         };
39         Y.Get = function() {};
41         /**
42         * Override for Get.script for loading local or remote YUI modules.
43         */
44         Y.Get.script = function(s, cb) {
45             var A = Y.Array,
46                 urls = A(s), url, i, l = urls.length;
47             for (i=0; i<l; i++) {
48                 url = urls[i];
50                 url = url.replace(/'/g, '%27');
51                 // doesn't need to be blocking, so don't block.
52                 include(url, function(err) {
53                     if (!Y.config) {
54                         Y.config = {
55                             debug: true
56                         };
57                     }
58                     if (err) {
59                         if (err.stack) {
60                             A.each(err.stack.split('\n'), function(frame) {
61                             });
62                         } else {
63                             console.log(err);
64                         }
65                     } else {
66                         pass(cb);
67                     }
68                 });
69             }
70         };
73     
74     var vm = require('vm'),
75         fs = require('fs');
78     var include = function(url, cb) {
79         var mod = fs.readFileSync(url, 'utf8');
80         var script = vm.createScript(mod, url);
81         var box = {
82             YUI: {
83                 add: function() {
84                     console.log('YUI in the sandbox');
85                     console.log(arguments);
86                     YUI.apply(YUI, arguments);
87                     cb();
88                 }
89             }
90         };
91         script.runInNewContext(box);
92         
93     };
97 }, '3.5.0' ,{requires:['yui-base']});