OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <!-- | 2 <!-- |
3 Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 3 Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
4 for details. All rights reserved. Use of this source code is governed by a | 4 for details. All rights reserved. Use of this source code is governed by a |
5 BSD-style license that can be found in the LICENSE file. | 5 BSD-style license that can be found in the LICENSE file. |
6 --> | 6 --> |
7 <html> | 7 <html> |
8 <body> | 8 <body> |
9 <element name="x-router-options" extends="ul" apply-author-styles> | 9 <element name="x-router-options" extends="ul" apply-author-styles> |
10 <template><content></content></template> | 10 <template><content></content></template> |
11 <script type="application/dart"> | 11 <script type="application/dart"> |
12 import 'dart:html'; | 12 import 'dart:html'; |
13 import 'package:web_ui/web_ui.dart'; | 13 import 'package:web_ui/web_ui.dart'; |
| 14 import 'package:web_ui/observe/html.dart'; |
14 | 15 |
15 /** | 16 /** |
16 * Given a set of child links to this page, this will add the "selected" CSS | 17 * Given a set of child links to this page, this will add the "selected" CSS |
17 * class to the link that matches window.location.hash. | 18 * class to the link that matches window.location.hash. |
18 * | 19 * |
19 * For example, if the current window.location.hash is "#/completed" and we | 20 * For example, if the current window.location.hash is "#/completed" and we |
20 * have a tag like `<a href="#/completed">` it will get the class | 21 * have a tag like `<a href="#/completed">` it will get the class |
21 * `class="selected"`, and other links will have that CSS class removed. | 22 * `class="selected"`, and other links will have that CSS class removed. |
22 */ | 23 */ |
23 class RouterOptions extends WebComponent { | 24 class RouterOptions extends WebComponent { |
24 | 25 |
25 var _stopWatcher; | 26 var _stopWatcher; |
26 | 27 |
27 void inserted() { | 28 void inserted() { |
28 super.inserted(); | 29 super.inserted(); |
29 | 30 |
30 var anchors = this.queryAll('a'); | 31 var anchors = this.queryAll('a'); |
31 | 32 |
32 // TODO(jmesserly): this only works because main() registered for | 33 _stopWatcher = watchAndInvoke(() => locationHash, (e) { |
33 // popState and hashChanged. | |
34 _stopWatcher = watchAndInvoke(() => window.location.hash, (e) { | |
35 var hash = e.newValue; | 34 var hash = e.newValue; |
36 if (hash == '') hash = '#/'; | 35 if (hash == '') hash = '#/'; |
37 for (var a in anchors) { | 36 for (var a in anchors) { |
38 updateCssClass(a, a.hash == hash, 'selected'); | 37 updateCssClass(a, a.hash == hash, 'selected'); |
39 } | 38 } |
40 }); | 39 }); |
41 } | 40 } |
42 | 41 |
43 void removed() { | 42 void removed() { |
44 _stopWatcher(); | 43 _stopWatcher(); |
45 super.removed(); | 44 super.removed(); |
46 } | 45 } |
47 } | 46 } |
48 </script> | 47 </script> |
49 </element> | 48 </element> |
50 </body> | 49 </body> |
51 </html> | 50 </html> |
OLD | NEW |