| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library trydart.cache; | 5 library trydart.cache; |
| 6 | 6 |
| 7 import 'dart:async' show | 7 import 'dart:async' show |
| 8 Timer; | 8 Timer; |
| 9 | 9 |
| 10 import 'dart:html' show | 10 import 'dart:html' show |
| 11 AnchorElement, | 11 AnchorElement, |
| 12 ApplicationCache, | 12 ApplicationCache, |
| 13 Event, | 13 Event, |
| 14 MeterElement, | 14 MeterElement, |
| 15 ProgressEvent, | 15 ProgressEvent, |
| 16 window; | 16 window; |
| 17 | 17 |
| 18 import 'ui.dart' show | 18 import 'ui.dart' show |
| 19 cacheStatusElement; | 19 cacheStatusElement; |
| 20 | 20 |
| 21 /// Called when the window has finished loading. | 21 /// Called when the window has finished loading. |
| 22 void onLoad(Event event) { | 22 void onLoad(Event event) { |
| 23 window.applicationCache.onUpdateReady.listen((_) => updateCacheStatus()); | 23 if (!ApplicationCache.supported) return; |
| 24 window.applicationCache.onCached.listen((_) => updateCacheStatus()); | 24 window.applicationCache.onUpdateReady.listen(updateCacheStatus); |
| 25 window.applicationCache.onChecking.listen((_) => updateCacheStatus()); | 25 window.applicationCache.onCached.listen(updateCacheStatus); |
| 26 window.applicationCache.onDownloading.listen((_) => updateCacheStatus()); | 26 window.applicationCache.onChecking.listen(updateCacheStatus); |
| 27 window.applicationCache.onError.listen((_) => updateCacheStatus()); | 27 window.applicationCache.onDownloading.listen(updateCacheStatus); |
| 28 window.applicationCache.onNoUpdate.listen((_) => updateCacheStatus()); | 28 window.applicationCache.onError.listen(updateCacheStatus); |
| 29 window.applicationCache.onObsolete.listen((_) => updateCacheStatus()); | 29 window.applicationCache.onNoUpdate.listen(updateCacheStatus); |
| 30 window.applicationCache.onObsolete.listen(updateCacheStatus); |
| 30 window.applicationCache.onProgress.listen(onCacheProgress); | 31 window.applicationCache.onProgress.listen(onCacheProgress); |
| 31 } | 32 } |
| 32 | 33 |
| 33 onCacheProgress(ProgressEvent event) { | 34 void onCacheProgress(Event event) { |
| 34 if (!event.lengthComputable) { | 35 if (event is ProgressEvent) { |
| 35 updateCacheStatus(); | 36 // Firefox doesn't fire a ProgressEvent on cache progress. Just a plain |
| 36 return; | 37 // Event with type == "progress". |
| 38 if (event.lengthComputable) { |
| 39 updateCacheStatusFromEvent(event); |
| 40 return; |
| 41 } |
| 37 } | 42 } |
| 43 updateCacheStatus(null); |
| 44 } |
| 45 |
| 46 void updateCacheStatusFromEvent(ProgressEvent event) { |
| 38 cacheStatusElement.nodes.clear(); | 47 cacheStatusElement.nodes.clear(); |
| 39 cacheStatusElement.appendText('Downloading SDK '); | 48 cacheStatusElement.appendText('Downloading SDK '); |
| 40 var progress = '${event.loaded} of ${event.total}'; | 49 var progress = '${event.loaded} of ${event.total}'; |
| 41 if (MeterElement.supported) { | 50 if (MeterElement.supported) { |
| 42 cacheStatusElement.append( | 51 cacheStatusElement.append( |
| 43 new MeterElement() | 52 new MeterElement() |
| 44 ..appendText(progress) | 53 ..appendText(progress) |
| 45 ..min = 0 | 54 ..min = 0 |
| 46 ..max = event.total | 55 ..max = event.total |
| 47 ..value = event.loaded); | 56 ..value = event.loaded); |
| 48 } else { | 57 } else { |
| 49 cacheStatusElement.appendText(progress); | 58 cacheStatusElement.appendText(progress); |
| 50 } | 59 } |
| 51 } | 60 } |
| 52 | 61 |
| 53 String cacheStatus() { | 62 String cacheStatus() { |
| 54 if (!ApplicationCache.supported) return 'offline not supported'; | 63 if (!ApplicationCache.supported) return 'offline not supported'; |
| 55 int status = window.applicationCache.status; | 64 int status = window.applicationCache.status; |
| 56 if (status == ApplicationCache.CHECKING) return 'Checking for updates'; | 65 if (status == ApplicationCache.CHECKING) return 'Checking for updates'; |
| 57 if (status == ApplicationCache.DOWNLOADING) return 'Downloading SDK'; | 66 if (status == ApplicationCache.DOWNLOADING) return 'Downloading SDK'; |
| 58 if (status == ApplicationCache.IDLE) return 'Try Dart! works offline'; | 67 if (status == ApplicationCache.IDLE) return 'Try Dart! works offline'; |
| 59 if (status == ApplicationCache.OBSOLETE) return 'OBSOLETE'; | 68 if (status == ApplicationCache.OBSOLETE) return 'OBSOLETE'; |
| 60 if (status == ApplicationCache.UNCACHED) return 'offline not available'; | 69 if (status == ApplicationCache.UNCACHED) return 'offline not available'; |
| 61 if (status == ApplicationCache.UPDATEREADY) return 'SDK downloaded'; | 70 if (status == ApplicationCache.UPDATEREADY) return 'SDK downloaded'; |
| 62 return '?'; | 71 return '?'; |
| 63 } | 72 } |
| 64 | 73 |
| 65 void updateCacheStatus() { | 74 void updateCacheStatus(_) { |
| 66 cacheStatusElement.nodes.clear(); | 75 cacheStatusElement.nodes.clear(); |
| 67 int status = window.applicationCache.status; | 76 int status = window.applicationCache.status; |
| 68 if (status == ApplicationCache.UPDATEREADY) { | 77 if (status == ApplicationCache.UPDATEREADY) { |
| 69 cacheStatusElement.appendText('New version of Try Dart! ready: '); | 78 cacheStatusElement.appendText('New version of Try Dart! ready: '); |
| 70 cacheStatusElement.append( | 79 cacheStatusElement.append( |
| 71 new AnchorElement(href: '#') | 80 new AnchorElement(href: '#') |
| 72 ..appendText('Load') | 81 ..appendText('Load') |
| 73 ..onClick.listen((event) { | 82 ..onClick.listen((event) { |
| 74 event.preventDefault(); | 83 event.preventDefault(); |
| 75 window.applicationCache.swapCache(); | 84 window.applicationCache.swapCache(); |
| 76 window.location.reload(); | 85 window.location.reload(); |
| 77 })); | 86 })); |
| 78 } else if (status == ApplicationCache.IDLE) { | 87 } else if (status == ApplicationCache.IDLE) { |
| 79 cacheStatusElement.appendText(cacheStatus()); | 88 cacheStatusElement.appendText(cacheStatus()); |
| 80 cacheStatusElement.classes.add('offlineyay'); | 89 cacheStatusElement.classes.add('offlineyay'); |
| 81 new Timer(const Duration(seconds: 10), () { | 90 new Timer(const Duration(seconds: 10), () { |
| 82 cacheStatusElement.style.display = 'none'; | 91 cacheStatusElement.style.display = 'none'; |
| 83 }); | 92 }); |
| 84 } else { | 93 } else { |
| 85 cacheStatusElement.appendText(cacheStatus()); | 94 cacheStatusElement.appendText(cacheStatus()); |
| 86 } | 95 } |
| 87 } | 96 } |
| OLD | NEW |