1 | import { inject } from 'aurelia-dependency-injection';
|
2 | import { BoundViewFactory, customAttribute, templateController, ViewSlot } from 'aurelia-templating';
|
3 | import { IfCore } from './if-core';
|
4 |
|
5 | @customAttribute('else')
|
6 | @templateController
|
7 | @inject(BoundViewFactory, ViewSlot)
|
8 | export class Else extends IfCore {
|
9 |
|
10 | |
11 |
|
12 |
|
13 | ifVm: any;
|
14 |
|
15 | constructor(viewFactory, viewSlot) {
|
16 | super(viewFactory, viewSlot);
|
17 | this._registerInIf();
|
18 | }
|
19 |
|
20 | bind(bindingContext, overrideContext) {
|
21 | super.bind(bindingContext, overrideContext);
|
22 |
|
23 | if (this.ifVm.condition) {
|
24 | this._hide();
|
25 | } else {
|
26 | this._show();
|
27 | }
|
28 | }
|
29 |
|
30 | |
31 |
|
32 |
|
33 | _registerInIf() {
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | let previous = (this.viewSlot as ViewSlot & { anchor: any}).anchor.previousSibling;
|
40 | while (previous && !previous.au) {
|
41 | previous = previous.previousSibling;
|
42 | }
|
43 | if (!previous || !previous.au.if) {
|
44 | throw new Error("Can't find matching If for Else custom attribute.");
|
45 | }
|
46 | this.ifVm = previous.au.if.viewModel;
|
47 | this.ifVm.elseVm = this;
|
48 | }
|
49 | }
|