/
portal.js
179 lines (179 loc) · 6.68 KB
/
portal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var aurelia_templating_1 = require("aurelia-templating");
var aurelia_pal_1 = require("aurelia-pal");
var document = aurelia_pal_1.PLATFORM.global.document;
var Portal = /** @class */ (function () {
function Portal(viewFactory, originalViewslot) {
this.viewFactory = viewFactory;
this.originalViewslot = originalViewslot;
this.strict = false;
this.initialRender = false;
this.currentTarget = unset;
}
Portal_1 = Portal;
Portal.getTarget = function (target, context) {
if (target) {
if (typeof target === 'string') {
var queryContext = document;
if (context) {
if (typeof context === 'string') {
context = document.querySelector(context);
}
if (context instanceof Element) {
queryContext = context;
}
}
target = queryContext.querySelector(target);
}
if (target instanceof Element) {
return target;
}
}
return null;
};
Portal.prototype.bind = function (bindingContext, overrideContext) {
if (!this.callbackContext) {
this.callbackContext = bindingContext;
}
var view = this.view;
if (!view) {
view = this.view = this.viewFactory.create();
}
var shouldInitRender = this.initialRender;
if (shouldInitRender) {
this.originalViewslot.add(view);
}
view.bind(bindingContext, overrideContext);
if (shouldInitRender) {
this.originalViewslot.remove(view);
}
};
Portal.prototype.attached = function () {
this.isAttached = true;
return this.render();
};
Portal.prototype.detached = function () {
this.isAttached = false;
if (this.viewSlot) {
this.viewSlot.detached();
}
};
Portal.prototype.unbind = function () {
if (this.viewSlot) {
this.viewSlot.remove(this.view);
this.viewSlot = null;
}
this.view.unbind();
this.callbackContext = null;
};
Portal.prototype.getTarget = function () {
var target = Portal_1.getTarget(this.target, this.renderContext);
if (target === null) {
if (this.strict) {
throw new Error('Render target not found.');
}
else {
target = document.body;
}
}
return target;
};
Portal.prototype.render = function () {
var _this = this;
var oldTarget = this.currentTarget;
var view = this.view;
var target = this.currentTarget = this.getTarget();
var oldViewSlot = this.viewSlot;
if (oldTarget === target && oldViewSlot) {
return;
}
var addAction = function () {
if (_this.isAttached) {
return Promise.resolve(typeof _this.activating === 'function'
? _this.activating.call(_this.callbackContext, target, view)
: null).then(function () {
if (target === _this.currentTarget || oldTarget === unset) {
var viewSlot = _this.viewSlot = new aurelia_templating_1.ViewSlot(target, true);
viewSlot.attached();
viewSlot.add(view);
_this.removed = false;
}
return Promise.resolve().then(function () {
typeof _this.activated === 'function'
? _this.activated.call(_this.callbackContext, target, view)
: null;
});
});
}
return Promise.resolve(null);
};
if (oldViewSlot) {
return Promise.resolve(typeof this.deactivating === 'function'
? this.deactivating.call(this.callbackContext, oldTarget, view)
: null).then(function () {
if (typeof _this.deactivated === 'function') {
_this.deactivated.call(_this.callbackContext, oldTarget, view);
}
_this.viewSlot = null;
if (!_this.removed) {
oldViewSlot.remove(view);
_this.removed = true;
}
return addAction();
});
}
return Promise.resolve(addAction());
};
Portal.prototype.targetChanged = function () {
return this.render();
};
/**
* Only needs the BoundViewFactory as a custom viewslot will be used
*/
Portal.inject = [aurelia_templating_1.BoundViewFactory, aurelia_templating_1.ViewSlot];
__decorate([
aurelia_templating_1.bindable({
primaryProperty: true,
defaultValue: null
})
], Portal.prototype, "target", void 0);
__decorate([
aurelia_templating_1.bindable({ changeHandler: 'targetChanged' })
], Portal.prototype, "renderContext", void 0);
__decorate([
aurelia_templating_1.bindable()
], Portal.prototype, "strict", void 0);
__decorate([
aurelia_templating_1.bindable()
], Portal.prototype, "initialRender", void 0);
__decorate([
aurelia_templating_1.bindable()
], Portal.prototype, "deactivating", void 0);
__decorate([
aurelia_templating_1.bindable()
], Portal.prototype, "activating", void 0);
__decorate([
aurelia_templating_1.bindable()
], Portal.prototype, "activated", void 0);
__decorate([
aurelia_templating_1.bindable()
], Portal.prototype, "deactivated", void 0);
__decorate([
aurelia_templating_1.bindable()
], Portal.prototype, "callbackContext", void 0);
Portal = Portal_1 = __decorate([
aurelia_templating_1.templateController(),
aurelia_templating_1.customAttribute('portal')
], Portal);
return Portal;
var Portal_1;
}());
exports.Portal = Portal;
var unset = {};