Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / jsbuilder / src / Loader.js
1 (function(ROOT) {
2
3 var Loader = ROOT.Loader = {
4     paths: {
5         _base: "."
6     },
7
8     existing: {},
9
10     exists: function(name) {
11         var root = ROOT;
12         var chunks = name.split('.');
13
14         for (var i = 0; i < chunks.length; i++) {
15             if (!root.hasOwnProperty(chunks[i]))
16                 return false;
17
18             root = root[chunks[i]];
19         }
20
21         return true;
22     },
23
24     setBasePath: function(name, path) {
25         if (!path) {
26             path = name;
27             name = "_base";
28         }
29
30         Loader.paths[name] = path;
31     },
32
33     getBasePath: function(name) {
34         // Start with the base path
35         var path = Loader.paths._base;
36
37         // Iterate through each specified name path ("Ext.layout.Layout" => "js/Ext/layout/Layout.js")
38         for (var stub in Loader.paths) {
39             if (stub === name.substring(0, stub.length)) {
40                 path += "/" + Loader.paths[stub];
41                 // Remove stub from name, as we've already pathed it
42                 name = name.substring(stub.length + 1);
43                 break;
44             }
45         }
46
47         return path + "/" + name.replace(/\./g, "/");
48     },
49
50     require: function(names, compulsory) {
51         if (compulsory == undefined)
52             compulsory = true;
53
54         if (typeof names == 'string')
55             names = [names];
56
57         names.forEach(function(name) {
58             if (!this.existing.hasOwnProperty(name)) {
59 //                writeln(this.getBasePath(name) + '.js');
60                 load(this.getBasePath(name) + '.js');
61
62                 var loaded = this.exists(name);
63                 this.existing[name] = loaded;
64             }
65
66             if (this.existing[name] == false && compulsory) {
67                 throw new Error("[Loader] Failed loading '" + name + "'");
68             }
69         }, Loader);
70     }
71 };
72
73 })(this);
74