Debian Codegraph @

terps/tads/tads3/lib/webuires/layoutwin.js

raw symbols SCIP
1 /* Copyright 2010 Michael Roberts */
2 /*
3 * Layout window scripts.
4 */
5
6 // initialize the layout window
7 function layoutInit()
8 {
9 // set up our event handlers
10 window.onresize = calcLayout;
11 window.onGameEvent = layoutGameEvent;
12 window.onGameState = layoutGameState;
13
14 // set up some window methods
15 window.windowFromPath = windowFromPath;
16 }
17
18 // server-to-client game event handler for a layout window
19 function layoutGameEvent(req, resp)
20 {
21 if (xmlHasChild(resp, "subwin"))
22 {
23 // subwindow creation - go create the window
24 createLayoutFramesXML(xmlChild(resp, "subwin"));
25 }
26
27 if (xmlHasChild(resp, "shutdown"))
28 {
29 // propagate shutdown notifications to subwindows
30 layoutWindows.forEach(function(w) {
31 if (w.onGameEvent)
32 w.onGameEvent(req, resp);
33 });
34 }
35 }
36
37 // server-to-client game state event handler for a layout window
38 function layoutGameState(req, resp)
39 {
40 // create subwindows
41 if (xmlHasChild(resp, "subwin"))
42 {
43 // create each subwindow in the list
44 createLayoutFramesXML(xmlChild(resp, "subwin"));
45 }
46 }
47
48 // set a new size for an existing layout window
49 function setWinLayout(win, rc)
50 {
51 // find the window
52 var l = layoutWinFromWindow(win);
53
54 // if we found it, set the new size information
55 if (l)
56 {
57 // convert 'rc' from a comma list, if applicable
58 if (typeof(rc) == "string")
59 rc = rc.replace(/^ +| +$/g, "").replace(/, +| +,/g, ",")
60 .split(",");
61
62 // replace the old rectangle with non-null new elements
63 for (var i = 0 ; i < rc.length && i < l.rc.length ; ++i)
64 {
65 // copy non-empty replacements
66 if (rc[i] != "")
67 l.rc[i] = rc[i];
68 }
69 }
70 }
71
72 // Window tracker list. We keep an object here representing each IFRAME
73 // window we're managing.
74 var layoutWindows = { };
75
76 // create layout frames from a list of XML <subwin> tags
77 function createLayoutFramesXML(subs)
78 {
79 // run through each <subwin> tag
80 for (var i = 0 ; i < subs.length ; ++i)
81 {
82 // get this child and decode its XML
83 var sub = subs[i];
84 var name = xmlChildText(sub, "name");
85 var rc = xmlChildText(sub, "pos");
86 var src = xmlChildText(sub, "src");
87
88 // turn the size list into an array
89 rc = rc.replace(/^ +| +$/g, "").replace(/, +| +,/g, ",").split(",");
90
91 // create the IFRAME for this window
92 createLayoutFrame(name, rc, "", src);
93 }
94
95 // recalculate the layout for the new windows
96 calcLayout();
97 }
98
99 // create a new iframe layout window
100 function createLayoutFrame(name, rc, style, src)
101 {
102 var obj;
103
104 // check to see if the window already exists
105 if (name in layoutWindows)
106 {
107 // we already have a window by this name - reuse it
108 obj = layoutWindows[name];
109
110 // update the window layout rectangle
111 obj.rc = rc;
112 }
113 else
114 {
115 // create the IFRAME element
116 var ele = document.createElement("iframe");
117 ele.setAttribute("id", name);
118 ele.setAttribute("name", name);
119 ele.setAttribute("style", style);
120
121 // this is for IE, to turn off the border (the capitalization counts)
122 ele.setAttribute("frameBorder", "0");
123
124 // add the new element to the document at the root level
125 var body = document.body || document.documentElement;
126 body.appendChild(ele);
127
128 // set up functions to retrieve its document and window objects
129 var getDoc = function() {
130 var f = this.ele;
131 return f.contentWindow ? f.contentWindow.document
132 : f.contentDocument;
133 };
134 var getWin = function() {
135 var f = this.ele;
136 return f.contentWindow || f.contentDocument.window;
137 };
138
139 // set up the tracker object
140 obj = {
141 name: name, rc: rc, ele: ele, getDoc: getDoc, getWin: getWin
142 };
143
144 // save it in the window list
145 layoutWindows[name] = obj;
146
147 // navigate to the location
148 if (src)
149 obj.getWin().location = src;
150
151 }
152
153 // recalculate the window layout for the new addition
154 calcLayout();
155
156 // return the new tracker
157 return obj;
158 }
159
160 // get a window tracker object from the IFRAME element
161 function layoutWinFromEle(ele)
162 {
163 return layoutWindows.valWhich(function(w) { return w.ele == ele; });
164 }
165
166 // get the layout object for a given window
167 function layoutWinFromWindow(win)
168 {
169 // scan our list of windows for a match to the target window
170 return layoutWindows.valWhich(
171 function(w) { return w.getWin() == win; });
172 }
173
174 // Get the window for a relative path. The first path element is
175 // interpreted as a child of the current window.
176 function windowFromPath(path)
177 {
178 // if the argument is a string, split it up on '.' separators
179 if (typeof(path) == "string")
180 path = path.split(".");
181
182 // look up the first path element in our child list
183 var w;
184 if (!(w = layoutWindows[path.shift()]))
185 return null;
186
187 // if that's the only path element, return the child
188 if (path.length == 0)
189 return w.getWin();
190
191 // look up the rest of the path via the child
192 return w.getWin().windowFromPath(path);
193 }
194
195 // get the name for a child window
196 function pathFromWindow(win)
197 {
198 // find the layout window object for the window
199 var l = layoutWinFromWindow(win);
200
201 // if we found it, build the path from our page name plus the window name
202 if (l)
203 return pageName + "." + l.name;
204 else
205 return null;
206 }
207
208 // recalculate the layout
209 function calcLayout()
210 {
211 // clear the cache of content rectangles
212 layoutWindows.forEach(function(w) { w.rcContent = null; });
213
214 // run through our list of tracker objects
215 layoutWindows.forEach(function(w)
216 {
217 // calculate each coordinate for this window
218 var rc = [];
219 for (var j = 0 ; j < 4 ; ++j)
220 rc[j] = calcLayoutCoord(w, j);
221
222 // limit the coordinates to the window size
223 var wrc = getWindowRect();
224 rc[0] = (rc[0] < 0 ? 0 : rc[0] > wrc.width ? wrc.width : rc[0]);
225 rc[1] = (rc[1] < 0 ? 0 : rc[1] > wrc.height ? wrc.height : rc[1]);
226 rc[2] = (rc[2] < 0 ? 0 :
227 rc[0] + rc[2] > wrc.width ? wrc.width - rc[0] : rc[2]);
228 rc[3] = (rc[3] < 0 ? 0 :
229 rc[1] + rc[3] > wrc.height ? wrc.height - rc[1] : rc[3]);
230
231 // set the new layout coordinates in the IFRAME's style properties
232 var ele = w.ele;
233 try
234 {
235 ele.style.left = rc[0] + "px";
236 ele.style.top = rc[1] + "px";
237 ele.style.width = rc[2] + "px";
238 ele.style.height = rc[3] + "px";
239 }
240 catch (exc)
241 {
242 }
243 });
244 }
245
246 // Calculate one coordinate for an iframe window. This resolves the
247 // current values of the constraint-based properties.
248 function calcLayoutCoord(win, i)
249 {
250 // get the target element from the (left, top, width, height) list
251 var val = win.rc[i];
252
253 // first, turn typographical units into pixel sizes: em, en, ex
254 val = val.replace(/([0-9.]+)(em|en|ex)/gi, function(m, val, unit) {
255
256 // Create a probe item on the page to measure the em size. This is
257 // simply a DIV element with its height explicitly set to 1 unit of
258 // whichever unit we're measuring.
259 var d = win.getDoc().createElement("DIV");
260 var b = win.getDoc().body;
261 d.style.height = "1" + unit;
262 b.appendChild(d);
263
264 // measure the height of the division to get the pixels per unit,
265 // then we're done with the probe element
266 var pixels = d.offsetHeight;
267 b.removeChild(d);
268
269 // return the number times the pixes per per
270 return parseFloat(val) * pixels;
271 });
272
273 // Next, apply percentages: these are relative to the window interior
274 // dimensions. Left/width percentages are relative to the window width,
275 // and top/height percentages are relative to the height.
276 val = val.replace(/([0-9]+)%/gi, function(m, val) {
277 return Math.floor(parseInt(val)/100
278 * getWindowRect()[(i % 2) ? "height" : "width"]);
279 });
280
281 // Apply object.prop items
282 val = val.replace(/([a-z_0-9]+)\.([a-z]+)/gi, function(m, obj, prop) {
283
284 // check for special names
285 switch(m)
286 {
287 case "window.width":
288 return getWindowRect().width;
289
290 case "window.height":
291 return getWindowRect().height;
292
293 case "content.width":
294 // if we don't have a cached value, cache it now
295 if (!win.rcContent)
296 win.rcContent = getContentRect(win.getWin());
297
298 // return the width from the cached content rectangle
299 return win.rcContent.width;
300
301 case "content.height":
302 // if we don't have a cached value, cache it now
303 if (!win.rcContent)
304 win.rcContent = getContentRect(win.getWin());
305
306 // return the height from the cached content rectangle
307 return win.rcContent.height;
308 }
309
310 // if 'obj' is the name of one of our layout windows, get a property
311 // of that window
312 if (obj in layoutWindows)
313 {
314 // get the window object
315 var w = layoutWindows[obj];
316
317 // look up the property
318 switch (prop)
319 {
320 case "left":
321 return calcLayoutCoord(w, 0);
322
323 case "top":
324 return calcLayoutCoord(w, 1);
325
326 case "width":
327 return calcLayoutCoord(w, 2);
328
329 case "height":
330 return calcLayoutCoord(w, 3);
331
332 case "right":
333 return parseInt(calcLayoutCoord(w, 0))
334 + parseInt(calcLayouttCoord(w, 2));
335
336 case "bottom":
337 return parseInt(calcLayoutCoord(w, 1))
338 + parseInt(calcLayoutCoord(w, 3));
339 }
340 }
341
342 // if we didn't find a translation, just return the original text
343 return m;
344 });
345
346 // The whole thing should be expressed as an integer expression now,
347 // with each integer being implicitly pixels (but with no explicit
348 // units in the string). Apply any arithmetic operators to the pixel
349 // expression.
350 if (typeof(val) == "string" && val.match(/^[- +*/0-9]+$/))
351 val = eval(val);
352
353 // turn it into an integer
354 if (typeof(val) == "string")
355 val = parseInt(val);
356
357 // return the final value
358 return val;
359 }
360
361 /*
362 * Set the default focus for a keyboard event to the appropriate child
363 * window. If we have a default command-line window - which is identified
364 * as a window named "command" - we'll send the focus there.
365 */
366 function setDefaultFocusChild(desc, fromWin)
367 {
368 // if we have a child named "command", set focus there
369 var w = layoutWindows["command"];
370 if (w && w.getWin().setDefaultFocusChild)
371 w.getWin().setDefaultFocusChild(desc, fromWin);
372 }
373
374 /*
375 * Apply a set of CSS styles to this window and its children
376 */
377 var origApplyCSS = applyCSS;
378 applyCSS = function(tab)
379 {
380 // apply the styles to this window
381 origApplyCSS(tab);
382
383 // apply the changes to each child
384 layoutWindows.forEach(function(w) { w.getWin().applyCSS(tab); });
385 }