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