forked from EricEve/adv3lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenuweb.t
337 lines (285 loc) · 10.2 KB
/
menuweb.t
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
#charset "us-ascii"
/*
* TADS 3 Library - Menu System, console edition
*
* This implements the menusys user interface for the traditional
* console-mode interpreters.
*/
#include "advlite.h"
/* ------------------------------------------------------------------------ */
/*
* Menu Item - user interface implementation for the console
*/
modify MenuItem
/*
* Call menu.display when you're ready to show the menu. This
* should be called on the top-level menu; we run the entire menu
* display process, and return when the user exits from the menu
* tree.
*/
display()
{
/* save the top-level key list */
MenuItem.curKeyList = keyList;
/* set myself as the current menu and the top-level menu */
MenuItem.topMenu = self;
MenuItem.curMenu = self;
/* display the menu in the javascript client */
showMenu(nil);
/* process network events until the user closes the menu */
MenuItem.isOpen = true;
processNetRequests({: !MenuItem.isOpen });
}
/* current menu, and current top-level menu */
curMenu = nil
topMenu = nil
/* is the menu open? */
isOpen = nil
/* show this menu as a submenu */
showMenu(from)
{
/* get the XML representation of the menu item list */
local xml = '<menusys><<getXML(from)>></menusys>';
/* tell the javascript client to display the menu */
webMainWin.sendWinEvent(xml);
/* save this in the main window as the current menu state */
webMainWin.menuSysState = xml;
}
/* navigate into a submenu */
enterSubMenu(idx)
{
/* validate the index and select the new menu */
if (idx >= 1 && idx <= contents.length())
{
/* get the new menu */
local m = contents[idx];
/* note the new menu location */
MenuItem.curMenu = m;
/* show the new menu */
m.showMenu(self);
}
}
/*
* Package my menu items as XML, to send to the javascript API.
* 'from' is the menu we just navigated from, if any. This is nil
* when we enter the top level menu, since we're not navigating from
* another menu; when we navigate from a parent to a child, this is
* the parent; when we return from a child to a parent, this is the
* child; and when we move directly from sibling to sibling (via a
* next/previous chapter command), this is the sibling. When we
* display a new topic in a topic list menu, this is simply 'self'.
*/
getXML(from)
{
/* set up a string buffer for the xml */
local s = new StringBuffer();
/* update the menu contents */
updateContents();
/* start with the menu title */
s.append('<title><<title.htmlify()>></title>');
/* note if we're the top-level menu */
if (location == nil || MenuItem.topMenu == self)
s.append('<isTop/>');
/* run through the contents */
for (local item in contents)
s.append('<menuItem><<item.title.htmlify()>></menuItem>');
/* if the 'from' menu is a child, initially select it */
local idx = contents.indexOf(from);
if (idx != nil)
s.append('<fromChild><<idx>></fromChild>');
/* add the keys */
getKeysXML(s);
/* return the string */
return toString(s);
}
/* get the XML description of the top-level key list */
getKeysXML(buf)
{
buf.append('<keylists>');
for (local kl in curKeyList)
{
buf.append('<keylist>');
for (local k in kl)
{
if (k == ' ')
k = 'U+0020';
buf.append('<key><<k>></key>');
}
buf.append('</keylist>');
}
buf.append('</keylists>');
}
/*
* Prepare a title or content string for our XML output. If 'val' is
* a string, we'll run it through the output formatter to expand any
* special <.xxx> sequences. If 'val' is a property, we'll evaluate
* the property of self, capturing the output if it generates any or
* capturing the string if it returns one. In all cases, we take the
* result string and convert TADS special characters to HTML, and
* finally html-escape the result for inclusion in XML output, and
* return the resulting string.
*/
formatXML(func)
{
/* call the function and process it through the menu stream filters */
local txt = menuOutputStream.captureOutput(func);
/* convert special characters and html-escape the result */
return txt.specialsToHtml().htmlify();
}
;
/*
* Menu system UI request processor. This receives requests from the
* javascript client in response to user actions: selecting a menu item,
* navigating to the parent menu, closing the menu.
*/
menuSysEventPage: WebResource
vpath = '/webui/menusys'
processRequest(req, query)
{
/* check the action type */
if (query.isKeyPresent('close'))
{
/* close the menu */
MenuItem.isOpen = nil;
}
else if (query.isKeyPresent('prev'))
{
/* go to the parent menu, or close the menu if at the top */
local cur = MenuItem.curMenu;
if (cur != nil && cur != MenuItem.topMenu && cur.location != nil)
{
/* go to the parent */
local par = cur.location;
MenuItem.curMenu = par;
par.showMenu(cur);
}
else
{
/* there's no parent - close the menu */
MenuItem.isOpen = nil;
/* tell the UI to close its menu dialog */
webMainWin.sendWinEvent('<menusys><close/></menusys>');
webMainWin.menuSysState = '';
}
}
else if (query.isKeyPresent('select'))
{
/*
* Select a child menu. The 'select=n' parameter is the
* index in the current menu's child list of the new item to
* select. Retrieve the index.
*/
MenuItem.curMenu.enterSubMenu(toInteger(query['select']));
}
else if (query.isKeyPresent('nextTopic'))
{
/* get the next topic in the current topic menu */
sendAck(req, MenuItem.curMenu.getNextTopicXML());
/* rebuild the menu state for the change */
webMainWin.menuSysState =
'<menusys><<MenuItem.curMenu.getXML(MenuItem.curMenu)
>></menusys>';
/* we've sent our reply, so we're done */
return;
}
else if (query.isKeyPresent('chapter'))
{
/* get the next or previous chapter, as applicable */
local dir = query['chapter'];
local m = MenuItem.curMenu;
local par = m.location;
local nxt = (dir == 'next'
? par.getNextMenu(m) : par.getPrevMenu(m));
/* enter this chapter */
if (nxt != nil)
{
MenuItem.curMenu = nxt;
nxt.showMenu(m);
}
}
/* acknowledge the request */
sendAck(req);
}
;
/* ------------------------------------------------------------------------ */
/*
* Menu topic item - console UI implementation
*/
modify MenuTopicItem
/* get the XML description of my menu list */
getXML(from)
{
/* start with an empty result buffer */
local s = new StringBuffer();
/* update our contents, as needed */
updateContents();
/* add the title and total number of items in the menu */
s.append('<title><<title.htmlify()>></title>'
+ '<numItems><<menuContents.length()>></numItems>');
/* note if we're the top-level menu */
if (location == nil || MenuItem.topMenu == self)
s.append('<isTop/>');
/* add each item in our list */
for (local i in 1..lastDisplayed)
s.append(getTopicXML(i));
/* add the keys */
getKeysXML(s);
/* return the XML string */
return toString(s);
}
/* get the next topic, in XML format */
getNextTopicXML()
{
/* if we're not already at the last item, advance the counter */
if (lastDisplayed < menuContents.length())
++lastDisplayed;
/* format the last item */
return getTopicXML(lastDisplayed);
}
/* get the XML formatted description of the item at the given index */
getTopicXML(i)
{
/* get the item */
local item = menuContents[i];
/* get the item's text, and format as XML */
item = formatXML(dataType(item) == TypeObject
? {: item.getItemText() } : item);
/* format the item text */
return '<topicItem><<item>></topicItem>';
}
;
/* ------------------------------------------------------------------------ */
/*
* Long topic item
*/
modify MenuLongTopicItem
/* get my XML description */
getXML(from)
{
/* start with an empty result buffer */
local s = new StringBuffer();
/* update our contents, as needed */
updateContents();
/* add my title (heading) */
local t = heading != nil && heading != '' ? heading : title;
s.append('<title><<t.htmlify()>></title>');
/* add our contents */
s.append('<longTopic><<formatXML({: menuContents })>></longTopic>');
/*
* if this is a chapter menu, note if we have links for the next
* and previous chapters
*/
if (isChapterMenu)
{
local m;
if ((m = location.getNextMenu(self)) != nil)
s.append('<nextChapter><<m.title.htmlify()>></nextChapter>');
if ((m = location.getPrevMenu(self)) != nil)
s.append('<prevChapter><<m.title.htmlify()>></prevChapter>');
}
/* add the keys */
getKeysXML(s);
/* return the XML string */
return toString(s);
}
;