knockoutjs-rails 1.2.1 → 1.03
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/lib/knockoutjs-rails/version.rb +1 -1
- data/vendor/assets/javascripts/knockout.js +1298 -76
- metadata +3 -3
data/README.md
CHANGED
@@ -20,7 +20,7 @@ Add the following directive to your Javascript manifest file (application.js):
|
|
20
20
|
|
21
21
|
## Versioning
|
22
22
|
|
23
|
-
knockoutjs-rails 1.
|
23
|
+
knockoutjs-rails 1.03 == Knockout.js 1.03
|
24
24
|
|
25
25
|
Every attempt is made to mirror the currently shipping Knockout.js version number wherever possible.
|
26
26
|
The major and minor version numbers will always represent the Knockout.js version, but the patch level
|
@@ -1,76 +1,1298 @@
|
|
1
|
-
// Knockout JavaScript library v1.
|
2
|
-
// (c) Steven Sanderson - http://knockoutjs.com/
|
3
|
-
// License:
|
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
|
-
|
1
|
+
// Knockout JavaScript library v1.03
|
2
|
+
// (c) 2010 Steven Sanderson - http://knockoutjs.com/
|
3
|
+
// License: Ms-Pl (http://www.opensource.org/licenses/ms-pl.html)
|
4
|
+
|
5
|
+
var ko = window.ko = {};
|
6
|
+
/// <reference path="namespace.js" />
|
7
|
+
|
8
|
+
ko.utils = new (function () {
|
9
|
+
var stringTrimRegex = /^(\s|\u00A0)+|(\s|\u00A0)+$/g;
|
10
|
+
|
11
|
+
return {
|
12
|
+
arrayForEach: function (array, action) {
|
13
|
+
for (var i = 0, j = array.length; i < j; i++)
|
14
|
+
action(array[i]);
|
15
|
+
},
|
16
|
+
|
17
|
+
arrayIndexOf: function (array, item) {
|
18
|
+
if (typeof array.indexOf == "function")
|
19
|
+
return array.indexOf(item);
|
20
|
+
for (var i = 0, j = array.length; i < j; i++)
|
21
|
+
if (array[i] == item)
|
22
|
+
return i;
|
23
|
+
return -1;
|
24
|
+
},
|
25
|
+
|
26
|
+
arrayFirst: function (array, predicate, predicateOwner) {
|
27
|
+
for (var i = 0, j = array.length; i < j; i++)
|
28
|
+
if (predicate.call(predicateOwner, array[i]))
|
29
|
+
return array[i];
|
30
|
+
return null;
|
31
|
+
},
|
32
|
+
|
33
|
+
arrayRemoveItem: function (array, itemToRemove) {
|
34
|
+
var index = ko.utils.arrayIndexOf(array, itemToRemove);
|
35
|
+
if (index >= 0)
|
36
|
+
array.splice(index, 1);
|
37
|
+
},
|
38
|
+
|
39
|
+
arrayGetDistinctValues: function (array) {
|
40
|
+
array = array || [];
|
41
|
+
var result = [];
|
42
|
+
for (var i = 0, j = array.length; i < j; i++) {
|
43
|
+
if (ko.utils.arrayIndexOf(result, array[i]) < 0)
|
44
|
+
result.push(array[i]);
|
45
|
+
}
|
46
|
+
return result;
|
47
|
+
},
|
48
|
+
|
49
|
+
arrayMap: function (array, mapping) {
|
50
|
+
array = array || [];
|
51
|
+
var result = [];
|
52
|
+
for (var i = 0, j = array.length; i < j; i++)
|
53
|
+
result.push(mapping(array[i]));
|
54
|
+
return result;
|
55
|
+
},
|
56
|
+
|
57
|
+
arrayFilter: function (array, predicate) {
|
58
|
+
array = array || [];
|
59
|
+
var result = [];
|
60
|
+
for (var i = 0, j = array.length; i < j; i++)
|
61
|
+
if (predicate(array[i]))
|
62
|
+
result.push(array[i]);
|
63
|
+
return result;
|
64
|
+
},
|
65
|
+
|
66
|
+
emptyDomNode: function (domNode) {
|
67
|
+
while (domNode.firstChild) {
|
68
|
+
ko.utils.domData.cleanNodeAndDescendants(domNode.firstChild);
|
69
|
+
domNode.removeChild(domNode.firstChild);
|
70
|
+
}
|
71
|
+
},
|
72
|
+
|
73
|
+
setDomNodeChildren: function (domNode, childNodes) {
|
74
|
+
ko.utils.emptyDomNode(domNode);
|
75
|
+
if (childNodes) {
|
76
|
+
ko.utils.arrayForEach(childNodes, function (childNode) {
|
77
|
+
domNode.appendChild(childNode);
|
78
|
+
});
|
79
|
+
}
|
80
|
+
},
|
81
|
+
|
82
|
+
replaceDomNodes: function (nodeToReplaceOrNodeArray, newNodesArray) {
|
83
|
+
var nodesToReplaceArray = nodeToReplaceOrNodeArray.nodeType ? [nodeToReplaceOrNodeArray] : nodeToReplaceOrNodeArray;
|
84
|
+
if (nodesToReplaceArray.length > 0) {
|
85
|
+
var insertionPoint = nodesToReplaceArray[0];
|
86
|
+
var parent = insertionPoint.parentNode;
|
87
|
+
for (var i = 0, j = newNodesArray.length; i < j; i++)
|
88
|
+
parent.insertBefore(newNodesArray[i], insertionPoint);
|
89
|
+
for (var i = 0, j = nodesToReplaceArray.length; i < j; i++) {
|
90
|
+
ko.utils.domData.cleanNodeAndDescendants(nodesToReplaceArray[i]);
|
91
|
+
parent.removeChild(nodesToReplaceArray[i]);
|
92
|
+
}
|
93
|
+
}
|
94
|
+
},
|
95
|
+
|
96
|
+
setOptionNodeSelectionState: function (optionNode, isSelected) {
|
97
|
+
// IE6 sometimes throws "unknown error" if you try to write to .selected directly, whereas Firefox struggles with setAttribute. Pick one based on browser.
|
98
|
+
if (navigator.userAgent.indexOf("MSIE 6") >= 0)
|
99
|
+
optionNode.setAttribute("selected", isSelected);
|
100
|
+
else
|
101
|
+
optionNode.selected = isSelected;
|
102
|
+
},
|
103
|
+
|
104
|
+
getElementsHavingAttribute: function (rootNode, attributeName) {
|
105
|
+
if ((!rootNode) || (rootNode.nodeType != 1)) return [];
|
106
|
+
var results = [];
|
107
|
+
if (rootNode.getAttribute(attributeName) !== null)
|
108
|
+
results.push(rootNode);
|
109
|
+
var descendants = rootNode.getElementsByTagName("*");
|
110
|
+
for (var i = 0, j = descendants.length; i < j; i++)
|
111
|
+
if (descendants[i].getAttribute(attributeName) !== null)
|
112
|
+
results.push(descendants[i]);
|
113
|
+
return results;
|
114
|
+
},
|
115
|
+
|
116
|
+
stringTrim: function (string) {
|
117
|
+
return (string || "").replace(stringTrimRegex, "");
|
118
|
+
},
|
119
|
+
|
120
|
+
stringTokenize: function (string, delimiter) {
|
121
|
+
var result = [];
|
122
|
+
var tokens = (string || "").split(delimiter);
|
123
|
+
for (var i = 0, j = tokens.length; i < j; i++) {
|
124
|
+
var trimmed = ko.utils.stringTrim(tokens[i]);
|
125
|
+
if (trimmed !== "")
|
126
|
+
result.push(trimmed);
|
127
|
+
}
|
128
|
+
return result;
|
129
|
+
},
|
130
|
+
|
131
|
+
evalWithinScope: function (expression, scope) {
|
132
|
+
if (scope === undefined)
|
133
|
+
return (new Function("return " + expression))();
|
134
|
+
with (scope) { return eval("(" + expression + ")"); }
|
135
|
+
},
|
136
|
+
|
137
|
+
domNodeIsContainedBy: function (node, containedByNode) {
|
138
|
+
if (containedByNode.compareDocumentPosition)
|
139
|
+
return (containedByNode.compareDocumentPosition(node) & 16) == 16;
|
140
|
+
while (node != null) {
|
141
|
+
if (node == containedByNode)
|
142
|
+
return true;
|
143
|
+
node = node.parentNode;
|
144
|
+
}
|
145
|
+
return false;
|
146
|
+
},
|
147
|
+
|
148
|
+
domNodeIsAttachedToDocument: function (node) {
|
149
|
+
return ko.utils.domNodeIsContainedBy(node, document);
|
150
|
+
},
|
151
|
+
|
152
|
+
registerEventHandler: function (element, eventType, handler) {
|
153
|
+
if (typeof jQuery != "undefined")
|
154
|
+
jQuery(element).bind(eventType, handler);
|
155
|
+
else if (typeof element.addEventListener == "function")
|
156
|
+
element.addEventListener(eventType, handler, false);
|
157
|
+
else if (typeof element.attachEvent != "undefined")
|
158
|
+
element.attachEvent("on" + eventType, function (event) {
|
159
|
+
handler.call(element, event);
|
160
|
+
});
|
161
|
+
else
|
162
|
+
throw new Error("Browser doesn't support addEventListener or attachEvent");
|
163
|
+
},
|
164
|
+
|
165
|
+
triggerEvent: function (element, eventType) {
|
166
|
+
if (!(element && element.nodeType))
|
167
|
+
throw new Error("element must be a DOM node when calling triggerEvent");
|
168
|
+
|
169
|
+
if (typeof element.fireEvent != "undefined") {
|
170
|
+
// Unlike other browsers, IE doesn't change the checked state of checkboxes/radiobuttons when you trigger their "click" event
|
171
|
+
// so to make it consistent, we'll do it manually here
|
172
|
+
if (eventType == "click") {
|
173
|
+
if ((element.tagName == "INPUT") && ((element.type.toLowerCase() == "checkbox") || (element.type.toLowerCase() == "radio")))
|
174
|
+
element.checked = element.checked !== true;
|
175
|
+
}
|
176
|
+
element.fireEvent("on" + eventType);
|
177
|
+
} else if (typeof document.createEvent == "function") {
|
178
|
+
if (typeof element.dispatchEvent == "function") {
|
179
|
+
var eventCategory = (eventType == "click" ? "MouseEvents" : "HTMLEvents"); // Might need to account for other event names at some point
|
180
|
+
var event = document.createEvent(eventCategory);
|
181
|
+
event.initEvent(eventType, true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, element);
|
182
|
+
element.dispatchEvent(event);
|
183
|
+
}
|
184
|
+
else
|
185
|
+
throw new Error("The supplied element doesn't support dispatchEvent");
|
186
|
+
}
|
187
|
+
else
|
188
|
+
throw new Error("Browser doesn't support triggering events");
|
189
|
+
},
|
190
|
+
|
191
|
+
unwrapObservable: function (value) {
|
192
|
+
return ko.isObservable(value) ? value() : value;
|
193
|
+
},
|
194
|
+
|
195
|
+
domNodeHasCssClass: function (node, className) {
|
196
|
+
var currentClassNames = (node.className || "").split(/\s+/);
|
197
|
+
return ko.utils.arrayIndexOf(currentClassNames, className) >= 0;
|
198
|
+
},
|
199
|
+
|
200
|
+
toggleDomNodeCssClass: function (node, className, shouldHaveClass) {
|
201
|
+
var hasClass = ko.utils.domNodeHasCssClass(node, className);
|
202
|
+
if (shouldHaveClass && !hasClass) {
|
203
|
+
node.className = (node.className || "") + " " + className;
|
204
|
+
} else if (hasClass && !shouldHaveClass) {
|
205
|
+
var currentClassNames = (node.className || "").split(/\s+/);
|
206
|
+
var newClassName = "";
|
207
|
+
for (var i = 0; i < currentClassNames.length; i++)
|
208
|
+
if (currentClassNames[i] != className)
|
209
|
+
newClassName += currentClassNames[i] + " ";
|
210
|
+
node.className = ko.utils.stringTrim(newClassName);
|
211
|
+
}
|
212
|
+
},
|
213
|
+
|
214
|
+
range: function (min, max) {
|
215
|
+
min = ko.utils.unwrapObservable(min);
|
216
|
+
max = ko.utils.unwrapObservable(max);
|
217
|
+
var result = [];
|
218
|
+
for (var i = min; i <= max; i++)
|
219
|
+
result.push(i);
|
220
|
+
return result;
|
221
|
+
},
|
222
|
+
|
223
|
+
stringifyJson: function (data) {
|
224
|
+
if ((typeof JSON == "undefined") || (typeof JSON.stringify == "undefined"))
|
225
|
+
throw new Error("Cannot find JSON.stringify(). Some browsers (e.g., IE < 8) don't support it natively, but you can overcome this by adding a script reference to json2.js, downloadable from http://www.json.org/json2.js");
|
226
|
+
return JSON.stringify(ko.utils.unwrapObservable(data));
|
227
|
+
},
|
228
|
+
|
229
|
+
postJson: function (url, data) {
|
230
|
+
data = ko.utils.unwrapObservable(data);
|
231
|
+
var form = document.createElement("FORM");
|
232
|
+
form.style.display = "none";
|
233
|
+
form.action = url;
|
234
|
+
form.method = "post";
|
235
|
+
for (var key in data) {
|
236
|
+
var input = document.createElement("INPUT");
|
237
|
+
input.name = key;
|
238
|
+
input.value = ko.utils.stringifyJson(ko.utils.unwrapObservable(data[key]));
|
239
|
+
form.appendChild(input);
|
240
|
+
}
|
241
|
+
document.body.appendChild(form);
|
242
|
+
form.submit();
|
243
|
+
setTimeout(function () { form.parentNode.removeChild(form); }, 0);
|
244
|
+
},
|
245
|
+
|
246
|
+
domData: {
|
247
|
+
uniqueId: 0,
|
248
|
+
dataStoreKeyExpandoPropertyName: "__ko__" + (new Date).getTime(),
|
249
|
+
dataStore: {},
|
250
|
+
get: function (node, key) {
|
251
|
+
var allDataForNode = ko.utils.domData.getAll(node, false);
|
252
|
+
return allDataForNode === undefined ? undefined : allDataForNode[key];
|
253
|
+
},
|
254
|
+
set: function (node, key, value) {
|
255
|
+
var allDataForNode = ko.utils.domData.getAll(node, true);
|
256
|
+
allDataForNode[key] = value;
|
257
|
+
},
|
258
|
+
getAll: function (node, createIfNotFound) {
|
259
|
+
var dataStoreKey = node[ko.utils.domData.dataStoreKeyExpandoPropertyName];
|
260
|
+
if (!dataStoreKey) {
|
261
|
+
if (!createIfNotFound)
|
262
|
+
return undefined;
|
263
|
+
dataStoreKey = node[ko.utils.domData.dataStoreKeyExpandoPropertyName] = "ko" + ko.utils.domData.uniqueId++;
|
264
|
+
ko.utils.domData[dataStoreKey] = {};
|
265
|
+
}
|
266
|
+
return ko.utils.domData[dataStoreKey];
|
267
|
+
},
|
268
|
+
cleanNode: function (node) {
|
269
|
+
var dataStoreKey = node[ko.utils.domData.dataStoreKeyExpandoPropertyName];
|
270
|
+
if (dataStoreKey) {
|
271
|
+
delete ko.utils.domData[dataStoreKey];
|
272
|
+
node[ko.utils.domData.dataStoreKeyExpandoPropertyName] = null;
|
273
|
+
}
|
274
|
+
},
|
275
|
+
cleanNodeAndDescendants: function (node) {
|
276
|
+
if ((node.nodeType != 1) && (node.nodeType != 9))
|
277
|
+
return;
|
278
|
+
ko.utils.domData.cleanNode(node);
|
279
|
+
var descendants = node.getElementsByTagName("*");
|
280
|
+
for (var i = 0, j = descendants.length; i < j; i++)
|
281
|
+
ko.utils.domData.cleanNode(descendants[i]);
|
282
|
+
}
|
283
|
+
}
|
284
|
+
}
|
285
|
+
})();
|
286
|
+
|
287
|
+
if (!Function.prototype.bind) {
|
288
|
+
// Function.prototype.bind is a standard part of ECMAScript 5th Edition (December 2009, http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf)
|
289
|
+
// In case the browser doesn't implement it natively, provide a JavaScript implementation. This implementation is based on the one in prototype.js
|
290
|
+
Function.prototype.bind = function (object) {
|
291
|
+
var originalFunction = this, args = Array.prototype.slice.call(arguments), object = args.shift();
|
292
|
+
return function () {
|
293
|
+
return originalFunction.apply(object, args.concat(Array.prototype.slice.call(arguments)));
|
294
|
+
};
|
295
|
+
};
|
296
|
+
}/// <reference path="utils.js" />
|
297
|
+
|
298
|
+
ko.memoization = (function () {
|
299
|
+
var memos = {};
|
300
|
+
|
301
|
+
function randomMax8HexChars() {
|
302
|
+
return (((1 + Math.random()) * 0x100000000) | 0).toString(16).substring(1);
|
303
|
+
}
|
304
|
+
function generateRandomId() {
|
305
|
+
return randomMax8HexChars() + randomMax8HexChars();
|
306
|
+
}
|
307
|
+
function findMemoNodes(rootNode, appendToArray) {
|
308
|
+
if (!rootNode)
|
309
|
+
return;
|
310
|
+
if (rootNode.nodeType == 8) {
|
311
|
+
var memoId = ko.memoization.parseMemoText(rootNode.nodeValue);
|
312
|
+
if (memoId != null)
|
313
|
+
appendToArray.push({ domNode: rootNode, memoId: memoId });
|
314
|
+
} else if (rootNode.nodeType == 1) {
|
315
|
+
for (var i = 0, childNodes = rootNode.childNodes, j = childNodes.length; i < j; i++)
|
316
|
+
findMemoNodes(childNodes[i], appendToArray);
|
317
|
+
}
|
318
|
+
}
|
319
|
+
|
320
|
+
return {
|
321
|
+
memoize: function (callback) {
|
322
|
+
if (typeof callback != "function")
|
323
|
+
throw new Error("You can only pass a function to ko.memoization.memoize()");
|
324
|
+
var memoId = generateRandomId();
|
325
|
+
memos[memoId] = callback;
|
326
|
+
return "<!--[ko_memo:" + memoId + "]-->";
|
327
|
+
},
|
328
|
+
|
329
|
+
unmemoize: function (memoId, callbackParams) {
|
330
|
+
var callback = memos[memoId];
|
331
|
+
if (callback === undefined)
|
332
|
+
throw new Error("Couldn't find any memo with ID " + memoId + ". Perhaps it's already been unmemoized.");
|
333
|
+
try {
|
334
|
+
callback.apply(null, callbackParams || []);
|
335
|
+
return true;
|
336
|
+
}
|
337
|
+
finally { delete memos[memoId]; }
|
338
|
+
},
|
339
|
+
|
340
|
+
unmemoizeDomNodeAndDescendants: function (domNode) {
|
341
|
+
var memos = [];
|
342
|
+
findMemoNodes(domNode, memos);
|
343
|
+
for (var i = 0, j = memos.length; i < j; i++) {
|
344
|
+
var node = memos[i].domNode;
|
345
|
+
ko.memoization.unmemoize(memos[i].memoId, [node]);
|
346
|
+
node.nodeValue = ""; // Neuter this node so we don't try to unmemoize it again
|
347
|
+
if (node.parentNode)
|
348
|
+
node.parentNode.removeChild(node); // If possible, erase it totally (not always possible - someone else might just hold a reference to it then call unmemoizeDomNodeAndDescendants again)
|
349
|
+
}
|
350
|
+
},
|
351
|
+
|
352
|
+
parseMemoText: function (memoText) {
|
353
|
+
var match = memoText.match(/^\[ko_memo\:(.*?)\]$/);
|
354
|
+
return match ? match[1] : null;
|
355
|
+
}
|
356
|
+
};
|
357
|
+
})();/// <reference path="../utils.js" />
|
358
|
+
|
359
|
+
ko.subscription = function (callback, disposeCallback) {
|
360
|
+
this.callback = callback;
|
361
|
+
this.dispose = disposeCallback;
|
362
|
+
};
|
363
|
+
|
364
|
+
ko.subscribable = function () {
|
365
|
+
var _subscriptions = [];
|
366
|
+
|
367
|
+
this.subscribe = function (callback, callbackTarget) {
|
368
|
+
var boundCallback = callbackTarget ? function () { callback.call(callbackTarget) } : callback;
|
369
|
+
|
370
|
+
var subscription = new ko.subscription(boundCallback, function () {
|
371
|
+
ko.utils.arrayRemoveItem(_subscriptions, subscription);
|
372
|
+
});
|
373
|
+
_subscriptions.push(subscription);
|
374
|
+
return subscription;
|
375
|
+
};
|
376
|
+
|
377
|
+
this.notifySubscribers = function (valueToNotify) {
|
378
|
+
ko.utils.arrayForEach(_subscriptions.slice(0), function (subscription) {
|
379
|
+
if (subscription)
|
380
|
+
subscription.callback(valueToNotify);
|
381
|
+
});
|
382
|
+
};
|
383
|
+
|
384
|
+
this.getSubscriptionsCount = function () {
|
385
|
+
return _subscriptions.length;
|
386
|
+
};
|
387
|
+
}
|
388
|
+
|
389
|
+
ko.isSubscribable = function (instance) {
|
390
|
+
return typeof instance.subscribe == "function" && typeof instance.notifySubscribers == "function";
|
391
|
+
};/// <reference path="subscribable.js" />
|
392
|
+
|
393
|
+
ko.dependencyDetection = (function () {
|
394
|
+
var _detectedDependencies = [];
|
395
|
+
|
396
|
+
return {
|
397
|
+
begin: function () {
|
398
|
+
_detectedDependencies.push([]);
|
399
|
+
},
|
400
|
+
|
401
|
+
end: function () {
|
402
|
+
return _detectedDependencies.pop();
|
403
|
+
},
|
404
|
+
|
405
|
+
registerDependency: function (subscribable) {
|
406
|
+
if (!ko.isSubscribable(subscribable))
|
407
|
+
throw "Only subscribable things can act as dependencies";
|
408
|
+
if (_detectedDependencies.length > 0) {
|
409
|
+
_detectedDependencies[_detectedDependencies.length - 1].push(subscribable);
|
410
|
+
}
|
411
|
+
}
|
412
|
+
};
|
413
|
+
})();/// <reference path="dependencyDetection.js" />
|
414
|
+
|
415
|
+
ko.observable = function (initialValue) {
|
416
|
+
var _latestValue = initialValue;
|
417
|
+
|
418
|
+
function observable(newValue) {
|
419
|
+
if (arguments.length > 0) {
|
420
|
+
_latestValue = newValue;
|
421
|
+
observable.notifySubscribers(_latestValue);
|
422
|
+
}
|
423
|
+
else // The caller only needs to be notified of changes if they did a "read" operation
|
424
|
+
ko.dependencyDetection.registerDependency(observable);
|
425
|
+
|
426
|
+
return _latestValue;
|
427
|
+
}
|
428
|
+
observable.__ko_proto__ = ko.observable;
|
429
|
+
observable.valueHasMutated = function () { observable.notifySubscribers(_latestValue); }
|
430
|
+
|
431
|
+
ko.subscribable.call(observable);
|
432
|
+
return observable;
|
433
|
+
}
|
434
|
+
ko.isObservable = function (instance) {
|
435
|
+
if ((instance === null) || (instance === undefined) || (instance.__ko_proto__ === undefined)) return false;
|
436
|
+
if (instance.__ko_proto__ === ko.observable) return true;
|
437
|
+
return ko.isObservable(instance.__ko_proto__); // Walk the prototype chain
|
438
|
+
}
|
439
|
+
ko.isWriteableObservable = function (instance) {
|
440
|
+
return (typeof instance == "function") && instance.__ko_proto__ === ko.observable;
|
441
|
+
}/// <reference path="observable.js" />
|
442
|
+
|
443
|
+
ko.observableArray = function (initialValues) {
|
444
|
+
var result = new ko.observable(initialValues);
|
445
|
+
|
446
|
+
ko.utils.arrayForEach(["pop", "push", "reverse", "shift", "sort", "splice", "unshift"], function (methodName) {
|
447
|
+
result[methodName] = function () {
|
448
|
+
var underlyingArray = result();
|
449
|
+
var methodCallResult = underlyingArray[methodName].apply(underlyingArray, arguments);
|
450
|
+
result.valueHasMutated();
|
451
|
+
return methodCallResult;
|
452
|
+
};
|
453
|
+
});
|
454
|
+
|
455
|
+
ko.utils.arrayForEach(["slice"], function (methodName) {
|
456
|
+
result[methodName] = function () {
|
457
|
+
var underlyingArray = result();
|
458
|
+
return underlyingArray[methodName].apply(underlyingArray, arguments);
|
459
|
+
};
|
460
|
+
});
|
461
|
+
|
462
|
+
result.remove = function (valueOrPredicate) {
|
463
|
+
var underlyingArray = result();
|
464
|
+
var remainingValues = [];
|
465
|
+
var removedValues = [];
|
466
|
+
var predicate = typeof valueOrPredicate == "function" ? valueOrPredicate : function (value) { return value === valueOrPredicate; };
|
467
|
+
for (var i = 0, j = underlyingArray.length; i < j; i++) {
|
468
|
+
var value = underlyingArray[i];
|
469
|
+
if (!predicate(value))
|
470
|
+
remainingValues.push(value);
|
471
|
+
else
|
472
|
+
removedValues.push(value);
|
473
|
+
}
|
474
|
+
result(remainingValues);
|
475
|
+
return removedValues;
|
476
|
+
};
|
477
|
+
|
478
|
+
result.removeAll = function (arrayOfValues) {
|
479
|
+
if (!arrayOfValues)
|
480
|
+
return [];
|
481
|
+
return result.remove(function (value) {
|
482
|
+
return ko.utils.arrayIndexOf(arrayOfValues, value) >= 0;
|
483
|
+
});
|
484
|
+
};
|
485
|
+
|
486
|
+
result.indexOf = function (item) {
|
487
|
+
var underlyingArray = result();
|
488
|
+
return ko.utils.arrayIndexOf(underlyingArray, item);
|
489
|
+
};
|
490
|
+
|
491
|
+
return result;
|
492
|
+
}/// <reference path="observable.js" />
|
493
|
+
|
494
|
+
ko.dependentObservable = function (evaluatorFunction, evaluatorFunctionTarget, options) {
|
495
|
+
if (typeof evaluatorFunction != "function")
|
496
|
+
throw "Pass a function that returns the value of the dependentObservable";
|
497
|
+
|
498
|
+
var _subscriptionsToDependencies = [];
|
499
|
+
function disposeAllSubscriptionsToDependencies() {
|
500
|
+
ko.utils.arrayForEach(_subscriptionsToDependencies, function (subscription) {
|
501
|
+
subscription.dispose();
|
502
|
+
});
|
503
|
+
_subscriptionsToDependencies = [];
|
504
|
+
}
|
505
|
+
|
506
|
+
function replaceSubscriptionsToDependencies(newDependencies) {
|
507
|
+
disposeAllSubscriptionsToDependencies();
|
508
|
+
ko.utils.arrayForEach(newDependencies, function (dependency) {
|
509
|
+
_subscriptionsToDependencies.push(dependency.subscribe(evaluate));
|
510
|
+
});
|
511
|
+
};
|
512
|
+
|
513
|
+
var _latestValue, _isFirstEvaluation = true;
|
514
|
+
function evaluate() {
|
515
|
+
if ((!_isFirstEvaluation) && options && typeof options.disposeWhen == "function") {
|
516
|
+
if (options.disposeWhen()) {
|
517
|
+
dependentObservable.dispose();
|
518
|
+
return;
|
519
|
+
}
|
520
|
+
}
|
521
|
+
|
522
|
+
try {
|
523
|
+
ko.dependencyDetection.begin();
|
524
|
+
_latestValue = evaluatorFunctionTarget ? evaluatorFunction.call(evaluatorFunctionTarget) : evaluatorFunction();
|
525
|
+
} catch (ex) {
|
526
|
+
throw ex;
|
527
|
+
} finally {
|
528
|
+
var distinctDependencies = ko.utils.arrayGetDistinctValues(ko.dependencyDetection.end());
|
529
|
+
replaceSubscriptionsToDependencies(distinctDependencies);
|
530
|
+
}
|
531
|
+
|
532
|
+
dependentObservable.notifySubscribers(_latestValue);
|
533
|
+
_isFirstEvaluation = false;
|
534
|
+
}
|
535
|
+
|
536
|
+
function dependentObservable() {
|
537
|
+
if (arguments.length > 0)
|
538
|
+
throw "Cannot write a value to a dependentObservable. Do not pass any parameters to it";
|
539
|
+
|
540
|
+
ko.dependencyDetection.registerDependency(dependentObservable);
|
541
|
+
return _latestValue;
|
542
|
+
}
|
543
|
+
dependentObservable.__ko_proto__ = ko.dependentObservable;
|
544
|
+
dependentObservable.getDependenciesCount = function () { return _subscriptionsToDependencies.length; }
|
545
|
+
dependentObservable.dispose = function () {
|
546
|
+
disposeAllSubscriptionsToDependencies();
|
547
|
+
};
|
548
|
+
|
549
|
+
ko.subscribable.call(dependentObservable);
|
550
|
+
evaluate();
|
551
|
+
return dependentObservable;
|
552
|
+
};
|
553
|
+
ko.dependentObservable.__ko_proto__ = ko.observable;/// <reference path="../utils.js" />
|
554
|
+
|
555
|
+
ko.jsonExpressionRewriting = (function () {
|
556
|
+
var restoreCapturedTokensRegex = /\[ko_token_(\d+)\]/g;
|
557
|
+
var javaScriptAssignmentTarget = /^[\_$a-z][\_$a-z]*(\[.*?\])*(\.[\_$a-z][\_$a-z]*(\[.*?\])*)*$/i;
|
558
|
+
var javaScriptReservedWords = ["true", "false"];
|
559
|
+
|
560
|
+
function restoreTokens(string, tokens) {
|
561
|
+
return string.replace(restoreCapturedTokensRegex, function (match, tokenIndex) {
|
562
|
+
return tokens[tokenIndex];
|
563
|
+
});
|
564
|
+
}
|
565
|
+
|
566
|
+
function isWriteableValue(expression) {
|
567
|
+
if (ko.utils.arrayIndexOf(javaScriptReservedWords, ko.utils.stringTrim(expression).toLowerCase()) >= 0)
|
568
|
+
return false;
|
569
|
+
return expression.match(javaScriptAssignmentTarget) !== null;
|
570
|
+
}
|
571
|
+
|
572
|
+
return {
|
573
|
+
parseJson: function (jsonString) {
|
574
|
+
jsonString = ko.utils.stringTrim(jsonString);
|
575
|
+
if (jsonString.length < 3)
|
576
|
+
return {};
|
577
|
+
|
578
|
+
// We're going to split on commas, so first extract any blocks that may contain commas other than those at the top level
|
579
|
+
var tokens = [];
|
580
|
+
var tokenStart = null, tokenEndChar;
|
581
|
+
for (var position = jsonString.charAt(0) == "{" ? 1 : 0; position < jsonString.length; position++) {
|
582
|
+
var c = jsonString.charAt(position);
|
583
|
+
if (tokenStart === null) {
|
584
|
+
switch (c) {
|
585
|
+
case '"':
|
586
|
+
case "'":
|
587
|
+
case "/":
|
588
|
+
tokenStart = position;
|
589
|
+
tokenEndChar = c;
|
590
|
+
break;
|
591
|
+
case "{":
|
592
|
+
tokenStart = position;
|
593
|
+
tokenEndChar = "}";
|
594
|
+
break;
|
595
|
+
case "[":
|
596
|
+
tokenStart = position;
|
597
|
+
tokenEndChar = "]";
|
598
|
+
break;
|
599
|
+
}
|
600
|
+
} else if (c == tokenEndChar) {
|
601
|
+
var token = jsonString.substring(tokenStart, position + 1);
|
602
|
+
tokens.push(token);
|
603
|
+
var replacement = "[ko_token_" + (tokens.length - 1) + "]";
|
604
|
+
jsonString = jsonString.substring(0, tokenStart) + replacement + jsonString.substring(position + 1);
|
605
|
+
position -= (token.length - replacement.length);
|
606
|
+
tokenStart = null;
|
607
|
+
}
|
608
|
+
}
|
609
|
+
|
610
|
+
// Now we can safely split on commas to get the key/value pairs
|
611
|
+
var result = {};
|
612
|
+
var keyValuePairs = jsonString.split(",");
|
613
|
+
for (var i = 0, j = keyValuePairs.length; i < j; i++) {
|
614
|
+
var pair = keyValuePairs[i];
|
615
|
+
var colonPos = pair.indexOf(":");
|
616
|
+
if ((colonPos > 0) && (colonPos < pair.length - 1)) {
|
617
|
+
var key = ko.utils.stringTrim(pair.substring(0, colonPos));
|
618
|
+
var value = ko.utils.stringTrim(pair.substring(colonPos + 1));
|
619
|
+
if (key.charAt(0) == "{")
|
620
|
+
key = key.substring(1);
|
621
|
+
if (value.charAt(value.length - 1) == "}")
|
622
|
+
value = value.substring(0, value.length - 1);
|
623
|
+
key = ko.utils.stringTrim(restoreTokens(key, tokens));
|
624
|
+
value = ko.utils.stringTrim(restoreTokens(value, tokens));
|
625
|
+
result[key] = value;
|
626
|
+
}
|
627
|
+
}
|
628
|
+
return result;
|
629
|
+
},
|
630
|
+
|
631
|
+
insertPropertyAccessorsIntoJson: function (jsonString) {
|
632
|
+
var parsed = ko.jsonExpressionRewriting.parseJson(jsonString);
|
633
|
+
var propertyAccessorTokens = [];
|
634
|
+
for (var key in parsed) {
|
635
|
+
var value = parsed[key];
|
636
|
+
if (isWriteableValue(value)) {
|
637
|
+
if (propertyAccessorTokens.length > 0)
|
638
|
+
propertyAccessorTokens.push(", ");
|
639
|
+
propertyAccessorTokens.push(key + " : function(__ko_value) { " + value + " = __ko_value; }");
|
640
|
+
}
|
641
|
+
}
|
642
|
+
|
643
|
+
if (propertyAccessorTokens.length > 0) {
|
644
|
+
var allPropertyAccessors = propertyAccessorTokens.join("");
|
645
|
+
jsonString = jsonString + ", '_ko_property_writers' : { " + allPropertyAccessors + " } ";
|
646
|
+
}
|
647
|
+
|
648
|
+
return jsonString;
|
649
|
+
}
|
650
|
+
};
|
651
|
+
})();/// <reference path="../subscribables/dependentObservable.js" />
|
652
|
+
|
653
|
+
(function () {
|
654
|
+
var bindingAttributeName = "data-bind";
|
655
|
+
ko.bindingHandlers = {};
|
656
|
+
|
657
|
+
function parseBindingAttribute(attributeText, viewModel) {
|
658
|
+
try {
|
659
|
+
var json = " { " + ko.jsonExpressionRewriting.insertPropertyAccessorsIntoJson(attributeText) + " } ";
|
660
|
+
return ko.utils.evalWithinScope(json, viewModel === null ? window : viewModel);
|
661
|
+
} catch (ex) {
|
662
|
+
throw new Error("Unable to parse binding attribute.\nMessage: " + ex + ";\nAttribute value: " + attributeText);
|
663
|
+
}
|
664
|
+
}
|
665
|
+
|
666
|
+
function invokeBindingHandler(handler, element, dataValue, allBindings, viewModel) {
|
667
|
+
handler(element, dataValue, allBindings, viewModel);
|
668
|
+
}
|
669
|
+
|
670
|
+
ko.applyBindingsToNode = function (node, bindings, viewModel) {
|
671
|
+
var isFirstEvaluation = true;
|
672
|
+
new ko.dependentObservable(
|
673
|
+
function () {
|
674
|
+
var evaluatedBindings = (typeof bindings == "function") ? bindings() : bindings;
|
675
|
+
var parsedBindings = evaluatedBindings || parseBindingAttribute(node.getAttribute(bindingAttributeName), viewModel);
|
676
|
+
for (var bindingKey in parsedBindings) {
|
677
|
+
if (ko.bindingHandlers[bindingKey]) {
|
678
|
+
if (isFirstEvaluation && typeof ko.bindingHandlers[bindingKey].init == "function")
|
679
|
+
invokeBindingHandler(ko.bindingHandlers[bindingKey].init, node, parsedBindings[bindingKey], parsedBindings, viewModel);
|
680
|
+
if (typeof ko.bindingHandlers[bindingKey].update == "function")
|
681
|
+
invokeBindingHandler(ko.bindingHandlers[bindingKey].update, node, parsedBindings[bindingKey], parsedBindings, viewModel);
|
682
|
+
}
|
683
|
+
}
|
684
|
+
},
|
685
|
+
null,
|
686
|
+
{ disposeWhen: function () { return !ko.utils.domNodeIsAttachedToDocument(node); } }
|
687
|
+
);
|
688
|
+
isFirstEvaluation = false;
|
689
|
+
};
|
690
|
+
|
691
|
+
ko.applyBindings = function (rootNode, viewModel) {
|
692
|
+
var elemsWithBindingAttribute = ko.utils.getElementsHavingAttribute(rootNode, bindingAttributeName);
|
693
|
+
ko.utils.arrayForEach(elemsWithBindingAttribute, function (element) {
|
694
|
+
ko.applyBindingsToNode(element, null, viewModel);
|
695
|
+
});
|
696
|
+
};
|
697
|
+
})();/// <reference path="bindingAttributeSyntax.js" />
|
698
|
+
|
699
|
+
ko.bindingHandlers.click = {
|
700
|
+
init: function (element, value, allBindings, viewModel) {
|
701
|
+
ko.utils.registerEventHandler(element, "click", function (event) {
|
702
|
+
try { value.call(viewModel); }
|
703
|
+
finally {
|
704
|
+
if (event.preventDefault)
|
705
|
+
event.preventDefault();
|
706
|
+
else
|
707
|
+
event.returnValue = false;
|
708
|
+
}
|
709
|
+
});
|
710
|
+
}
|
711
|
+
};
|
712
|
+
|
713
|
+
ko.bindingHandlers.submit = {
|
714
|
+
init: function (element, value, allBindings, viewModel) {
|
715
|
+
if (typeof value != "function")
|
716
|
+
throw new Error("The value for a submit binding must be a function to invoke on submit");
|
717
|
+
ko.utils.registerEventHandler(element, "submit", function (event) {
|
718
|
+
try { value.call(viewModel); }
|
719
|
+
finally {
|
720
|
+
if (event.preventDefault)
|
721
|
+
event.preventDefault();
|
722
|
+
else
|
723
|
+
event.returnValue = false;
|
724
|
+
}
|
725
|
+
});
|
726
|
+
}
|
727
|
+
};
|
728
|
+
|
729
|
+
ko.bindingHandlers.visible = {
|
730
|
+
update: function (element, value) {
|
731
|
+
value = ko.utils.unwrapObservable(value);
|
732
|
+
var isCurrentlyVisible = !(element.style.display == "none");
|
733
|
+
if (value && !isCurrentlyVisible)
|
734
|
+
element.style.display = "";
|
735
|
+
else if ((!value) && isCurrentlyVisible)
|
736
|
+
element.style.display = "none";
|
737
|
+
}
|
738
|
+
}
|
739
|
+
|
740
|
+
ko.bindingHandlers.enable = {
|
741
|
+
update: function (element, value) {
|
742
|
+
value = ko.utils.unwrapObservable(value);
|
743
|
+
if (value && element.disabled)
|
744
|
+
element.removeAttribute("disabled");
|
745
|
+
else if ((!value) && (!element.disabled))
|
746
|
+
element.disabled = true;
|
747
|
+
}
|
748
|
+
};
|
749
|
+
|
750
|
+
ko.bindingHandlers.disable = { update: function (element, value) { ko.bindingHandlers.enable.update(element, !ko.utils.unwrapObservable(value)); } };
|
751
|
+
|
752
|
+
ko.bindingHandlers.value = {
|
753
|
+
init: function (element, value, allBindings) {
|
754
|
+
var eventName = allBindings.valueUpdate || "change";
|
755
|
+
if (ko.isWriteableObservable(value))
|
756
|
+
ko.utils.registerEventHandler(element, eventName, function () { value(this.value); });
|
757
|
+
else if (allBindings._ko_property_writers && allBindings._ko_property_writers.value)
|
758
|
+
ko.utils.registerEventHandler(element, eventName, function () { allBindings._ko_property_writers.value(this.value); });
|
759
|
+
},
|
760
|
+
update: function (element, value) {
|
761
|
+
var newValue = ko.utils.unwrapObservable(value);
|
762
|
+
|
763
|
+
if (newValue != element.value) {
|
764
|
+
var applyValueAction = function () { element.value = newValue; };
|
765
|
+
applyValueAction();
|
766
|
+
|
767
|
+
// Workaround for IE6 bug: It won't reliably apply values to SELECT nodes during the same execution thread
|
768
|
+
// right after you've changed the set of OPTION nodes on it. So for that node type, we'll schedule a second thread
|
769
|
+
// to apply the value as well.
|
770
|
+
var alsoApplyAsynchronously = element.tagName == "SELECT";
|
771
|
+
if (alsoApplyAsynchronously)
|
772
|
+
setTimeout(applyValueAction, 0);
|
773
|
+
}
|
774
|
+
}
|
775
|
+
};
|
776
|
+
|
777
|
+
ko.bindingHandlers.options = {
|
778
|
+
update: function (element, value, allBindings) {
|
779
|
+
|
780
|
+
if (element.tagName != "SELECT")
|
781
|
+
throw new Error("values binding applies only to SELECT elements");
|
782
|
+
|
783
|
+
var previousSelectedValues = ko.utils.arrayMap(ko.utils.arrayFilter(element.childNodes, function (node) {
|
784
|
+
return node.tagName && node.tagName == "OPTION" && node.selected;
|
785
|
+
}), function (node) {
|
786
|
+
return node.value || node.innerText || node.textContent;
|
787
|
+
});
|
788
|
+
|
789
|
+
value = ko.utils.unwrapObservable(value);
|
790
|
+
var selectedValue = element.value;
|
791
|
+
ko.utils.emptyDomNode(element);
|
792
|
+
|
793
|
+
if (value) {
|
794
|
+
if (typeof value.length != "number")
|
795
|
+
value = [value];
|
796
|
+
for (var i = 0, j = value.length; i < j; i++) {
|
797
|
+
var option = document.createElement("OPTION");
|
798
|
+
var optionValue = typeof allBindings.options_value == "string" ? value[i][allBindings.options_value] : value[i];
|
799
|
+
option.value = optionValue.toString();
|
800
|
+
option.innerHTML = (typeof allBindings.options_text == "string" ? value[i][allBindings.options_text] : optionValue).toString();
|
801
|
+
element.appendChild(option);
|
802
|
+
}
|
803
|
+
|
804
|
+
// IE6 doesn't like us to assign selection to OPTION nodes before they're added to the document.
|
805
|
+
// That's why we first added them without selection. Now it's time to set the selection.
|
806
|
+
var newOptions = element.getElementsByTagName("OPTION");
|
807
|
+
for (var i = 0, j = newOptions.length; i < j; i++) {
|
808
|
+
if (ko.utils.arrayIndexOf(previousSelectedValues, newOptions[i].value) >= 0)
|
809
|
+
ko.utils.setOptionNodeSelectionState(newOptions[i], true);
|
810
|
+
}
|
811
|
+
}
|
812
|
+
}
|
813
|
+
};
|
814
|
+
|
815
|
+
ko.bindingHandlers.selectedOptions = {
|
816
|
+
getSelectedValuesFromSelectNode: function (selectNode) {
|
817
|
+
var result = [];
|
818
|
+
var nodes = selectNode.childNodes;
|
819
|
+
for (var i = 0, j = nodes.length; i < j; i++) {
|
820
|
+
var node = nodes[i];
|
821
|
+
if ((node.tagName == "OPTION") && node.selected)
|
822
|
+
result.push(node.value);
|
823
|
+
}
|
824
|
+
return result;
|
825
|
+
},
|
826
|
+
init: function (element, value, allBindings) {
|
827
|
+
|
828
|
+
if (ko.isWriteableObservable(value))
|
829
|
+
ko.utils.registerEventHandler(element, "change", function () { value(ko.bindingHandlers.selectedOptions.getSelectedValuesFromSelectNode(this)); });
|
830
|
+
else if (allBindings._ko_property_writers && allBindings._ko_property_writers.value)
|
831
|
+
ko.utils.registerEventHandler(element, "change", function () { allBindings._ko_property_writers.value(ko.bindingHandlers.selectedOptions.getSelectedValuesFromSelectNode(this)); });
|
832
|
+
},
|
833
|
+
update: function (element, value) {
|
834
|
+
if (element.tagName != "SELECT")
|
835
|
+
throw new Error("values binding applies only to SELECT elements");
|
836
|
+
|
837
|
+
var newValue = ko.utils.unwrapObservable(value);
|
838
|
+
if (newValue && typeof newValue.length == "number") {
|
839
|
+
var nodes = element.childNodes;
|
840
|
+
for (var i = 0, j = nodes.length; i < j; i++) {
|
841
|
+
var node = nodes[i];
|
842
|
+
if (node.tagName == "OPTION")
|
843
|
+
ko.utils.setOptionNodeSelectionState(node, ko.utils.arrayIndexOf(newValue, node.value) >= 0);
|
844
|
+
}
|
845
|
+
}
|
846
|
+
}
|
847
|
+
};
|
848
|
+
|
849
|
+
ko.bindingHandlers.text = {
|
850
|
+
update: function (element, value) {
|
851
|
+
value = ko.utils.unwrapObservable(value);
|
852
|
+
typeof element.innerText == "string" ? element.innerText = value
|
853
|
+
: element.textContent = value;
|
854
|
+
}
|
855
|
+
};
|
856
|
+
|
857
|
+
ko.bindingHandlers.css = {
|
858
|
+
update: function (element, value) {
|
859
|
+
value = value || {};
|
860
|
+
for (var className in value) {
|
861
|
+
if (typeof className == "string") {
|
862
|
+
var shouldHaveClass = ko.utils.unwrapObservable(value[className]);
|
863
|
+
ko.utils.toggleDomNodeCssClass(element, className, shouldHaveClass);
|
864
|
+
}
|
865
|
+
}
|
866
|
+
}
|
867
|
+
};
|
868
|
+
|
869
|
+
ko.bindingHandlers.style = {
|
870
|
+
update: function (element, value) {
|
871
|
+
value = ko.utils.unwrapObservable(value || {});
|
872
|
+
for (var styleName in value) {
|
873
|
+
if (typeof styleName == "string") {
|
874
|
+
var styleValue = ko.utils.unwrapObservable(value[styleName]);
|
875
|
+
element.style[styleName] = styleValue || ""; // Empty string removes the value, whereas null/undefined have no effect
|
876
|
+
}
|
877
|
+
}
|
878
|
+
}
|
879
|
+
};
|
880
|
+
|
881
|
+
ko.bindingHandlers.uniqueName = {
|
882
|
+
init: function (element, value) {
|
883
|
+
if (value) {
|
884
|
+
element.name = "ko_unique_" + (++ko.bindingHandlers.uniqueName.currentIndex);
|
885
|
+
|
886
|
+
// Workaround IE 6 issue - http://www.matts411.com/post/setting_the_name_attribute_in_ie_dom/
|
887
|
+
if (/MSIE 6/i.test(navigator.userAgent))
|
888
|
+
element.mergeAttributes(document.createElement("<INPUT name='" + element.name + "'/>"), false);
|
889
|
+
}
|
890
|
+
}
|
891
|
+
};
|
892
|
+
ko.bindingHandlers.uniqueName.currentIndex = 0;
|
893
|
+
|
894
|
+
ko.bindingHandlers.checked = {
|
895
|
+
init: function (element, value, allBindings) {
|
896
|
+
if (ko.isWriteableObservable(value)) {
|
897
|
+
var updateHandler;
|
898
|
+
if (element.type == "checkbox")
|
899
|
+
updateHandler = function () { value(this.checked) };
|
900
|
+
else if (element.type == "radio")
|
901
|
+
updateHandler = function () { if (this.checked) value(this.value) };
|
902
|
+
if (updateHandler) {
|
903
|
+
ko.utils.registerEventHandler(element, "change", updateHandler);
|
904
|
+
ko.utils.registerEventHandler(element, "click", updateHandler);
|
905
|
+
}
|
906
|
+
} else if (allBindings._ko_property_writers && allBindings._ko_property_writers.checked) {
|
907
|
+
var updateHandler;
|
908
|
+
if (element.type == "checkbox")
|
909
|
+
updateHandler = function () { allBindings._ko_property_writers.checked(this.checked) };
|
910
|
+
else if (element.type == "radio")
|
911
|
+
updateHandler = function () { if (this.checked) allBindings._ko_property_writers.checked(this.value) };
|
912
|
+
if (updateHandler) {
|
913
|
+
ko.utils.registerEventHandler(element, "change", updateHandler);
|
914
|
+
ko.utils.registerEventHandler(element, "click", updateHandler);
|
915
|
+
}
|
916
|
+
}
|
917
|
+
|
918
|
+
// IE 6 won't allow radio buttons to be selected unless they have a name
|
919
|
+
if ((element.type == "radio") && !element.name)
|
920
|
+
ko.bindingHandlers.uniqueName.init(element, true);
|
921
|
+
},
|
922
|
+
update: function (element, value) {
|
923
|
+
value = ko.utils.unwrapObservable(value);
|
924
|
+
if (element.type == "checkbox")
|
925
|
+
element.checked = value;
|
926
|
+
else if (element.type == "radio")
|
927
|
+
element.checked = (element.value == value);
|
928
|
+
}
|
929
|
+
};/// <reference path="../utils.js" />
|
930
|
+
|
931
|
+
ko.templateEngine = function () {
|
932
|
+
this.renderTemplate = function (templateName, data, options) {
|
933
|
+
throw "Override renderTemplate in your ko.templateEngine subclass";
|
934
|
+
},
|
935
|
+
this.isTemplateRewritten = function (templateName) {
|
936
|
+
throw "Override isTemplateRewritten in your ko.templateEngine subclass";
|
937
|
+
},
|
938
|
+
this.rewriteTemplate = function (templateName, rewriterCallback) {
|
939
|
+
throw "Override rewriteTemplate in your ko.templateEngine subclass";
|
940
|
+
},
|
941
|
+
this.createJavaScriptEvaluatorBlock = function (script) {
|
942
|
+
throw "Override createJavaScriptEvaluatorBlock in your ko.templateEngine subclass";
|
943
|
+
}
|
944
|
+
};/// <reference path="templateEngine.js" />
|
945
|
+
|
946
|
+
ko.templateRewriting = (function () {
|
947
|
+
var memoizeBindingAttributeSyntaxRegex = /(<[a-z]+(\s+(?!data-bind=)[a-z0-9]+(=(\"[^\"]*\"|\'[^\']*\'))?)*\s+)data-bind=(["'])(.*?)\5/g;
|
948
|
+
|
949
|
+
return {
|
950
|
+
ensureTemplateIsRewritten: function (template, templateEngine) {
|
951
|
+
if (!templateEngine.isTemplateRewritten(template))
|
952
|
+
templateEngine.rewriteTemplate(template, function (htmlString) {
|
953
|
+
return ko.templateRewriting.memoizeBindingAttributeSyntax(htmlString, templateEngine);
|
954
|
+
});
|
955
|
+
},
|
956
|
+
|
957
|
+
memoizeBindingAttributeSyntax: function (htmlString, templateEngine) {
|
958
|
+
return htmlString.replace(memoizeBindingAttributeSyntaxRegex, function () {
|
959
|
+
var tagToRetain = arguments[1];
|
960
|
+
var dataBindAttributeValue = arguments[6];
|
961
|
+
|
962
|
+
dataBindAttributeValue = ko.jsonExpressionRewriting.insertPropertyAccessorsIntoJson(dataBindAttributeValue);
|
963
|
+
|
964
|
+
// For no obvious reason, Opera fails to evaluate dataBindAttributeValue unless it's wrapped in an additional anonymous function,
|
965
|
+
// even though Opera's built-in debugger can evaluate it anyway. No other browser requires this extra indirection.
|
966
|
+
var applyBindingsToNextSiblingScript = "ko.templateRewriting.applyMemoizedBindingsToNextSibling(function() { \
|
967
|
+
return (function() { return { " + dataBindAttributeValue + " } })() \
|
968
|
+
})";
|
969
|
+
return templateEngine.createJavaScriptEvaluatorBlock(applyBindingsToNextSiblingScript) + tagToRetain;
|
970
|
+
});
|
971
|
+
},
|
972
|
+
|
973
|
+
applyMemoizedBindingsToNextSibling: function (bindings) {
|
974
|
+
return ko.memoization.memoize(function (domNode) {
|
975
|
+
if (domNode.nextSibling)
|
976
|
+
ko.applyBindingsToNode(domNode.nextSibling, bindings, null);
|
977
|
+
});
|
978
|
+
}
|
979
|
+
}
|
980
|
+
})();/// <reference path="templating.js" />
|
981
|
+
/// <reference path="../subscribables/dependentObservable.js" />
|
982
|
+
|
983
|
+
(function () {
|
984
|
+
var _templateEngine;
|
985
|
+
ko.setTemplateEngine = function (templateEngine) {
|
986
|
+
if ((templateEngine != undefined) && !(templateEngine instanceof ko.templateEngine))
|
987
|
+
throw "templateEngine must inherit from ko.templateEngine";
|
988
|
+
_templateEngine = templateEngine;
|
989
|
+
}
|
990
|
+
|
991
|
+
function getFirstNodeFromPossibleArray(nodeOrNodeArray) {
|
992
|
+
return nodeOrNodeArray.nodeType ? nodeOrNodeArray
|
993
|
+
: nodeOrNodeArray.length > 0 ? nodeOrNodeArray[0]
|
994
|
+
: null;
|
995
|
+
}
|
996
|
+
|
997
|
+
function executeTemplate(targetNodeOrNodeArray, renderMode, template, data, options) {
|
998
|
+
var dataForTemplate = ko.utils.unwrapObservable(data);
|
999
|
+
|
1000
|
+
options = options || {};
|
1001
|
+
var templateEngineToUse = (options.templateEngine || _templateEngine);
|
1002
|
+
ko.templateRewriting.ensureTemplateIsRewritten(template, templateEngineToUse);
|
1003
|
+
var renderedNodesArray = templateEngineToUse.renderTemplate(template, dataForTemplate, options);
|
1004
|
+
|
1005
|
+
// Loosely check result is an array of DOM nodes
|
1006
|
+
if ((typeof renderedNodesArray.length != "number") || (renderedNodesArray.length > 0 && typeof renderedNodesArray[0].nodeType != "number"))
|
1007
|
+
throw "Template engine must return an array of DOM nodes";
|
1008
|
+
|
1009
|
+
if (renderedNodesArray)
|
1010
|
+
ko.utils.arrayForEach(renderedNodesArray, function (renderedNode) {
|
1011
|
+
ko.memoization.unmemoizeDomNodeAndDescendants(renderedNode);
|
1012
|
+
});
|
1013
|
+
|
1014
|
+
switch (renderMode) {
|
1015
|
+
case "replaceChildren": ko.utils.setDomNodeChildren(targetNodeOrNodeArray, renderedNodesArray); break;
|
1016
|
+
case "replaceNode": ko.utils.replaceDomNodes(targetNodeOrNodeArray, renderedNodesArray); break;
|
1017
|
+
case "ignoreTargetNode": break;
|
1018
|
+
default: throw new Error("Unknown renderMode: " + renderMode);
|
1019
|
+
}
|
1020
|
+
|
1021
|
+
return renderedNodesArray;
|
1022
|
+
}
|
1023
|
+
|
1024
|
+
ko.renderTemplate = function (template, data, options, targetNodeOrNodeArray, renderMode) {
|
1025
|
+
options = options || {};
|
1026
|
+
if ((options.templateEngine || _templateEngine) == undefined)
|
1027
|
+
throw "Set a template engine before calling renderTemplate";
|
1028
|
+
renderMode = renderMode || "replaceChildren";
|
1029
|
+
|
1030
|
+
if (targetNodeOrNodeArray) {
|
1031
|
+
var firstTargetNode = getFirstNodeFromPossibleArray(targetNodeOrNodeArray);
|
1032
|
+
var whenToDispose = function () { return (!firstTargetNode) || !ko.utils.domNodeIsAttachedToDocument(firstTargetNode); };
|
1033
|
+
|
1034
|
+
return new ko.dependentObservable( // So the DOM is automatically updated when any dependency changes
|
1035
|
+
function () {
|
1036
|
+
var renderedNodesArray = executeTemplate(targetNodeOrNodeArray, renderMode, template, data, options);
|
1037
|
+
if (renderMode == "replaceNode") {
|
1038
|
+
targetNodeOrNodeArray = renderedNodesArray;
|
1039
|
+
firstTargetNode = getFirstNodeFromPossibleArray(targetNodeOrNodeArray);
|
1040
|
+
}
|
1041
|
+
},
|
1042
|
+
null,
|
1043
|
+
{ disposeWhen: whenToDispose }
|
1044
|
+
);
|
1045
|
+
} else {
|
1046
|
+
// 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
|
1047
|
+
return ko.memoization.memoize(function (domNode) {
|
1048
|
+
ko.renderTemplate(template, data, options, domNode, "replaceNode");
|
1049
|
+
});
|
1050
|
+
}
|
1051
|
+
};
|
1052
|
+
|
1053
|
+
ko.renderTemplateForEach = function (template, arrayOrObservableArray, options, targetNode) {
|
1054
|
+
var whenToDispose = function () { return !ko.utils.domNodeIsAttachedToDocument(targetNode); };
|
1055
|
+
|
1056
|
+
new ko.dependentObservable(function () {
|
1057
|
+
var unwrappedArray = ko.utils.unwrapObservable(arrayOrObservableArray);
|
1058
|
+
if (typeof unwrappedArray.length == "undefined") // Coerce single value into array
|
1059
|
+
unwrappedArray = [unwrappedArray];
|
1060
|
+
|
1061
|
+
ko.utils.setDomNodeChildrenFromArrayMapping(targetNode, unwrappedArray, function (arrayValue) {
|
1062
|
+
return executeTemplate(null, "ignoreTargetNode", template, arrayValue, options);
|
1063
|
+
}, options);
|
1064
|
+
}, null, { disposeWhen: whenToDispose });
|
1065
|
+
};
|
1066
|
+
|
1067
|
+
ko.bindingHandlers.template = {
|
1068
|
+
update: function (element, bindingValue, allBindings, viewModel) {
|
1069
|
+
var templateName = typeof bindingValue == "string" ? bindingValue : bindingValue.name;
|
1070
|
+
|
1071
|
+
if (typeof bindingValue.foreach != "undefined") {
|
1072
|
+
// Render once for each data point
|
1073
|
+
ko.renderTemplateForEach(templateName, bindingValue.foreach || [], { afterAdd: bindingValue.afterAdd, beforeRemove: bindingValue.beforeRemove }, element);
|
1074
|
+
}
|
1075
|
+
else {
|
1076
|
+
// Render once for this single data point (or use the viewModel if no data was provided)
|
1077
|
+
var templateData = bindingValue.data;
|
1078
|
+
ko.renderTemplate(templateName, typeof templateData == "undefined" ? viewModel : templateData, null, element);
|
1079
|
+
}
|
1080
|
+
}
|
1081
|
+
};
|
1082
|
+
})();/// <reference path="../../utils.js" />
|
1083
|
+
|
1084
|
+
// Simple calculation based on Levenshtein distance.
|
1085
|
+
(function () {
|
1086
|
+
|
1087
|
+
function calculateEditDistanceMatrix(oldArray, newArray, maxAllowedDistance) {
|
1088
|
+
var distances = [];
|
1089
|
+
for (var i = 0; i <= newArray.length; i++)
|
1090
|
+
distances[i] = [];
|
1091
|
+
|
1092
|
+
// Top row - transform old array into empty array via deletions
|
1093
|
+
for (var i = 0, j = Math.min(oldArray.length, maxAllowedDistance); i <= j; i++)
|
1094
|
+
distances[0][i] = i;
|
1095
|
+
|
1096
|
+
// Left row - transform empty array into new array via additions
|
1097
|
+
for (var i = 1, j = Math.min(newArray.length, maxAllowedDistance); i <= j; i++) {
|
1098
|
+
distances[i][0] = i;
|
1099
|
+
}
|
1100
|
+
|
1101
|
+
// Fill out the body of the array
|
1102
|
+
var oldIndex, oldIndexMax = oldArray.length, newIndex, newIndexMax = newArray.length;
|
1103
|
+
var distanceViaAddition, distanceViaDeletion;
|
1104
|
+
for (oldIndex = 1; oldIndex <= oldIndexMax; oldIndex++) {
|
1105
|
+
var newIndexMinForRow = Math.max(1, oldIndex - maxAllowedDistance);
|
1106
|
+
var newIndexMaxForRow = Math.min(newIndexMax, oldIndex + maxAllowedDistance);
|
1107
|
+
for (newIndex = newIndexMinForRow; newIndex <= newIndexMaxForRow; newIndex++) {
|
1108
|
+
if (oldArray[oldIndex - 1] === newArray[newIndex - 1])
|
1109
|
+
distances[newIndex][oldIndex] = distances[newIndex - 1][oldIndex - 1];
|
1110
|
+
else {
|
1111
|
+
var northDistance = distances[newIndex - 1][oldIndex] === undefined ? Number.MAX_VALUE : distances[newIndex - 1][oldIndex] + 1;
|
1112
|
+
var westDistance = distances[newIndex][oldIndex - 1] === undefined ? Number.MAX_VALUE : distances[newIndex][oldIndex - 1] + 1;
|
1113
|
+
distances[newIndex][oldIndex] = Math.min(northDistance, westDistance);
|
1114
|
+
}
|
1115
|
+
}
|
1116
|
+
}
|
1117
|
+
|
1118
|
+
return distances;
|
1119
|
+
}
|
1120
|
+
|
1121
|
+
function findEditScriptFromEditDistanceMatrix(editDistanceMatrix, oldArray, newArray) {
|
1122
|
+
var oldIndex = oldArray.length;
|
1123
|
+
var newIndex = newArray.length;
|
1124
|
+
var editScript = [];
|
1125
|
+
var maxDistance = editDistanceMatrix[newIndex][oldIndex];
|
1126
|
+
if (maxDistance === undefined)
|
1127
|
+
return null; // maxAllowedDistance must be too small
|
1128
|
+
while ((oldIndex > 0) || (newIndex > 0)) {
|
1129
|
+
var me = editDistanceMatrix[newIndex][oldIndex];
|
1130
|
+
var distanceViaAdd = (newIndex > 0) ? editDistanceMatrix[newIndex - 1][oldIndex] : maxDistance + 1;
|
1131
|
+
var distanceViaDelete = (oldIndex > 0) ? editDistanceMatrix[newIndex][oldIndex - 1] : maxDistance + 1;
|
1132
|
+
var distanceViaRetain = (newIndex > 0) && (oldIndex > 0) ? editDistanceMatrix[newIndex - 1][oldIndex - 1] : maxDistance + 1;
|
1133
|
+
if ((distanceViaAdd === undefined) || (distanceViaAdd < me - 1)) distanceViaAdd = maxDistance + 1;
|
1134
|
+
if ((distanceViaDelete === undefined) || (distanceViaDelete < me - 1)) distanceViaDelete = maxDistance + 1;
|
1135
|
+
if (distanceViaRetain < me - 1) distanceViaRetain = maxDistance + 1;
|
1136
|
+
|
1137
|
+
if ((distanceViaAdd <= distanceViaDelete) && (distanceViaAdd < distanceViaRetain)) {
|
1138
|
+
editScript.push({ status: "added", value: newArray[newIndex - 1] });
|
1139
|
+
newIndex--;
|
1140
|
+
} else if ((distanceViaDelete < distanceViaAdd) && (distanceViaDelete < distanceViaRetain)) {
|
1141
|
+
editScript.push({ status: "deleted", value: oldArray[oldIndex - 1] });
|
1142
|
+
oldIndex--;
|
1143
|
+
} else {
|
1144
|
+
editScript.push({ status: "retained", value: oldArray[oldIndex - 1] });
|
1145
|
+
newIndex--;
|
1146
|
+
oldIndex--;
|
1147
|
+
}
|
1148
|
+
}
|
1149
|
+
return editScript.reverse();
|
1150
|
+
}
|
1151
|
+
|
1152
|
+
ko.utils.compareArrays = function (oldArray, newArray, maxEditsToConsider) {
|
1153
|
+
if (maxEditsToConsider === undefined) {
|
1154
|
+
return ko.utils.compareArrays(oldArray, newArray, 1) // First consider likely case where there is at most one edit (very fast)
|
1155
|
+
|| ko.utils.compareArrays(oldArray, newArray, 10) // If that fails, account for a fair number of changes while still being fast
|
1156
|
+
|| ko.utils.compareArrays(oldArray, newArray, Number.MAX_VALUE); // Ultimately give the right answer, even though it may take a long time
|
1157
|
+
} else {
|
1158
|
+
oldArray = oldArray || [];
|
1159
|
+
newArray = newArray || [];
|
1160
|
+
var editDistanceMatrix = calculateEditDistanceMatrix(oldArray, newArray, maxEditsToConsider);
|
1161
|
+
return findEditScriptFromEditDistanceMatrix(editDistanceMatrix, oldArray, newArray);
|
1162
|
+
}
|
1163
|
+
};
|
1164
|
+
})();/// <reference path="compareArrays.js" />
|
1165
|
+
|
1166
|
+
(function () {
|
1167
|
+
// Objective:
|
1168
|
+
// * Given an input array, a container DOM node, and a function from array elements to arrays of DOM nodes,
|
1169
|
+
// map the array elements to arrays of DOM nodes, concatenate together all these arrays, and use them to populate the container DOM node
|
1170
|
+
// * Next time we're given the same combination of things (with the array possibly having mutated), update the container DOM node
|
1171
|
+
// so that its children is again the concatenation of the mappings of the array elements, but don't re-map any array elements that we
|
1172
|
+
// previously mapped - retain those nodes, and just insert/delete other ones
|
1173
|
+
|
1174
|
+
ko.utils.setDomNodeChildrenFromArrayMapping = function (domNode, array, mapping, options) {
|
1175
|
+
// Compare the provided array against the previous one
|
1176
|
+
array = array || [];
|
1177
|
+
options = options || {};
|
1178
|
+
var isFirstExecution = ko.utils.domData.get(domNode, "setDomNodeChildrenFromArrayMapping_lastMappingResult") === undefined;
|
1179
|
+
var lastMappingResult = ko.utils.domData.get(domNode, "setDomNodeChildrenFromArrayMapping_lastMappingResult") || [];
|
1180
|
+
var lastArray = ko.utils.arrayMap(lastMappingResult, function (x) { return x.arrayEntry; });
|
1181
|
+
var editScript = ko.utils.compareArrays(lastArray, array);
|
1182
|
+
|
1183
|
+
// Build the new mapping result
|
1184
|
+
var newMappingResult = [];
|
1185
|
+
var lastMappingResultIndex = 0;
|
1186
|
+
var nodesToDelete = [];
|
1187
|
+
var nodesAdded = [];
|
1188
|
+
var insertAfterNode = null;
|
1189
|
+
for (var i = 0, j = editScript.length; i < j; i++) {
|
1190
|
+
switch (editScript[i].status) {
|
1191
|
+
case "retained":
|
1192
|
+
// Just keep the information - don't touch the nodes
|
1193
|
+
var dataToRetain = lastMappingResult[lastMappingResultIndex];
|
1194
|
+
newMappingResult.push(dataToRetain);
|
1195
|
+
if (dataToRetain.domNodes.length > 0)
|
1196
|
+
insertAfterNode = dataToRetain.domNodes[dataToRetain.domNodes.length - 1];
|
1197
|
+
lastMappingResultIndex++;
|
1198
|
+
break;
|
1199
|
+
|
1200
|
+
case "deleted":
|
1201
|
+
// Queue these nodes for later removal
|
1202
|
+
ko.utils.arrayForEach(lastMappingResult[lastMappingResultIndex].domNodes, function (node) {
|
1203
|
+
nodesToDelete.push(node);
|
1204
|
+
insertAfterNode = node;
|
1205
|
+
});
|
1206
|
+
lastMappingResultIndex++;
|
1207
|
+
break;
|
1208
|
+
|
1209
|
+
case "added":
|
1210
|
+
// Map this array value and insert the resulting nodes at the current insertion point
|
1211
|
+
var mappedNodes = mapping(editScript[i].value) || [];
|
1212
|
+
newMappingResult.push({ arrayEntry: editScript[i].value, domNodes: mappedNodes });
|
1213
|
+
for (var nodeIndex = 0, nodeIndexMax = mappedNodes.length; nodeIndex < nodeIndexMax; nodeIndex++) {
|
1214
|
+
var node = mappedNodes[nodeIndex];
|
1215
|
+
nodesAdded.push(node);
|
1216
|
+
if (insertAfterNode == null) {
|
1217
|
+
// Insert at beginning
|
1218
|
+
if (domNode.firstChild)
|
1219
|
+
domNode.insertBefore(node, domNode.firstChild);
|
1220
|
+
else
|
1221
|
+
domNode.appendChild(node);
|
1222
|
+
} else {
|
1223
|
+
// Insert after insertion point
|
1224
|
+
if (insertAfterNode.nextSibling)
|
1225
|
+
domNode.insertBefore(node, insertAfterNode.nextSibling);
|
1226
|
+
else
|
1227
|
+
domNode.appendChild(node);
|
1228
|
+
}
|
1229
|
+
insertAfterNode = node;
|
1230
|
+
}
|
1231
|
+
break;
|
1232
|
+
}
|
1233
|
+
}
|
1234
|
+
|
1235
|
+
ko.utils.arrayForEach(nodesToDelete, function (node) { ko.utils.domData.cleanNodeAndDescendants(node); });
|
1236
|
+
|
1237
|
+
var invokedBeforeRemoveCallback = false;
|
1238
|
+
if (!isFirstExecution) {
|
1239
|
+
if (options.afterAdd)
|
1240
|
+
options.afterAdd(nodesAdded);
|
1241
|
+
if (options.beforeRemove) {
|
1242
|
+
options.beforeRemove(nodesToDelete);
|
1243
|
+
invokedBeforeRemoveCallback = true;
|
1244
|
+
}
|
1245
|
+
}
|
1246
|
+
if (!invokedBeforeRemoveCallback)
|
1247
|
+
ko.utils.arrayForEach(nodesToDelete, function (node) {
|
1248
|
+
if (node.parentNode)
|
1249
|
+
node.parentNode.removeChild(node);
|
1250
|
+
});
|
1251
|
+
|
1252
|
+
// Store a copy of the array items we just considered so we can difference it next time
|
1253
|
+
ko.utils.domData.set(domNode, "setDomNodeChildrenFromArrayMapping_lastMappingResult", newMappingResult);
|
1254
|
+
}
|
1255
|
+
})();/// <reference path="../templating.js" />
|
1256
|
+
|
1257
|
+
ko.jqueryTmplTemplateEngine = function () {
|
1258
|
+
function getTemplateNode(template) {
|
1259
|
+
var templateNode = document.getElementById(template);
|
1260
|
+
if (templateNode == null)
|
1261
|
+
throw new Error("Cannot find template with ID=" + template);
|
1262
|
+
return templateNode;
|
1263
|
+
}
|
1264
|
+
|
1265
|
+
this.renderTemplate = function (template, data, options) {
|
1266
|
+
// jquery.tmpl doesn't like it if the template returns just text content or nothing - it only likes you to return DOM nodes.
|
1267
|
+
// To make things more flexible, we can wrap the whole template in a <script> node so that jquery.tmpl just processes it as
|
1268
|
+
// text and doesn't try to parse the output. Then, since jquery.tmpl has jQuery as a dependency anyway, we can use jQuery to
|
1269
|
+
// parse that text into a document fragment using jQuery.clean().
|
1270
|
+
var templateTextInWrapper = "<script type=\"text/html\">" + ko.utils.stringTrim(getTemplateNode(template).text) + "</script>";
|
1271
|
+
var renderedMarkupInWrapper = $.tmpl(templateTextInWrapper, data);
|
1272
|
+
return jQuery.clean([renderedMarkupInWrapper[0].text], document);
|
1273
|
+
},
|
1274
|
+
|
1275
|
+
this.isTemplateRewritten = function (template) {
|
1276
|
+
return getTemplateNode(template).isRewritten === true;
|
1277
|
+
},
|
1278
|
+
|
1279
|
+
this.rewriteTemplate = function (template, rewriterCallback) {
|
1280
|
+
var templateNode = getTemplateNode(template);
|
1281
|
+
var rewritten = rewriterCallback(templateNode.text)
|
1282
|
+
templateNode.text = rewritten;
|
1283
|
+
templateNode.isRewritten = true;
|
1284
|
+
},
|
1285
|
+
|
1286
|
+
this.createJavaScriptEvaluatorBlock = function (script) {
|
1287
|
+
return "{{= " + script + "}}";
|
1288
|
+
},
|
1289
|
+
|
1290
|
+
// Am considering making template registration a native part of the API (and storing added templates centrally), but for now it's specific to this template engine
|
1291
|
+
this.addTemplate = function (templateName, templateMarkup) {
|
1292
|
+
document.write("<script type='text/html' id='" + templateName + "'>" + templateMarkup + "</script>");
|
1293
|
+
}
|
1294
|
+
};
|
1295
|
+
ko.jqueryTmplTemplateEngine.prototype = new ko.templateEngine();
|
1296
|
+
|
1297
|
+
// Use this one by default
|
1298
|
+
ko.setTemplateEngine(new ko.jqueryTmplTemplateEngine());
|