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