source: gs3-extensions/seaweed-debug/trunk/src/Bootstrapper.js@ 25160

Last change on this file since 25160 was 25160, checked in by sjm84, 12 years ago

Initial cut at a version of seaweed for debugging purposes. Check it out live into the web/ext folder

File size: 6.5 KB
Line 
1/*
2 * file: Bootstrapper.js
3 *
4 * @BEGINLICENSE
5 * Copyright 2010 Brook Novak (email : [email protected])
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 * @ENDLICENSE
18 */
19/**********************************************************************************************************************
20 * @author Brook Novak
21 *
22 * Assumes:
23 * - Core.js is prepended(or loaded).
24 * - An array named "baseScripts" exists with all relative paths of scripts to load first in the given order.
25 * - An array named "dependantScripts" exists with all relative paths of scripts to load after the base scripts.
26 * - A string named "thisFilename" exists, set to the filename of this file, which is contained in the libraries TDL.
27 * - That this script is encapsulated in an anonymous function (To avoid polluting global scope)
28 **********************************************************************************************************************/
29
30// Mimic the addHandler method in the events module to make available before the events module script is even available
31var eventHandlers = [];
32de.events.addHandler = function(eventSource, eventName, handler) { // will be overridden
33 eventHandlers.push(arguments);
34}
35
36// Override the onready function (declared in Core)- so that all callbacks are invoked once
37// the window.onload occurs AND all scripts are loaded
38var onReadyCallbacks = [],
39 origReady = de.onready;
40de.onready = function(handler) {
41 onReadyCallbacks.push(handler);
42}
43
44/**
45 * @namespace
46 * The bootstrap namespace.
47 */
48bootstrap = {
49
50 /**
51 * A list of all laoded module names
52 * @type [String]
53 */
54 loadedModules : [],
55
56 /**
57 * For all scripts in the API you must invoke this function to register the script when the script is first executed.
58 *
59 * @param {String} name The name of module that is loaded.
60 * The Module name is the relative path without the file extension (.js),
61 * and directory separators are replaced with period-symbols.
62 */
63 provides : function(name) {
64
65 // debug.println("Loaded: " + name);
66
67 this.loadedModules.push(name);
68
69 // First Phase: base-script loading
70 if (baseScripts.length > 0) {
71
72 // A base script is loaded - remove it
73 baseScripts.shift();
74
75 if (baseScripts.length > 0) { // More base scripts to load
76
77 // Load the next base script
78 loadScript(baseURL + baseScripts[0]);
79
80 } else { // Finished download base scripts
81
82 // Pull in remaining API source files asynchronously
83 for (var i in dependantScripts) {
84 loadScript(baseURL + dependantScripts[i]);
85 }
86 }
87
88 } else { // Second phase: dependant-script loading
89
90 // A dependant script is loaded.
91
92 // Get relative path for registered module
93 var registeredPath = name.replace(/\./g,"/").toLowerCase() + ".js";
94
95 // Remove include path for the registered module
96 for (var i = 0; i < dependantScripts.length; i++) {
97 if (dependantScripts[i].toLowerCase() == registeredPath) {
98 dependantScripts.splice(i, 1);
99 break;
100 }
101 }
102
103 if (dependantScripts.length == 0) { // All scripts loaded?
104
105 // Schedule finishing-up code on another event to let the last script load
106 setTimeout(function() {
107
108 // Add all registered events...
109 for (var i in eventHandlers) {
110 var args = eventHandlers[i];
111
112 var eventSource = (args.length > 0) ? args[0] : null,
113 eventName = (args.length > 1) ? args[1] : null,
114 handler = (args.length > 2) ? args[2] : null;
115
116 _addHandler(eventSource, eventName, handler);
117 }
118
119 // Add all on-ready funcs
120 for (var i in onReadyCallbacks) {
121 origReady(onReadyCallbacks[i]); // Fires instantly is onload already occured
122 }
123
124 de.onready = origReady;
125
126 }, 1);
127
128 }
129 }
130
131 }
132
133};
134
135// Determine the base URL of the library.
136var baseURL;
137var libcoreRegExp = new RegExp("(^|[\\/\\\\])" + thisFilename.replace(/\./g,"\\.") + "(\\?|$)");
138var scriptElements = document.getElementsByTagName("script");
139
140for (var i in scriptElements) {
141 var src = scriptElements[i].src;
142 if (src && src.match(libcoreRegExp)) {
143
144 baseURL = src;
145 i = src.lastIndexOf("/");
146 if (i == -1) i = src.lastIndexOf("\\");
147 baseURL = (i == -1) ? "" : src.substring(0, i + 1);
148
149 break;
150 }
151
152}
153
154if (!baseURL) throw new Error("Unable to import library: could not discover DEdit libhome");
155
156// Pull in API base source files first, once this is done the rest of the api source will be pulled in
157loadScript(baseURL + baseScripts[0]);
158
159function loadScript(scriptSrc){
160
161 // gets document head element
162 var docHead = document.getElementsByTagName('head')[0];
163
164 if (docHead) {
165 // creates a new script tag
166 var scriptEl = document.createElement('script');
167
168 // adds src and type attribute to script tag
169 scriptEl.setAttribute('src', scriptSrc);
170 scriptEl.setAttribute('type', 'text/javascript');
171
172 // append the script tag to document head element
173 docHead.appendChild(scriptEl);
174 }
175}
176
Note: See TracBrowser for help on using the repository browser.