| 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 * A simple news reader in Dart. | |
| 7 */ | |
| 8 class Swarm extends App { | |
| 9 /** | |
| 10 * Flag to insure the onLoad isn't called when callback from initializeFromUrl | |
| 11 * could occur before the document's onload event. | |
| 12 */ | |
| 13 bool onLoadFired; | |
| 14 | |
| 15 /** Collections of datafeeds to show per page. */ | |
| 16 Sections sections; | |
| 17 | |
| 18 /** The front page of the app. */ | |
| 19 FrontView frontView; | |
| 20 | |
| 21 /** Observable UI state. */ | |
| 22 SwarmState state; | |
| 23 | |
| 24 Swarm() : super(), onLoadFired = false { | |
| 25 Sections.initializeFromUrl((currSections) { | |
| 26 sections = currSections; | |
| 27 state = new SwarmState(sections); | |
| 28 setupApp(); | |
| 29 }); | |
| 30 // Catch user keypresses and decide whether to use them for the | |
| 31 // Streams app or pass them on to the browser. | |
| 32 document.on.keyUp.add((e) { | |
| 33 if (frontView != null) { | |
| 34 frontView.processKeyEvent(e); | |
| 35 } | |
| 36 }); | |
| 37 } | |
| 38 | |
| 39 /** | |
| 40 * Tells each data source to check the server for the latest data. | |
| 41 */ | |
| 42 void refresh() { | |
| 43 sections.refresh(); | |
| 44 | |
| 45 // Hook up listeners about any data source additions or deletions. We don't | |
| 46 // differeniate additions or deletions just the fact that data feeds have | |
| 47 // changed. We might want more fidelity later. | |
| 48 sections.sectionTitles.forEach((title) { | |
| 49 Section section = sections.findSection(title); | |
| 50 // TODO(terry): addChangeListener needs to return an id so previous | |
| 51 // listener can be removed, otherwise anonymous functions | |
| 52 // can't easily be used. See b/5063673 | |
| 53 section.feeds.addChangeListener((data) { | |
| 54 // TODO(jacobr): implement this. | |
| 55 print("Refresh sections not impl yet."); | |
| 56 }); | |
| 57 }); | |
| 58 } | |
| 59 | |
| 60 /** The page load event handler. */ | |
| 61 void onLoad() { | |
| 62 onLoadFired = true; | |
| 63 super.onLoad(); | |
| 64 setupApp(); | |
| 65 } | |
| 66 | |
| 67 /** | |
| 68 * Setup the application's world. | |
| 69 */ | |
| 70 void setupApp() { | |
| 71 // TODO(terry): Should be able to spinup the app w/o waiting for data. | |
| 72 // If the document is already loaded so we can setup the app anytime. | |
| 73 // Otherwise, we'll wait to setup the world until the document is ready | |
| 74 // to render. | |
| 75 if (onLoadFired && state != null) { | |
| 76 render(); | |
| 77 // This call loads the initial data. | |
| 78 refresh(); | |
| 79 eraseSplashScreen(); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 void render() { | |
| 84 frontView = new FrontView(this); | |
| 85 frontView.addToDocument(document.body); | |
| 86 } | |
| 87 } | |
| OLD | NEW |