-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcya.js
686 lines (655 loc) · 27.8 KB
/
cya.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
/*
Copyright (C) 2016 Solra Bizna
CYA is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
CYA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
CYA. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
/**DEFINITION:Definitions
*/
/**Adventure
* An HTML document designed to be used with the Engine, made of `Page`s.
*/
/**Engine
* The JavaScript code that powers the Adventures.
*/
/**Page
* A reference to a `<page>` element. `Page`s can either be referred to by
* their `name` attribute or passed directly as `Element`s (the latter usually
* being used for dynamically generated pages).
*/
/**Playfield
* The `Element` into which the contents of `<page>`s are placed.
*/
/**Element
* A DOM element object, corresponding to an element in the HTML. See
* [the MDN page](https://developer.mozilla.org/en-US/docs/Web/API/Element).
*
* Simple Adventures don't have to care about this.
*
* Anywhere the Engine asks for an `Element`, and does not specify particular
* handling for non-Element values, you may optionally pass a string giving the
* ID of an `Element`. (Examples where this does not apply are `Page`
* references and `<execute>`/`<eval>` elements.)
*/
/**ELEMENT:Structure of an Adventure
*
* An Adventure is an ordinary HTML document for the most part. Including the
* Engine script into the page changes the meaning of certain elements. _All_
* styling is provided by the Adventure via a combination of CSS and HTML
* outside the `Playfield`.
*
* Overall, the recommended structure of an Adventure looks like:
*
* <html>
* <head>
* ...title, charset, etc.
* <script src="cya.min.js"></script>
* ...supplemental scripts, links, stylesheets, etc.
* </head>
* <body>
* <noscript>
* ...pretty message explaining that JavaScript is required
* </noscript>
* <script language="JavaScript">
* ...set any Engine Variables you want to customize here
* </script>
* <div id="cya_playfield"></div>
* <div style="display:none"> (or some other means of hiding the pages)
* <page name="start">
* ...contents of the starting page
* </page>
* ...more pages
* </div>
* </body>
* </html>
*
* There are many more possibilities. For instance, instead of being a `div`
* that effectively takes up the entire browser window, you could have certain
* information (e.g. inventory, character stats...) in a normal HTML layout and
* make the playfield be a scrollable element of that layout. You could also
* use the `<body>` directly as a playfield, though that limits your options
* for styling.
*
* Certain elements are treated specially. The most important such elements are
* the `<page>` and `<choice>` elements; every Adventure needs a few of them.
*/
/**<page>
* A `<page>` element is the bread of an Adventure. It corresponds to an
* individual page of a Choose Your Own Adventure book. The contents of a
* `<page>` are added dynamically to the `Playfield` on demand; whether by the
* action of a script, or (more usually) by the user selecting a `<choice>`.
*
* When a `<page>` is presented, its contents are styled by a surrounding
* `<div class="cya_page">`, one for each `<page>`.
*
* A `<page>` element will almost always have a `name` attribute, (e.g.
* `<page name="caves2">`. This name is how other parts of the Adventure
* (including `<choice>`s) refer to the page. Any name that is valid in HTML
* may be used. Some names are special; "start" is presented at the beginning
* of the Adventure, and "ending" is presented whenever an `<ending>` element
* is processed. (These names may be overridden with the `start_page` and
* `end_page` external variables, respectively.)
*/
/**<choice>
* A `<choice>` element presents a choice to the user. Each page will typically
* end with one or more `<choice>`s, allowing the user to choose what they do
* next in the Adventure. Only one of the available `<choice>`s can be taken.
*
* When a `<choice>` is presented, it is in one of three ways:
*
* <a class="cya_choice" href="...">A choice currently available</a>
* <a class="cya_accepted_choice" href="...">A previously-available choice which the user selected</a>
* <a class="cya_rejected_choice" href="...">A previously-available choice which the user did not select</a>
*
* Most Adventures will want CSS like:
*
* a.cya_rejected_choice {display:none;}
*
* so that rejected choices are hidden. Other Adventures may want to set
* `pivot_choices` to true, so that the accepted choice is moved to the end of
* the list (especially if they use `scroll_view`).
*
* Most `<choice>`s will have a `target` attribute naming the `<page>` that
* will be presented if that choice is made. For nearly all Adventures, this is
* all that's required. (e.g. `<choice target="caves4">`)
*
* If the `enable_hotkeys` engine variable is true, `<choice>`s may have a
* `hotkey` attribute giving the hotkey to use. You should only use numbers and
* lowercase ASCII letters for these hotkeys. `<choice>`s with no `hotkey`
* attribute will use the first hotkey 1-0 that is available (or none).
*
* Advanced Adventures may wish to execute some JavaScript code when a
* particular choice is made. They can do so by providing an `execute`
* attribute. The code within will be executed before the next page is
* presented. If it `return`s a `Page`, that `Page` will be used in place of
* the `target` attribute.
*
* If an `eval` attribute is present, then it contains a JavaScript expression
* that is evaluated, and treated just like the return value of an `execute`
* attribute. If both `eval` and `execute` are present, `execute` applies
* first and then `eval` is evaluated.
*/
/**<ending>
* When an `<ending>` element is encountered, the JavaScript variable `ending`
* is set to the `<ending>` element's contents, and the "ending" page is called
* upon.
*
* NOTE: This does _not_ result in the insertion of the "ending" `<page>`'s
* contents at the point the `<ending>` element appears. Instead, they appear
* _after_ the contents of the page that contained the `<ending>` element. In
* addition, the two pages have separate containing `<div>`s.
*/
/**<execute>
* `<execute>` contains JavaScript code that will be executed every time the
* containing `<page>` is presented. It may optionally `return` an `Element`,
* which will be inserted directly without further processing, or another
* value, which will be inserted as plain text.
*
* Future Engine versions may process Engine-specific elements that result from
* an `<execute>`, and may be extended to process arrays as if they were
* `return`ed by sequential `<execute>`s.
*/
/**<eval>
* Shorthand for `<execute>return (...)</execute>`. `<eval>` usually contains
* a single, simple JavaScript expression so that the result is directly
* inserted. For example: `You have <eval>player_hitpoints</eval> HP.`
*/
/**<if>
* The contents of an `<if>` element are inserted directly, if (and _only_ if)
* the `condition` is true.
*
* The `condition` attribute is a JavaScript expression used to decide whether
* to insert the `<if>`'s contents.
*/
/**EXTERN:Engine Variables
*
* These are variables used by an Adventure to control the Engine's behavior.
* Adventures that wish to override the defaults should do so in a `<script>`
* element if they want the overridden values to apply from the beginning. (See
* the example Adventure.)
*
* Unless otherwise specified, external variables may be changed at any time
* and the changes will take effect immediately.
*/
var cya = {
/**
* The first `Page` that will be displayed after loading the document. This
* value is only used by the Engine once, when the Adventure is loaded.
*
* User code may choose to respect this value with something like:
*
* <choice eval="cya.start_page">Restart</choice>
*/
start_page:"start",
/**
* The `Page` that will be displayed when an `<ending>` tag is encountered.
*/
ending_page:"ending",
/**
* The `Element` that all processed `Page`s will be displayed in.
*/
playfield:"cya_playfield",
/**
* If true, choices can be selected by using the 1-0 keys to target
* the first through tenth choices. In addition,
* `<span class="cya_hotkey">#</span>` will precede the contents of each
* choice, where `#` is the hotkey. The hotkeys are always accessible using
* browser-specific AccessKeys, and are accessible directly whenever focus
* is not inside an `<input>` or `<textarea>` element.
*
* `<choice>` elements with an explicit `hotkey` attribute will use that
* hotkey instead of 1-0.
*
* Changes made to `enable_hotkeys` will take effect the next time a page
* is processed.
*/
enable_hotkeys:true,
/**
* The `Element` that will be scrolled when a `<choice>` is selected. May
* be undefined, in which case automatic scrolling is disabled.
*
* A useful value is `document.body`.
*/
scroll_view:undefined,
/**
* An `Element` that will have its height adjusted dynamically, so that the
* `scroll_view` can always scroll at least as far down as the selected
* choice. (Only applicable if `scroll_view` is also defined.)
*/
scroll_spacer:undefined,
/**
* If `scroll_to_chosen_choice` is true, `scroll_view` will be scrolled to
* the _chosen_ choice. If false, (default) the view will be scrolled to
* the _first_ choice. (If `scroll_view` is null, this is ignored.)
*/
scroll_to_chosen_choice:false,
/**
* If `pivot_choices` is true, any `<choice>` selected by the player is
* moved to the end of its page. This is useful if rejected choices are not
* hidden, especially if `scroll_view` is used.
*/
pivot_choices:false,
/**
* tl;dr: Set this to true to check your Adventure for errors every time it
* loads.
*
* If `validate_on_load` is true, `cya.validate()` is automatically called
* on every `<page>` that `cya.findpages()` finds. (`cya.findpages()` is
* called automatically when the Adventure first loads.) This will slow
* down loading, especially for large Adventures. You may wish to set it to
* `true` while testing your Adventure, and `false` when you deploy.
*/
validate_on_load:false,
};
/**FUNCTION:Engine Functions
*
* Other than an occasional `cya.page()`, you will probably not need to call
* any engine functions directly.
*/
// (we are hiding the rest of our state)
(function() {
// Maps `Page` names to `Element`s
var page_cache = {};
// List of `choice`s that have been made active since last time a `choice`
// was made (so we can disable previous `choice`s when one is made)
// `choice` objects have no formal identity
var current_choices = [];
// Maps active `choice`s to hotkeys.
var current_hotkeys = {};
// List of default hotkeys to use.
var DEFAULT_HOTKEYS = ["1","2","3","4","5","6","7","8","9","0"];
// Queue of `Page`s that were requested while a `Page` was already in the
// process of being processed
var deferred_pages = [];
// Whether a `Page` is currently being processed. If `cya.page()` is called
// while this is true, the `Page` will be added to `deferred_pages`
// instead. (It will be processed before the original `cya.page()`
// returns.)
var inside_page = false;
// If the parameter is an `Element`, returns it. Otherwise, attempts to
// return an `Element` with that ID.
var element_parameter = function(el) {
if(el == null) return null;
else if(el instanceof Element) return el;
else return document.getElementById(el);
}
// If the parameter is an `Element`, returns it. Otherwise, attempts to
// find a `<page>` with that name.
var find_page = function(page) {
var ret = undefined;
if(page instanceof Element) ret = page;
else ret = page_cache[page];
if(ret == undefined) {
if(cya.pages.namedItem) ret = cya.pages.namedItem(page);
if(ret == undefined) {
// try a linear search
for(var i = 0; cya.pages[i]; ++i) {
if(cya.pages[i].getAttribute("name") == page) {
ret = cya.pages[i];
break;
}
}
}
page_cache[page] = ret;
}
return ret;
}
// Disables all previously-active `choice`s. The parameter is a `choice`;
// if it is found in `current_choices`, then that choice will be marked as
// a `cya_accepted_choice` rather than a `cya_rejected_choice`.
// Usually called when a choice is made.
// TODO: Make this public to allow timed choices? (yuck)
var clear_old_choices = function(accepted_choice) {
var node_to_pop = undefined;
var last_node = undefined;
for(var i = 0; current_choices[i]; ++i) {
var dis = current_choices[i];
dis.node.removeAttribute("href");
dis.node.removeAttribute("accesskey");
if(dis == accepted_choice) {
dis.node.setAttribute("class","cya_accepted_choice");
/* only pivot if there is more than one choice */
if(cya.pivot_choices && current_choices[1] != undefined)
node_to_pop = dis.node;
}
else {
dis.node.setAttribute("class","cya_rejected_choice");
last_node = dis.node;
}
dis.node.removeEventListener("click",
dis.listener);
}
if(node_to_pop != undefined && last_node != undefined
&& last_node.parentNode != undefined) {
node_to_pop.remove();
last_node.parentNode.appendChild(node_to_pop);
}
current_choices = [];
current_hotkeys = {};
}
// The event listener attached to the `click` event of a `choice`.
// (Actually, this function is not passed directly; it is first bound to
// a particular `choice` object.)
var choice_listener = function() {
var first_choice = current_choices[0];
clear_old_choices(this);
var target_page = this.target_page;
if(this.code) {
var code_page = this.code();
if(code_page != undefined) target_page = code_page;
}
if(target_page) {
cya.page(this.target_page);
var scroll_view = element_parameter(cya.scroll_view);
if(scroll_view != undefined) {
// inspired by: https://www.kirupa.com/html5
// /get_element_position_using_javascript.htm
var y = 0;
var el = cya.scroll_to_chosen_choice ? this.node
: first_choice.node;
while(el != undefined && el != scroll_view) {
y += (el.offsetTop - el.scrollTop + el.clientTop);
el = el.offsetParent;
}
if(el == scroll_view) {
var spacer = element_parameter(cya.scroll_spacer);
if(spacer != undefined) {
spacer.setAttribute("style", "height:100%");
var space = Math.max(0, scroll_view.clientHeight
- (scroll_view.scrollHeight - y
- spacer.clientHeight));
spacer.setAttribute("style", "height:"+space);
}
scroll_view.scrollTop = y;
}
else if(console != undefined)
console.log("A <choice> was selected that was not a child"
+" of the scroll_view!");
}
}
}
// When passed an `Element` that can have code, it returns a `Function`
// for that code or null if there is no code. For other `Element`s, it
// always returns `null`.
//
// If `lowerName` is specified, it overrides the value of node.tagName.
// It should be specified if the lowercased name of the node is already
// known, so that it does not have to be redundantly retrieved and case
// folded.
var node_code = function(node, lowerName) {
if(node.cached_code !== undefined) return node.cached_code;
if(lowerName == undefined) lowerName = node.tagName.toLowerCase();
switch(lowerName) {
case "choice":
var all_code = [];
if(node.hasAttribute("execute"))
all_code.push(node.getAttribute("execute"));
if(node.hasAttribute("eval"))
all_code.push("return ("+node.getAttribute("eval")+")");
if(all_code.length > 0)
node.cached_code = new Function(all_code.join("\n"));
break;
case "if":
if(node.hasAttribute("condition"))
node.cached_code =
new Function("return ("+node.getAttribute("condition")+")");
break;
case "eval":
if(node.cached_code == undefined)
node.cached_code = new Function("return ("+node.innerText+")");
break;
case "execute":
if(node.cached_code == undefined)
node.cached_code = new Function(node.innerText);
break;
}
if(node.cached_code === undefined) node.cached_code = null;
return node.cached_code;
}
// Creates a `choice` object for a given `<choice>` element (`source_node`)
// which is being used to create a given `<a>` element (`real_node`), and
// attaches that `choice` object to the `<a>` element. Extracts all
// necessary information from the `<choice>` element.
var register_choice = function(real_node, source_node) {
var choice = {node:real_node};
current_choices.push(choice);
if(source_node.hasAttribute("target")) {
choice.target_page = source_node.getAttribute("target");
}
choice.code = node_code(source_node, "choice");
choice.listener = choice_listener.bind(choice);
real_node.addEventListener("click", choice.listener)
var chosen_hotkey = undefined;
if(source_node.hasAttribute("hotkey"))
chosen_hotkey = source_node.getAttribute("hotkey");
if(chosen_hotkey == undefined) {
for(var i in DEFAULT_HOTKEYS) {
var hotkey = DEFAULT_HOTKEYS[i];
if(current_hotkeys[hotkey] == undefined) {
chosen_hotkey = hotkey;
break;
}
}
}
if(chosen_hotkey != undefined) {
choice.hotkey = chosen_hotkey;
current_hotkeys[chosen_hotkey] = choice
var hotspan = document.createElement("span");
hotspan.setAttribute("class", "cya_hotkey");
hotspan.appendChild(document.createTextNode(chosen_hotkey));
choice.node.appendChild(hotspan);
choice.node.setAttribute("accesskey", chosen_hotkey);
}
}
// `source` is an `Element` from the tree of a `Page`. `target` is the
// `Element` that is being created from `source`. Goes through each of
// `source`'s children (if it has any), and preprocesses them as needed
// before appending them to `target`.
var append_and_translate_children = function(target, source) {
if(source.childNodes == undefined) return;
for(var i = 0; source.childNodes[i]; ++i) {
var child = source.childNodes[i];
if(child.nodeType == document.ELEMENT_NODE) {
var lowerName = child.tagName.toLowerCase();
switch(lowerName) {
case "choice":
var nu = document.createElement("a");
register_choice(nu, child);
nu.setAttribute("class","cya_choice");
nu.setAttribute("href","javascript:(void)0");
append_and_translate_children(nu, child);
target.appendChild(nu);
break;
case "ending":
window.ending = child.innerText;
cya.page(cya.ending_page);
break;
case "if":
var code = node_code(child, lowerName);
if(code) {
if(code())
append_and_translate_children(target, child);
}
else if(console != undefined)
console.log("There is an <if> without a condition");
break;
case "eval":
case "execute":
var code = node_code(child, lowerName);
if(code) {
var result = code();
if(result != undefined) {
if(result instanceof Element)
target.appendChild(result);
else
target.appendChild(document
.createTextNode(result));
}
}
/* code should never be undefined */
break;
default:
var nu = child.cloneNode(false);
append_and_translate_children(nu, child);
target.appendChild(nu);
break;
}
}
else {
target.appendChild(child.cloneNode(true));
}
}
}
// Handles a keydown event.
var keydown = function(e) {
if(!cya.enable_hotkeys) return;
if(e.ctrlKey || e.shiftKey || e.metaKey || e.altKey) return;
var target = e.target;
if(target != undefined && (target.tagName == "INPUT"
|| target.tagName == "TEXTAREA"))
return;
var key;
if(e.key) key = e.key;
else if(e.keyCode) key = string.fromCharCode(e.keyCode);
else return;
if(current_hotkeys[key] != undefined) {
var choice = current_hotkeys[key];
if(e.stopPropagation) e.stopPropagation();
if(e.preventDefault) e.preventDefault();
choice.listener(e);
}
}
/**
* Presents a given `Page` into the `playfield`. May be called at any time
* after Adventure load; if called recursively (as from an `<execute>`
* element inside a `<page>`), the page will be presented immediately after
* all currently-processing `Page`s.
*/
cya.page = function(page) {
if(inside_page) { deferred_pages.push(page); return; }
inside_page = true;
var target = element_parameter(cya.playfield);
if(target == undefined) {
window.alert("Playfield not found! We can't do anything!");
return;
}
var next_page = find_page(page);
if(next_page == undefined) {
window.alert("Page not found: "+page);
}
else {
var to_append = document.createElement("div");
to_append.setAttribute("class", "cya_page");
append_and_translate_children(to_append, next_page);
target.appendChild(to_append);
}
inside_page = false;
var deferred_page = deferred_pages.shift();
if(deferred_page != undefined)
return cya.page(deferred_page);
}
// Does the grunt work of `cya.validate()`
var recursively_validate = function(node, pageName) {
if(node.nodeType != document.ELEMENT_NODE) return true;
var ret = true;
var nodeName = node.tagName.toLowerCase();
switch(nodeName) {
case "choice":
if(node.hasAttribute("target")) {
if(find_page(node.getAttribute("target")) == undefined) {
console.log("page \""+pageName+"\": references a"
+" nonexistent page: "
+node.getAttribute("target"));
ret = false;
}
}
break;
}
try {
node_code(node);
}
catch(e) {
console.log("page \""+pageName+"\": error while compiling"
+" JavaScript code: "+e.message);
ret = false;
}
if(node.childNodes != undefined) {
for(var i = 0; node.childNodes[i]; ++i) {
ret = recursively_validate(node.childNodes[i],
pageName) && ret;
}
}
return ret;
}
/**
* Validates a `<page>`. Makes sure that all of its JavaScript code
* compiles, and none of its `<choice>` elements refer to a nonexistent
* page. (Note: `<choice>` elements that select a nonexistent page through
* JavaScript cannot be detected.)
*
* Returns `true` if the page validated, and `false` if it didn't.
*/
cya.validate = function(page) {
var pageName = page.getAttribute("name");
if(pageName == undefined) pageName = "(an unnamed page)";
return recursively_validate(page, pageName);
}
/**
* Locates all `<page>` elements in the Adventure, and caches their names
* so they can be retrieved quickly.
*
* Normally you do not need to call this function, as it is called
* automatically when the Adventure is loaded. You only need to call it if
* you are dynamically adding or removing `<page>` elements from the
* Adventure's DOM tree. (If you are building `<page>` `Elements`
* dynamically and passing them around directly, you do _not_ need to call
* this function as those are not being accessed by name.)
*
* Returns true if all pages validated (or no pages were checked), and
* false if validation of at least one page failed.
*/
cya.findpages = function() {
var ok = true;
cya.pages = document.getElementsByTagName("page");
page_cache = {}
for(var i = 0; cya.pages[i]; ++i) {
var name = cya.pages[i].getAttribute("name");
if(name != undefined)
page_cache[name] = cya.pages[i];
else if(console != undefined)
console.log("Warning: found an unnamed <page>!");
if(cya.validate_on_load) ok = cya.validate(cya.pages[i]) && ok;
}
return ok;
}
// Initializes the Adventure. This consists of calling `cya.findpages()`,
// presenting the `start_page`, and (if present) scrolling the
// `scroll_view` to the top.
// This is called automatically when the `DOMContentLoaded` event fires.
var init = function() {
if(cya.findpages()) {
cya.page(cya.start_page);
if(cya.scroll_view) {
cya.scroll_view.scrollTop = 0;
}
document.addEventListener("keydown", keydown, false);
}
else {
var fakePage = document.createElement("page");
fakePage.innerText = "Adventure validation failed. See the"
+" JavaScript console for details.";
cya.page(fakePage);
}
}
document.addEventListener("DOMContentLoaded", init, false);
})()
// @license-end