@tko/binding.template 4.0.0-alpha8.4 → 4.0.0-beta1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/foreach.js +32 -0
- package/dist/foreach.js.map +7 -0
- package/dist/index.cjs +3518 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +7 -0
- package/dist/index.mjs +15 -0
- package/dist/index.mjs.map +7 -0
- package/dist/nativeTemplateEngine.js +27 -0
- package/dist/nativeTemplateEngine.js.map +7 -0
- package/dist/templateEngine.js +38 -0
- package/dist/templateEngine.js.map +7 -0
- package/dist/templateSources.js +80 -0
- package/dist/templateSources.js.map +7 -0
- package/dist/templating.js +315 -0
- package/dist/templating.js.map +7 -0
- package/helpers/{dummyTemplateEngine.js → dummyTemplateEngine.ts} +4 -3
- package/package.json +20 -30
- package/dist/binding.template.es6.js +0 -557
- package/dist/binding.template.es6.js.map +0 -1
- package/dist/binding.template.js +0 -576
- package/dist/binding.template.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
|
+
"version": "4.0.0-beta1.3",
|
|
2
3
|
"name": "@tko/binding.template",
|
|
3
|
-
"version": "4.0.0-alpha8.4",
|
|
4
4
|
"description": "TKO Template bindings",
|
|
5
5
|
"module": "dist/binding.template.js",
|
|
6
6
|
"files": [
|
|
@@ -9,56 +9,46 @@
|
|
|
9
9
|
],
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
|
-
"url": "git+https://github.com/knockout/tko.
|
|
12
|
+
"url": "git+https://github.com/knockout/tko.git"
|
|
13
13
|
},
|
|
14
14
|
"keywords": [
|
|
15
15
|
"knockout",
|
|
16
16
|
"tko",
|
|
17
17
|
"ko"
|
|
18
18
|
],
|
|
19
|
-
"author": "Knockout",
|
|
19
|
+
"author": "The Knockout Team",
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"bugs": {
|
|
22
|
-
"url": "https://github.com/knockout/tko
|
|
22
|
+
"url": "https://github.com/knockout/tko/issues"
|
|
23
23
|
},
|
|
24
24
|
"homepage": "https://tko.io",
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@tko/bind": "^4.0.0-
|
|
27
|
-
"@tko/computed": "^4.0.0-
|
|
28
|
-
"@tko/observable": "^4.0.0-
|
|
29
|
-
"@tko/utils": "^4.0.0-
|
|
30
|
-
"tslib": "^
|
|
26
|
+
"@tko/bind": "^4.0.0-beta1.3",
|
|
27
|
+
"@tko/computed": "^4.0.0-beta1.3",
|
|
28
|
+
"@tko/observable": "^4.0.0-beta1.3",
|
|
29
|
+
"@tko/utils": "^4.0.0-beta1.3",
|
|
30
|
+
"tslib": "^2.2.0"
|
|
31
31
|
},
|
|
32
32
|
"karma": {
|
|
33
33
|
"frameworks": [
|
|
34
34
|
"jasmine"
|
|
35
35
|
]
|
|
36
36
|
},
|
|
37
|
-
"
|
|
38
|
-
"@tko/binding.if": "^4.0.0-
|
|
39
|
-
},
|
|
40
|
-
"__about__shared.package.json": "These properties are copied into all packages/*/package.json. Run `yarn repackage`",
|
|
41
|
-
"standard": {
|
|
42
|
-
"env": [
|
|
43
|
-
"browser",
|
|
44
|
-
"jasmine",
|
|
45
|
-
"mocha"
|
|
46
|
-
]
|
|
47
|
-
},
|
|
48
|
-
"scripts": {
|
|
49
|
-
"test": "npx karma start ../../karma.conf.js --once",
|
|
50
|
-
"build": "npx rollup -c ../../rollup.config.js",
|
|
51
|
-
"watch": "npx karma start ../../karma.conf.js",
|
|
52
|
-
"prepare": "npx rollup -c ../../rollup.config.js"
|
|
53
|
-
},
|
|
54
|
-
"publishConfig": {
|
|
55
|
-
"access": "public"
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@tko/binding.if": "^4.0.0-alpha9.0"
|
|
56
39
|
},
|
|
57
40
|
"licenses": [
|
|
58
41
|
{
|
|
59
42
|
"type": "MIT",
|
|
60
|
-
"url": "
|
|
43
|
+
"url": "https://opensource.org/licenses/MIT"
|
|
61
44
|
}
|
|
62
45
|
],
|
|
63
|
-
"
|
|
46
|
+
"exports": {
|
|
47
|
+
".": {
|
|
48
|
+
"require": "./dist/index.cjs",
|
|
49
|
+
"import": "./dist/index.js"
|
|
50
|
+
},
|
|
51
|
+
"./helpers/*": "./helpers/*"
|
|
52
|
+
},
|
|
53
|
+
"gitHead": "a8843acb8ae085915115e53a4e057b30731c635e"
|
|
64
54
|
}
|
|
@@ -1,557 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* TKO Template bindings 🥊 @tko/binding.template@4.0.0-alpha8.4
|
|
3
|
-
* (c) The Knockout.js Team - https://tko.io
|
|
4
|
-
* License: MIT (http://www.opensource.org/licenses/mit-license.php)
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { tagNameLower, setHtml, domData, parseHtmlForTemplateNodes, extend, options, virtualElements, fixUpContinuousNodeArray, replaceDomNodes, memoization, domNodeIsAttachedToDocument, moveCleanedNodesToContainerElement, arrayFilter, ieVersion, makeArray, parseHtmlFragment } from '@tko/utils';
|
|
8
|
-
import { applyBindings, setDomNodeChildrenFromArrayMapping, AsyncBindingHandler, bindingEvent, bindingContext } from '@tko/bind';
|
|
9
|
-
import { computed } from '@tko/computed';
|
|
10
|
-
import { isObservable, dependencyDetection, unwrap, observable, isObservableArray, peek } from '@tko/observable';
|
|
11
|
-
|
|
12
|
-
// A template source represents a read/write way of accessing a template. This is to eliminate the need for template loading/saving
|
|
13
|
-
|
|
14
|
-
// ---- ko.templateSources.domElement -----
|
|
15
|
-
|
|
16
|
-
// template types
|
|
17
|
-
var templateScript = 1,
|
|
18
|
-
templateTextArea = 2,
|
|
19
|
-
templateTemplate = 3,
|
|
20
|
-
templateElement = 4;
|
|
21
|
-
|
|
22
|
-
function domElement (element) {
|
|
23
|
-
this.domElement = element;
|
|
24
|
-
|
|
25
|
-
if (!element) { return }
|
|
26
|
-
var tagNameLower$$1 = tagNameLower(element);
|
|
27
|
-
this.templateType =
|
|
28
|
-
tagNameLower$$1 === 'script' ? templateScript
|
|
29
|
-
: tagNameLower$$1 === 'textarea' ? templateTextArea
|
|
30
|
-
// For browsers with proper <template> element support, where the .content property gives a document fragment
|
|
31
|
-
: tagNameLower$$1 == 'template' && element.content && element.content.nodeType === 11 ? templateTemplate
|
|
32
|
-
: templateElement;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
domElement.prototype.text = function (/* valueToWrite */) {
|
|
36
|
-
var elemContentsProperty = this.templateType === templateScript ? 'text'
|
|
37
|
-
: this.templateType === templateTextArea ? 'value'
|
|
38
|
-
: 'innerHTML';
|
|
39
|
-
|
|
40
|
-
if (arguments.length == 0) {
|
|
41
|
-
return this.domElement[elemContentsProperty]
|
|
42
|
-
} else {
|
|
43
|
-
var valueToWrite = arguments[0];
|
|
44
|
-
if (elemContentsProperty === 'innerHTML') { setHtml(this.domElement, valueToWrite); } else { this.domElement[elemContentsProperty] = valueToWrite; }
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
var dataDomDataPrefix = domData.nextKey() + '_';
|
|
49
|
-
domElement.prototype.data = function (key /*, valueToWrite */) {
|
|
50
|
-
if (arguments.length === 1) {
|
|
51
|
-
return domData.get(this.domElement, dataDomDataPrefix + key)
|
|
52
|
-
} else {
|
|
53
|
-
domData.set(this.domElement, dataDomDataPrefix + key, arguments[1]);
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
var templatesDomDataKey = domData.nextKey();
|
|
58
|
-
function getTemplateDomData (element) {
|
|
59
|
-
return domData.get(element, templatesDomDataKey) || {}
|
|
60
|
-
}
|
|
61
|
-
function setTemplateDomData (element, data) {
|
|
62
|
-
domData.set(element, templatesDomDataKey, data);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
domElement.prototype.nodes = function (/* valueToWrite */) {
|
|
66
|
-
var element = this.domElement;
|
|
67
|
-
if (arguments.length == 0) {
|
|
68
|
-
const templateData = getTemplateDomData(element);
|
|
69
|
-
let nodes = templateData.containerData || (
|
|
70
|
-
this.templateType === templateTemplate ? element.content :
|
|
71
|
-
this.templateType === templateElement ? element :
|
|
72
|
-
undefined
|
|
73
|
-
);
|
|
74
|
-
if (!nodes || templateData.alwaysCheckText) {
|
|
75
|
-
// If the template is associated with an element that stores the template as text,
|
|
76
|
-
// parse and cache the nodes whenever there's new text content available. This allows
|
|
77
|
-
// the user to update the template content by updating the text of template node.
|
|
78
|
-
const text = this['text']();
|
|
79
|
-
if (text) {
|
|
80
|
-
nodes = parseHtmlForTemplateNodes(text, element.ownerDocument);
|
|
81
|
-
this['text'](''); // clear the text from the node
|
|
82
|
-
setTemplateDomData(element, {containerData: nodes, alwaysCheckText: true});
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return nodes
|
|
87
|
-
} else {
|
|
88
|
-
var valueToWrite = arguments[0];
|
|
89
|
-
setTemplateDomData(element, {containerData: valueToWrite});
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
// ---- ko.templateSources.anonymousTemplate -----
|
|
94
|
-
// Anonymous templates are normally saved/retrieved as DOM nodes through "nodes".
|
|
95
|
-
// For compatibility, you can also read "text"; it will be serialized from the nodes on demand.
|
|
96
|
-
// Writing to "text" is still supported, but then the template data will not be available as DOM nodes.
|
|
97
|
-
|
|
98
|
-
function anonymousTemplate (element) {
|
|
99
|
-
this.domElement = element;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
anonymousTemplate.prototype = new domElement();
|
|
103
|
-
anonymousTemplate.prototype.constructor = anonymousTemplate;
|
|
104
|
-
anonymousTemplate.prototype.text = function (/* valueToWrite */) {
|
|
105
|
-
if (arguments.length == 0) {
|
|
106
|
-
var templateData = getTemplateDomData(this.domElement);
|
|
107
|
-
if (templateData.textData === undefined && templateData.containerData) { templateData.textData = templateData.containerData.innerHTML; }
|
|
108
|
-
return templateData.textData
|
|
109
|
-
} else {
|
|
110
|
-
var valueToWrite = arguments[0];
|
|
111
|
-
setTemplateDomData(this.domElement, {textData: valueToWrite});
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
// If you want to make a custom template engine,
|
|
116
|
-
|
|
117
|
-
function templateEngine () { }
|
|
118
|
-
extend(templateEngine.prototype, {
|
|
119
|
-
renderTemplateSource: function (templateSource, bindingContext$$1, options$$1, templateDocument) {
|
|
120
|
-
options$$1.onError('Override renderTemplateSource');
|
|
121
|
-
},
|
|
122
|
-
|
|
123
|
-
createJavaScriptEvaluatorBlock: function (script) {
|
|
124
|
-
options.onError('Override createJavaScriptEvaluatorBlock');
|
|
125
|
-
},
|
|
126
|
-
|
|
127
|
-
makeTemplateSource: function (template, templateDocument) {
|
|
128
|
-
// Named template
|
|
129
|
-
if (typeof template === 'string') {
|
|
130
|
-
templateDocument = templateDocument || document;
|
|
131
|
-
var elem = templateDocument.getElementById(template);
|
|
132
|
-
if (!elem) { options.onError('Cannot find template with ID ' + template); }
|
|
133
|
-
return new domElement(elem)
|
|
134
|
-
} else if ((template.nodeType == 1) || (template.nodeType == 8)) {
|
|
135
|
-
// Anonymous template
|
|
136
|
-
return new anonymousTemplate(template)
|
|
137
|
-
} else { options.onError('Unknown template type: ' + template); }
|
|
138
|
-
},
|
|
139
|
-
|
|
140
|
-
renderTemplate: function (template, bindingContext$$1, options$$1, templateDocument) {
|
|
141
|
-
var templateSource = this['makeTemplateSource'](template, templateDocument);
|
|
142
|
-
return this.renderTemplateSource(templateSource, bindingContext$$1, options$$1, templateDocument)
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
var _templateEngine;
|
|
147
|
-
const cleanContainerDomDataKey = domData.nextKey();
|
|
148
|
-
|
|
149
|
-
function setTemplateEngine (tEngine) {
|
|
150
|
-
if ((tEngine !== undefined) && !(tEngine instanceof templateEngine)) {
|
|
151
|
-
// TODO: ko.templateEngine to appropriate name
|
|
152
|
-
throw new Error('templateEngine must inherit from ko.templateEngine')
|
|
153
|
-
}
|
|
154
|
-
_templateEngine = tEngine;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
function invokeForEachNodeInContinuousRange (firstNode, lastNode, action) {
|
|
158
|
-
let node;
|
|
159
|
-
let nextInQueue = firstNode;
|
|
160
|
-
let firstOutOfRangeNode = virtualElements.nextSibling(lastNode);
|
|
161
|
-
while (nextInQueue && ((node = nextInQueue) !== firstOutOfRangeNode)) {
|
|
162
|
-
nextInQueue = virtualElements.nextSibling(node);
|
|
163
|
-
action(node, nextInQueue);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
function activateBindingsOnContinuousNodeArray (continuousNodeArray, bindingContext$$1, afterBindingCallback) {
|
|
168
|
-
// To be used on any nodes that have been rendered by a template and have been inserted into some parent element
|
|
169
|
-
// Walks through continuousNodeArray (which *must* be continuous, i.e., an uninterrupted sequence of sibling nodes, because
|
|
170
|
-
// the algorithm for walking them relies on this), and for each top-level item in the virtual-element sense,
|
|
171
|
-
// (1) Does a regular "applyBindings" to associate bindingContext with this node and to activate any non-memoized bindings
|
|
172
|
-
// (2) Unmemoizes any memos in the DOM subtree (e.g., to activate bindings that had been memoized during template rewriting)
|
|
173
|
-
|
|
174
|
-
if (continuousNodeArray.length) {
|
|
175
|
-
var firstNode = continuousNodeArray[0];
|
|
176
|
-
var lastNode = continuousNodeArray[continuousNodeArray.length - 1];
|
|
177
|
-
var parentNode = firstNode.parentNode;
|
|
178
|
-
var provider = options.bindingProviderInstance;
|
|
179
|
-
var preprocessNode = provider.preprocessNode;
|
|
180
|
-
|
|
181
|
-
if (preprocessNode) {
|
|
182
|
-
invokeForEachNodeInContinuousRange(firstNode, lastNode, function (node, nextNodeInRange) {
|
|
183
|
-
var nodePreviousSibling = node.previousSibling;
|
|
184
|
-
var newNodes = preprocessNode.call(provider, node);
|
|
185
|
-
if (newNodes) {
|
|
186
|
-
if (node === firstNode) { firstNode = newNodes[0] || nextNodeInRange; }
|
|
187
|
-
if (node === lastNode) { lastNode = newNodes[newNodes.length - 1] || nodePreviousSibling; }
|
|
188
|
-
}
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
// Because preprocessNode can change the nodes, including the first and last nodes, update continuousNodeArray to match.
|
|
192
|
-
// We need the full set, including inner nodes, because the unmemoize step might remove the first node (and so the real
|
|
193
|
-
// first node needs to be in the array).
|
|
194
|
-
continuousNodeArray.length = 0;
|
|
195
|
-
if (!firstNode) { // preprocessNode might have removed all the nodes, in which case there's nothing left to do
|
|
196
|
-
return
|
|
197
|
-
}
|
|
198
|
-
if (firstNode === lastNode) {
|
|
199
|
-
continuousNodeArray.push(firstNode);
|
|
200
|
-
} else {
|
|
201
|
-
continuousNodeArray.push(firstNode, lastNode);
|
|
202
|
-
fixUpContinuousNodeArray(continuousNodeArray, parentNode);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
// Need to applyBindings *before* unmemoziation, because unmemoization might introduce extra nodes (that we don't want to re-bind)
|
|
207
|
-
// whereas a regular applyBindings won't introduce new memoized nodes
|
|
208
|
-
invokeForEachNodeInContinuousRange(firstNode, lastNode, function (node) {
|
|
209
|
-
if (node.nodeType === 1 || node.nodeType === 8) { applyBindings(bindingContext$$1, node).then(afterBindingCallback); }
|
|
210
|
-
});
|
|
211
|
-
invokeForEachNodeInContinuousRange(firstNode, lastNode, function (node) {
|
|
212
|
-
if (node.nodeType === 1 || node.nodeType === 8) { memoization.unmemoizeDomNodeAndDescendants(node, [bindingContext$$1]); }
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
// Make sure any changes done by applyBindings or unmemoize are reflected in the array
|
|
216
|
-
fixUpContinuousNodeArray(continuousNodeArray, parentNode);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
function getFirstNodeFromPossibleArray (nodeOrNodeArray) {
|
|
221
|
-
return nodeOrNodeArray.nodeType ? nodeOrNodeArray
|
|
222
|
-
: nodeOrNodeArray.length > 0 ? nodeOrNodeArray[0]
|
|
223
|
-
: null
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
function executeTemplate (targetNodeOrNodeArray, renderMode, template, bindingContext$$1, options$$1, afterBindingCallback) {
|
|
227
|
-
options$$1 = options$$1 || {};
|
|
228
|
-
var firstTargetNode = targetNodeOrNodeArray && getFirstNodeFromPossibleArray(targetNodeOrNodeArray);
|
|
229
|
-
var templateDocument = (firstTargetNode || template || {}).ownerDocument;
|
|
230
|
-
var templateEngineToUse = (options$$1.templateEngine || _templateEngine);
|
|
231
|
-
var renderedNodesArray = templateEngineToUse.renderTemplate(template, bindingContext$$1, options$$1, templateDocument);
|
|
232
|
-
|
|
233
|
-
// Loosely check result is an array of DOM nodes
|
|
234
|
-
if ((typeof renderedNodesArray.length !== 'number') || (renderedNodesArray.length > 0 && typeof renderedNodesArray[0].nodeType !== 'number')) { throw new Error('Template engine must return an array of DOM nodes') }
|
|
235
|
-
|
|
236
|
-
var haveAddedNodesToParent = false;
|
|
237
|
-
switch (renderMode) {
|
|
238
|
-
case 'replaceChildren':
|
|
239
|
-
virtualElements.setDomNodeChildren(targetNodeOrNodeArray, renderedNodesArray);
|
|
240
|
-
haveAddedNodesToParent = true;
|
|
241
|
-
break
|
|
242
|
-
case 'replaceNode':
|
|
243
|
-
replaceDomNodes(targetNodeOrNodeArray, renderedNodesArray);
|
|
244
|
-
haveAddedNodesToParent = true;
|
|
245
|
-
break
|
|
246
|
-
case 'ignoreTargetNode': break
|
|
247
|
-
default:
|
|
248
|
-
throw new Error('Unknown renderMode: ' + renderMode)
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
if (haveAddedNodesToParent) {
|
|
252
|
-
activateBindingsOnContinuousNodeArray(renderedNodesArray, bindingContext$$1, afterBindingCallback);
|
|
253
|
-
if (options$$1.afterRender) { dependencyDetection.ignore(options$$1.afterRender, null, [renderedNodesArray, bindingContext$$1['$data']]); }
|
|
254
|
-
if (renderMode === 'replaceChildren') {
|
|
255
|
-
bindingEvent.notify(targetNodeOrNodeArray, bindingEvent.childrenComplete);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
return renderedNodesArray
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
function resolveTemplateName (template, data, context) {
|
|
263
|
-
// The template can be specified as:
|
|
264
|
-
if (isObservable(template)) {
|
|
265
|
-
// 1. An observable, with string value
|
|
266
|
-
return template()
|
|
267
|
-
} else if (typeof template === 'function') {
|
|
268
|
-
// 2. A function of (data, context) returning a string
|
|
269
|
-
return template(data, context)
|
|
270
|
-
} else {
|
|
271
|
-
// 3. A string
|
|
272
|
-
return template
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
function renderTemplate (template, dataOrBindingContext, options$$1, targetNodeOrNodeArray, renderMode, afterBindingCallback) {
|
|
277
|
-
options$$1 = options$$1 || {};
|
|
278
|
-
if ((options$$1.templateEngine || _templateEngine) === undefined) { throw new Error('Set a template engine before calling renderTemplate') }
|
|
279
|
-
renderMode = renderMode || 'replaceChildren';
|
|
280
|
-
|
|
281
|
-
if (targetNodeOrNodeArray) {
|
|
282
|
-
var firstTargetNode = getFirstNodeFromPossibleArray(targetNodeOrNodeArray);
|
|
283
|
-
|
|
284
|
-
var whenToDispose = function () { return (!firstTargetNode) || !domNodeIsAttachedToDocument(firstTargetNode) }; // Passive disposal (on next evaluation)
|
|
285
|
-
var activelyDisposeWhenNodeIsRemoved = (firstTargetNode && renderMode === 'replaceNode') ? firstTargetNode.parentNode : firstTargetNode;
|
|
286
|
-
|
|
287
|
-
return computed( // So the DOM is automatically updated when any dependency changes
|
|
288
|
-
function () {
|
|
289
|
-
// Ensure we've got a proper binding context to work with
|
|
290
|
-
var bindingContext$$1 = (dataOrBindingContext && (dataOrBindingContext instanceof bindingContext))
|
|
291
|
-
? dataOrBindingContext
|
|
292
|
-
: new bindingContext(dataOrBindingContext, null, null, null, { 'exportDependencies': true });
|
|
293
|
-
|
|
294
|
-
var templateName = resolveTemplateName(template, bindingContext$$1.$data, bindingContext$$1);
|
|
295
|
-
const renderedNodesArray = executeTemplate(targetNodeOrNodeArray, renderMode, templateName, bindingContext$$1, options$$1, afterBindingCallback);
|
|
296
|
-
|
|
297
|
-
if (renderMode === 'replaceNode') {
|
|
298
|
-
targetNodeOrNodeArray = renderedNodesArray;
|
|
299
|
-
firstTargetNode = getFirstNodeFromPossibleArray(targetNodeOrNodeArray);
|
|
300
|
-
}
|
|
301
|
-
},
|
|
302
|
-
null,
|
|
303
|
-
{ disposeWhen: whenToDispose, disposeWhenNodeIsRemoved: activelyDisposeWhenNodeIsRemoved }
|
|
304
|
-
)
|
|
305
|
-
} else {
|
|
306
|
-
// We don't yet have a DOM node to evaluate, so use a memo and render the template later when there is a DOM node
|
|
307
|
-
return memoization.memoize(function (domNode) {
|
|
308
|
-
renderTemplate(template, dataOrBindingContext, options$$1, domNode, 'replaceNode');
|
|
309
|
-
})
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
function renderTemplateForEach (template, arrayOrObservableArray, options$$1, targetNode, parentBindingContext, afterBindingCallback) {
|
|
314
|
-
// Since setDomNodeChildrenFromArrayMapping always calls executeTemplateForArrayItem and then
|
|
315
|
-
// activateBindingsCallback for added items, we can store the binding context in the former to use in the latter.
|
|
316
|
-
var arrayItemContext;
|
|
317
|
-
|
|
318
|
-
// This will be called by setDomNodeChildrenFromArrayMapping to get the nodes to add to targetNode
|
|
319
|
-
function executeTemplateForArrayItem (arrayValue, index) {
|
|
320
|
-
// Support selecting template as a function of the data being rendered
|
|
321
|
-
if (options$$1.as) {
|
|
322
|
-
if (options.createChildContextWithAs) {
|
|
323
|
-
arrayItemContext = parentBindingContext.createChildContext(
|
|
324
|
-
arrayValue, options$$1.as, context => { context.$index = index; }
|
|
325
|
-
);
|
|
326
|
-
} else {
|
|
327
|
-
arrayItemContext = parentBindingContext.extend({
|
|
328
|
-
[options$$1.as]: arrayValue,
|
|
329
|
-
$index: index
|
|
330
|
-
});
|
|
331
|
-
}
|
|
332
|
-
} else {
|
|
333
|
-
arrayItemContext = parentBindingContext.createChildContext(arrayValue, options$$1.as, context => { context.$index = index; });
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
var templateName = resolveTemplateName(template, arrayValue, arrayItemContext);
|
|
337
|
-
return executeTemplate(targetNode, 'ignoreTargetNode', templateName, arrayItemContext, options$$1, afterBindingCallback)
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
// This will be called whenever setDomNodeChildrenFromArrayMapping has added nodes to targetNode
|
|
341
|
-
var activateBindingsCallback = function (arrayValue, addedNodesArray /*, index */) {
|
|
342
|
-
activateBindingsOnContinuousNodeArray(addedNodesArray, arrayItemContext, afterBindingCallback);
|
|
343
|
-
if (options$$1.afterRender) { options$$1.afterRender(addedNodesArray, arrayValue); }
|
|
344
|
-
|
|
345
|
-
// release the "cache" variable, so that it can be collected by
|
|
346
|
-
// the GC when its value isn't used from within the bindings anymore.
|
|
347
|
-
arrayItemContext = null;
|
|
348
|
-
};
|
|
349
|
-
|
|
350
|
-
// Call setDomNodeChildrenFromArrayMapping, ignoring any observables unwrapped within (most likely from a callback function).
|
|
351
|
-
// If the array items are observables, though, they will be unwrapped in executeTemplateForArrayItem and managed within setDomNodeChildrenFromArrayMapping.
|
|
352
|
-
function localSetDomNodeChildrenFromArrayMapping (newArray, changeList) {
|
|
353
|
-
dependencyDetection.ignore(setDomNodeChildrenFromArrayMapping, null, [targetNode, newArray, executeTemplateForArrayItem, options$$1, activateBindingsCallback, changeList]);
|
|
354
|
-
bindingEvent.notify(targetNode, bindingEvent.childrenComplete);
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
const shouldHideDestroyed = (options$$1.includeDestroyed === false) || (options.foreachHidesDestroyed && !options$$1.includeDestroyed);
|
|
358
|
-
if (!shouldHideDestroyed && !options$$1.beforeRemove && isObservableArray(arrayOrObservableArray)) {
|
|
359
|
-
localSetDomNodeChildrenFromArrayMapping(arrayOrObservableArray.peek());
|
|
360
|
-
var subscription = arrayOrObservableArray.subscribe(function (changeList) {
|
|
361
|
-
localSetDomNodeChildrenFromArrayMapping(arrayOrObservableArray(), changeList);
|
|
362
|
-
}, null, 'arrayChange');
|
|
363
|
-
subscription.disposeWhenNodeIsRemoved(targetNode);
|
|
364
|
-
return subscription
|
|
365
|
-
} else {
|
|
366
|
-
return computed(function () {
|
|
367
|
-
var unwrappedArray = unwrap(arrayOrObservableArray) || [];
|
|
368
|
-
const unwrappedIsIterable = Symbol.iterator in unwrappedArray;
|
|
369
|
-
if (!unwrappedIsIterable) { unwrappedArray = [unwrappedArray]; }
|
|
370
|
-
if (shouldHideDestroyed) {
|
|
371
|
-
// Filter out any entries marked as destroyed
|
|
372
|
-
unwrappedArray = arrayFilter(unwrappedArray, function (item) {
|
|
373
|
-
return item === undefined || item === null || !unwrap(item._destroy);
|
|
374
|
-
});
|
|
375
|
-
}
|
|
376
|
-
localSetDomNodeChildrenFromArrayMapping(unwrappedArray);
|
|
377
|
-
}, null, { disposeWhenNodeIsRemoved: targetNode })
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
let templateComputedDomDataKey = domData.nextKey();
|
|
382
|
-
|
|
383
|
-
class TemplateBindingHandler extends AsyncBindingHandler {
|
|
384
|
-
constructor (params) {
|
|
385
|
-
super(params);
|
|
386
|
-
const element = this.$element;
|
|
387
|
-
const bindingValue = unwrap(this.value);
|
|
388
|
-
|
|
389
|
-
// Expose 'conditional' for `else` chaining.
|
|
390
|
-
domData.set(element, 'conditional', {
|
|
391
|
-
elseChainSatisfied: observable(true)
|
|
392
|
-
});
|
|
393
|
-
|
|
394
|
-
// Support anonymous templates
|
|
395
|
-
if (typeof bindingValue === 'string' || bindingValue.name) {
|
|
396
|
-
this.bindNamedTemplate();
|
|
397
|
-
} else if ('nodes' in bindingValue) {
|
|
398
|
-
this.bindNodeTemplate(bindingValue.nodes || []);
|
|
399
|
-
} else {
|
|
400
|
-
this.bindAnonymousTemplate();
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
bindNamedTemplate () {
|
|
405
|
-
// It's a named template - clear the element
|
|
406
|
-
virtualElements.emptyNode(this.$element);
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
// We've been given an array of DOM nodes. Save them as the template source.
|
|
410
|
-
// There is no known use case for the node array being an observable array (if the output
|
|
411
|
-
// varies, put that behavior *into* your template - that's what templates are for), and
|
|
412
|
-
// the implementation would be a mess, so assert that it's not observable.
|
|
413
|
-
bindNodeTemplate (nodes) {
|
|
414
|
-
if (isObservable(nodes)) {
|
|
415
|
-
throw new Error('The "nodes" option must be a plain, non-observable array.')
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
// If the nodes are already attached to a KO-generated container, we reuse that container without moving the
|
|
419
|
-
// elements to a new one (we check only the first node, as the nodes are always moved together)
|
|
420
|
-
let container = nodes[0] && nodes[0].parentNode;
|
|
421
|
-
if (!container || !domData.get(container, cleanContainerDomDataKey)) {
|
|
422
|
-
container = moveCleanedNodesToContainerElement(nodes);
|
|
423
|
-
domData.set(container, cleanContainerDomDataKey, true);
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
new anonymousTemplate(this.$element).nodes(container);
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
bindAnonymousTemplate () {
|
|
430
|
-
// It's an anonymous template - store the element contents, then clear the element
|
|
431
|
-
const templateNodes = virtualElements.childNodes(this.$element);
|
|
432
|
-
if (templateNodes.length === 0) {
|
|
433
|
-
throw new Error('Anonymous template defined, but no template content was provided.')
|
|
434
|
-
}
|
|
435
|
-
const container = moveCleanedNodesToContainerElement(templateNodes); // This also removes the nodes from their current parent
|
|
436
|
-
new anonymousTemplate(this.$element).nodes(container);
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
onValueChange () {
|
|
440
|
-
const element = this.$element;
|
|
441
|
-
const bindingContext$$1 = this.$context;
|
|
442
|
-
var value = this.value;
|
|
443
|
-
var options$$1 = unwrap(value);
|
|
444
|
-
var shouldDisplay = true;
|
|
445
|
-
var templateComputed = null;
|
|
446
|
-
var elseChainSatisfied = domData.get(element, 'conditional').elseChainSatisfied;
|
|
447
|
-
var templateName;
|
|
448
|
-
|
|
449
|
-
if (typeof options$$1 === 'string') {
|
|
450
|
-
templateName = value;
|
|
451
|
-
options$$1 = {};
|
|
452
|
-
} else {
|
|
453
|
-
templateName = options$$1.name;
|
|
454
|
-
|
|
455
|
-
// Support "if"/"ifnot" conditions
|
|
456
|
-
if ('if' in options$$1) {
|
|
457
|
-
shouldDisplay = unwrap(options$$1.if);
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
if (shouldDisplay && 'ifnot' in options$$1) {
|
|
461
|
-
shouldDisplay = !unwrap(options$$1.ifnot);
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
if ('foreach' in options$$1) {
|
|
466
|
-
// Render once for each data point (treating data set as empty if shouldDisplay==false)
|
|
467
|
-
var dataArray = (shouldDisplay && options$$1.foreach) || [];
|
|
468
|
-
templateComputed = renderTemplateForEach(templateName || element, dataArray, options$$1, element, bindingContext$$1, this.completeBinding);
|
|
469
|
-
|
|
470
|
-
elseChainSatisfied((unwrap(dataArray) || []).length !== 0);
|
|
471
|
-
} else if (shouldDisplay) {
|
|
472
|
-
// Render once for this single data point (or use the viewModel if no data was provided)
|
|
473
|
-
var innerBindingContext = ('data' in options$$1)
|
|
474
|
-
? bindingContext$$1.createStaticChildContext(options$$1.data, options$$1.as) // Given an explicit 'data' value, we create a child binding context for it
|
|
475
|
-
: bindingContext$$1; // Given no explicit 'data' value, we retain the same binding context
|
|
476
|
-
templateComputed = renderTemplate(templateName || element, innerBindingContext, options$$1, element, undefined, this.completeBinding);
|
|
477
|
-
elseChainSatisfied(true);
|
|
478
|
-
} else {
|
|
479
|
-
virtualElements.emptyNode(element);
|
|
480
|
-
elseChainSatisfied(false);
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
// It only makes sense to have a single template computed per element (otherwise which one should have its output displayed?)
|
|
484
|
-
this.disposeOldComputedAndStoreNewOne(element, templateComputed);
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
disposeOldComputedAndStoreNewOne (element, newComputed) {
|
|
488
|
-
let oldComputed = domData.get(element, templateComputedDomDataKey);
|
|
489
|
-
if (oldComputed && (typeof oldComputed.dispose === 'function')) { oldComputed.dispose(); }
|
|
490
|
-
domData.set(element, templateComputedDomDataKey, (newComputed && (!newComputed.isActive || newComputed.isActive())) ? newComputed : undefined);
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
get controlsDescendants () { return true }
|
|
494
|
-
static get allowVirtualElements () { return true }
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
function nativeTemplateEngine () {
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
nativeTemplateEngine.prototype = new templateEngine();
|
|
501
|
-
nativeTemplateEngine.prototype.constructor = nativeTemplateEngine;
|
|
502
|
-
nativeTemplateEngine.prototype.renderTemplateSource = function (templateSource, bindingContext$$1, options$$1, templateDocument) {
|
|
503
|
-
var useNodesIfAvailable = !(ieVersion < 9), // IE<9 cloneNode doesn't work properly
|
|
504
|
-
templateNodesFunc = useNodesIfAvailable ? templateSource.nodes : null,
|
|
505
|
-
templateNodes = templateNodesFunc ? templateSource.nodes() : null;
|
|
506
|
-
|
|
507
|
-
if (templateNodes) {
|
|
508
|
-
return makeArray(templateNodes.cloneNode(true).childNodes)
|
|
509
|
-
} else {
|
|
510
|
-
var templateText = templateSource.text();
|
|
511
|
-
return parseHtmlFragment(templateText, templateDocument)
|
|
512
|
-
}
|
|
513
|
-
};
|
|
514
|
-
|
|
515
|
-
nativeTemplateEngine.instance = new nativeTemplateEngine();
|
|
516
|
-
setTemplateEngine(nativeTemplateEngine.instance);
|
|
517
|
-
|
|
518
|
-
// "foreach: someExpression" is equivalent to "template: { foreach: someExpression }"
|
|
519
|
-
// "foreach: { data: someExpression, afterAdd: myfn }" is equivalent to "template: { foreach: someExpression, afterAdd: myfn }"
|
|
520
|
-
class TemplateForEachBindingHandler extends TemplateBindingHandler {
|
|
521
|
-
get value () {
|
|
522
|
-
const modelValue = this.valueAccessor();
|
|
523
|
-
const unwrappedValue = peek(modelValue); // Unwrap without setting a dependency here
|
|
524
|
-
|
|
525
|
-
// If unwrappedValue is the array, pass in the wrapped value on its own
|
|
526
|
-
// The value will be unwrapped and tracked within the template binding
|
|
527
|
-
// (See https://github.com/SteveSanderson/knockout/issues/523)
|
|
528
|
-
if (!unwrappedValue || typeof unwrappedValue.length === 'number') {
|
|
529
|
-
return { foreach: modelValue, templateEngine: nativeTemplateEngine.instance }
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
// If unwrappedValue.data is the array, preserve all relevant options and unwrap again value so we get updates
|
|
533
|
-
unwrap(modelValue);
|
|
534
|
-
return {
|
|
535
|
-
foreach: unwrappedValue.data,
|
|
536
|
-
as: unwrappedValue.as,
|
|
537
|
-
includeDestroyed: unwrappedValue.includeDestroyed,
|
|
538
|
-
afterAdd: unwrappedValue.afterAdd,
|
|
539
|
-
beforeRemove: unwrappedValue.beforeRemove,
|
|
540
|
-
afterRender: unwrappedValue.afterRender,
|
|
541
|
-
beforeMove: unwrappedValue.beforeMove,
|
|
542
|
-
afterMove: unwrappedValue.afterMove,
|
|
543
|
-
templateEngine: nativeTemplateEngine.instance
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
// 'let': letBinding,
|
|
549
|
-
// template: template,
|
|
550
|
-
|
|
551
|
-
const bindings = {
|
|
552
|
-
foreach: TemplateForEachBindingHandler,
|
|
553
|
-
template: TemplateBindingHandler
|
|
554
|
-
};
|
|
555
|
-
|
|
556
|
-
export { bindings, nativeTemplateEngine, templateEngine, setTemplateEngine, renderTemplate, TemplateBindingHandler, domElement, anonymousTemplate };
|
|
557
|
-
//# sourceMappingURL=binding.template.es6.js.map
|