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 more elegantly? |
| 6 // In general, it seems like we want a convenient way to take a Stream plus a |
| 7 // getter and convert this into an Observable. |
| 8 |
| 9 /// Helpers for exposing dart:html as observable data. |
| 10 library observe.html; |
| 11 |
| 12 import 'dart:html'; |
| 13 |
| 14 import 'observe.dart'; |
| 15 |
| 16 /// An observable version of [window.location.hash]. |
| 17 final ObservableLocationHash windowLocation = new ObservableLocationHash._(); |
| 18 |
| 19 class ObservableLocationHash extends ChangeNotifier { |
| 20 Object _currentHash; |
| 21 |
| 22 ObservableLocationHash._() { |
| 23 // listen on changes to #hash in the URL |
| 24 // Note: listen on both popState and hashChange, because IE9 doesn't support |
| 25 // history API. See http://dartbug.com/5483 |
| 26 // TODO(jmesserly): only listen to these if someone is listening to our |
| 27 // changes. |
| 28 window.onHashChange.listen(_notifyHashChange); |
| 29 window.onPopState.listen(_notifyHashChange); |
| 30 |
| 31 _currentHash = hash; |
| 32 } |
| 33 |
| 34 @reflectable String get hash => window.location.hash; |
| 35 |
| 36 /// Pushes a new URL state, similar to the affect of clicking a link. |
| 37 /// Has no effect if the [value] already equals [window.location.hash]. |
| 38 @reflectable void set hash(String value) { |
| 39 if (value == hash) return; |
| 40 |
| 41 window.history.pushState(null, '', value); |
| 42 _notifyHashChange(null); |
| 43 } |
| 44 |
| 45 void _notifyHashChange(_) { |
| 46 var oldValue = _currentHash; |
| 47 _currentHash = hash; |
| 48 notifyPropertyChange(#hash, oldValue, _currentHash); |
| 49 } |
| 50 } |
| 51 |
| 52 /// *Deprecated* use [CssClassSet.toggle] instead. |
| 53 /// |
| 54 /// Add or remove CSS class [className] based on the [value]. |
| 55 @deprecated |
| 56 void updateCssClass(Element element, String className, bool value) { |
| 57 if (value == true) { |
| 58 element.classes.add(className); |
| 59 } else { |
| 60 element.classes.remove(className); |
| 61 } |
| 62 } |
| 63 |
| 64 /// *Deprecated* use `class="{{ binding }}"` in your HTML instead. It will also |
| 65 /// work on a `<polymer-element>`. |
| 66 /// |
| 67 /// Bind a CSS class to the observable [object] and property [path]. |
| 68 @deprecated |
| 69 PathObserver bindCssClass(Element element, String className, |
| 70 Observable object, String path) { |
| 71 |
| 72 callback(value) { |
| 73 updateCssClass(element, className, value); |
| 74 } |
| 75 |
| 76 var obs = new PathObserver(object, path); |
| 77 callback(obs.open(callback)); |
| 78 return obs; |
| 79 } |
OLD | NEW |