Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(130)

Side by Side Diff: samples/third_party/pop-pop-win/web/platform_web.dart

Issue 200723008: samples/poppopwin: upgrade to first-class sample (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fixed pkgbuild.status for rename Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 library ppw_platform_web;
2
3 import 'dart:async';
4 import 'dart:html';
5 import 'dart:js' as js;
6 import 'package:poppopwin/platform_target.dart';
7
8 class PlatformWeb extends PlatformTarget {
9 static const String _BIG_HASH = '#big';
10 static const String _ABOUT_HASH = '#about';
11
12 final StreamController _aboutController = new StreamController(sync: true);
13
14 PlatformWeb() : super.base() {
15 window.onPopState.listen((args) => _processUrlHash());
16 }
17
18 @override
19 Future clearValues() {
20 window.localStorage.clear();
21 return new Future.value();
22 }
23
24 @override
25 Future setValue(String key, String value) {
26 window.localStorage[key] = value;
27 return new Future.value();
28 }
29
30 @override
31 Future<String> getValue(String key) =>
32 new Future.value(window.localStorage[key]);
33
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;
51
52 bool get showAbout => _urlHash == _ABOUT_HASH;
53
54 Stream get aboutChanged => _aboutController.stream;
55
56 void toggleAbout([bool value]) {
57 final Location loc = window.location;
58 // ensure we treat empty hash like '#', which makes comparison easy later
59 final hash = loc.hash.length == 0 ? '#' : loc.hash;
60
61 final isOpen = hash == _ABOUT_HASH;
62 if(value == null) {
63 // then toggle the current value
64 value = !isOpen;
65 }
66
67 var targetHash = value ? _ABOUT_HASH : '#';
68 if(targetHash != hash) {
69 loc.assign(targetHash);
70 }
71 _aboutController.add(null);
72 }
73
74 String get _urlHash => window.location.hash;
75
76 void _processUrlHash() {
77 final Location loc = window.location;
78 final hash = loc.hash;
79 final href = loc.href;
80
81 final History history = window.history;
82 switch(hash) {
83 case "#reset":
84 assert(href.endsWith(hash));
85 var newLoc = href.substring(0, href.length - hash.length);
86
87 window.localStorage.clear();
88
89 loc.replace(newLoc);
90 break;
91 case _BIG_HASH:
92 loc.reload();
93 break;
94 case _ABOUT_HASH:
95 _aboutController.add(null);
96 break;
97 }
98 }
99 }
OLDNEW
« no previous file with comments | « samples/third_party/pop-pop-win/web/index.html ('k') | samples/third_party/pop-pop-win/web/style.css » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698