OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of swarmlib; | 5 part of swarmlib; |
6 | 6 |
7 /** | 7 /** |
8 * The base class that should be extended by all HTML applications. | 8 * The base class that should be extended by all HTML applications. |
9 * | 9 * |
10 * It should both be easy to use for users coming over from JavaScript, but | 10 * It should both be easy to use for users coming over from JavaScript, but |
11 * also offer a clear notion of OO encapsulation. | 11 * also offer a clear notion of OO encapsulation. |
12 * | 12 * |
13 * This class or something similar belongs in the standard DOM library. | 13 * This class or something similar belongs in the standard DOM library. |
14 */ | 14 */ |
15 class App { | 15 class App { |
16 | |
17 App() {} | 16 App() {} |
18 | 17 |
19 /** Begins executing code in this [App]. */ | 18 /** Begins executing code in this [App]. */ |
20 void run() { | 19 void run() { |
21 // If the script is async, by the time we get here the DOM content may | 20 // If the script is async, by the time we get here the DOM content may |
22 // already be loaded, so waiting on the DOMContentLoaded event is a no-op. | 21 // already be loaded, so waiting on the DOMContentLoaded event is a no-op. |
23 // Guard against this by checking whether the document readiness state has | 22 // Guard against this by checking whether the document readiness state has |
24 // gotten as far as "interactive". (We believe the transition to | 23 // gotten as far as "interactive". (We believe the transition to |
25 // "interactive" is when the DOMContentLoaded event fires, but haven't | 24 // "interactive" is when the DOMContentLoaded event fires, but haven't |
26 // found that specified; if that's not true it leaves a race bug.) | 25 // found that specified; if that's not true it leaves a race bug.) |
27 if (document.readyState == "interactive" || | 26 if (document.readyState == "interactive" || |
28 document.readyState == "complete" || | 27 document.readyState == "complete" || |
29 document.readyState == "loaded") { | 28 document.readyState == "loaded") { |
30 // We use a timer to insure that onLoad is always called in an async | 29 // We use a timer to insure that onLoad is always called in an async |
31 // manner even if the document is already loaded. | 30 // manner even if the document is already loaded. |
32 Timer.run(onLoad); | 31 Timer.run(onLoad); |
33 } else { | 32 } else { |
34 window.onContentLoaded.listen( | 33 window.onContentLoaded.listen( |
35 // TODO(sigmund): Consider eliminating the call to "wrap", for | 34 // TODO(sigmund): Consider eliminating the call to "wrap", for |
36 // instance, modify event listeners to always wrap, or extend DOM code | 35 // instance, modify event listeners to always wrap, or extend DOM code |
37 // to intercept the beginning & end of each event loop | 36 // to intercept the beginning & end of each event loop |
38 EventBatch.wrap((Event event) => onLoad())); | 37 EventBatch.wrap((Event event) => onLoad())); |
39 } | 38 } |
40 } | 39 } |
41 | 40 |
42 /** | 41 /** |
43 * Called when the DOM is fully loaded but potentially before resources. | 42 * Called when the DOM is fully loaded but potentially before resources. |
44 * | 43 * |
45 * For most apps, any startup code should be in this method. Be sure to call | 44 * For most apps, any startup code should be in this method. Be sure to call |
46 * the superclass implementation. | 45 * the superclass implementation. |
47 */ | 46 */ |
48 void onLoad() { | 47 void onLoad() { |
49 // Prevent the default browser behavior of scrolling the window. | 48 // Prevent the default browser behavior of scrolling the window. |
50 document.onTouchMove.listen((Event event) => event.preventDefault()); | 49 document.onTouchMove.listen((Event event) => event.preventDefault()); |
51 | 50 |
52 // Swap and reload the cache if ready | 51 // Swap and reload the cache if ready |
53 if (!swapAndReloadCache()) { | 52 if (!swapAndReloadCache()) { |
54 // Otherwise wait until an update to the cache is ready | 53 // Otherwise wait until an update to the cache is ready |
55 window.applicationCache.onUpdateReady.listen( | 54 window.applicationCache.onUpdateReady.listen((e) => swapAndReloadCache()); |
56 (e) => swapAndReloadCache()); | |
57 } | 55 } |
58 } | 56 } |
59 | 57 |
60 /** | 58 /** |
61 * Erase the static splash screen. | 59 * Erase the static splash screen. |
62 * | 60 * |
63 * Assumption: if a splash screen exists, an element #appSplash contains it. | 61 * Assumption: if a splash screen exists, an element #appSplash contains it. |
64 */ | 62 */ |
65 void eraseSplashScreen() { | 63 void eraseSplashScreen() { |
66 final splash = document.querySelector("#appSplash"); | 64 final splash = document.querySelector("#appSplash"); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 // TODO(jmesserly): Several problems with this: | 100 // TODO(jmesserly): Several problems with this: |
103 // * How do we authenticate against the server? | 101 // * How do we authenticate against the server? |
104 // * How do we talk to a server other than thump? | 102 // * How do we talk to a server other than thump? |
105 assert(url.startsWith('/')); | 103 assert(url.startsWith('/')); |
106 return 'http://thump.googleplex.com$url'; | 104 return 'http://thump.googleplex.com$url'; |
107 } else { | 105 } else { |
108 return url; | 106 return url; |
109 } | 107 } |
110 } | 108 } |
111 } | 109 } |
OLD | NEW |