| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014, 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 library pop_pop_win.platform_web; | |
| 5 | |
| 6 import 'dart:async'; | |
| 7 import 'dart:html'; | |
| 8 import 'package:pop_pop_win/platform_target.dart'; | |
| 9 | |
| 10 class PlatformWeb extends PlatformTarget { | |
| 11 static const String _ABOUT_HASH = '#about'; | |
| 12 bool _sizeAccessed = false; | |
| 13 | |
| 14 final StreamController _aboutController = new StreamController(sync: true); | |
| 15 | |
| 16 PlatformWeb() : super.base() { | |
| 17 window.onPopState.listen((args) => _processUrlHash()); | |
| 18 } | |
| 19 | |
| 20 @override | |
| 21 Future clearValues() { | |
| 22 window.localStorage.clear(); | |
| 23 return new Future.value(); | |
| 24 } | |
| 25 | |
| 26 @override | |
| 27 Future setValue(String key, String value) { | |
| 28 window.localStorage[key] = value; | |
| 29 return new Future.value(); | |
| 30 } | |
| 31 | |
| 32 @override | |
| 33 Future<String> getValue(String key) => | |
| 34 new Future.value(window.localStorage[key]); | |
| 35 | |
| 36 int get size { | |
| 37 _sizeAccessed = true; | |
| 38 var hash = (_urlHash == null) ? '7' : _urlHash; | |
| 39 hash = hash.replaceAll('#', ''); | |
| 40 return int.parse(hash, onError: (e) => 7); | |
| 41 } | |
| 42 | |
| 43 bool get showAbout => _urlHash == _ABOUT_HASH; | |
| 44 | |
| 45 Stream get aboutChanged => _aboutController.stream; | |
| 46 | |
| 47 void toggleAbout([bool value]) { | |
| 48 var loc = window.location; | |
| 49 // ensure we treat empty hash like '#', which makes comparison easy later | |
| 50 var hash = loc.hash.length == 0 ? '#' : loc.hash; | |
| 51 | |
| 52 var isOpen = hash == _ABOUT_HASH; | |
| 53 if (value == null) { | |
| 54 // then toggle the current value | |
| 55 value = !isOpen; | |
| 56 } | |
| 57 | |
| 58 var targetHash = value ? _ABOUT_HASH : '#'; | |
| 59 if (targetHash != hash) { | |
| 60 loc.assign(targetHash); | |
| 61 } | |
| 62 _aboutController.add(null); | |
| 63 } | |
| 64 | |
| 65 String get _urlHash => window.location.hash; | |
| 66 | |
| 67 void _processUrlHash() { | |
| 68 var loc = window.location; | |
| 69 var hash = loc.hash; | |
| 70 var href = loc.href; | |
| 71 | |
| 72 switch (hash) { | |
| 73 case "#reset": | |
| 74 assert(href.endsWith(hash)); | |
| 75 var newLoc = href.substring(0, href.length - hash.length); | |
| 76 | |
| 77 window.localStorage.clear(); | |
| 78 | |
| 79 loc.replace(newLoc); | |
| 80 break; | |
| 81 case _ABOUT_HASH: | |
| 82 _aboutController.add(null); | |
| 83 break; | |
| 84 default: | |
| 85 if (hash.isNotEmpty && _sizeAccessed) { | |
| 86 loc.reload(); | |
| 87 } | |
| 88 break; | |
| 89 } | |
| 90 } | |
| 91 } | |
| OLD | NEW |