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