5 var JSAN = function () { JSAN.addRepository(arguments) }
13 JSAN.globalScope = self;
14 JSAN.includePath = ['.', 'lib'];
15 JSAN.errorLevel = "none";
16 JSAN.errorMessage = "";
23 JSAN.use = function () {
24 var classdef = JSAN.require(arguments[0]);
25 if (!classdef) return null;
27 var importList = JSAN._parseUseArgs.apply(JSAN, arguments).importList;
28 JSAN.exporter(classdef, importList);
37 JSAN.require = function (pkg) {
38 var path = JSAN._convertPackageToPath(pkg);
39 if (JSAN.loaded[path]) {
40 return JSAN.loaded[path];
44 var classdef = eval(pkg);
45 if (typeof classdef != 'undefined') return classdef;
46 } catch (e) { /* nice try, eh? */ }
49 for (var i = 0; i < JSAN.includePath.length; i++) {
52 var url = JSAN._convertPathToUrl(path, JSAN.includePath[i]);
53 js = JSAN._loadJSFromUrl(url);
55 if (i == JSAN.includePath.length - 1) throw e;
58 var classdef = JSAN._createScript(js, pkg);
59 JSAN.loaded[path] = classdef;
71 JSAN.exporter = function () {
72 JSAN._exportItems.apply(JSAN, arguments);
79 JSAN.addRepository = function () {
80 var temp = JSAN._flatten( arguments );
81 // Need to go in reverse to do something as simple as unshift( @foo, @_ );
82 for ( var i = temp.length - 1; i >= 0; i-- )
83 JSAN.includePath.unshift(temp[i]);
87 JSAN._flatten = function( list1 ) {
88 var list2 = new Array();
89 for ( var i = 0; i < list1.length; i++ ) {
90 if ( typeof list1[i] == 'object' ) {
91 list2 = JSAN._flatten( list1[i], list2 );
94 list2.push( list1[i] );
100 JSAN._findMyPath = function () {
102 var scripts = document.getElementsByTagName('script');
103 for ( var i = 0; i < scripts.length; i++ ) {
104 var src = scripts[i].getAttribute('src');
106 var inc = src.match(/^(.*?)\/?JSAN.js/);
109 for (var j = 0; j < JSAN.includePath.length; j++) {
110 if (JSAN.includePath[j] == repo) {
114 JSAN.addRepository(repo);
122 JSAN._convertPathToUrl = function (path, repository) {
123 return repository.concat('/' + path);
127 JSAN._convertPackageToPath = function (pkg) {
128 var path = pkg.replace(/\./g, '/');
129 path = path.concat('.js');
133 JSAN._parseUseArgs = function () {
134 var pkg = arguments[0];
137 for (var i = 1; i < arguments.length; i++)
138 importList.push(arguments[i]);
142 importList: importList
146 JSAN._loadJSFromUrl = function (url) {
147 return new JSAN.Request().getText(url);
150 JSAN._findExportInList = function (list, request) {
151 if (list == null) return false;
152 for (var i = 0; i < list.length; i++)
153 if (list[i] == request)
158 JSAN._findExportInTag = function (tags, request) {
159 if (tags == null) return [];
166 JSAN._exportItems = function (classdef, importList) {
167 var exportList = new Array();
168 var EXPORT = classdef.EXPORT;
169 var EXPORT_OK = classdef.EXPORT_OK;
170 var EXPORT_TAGS = classdef.EXPORT_TAGS;
172 if (importList.length > 0) {
173 importList = JSAN._flatten( importList );
175 for (var i = 0; i < importList.length; i++) {
176 var request = importList[i];
177 if ( JSAN._findExportInList(EXPORT, request)
178 || JSAN._findExportInList(EXPORT_OK, request)) {
179 exportList.push(request);
182 var list = JSAN._findExportInTag(EXPORT_TAGS, request);
183 for (var i = 0; i < list.length; i++) {
184 exportList.push(list[i]);
190 JSAN._exportList(classdef, exportList);
193 JSAN._exportList = function (classdef, exportList) {
194 if (typeof(exportList) != 'object') return null;
195 for (var i = 0; i < exportList.length; i++) {
196 var name = exportList[i];
198 if (JSAN.globalScope[name] == null)
199 JSAN.globalScope[name] = classdef[name];
203 JSAN._makeNamespace = function(js, pkg) {
204 var spaces = pkg.split('.');
205 var parent = JSAN.globalScope;
207 var classdef = eval(pkg);
208 for (var i = 0; i < spaces.length; i++) {
209 var name = spaces[i];
210 if (i == spaces.length - 1) {
211 if (typeof parent[name] == 'undefined') {
212 parent[name] = classdef;
213 if ( typeof classdef['prototype'] != 'undefined' ) {
214 parent[name].prototype = classdef.prototype;
218 if (parent[name] == undefined) {
223 parent = parent[name];
228 JSAN._handleError = function (msg, level) {
229 if (!level) level = JSAN.errorLevel;
230 JSAN.errorMessage = msg;
240 throw new Error(msg);
245 JSAN._createScript = function (js, pkg) {
247 return JSAN._makeNamespace(js, pkg);
249 JSAN._handleError("Could not create namespace[" + pkg + "]: " + e);
256 use: function () { JSAN.use.apply(JSAN, arguments) }
260 // Low-Level HTTP Request
261 JSAN.Request = function (jsan) {
262 if (JSAN.globalScope.XMLHttpRequest) {
263 this._req = new XMLHttpRequest();
265 this._req = new ActiveXObject("Microsoft.XMLHTTP");
269 JSAN.Request.prototype = {
272 getText: function (url) {
273 this._req.open("GET", url, false);
275 this._req.send(null);
276 if (this._req.status == 200 || this._req.status == 0)
277 return this._req.responseText;
279 JSAN._handleError("File not found: " + url);
283 JSAN._handleError("File not found: " + url);