|
| 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 'dart:async'; |
| 6 | + |
| 7 | +import 'package:angular/angular.dart'; |
| 8 | +import 'package:angular_components/laminate/portal/portal.dart'; |
| 9 | +import 'package:angular_components/model/action/async_action.dart'; |
| 10 | +import 'package:angular_components/utils/angular/properties/properties.dart'; |
| 11 | + |
| 12 | +/// A step within the stepper. |
| 13 | +/// |
| 14 | +/// __Example usage:__ |
| 15 | +/// |
| 16 | +/// <material-stepper> |
| 17 | +/// <template step name="Step A"> |
| 18 | +/// <div> |
| 19 | +/// [...] |
| 20 | +/// </div> |
| 21 | +/// </template> |
| 22 | +/// <template step name="Step B"> |
| 23 | +/// <div> |
| 24 | +/// [...] |
| 25 | +/// </div> |
| 26 | +/// </template> |
| 27 | +/// <material-stepper> |
| 28 | +/// |
| 29 | +@Directive( |
| 30 | + selector: '[step]', |
| 31 | + exportAs: 'step', |
| 32 | +) |
| 33 | +class StepDirective extends TemplatePortal { |
| 34 | + final _continueController = |
| 35 | + new StreamController<AsyncAction<bool>>.broadcast(sync: true); |
| 36 | + |
| 37 | + final _cancelController = |
| 38 | + new StreamController<AsyncAction<bool>>.broadcast(sync: true); |
| 39 | + |
| 40 | + final _jumpController = |
| 41 | + new StreamController<AsyncAction<bool>>.broadcast(sync: true); |
| 42 | + |
| 43 | + /// Name shown as the title. |
| 44 | + @Input() |
| 45 | + String name; |
| 46 | + |
| 47 | + bool _optional = false; |
| 48 | + |
| 49 | + /// Whether the step is optional. Optional steps have an extra label denoting |
| 50 | + /// that they're optional and should be skip-able. Default is false. |
| 51 | + bool get optional => _optional; |
| 52 | + @Input() |
| 53 | + set optional(value) { |
| 54 | + _optional = getBool(value); |
| 55 | + } |
| 56 | + |
| 57 | + /// Summary text shown when the step is completed in a vertical default-sized |
| 58 | + /// stepper. For other steppers, this doesn't apply. |
| 59 | + @Input() |
| 60 | + String completeSummary; |
| 61 | + |
| 62 | + bool _hideButtons = false; |
| 63 | + |
| 64 | + /// Whether the buttons should be hidden on this step. |
| 65 | + bool get hideButtons => _hideButtons; |
| 66 | + @Input() |
| 67 | + set hideButtons(value) { |
| 68 | + _hideButtons = getBool(value); |
| 69 | + } |
| 70 | + |
| 71 | + bool _cancelHidden = false; |
| 72 | + |
| 73 | + /// Whether the cancel button should be hidden on this step. |
| 74 | + bool get cancelHidden => _cancelHidden; |
| 75 | + @Input() |
| 76 | + set cancelHidden(value) { |
| 77 | + _cancelHidden = getBool(value); |
| 78 | + } |
| 79 | + |
| 80 | + bool _complete = false; |
| 81 | + |
| 82 | + /// Whether the step is completed. |
| 83 | + /// |
| 84 | + /// This is set when the stepper goes to the next step. |
| 85 | + bool get complete => _complete; |
| 86 | + @Input() |
| 87 | + set complete(value) { |
| 88 | + _complete = getBool(value); |
| 89 | + } |
| 90 | + |
| 91 | + bool _canContinue = true; |
| 92 | + |
| 93 | + /// Whether the step can continue. |
| 94 | + /// |
| 95 | + /// This can be used to prevent continuing on from a step until all parts of |
| 96 | + /// the current step meet validation requirements. |
| 97 | + bool get canContinue => _canContinue; |
| 98 | + @Input() |
| 99 | + set canContinue(value) { |
| 100 | + _canContinue = getBool(value); |
| 101 | + } |
| 102 | + |
| 103 | + /// The selection state of the step. |
| 104 | + bool _active = false; |
| 105 | + bool busy = false; |
| 106 | + bool isLast = false; |
| 107 | + bool isSelectable = false; |
| 108 | + int index; |
| 109 | + |
| 110 | + /// Optional summary directive associated with this step. |
| 111 | + SummaryDirective summaryDirective; |
| 112 | + |
| 113 | + StepDirective(TemplateRef ref, ViewContainerRef viewContainerRef) |
| 114 | + : super(ref, viewContainerRef); |
| 115 | + |
| 116 | + set active(bool value) { |
| 117 | + _active = value; |
| 118 | + } |
| 119 | + |
| 120 | + bool get isOptional => optional; |
| 121 | + |
| 122 | + bool get active => _active; |
| 123 | + |
| 124 | + bool get isFirst => index == 0; |
| 125 | + |
| 126 | + bool get isNotSelectable => !isSelectable; |
| 127 | + |
| 128 | + /// Called when the Continue button is clicked. If the event handler calls |
| 129 | + /// $event.cancel(), the step won't be continued. |
| 130 | + @Output('continue') |
| 131 | + Stream<AsyncAction<bool>> get continueStream => _continueController.stream; |
| 132 | + |
| 133 | + /// Called when the Cancel button is clicked. If the event handler calls |
| 134 | + /// $event.cancel(), the step won't be cancelled. |
| 135 | + @Output('cancel') |
| 136 | + Stream<AsyncAction<bool>> get cancelStream => _cancelController.stream; |
| 137 | + |
| 138 | + @Output('jumpHere') |
| 139 | + Stream<AsyncAction<bool>> get jumpStream => _jumpController.stream; |
| 140 | + |
| 141 | + void requestStepContinue(AsyncAction<bool> action) { |
| 142 | + _requestStepAction(action, _continueController); |
| 143 | + } |
| 144 | + |
| 145 | + void requestStepCancel(AsyncAction<bool> action) { |
| 146 | + _requestStepAction(action, _cancelController); |
| 147 | + } |
| 148 | + |
| 149 | + void requestStepJump(AsyncAction<bool> action) { |
| 150 | + _requestStepAction(action, _jumpController); |
| 151 | + } |
| 152 | + |
| 153 | + bool get shouldShowSummary => !active && completeSummary != null && complete; |
| 154 | + |
| 155 | + void _requestStepAction(AsyncAction<bool> action, |
| 156 | + StreamController<AsyncAction<bool>> controller) { |
| 157 | + busy = true; |
| 158 | + controller.add(action); |
| 159 | + action.onDone.then((_) { |
| 160 | + busy = false; |
| 161 | + }); |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +/// A [TemplatePortal] which is used to house an optional summary view for a |
| 166 | +/// [StepDirective]. |
| 167 | +/// |
| 168 | +/// Note: Only works with vertical orrientation. |
| 169 | +/// |
| 170 | +/// __Example usage:__ |
| 171 | +/// |
| 172 | +/// <material-stepper orientation="vertical"> |
| 173 | +/// <template step name="A" #stepA="step">Step</template> |
| 174 | +/// <template [summary]="stepA"><h3>Summary A</h3></template> |
| 175 | +/// </material-stepper> |
| 176 | +/// |
| 177 | +@Directive( |
| 178 | + selector: '[summary]', |
| 179 | +) |
| 180 | +class SummaryDirective extends TemplatePortal { |
| 181 | + /// The [StepDirective] associated with this summary. |
| 182 | + @Input() |
| 183 | + set summary(StepDirective step) { |
| 184 | + assert(step != null, 'Should pass in a valid step for a summary.'); |
| 185 | + step.summaryDirective = this; |
| 186 | + } |
| 187 | + |
| 188 | + SummaryDirective(TemplateRef ref, ViewContainerRef viewContainerRef) |
| 189 | + : super(ref, viewContainerRef); |
| 190 | +} |
0 commit comments