UNPKG

1.88 kBPlain TextView Raw
1import {Optional} from 'aurelia-dependency-injection';
2import {customAttribute, Animator} from 'aurelia-templating';
3import {DOM} from 'aurelia-pal';
4import {injectAureliaHideStyleAtBoundary, aureliaHideClassName} from './aurelia-hide-style';
5
6/**
7 * Binding to conditionally show markup in the DOM based on the value.
8 * - different from "if" in that the markup is still added to the DOM, simply not shown.
9 */
10@customAttribute('show')
11export class Show {
12
13 /**@internal*/
14 element: any;
15 /**@internal*/
16 animator: any;
17 /**@internal*/
18 domBoundary: any;
19
20 value: any;
21
22 /**@internal */
23 static inject() {
24 return [DOM.Element, Animator, Optional.of(DOM.boundary, true)];
25 }
26
27 /**
28 * Creates a new instance of Show.
29 * @param element Target element to conditionally show.
30 * @param animator The animator that conditionally adds or removes the aurelia-hide css class.
31 * @param domBoundary The DOM boundary. Used when the behavior appears within a component that utilizes the shadow DOM.
32 */
33 constructor(element, animator, domBoundary) {
34 this.element = element;
35 this.animator = animator;
36 this.domBoundary = domBoundary;
37 }
38
39 /**
40 * Invoked when the behavior is created.
41 */
42 created() {
43 injectAureliaHideStyleAtBoundary(this.domBoundary);
44 }
45
46 /**
47 * Invoked everytime the bound value changes.
48 * @param newValue The new value.
49 */
50 valueChanged(newValue) {
51 let element = this.element;
52 let animator = this.animator;
53 if (newValue) {
54 animator.removeClass(element, aureliaHideClassName);
55 } else {
56 animator.addClass(element, aureliaHideClassName);
57 }
58 }
59
60 /**
61 * Binds the Show attribute.
62 */
63 // eslint-disable-next-line @typescript-eslint/no-unused-vars
64 bind(bindingContext) {
65 this.valueChanged(this.value);
66 }
67}