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