| 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 /** The top-level collection of all sections for a user. */ | 5 /** The top-level collection of all sections for a user. */ |
| 6 // TODO(jimhug): This is known as UserData in the server model. | 6 // TODO(jimhug): This is known as UserData in the server model. |
| 7 class Sections implements Collection<Section> { | 7 class Sections implements Collection<Section> { |
| 8 final List<Section> _sections; | 8 final List<Section> _sections; |
| 9 | 9 |
| 10 Sections(this._sections); | 10 Sections(this._sections); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 * Find the Section object that has a given title. | 24 * Find the Section object that has a given title. |
| 25 * This is used to integrate well with [ConveyorView]. | 25 * This is used to integrate well with [ConveyorView]. |
| 26 */ | 26 */ |
| 27 Section findSection(String name) { | 27 Section findSection(String name) { |
| 28 return CollectionUtils.find(_sections, (sect) => sect.title == name); | 28 return CollectionUtils.find(_sections, (sect) => sect.title == name); |
| 29 } | 29 } |
| 30 | 30 |
| 31 // TODO(jimhug): Track down callers! | 31 // TODO(jimhug): Track down callers! |
| 32 Iterator<Section> iterator() => _sections.iterator(); | 32 Iterator<Section> iterator() => _sections.iterator(); |
| 33 | 33 |
| 34 // TODO(jimhug): Better support for switching between local dev and server. | |
| 35 static bool get runningFromFile() { | |
| 36 return window.location.protocol.startsWith('file:'); | |
| 37 } | |
| 38 | |
| 39 static String get home() { | 34 static String get home() { |
| 40 // TODO(jmesserly): window.location.origin not available on Safari 4. | 35 // TODO(jmesserly): window.location.origin not available on Safari 4. |
| 41 // Move this workaround to the DOM code. See bug 5389503. | 36 // Move this workaround to the DOM code. See bug 5389503. |
| 42 return '${window.location.protocol}//${window.location.host}'; | 37 return '${window.location.protocol}//${window.location.host}'; |
| 43 } | 38 } |
| 44 | 39 |
| 45 // This method is exposed for tests. | 40 // This method is exposed for tests. |
| 46 static void initializeFromData(String data, void callback(Sections sects)) { | 41 static void initializeFromData(String data, void callback(Sections sects)) { |
| 47 final decoder = new Decoder(data); | 42 final decoder = new Decoder(data); |
| 48 int nSections = decoder.readInt(); | 43 int nSections = decoder.readInt(); |
| 49 final sections = new List<Section>(); | 44 final sections = new List<Section>(); |
| 50 | 45 |
| 51 for (int i=0; i < nSections; i++) { | 46 for (int i=0; i < nSections; i++) { |
| 52 sections.add(Section.decode(decoder)); | 47 sections.add(Section.decode(decoder)); |
| 53 } | 48 } |
| 54 callback(new Sections(sections)); | 49 callback(new Sections(sections)); |
| 55 } | 50 } |
| 56 | 51 |
| 57 static void initializeFromUrl(void callback(Sections sections)) { | 52 static void initializeFromUrl(void callback(Sections sections)) { |
| 58 if (Sections.runningFromFile) { | 53 // TODO(jmesserly): display an error if we fail here! Silent failure bad. |
| 59 initializeFromData(CannedData.data['user.data'], callback); | 54 new XMLHttpRequest.get('data/user.data', |
| 60 } else { | 55 EventBatch.wrap((request) { |
| 61 // TODO(jmesserly): display an error if we fail here! Silent failure bad. | 56 // TODO(jimhug): Nice response if get error back from server. |
| 62 new XMLHttpRequest.get('data/user.data', | 57 // TODO(jimhug): Might be more efficient to parse request in sections. |
| 63 EventBatch.wrap((request) { | 58 initializeFromData(request.responseText, callback); |
| 64 // TODO(jimhug): Nice response if get error back from server. | 59 })); |
| 65 // TODO(jimhug): Might be more efficient to parse request in sections. | |
| 66 initializeFromData(request.responseText, callback); | |
| 67 })); | |
| 68 } | |
| 69 } | 60 } |
| 70 | 61 |
| 71 Section findSectionById(String id) { | 62 Section findSectionById(String id) { |
| 72 return CollectionUtils.find(_sections, (section) => section.id == id); | 63 return CollectionUtils.find(_sections, (section) => section.id == id); |
| 73 } | 64 } |
| 74 | 65 |
| 75 /** | 66 /** |
| 76 * Given the name of a section, find its index in the set. | 67 * Given the name of a section, find its index in the set. |
| 77 */ | 68 */ |
| 78 int findSectionIndex(String name) { | 69 int findSectionIndex(String name) { |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 } | 186 } |
| 196 | 187 |
| 197 String get dataUri() { | 188 String get dataUri() { |
| 198 return Uri.encodeComponent(id).replaceAll('%2F', '/'). | 189 return Uri.encodeComponent(id).replaceAll('%2F', '/'). |
| 199 replaceAll('%253A', '%3A'); | 190 replaceAll('%253A', '%3A'); |
| 200 } | 191 } |
| 201 | 192 |
| 202 String get thumbUrl() { | 193 String get thumbUrl() { |
| 203 if (!hasThumbnail) return null; | 194 if (!hasThumbnail) return null; |
| 204 | 195 |
| 205 var home; | 196 var home = Sections.home; |
| 206 if (Sections.runningFromFile) { | |
| 207 home = 'http://dart.googleplex.com'; | |
| 208 } else { | |
| 209 home = Sections.home; | |
| 210 } | |
| 211 // By default images from the real server are cached. | 197 // By default images from the real server are cached. |
| 212 // Bump the version flag if you change the thumbnail size, and you want to | 198 // Bump the version flag if you change the thumbnail size, and you want to |
| 213 // get the new images. Our server ignores the query params but it gets | 199 // get the new images. Our server ignores the query params but it gets |
| 214 // around appengine server side caching and the client side cache. | 200 // around appengine server side caching and the client side cache. |
| 215 return 'data/$dataUri.jpg'; | 201 return 'data/$dataUri.jpg'; |
| 216 } | 202 } |
| 217 | 203 |
| 218 // TODO(jimhug): need to return a lazy Observable<String> and also | 204 // TODO(jimhug): need to return a lazy Observable<String> and also |
| 219 // add support for preloading. | 205 // add support for preloading. |
| 220 void _ensureLoaded() { | 206 void _ensureLoaded() { |
| 221 if (_htmlBody !== null) return; | 207 if (_htmlBody !== null) return; |
| 222 | 208 |
| 223 var name = '$dataUri.html'; | 209 var name = '$dataUri.html'; |
| 224 if (Sections.runningFromFile) { | 210 // TODO(jimhug): Remove this truly evil synchronous xhr. |
| 225 _htmlBody = CannedData.data[name]; | 211 final req = new XMLHttpRequest(); |
| 226 } else { | 212 req.open('GET', 'data/$name', false); |
| 227 // TODO(jimhug): Remove this truly evil synchronoush xhr. | 213 req.send(); |
| 228 final req = new XMLHttpRequest(); | 214 _htmlBody = req.responseText; |
| 229 req.open('GET', 'data/$name', false); | |
| 230 req.send(); | |
| 231 _htmlBody = req.responseText; | |
| 232 } | |
| 233 } | 215 } |
| 234 | 216 |
| 235 static Article decodeHeader(Feed source, Decoder decoder) { | 217 static Article decodeHeader(Feed source, Decoder decoder) { |
| 236 final id = decoder.readString(); | 218 final id = decoder.readString(); |
| 237 final title = decoder.readString(); | 219 final title = decoder.readString(); |
| 238 final srcUrl = decoder.readString(); | 220 final srcUrl = decoder.readString(); |
| 239 final hasThumbnail = decoder.readBool(); | 221 final hasThumbnail = decoder.readBool(); |
| 240 final author = decoder.readString(); | 222 final author = decoder.readString(); |
| 241 final dateInSeconds = decoder.readInt(); | 223 final dateInSeconds = decoder.readInt(); |
| 242 final snippet = decoder.readString(); | 224 final snippet = decoder.readString(); |
| 243 final date = | 225 final date = |
| 244 new Date.fromMillisecondsSinceEpoch(dateInSeconds*1000, isUtc: true); | 226 new Date.fromMillisecondsSinceEpoch(dateInSeconds*1000, isUtc: true); |
| 245 return new Article(source, id, date, title, author, srcUrl, hasThumbnail, | 227 return new Article(source, id, date, title, author, srcUrl, hasThumbnail, |
| 246 snippet); | 228 snippet); |
| 247 } | 229 } |
| 248 } | 230 } |
| OLD | NEW |