1 | import { ViewFactory, ViewSlot } from 'aurelia-templating';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | export class IfCore {
|
7 | |
8 |
|
9 |
|
10 | viewFactory: ViewFactory;
|
11 | |
12 |
|
13 |
|
14 | viewSlot: ViewSlot;
|
15 | |
16 |
|
17 |
|
18 | view: any;
|
19 | |
20 |
|
21 |
|
22 | bindingContext: any;
|
23 | |
24 |
|
25 |
|
26 | overrideContext: any;
|
27 | |
28 |
|
29 |
|
30 | showing: boolean;
|
31 | |
32 |
|
33 |
|
34 | cache: boolean | string;
|
35 |
|
36 | constructor(viewFactory: ViewFactory, viewSlot: ViewSlot) {
|
37 | this.viewFactory = viewFactory;
|
38 | this.viewSlot = viewSlot;
|
39 | this.view = null;
|
40 | this.bindingContext = null;
|
41 | this.overrideContext = null;
|
42 |
|
43 |
|
44 |
|
45 | this.showing = false;
|
46 | this.cache = true;
|
47 | }
|
48 |
|
49 | bind(bindingContext, overrideContext) {
|
50 |
|
51 | this.bindingContext = bindingContext;
|
52 | this.overrideContext = overrideContext;
|
53 | }
|
54 |
|
55 | unbind() {
|
56 | if (this.view === null) {
|
57 | return;
|
58 | }
|
59 |
|
60 | this.view.unbind();
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | if (!this.viewFactory.isCaching) {
|
66 | return;
|
67 | }
|
68 |
|
69 | if (this.showing) {
|
70 | this.showing = false;
|
71 | this.viewSlot.remove(this.view, true, true);
|
72 | } else {
|
73 | this.view.returnToCache();
|
74 | }
|
75 |
|
76 | this.view = null;
|
77 | }
|
78 |
|
79 | |
80 |
|
81 |
|
82 | _show() {
|
83 | if (this.showing) {
|
84 |
|
85 |
|
86 |
|
87 | if (!this.view.isBound) {
|
88 | this.view.bind(this.bindingContext, this.overrideContext);
|
89 | }
|
90 | return;
|
91 | }
|
92 |
|
93 | if (this.view === null) {
|
94 | this.view = (this.viewFactory as any).create();
|
95 | }
|
96 |
|
97 | if (!this.view.isBound) {
|
98 | this.view.bind(this.bindingContext, this.overrideContext);
|
99 | }
|
100 |
|
101 | this.showing = true;
|
102 | return this.viewSlot.add(this.view);
|
103 | }
|
104 |
|
105 | |
106 |
|
107 |
|
108 | _hide() {
|
109 | if (!this.showing) {
|
110 | return;
|
111 | }
|
112 |
|
113 | this.showing = false;
|
114 | let removed = this.viewSlot.remove(this.view);
|
115 |
|
116 | if (removed instanceof Promise) {
|
117 | return removed.then(() => {
|
118 | this._unbindView();
|
119 | });
|
120 | }
|
121 |
|
122 | this._unbindView();
|
123 | }
|
124 |
|
125 | |
126 |
|
127 |
|
128 | _unbindView() {
|
129 | const cache = this.cache === 'false' ? false : !!this.cache;
|
130 | this.view.unbind();
|
131 | if (!cache) {
|
132 | this.view = null;
|
133 | }
|
134 | }
|
135 | }
|