-
Notifications
You must be signed in to change notification settings - Fork 3
/
history.js
313 lines (255 loc) · 7.08 KB
/
history.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//
// Class: History
//
// A class used to manage the state of the story -- displaying new passages
// and rewinding to the past.
//
// Property: History
// An array representing the state of the story. history[0] is the current
// state, history[1] is the state just before the present, and so on.
// Each entry in the history is an object with two properties: *passage*,
// which corresponds to the <Passage> just displayed, and *variables*.
// Variables is in itself an object. Each property is a variable set
// by the story via the <<set>> macro.
//
//
// Constructor: History
// Initializes a History object.
//
// Parameters:
// none
//
function History()
{
this.history = [{ passage: null, variables: {} }];
};
//
// Method: init
// This first attempts to restore the state of the story via the <restore>
// method. If that fails, it then either displays the passages linked in the
// *StartPassages* passage, or gives up and tries to display a passage
// named *Start*.
//
// Parameters:
// none
//
// Returns:
// nothing
//
History.prototype.init = function()
{
if (! this.restore())
if (tale.has('StartPassages'))
{
console.log('showing StartPassages', tale.get('StartPassages').text.readBracketedList());
var initials = tale.get('StartPassages').text.readBracketedList();
for (var i = 0; i < initials.length; i++)
this.display(initials[i], null, 'quietly');
}
else
{
console.log('no StartPassages, showing Start');
this.display('Start', null, 'quietly');
};
};
//
// Method: close
// This removes a passage from display onscreen. This does not animate
// its disappearance.
//
// Parameters:
// passage - the <Passage> to remove
//
// Returns:
// nothing
//
History.prototype.close = function (passage)
{
// we hide the passage immediately, without animation
var el = $('passage' + passage.title);
console.log('closing "' + passage.title + '"');
if (el)
el.parentNode.removeChild(el);
};
//
// Method: display
// Displays a passage on the page. If a passage has previously been
// displayed, the browser window is scrolled so it is in view.
//
// Parameters:
// title - the title of the passage to display.
// link - the DOM element corresponding to the link that was clicked to
// view the passage. The new passage is displayed immediately below the passage
// enclosed by the link. This parameter is optional. If it is omitted,
// the passage is displayed at the bottom of the page.
// render - may be either "quietly" or "offscreen". If a "quietly" value
// is passed, the passage's appearance is not animated. "offscreen"
// asks that the passage be rendered, but not displayed at all. This
// parameter is optional. If it is omitted, then the passage's appearance
// is animated.
//
// Returns:
// The DOM element containing the passage on the page.
//
History.prototype.display = function (title, link, render)
{
console.log('displaying "' + title + '" ' + (render || '') + ' from ', link);
// find enclosing passage of the link
var sourcePassage = link;
while (sourcePassage && (sourcePassage.className.indexOf('passage') == -1))
if (sourcePassage.parentNode.className)
sourcePassage = sourcePassage.parentNode;
else
break;
// check if passage is already displayed
if (el = $('passage' + title))
{
scrollWindowTo(el);
return;
};
// create a fresh entry in the history
var passage = tale.get(title);
this.history.unshift({ passage: passage,
variables: clone(this.history[0].variables) } );
// add it to the page
var div = passage.render();
if (render != 'offscreen')
{
if (sourcePassage)
$('passages').insertBefore(div, sourcePassage.nextSibling);
else
$('passages').appendChild(div);
// animate its appearance
if (render != 'quietly')
{
scrollWindowTo(div);
fade(div, { fade: 'in' });
}
}
if ((render == 'quietly') || (render == 'offscreen'))
div.style.visibility = 'visible';
return div;
};
//
// Method: restart
// Restarts the story from the beginning. This actually forces the
// browser to refresh the page.
//
// Parameters:
// none
//
// Returns:
// none
//
History.prototype.restart = function()
{
// clear any bookmark
// this has the side effect of forcing a page reload
window.location.hash = '';
};
//
// Method: save
// Returns a hash to append to the page's URL that will be later
// read by the <restore> method. How this is generated is not
// guaranteed to remain constant in future releases -- but it
// is guaranteed to be understood by <restore>.
//
// Parameters:
// passage - a <Passage> whose point in the history to save.
// This parameter is optional -- if omitted, then the
// entire story's history is saved.
//
// Returns:
// A hash to append to the page's URL.
//
History.prototype.save = function (passage)
{
var order = '';
// encode our history
for (var i = 0; i < this.history.length; i++)
{
if ((this.history[i].passage) && (this.history[i].passage.id))
{
order += this.history[i].passage.id.toString(36) + '.';
if (this.history[i].passage.id == passage.id)
break;
}
};
// strip the trailing period
return '#' + order.substr(0, order.length - 1);
};
//
// Method: restore
// Attempts to restore the state of the story as saved by <save>.
//
// Parameters:
// none
//
// Returns:
// Whether this method actually restored anything.
//
History.prototype.restore = function ()
{
try
{
if (window.location.hash == '')
return false;
var order = window.location.hash.replace('#', '').split('.');
var passages = [];
// render the passages offscreen in the order the reader clicked them
// we can't show them, because contents along the way may be
// incorrect (e.g. <<choice>>)
for (var i = order.length - 1; i >= 0; i--)
{
var id = parseInt(order[i], 36);
if (! tale.has(id))
return false;
console.log('restoring id ' + id);
passages.unshift(this.display(id, null, 'offscreen'));
};
// our state is now correct
// we now display the last passage
$('passages').appendChild(passages[0]);
return true;
}
catch (e)
{
console.log("restore failed", e);
return false;
};
};
//
// Method: rewindTo
// Rewinds the state of the story to a particular <Passage>.
//
// Parameters:
// passage - a <Passage> to rewind to.
//
// Returns:
// nothing
//
History.prototype.rewindTo = function (passage)
{
// fade out the story while we work
console.log('rewinding to "' + passage.title + '"');
var self = this;
fade($('passages'), { fade: 'out', onComplete: work });
function work()
{
// delete passages after the one we are rewinding to
while (self.history[0].passage.title != passage.title)
self.close(self.history.shift().passage);
// i is now the index of the passage we are rewinding to
// we restore it to its original state
self.history[0].variables = clone(self.history[1].variables);
passage.reset();
var els = $('passage' + passage.title).childNodes;
for (var i = 0; i < els.length; i++)
if (els[i].className == 'body')
{
removeChildren(els[i]);
new Wikifier(els[i], passage.text);
};
fade($('passages'), { fade: 'in' });
};
};