| 1 | /* Copyright (c) 2010 by Michael J. Roberts. All Rights Reserved. */ |
| 2 | /* |
| 3 | * Status line scripts for TADS 3 Web UI. |
| 4 | */ |
| 5 | |
| 6 | function statLineInit() |
| 7 | { |
| 8 | // initialize the utility package |
| 9 | utilInit(); |
| 10 | |
| 11 | // ask our parent to recalculate the window layout now that we're here |
| 12 | parent.calcLayout(); |
| 13 | |
| 14 | // set up the default document-level key handler - this will send focus |
| 15 | // back to the main command window if the user presses a key while |
| 16 | // focus is in our window (and not in a key-consuming control) |
| 17 | addKeyHandler(document, genericDocKeyFocus); |
| 18 | |
| 19 | // get the initial state |
| 20 | getInitState(); |
| 21 | |
| 22 | // load preference styles |
| 23 | $win().prefsToCSS(null, window); |
| 24 | } |
| 25 | |
| 26 | function onGameState(req, resp) |
| 27 | { |
| 28 | // set the text from the status |
| 29 | setStatusText(xmlChildText(resp, "text")); |
| 30 | |
| 31 | // recalculate the parent window layout, in case the layout is sensitive |
| 32 | // to our content size |
| 33 | parent.calcLayout(); |
| 34 | } |
| 35 | |
| 36 | function onGameEvent(req, resp) |
| 37 | { |
| 38 | if (xmlHasChild(resp, "text")) |
| 39 | setStatusText(xmlChildText(resp, "text")); |
| 40 | if (xmlHasChild(resp, "appendText")) |
| 41 | appendStatusText(xmlChildText(resp, "appendText")); |
| 42 | if (xmlHasChild(resp, "resize")) |
| 43 | parent.calcLayout(); |
| 44 | if (xmlHasChild(resp, "setsize")) |
| 45 | parent.setWinLayout(this, xmlChildText(resp, "setsize")); |
| 46 | } |
| 47 | |
| 48 | var statText = ""; |
| 49 | function setStatusText(msg) |
| 50 | { |
| 51 | msg = BrowserInfo.adjustText(msg); |
| 52 | statText = msg; |
| 53 | $("statusMain").innerHTML = msg; |
| 54 | } |
| 55 | |
| 56 | function appendStatusText(msg) |
| 57 | { |
| 58 | msg = BrowserInfo.adjustText(msg); |
| 59 | statText += msg; |
| 60 | $("statusMain").innerHTML = statText; |
| 61 | } |
| 62 | |
| 63 | /* ------------------------------------------------------------------------ */ |
| 64 | /* |
| 65 | * Network waiting indicator ("throbber"). We show this as part of the |
| 66 | * status line because the throbber is basically a status display, so (a) |
| 67 | * it's logical to group with the status line in the abstract, and (b) in |
| 68 | * concrete terms, the status line is usually a good visual location for |
| 69 | * this because it's prominent but out of the way of the input area of the |
| 70 | * window. |
| 71 | */ |
| 72 | var NetThrobber = |
| 73 | { |
| 74 | // number of open network requests |
| 75 | netRequestCount: 0, |
| 76 | |
| 77 | // timeout for starting the visual indicator, if one is pending |
| 78 | starter: null, |
| 79 | |
| 80 | // start a network request |
| 81 | onStartRequest: function() |
| 82 | { |
| 83 | // Count the request; if it's the first one, set a timer to |
| 84 | // start the throbber. Note that we don't simply start it |
| 85 | // immediately: if the request completes quickly, it's better |
| 86 | // not to show the throbber at all, since the quick on-off |
| 87 | // flash would be annoying. We only need the throbber when |
| 88 | // the request takes a noticeable amount of time to complete. |
| 89 | // So, wait until a few moments before starting it; if the |
| 90 | // request completes before the wait expires, we'll skip the |
| 91 | // whole thing and avoid any flashing. If the request turns |
| 92 | // out to take longer than the timeout, it's a long enough wait |
| 93 | // that we want visual feedback that something's going on. |
| 94 | if (this.netRequestCount++ == 0 && !this.starter) |
| 95 | { |
| 96 | this.starter = setTimeout(function() { |
| 97 | NetThrobber.starter = null; |
| 98 | NetThrobber.show(); |
| 99 | }, 500); |
| 100 | } |
| 101 | }, |
| 102 | |
| 103 | // end a network request |
| 104 | onEndRequest: function() |
| 105 | { |
| 106 | // Decrement the open request count. If this was the last open |
| 107 | // request, turn off the wait indicator. |
| 108 | if (--this.netRequestCount == 0) |
| 109 | this.hide(); |
| 110 | }, |
| 111 | |
| 112 | // show the throbber |
| 113 | show: function() |
| 114 | { |
| 115 | $("netThrobberDiv").style.display = "block"; |
| 116 | $win().status = "Waiting for the game to respond..."; |
| 117 | }, |
| 118 | |
| 119 | // hide the throbber |
| 120 | hide: function() |
| 121 | { |
| 122 | // if we have a timer waiting to show the throbber, simply kill |
| 123 | // the timer; otherwise hide the display |
| 124 | if (this.starter) |
| 125 | { |
| 126 | clearTimeout(this.starter); |
| 127 | this.starter = null; |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | $("netThrobberDiv").style.display = "none"; |
| 132 | $win().status = ""; |
| 133 | } |
| 134 | } |
| 135 | }; |
| 136 |