| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * The base class that should be extended by all HTML applications. | |
| 7 * | |
| 8 * It should both be easy to use for users coming over from JavaScript, but | |
| 9 * also offer a clear notion of OO encapsulation. | |
| 10 * | |
| 11 * This class or something similar belongs in the standard DOM library. | |
| 12 */ | |
| 13 class App { | |
| 14 | |
| 15 App() {} | |
| 16 | |
| 17 /** Begins executing code in this [App]. */ | |
| 18 void run() { | |
| 19 // If the script is async, by the time we get here the DOM content may | |
| 20 // already be loaded, so waiting on the DOMContentLoaded event is a no-op. | |
| 21 // Guard against this by checking whether the document readiness state has | |
| 22 // gotten as far as "interactive". (We believe the transition to | |
| 23 // "interactive" is when the DOMContentLoaded event fires, but haven't | |
| 24 // found that specified; if that's not true it leaves a race bug.) | |
| 25 if (document.readyState == "interactive" || | |
| 26 document.readyState == "complete" || | |
| 27 document.readyState == "loaded") { | |
| 28 // We use setTimeout to insure that onLoad is always called in an async | |
| 29 // manner even if the document is already loaded. | |
| 30 window.setTimeout(() => onLoad(), 0); | |
| 31 } else { | |
| 32 window.on.contentLoaded.add( | |
| 33 // TODO(sigmund): Consider eliminating the call to "wrap", for | |
| 34 // instance, modify event listeners to always wrap, or extend DOM code | |
| 35 // to intercept the beginning & end of each event loop | |
| 36 EventBatch.wrap((Event event) => onLoad())); | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 /** | |
| 41 * Called when the DOM is fully loaded but potentially before resources. | |
| 42 * | |
| 43 * For most apps, any startup code should be in this method. Be sure to call | |
| 44 * the superclass implementation. | |
| 45 */ | |
| 46 void onLoad() { | |
| 47 // Prevent the default browser behavior of scrolling the window. | |
| 48 document.on.touchMove.add((Event event) => event.preventDefault()); | |
| 49 | |
| 50 // Swap and reload the cache if ready | |
| 51 if (!swapAndReloadCache()) { | |
| 52 // Otherwise wait until an update to the cache is ready | |
| 53 window.applicationCache.on.updateReady.add( | |
| 54 (e) => swapAndReloadCache()); | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 /** | |
| 59 * Erase the static splash screen. | |
| 60 * | |
| 61 * Assumption: if a splash screen exists, an element #appSplash contains it. | |
| 62 */ | |
| 63 void eraseSplashScreen() { | |
| 64 final splash = document.query("#appSplash"); | |
| 65 // Delete it if found, but it's okay for it not to be -- maybe | |
| 66 // somebody just didn't want to use our splash mechanism. | |
| 67 if (splash !== null) { | |
| 68 splash.remove(); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 /** | |
| 73 * Swaps and reloads the app cache if an update is ready. Returns false if | |
| 74 * an update is not ready. | |
| 75 */ | |
| 76 bool swapAndReloadCache() { | |
| 77 DOMApplicationCache appCache = window.applicationCache; | |
| 78 if (appCache.status !== DOMApplicationCache.UPDATEREADY) { | |
| 79 return false; | |
| 80 } | |
| 81 | |
| 82 print('App cache update ready, now swapping...'); | |
| 83 window.applicationCache.swapCache(); | |
| 84 print('App cache swapped, now reloading page...'); | |
| 85 window.location.reload(); | |
| 86 return true; | |
| 87 } | |
| 88 | |
| 89 /** Returns true if we are running as a packaged application. */ | |
| 90 static bool get isPackaged() { | |
| 91 return window.location.protocol == 'chrome-extension:'; | |
| 92 } | |
| 93 | |
| 94 /** | |
| 95 * Gets the server URL. This is needed when we are loaded from a packaged | |
| 96 * Chrome app. | |
| 97 */ | |
| 98 static String serverUrl(String url) { | |
| 99 if (isPackaged) { | |
| 100 // TODO(jmesserly): Several problems with this: | |
| 101 // * How do we authenticate against the server? | |
| 102 // * How do we talk to a server other than thump? | |
| 103 assert(url.startsWith('/')); | |
| 104 return 'http://thump.googleplex.com' + url; | |
| 105 } else { | |
| 106 return url; | |
| 107 } | |
| 108 } | |
| 109 } | |
| OLD | NEW |