|
| 1 | +// Copyright (c) 2016, 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 | +import 'package:angular/angular.dart'; |
| 6 | +import 'package:angular/meta.dart'; |
| 7 | + |
| 8 | +import 'material_icon.dart'; |
| 9 | + |
| 10 | +/// Adds a second icon to [MaterialIconComponent] that can be toggled on and |
| 11 | +/// off. |
| 12 | +/// |
| 13 | +/// See [https://www.google.com/design/icons/](https://goo.gl/YKrYlu) for |
| 14 | +/// available icons. If the icon name contains spaces, replace them with |
| 15 | +/// underscores. |
| 16 | +/// |
| 17 | +/// The following css selectors can be used to target the icons for stylign in |
| 18 | +/// various states: |
| 19 | +/// - `.basic-icon` -- Standard icon displayed when _not_ in the toggled state. |
| 20 | +/// - `.toggled-icon` -- Toggle icon displayed when in the toggled state. |
| 21 | +/// - `.hide-icon` -- No icon when in a toggled state that doesn't have an icon. |
| 22 | +@Directive(selector: 'material-icon[toggle]') |
| 23 | +class MaterialIconToggleDirective implements AfterChanges { |
| 24 | + MaterialIconComponent iconRef; |
| 25 | + final ChangeDetectorRef _cdRef; |
| 26 | + bool _showToggled; |
| 27 | + bool _showBasic; |
| 28 | + bool _hideIcon; |
| 29 | + |
| 30 | + MaterialIconToggleDirective(this._cdRef, this.iconRef); |
| 31 | + |
| 32 | + /// The `Icon` model (third_party.dart_src.acx.model.ui.Icon) or icon |
| 33 | + /// identifier (String) to display. |
| 34 | + /// |
| 35 | + /// Defaults to a checkmark icon. |
| 36 | + @Input() |
| 37 | + dynamic toggledIcon = 'check'; |
| 38 | + |
| 39 | + /// The `Icon` model (third_party.dart_src.acx.model.ui.Icon) or icon |
| 40 | + /// identifier (String) to display. |
| 41 | + /// |
| 42 | + /// If not provided this toggle icon will not display an icon in the standard |
| 43 | + /// state. |
| 44 | + @Input() |
| 45 | + dynamic icon; |
| 46 | + |
| 47 | + /// The toggled state of this icon. |
| 48 | + /// |
| 49 | + /// The [toggledIcon] is displayed when true. |
| 50 | + @Input() |
| 51 | + bool toggle; |
| 52 | + |
| 53 | + @override |
| 54 | + ngAfterChanges() { |
| 55 | + iconRef.icon = toggle ? toggledIcon : icon; |
| 56 | + _showToggled = toggle && _hasValue(toggledIcon); |
| 57 | + _showBasic = !toggle && _hasValue(icon); |
| 58 | + _hideIcon = |
| 59 | + (toggle && !_hasValue(toggledIcon)) || (!toggle && !_hasValue(icon)); |
| 60 | + // Ensure the icon displayed by the underlying material-icon is updated. |
| 61 | + _cdRef.markForCheck(); |
| 62 | + } |
| 63 | + |
| 64 | + @HostBinding('class.basic-icon.if') |
| 65 | + @visibleForTemplate |
| 66 | + bool get showBasic => _showBasic; |
| 67 | + |
| 68 | + @HostBinding('class.toggled-icon.if') |
| 69 | + @visibleForTemplate |
| 70 | + bool get showToggled => _showToggled; |
| 71 | + |
| 72 | + @HostBinding('class.hide-icon.if') |
| 73 | + @visibleForTemplate |
| 74 | + bool get hideIcon => _hideIcon; |
| 75 | + |
| 76 | + bool _hasValue(dynamic iconValue) => !(iconValue == null || iconValue == ''); |
| 77 | +} |
0 commit comments