2012-04-30 11:23:03 +00:00
|
|
|
/*\
|
2013-05-31 15:53:19 +00:00
|
|
|
title: $:/boot/bootprefix.js
|
2012-04-30 11:23:03 +00:00
|
|
|
type: application/javascript
|
|
|
|
|
|
|
|
This file sets up the globals that need to be available when JavaScript modules are executed in the browser. The overall sequence is:
|
|
|
|
|
|
|
|
# BootPrefix.js
|
|
|
|
# <module definitions>
|
|
|
|
# Boot.js
|
|
|
|
|
|
|
|
See Boot.js for further details of the boot process.
|
|
|
|
|
2012-11-12 22:15:52 +00:00
|
|
|
\*/
|
2012-04-30 11:23:03 +00:00
|
|
|
|
2013-10-12 17:44:09 +00:00
|
|
|
var _bootprefix = (function($tw) {
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2014-05-07 11:51:16 +00:00
|
|
|
$tw = $tw || Object.create(null);
|
|
|
|
$tw.boot = $tw.boot || Object.create(null);
|
2014-01-14 14:09:04 +00:00
|
|
|
|
|
|
|
// Detect platforms
|
2015-01-11 14:21:31 +00:00
|
|
|
if(!("browser" in $tw)) {
|
|
|
|
$tw.browser = typeof(window) !== "undefined" ? {} : null;
|
|
|
|
}
|
|
|
|
if(!("node" in $tw)) {
|
|
|
|
$tw.node = typeof(process) === "object" ? {} : null;
|
|
|
|
}
|
|
|
|
if(!("nodeWebKit" in $tw)) {
|
|
|
|
$tw.nodeWebKit = $tw.node && global.window && global.window.nwDispatcher ? {} : null;
|
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
|
2014-05-07 11:51:16 +00:00
|
|
|
// Set default boot tasks
|
|
|
|
$tw.boot.tasks = {
|
|
|
|
trapErrors: !!($tw.browser && !$tw.node),
|
|
|
|
readBrowserTiddlers: !!($tw.browser && !$tw.node)
|
|
|
|
};
|
|
|
|
|
2012-11-15 10:40:03 +00:00
|
|
|
/*
|
|
|
|
Information about each module is kept in an object with these members:
|
|
|
|
moduleType: type of module
|
|
|
|
definition: object, function or string defining the module; see below
|
|
|
|
exports: exports of the module, filled in after execution
|
|
|
|
|
|
|
|
The `definition` can be of several types:
|
|
|
|
|
|
|
|
* An object can be used to directly specify the exports of the module
|
|
|
|
* A function with the arguments `module,require,exports` that returns `exports`
|
|
|
|
* A string function body with the same arguments
|
|
|
|
|
|
|
|
Each moduleInfo object is stored in two hashmaps: $tw.modules.titles and $tw.modules.types. The first is indexed by title and the second is indexed by type and then title
|
|
|
|
*/
|
|
|
|
$tw.modules = {
|
|
|
|
titles: {}, // hashmap by module name of moduleInfo
|
|
|
|
types: {} // hashmap by module type and then name of moduleInfo
|
|
|
|
};
|
2012-04-30 11:23:03 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Define a JavaScript tiddler module for later execution
|
|
|
|
moduleName: name of module being defined
|
|
|
|
moduleType: type of module
|
2012-11-15 10:40:03 +00:00
|
|
|
definition: module definition; see discussion above
|
2012-04-30 11:23:03 +00:00
|
|
|
*/
|
2012-11-15 10:40:03 +00:00
|
|
|
$tw.modules.define = function(moduleName,moduleType,definition) {
|
|
|
|
// Create the moduleInfo
|
|
|
|
var moduleInfo = {
|
|
|
|
moduleType: moduleType,
|
|
|
|
definition: definition,
|
|
|
|
exports: undefined
|
|
|
|
};
|
2012-11-17 13:08:25 +00:00
|
|
|
// If the definition is already an object we can use it as the exports
|
|
|
|
if(typeof moduleInfo.definition === "object") {
|
|
|
|
moduleInfo.exports = definition;
|
|
|
|
}
|
2012-11-15 10:40:03 +00:00
|
|
|
// Store the module in the titles hashmap
|
2012-11-14 11:23:43 +00:00
|
|
|
if(Object.prototype.hasOwnProperty.call($tw.modules.titles,moduleName)) {
|
|
|
|
console.log("Warning: Redefined module - " + moduleName);
|
|
|
|
}
|
2012-11-15 10:40:03 +00:00
|
|
|
$tw.modules.titles[moduleName] = moduleInfo;
|
|
|
|
// Store the module in the types hashmap
|
|
|
|
if(!Object.prototype.hasOwnProperty.call($tw.modules.types,moduleType)) {
|
|
|
|
$tw.modules.types[moduleType] = {};
|
|
|
|
}
|
|
|
|
if(Object.prototype.hasOwnProperty.call($tw.modules.types[moduleType],moduleName)) {
|
|
|
|
console.log("Warning: Redefined module - " + moduleName);
|
|
|
|
}
|
|
|
|
$tw.modules.types[moduleType][moduleName] = moduleInfo;
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
2012-10-12 18:01:03 +00:00
|
|
|
|
|
|
|
/*
|
2013-03-22 19:27:09 +00:00
|
|
|
External JavaScript can populate this array before calling boot.js in order to preload tiddlers
|
2012-10-12 18:01:03 +00:00
|
|
|
*/
|
|
|
|
$tw.preloadTiddlers = $tw.preloadTiddlers || [];
|
|
|
|
|
2013-03-22 19:27:09 +00:00
|
|
|
/*
|
|
|
|
Convenience function for pushing a tiddler onto the preloading array
|
|
|
|
*/
|
2012-10-12 18:01:03 +00:00
|
|
|
$tw.preloadTiddler = function(fields) {
|
|
|
|
$tw.preloadTiddlers.push(fields);
|
|
|
|
};
|
2013-10-12 17:44:09 +00:00
|
|
|
|
2015-12-19 18:52:25 +00:00
|
|
|
/*
|
|
|
|
Convenience function for pushing an array of tiddlers onto the preloading array
|
|
|
|
*/
|
|
|
|
$tw.preloadTiddlerArray = function(fieldsArray) {
|
|
|
|
$tw.preloadTiddlers.push.apply($tw.preloadTiddlers,fieldsArray);
|
|
|
|
};
|
|
|
|
|
|
|
|
return $tw;
|
2013-10-12 17:44:09 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
if(typeof(exports) === "undefined") {
|
|
|
|
// Set up $tw global for the browser
|
2014-10-06 09:02:20 +00:00
|
|
|
window.$tw = _bootprefix(window.$tw);
|
2013-10-12 17:44:09 +00:00
|
|
|
} else {
|
|
|
|
// Export functionality as a module
|
|
|
|
exports.bootprefix = _bootprefix;
|
|
|
|
}
|