-
Notifications
You must be signed in to change notification settings - Fork 77
/
shrdlite-html.ts
106 lines (90 loc) · 3.77 KB
/
shrdlite-html.ts
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
import {World} from "./World";
import {SVGWorld} from "./SVGWorld";
import {ExampleWorlds} from "./ExampleWorlds";
import {parseUtteranceIntoPlan, splitStringIntoPlan} from "./Shrdlite";
import "./lib/jquery";
/********************************************************************************
** shrdlite-html
This is the main file for the browser-based version.
You don't have to edit this file.
********************************************************************************/
var defaultWorld = 'small';
var defaultSpeech = false;
$(function(){
var current : string = getURLParameter('world');
if (!(current in ExampleWorlds)) {
current = defaultWorld;
}
var speech : string = (getURLParameter('speech') || "").toLowerCase();
var useSpeech : boolean = (speech == 'true' || speech == '1' || defaultSpeech);
$('#currentworld').text(current);
$('<a>').text('reset')
.attr('href', '?world=' + current + '&speech=' + useSpeech)
.appendTo($('#resetworld'));
$('#otherworlds').empty();
for (var wname in ExampleWorlds) {
if (wname !== current) {
$('<a>').text(wname)
.attr('href', '?world=' + wname + '&speech=' + useSpeech)
.appendTo($('#otherworlds'))
.after(' ');
}
}
$('<a>').text(useSpeech ? 'turn off' : 'turn on')
.attr('href', '?world=' + current + '&speech=' + (!useSpeech))
.appendTo($('#togglespeech'));
var world = new SVGWorld(ExampleWorlds[current], useSpeech);
interactiveLoop(world);
});
// The interaction loop.
// It calls 'splitStringIntoPlan()' and 'parseUtteranceIntoPlan()' after each utterance.
function interactiveLoop(world : World) : void {
function endlessLoop(utterance : string = "") : void {
var inputPrompt = "What can I do for you today? ";
var nextInput = () => world.readUserInput(inputPrompt, endlessLoop);
if (utterance.trim()) {
var theplan : string[] | null = splitStringIntoPlan(utterance);
if (!theplan) {
theplan = parseUtteranceIntoPlan(world, utterance);
}
if (theplan) {
world.printDebugInfo("Plan: " + theplan.join(", "));
world.performPlan(theplan, nextInput);
return;
}
}
nextInput();
}
world.printWorld(endlessLoop);
}
// This function will ask for confirmation if the user tries to close the window.
// Adapted from: http://www.openjs.com/scripts/events/exit_confirmation.php
function goodbye(event : any) {
// Note: the type of 'event' is really 'Event', but its interface says that
// 'event.returnValue' is a boolean, which is not the case, so we set the type to 'any'
if (!event) event = window.event;
// event.cancelBubble is supported by IE - this will kill the bubbling process.
event.cancelBubble = true;
// This is displayed in the dialog:
event.returnValue = 'Are you certain?\nYou cannot undo this, you know.';
// event.stopPropagation works in Firefox.
if (event.stopPropagation) {
event.stopPropagation();
event.preventDefault();
}
}
window.onbeforeunload = goodbye;
// This function gets the URL parameter value for a given key all parameters in the URL string,
// i.e., if the URL is "http://..../....?x=3&y=42", then getURLParameter("y") == 42
// Adapted from: http://www.jquerybyexample.net/2012/06/get-url-parameters-using-jquery.html
function getURLParameter(sParam : string) : string {
var sPageURL = window.location.search.slice(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++) {
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam) {
return sParameterName[1];
}
}
return "";
}