-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhistory.js
258 lines (211 loc) · 5.58 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
//
// 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* corresponds to the <Passage> just displayed.
//
// *variables* is in itself an object. Each property is a variable set
// by the story via the <<set>> macro.
//
// *hash* is a URL hash guaranteed to load that specific point in time.
//
//
// Constructor: History
// Initializes a History object.
//
// Parameters:
// none
//
function History()
{
this.history = [{ passage: null, variables: {}, hash: null }];
};
//
// 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()
{
var self = this;
if (! this.restore())
this.display('Start', null);
this.hash = window.location.hash;
this.interval = window.setInterval(function() { self.watchHash.apply(self) }, 250);
};
//
// Method: display
// Displays a passage on the page.
//
// Parameters:
// title - the title of the passage to display.
// link - the DOM element corresponding to the link that was clicked to
// view the passage. This parameter has no effect but is maintained
// for Jonah compatibility.
// 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);
// create a fresh entry in the history
var passage = tale.get(title);
this.history.unshift({ passage: passage, variables: clone(this.history[0].variables) } );
this.history[0].hash = this.save();
// add it to the page
var div = passage.render();
if (render != 'offscreen')
{
removeChildren($('passages'));
$('passages').appendChild(div);
// animate its appearance
if (render != 'quietly')
fade(div, { fade: 'in' });
}
if ((render == 'quietly') || (render == 'offscreen'))
div.style.visibility = 'visible';
if (render != 'offscreen')
{
document.title = tale.title;
this.hash = this.save();
if (passage.title != 'Start')
{
document.title += ': ' + passage.title;
window.location.hash = this.hash;
};
window.scroll(0, 0);
};
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
// (in most cases)
window.location.hash = '';
};
//
// Method: save
// Appends a hash 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:
// none
//
// Returns:
// nothing
//
History.prototype.save = function (passage)
{
var order = '';
// encode our history
for (var i = this.history.length - 1; i >= 0; i--)
{
if ((this.history[i].passage) && (this.history[i].passage.id))
order += this.history[i].passage.id.toString(36) + '.';
};
// 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 == '') || (window.location.hash == '#'))
return false;
var order = window.location.hash.replace('#', '').split('.');
var passages = [];
// render the passages in the order the reader clicked them
// we only show the very last one
for (var i = 0; i < order.length; i++)
{
var id = parseInt(order[i], 36);
if (! tale.has(id))
return false;
console.log('restoring id ' + id);
var method = (i == order.length - 1) ? '' : 'offscreen';
passages.unshift(this.display(id, null, method));
};
return true;
}
catch (e)
{
console.log("restore failed", e);
return false;
};
};
//
// Method: watchHash
// Watches the browser's address bar for changes in its hash, and
// calls <restore> accordingly. This is set to run at an interval
// in <init>.
//
// Parameters:
// none
//
// Returns:
// nothing
//
History.prototype.watchHash = function()
{
if (window.location.hash != this.hash)
{
console.log('new hash: ' + window.location.hash + ', was ' + this.hash);
if ((window.location.hash != '') && (window.location.hash != '#'))
{
this.history = [{ passage: null, variables: {} }];
removeChildren($('passages'));
$('passages').style.visibility = 'hidden';
if (! this.restore())
alert('The passage you had previously visited could not be found.');
$('passages').style.visibility = 'visible';
}
else
window.location.reload();
this.hash = window.location.hash;
}
};