-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathmain.js
235 lines (217 loc) · 7.1 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//--
//-- Main
//--
var params = null; // Command line parameters
var store = null; // TiddlyWiki storage
var story = null; // Main story
var formatter = null; // Default formatters for the wikifier
var anim = typeof Animator == "function" ? new Animator() : null; // Animation engine
var readOnly = false; // Whether we're in readonly mode
var highlightHack = null; // Embarrassing hack department...
var hadConfirmExit = false; // Don't warn more than once
var safeMode = false; // Disable all plugins and cookies
var showBackstage; // Whether to include the backstage area
var installedPlugins = []; // Information filled in when plugins are executed
var startingUp = false; // Whether we're in the process of starting up
var pluginInfo, tiddler; // Used to pass information to plugins in loadPlugins()
// Whether this file can be saved back to the same location [Preemption]
window.allowSave = window.allowSave || function(l) {
//# allow save from ANYWHERE (TW280+ uses fallback HTML5 download from data:// URI)
//# return (document.location.protocol == "file:");
return true;
};
// Whether this file is being viewed locally
window.isLocal = function() {
return (document.location.protocol == "file:");
};
//# Use of the JavaSaver applet is removed from the core; this is only kept for backwards compatibility
var useJavaSaver = false;
// Allow preemption code a chance to tweak config and useJavaSaver [Preemption]
if (window.tweakConfig) window.tweakConfig();
if(!window || !window.console) {
console = { tiddlywiki: true, log: function(message) { displayMessage(message) } };
}
// Starting up
function main() {
//# save loaded document HTML before making changes
window.originalHTML = recreateOriginal();
var t10, t9, t8, t7, t6, t5, t4, t3, t2, t1, t0 = new Date();
startingUp = true;
var doc = jQuery(document);
jQuery.noConflict();
window.onbeforeunload = function(e) { if(window.confirmExit) return confirmExit(); };
params = getParameters();
if(params) params = params.parseParams("open", null, false);
store = new TiddlyWiki({ config: config });
invokeParamifier(params, "oninit");
story = new Story("tiddlerDisplay", "tiddler");
addEvent(document, "click", Popup.onDocumentClick);
saveTest();
for(var i = 0; i < config.notifyTiddlers.length; i++)
store.addNotification(config.notifyTiddlers[i].name, config.notifyTiddlers[i].notify);
t1 = new Date();
loadShadowTiddlers();
doc.trigger("loadShadows");
t2 = new Date();
store.loadFromDiv("storeArea", "store", true);
doc.trigger("loadTiddlers");
loadOptions();
t3 = new Date();
invokeParamifier(params, "onload");
t4 = new Date();
readOnly = window.isLocal() ? false : config.options.chkHttpReadOnly;
var pluginProblem = loadPlugins("systemConfig");
doc.trigger("loadPlugins");
t5 = new Date();
formatter = new Formatter(config.formatters);
invokeParamifier(params, "onconfig");
story.switchTheme(config.options.txtTheme);
showBackstage = showBackstage !== undefined ? showBackstage : !readOnly;
t6 = new Date();
for(var name in config.macros) {
if(config.macros[name].init)
config.macros[name].init();
}
t7 = new Date();
store.notifyAll();
t8 = new Date();
restart();
refreshDisplay();
t9 = new Date();
if(pluginProblem) {
story.displayTiddler(null, "PluginManager");
displayMessage(config.messages.customConfigError);
}
if(showBackstage)
backstage.init();
t10 = new Date();
if(config.options.chkDisplayInstrumentation) {
displayMessage("LoadShadows " + (t2 - t1) + " ms");
displayMessage("LoadFromDiv " + (t3 - t2) + " ms");
displayMessage("LoadPlugins " + (t5 - t4) + " ms");
displayMessage("Macro init " + (t7 - t6) + " ms");
displayMessage("Notify " + (t8 - t7) + " ms");
displayMessage("Restart " + (t9 - t8) + " ms");
displayMessage("Total: " + (t10 - t0) + " ms");
}
startingUp = false;
doc.trigger("startup");
}
// Called on unload. Functions may get unloaded too, so they are called conditionally.
function unload() {
if(window.checkUnsavedChanges) checkUnsavedChanges();
if(window.scrubNodes) scrubNodes(document.body);
}
// Restarting
function restart() {
invokeParamifier(params, "onstart");
if(story.isEmpty()) {
story.displayDefaultTiddlers();
}
window.scrollTo(0, 0);
}
function saveTest() {
var s = document.getElementById("saveTest");
if(s.hasChildNodes())
alert(config.messages.savedSnapshotError);
s.appendChild(document.createTextNode("savetest"));
}
function loadShadowTiddlers() {
var shadows = new TiddlyWiki();
shadows.loadFromDiv("shadowArea", "shadows", true);
shadows.forEachTiddler(function(title, tiddler) { config.shadowTiddlers[title] = tiddler.text });
}
function loadPlugins(tag) {
if(safeMode) return false;
var tiddlers = store.getTaggedTiddlers(tag);
//# ensure the plugins are sorted into case sensitive order
tiddlers.sort(function(a, b) { return a.title < b.title ? -1 : (a.title == b.title ? 0 : 1) });
var toLoad = [];
var nLoaded = 0;
var map = {};
var nPlugins = tiddlers.length;
installedPlugins = [];
for(var i = 0; i < nPlugins; i++) {
var p = getPluginInfo(tiddlers[i]);
installedPlugins[i] = p;
var n = p.Name || p.title;
if(n) map[n] = p;
n = p.Source;
if(n) map[n] = p;
}
var visit = function(p) {
if(!p || p.done) return;
p.done = 1;
var reqs = p.Requires;
if(reqs) {
reqs = reqs.readBracketedList();
for(var i = 0; i < reqs.length; i++)
visit(map[reqs[i]]);
}
toLoad.push(p);
};
for(i = 0; i < nPlugins; i++)
visit(installedPlugins[i]);
for(i = 0; i < toLoad.length; i++) {
p = toLoad[i];
pluginInfo = p;
tiddler = p.tiddler;
if(isPluginExecutable(p)) {
if(isPluginEnabled(p)) {
p.executed = true;
var startTime = new Date();
try {
if(tiddler.text) window.eval(tiddler.text);
nLoaded++;
} catch(ex) {
p.log.push(config.messages.pluginError.format([exceptionText(ex)]));
p.error = true;
if(!console.tiddlywiki) {
console.log("error evaluating " + tiddler.title, ex);
}
}
pluginInfo.startupTime = String((new Date()) - startTime) + "ms";
} else {
nPlugins--;
}
} else {
p.warning = true;
}
}
return nLoaded != nPlugins;
}
function getPluginInfo(tiddler) {
var p = store.getTiddlerSlices(tiddler.title, ["Name", "Description", "Version",
"Requires", "CoreVersion", "Date", "Source", "Author", "License", "Browsers"]);
p.tiddler = tiddler;
p.title = tiddler.title;
p.log = [];
return p;
}
// Check that a particular plugin is valid for execution
function isPluginExecutable(plugin) {
if(plugin.tiddler.isTagged("systemConfigForce")) {
plugin.log.push(config.messages.pluginForced);
return true;
}
if(plugin["CoreVersion"]) {
var coreVersion = plugin["CoreVersion"].split(".");
var w = parseInt(coreVersion[0], 10) - version.major;
if(w == 0 && coreVersion[1])
w = parseInt(coreVersion[1], 10) - version.minor;
if(w == 0 && coreVersion[2])
w = parseInt(coreVersion[2], 10) - version.revision;
if(w > 0) {
plugin.log.push(config.messages.pluginVersionError);
return false;
}
}
return true;
}
function isPluginEnabled(plugin) {
if(plugin.tiddler.isTagged("systemConfigDisable")) {
plugin.log.push(config.messages.pluginDisabled);
return false;
}
return true;
}