OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, 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 |
| 5 // TODO(jmesserly): can we handle this better? This seems like an unfortunate |
| 6 // limitation of our read barriers. |
| 7 |
| 8 /** Helpers for exposing dart:html as observable data. */ |
| 9 library web_ui.observe.html; |
| 10 |
| 11 import 'dart:html'; |
| 12 import 'package:web_ui/observe.dart'; |
| 13 |
| 14 ObservableReference<String> _hash; |
| 15 |
| 16 /** An observable version of [window.location.hash]. */ |
| 17 String get locationHash { |
| 18 if (_hash == null) { |
| 19 _hash = new ObservableReference(window.location.hash); |
| 20 |
| 21 window.onHashChange.listen(_updateLocationHash); |
| 22 window.onPopState.listen(_updateLocationHash); |
| 23 } |
| 24 |
| 25 return _hash.value; |
| 26 } |
| 27 |
| 28 /** |
| 29 * Pushes a new URL state, similar to the affect of clicking a link. |
| 30 * Has no effect if the [value] already equals [window.location.hash]. |
| 31 */ |
| 32 set locationHash(String value) { |
| 33 if (value == window.location.hash) return; |
| 34 |
| 35 window.history.pushState(const {}, '', value); |
| 36 _updateLocationHash(null); |
| 37 } |
| 38 |
| 39 |
| 40 // listen on changes to #hash in the URL |
| 41 // Note: listen on both popState and hashChange, because IE9 doesnh't support |
| 42 // history API, and it doesn't work properly on Opera 12. |
| 43 // See http://dartbug.com/5483 |
| 44 _updateLocationHash(_) { |
| 45 if (_hash != null) { |
| 46 _hash.value = window.location.hash; |
| 47 } |
| 48 } |
OLD | NEW |