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