vendor/assets/javascripts/knockout.js in knockoutjs-rails-1.05 vs vendor/assets/javascripts/knockout.js in knockoutjs-rails-2.0.0
- old
+ new
@@ -1,28 +1,72 @@
-// Knockout JavaScript library v1.05
-// (c) 2010 Steven Sanderson - http://knockoutjs.com/
-// License: Ms-Pl (http://www.opensource.org/licenses/ms-pl.html)
+// Knockout JavaScript library v2.0.0
+// (c) Steven Sanderson - http://knockoutjs.com/
+// License: MIT (http://www.opensource.org/licenses/mit-license.php)
-var ko = window.ko = {};
-/// <reference path="namespace.js" />
-
+(function(window,undefined){
+var ko = window["ko"] = {};
+// Google Closure Compiler helpers (used only to make the minified file smaller)
+ko.exportSymbol = function(publicPath, object) {
+ var tokens = publicPath.split(".");
+ var target = window;
+ for (var i = 0; i < tokens.length - 1; i++)
+ target = target[tokens[i]];
+ target[tokens[tokens.length - 1]] = object;
+};
+ko.exportProperty = function(owner, publicName, object) {
+ owner[publicName] = object;
+};
ko.utils = new (function () {
var stringTrimRegex = /^(\s|\u00A0)+|(\s|\u00A0)+$/g;
+ // Represent the known event types in a compact way, then at runtime transform it into a hash with event name as key (for fast lookup)
+ var knownEvents = {}, knownEventTypesByEventName = {};
+ var keyEventTypeName = /Firefox\/2/i.test(navigator.userAgent) ? 'KeyboardEvent' : 'UIEvents';
+ knownEvents[keyEventTypeName] = ['keyup', 'keydown', 'keypress'];
+ knownEvents['MouseEvents'] = ['click', 'dblclick', 'mousedown', 'mouseup', 'mousemove', 'mouseover', 'mouseout', 'mouseenter', 'mouseleave'];
+ for (var eventType in knownEvents) {
+ var knownEventsForType = knownEvents[eventType];
+ if (knownEventsForType.length) {
+ for (var i = 0, j = knownEventsForType.length; i < j; i++)
+ knownEventTypesByEventName[knownEventsForType[i]] = eventType;
+ }
+ }
+
+ // Detect IE versions for bug workarounds (uses IE conditionals, not UA string, for robustness)
+ var ieVersion = (function() {
+ var version = 3, div = document.createElement('div'), iElems = div.getElementsByTagName('i');
+
+ // Keep constructing conditional HTML blocks until we hit one that resolves to an empty fragment
+ while (
+ div.innerHTML = '<!--[if gt IE ' + (++version) + ']><i></i><![endif]-->',
+ iElems[0]
+ );
+ return version > 4 ? version : undefined;
+ }());
+ var isIe6 = ieVersion === 6,
+ isIe7 = ieVersion === 7;
+
+ function isClickOnCheckableElement(element, eventType) {
+ if ((element.tagName != "INPUT") || !element.type) return false;
+ if (eventType.toLowerCase() != "click") return false;
+ var inputType = element.type.toLowerCase();
+ return (inputType == "checkbox") || (inputType == "radio");
+ }
+
return {
fieldsIncludedWithJsonPost: ['authenticity_token', /^__RequestVerificationToken(_.*)?$/],
arrayForEach: function (array, action) {
for (var i = 0, j = array.length; i < j; i++)
action(array[i]);
},
arrayIndexOf: function (array, item) {
- if (typeof array.indexOf == "function")
- return array.indexOf(item);
+ if (typeof Array.prototype.indexOf == "function")
+ return Array.prototype.indexOf.call(array, item);
for (var i = 0, j = array.length; i < j; i++)
- if (array[i] == item)
+ if (array[i] === item)
return i;
return -1;
},
arrayFirst: function (array, predicate, predicateOwner) {
@@ -44,11 +88,11 @@
for (var i = 0, j = array.length; i < j; i++) {
if (ko.utils.arrayIndexOf(result, array[i]) < 0)
result.push(array[i]);
}
return result;
- },
+ },
arrayMap: function (array, mapping) {
array = array || [];
var result = [];
for (var i = 0, j = array.length; i < j; i++)
@@ -66,16 +110,25 @@
},
arrayPushAll: function (array, valuesToPush) {
for (var i = 0, j = valuesToPush.length; i < j; i++)
array.push(valuesToPush[i]);
+ return array;
},
+ extend: function (target, source) {
+ for(var prop in source) {
+ if(source.hasOwnProperty(prop)) {
+ target[prop] = source[prop];
+ }
+ }
+ return target;
+ },
+
emptyDomNode: function (domNode) {
while (domNode.firstChild) {
- ko.utils.domData.cleanNodeAndDescendants(domNode.firstChild);
- domNode.removeChild(domNode.firstChild);
+ ko.removeNode(domNode.firstChild);
}
},
setDomNodeChildren: function (domNode, childNodes) {
ko.utils.emptyDomNode(domNode);
@@ -92,12 +145,11 @@
var insertionPoint = nodesToReplaceArray[0];
var parent = insertionPoint.parentNode;
for (var i = 0, j = newNodesArray.length; i < j; i++)
parent.insertBefore(newNodesArray[i], insertionPoint);
for (var i = 0, j = nodesToReplaceArray.length; i < j; i++) {
- ko.utils.domData.cleanNodeAndDescendants(nodesToReplaceArray[i]);
- parent.removeChild(nodesToReplaceArray[i]);
+ ko.removeNode(nodesToReplaceArray[i]);
}
}
},
setOptionNodeSelectionState: function (optionNode, isSelected) {
@@ -105,23 +157,11 @@
if (navigator.userAgent.indexOf("MSIE 6") >= 0)
optionNode.setAttribute("selected", isSelected);
else
optionNode.selected = isSelected;
},
-
- getElementsHavingAttribute: function (rootNode, attributeName) {
- if ((!rootNode) || (rootNode.nodeType != 1)) return [];
- var results = [];
- if (rootNode.getAttribute(attributeName) !== null)
- results.push(rootNode);
- var descendants = rootNode.getElementsByTagName("*");
- for (var i = 0, j = descendants.length; i < j; i++)
- if (descendants[i].getAttribute(attributeName) !== null)
- results.push(descendants[i]);
- return results;
- },
-
+
stringTrim: function (string) {
return (string || "").replace(stringTrimRegex, "");
},
stringTokenize: function (string, delimiter) {
@@ -132,15 +172,29 @@
if (trimmed !== "")
result.push(trimmed);
}
return result;
},
+
+ stringStartsWith: function (string, startsWith) {
+ string = string || "";
+ if (startsWith.length > string.length)
+ return false;
+ return string.substring(0, startsWith.length) === startsWith;
+ },
- evalWithinScope: function (expression, scope) {
- if (scope === undefined)
- return (new Function("return " + expression))();
- with (scope) { return eval("(" + expression + ")"); }
+ evalWithinScope: function (expression /*, scope1, scope2, scope3... */) {
+ // Build the source for a function that evaluates "expression"
+ // For each scope variable, add an extra level of "with" nesting
+ // Example result: with(sc[1]) { with(sc[0]) { return (expression) } }
+ var scopes = Array.prototype.slice.call(arguments, 1);
+ var functionBody = "return (" + expression + ")";
+ for (var i = 0; i < scopes.length; i++) {
+ if (scopes[i] && typeof scopes[i] == "object")
+ functionBody = "with(sc[" + i + "]) { " + functionBody + " } ";
+ }
+ return (new Function("sc", functionBody))(scopes);
},
domNodeIsContainedBy: function (node, containedByNode) {
if (containedByNode.compareDocumentPosition)
return (containedByNode.compareDocumentPosition(node) & 16) == 16;
@@ -155,13 +209,27 @@
domNodeIsAttachedToDocument: function (node) {
return ko.utils.domNodeIsContainedBy(node, document);
},
registerEventHandler: function (element, eventType, handler) {
- if (typeof jQuery != "undefined")
- jQuery(element).bind(eventType, handler);
- else if (typeof element.addEventListener == "function")
+ if (typeof jQuery != "undefined") {
+ if (isClickOnCheckableElement(element, eventType)) {
+ // For click events on checkboxes, jQuery interferes with the event handling in an awkward way:
+ // it toggles the element checked state *after* the click event handlers run, whereas native
+ // click events toggle the checked state *before* the event handler.
+ // Fix this by intecepting the handler and applying the correct checkedness before it runs.
+ var originalHandler = handler;
+ handler = function(event, eventData) {
+ var jQuerySuppliedCheckedState = this.checked;
+ if (eventData)
+ this.checked = eventData.checkedStateBeforeEvent !== true;
+ originalHandler.call(this, event);
+ this.checked = jQuerySuppliedCheckedState; // Restore the state jQuery applied
+ };
+ }
+ jQuery(element)['bind'](eventType, handler);
+ } else if (typeof element.addEventListener == "function")
element.addEventListener(eventType, handler, false);
else if (typeof element.attachEvent != "undefined")
element.attachEvent("on" + eventType, function (event) {
handler.call(element, event);
});
@@ -171,27 +239,34 @@
triggerEvent: function (element, eventType) {
if (!(element && element.nodeType))
throw new Error("element must be a DOM node when calling triggerEvent");
- if (typeof element.fireEvent != "undefined") {
- // Unlike other browsers, IE doesn't change the checked state of checkboxes/radiobuttons when you trigger their "click" event
- // so to make it consistent, we'll do it manually here
- if (eventType == "click") {
- if ((element.tagName == "INPUT") && ((element.type.toLowerCase() == "checkbox") || (element.type.toLowerCase() == "radio")))
- element.checked = element.checked !== true;
+ if (typeof jQuery != "undefined") {
+ var eventData = [];
+ if (isClickOnCheckableElement(element, eventType)) {
+ // Work around the jQuery "click events on checkboxes" issue described above by storing the original checked state before triggering the handler
+ eventData.push({ checkedStateBeforeEvent: element.checked });
}
- element.fireEvent("on" + eventType);
+ jQuery(element)['trigger'](eventType, eventData);
} else if (typeof document.createEvent == "function") {
if (typeof element.dispatchEvent == "function") {
- var eventCategory = (eventType == "click" ? "MouseEvents" : "HTMLEvents"); // Might need to account for other event names at some point
+ var eventCategory = knownEventTypesByEventName[eventType] || "HTMLEvents";
var event = document.createEvent(eventCategory);
event.initEvent(eventType, true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, element);
element.dispatchEvent(event);
}
else
throw new Error("The supplied element doesn't support dispatchEvent");
+ } else if (typeof element.fireEvent != "undefined") {
+ // Unlike other browsers, IE doesn't change the checked state of checkboxes/radiobuttons when you trigger their "click" event
+ // so to make it consistent, we'll do it manually here
+ if (eventType == "click") {
+ if ((element.tagName == "INPUT") && ((element.type.toLowerCase() == "checkbox") || (element.type.toLowerCase() == "radio")))
+ element.checked = element.checked !== true;
+ }
+ element.fireEvent("on" + eventType);
}
else
throw new Error("Browser doesn't support triggering events");
},
@@ -216,10 +291,39 @@
newClassName += currentClassNames[i] + " ";
node.className = ko.utils.stringTrim(newClassName);
}
},
+ outerHTML: function(node) {
+ // For Chrome on non-text nodes
+ // (Although IE supports outerHTML, the way it formats HTML is inconsistent - sometimes closing </li> tags are omitted, sometimes not. That caused https://github.com/SteveSanderson/knockout/issues/212.)
+ if (ieVersion === undefined) {
+ var nativeOuterHtml = node.outerHTML;
+ if (typeof nativeOuterHtml == "string")
+ return nativeOuterHtml;
+ }
+
+ // Other browsers
+ var dummyContainer = window.document.createElement("div");
+ dummyContainer.appendChild(node.cloneNode(true));
+ return dummyContainer.innerHTML;
+ },
+
+ setTextContent: function(element, textContent) {
+ var value = ko.utils.unwrapObservable(textContent);
+ if ((value === null) || (value === undefined))
+ value = "";
+
+ 'innerText' in element ? element.innerText = value
+ : element.textContent = value;
+
+ if (ieVersion >= 9) {
+ // Believe it or not, this actually fixes an IE9 rendering bug. Insane. https://github.com/SteveSanderson/knockout/issues/209
+ element.innerHTML = element.innerHTML;
+ }
+ },
+
range: function (min, max) {
min = ko.utils.unwrapObservable(min);
max = ko.utils.unwrapObservable(max);
var result = [];
for (var i = min; i <= max; i++)
@@ -227,16 +331,19 @@
return result;
},
makeArray: function(arrayLikeObject) {
var result = [];
- for (var i = arrayLikeObject.length - 1; i >= 0; i--){
+ for (var i = 0, j = arrayLikeObject.length; i < j; i++) {
result.push(arrayLikeObject[i]);
};
return result;
},
+ isIe6 : isIe6,
+ isIe7 : isIe7,
+
getFormFields: function(form, fieldName) {
var fields = ko.utils.makeArray(form.getElementsByTagName("INPUT")).concat(ko.utils.makeArray(form.getElementsByTagName("TEXTAREA")));
var isMatchingField = (typeof fieldName == 'string')
? function(field) { return field.name === fieldName }
: function(field) { return fieldName.test(field.name) }; // Treat fieldName as regex or object containing predicate
@@ -245,21 +352,33 @@
if (isMatchingField(fields[i]))
matches.push(fields[i]);
};
return matches;
},
+
+ parseJson: function (jsonString) {
+ if (typeof jsonString == "string") {
+ jsonString = ko.utils.stringTrim(jsonString);
+ if (jsonString) {
+ if (window.JSON && window.JSON.parse) // Use native parsing where available
+ return window.JSON.parse(jsonString);
+ return (new Function("return " + jsonString))(); // Fallback on less safe parsing for older browsers
+ }
+ }
+ return null;
+ },
stringifyJson: function (data) {
if ((typeof JSON == "undefined") || (typeof JSON.stringify == "undefined"))
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");
return JSON.stringify(ko.utils.unwrapObservable(data));
},
postJson: function (urlOrForm, data, options) {
options = options || {};
- var params = options.params || {};
- var includeFields = options.includeFields || this.fieldsIncludedWithJsonPost;
+ var params = options['params'] || {};
+ var includeFields = options['includeFields'] || this.fieldsIncludedWithJsonPost;
var url = urlOrForm;
// If we were given a form, use its 'action' URL and pick out any requested field values
if((typeof urlOrForm == 'object') && (urlOrForm.tagName == "FORM")) {
var originalForm = urlOrForm;
@@ -287,66 +406,252 @@
input.name = key;
input.value = params[key];
form.appendChild(input);
}
document.body.appendChild(form);
- options.submitter ? options.submitter(form) : form.submit();
+ options['submitter'] ? options['submitter'](form) : form.submit();
setTimeout(function () { form.parentNode.removeChild(form); }, 0);
- },
-
- domData: {
- uniqueId: 0,
- dataStoreKeyExpandoPropertyName: "__ko__" + (new Date).getTime(),
- dataStore: {},
- get: function (node, key) {
- var allDataForNode = ko.utils.domData.getAll(node, false);
- return allDataForNode === undefined ? undefined : allDataForNode[key];
- },
- set: function (node, key, value) {
- var allDataForNode = ko.utils.domData.getAll(node, true);
- allDataForNode[key] = value;
- },
- getAll: function (node, createIfNotFound) {
- var dataStoreKey = node[ko.utils.domData.dataStoreKeyExpandoPropertyName];
- if (!dataStoreKey) {
- if (!createIfNotFound)
- return undefined;
- dataStoreKey = node[ko.utils.domData.dataStoreKeyExpandoPropertyName] = "ko" + ko.utils.domData.uniqueId++;
- ko.utils.domData[dataStoreKey] = {};
- }
- return ko.utils.domData[dataStoreKey];
- },
- cleanNode: function (node) {
- var dataStoreKey = node[ko.utils.domData.dataStoreKeyExpandoPropertyName];
- if (dataStoreKey) {
- delete ko.utils.domData[dataStoreKey];
- node[ko.utils.domData.dataStoreKeyExpandoPropertyName] = null;
- }
- },
- cleanNodeAndDescendants: function (node) {
- if ((node.nodeType != 1) && (node.nodeType != 9))
- return;
- ko.utils.domData.cleanNode(node);
- var descendants = node.getElementsByTagName("*");
- for (var i = 0, j = descendants.length; i < j; i++)
- ko.utils.domData.cleanNode(descendants[i]);
- }
}
}
})();
-if (!Function.prototype.bind) {
+ko.exportSymbol('ko.utils', ko.utils);
+ko.utils.arrayForEach([
+ ['arrayForEach', ko.utils.arrayForEach],
+ ['arrayFirst', ko.utils.arrayFirst],
+ ['arrayFilter', ko.utils.arrayFilter],
+ ['arrayGetDistinctValues', ko.utils.arrayGetDistinctValues],
+ ['arrayIndexOf', ko.utils.arrayIndexOf],
+ ['arrayMap', ko.utils.arrayMap],
+ ['arrayPushAll', ko.utils.arrayPushAll],
+ ['arrayRemoveItem', ko.utils.arrayRemoveItem],
+ ['extend', ko.utils.extend],
+ ['fieldsIncludedWithJsonPost', ko.utils.fieldsIncludedWithJsonPost],
+ ['getFormFields', ko.utils.getFormFields],
+ ['postJson', ko.utils.postJson],
+ ['parseJson', ko.utils.parseJson],
+ ['registerEventHandler', ko.utils.registerEventHandler],
+ ['stringifyJson', ko.utils.stringifyJson],
+ ['range', ko.utils.range],
+ ['toggleDomNodeCssClass', ko.utils.toggleDomNodeCssClass],
+ ['triggerEvent', ko.utils.triggerEvent],
+ ['unwrapObservable', ko.utils.unwrapObservable]
+], function(item) {
+ ko.exportSymbol('ko.utils.' + item[0], item[1]);
+});
+
+if (!Function.prototype['bind']) {
// 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)
// In case the browser doesn't implement it natively, provide a JavaScript implementation. This implementation is based on the one in prototype.js
- Function.prototype.bind = function (object) {
+ Function.prototype['bind'] = function (object) {
var originalFunction = this, args = Array.prototype.slice.call(arguments), object = args.shift();
return function () {
return originalFunction.apply(object, args.concat(Array.prototype.slice.call(arguments)));
};
};
-}/// <reference path="utils.js" />
+}
+ko.utils.domData = new (function () {
+ var uniqueId = 0;
+ var dataStoreKeyExpandoPropertyName = "__ko__" + (new Date).getTime();
+ var dataStore = {};
+ return {
+ get: function (node, key) {
+ var allDataForNode = ko.utils.domData.getAll(node, false);
+ return allDataForNode === undefined ? undefined : allDataForNode[key];
+ },
+ set: function (node, key, value) {
+ if (value === undefined) {
+ // Make sure we don't actually create a new domData key if we are actually deleting a value
+ if (ko.utils.domData.getAll(node, false) === undefined)
+ return;
+ }
+ var allDataForNode = ko.utils.domData.getAll(node, true);
+ allDataForNode[key] = value;
+ },
+ getAll: function (node, createIfNotFound) {
+ var dataStoreKey = node[dataStoreKeyExpandoPropertyName];
+ var hasExistingDataStore = dataStoreKey && (dataStoreKey !== "null");
+ if (!hasExistingDataStore) {
+ if (!createIfNotFound)
+ return undefined;
+ dataStoreKey = node[dataStoreKeyExpandoPropertyName] = "ko" + uniqueId++;
+ dataStore[dataStoreKey] = {};
+ }
+ return dataStore[dataStoreKey];
+ },
+ clear: function (node) {
+ var dataStoreKey = node[dataStoreKeyExpandoPropertyName];
+ if (dataStoreKey) {
+ delete dataStore[dataStoreKey];
+ node[dataStoreKeyExpandoPropertyName] = null;
+ }
+ }
+ }
+})();
+ko.exportSymbol('ko.utils.domData', ko.utils.domData);
+ko.exportSymbol('ko.utils.domData.clear', ko.utils.domData.clear); // Exporting only so specs can clear up after themselves fully
+ko.utils.domNodeDisposal = new (function () {
+ var domDataKey = "__ko_domNodeDisposal__" + (new Date).getTime();
+
+ function getDisposeCallbacksCollection(node, createIfNotFound) {
+ var allDisposeCallbacks = ko.utils.domData.get(node, domDataKey);
+ if ((allDisposeCallbacks === undefined) && createIfNotFound) {
+ allDisposeCallbacks = [];
+ ko.utils.domData.set(node, domDataKey, allDisposeCallbacks);
+ }
+ return allDisposeCallbacks;
+ }
+ function destroyCallbacksCollection(node) {
+ ko.utils.domData.set(node, domDataKey, undefined);
+ }
+
+ function cleanSingleNode(node) {
+ // Run all the dispose callbacks
+ var callbacks = getDisposeCallbacksCollection(node, false);
+ if (callbacks) {
+ callbacks = callbacks.slice(0); // Clone, as the array may be modified during iteration (typically, callbacks will remove themselves)
+ for (var i = 0; i < callbacks.length; i++)
+ callbacks[i](node);
+ }
+
+ // Also erase the DOM data
+ ko.utils.domData.clear(node);
+
+ // Special support for jQuery here because it's so commonly used.
+ // Many jQuery plugins (including jquery.tmpl) store data using jQuery's equivalent of domData
+ // so notify it to tear down any resources associated with the node & descendants here.
+ if ((typeof jQuery == "function") && (typeof jQuery['cleanData'] == "function"))
+ jQuery['cleanData']([node]);
+ }
+
+ return {
+ addDisposeCallback : function(node, callback) {
+ if (typeof callback != "function")
+ throw new Error("Callback must be a function");
+ getDisposeCallbacksCollection(node, true).push(callback);
+ },
+
+ removeDisposeCallback : function(node, callback) {
+ var callbacksCollection = getDisposeCallbacksCollection(node, false);
+ if (callbacksCollection) {
+ ko.utils.arrayRemoveItem(callbacksCollection, callback);
+ if (callbacksCollection.length == 0)
+ destroyCallbacksCollection(node);
+ }
+ },
+
+ cleanNode : function(node) {
+ if ((node.nodeType != 1) && (node.nodeType != 9))
+ return;
+ cleanSingleNode(node);
+
+ // Clone the descendants list in case it changes during iteration
+ var descendants = [];
+ ko.utils.arrayPushAll(descendants, node.getElementsByTagName("*"));
+ for (var i = 0, j = descendants.length; i < j; i++)
+ cleanSingleNode(descendants[i]);
+ },
+
+ removeNode : function(node) {
+ ko.cleanNode(node);
+ if (node.parentNode)
+ node.parentNode.removeChild(node);
+ }
+ }
+})();
+ko.cleanNode = ko.utils.domNodeDisposal.cleanNode; // Shorthand name for convenience
+ko.removeNode = ko.utils.domNodeDisposal.removeNode; // Shorthand name for convenience
+ko.exportSymbol('ko.cleanNode', ko.cleanNode);
+ko.exportSymbol('ko.removeNode', ko.removeNode);
+ko.exportSymbol('ko.utils.domNodeDisposal', ko.utils.domNodeDisposal);
+ko.exportSymbol('ko.utils.domNodeDisposal.addDisposeCallback', ko.utils.domNodeDisposal.addDisposeCallback);
+ko.exportSymbol('ko.utils.domNodeDisposal.removeDisposeCallback', ko.utils.domNodeDisposal.removeDisposeCallback);(function () {
+ var leadingCommentRegex = /^(\s*)<!--(.*?)-->/;
+
+ function simpleHtmlParse(html) {
+ // Based on jQuery's "clean" function, but only accounting for table-related elements.
+ // If you have referenced jQuery, this won't be used anyway - KO will use jQuery's "clean" function directly
+
+ // Note that there's still an issue in IE < 9 whereby it will discard comment nodes that are the first child of
+ // a descendant node. For example: "<div><!-- mycomment -->abc</div>" will get parsed as "<div>abc</div>"
+ // This won't affect anyone who has referenced jQuery, and there's always the workaround of inserting a dummy node
+ // (possibly a text node) in front of the comment. So, KO does not attempt to workaround this IE issue automatically at present.
+
+ // Trim whitespace, otherwise indexOf won't work as expected
+ var tags = ko.utils.stringTrim(html).toLowerCase(), div = document.createElement("div");
+
+ // Finds the first match from the left column, and returns the corresponding "wrap" data from the right column
+ var wrap = tags.match(/^<(thead|tbody|tfoot)/) && [1, "<table>", "</table>"] ||
+ !tags.indexOf("<tr") && [2, "<table><tbody>", "</tbody></table>"] ||
+ (!tags.indexOf("<td") || !tags.indexOf("<th")) && [3, "<table><tbody><tr>", "</tr></tbody></table>"] ||
+ /* anything else */ [0, "", ""];
+
+ // Go to html and back, then peel off extra wrappers
+ // Note that we always prefix with some dummy text, because otherwise, IE<9 will strip out leading comment nodes in descendants. Total madness.
+ var markup = "ignored<div>" + wrap[1] + html + wrap[2] + "</div>";
+ if (typeof window['innerShiv'] == "function") {
+ div.appendChild(window['innerShiv'](markup));
+ } else {
+ div.innerHTML = markup;
+ }
+
+ // Move to the right depth
+ while (wrap[0]--)
+ div = div.lastChild;
+
+ return ko.utils.makeArray(div.lastChild.childNodes);
+ }
+
+ function jQueryHtmlParse(html) {
+ var elems = jQuery['clean']([html]);
+
+ // As of jQuery 1.7.1, jQuery parses the HTML by appending it to some dummy parent nodes held in an in-memory document fragment.
+ // Unfortunately, it never clears the dummy parent nodes from the document fragment, so it leaks memory over time.
+ // Fix this by finding the top-most dummy parent element, and detaching it from its owner fragment.
+ if (elems && elems[0]) {
+ // Find the top-most parent element that's a direct child of a document fragment
+ var elem = elems[0];
+ while (elem.parentNode && elem.parentNode.nodeType !== 11 /* i.e., DocumentFragment */)
+ elem = elem.parentNode;
+ // ... then detach it
+ if (elem.parentNode)
+ elem.parentNode.removeChild(elem);
+ }
+
+ return elems;
+ }
+
+ ko.utils.parseHtmlFragment = function(html) {
+ return typeof jQuery != 'undefined' ? jQueryHtmlParse(html) // As below, benefit from jQuery's optimisations where possible
+ : simpleHtmlParse(html); // ... otherwise, this simple logic will do in most common cases.
+ };
+
+ ko.utils.setHtml = function(node, html) {
+ ko.utils.emptyDomNode(node);
+
+ if ((html !== null) && (html !== undefined)) {
+ if (typeof html != 'string')
+ html = html.toString();
+
+ // jQuery contains a lot of sophisticated code to parse arbitrary HTML fragments,
+ // for example <tr> elements which are not normally allowed to exist on their own.
+ // If you've referenced jQuery we'll use that rather than duplicating its code.
+ if (typeof jQuery != 'undefined') {
+ jQuery(node)['html'](html);
+ } else {
+ // ... otherwise, use KO's own parsing logic.
+ var parsedNodes = ko.utils.parseHtmlFragment(html);
+ for (var i = 0; i < parsedNodes.length; i++)
+ node.appendChild(parsedNodes[i]);
+ }
+ }
+ };
+})();
+
+ko.exportSymbol('ko.utils.parseHtmlFragment', ko.utils.parseHtmlFragment);
+ko.exportSymbol('ko.utils.setHtml', ko.utils.setHtml);
ko.memoization = (function () {
var memos = {};
function randomMax8HexChars() {
return (((1 + Math.random()) * 0x100000000) | 0).toString(16).substring(1);
@@ -385,16 +690,19 @@
return true;
}
finally { delete memos[memoId]; }
},
- unmemoizeDomNodeAndDescendants: function (domNode) {
+ unmemoizeDomNodeAndDescendants: function (domNode, extraCallbackParamsArray) {
var memos = [];
findMemoNodes(domNode, memos);
for (var i = 0, j = memos.length; i < j; i++) {
var node = memos[i].domNode;
- ko.memoization.unmemoize(memos[i].memoId, [node]);
+ var combinedParams = [node];
+ if (extraCallbackParamsArray)
+ ko.utils.arrayPushAll(combinedParams, extraCallbackParamsArray);
+ ko.memoization.unmemoize(memos[i].memoId, combinedParams);
node.nodeValue = ""; // Neuter this node so we don't try to unmemoize it again
if (node.parentNode)
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)
}
},
@@ -402,522 +710,1451 @@
parseMemoText: function (memoText) {
var match = memoText.match(/^\[ko_memo\:(.*?)\]$/);
return match ? match[1] : null;
}
};
-})();/// <reference path="../utils.js" />
+})();
+ko.exportSymbol('ko.memoization', ko.memoization);
+ko.exportSymbol('ko.memoization.memoize', ko.memoization.memoize);
+ko.exportSymbol('ko.memoization.unmemoize', ko.memoization.unmemoize);
+ko.exportSymbol('ko.memoization.parseMemoText', ko.memoization.parseMemoText);
+ko.exportSymbol('ko.memoization.unmemoizeDomNodeAndDescendants', ko.memoization.unmemoizeDomNodeAndDescendants);
+ko.extenders = {
+ 'throttle': function(target, timeout) {
+ // Throttling means two things:
+
+ // (1) For dependent observables, we throttle *evaluations* so that, no matter how fast its dependencies
+ // notify updates, the target doesn't re-evaluate (and hence doesn't notify) faster than a certain rate
+ target['throttleEvaluation'] = timeout;
+
+ // (2) For writable targets (observables, or writable dependent observables), we throttle *writes*
+ // so the target cannot change value synchronously or faster than a certain rate
+ var writeTimeoutInstance = null;
+ return ko.dependentObservable({
+ 'read': target,
+ 'write': function(value) {
+ clearTimeout(writeTimeoutInstance);
+ writeTimeoutInstance = setTimeout(function() {
+ target(value);
+ }, timeout);
+ }
+ });
+ },
+
+ 'notify': function(target, notifyWhen) {
+ target["equalityComparer"] = notifyWhen == "always"
+ ? function() { return false } // Treat all values as not equal
+ : ko.observable["fn"]["equalityComparer"];
+ return target;
+ }
+};
+
+function applyExtenders(requestedExtenders) {
+ var target = this;
+ if (requestedExtenders) {
+ for (var key in requestedExtenders) {
+ var extenderHandler = ko.extenders[key];
+ if (typeof extenderHandler == 'function') {
+ target = extenderHandler(target, requestedExtenders[key]);
+ }
+ }
+ }
+ return target;
+}
+
+ko.exportSymbol('ko.extenders', ko.extenders);
ko.subscription = function (callback, disposeCallback) {
this.callback = callback;
- this.dispose = disposeCallback;
+ this.disposeCallback = disposeCallback;
+ ko.exportProperty(this, 'dispose', this.dispose);
};
+ko.subscription.prototype.dispose = function () {
+ this.isDisposed = true;
+ this.disposeCallback();
+};
ko.subscribable = function () {
- var _subscriptions = [];
+ this._subscriptions = {};
- this.subscribe = function (callback, callbackTarget) {
- var boundCallback = callbackTarget ? function () { callback.call(callbackTarget) } : callback;
+ ko.utils.extend(this, ko.subscribable['fn']);
+ ko.exportProperty(this, 'subscribe', this.subscribe);
+ ko.exportProperty(this, 'extend', this.extend);
+ ko.exportProperty(this, 'getSubscriptionsCount', this.getSubscriptionsCount);
+}
+var defaultEvent = "change";
+
+ko.subscribable['fn'] = {
+ subscribe: function (callback, callbackTarget, event) {
+ event = event || defaultEvent;
+ var boundCallback = callbackTarget ? callback.bind(callbackTarget) : callback;
+
var subscription = new ko.subscription(boundCallback, function () {
- ko.utils.arrayRemoveItem(_subscriptions, subscription);
- });
- _subscriptions.push(subscription);
+ ko.utils.arrayRemoveItem(this._subscriptions[event], subscription);
+ }.bind(this));
+
+ if (!this._subscriptions[event])
+ this._subscriptions[event] = [];
+ this._subscriptions[event].push(subscription);
return subscription;
- };
+ },
- this.notifySubscribers = function (valueToNotify) {
- ko.utils.arrayForEach(_subscriptions.slice(0), function (subscription) {
- if (subscription)
- subscription.callback(valueToNotify);
- });
- };
+ "notifySubscribers": function (valueToNotify, event) {
+ event = event || defaultEvent;
+ if (this._subscriptions[event]) {
+ ko.utils.arrayForEach(this._subscriptions[event].slice(0), function (subscription) {
+ // In case a subscription was disposed during the arrayForEach cycle, check
+ // for isDisposed on each subscription before invoking its callback
+ if (subscription && (subscription.isDisposed !== true))
+ subscription.callback(valueToNotify);
+ });
+ }
+ },
- this.getSubscriptionsCount = function () {
- return _subscriptions.length;
- };
-}
+ getSubscriptionsCount: function () {
+ var total = 0;
+ for (var eventName in this._subscriptions) {
+ if (this._subscriptions.hasOwnProperty(eventName))
+ total += this._subscriptions[eventName].length;
+ }
+ return total;
+ },
+
+ extend: applyExtenders
+};
+
ko.isSubscribable = function (instance) {
- return typeof instance.subscribe == "function" && typeof instance.notifySubscribers == "function";
-};/// <reference path="subscribable.js" />
+ return typeof instance.subscribe == "function" && typeof instance["notifySubscribers"] == "function";
+};
-ko.dependencyDetection = (function () {
- var _detectedDependencies = [];
+ko.exportSymbol('ko.subscribable', ko.subscribable);
+ko.exportSymbol('ko.isSubscribable', ko.isSubscribable);
+ko.dependencyDetection = (function () {
+ var _frames = [];
+
return {
- begin: function () {
- _detectedDependencies.push([]);
+ begin: function (callback) {
+ _frames.push({ callback: callback, distinctDependencies:[] });
},
end: function () {
- return _detectedDependencies.pop();
+ _frames.pop();
},
registerDependency: function (subscribable) {
if (!ko.isSubscribable(subscribable))
throw "Only subscribable things can act as dependencies";
- if (_detectedDependencies.length > 0) {
- _detectedDependencies[_detectedDependencies.length - 1].push(subscribable);
+ if (_frames.length > 0) {
+ var topFrame = _frames[_frames.length - 1];
+ if (ko.utils.arrayIndexOf(topFrame.distinctDependencies, subscribable) >= 0)
+ return;
+ topFrame.distinctDependencies.push(subscribable);
+ topFrame.callback(subscribable);
}
}
};
-})();/// <reference path="dependencyDetection.js" />
+})();var primitiveTypes = { 'undefined':true, 'boolean':true, 'number':true, 'string':true };
ko.observable = function (initialValue) {
var _latestValue = initialValue;
- function observable(newValue) {
+ function observable() {
if (arguments.length > 0) {
- _latestValue = newValue;
- observable.notifySubscribers(_latestValue);
+ // Write
+
+ // Ignore writes if the value hasn't changed
+ if ((!observable['equalityComparer']) || !observable['equalityComparer'](_latestValue, arguments[0])) {
+ observable.valueWillMutate();
+ _latestValue = arguments[0];
+ observable.valueHasMutated();
+ }
+ return this; // Permits chained assignments
}
- else // The caller only needs to be notified of changes if they did a "read" operation
- ko.dependencyDetection.registerDependency(observable);
-
- return _latestValue;
+ else {
+ // Read
+ ko.dependencyDetection.registerDependency(observable); // The caller only needs to be notified of changes if they did a "read" operation
+ return _latestValue;
+ }
}
- observable.__ko_proto__ = ko.observable;
- observable.valueHasMutated = function () { observable.notifySubscribers(_latestValue); }
-
ko.subscribable.call(observable);
+ observable.valueHasMutated = function () { observable["notifySubscribers"](_latestValue); }
+ observable.valueWillMutate = function () { observable["notifySubscribers"](_latestValue, "beforeChange"); }
+ ko.utils.extend(observable, ko.observable['fn']);
+
+ ko.exportProperty(observable, "valueHasMutated", observable.valueHasMutated);
+ ko.exportProperty(observable, "valueWillMutate", observable.valueWillMutate);
+
return observable;
}
+
+ko.observable['fn'] = {
+ __ko_proto__: ko.observable,
+
+ "equalityComparer": function valuesArePrimitiveAndEqual(a, b) {
+ var oldValueIsPrimitive = (a === null) || (typeof(a) in primitiveTypes);
+ return oldValueIsPrimitive ? (a === b) : false;
+ }
+};
+
ko.isObservable = function (instance) {
if ((instance === null) || (instance === undefined) || (instance.__ko_proto__ === undefined)) return false;
if (instance.__ko_proto__ === ko.observable) return true;
return ko.isObservable(instance.__ko_proto__); // Walk the prototype chain
}
ko.isWriteableObservable = function (instance) {
- return (typeof instance == "function") && instance.__ko_proto__ === ko.observable;
-}/// <reference path="observable.js" />
+ // Observable
+ if ((typeof instance == "function") && instance.__ko_proto__ === ko.observable)
+ return true;
+ // Writeable dependent observable
+ if ((typeof instance == "function") && (instance.__ko_proto__ === ko.dependentObservable) && (instance.hasWriteFunction))
+ return true;
+ // Anything else
+ return false;
+}
+
+ko.exportSymbol('ko.observable', ko.observable);
+ko.exportSymbol('ko.isObservable', ko.isObservable);
+ko.exportSymbol('ko.isWriteableObservable', ko.isWriteableObservable);
ko.observableArray = function (initialValues) {
+ if (arguments.length == 0) {
+ // Zero-parameter constructor initializes to empty array
+ initialValues = [];
+ }
+ if ((initialValues !== null) && (initialValues !== undefined) && !('length' in initialValues))
+ throw new Error("The argument passed when initializing an observable array must be an array, or null, or undefined.");
+
var result = new ko.observable(initialValues);
+ ko.utils.extend(result, ko.observableArray['fn']);
+
+ ko.exportProperty(result, "remove", result.remove);
+ ko.exportProperty(result, "removeAll", result.removeAll);
+ ko.exportProperty(result, "destroy", result.destroy);
+ ko.exportProperty(result, "destroyAll", result.destroyAll);
+ ko.exportProperty(result, "indexOf", result.indexOf);
+ ko.exportProperty(result, "replace", result.replace);
+
+ return result;
+}
- ko.utils.arrayForEach(["pop", "push", "reverse", "shift", "sort", "splice", "unshift"], function (methodName) {
- result[methodName] = function () {
- var underlyingArray = result();
- var methodCallResult = underlyingArray[methodName].apply(underlyingArray, arguments);
- result.valueHasMutated();
- return methodCallResult;
- };
- });
-
- ko.utils.arrayForEach(["slice"], function (methodName) {
- result[methodName] = function () {
- var underlyingArray = result();
- return underlyingArray[methodName].apply(underlyingArray, arguments);
- };
- });
-
- result.remove = function (valueOrPredicate) {
- var underlyingArray = result();
- var remainingValues = [];
+ko.observableArray['fn'] = {
+ remove: function (valueOrPredicate) {
+ var underlyingArray = this();
var removedValues = [];
var predicate = typeof valueOrPredicate == "function" ? valueOrPredicate : function (value) { return value === valueOrPredicate; };
- for (var i = 0, j = underlyingArray.length; i < j; i++) {
+ for (var i = 0; i < underlyingArray.length; i++) {
var value = underlyingArray[i];
- if (!predicate(value))
- remainingValues.push(value);
- else
+ if (predicate(value)) {
+ if (removedValues.length === 0) {
+ this.valueWillMutate();
+ }
removedValues.push(value);
+ underlyingArray.splice(i, 1);
+ i--;
+ }
}
- result(remainingValues);
+ if (removedValues.length) {
+ this.valueHasMutated();
+ }
return removedValues;
- };
+ },
- result.removeAll = function (arrayOfValues) {
+ removeAll: function (arrayOfValues) {
+ // If you passed zero args, we remove everything
+ if (arrayOfValues === undefined) {
+ var underlyingArray = this();
+ var allValues = underlyingArray.slice(0);
+ this.valueWillMutate();
+ underlyingArray.splice(0, underlyingArray.length);
+ this.valueHasMutated();
+ return allValues;
+ }
+ // If you passed an arg, we interpret it as an array of entries to remove
if (!arrayOfValues)
return [];
- return result.remove(function (value) {
+ return this.remove(function (value) {
return ko.utils.arrayIndexOf(arrayOfValues, value) >= 0;
});
- };
+ },
- result.destroy = function (valueOrPredicate) {
- var underlyingArray = result();
- var predicate = typeof valueOrPredicate == "function" ? valueOrPredicate : function (value) { return value === valueOrPredicate; };
- for (var i = underlyingArray.length - 1; i >= 0; i--) {
- var value = underlyingArray[i];
- if (predicate(value))
- underlyingArray[i]._destroy = true;
- }
- result.valueHasMutated();
- };
-
- result.destroyAll = function (arrayOfValues) {
+ destroy: function (valueOrPredicate) {
+ var underlyingArray = this();
+ var predicate = typeof valueOrPredicate == "function" ? valueOrPredicate : function (value) { return value === valueOrPredicate; };
+ this.valueWillMutate();
+ for (var i = underlyingArray.length - 1; i >= 0; i--) {
+ var value = underlyingArray[i];
+ if (predicate(value))
+ underlyingArray[i]["_destroy"] = true;
+ }
+ this.valueHasMutated();
+ },
+
+ destroyAll: function (arrayOfValues) {
+ // If you passed zero args, we destroy everything
+ if (arrayOfValues === undefined)
+ return this.destroy(function() { return true });
+
+ // If you passed an arg, we interpret it as an array of entries to destroy
if (!arrayOfValues)
return [];
- return result.destroy(function (value) {
+ return this.destroy(function (value) {
return ko.utils.arrayIndexOf(arrayOfValues, value) >= 0;
- });
- };
+ });
+ },
- result.indexOf = function (item) {
- var underlyingArray = result();
+ indexOf: function (item) {
+ var underlyingArray = this();
return ko.utils.arrayIndexOf(underlyingArray, item);
+ },
+
+ replace: function(oldItem, newItem) {
+ var index = this.indexOf(oldItem);
+ if (index >= 0) {
+ this.valueWillMutate();
+ this()[index] = newItem;
+ this.valueHasMutated();
+ }
+ }
+}
+
+// Populate ko.observableArray.fn with read/write functions from native arrays
+ko.utils.arrayForEach(["pop", "push", "reverse", "shift", "sort", "splice", "unshift"], function (methodName) {
+ ko.observableArray['fn'][methodName] = function () {
+ var underlyingArray = this();
+ this.valueWillMutate();
+ var methodCallResult = underlyingArray[methodName].apply(underlyingArray, arguments);
+ this.valueHasMutated();
+ return methodCallResult;
};
+});
- return result;
-}/// <reference path="observable.js" />
+// Populate ko.observableArray.fn with read-only functions from native arrays
+ko.utils.arrayForEach(["slice"], function (methodName) {
+ ko.observableArray['fn'][methodName] = function () {
+ var underlyingArray = this();
+ return underlyingArray[methodName].apply(underlyingArray, arguments);
+ };
+});
-ko.dependentObservable = function (evaluatorFunction, evaluatorFunctionTarget, options) {
- if (typeof evaluatorFunction != "function")
+ko.exportSymbol('ko.observableArray', ko.observableArray);
+function prepareOptions(evaluatorFunctionOrOptions, evaluatorFunctionTarget, options) {
+ if (evaluatorFunctionOrOptions && typeof evaluatorFunctionOrOptions == "object") {
+ // Single-parameter syntax - everything is on this "options" param
+ options = evaluatorFunctionOrOptions;
+ } else {
+ // Multi-parameter syntax - construct the options according to the params passed
+ options = options || {};
+ options["read"] = evaluatorFunctionOrOptions || options["read"];
+ }
+ // By here, "options" is always non-null
+
+ if (typeof options["read"] != "function")
throw "Pass a function that returns the value of the dependentObservable";
+
+ return options;
+}
+ko.dependentObservable = function (evaluatorFunctionOrOptions, evaluatorFunctionTarget, options) {
+ var _latestValue,
+ _hasBeenEvaluated = false,
+ options = prepareOptions(evaluatorFunctionOrOptions, evaluatorFunctionTarget, options);
+
+ // Build "disposeWhenNodeIsRemoved" and "disposeWhenNodeIsRemovedCallback" option values
+ // (Note: "disposeWhenNodeIsRemoved" option both proactively disposes as soon as the node is removed using ko.removeNode(),
+ // plus adds a "disposeWhen" callback that, on each evaluation, disposes if the node was removed by some other means.)
+ var disposeWhenNodeIsRemoved = (typeof options["disposeWhenNodeIsRemoved"] == "object") ? options["disposeWhenNodeIsRemoved"] : null;
+ var disposeWhenNodeIsRemovedCallback = null;
+ if (disposeWhenNodeIsRemoved) {
+ disposeWhenNodeIsRemovedCallback = function() { dependentObservable.dispose() };
+ ko.utils.domNodeDisposal.addDisposeCallback(disposeWhenNodeIsRemoved, disposeWhenNodeIsRemovedCallback);
+ var existingDisposeWhenFunction = options["disposeWhen"];
+ options["disposeWhen"] = function () {
+ return (!ko.utils.domNodeIsAttachedToDocument(disposeWhenNodeIsRemoved))
+ || ((typeof existingDisposeWhenFunction == "function") && existingDisposeWhenFunction());
+ }
+ }
+
var _subscriptionsToDependencies = [];
function disposeAllSubscriptionsToDependencies() {
ko.utils.arrayForEach(_subscriptionsToDependencies, function (subscription) {
subscription.dispose();
});
_subscriptionsToDependencies = [];
}
+
+ var evaluationTimeoutInstance = null;
+ function evaluatePossiblyAsync() {
+ var throttleEvaluationTimeout = dependentObservable['throttleEvaluation'];
+ if (throttleEvaluationTimeout && throttleEvaluationTimeout >= 0) {
+ clearTimeout(evaluationTimeoutInstance);
+ evaluationTimeoutInstance = setTimeout(evaluateImmediate, throttleEvaluationTimeout);
+ } else
+ evaluateImmediate();
+ }
- function replaceSubscriptionsToDependencies(newDependencies) {
- disposeAllSubscriptionsToDependencies();
- ko.utils.arrayForEach(newDependencies, function (dependency) {
- _subscriptionsToDependencies.push(dependency.subscribe(evaluate));
- });
- };
-
- var _latestValue, _isFirstEvaluation = true;
- function evaluate() {
- if ((!_isFirstEvaluation) && options && typeof options.disposeWhen == "function") {
- if (options.disposeWhen()) {
+ function evaluateImmediate() {
+ // Don't dispose on first evaluation, because the "disposeWhen" callback might
+ // e.g., dispose when the associated DOM element isn't in the doc, and it's not
+ // going to be in the doc until *after* the first evaluation
+ if ((_hasBeenEvaluated) && typeof options["disposeWhen"] == "function") {
+ if (options["disposeWhen"]()) {
dependentObservable.dispose();
return;
}
}
try {
- ko.dependencyDetection.begin();
- _latestValue = evaluatorFunctionTarget ? evaluatorFunction.call(evaluatorFunctionTarget) : evaluatorFunction();
- } catch (ex) {
- throw ex;
+ disposeAllSubscriptionsToDependencies();
+ ko.dependencyDetection.begin(function(subscribable) {
+ _subscriptionsToDependencies.push(subscribable.subscribe(evaluatePossiblyAsync));
+ });
+ var valueForThis = options["owner"] || evaluatorFunctionTarget; // If undefined, it will default to "window" by convention. This might change in the future.
+ var newValue = options["read"].call(valueForThis);
+ dependentObservable["notifySubscribers"](_latestValue, "beforeChange");
+ _latestValue = newValue;
} finally {
- var distinctDependencies = ko.utils.arrayGetDistinctValues(ko.dependencyDetection.end());
- replaceSubscriptionsToDependencies(distinctDependencies);
+ ko.dependencyDetection.end();
}
- dependentObservable.notifySubscribers(_latestValue);
- _isFirstEvaluation = false;
+ dependentObservable["notifySubscribers"](_latestValue);
+ _hasBeenEvaluated = true;
}
function dependentObservable() {
- if (arguments.length > 0)
- throw "Cannot write a value to a dependentObservable. Do not pass any parameters to it";
-
- ko.dependencyDetection.registerDependency(dependentObservable);
- return _latestValue;
+ if (arguments.length > 0) {
+ if (typeof options["write"] === "function") {
+ // Writing a value
+ var valueForThis = options["owner"] || evaluatorFunctionTarget; // If undefined, it will default to "window" by convention. This might change in the future.
+ options["write"].apply(valueForThis, arguments);
+ } else {
+ throw "Cannot write a value to a dependentObservable unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters.";
+ }
+ } else {
+ // Reading the value
+ if (!_hasBeenEvaluated)
+ evaluateImmediate();
+ ko.dependencyDetection.registerDependency(dependentObservable);
+ return _latestValue;
+ }
}
- dependentObservable.__ko_proto__ = ko.dependentObservable;
dependentObservable.getDependenciesCount = function () { return _subscriptionsToDependencies.length; }
+ dependentObservable.hasWriteFunction = typeof options["write"] === "function";
dependentObservable.dispose = function () {
+ if (disposeWhenNodeIsRemoved)
+ ko.utils.domNodeDisposal.removeDisposeCallback(disposeWhenNodeIsRemoved, disposeWhenNodeIsRemovedCallback);
disposeAllSubscriptionsToDependencies();
};
-
+
ko.subscribable.call(dependentObservable);
- evaluate();
+ ko.utils.extend(dependentObservable, ko.dependentObservable['fn']);
+
+ if (options['deferEvaluation'] !== true)
+ evaluateImmediate();
+
+ ko.exportProperty(dependentObservable, 'dispose', dependentObservable.dispose);
+ ko.exportProperty(dependentObservable, 'getDependenciesCount', dependentObservable.getDependenciesCount);
+
return dependentObservable;
};
-ko.dependentObservable.__ko_proto__ = ko.observable;(function () {
+
+ko.dependentObservable['fn'] = {
+ __ko_proto__: ko.dependentObservable
+};
+
+ko.dependentObservable.__ko_proto__ = ko.observable;
+
+ko.exportSymbol('ko.dependentObservable', ko.dependentObservable);
+ko.exportSymbol('ko.computed', ko.dependentObservable); // Make "ko.computed" an alias for "ko.dependentObservable"
+(function() {
+ var maxNestedObservableDepth = 10; // Escape the (unlikely) pathalogical case where an observable's current value is itself (or similar reference cycle)
+
+ ko.toJS = function(rootObject) {
+ if (arguments.length == 0)
+ throw new Error("When calling ko.toJS, pass the object you want to convert.");
+
+ // We just unwrap everything at every level in the object graph
+ return mapJsObjectGraph(rootObject, function(valueToMap) {
+ // Loop because an observable's value might in turn be another observable wrapper
+ for (var i = 0; ko.isObservable(valueToMap) && (i < maxNestedObservableDepth); i++)
+ valueToMap = valueToMap();
+ return valueToMap;
+ });
+ };
+
+ ko.toJSON = function(rootObject) {
+ var plainJavaScriptObject = ko.toJS(rootObject);
+ return ko.utils.stringifyJson(plainJavaScriptObject);
+ };
+
+ function mapJsObjectGraph(rootObject, mapInputCallback, visitedObjects) {
+ visitedObjects = visitedObjects || new objectLookup();
+
+ rootObject = mapInputCallback(rootObject);
+ var canHaveProperties = (typeof rootObject == "object") && (rootObject !== null) && (rootObject !== undefined) && (!(rootObject instanceof Date));
+ if (!canHaveProperties)
+ return rootObject;
+
+ var outputProperties = rootObject instanceof Array ? [] : {};
+ visitedObjects.save(rootObject, outputProperties);
+
+ visitPropertiesOrArrayEntries(rootObject, function(indexer) {
+ var propertyValue = mapInputCallback(rootObject[indexer]);
+
+ switch (typeof propertyValue) {
+ case "boolean":
+ case "number":
+ case "string":
+ case "function":
+ outputProperties[indexer] = propertyValue;
+ break;
+ case "object":
+ case "undefined":
+ var previouslyMappedValue = visitedObjects.get(propertyValue);
+ outputProperties[indexer] = (previouslyMappedValue !== undefined)
+ ? previouslyMappedValue
+ : mapJsObjectGraph(propertyValue, mapInputCallback, visitedObjects);
+ break;
+ }
+ });
+
+ return outputProperties;
+ }
+
+ function visitPropertiesOrArrayEntries(rootObject, visitorCallback) {
+ if (rootObject instanceof Array) {
+ for (var i = 0; i < rootObject.length; i++)
+ visitorCallback(i);
+ } else {
+ for (var propertyName in rootObject)
+ visitorCallback(propertyName);
+ }
+ };
+
+ function objectLookup() {
+ var keys = [];
+ var values = [];
+ this.save = function(key, value) {
+ var existingIndex = ko.utils.arrayIndexOf(keys, key);
+ if (existingIndex >= 0)
+ values[existingIndex] = value;
+ else {
+ keys.push(key);
+ values.push(value);
+ }
+ };
+ this.get = function(key) {
+ var existingIndex = ko.utils.arrayIndexOf(keys, key);
+ return (existingIndex >= 0) ? values[existingIndex] : undefined;
+ };
+ };
+})();
+
+ko.exportSymbol('ko.toJS', ko.toJS);
+ko.exportSymbol('ko.toJSON', ko.toJSON);(function () {
+ var hasDomDataExpandoProperty = '__ko__hasDomDataOptionValue__';
+
// Normally, SELECT elements and their OPTIONs can only take value of type 'string' (because the values
// are stored on DOM attributes). ko.selectExtensions provides a way for SELECTs/OPTIONs to have values
// that are arbitrary objects. This is very convenient when implementing things like cascading dropdowns.
ko.selectExtensions = {
readValue : function(element) {
if (element.tagName == 'OPTION') {
- var valueAttributeValue = element.getAttribute("value");
- if (valueAttributeValue !== ko.bindingHandlers.options.optionValueDomDataKey)
- return valueAttributeValue;
- return ko.utils.domData.get(element, ko.bindingHandlers.options.optionValueDomDataKey);
+ if (element[hasDomDataExpandoProperty] === true)
+ return ko.utils.domData.get(element, ko.bindingHandlers.options.optionValueDomDataKey);
+ return element.getAttribute("value");
} else if (element.tagName == 'SELECT')
return element.selectedIndex >= 0 ? ko.selectExtensions.readValue(element.options[element.selectedIndex]) : undefined;
else
return element.value;
},
writeValue: function(element, value) {
- if (element.tagName == 'OPTION') {
- ko.utils.domData.set(element, ko.bindingHandlers.options.optionValueDomDataKey, value);
- element.value = ko.bindingHandlers.options.optionValueDomDataKey;
+ if (element.tagName == 'OPTION') {
+ switch(typeof value) {
+ case "string":
+ ko.utils.domData.set(element, ko.bindingHandlers.options.optionValueDomDataKey, undefined);
+ if (hasDomDataExpandoProperty in element) { // IE <= 8 throws errors if you delete non-existent properties from a DOM node
+ delete element[hasDomDataExpandoProperty];
+ }
+ element.value = value;
+ break;
+ default:
+ // Store arbitrary object using DomData
+ ko.utils.domData.set(element, ko.bindingHandlers.options.optionValueDomDataKey, value);
+ element[hasDomDataExpandoProperty] = true;
+
+ // Special treatment of numbers is just for backward compatibility. KO 1.2.1 wrote numerical values to element.value.
+ element.value = typeof value === "number" ? value : "";
+ break;
+ }
} else if (element.tagName == 'SELECT') {
for (var i = element.options.length - 1; i >= 0; i--) {
if (ko.selectExtensions.readValue(element.options[i]) == value) {
element.selectedIndex = i;
break;
}
}
- } else
+ } else {
+ if ((value === null) || (value === undefined))
+ value = "";
element.value = value;
+ }
}
- };
-})();/// <reference path="../utils.js" />
+ };
+})();
+ko.exportSymbol('ko.selectExtensions', ko.selectExtensions);
+ko.exportSymbol('ko.selectExtensions.readValue', ko.selectExtensions.readValue);
+ko.exportSymbol('ko.selectExtensions.writeValue', ko.selectExtensions.writeValue);
+
ko.jsonExpressionRewriting = (function () {
- var restoreCapturedTokensRegex = /\[ko_token_(\d+)\]/g;
- var javaScriptAssignmentTarget = /^[\_$a-z][\_$a-z]*(\[.*?\])*(\.[\_$a-z][\_$a-z]*(\[.*?\])*)*$/i;
+ var restoreCapturedTokensRegex = /\@ko_token_(\d+)\@/g;
+ var javaScriptAssignmentTarget = /^[\_$a-z][\_$a-z0-9]*(\[.*?\])*(\.[\_$a-z][\_$a-z0-9]*(\[.*?\])*)*$/i;
var javaScriptReservedWords = ["true", "false"];
function restoreTokens(string, tokens) {
- return string.replace(restoreCapturedTokensRegex, function (match, tokenIndex) {
- return tokens[tokenIndex];
- });
+ var prevValue = null;
+ while (string != prevValue) { // Keep restoring tokens until it no longer makes a difference (they may be nested)
+ prevValue = string;
+ string = string.replace(restoreCapturedTokensRegex, function (match, tokenIndex) {
+ return tokens[tokenIndex];
+ });
+ }
+ return string;
}
function isWriteableValue(expression) {
if (ko.utils.arrayIndexOf(javaScriptReservedWords, ko.utils.stringTrim(expression).toLowerCase()) >= 0)
return false;
return expression.match(javaScriptAssignmentTarget) !== null;
}
+ function ensureQuoted(key) {
+ var trimmedKey = ko.utils.stringTrim(key);
+ switch (trimmedKey.length && trimmedKey.charAt(0)) {
+ case "'":
+ case '"':
+ return key;
+ default:
+ return "'" + trimmedKey + "'";
+ }
+ }
+
return {
- parseJson: function (jsonString) {
- jsonString = ko.utils.stringTrim(jsonString);
- if (jsonString.length < 3)
- return {};
+ bindingRewriteValidators: [],
+
+ parseObjectLiteral: function(objectLiteralString) {
+ // A full tokeniser+lexer would add too much weight to this library, so here's a simple parser
+ // that is sufficient just to split an object literal string into a set of top-level key-value pairs
- // We're going to split on commas, so first extract any blocks that may contain commas other than those at the top level
+ var str = ko.utils.stringTrim(objectLiteralString);
+ if (str.length < 3)
+ return [];
+ if (str.charAt(0) === "{")// Ignore any braces surrounding the whole object literal
+ str = str.substring(1, str.length - 1);
+
+ // Pull out any string literals and regex literals
var tokens = [];
var tokenStart = null, tokenEndChar;
- for (var position = jsonString.charAt(0) == "{" ? 1 : 0; position < jsonString.length; position++) {
- var c = jsonString.charAt(position);
+ for (var position = 0; position < str.length; position++) {
+ var c = str.charAt(position);
if (tokenStart === null) {
switch (c) {
case '"':
case "'":
case "/":
tokenStart = position;
tokenEndChar = c;
break;
- case "{":
- tokenStart = position;
- tokenEndChar = "}";
- break;
- case "[":
- tokenStart = position;
- tokenEndChar = "]";
- break;
}
- } else if (c == tokenEndChar) {
- var token = jsonString.substring(tokenStart, position + 1);
+ } else if ((c == tokenEndChar) && (str.charAt(position - 1) !== "\\")) {
+ var token = str.substring(tokenStart, position + 1);
tokens.push(token);
- var replacement = "[ko_token_" + (tokens.length - 1) + "]";
- jsonString = jsonString.substring(0, tokenStart) + replacement + jsonString.substring(position + 1);
+ var replacement = "@ko_token_" + (tokens.length - 1) + "@";
+ str = str.substring(0, tokenStart) + replacement + str.substring(position + 1);
position -= (token.length - replacement.length);
tokenStart = null;
}
}
+ // Next pull out balanced paren, brace, and bracket blocks
+ tokenStart = null;
+ tokenEndChar = null;
+ var tokenDepth = 0, tokenStartChar = null;
+ for (var position = 0; position < str.length; position++) {
+ var c = str.charAt(position);
+ if (tokenStart === null) {
+ switch (c) {
+ case "{": tokenStart = position; tokenStartChar = c;
+ tokenEndChar = "}";
+ break;
+ case "(": tokenStart = position; tokenStartChar = c;
+ tokenEndChar = ")";
+ break;
+ case "[": tokenStart = position; tokenStartChar = c;
+ tokenEndChar = "]";
+ break;
+ }
+ }
+
+ if (c === tokenStartChar)
+ tokenDepth++;
+ else if (c === tokenEndChar) {
+ tokenDepth--;
+ if (tokenDepth === 0) {
+ var token = str.substring(tokenStart, position + 1);
+ tokens.push(token);
+ var replacement = "@ko_token_" + (tokens.length - 1) + "@";
+ str = str.substring(0, tokenStart) + replacement + str.substring(position + 1);
+ position -= (token.length - replacement.length);
+ tokenStart = null;
+ }
+ }
+ }
+
// Now we can safely split on commas to get the key/value pairs
- var result = {};
- var keyValuePairs = jsonString.split(",");
+ var result = [];
+ var keyValuePairs = str.split(",");
for (var i = 0, j = keyValuePairs.length; i < j; i++) {
var pair = keyValuePairs[i];
var colonPos = pair.indexOf(":");
if ((colonPos > 0) && (colonPos < pair.length - 1)) {
- var key = ko.utils.stringTrim(pair.substring(0, colonPos));
- var value = ko.utils.stringTrim(pair.substring(colonPos + 1));
- if (key.charAt(0) == "{")
- key = key.substring(1);
- if (value.charAt(value.length - 1) == "}")
- value = value.substring(0, value.length - 1);
- key = ko.utils.stringTrim(restoreTokens(key, tokens));
- value = ko.utils.stringTrim(restoreTokens(value, tokens));
- result[key] = value;
+ var key = pair.substring(0, colonPos);
+ var value = pair.substring(colonPos + 1);
+ result.push({ 'key': restoreTokens(key, tokens), 'value': restoreTokens(value, tokens) });
+ } else {
+ result.push({ 'unknown': restoreTokens(pair, tokens) });
}
}
- return result;
+ return result;
},
- insertPropertyAccessorsIntoJson: function (jsonString) {
- var parsed = ko.jsonExpressionRewriting.parseJson(jsonString);
- var propertyAccessorTokens = [];
- for (var key in parsed) {
- var value = parsed[key];
- if (isWriteableValue(value)) {
- if (propertyAccessorTokens.length > 0)
- propertyAccessorTokens.push(", ");
- propertyAccessorTokens.push(key + " : function(__ko_value) { " + value + " = __ko_value; }");
+ insertPropertyAccessorsIntoJson: function (objectLiteralStringOrKeyValueArray) {
+ var keyValueArray = typeof objectLiteralStringOrKeyValueArray === "string"
+ ? ko.jsonExpressionRewriting.parseObjectLiteral(objectLiteralStringOrKeyValueArray)
+ : objectLiteralStringOrKeyValueArray;
+ var resultStrings = [], propertyAccessorResultStrings = [];
+
+ var keyValueEntry;
+ for (var i = 0; keyValueEntry = keyValueArray[i]; i++) {
+ if (resultStrings.length > 0)
+ resultStrings.push(",");
+
+ if (keyValueEntry['key']) {
+ var quotedKey = ensureQuoted(keyValueEntry['key']), val = keyValueEntry['value'];
+ resultStrings.push(quotedKey);
+ resultStrings.push(":");
+ resultStrings.push(val);
+
+ if (isWriteableValue(ko.utils.stringTrim(val))) {
+ if (propertyAccessorResultStrings.length > 0)
+ propertyAccessorResultStrings.push(", ");
+ propertyAccessorResultStrings.push(quotedKey + " : function(__ko_value) { " + val + " = __ko_value; }");
+ }
+ } else if (keyValueEntry['unknown']) {
+ resultStrings.push(keyValueEntry['unknown']);
}
}
- if (propertyAccessorTokens.length > 0) {
- var allPropertyAccessors = propertyAccessorTokens.join("");
- jsonString = jsonString + ", '_ko_property_writers' : { " + allPropertyAccessors + " } ";
+ var combinedResult = resultStrings.join("");
+ if (propertyAccessorResultStrings.length > 0) {
+ var allPropertyAccessors = propertyAccessorResultStrings.join("");
+ combinedResult = combinedResult + ", '_ko_property_writers' : { " + allPropertyAccessors + " } ";
}
- return jsonString;
+ return combinedResult;
+ },
+
+ keyValueArrayContainsKey: function(keyValueArray, key) {
+ for (var i = 0; i < keyValueArray.length; i++)
+ if (ko.utils.stringTrim(keyValueArray[i]['key']) == key)
+ return true;
+ return false;
}
};
-})();/// <reference path="../subscribables/dependentObservable.js" />
+})();
-(function () {
- var bindingAttributeName = "data-bind";
+ko.exportSymbol('ko.jsonExpressionRewriting', ko.jsonExpressionRewriting);
+ko.exportSymbol('ko.jsonExpressionRewriting.bindingRewriteValidators', ko.jsonExpressionRewriting.bindingRewriteValidators);
+ko.exportSymbol('ko.jsonExpressionRewriting.parseObjectLiteral', ko.jsonExpressionRewriting.parseObjectLiteral);
+ko.exportSymbol('ko.jsonExpressionRewriting.insertPropertyAccessorsIntoJson', ko.jsonExpressionRewriting.insertPropertyAccessorsIntoJson);
+(function() {
+ // "Virtual elements" is an abstraction on top of the usual DOM API which understands the notion that comment nodes
+ // may be used to represent hierarchy (in addition to the DOM's natural hierarchy).
+ // If you call the DOM-manipulating functions on ko.virtualElements, you will be able to read and write the state
+ // of that virtual hierarchy
+ //
+ // The point of all this is to support containerless templates (e.g., <!-- ko foreach:someCollection -->blah<!-- /ko -->)
+ // without having to scatter special cases all over the binding and templating code.
+
+ // IE 9 cannot reliably read the "nodeValue" property of a comment node (see https://github.com/SteveSanderson/knockout/issues/186)
+ // but it does give them a nonstandard alternative property called "text" that it can read reliably. Other browsers don't have that property.
+ // So, use node.text where available, and node.nodeValue elsewhere
+ var commentNodesHaveTextProperty = document.createComment("test").text === "<!--test-->";
+
+ var startCommentRegex = commentNodesHaveTextProperty ? /^<!--\s*ko\s+(.*\:.*)\s*-->$/ : /^\s*ko\s+(.*\:.*)\s*$/;
+ var endCommentRegex = commentNodesHaveTextProperty ? /^<!--\s*\/ko\s*-->$/ : /^\s*\/ko\s*$/;
+ var htmlTagsWithOptionallyClosingChildren = { 'ul': true, 'ol': true };
+
+ function isStartComment(node) {
+ return (node.nodeType == 8) && (commentNodesHaveTextProperty ? node.text : node.nodeValue).match(startCommentRegex);
+ }
+
+ function isEndComment(node) {
+ return (node.nodeType == 8) && (commentNodesHaveTextProperty ? node.text : node.nodeValue).match(endCommentRegex);
+ }
+
+ function getVirtualChildren(startComment, allowUnbalanced) {
+ var currentNode = startComment;
+ var depth = 1;
+ var children = [];
+ while (currentNode = currentNode.nextSibling) {
+ if (isEndComment(currentNode)) {
+ depth--;
+ if (depth === 0)
+ return children;
+ }
+
+ children.push(currentNode);
+
+ if (isStartComment(currentNode))
+ depth++;
+ }
+ if (!allowUnbalanced)
+ throw new Error("Cannot find closing comment tag to match: " + startComment.nodeValue);
+ return null;
+ }
+
+ function getMatchingEndComment(startComment, allowUnbalanced) {
+ var allVirtualChildren = getVirtualChildren(startComment, allowUnbalanced);
+ if (allVirtualChildren) {
+ if (allVirtualChildren.length > 0)
+ return allVirtualChildren[allVirtualChildren.length - 1].nextSibling;
+ return startComment.nextSibling;
+ } else
+ return null; // Must have no matching end comment, and allowUnbalanced is true
+ }
+
+ function nodeArrayToText(nodeArray, cleanNodes) {
+ var texts = [];
+ for (var i = 0, j = nodeArray.length; i < j; i++) {
+ if (cleanNodes)
+ ko.utils.domNodeDisposal.cleanNode(nodeArray[i]);
+ texts.push(ko.utils.outerHTML(nodeArray[i]));
+ }
+ return String.prototype.concat.apply("", texts);
+ }
+
+ function getUnbalancedChildTags(node) {
+ // e.g., from <div>OK</div><!-- ko blah --><span>Another</span>, returns: <!-- ko blah --><span>Another</span>
+ // from <div>OK</div><!-- /ko --><!-- /ko -->, returns: <!-- /ko --><!-- /ko -->
+ var childNode = node.firstChild, captureRemaining = null;
+ if (childNode) {
+ do {
+ if (captureRemaining) // We already hit an unbalanced node and are now just scooping up all subsequent nodes
+ captureRemaining.push(childNode);
+ else if (isStartComment(childNode)) {
+ var matchingEndComment = getMatchingEndComment(childNode, /* allowUnbalanced: */ true);
+ if (matchingEndComment) // It's a balanced tag, so skip immediately to the end of this virtual set
+ childNode = matchingEndComment;
+ else
+ captureRemaining = [childNode]; // It's unbalanced, so start capturing from this point
+ } else if (isEndComment(childNode)) {
+ captureRemaining = [childNode]; // It's unbalanced (if it wasn't, we'd have skipped over it already), so start capturing
+ }
+ } while (childNode = childNode.nextSibling);
+ }
+ return captureRemaining;
+ }
+
+ ko.virtualElements = {
+ allowedBindings: {},
+
+ childNodes: function(node) {
+ return isStartComment(node) ? getVirtualChildren(node) : node.childNodes;
+ },
+
+ emptyNode: function(node) {
+ if (!isStartComment(node))
+ ko.utils.emptyDomNode(node);
+ else {
+ var virtualChildren = ko.virtualElements.childNodes(node);
+ for (var i = 0, j = virtualChildren.length; i < j; i++)
+ ko.removeNode(virtualChildren[i]);
+ }
+ },
+
+ setDomNodeChildren: function(node, childNodes) {
+ if (!isStartComment(node))
+ ko.utils.setDomNodeChildren(node, childNodes);
+ else {
+ ko.virtualElements.emptyNode(node);
+ var endCommentNode = node.nextSibling; // Must be the next sibling, as we just emptied the children
+ for (var i = 0, j = childNodes.length; i < j; i++)
+ endCommentNode.parentNode.insertBefore(childNodes[i], endCommentNode);
+ }
+ },
+
+ prepend: function(containerNode, nodeToPrepend) {
+ if (!isStartComment(containerNode)) {
+ if (containerNode.firstChild)
+ containerNode.insertBefore(nodeToPrepend, containerNode.firstChild);
+ else
+ containerNode.appendChild(nodeToPrepend);
+ } else {
+ // Start comments must always have a parent and at least one following sibling (the end comment)
+ containerNode.parentNode.insertBefore(nodeToPrepend, containerNode.nextSibling);
+ }
+ },
+
+ insertAfter: function(containerNode, nodeToInsert, insertAfterNode) {
+ if (!isStartComment(containerNode)) {
+ // Insert after insertion point
+ if (insertAfterNode.nextSibling)
+ containerNode.insertBefore(nodeToInsert, insertAfterNode.nextSibling);
+ else
+ containerNode.appendChild(nodeToInsert);
+ } else {
+ // Children of start comments must always have a parent and at least one following sibling (the end comment)
+ containerNode.parentNode.insertBefore(nodeToInsert, insertAfterNode.nextSibling);
+ }
+ },
+
+ nextSibling: function(node) {
+ if (!isStartComment(node)) {
+ if (node.nextSibling && isEndComment(node.nextSibling))
+ return undefined;
+ return node.nextSibling;
+ } else {
+ return getMatchingEndComment(node).nextSibling;
+ }
+ },
+
+ virtualNodeBindingValue: function(node) {
+ var regexMatch = isStartComment(node);
+ return regexMatch ? regexMatch[1] : null;
+ },
+
+ extractAnonymousTemplateIfVirtualElement: function(node) {
+ if (ko.virtualElements.virtualNodeBindingValue(node)) {
+ // Empty out the virtual children, and associate "node" with an anonymous template matching its previous virtual children
+ var virtualChildren = ko.virtualElements.childNodes(node);
+ var anonymousTemplateText = nodeArrayToText(virtualChildren, true);
+ ko.virtualElements.emptyNode(node);
+ new ko.templateSources.anonymousTemplate(node).text(anonymousTemplateText);
+ }
+ },
+
+ normaliseVirtualElementDomStructure: function(elementVerified) {
+ // Workaround for https://github.com/SteveSanderson/knockout/issues/155
+ // (IE <= 8 or IE 9 quirks mode parses your HTML weirdly, treating closing </li> tags as if they don't exist, thereby moving comment nodes
+ // that are direct descendants of <ul> into the preceding <li>)
+ if (!htmlTagsWithOptionallyClosingChildren[elementVerified.tagName.toLowerCase()])
+ return;
+
+ // Scan immediate children to see if they contain unbalanced comment tags. If they do, those comment tags
+ // must be intended to appear *after* that child, so move them there.
+ var childNode = elementVerified.firstChild;
+ if (childNode) {
+ do {
+ if (childNode.nodeType === 1) {
+ var unbalancedTags = getUnbalancedChildTags(childNode);
+ if (unbalancedTags) {
+ // Fix up the DOM by moving the unbalanced tags to where they most likely were intended to be placed - *after* the child
+ var nodeToInsertBefore = childNode.nextSibling;
+ for (var i = 0; i < unbalancedTags.length; i++) {
+ if (nodeToInsertBefore)
+ elementVerified.insertBefore(unbalancedTags[i], nodeToInsertBefore);
+ else
+ elementVerified.appendChild(unbalancedTags[i]);
+ }
+ }
+ }
+ } while (childNode = childNode.nextSibling);
+ }
+ }
+ };
+})();
+(function() {
+ var defaultBindingAttributeName = "data-bind";
+
+ ko.bindingProvider = function() { };
+
+ ko.utils.extend(ko.bindingProvider.prototype, {
+ 'nodeHasBindings': function(node) {
+ switch (node.nodeType) {
+ case 1: return node.getAttribute(defaultBindingAttributeName) != null; // Element
+ case 8: return ko.virtualElements.virtualNodeBindingValue(node) != null; // Comment node
+ default: return false;
+ }
+ },
+
+ 'getBindings': function(node, bindingContext) {
+ var bindingsString = this['getBindingsString'](node, bindingContext);
+ return bindingsString ? this['parseBindingsString'](bindingsString, bindingContext) : null;
+ },
+
+ // The following function is only used internally by this default provider.
+ // It's not part of the interface definition for a general binding provider.
+ 'getBindingsString': function(node, bindingContext) {
+ switch (node.nodeType) {
+ case 1: return node.getAttribute(defaultBindingAttributeName); // Element
+ case 8: return ko.virtualElements.virtualNodeBindingValue(node); // Comment node
+ default: return null;
+ }
+ },
+
+ // The following function is only used internally by this default provider.
+ // It's not part of the interface definition for a general binding provider.
+ 'parseBindingsString': function(bindingsString, bindingContext) {
+ try {
+ var viewModel = bindingContext['$data'];
+ var rewrittenBindings = " { " + ko.jsonExpressionRewriting.insertPropertyAccessorsIntoJson(bindingsString) + " } ";
+ return ko.utils.evalWithinScope(rewrittenBindings, viewModel === null ? window : viewModel, bindingContext);
+ } catch (ex) {
+ throw new Error("Unable to parse bindings.\nMessage: " + ex + ";\nBindings value: " + bindingsString);
+ }
+ }
+ });
+
+ ko.bindingProvider['instance'] = new ko.bindingProvider();
+})();
+
+ko.exportSymbol('ko.bindingProvider', ko.bindingProvider);(function () {
ko.bindingHandlers = {};
- function parseBindingAttribute(attributeText, viewModel) {
- try {
- var json = " { " + ko.jsonExpressionRewriting.insertPropertyAccessorsIntoJson(attributeText) + " } ";
- return ko.utils.evalWithinScope(json, viewModel === null ? window : viewModel);
- } catch (ex) {
- throw new Error("Unable to parse binding attribute.\nMessage: " + ex + ";\nAttribute value: " + attributeText);
+ ko.bindingContext = function(dataItem, parentBindingContext) {
+ this['$data'] = dataItem;
+ if (parentBindingContext) {
+ this['$parent'] = parentBindingContext['$data'];
+ this['$parents'] = (parentBindingContext['$parents'] || []).slice(0);
+ this['$parents'].unshift(this['$parent']);
+ this['$root'] = parentBindingContext['$root'];
+ } else {
+ this['$parents'] = [];
+ this['$root'] = dataItem;
}
}
+ ko.bindingContext.prototype['createChildContext'] = function (dataItem) {
+ return new ko.bindingContext(dataItem, this);
+ };
- function invokeBindingHandler(handler, element, dataValue, allBindings, viewModel) {
- handler(element, dataValue, allBindings, viewModel);
+ function validateThatBindingIsAllowedForVirtualElements(bindingName) {
+ var validator = ko.virtualElements.allowedBindings[bindingName];
+ if (!validator)
+ throw new Error("The binding '" + bindingName + "' cannot be used with virtual elements")
}
- ko.applyBindingsToNode = function (node, bindings, viewModel) {
- var isFirstEvaluation = true;
+ function applyBindingsToDescendantsInternal (viewModel, elementVerified) {
+ var currentChild, nextInQueue = elementVerified.childNodes[0];
+ while (currentChild = nextInQueue) {
+ // Keep a record of the next child *before* applying bindings, in case the binding removes the current child from its position
+ nextInQueue = ko.virtualElements.nextSibling(currentChild);
+ applyBindingsToNodeAndDescendantsInternal(viewModel, currentChild, false);
+ }
+ }
+
+ function applyBindingsToNodeAndDescendantsInternal (viewModel, nodeVerified, isRootNodeForBindingContext) {
+ var shouldBindDescendants = true;
+
+ // Perf optimisation: Apply bindings only if...
+ // (1) It's a root element for this binding context, as we will need to store the binding context on this node
+ // Note that we can't store binding contexts on non-elements (e.g., text nodes), as IE doesn't allow expando properties for those
+ // (2) It might have bindings (e.g., it has a data-bind attribute, or it's a marker for a containerless template)
+ var isElement = (nodeVerified.nodeType == 1);
+ if (isElement) // Workaround IE <= 8 HTML parsing weirdness
+ ko.virtualElements.normaliseVirtualElementDomStructure(nodeVerified);
+
+ var shouldApplyBindings = (isElement && isRootNodeForBindingContext) // Case (1)
+ || ko.bindingProvider['instance']['nodeHasBindings'](nodeVerified); // Case (2)
+ if (shouldApplyBindings)
+ shouldBindDescendants = applyBindingsToNodeInternal(nodeVerified, null, viewModel, isRootNodeForBindingContext).shouldBindDescendants;
+
+ if (isElement && shouldBindDescendants)
+ applyBindingsToDescendantsInternal(viewModel, nodeVerified);
+ }
+
+ function applyBindingsToNodeInternal (node, bindings, viewModelOrBindingContext, isRootNodeForBindingContext) {
+ // Need to be sure that inits are only run once, and updates never run until all the inits have been run
+ var initPhase = 0; // 0 = before all inits, 1 = during inits, 2 = after all inits
+
+ // Pre-process any anonymous template bounded by comment nodes
+ ko.virtualElements.extractAnonymousTemplateIfVirtualElement(node);
+
+ // Each time the dependentObservable is evaluated (after data changes),
+ // the binding attribute is reparsed so that it can pick out the correct
+ // model properties in the context of the changed data.
+ // DOM event callbacks need to be able to access this changed data,
+ // so we need a single parsedBindings variable (shared by all callbacks
+ // associated with this node's bindings) that all the closures can access.
+ var parsedBindings;
+ function makeValueAccessor(bindingKey) {
+ return function () { return parsedBindings[bindingKey] }
+ }
+ function parsedBindingsAccessor() {
+ return parsedBindings;
+ }
+
+ var bindingHandlerThatControlsDescendantBindings;
new ko.dependentObservable(
function () {
+ // Ensure we have a nonnull binding context to work with
+ var bindingContextInstance = viewModelOrBindingContext && (viewModelOrBindingContext instanceof ko.bindingContext)
+ ? viewModelOrBindingContext
+ : new ko.bindingContext(ko.utils.unwrapObservable(viewModelOrBindingContext));
+ var viewModel = bindingContextInstance['$data'];
+
+ // We only need to store the bindingContext at the root of the subtree where it applies
+ // as all descendants will be able to find it by scanning up their ancestry
+ if (isRootNodeForBindingContext)
+ ko.storedBindingContextForNode(node, bindingContextInstance);
+
+ // Use evaluatedBindings if given, otherwise fall back on asking the bindings provider to give us some bindings
var evaluatedBindings = (typeof bindings == "function") ? bindings() : bindings;
- var parsedBindings = evaluatedBindings || parseBindingAttribute(node.getAttribute(bindingAttributeName), viewModel);
-
- // First run all the inits, so bindings can register for notification on changes
- if (isFirstEvaluation) {
- for (var bindingKey in parsedBindings) {
- if (ko.bindingHandlers[bindingKey] && typeof ko.bindingHandlers[bindingKey].init == "function")
- invokeBindingHandler(ko.bindingHandlers[bindingKey].init, node, parsedBindings[bindingKey], parsedBindings, viewModel);
- }
+ parsedBindings = evaluatedBindings || ko.bindingProvider['instance']['getBindings'](node, bindingContextInstance);
+
+ if (parsedBindings) {
+ // First run all the inits, so bindings can register for notification on changes
+ if (initPhase === 0) {
+ initPhase = 1;
+ for (var bindingKey in parsedBindings) {
+ var binding = ko.bindingHandlers[bindingKey];
+ if (binding && node.nodeType === 8)
+ validateThatBindingIsAllowedForVirtualElements(bindingKey);
+
+ if (binding && typeof binding["init"] == "function") {
+ var handlerInitFn = binding["init"];
+ var initResult = handlerInitFn(node, makeValueAccessor(bindingKey), parsedBindingsAccessor, viewModel, bindingContextInstance);
+
+ // If this binding handler claims to control descendant bindings, make a note of this
+ if (initResult && initResult['controlsDescendantBindings']) {
+ if (bindingHandlerThatControlsDescendantBindings !== undefined)
+ throw new Error("Multiple bindings (" + bindingHandlerThatControlsDescendantBindings + " and " + bindingKey + ") are trying to control descendant bindings of the same element. You cannot use these bindings together on the same element.");
+ bindingHandlerThatControlsDescendantBindings = bindingKey;
+ }
+ }
+ }
+ initPhase = 2;
+ }
+
+ // ... then run all the updates, which might trigger changes even on the first evaluation
+ if (initPhase === 2) {
+ for (var bindingKey in parsedBindings) {
+ var binding = ko.bindingHandlers[bindingKey];
+ if (binding && typeof binding["update"] == "function") {
+ var handlerUpdateFn = binding["update"];
+ handlerUpdateFn(node, makeValueAccessor(bindingKey), parsedBindingsAccessor, viewModel, bindingContextInstance);
+ }
+ }
+ }
}
-
- // ... then run all the updates, which might trigger changes even on the first evaluation
- for (var bindingKey in parsedBindings) {
- if (ko.bindingHandlers[bindingKey] && typeof ko.bindingHandlers[bindingKey].update == "function")
- invokeBindingHandler(ko.bindingHandlers[bindingKey].update, node, parsedBindings[bindingKey], parsedBindings, viewModel);
- }
},
null,
- { disposeWhen: function () { return !ko.utils.domNodeIsAttachedToDocument(node); } }
+ { 'disposeWhenNodeIsRemoved' : node }
);
- isFirstEvaluation = false;
+
+ return {
+ shouldBindDescendants: bindingHandlerThatControlsDescendantBindings === undefined
+ };
};
- ko.applyBindings = function (rootNode, viewModel) {
- var elemsWithBindingAttribute = ko.utils.getElementsHavingAttribute(rootNode, bindingAttributeName);
- ko.utils.arrayForEach(elemsWithBindingAttribute, function (element) {
- ko.applyBindingsToNode(element, null, viewModel);
- });
+ var storedBindingContextDomDataKey = "__ko_bindingContext__";
+ ko.storedBindingContextForNode = function (node, bindingContext) {
+ if (arguments.length == 2)
+ ko.utils.domData.set(node, storedBindingContextDomDataKey, bindingContext);
+ else
+ return ko.utils.domData.get(node, storedBindingContextDomDataKey);
+ }
+
+ ko.applyBindingsToNode = function (node, bindings, viewModel) {
+ if (node.nodeType === 1) // If it's an element, workaround IE <= 8 HTML parsing weirdness
+ ko.virtualElements.normaliseVirtualElementDomStructure(node);
+ return applyBindingsToNodeInternal(node, bindings, viewModel, true);
};
-})();/// <reference path="bindingAttributeSyntax.js" />
-ko.bindingHandlers.click = {
- init: function (element, value, allBindings, viewModel) {
- ko.utils.registerEventHandler(element, "click", function (event) {
- try { value.call(viewModel); }
- finally {
- if (event.preventDefault)
- event.preventDefault();
- else
- event.returnValue = false;
- }
- });
+ ko.applyBindingsToDescendants = function(viewModel, rootNode) {
+ if (rootNode.nodeType === 1)
+ applyBindingsToDescendantsInternal(viewModel, rootNode);
+ };
+
+ ko.applyBindings = function (viewModel, rootNode) {
+ if (rootNode && (rootNode.nodeType !== 1) && (rootNode.nodeType !== 8))
+ throw new Error("ko.applyBindings: first parameter should be your view model; second parameter should be a DOM node");
+ rootNode = rootNode || window.document.body; // Make "rootNode" parameter optional
+
+ applyBindingsToNodeAndDescendantsInternal(viewModel, rootNode, true);
+ };
+
+ // Retrieving binding context from arbitrary nodes
+ ko.contextFor = function(node) {
+ // We can only do something meaningful for elements and comment nodes (in particular, not text nodes, as IE can't store domdata for them)
+ switch (node.nodeType) {
+ case 1:
+ case 8:
+ var context = ko.storedBindingContextForNode(node);
+ if (context) return context;
+ if (node.parentNode) return ko.contextFor(node.parentNode);
+ break;
+ }
+ return undefined;
+ };
+ ko.dataFor = function(node) {
+ var context = ko.contextFor(node);
+ return context ? context['$data'] : undefined;
+ };
+
+ ko.exportSymbol('ko.bindingHandlers', ko.bindingHandlers);
+ ko.exportSymbol('ko.applyBindings', ko.applyBindings);
+ ko.exportSymbol('ko.applyBindingsToDescendants', ko.applyBindingsToDescendants);
+ ko.exportSymbol('ko.applyBindingsToNode', ko.applyBindingsToNode);
+ ko.exportSymbol('ko.contextFor', ko.contextFor);
+ ko.exportSymbol('ko.dataFor', ko.dataFor);
+})();// For certain common events (currently just 'click'), allow a simplified data-binding syntax
+// e.g. click:handler instead of the usual full-length event:{click:handler}
+var eventHandlersWithShortcuts = ['click'];
+ko.utils.arrayForEach(eventHandlersWithShortcuts, function(eventName) {
+ ko.bindingHandlers[eventName] = {
+ 'init': function(element, valueAccessor, allBindingsAccessor, viewModel) {
+ var newValueAccessor = function () {
+ var result = {};
+ result[eventName] = valueAccessor();
+ return result;
+ };
+ return ko.bindingHandlers['event']['init'].call(this, element, newValueAccessor, allBindingsAccessor, viewModel);
+ }
+ }
+});
+
+
+ko.bindingHandlers['event'] = {
+ 'init' : function (element, valueAccessor, allBindingsAccessor, viewModel) {
+ var eventsToHandle = valueAccessor() || {};
+ for(var eventNameOutsideClosure in eventsToHandle) {
+ (function() {
+ var eventName = eventNameOutsideClosure; // Separate variable to be captured by event handler closure
+ if (typeof eventName == "string") {
+ ko.utils.registerEventHandler(element, eventName, function (event) {
+ var handlerReturnValue;
+ var handlerFunction = valueAccessor()[eventName];
+ if (!handlerFunction)
+ return;
+ var allBindings = allBindingsAccessor();
+
+ try {
+ // Take all the event args, and prefix with the viewmodel
+ var argsForHandler = ko.utils.makeArray(arguments);
+ argsForHandler.unshift(viewModel);
+ handlerReturnValue = handlerFunction.apply(viewModel, argsForHandler);
+ } finally {
+ if (handlerReturnValue !== true) { // Normally we want to prevent default action. Developer can override this be explicitly returning true.
+ if (event.preventDefault)
+ event.preventDefault();
+ else
+ event.returnValue = false;
+ }
+ }
+
+ var bubble = allBindings[eventName + 'Bubble'] !== false;
+ if (!bubble) {
+ event.cancelBubble = true;
+ if (event.stopPropagation)
+ event.stopPropagation();
+ }
+ });
+ }
+ })();
+ }
}
};
-ko.bindingHandlers.submit = {
- init: function (element, value, allBindings, viewModel) {
- if (typeof value != "function")
- throw new Error("The value for a submit binding must be a function to invoke on submit");
+ko.bindingHandlers['submit'] = {
+ 'init': function (element, valueAccessor, allBindingsAccessor, viewModel) {
+ if (typeof valueAccessor() != "function")
+ throw new Error("The value for a submit binding must be a function");
ko.utils.registerEventHandler(element, "submit", function (event) {
- try { value.call(viewModel, element); }
+ var handlerReturnValue;
+ var value = valueAccessor();
+ try { handlerReturnValue = value.call(viewModel, element); }
finally {
- if (event.preventDefault)
- event.preventDefault();
- else
- event.returnValue = false;
+ if (handlerReturnValue !== true) { // Normally we want to prevent default action. Developer can override this be explicitly returning true.
+ if (event.preventDefault)
+ event.preventDefault();
+ else
+ event.returnValue = false;
+ }
}
});
}
};
-ko.bindingHandlers.visible = {
- update: function (element, value) {
- value = ko.utils.unwrapObservable(value);
+ko.bindingHandlers['visible'] = {
+ 'update': function (element, valueAccessor) {
+ var value = ko.utils.unwrapObservable(valueAccessor());
var isCurrentlyVisible = !(element.style.display == "none");
if (value && !isCurrentlyVisible)
element.style.display = "";
else if ((!value) && isCurrentlyVisible)
element.style.display = "none";
}
}
-ko.bindingHandlers.enable = {
- update: function (element, value) {
- value = ko.utils.unwrapObservable(value);
+ko.bindingHandlers['enable'] = {
+ 'update': function (element, valueAccessor) {
+ var value = ko.utils.unwrapObservable(valueAccessor());
if (value && element.disabled)
element.removeAttribute("disabled");
else if ((!value) && (!element.disabled))
element.disabled = true;
}
};
-ko.bindingHandlers.disable = { update: function (element, value) { ko.bindingHandlers.enable.update(element, !ko.utils.unwrapObservable(value)); } };
+ko.bindingHandlers['disable'] = {
+ 'update': function (element, valueAccessor) {
+ ko.bindingHandlers['enable']['update'](element, function() { return !ko.utils.unwrapObservable(valueAccessor()) });
+ }
+};
-ko.bindingHandlers.value = {
- init: function (element, value, allBindings) {
- var eventName = allBindings.valueUpdate || "change";
- if (ko.isWriteableObservable(value))
- ko.utils.registerEventHandler(element, eventName, function () {
- value(ko.selectExtensions.readValue(this));
- });
- else if (allBindings._ko_property_writers && allBindings._ko_property_writers.value)
- ko.utils.registerEventHandler(element, eventName, function () {
- allBindings._ko_property_writers.value(ko.selectExtensions.readValue(this));
- });
+function ensureDropdownSelectionIsConsistentWithModelValue(element, modelValue, preferModelValue) {
+ if (preferModelValue) {
+ if (modelValue !== ko.selectExtensions.readValue(element))
+ ko.selectExtensions.writeValue(element, modelValue);
+ }
+
+ // No matter which direction we're syncing in, we want the end result to be equality between dropdown value and model value.
+ // If they aren't equal, either we prefer the dropdown value, or the model value couldn't be represented, so either way,
+ // change the model value to match the dropdown.
+ if (modelValue !== ko.selectExtensions.readValue(element))
+ ko.utils.triggerEvent(element, "change");
+};
+
+ko.bindingHandlers['value'] = {
+ 'init': function (element, valueAccessor, allBindingsAccessor) {
+ // Always catch "change" event; possibly other events too if asked
+ var eventsToCatch = ["change"];
+ var requestedEventsToCatch = allBindingsAccessor()["valueUpdate"];
+ if (requestedEventsToCatch) {
+ if (typeof requestedEventsToCatch == "string") // Allow both individual event names, and arrays of event names
+ requestedEventsToCatch = [requestedEventsToCatch];
+ ko.utils.arrayPushAll(eventsToCatch, requestedEventsToCatch);
+ eventsToCatch = ko.utils.arrayGetDistinctValues(eventsToCatch);
+ }
+
+ ko.utils.arrayForEach(eventsToCatch, function(eventName) {
+ // The syntax "after<eventname>" means "run the handler asynchronously after the event"
+ // This is useful, for example, to catch "keydown" events after the browser has updated the control
+ // (otherwise, ko.selectExtensions.readValue(this) will receive the control's value *before* the key event)
+ var handleEventAsynchronously = false;
+ if (ko.utils.stringStartsWith(eventName, "after")) {
+ handleEventAsynchronously = true;
+ eventName = eventName.substring("after".length);
+ }
+ var runEventHandler = handleEventAsynchronously ? function(handler) { setTimeout(handler, 0) }
+ : function(handler) { handler() };
+
+ ko.utils.registerEventHandler(element, eventName, function () {
+ runEventHandler(function() {
+ var modelValue = valueAccessor();
+ var elementValue = ko.selectExtensions.readValue(element);
+ if (ko.isWriteableObservable(modelValue))
+ modelValue(elementValue);
+ else {
+ var allBindings = allBindingsAccessor();
+ if (allBindings['_ko_property_writers'] && allBindings['_ko_property_writers']['value'])
+ allBindings['_ko_property_writers']['value'](elementValue);
+ }
+ });
+ });
+ });
},
- update: function (element, value) {
- var newValue = ko.utils.unwrapObservable(value);
- if (newValue != ko.selectExtensions.readValue(element)) {
+ 'update': function (element, valueAccessor) {
+ var newValue = ko.utils.unwrapObservable(valueAccessor());
+ var elementValue = ko.selectExtensions.readValue(element);
+ var valueHasChanged = (newValue != elementValue);
+
+ // JavaScript's 0 == "" behavious is unfortunate here as it prevents writing 0 to an empty text box (loose equality suggests the values are the same).
+ // We don't want to do a strict equality comparison as that is more confusing for developers in certain cases, so we specifically special case 0 != "" here.
+ if ((newValue === 0) && (elementValue !== 0) && (elementValue !== "0"))
+ valueHasChanged = true;
+
+ if (valueHasChanged) {
var applyValueAction = function () { ko.selectExtensions.writeValue(element, newValue); };
applyValueAction();
// Workaround for IE6 bug: It won't reliably apply values to SELECT nodes during the same execution thread
// right after you've changed the set of OPTION nodes on it. So for that node type, we'll schedule a second thread
// to apply the value as well.
var alsoApplyAsynchronously = element.tagName == "SELECT";
if (alsoApplyAsynchronously)
setTimeout(applyValueAction, 0);
}
+
+ // If you try to set a model value that can't be represented in an already-populated dropdown, reject that change,
+ // because you're not allowed to have a model value that disagrees with a visible UI selection.
+ if ((element.tagName == "SELECT") && (element.length > 0))
+ ensureDropdownSelectionIsConsistentWithModelValue(element, newValue, /* preferModelValue */ false);
}
};
-ko.bindingHandlers.options = {
- update: function (element, value, allBindings) {
+ko.bindingHandlers['options'] = {
+ 'update': function (element, valueAccessor, allBindingsAccessor) {
if (element.tagName != "SELECT")
throw new Error("options binding applies only to SELECT elements");
+ var selectWasPreviouslyEmpty = element.length == 0;
var previousSelectedValues = ko.utils.arrayMap(ko.utils.arrayFilter(element.childNodes, function (node) {
return node.tagName && node.tagName == "OPTION" && node.selected;
}), function (node) {
return ko.selectExtensions.readValue(node) || node.innerText || node.textContent;
});
+ var previousScrollTop = element.scrollTop;
+ element.scrollTop = 0; // Workaround for a Chrome rendering bug. Note that we restore the scroll position later. (https://github.com/SteveSanderson/knockout/issues/215)
- value = ko.utils.unwrapObservable(value);
+ var value = ko.utils.unwrapObservable(valueAccessor());
var selectedValue = element.value;
- ko.utils.emptyDomNode(element);
+ // Remove all existing <option>s.
+ // Need to use .remove() rather than .removeChild() for <option>s otherwise IE behaves oddly (https://github.com/SteveSanderson/knockout/issues/134)
+ while (element.length > 0) {
+ ko.cleanNode(element.options[0]);
+ element.remove(0);
+ }
+
if (value) {
+ var allBindings = allBindingsAccessor();
if (typeof value.length != "number")
value = [value];
- if (allBindings.optionsCaption) {
+ if (allBindings['optionsCaption']) {
var option = document.createElement("OPTION");
- option.innerHTML = allBindings.optionsCaption;
+ ko.utils.setHtml(option, allBindings['optionsCaption']);
ko.selectExtensions.writeValue(option, undefined);
element.appendChild(option);
}
for (var i = 0, j = value.length; i < j; i++) {
var option = document.createElement("OPTION");
- var optionValue = typeof allBindings.optionsValue == "string" ? value[i][allBindings.optionsValue] : value[i];
- if (typeof optionValue == 'object')
- ko.selectExtensions.writeValue(option, optionValue);
+
+ // Apply a value to the option element
+ var optionValue = typeof allBindings['optionsValue'] == "string" ? value[i][allBindings['optionsValue']] : value[i];
+ optionValue = ko.utils.unwrapObservable(optionValue);
+ ko.selectExtensions.writeValue(option, optionValue);
+
+ // Apply some text to the option element
+ var optionsTextValue = allBindings['optionsText'];
+ var optionText;
+ if (typeof optionsTextValue == "function")
+ optionText = optionsTextValue(value[i]); // Given a function; run it against the data value
+ else if (typeof optionsTextValue == "string")
+ optionText = value[i][optionsTextValue]; // Given a string; treat it as a property name on the data value
else
- option.value = optionValue.toString();
- option.innerHTML = (typeof allBindings.optionsText == "string" ? value[i][allBindings.optionsText] : optionValue).toString();
+ optionText = optionValue; // Given no optionsText arg; use the data value itself
+ if ((optionText === null) || (optionText === undefined))
+ optionText = "";
+
+ ko.utils.setTextContent(option, optionText);
+
element.appendChild(option);
}
// IE6 doesn't like us to assign selection to OPTION nodes before they're added to the document.
// That's why we first added them without selection. Now it's time to set the selection.
@@ -927,37 +2164,53 @@
if (ko.utils.arrayIndexOf(previousSelectedValues, ko.selectExtensions.readValue(newOptions[i])) >= 0) {
ko.utils.setOptionNodeSelectionState(newOptions[i], true);
countSelectionsRetained++;
}
}
+
+ if (previousScrollTop)
+ element.scrollTop = previousScrollTop;
+
+ if (selectWasPreviouslyEmpty && ('value' in allBindings)) {
+ // Ensure consistency between model value and selected option.
+ // If the dropdown is being populated for the first time here (or was otherwise previously empty),
+ // the dropdown selection state is meaningless, so we preserve the model value.
+ ensureDropdownSelectionIsConsistentWithModelValue(element, ko.utils.unwrapObservable(allBindings['value']), /* preferModelValue */ true);
+ }
}
}
};
-ko.bindingHandlers.options.optionValueDomDataKey = '__ko.bindingHandlers.options.optionValueDomData__';
+ko.bindingHandlers['options'].optionValueDomDataKey = '__ko.optionValueDomData__';
-ko.bindingHandlers.selectedOptions = {
+ko.bindingHandlers['selectedOptions'] = {
getSelectedValuesFromSelectNode: function (selectNode) {
var result = [];
var nodes = selectNode.childNodes;
for (var i = 0, j = nodes.length; i < j; i++) {
var node = nodes[i];
if ((node.tagName == "OPTION") && node.selected)
result.push(ko.selectExtensions.readValue(node));
}
return result;
},
- init: function (element, value, allBindings) {
- if (ko.isWriteableObservable(value))
- ko.utils.registerEventHandler(element, "change", function () { value(ko.bindingHandlers.selectedOptions.getSelectedValuesFromSelectNode(this)); });
- else if (allBindings._ko_property_writers && allBindings._ko_property_writers.value)
- ko.utils.registerEventHandler(element, "change", function () { allBindings._ko_property_writers.value(ko.bindingHandlers.selectedOptions.getSelectedValuesFromSelectNode(this)); });
+ 'init': function (element, valueAccessor, allBindingsAccessor) {
+ ko.utils.registerEventHandler(element, "change", function () {
+ var value = valueAccessor();
+ if (ko.isWriteableObservable(value))
+ value(ko.bindingHandlers['selectedOptions'].getSelectedValuesFromSelectNode(this));
+ else {
+ var allBindings = allBindingsAccessor();
+ if (allBindings['_ko_property_writers'] && allBindings['_ko_property_writers']['value'])
+ allBindings['_ko_property_writers']['value'](ko.bindingHandlers['selectedOptions'].getSelectedValuesFromSelectNode(this));
+ }
+ });
},
- update: function (element, value) {
+ 'update': function (element, valueAccessor) {
if (element.tagName != "SELECT")
throw new Error("values binding applies only to SELECT elements");
- var newValue = ko.utils.unwrapObservable(value);
+ var newValue = ko.utils.unwrapObservable(valueAccessor());
if (newValue && typeof newValue.length == "number") {
var nodes = element.childNodes;
for (var i = 0, j = nodes.length; i < j; i++) {
var node = nodes[i];
if (node.tagName == "OPTION")
@@ -965,253 +2218,682 @@
}
}
}
};
-ko.bindingHandlers.text = {
- update: function (element, value) {
- value = ko.utils.unwrapObservable(value);
- typeof element.innerText == "string" ? element.innerText = value
- : element.textContent = value;
+ko.bindingHandlers['text'] = {
+ 'update': function (element, valueAccessor) {
+ ko.utils.setTextContent(element, valueAccessor());
}
};
-ko.bindingHandlers.css = {
- update: function (element, value) {
- value = value || {};
+ko.bindingHandlers['html'] = {
+ 'init': function() {
+ // Prevent binding on the dynamically-injected HTML (as developers are unlikely to expect that, and it has security implications)
+ return { 'controlsDescendantBindings': true };
+ },
+ 'update': function (element, valueAccessor) {
+ var value = ko.utils.unwrapObservable(valueAccessor());
+ ko.utils.setHtml(element, value);
+ }
+};
+
+ko.bindingHandlers['css'] = {
+ 'update': function (element, valueAccessor) {
+ var value = ko.utils.unwrapObservable(valueAccessor() || {});
for (var className in value) {
if (typeof className == "string") {
var shouldHaveClass = ko.utils.unwrapObservable(value[className]);
ko.utils.toggleDomNodeCssClass(element, className, shouldHaveClass);
}
}
}
};
-ko.bindingHandlers.style = {
- update: function (element, value) {
- value = ko.utils.unwrapObservable(value || {});
+ko.bindingHandlers['style'] = {
+ 'update': function (element, valueAccessor) {
+ var value = ko.utils.unwrapObservable(valueAccessor() || {});
for (var styleName in value) {
if (typeof styleName == "string") {
var styleValue = ko.utils.unwrapObservable(value[styleName]);
element.style[styleName] = styleValue || ""; // Empty string removes the value, whereas null/undefined have no effect
}
}
}
};
-ko.bindingHandlers.uniqueName = {
- init: function (element, value) {
- if (value) {
- element.name = "ko_unique_" + (++ko.bindingHandlers.uniqueName.currentIndex);
+ko.bindingHandlers['uniqueName'] = {
+ 'init': function (element, valueAccessor) {
+ if (valueAccessor()) {
+ element.name = "ko_unique_" + (++ko.bindingHandlers['uniqueName'].currentIndex);
- // Workaround IE 6 issue - http://www.matts411.com/post/setting_the_name_attribute_in_ie_dom/
- if (/MSIE 6/i.test(navigator.userAgent))
- element.mergeAttributes(document.createElement("<INPUT name='" + element.name + "'/>"), false);
+ // Workaround IE 6/7 issue
+ // - https://github.com/SteveSanderson/knockout/issues/197
+ // - http://www.matts411.com/post/setting_the_name_attribute_in_ie_dom/
+ if (ko.utils.isIe6 || ko.utils.isIe7)
+ element.mergeAttributes(document.createElement("<input name='" + element.name + "'/>"), false);
}
}
};
-ko.bindingHandlers.uniqueName.currentIndex = 0;
+ko.bindingHandlers['uniqueName'].currentIndex = 0;
-ko.bindingHandlers.checked = {
- init: function (element, value, allBindings) {
- if (ko.isWriteableObservable(value)) {
- var updateHandler;
- if (element.type == "checkbox")
- updateHandler = function () { value(this.checked) };
- else if (element.type == "radio")
- updateHandler = function () { if (this.checked) value(this.value) };
- if (updateHandler) {
- ko.utils.registerEventHandler(element, "change", updateHandler);
- ko.utils.registerEventHandler(element, "click", updateHandler);
+ko.bindingHandlers['checked'] = {
+ 'init': function (element, valueAccessor, allBindingsAccessor) {
+ var updateHandler = function() {
+ var valueToWrite;
+ if (element.type == "checkbox") {
+ valueToWrite = element.checked;
+ } else if ((element.type == "radio") && (element.checked)) {
+ valueToWrite = element.value;
+ } else {
+ return; // "checked" binding only responds to checkboxes and selected radio buttons
}
- } else if (allBindings._ko_property_writers && allBindings._ko_property_writers.checked) {
- var updateHandler;
- if (element.type == "checkbox")
- updateHandler = function () { allBindings._ko_property_writers.checked(this.checked) };
- else if (element.type == "radio")
- updateHandler = function () { if (this.checked) allBindings._ko_property_writers.checked(this.value) };
- if (updateHandler) {
- ko.utils.registerEventHandler(element, "change", updateHandler);
- ko.utils.registerEventHandler(element, "click", updateHandler);
+
+ var modelValue = valueAccessor();
+ if ((element.type == "checkbox") && (ko.utils.unwrapObservable(modelValue) instanceof Array)) {
+ // For checkboxes bound to an array, we add/remove the checkbox value to that array
+ // This works for both observable and non-observable arrays
+ var existingEntryIndex = ko.utils.arrayIndexOf(ko.utils.unwrapObservable(modelValue), element.value);
+ if (element.checked && (existingEntryIndex < 0))
+ modelValue.push(element.value);
+ else if ((!element.checked) && (existingEntryIndex >= 0))
+ modelValue.splice(existingEntryIndex, 1);
+ } else if (ko.isWriteableObservable(modelValue)) {
+ if (modelValue() !== valueToWrite) { // Suppress repeated events when there's nothing new to notify (some browsers raise them)
+ modelValue(valueToWrite);
+ }
+ } else {
+ var allBindings = allBindingsAccessor();
+ if (allBindings['_ko_property_writers'] && allBindings['_ko_property_writers']['checked']) {
+ allBindings['_ko_property_writers']['checked'](valueToWrite);
+ }
}
- }
+ };
+ ko.utils.registerEventHandler(element, "click", updateHandler);
// IE 6 won't allow radio buttons to be selected unless they have a name
if ((element.type == "radio") && !element.name)
- ko.bindingHandlers.uniqueName.init(element, true);
+ ko.bindingHandlers['uniqueName']['init'](element, function() { return true });
},
- update: function (element, value) {
- value = ko.utils.unwrapObservable(value);
- if (element.type == "checkbox")
- element.checked = value;
- else if (element.type == "radio")
+ 'update': function (element, valueAccessor) {
+ var value = ko.utils.unwrapObservable(valueAccessor());
+
+ if (element.type == "checkbox") {
+ if (value instanceof Array) {
+ // When bound to an array, the checkbox being checked represents its value being present in that array
+ element.checked = ko.utils.arrayIndexOf(value, element.value) >= 0;
+ } else {
+ // When bound to anything other value (not an array), the checkbox being checked represents the value being trueish
+ element.checked = value;
+ }
+ } else if (element.type == "radio") {
element.checked = (element.value == value);
+ }
}
-};/// <reference path="../utils.js" />
+};
-ko.templateEngine = function () {
- this.renderTemplate = function (templateName, data, options) {
- throw "Override renderTemplate in your ko.templateEngine subclass";
+ko.bindingHandlers['attr'] = {
+ 'update': function(element, valueAccessor, allBindingsAccessor) {
+ var value = ko.utils.unwrapObservable(valueAccessor()) || {};
+ for (var attrName in value) {
+ if (typeof attrName == "string") {
+ var attrValue = ko.utils.unwrapObservable(value[attrName]);
+
+ // To cover cases like "attr: { checked:someProp }", we want to remove the attribute entirely
+ // when someProp is a "no value"-like value (strictly null, false, or undefined)
+ // (because the absence of the "checked" attr is how to mark an element as not checked, etc.)
+ if ((attrValue === false) || (attrValue === null) || (attrValue === undefined))
+ element.removeAttribute(attrName);
+ else
+ element.setAttribute(attrName, attrValue.toString());
+ }
+ }
+ }
+};
+
+ko.bindingHandlers['hasfocus'] = {
+ 'init': function(element, valueAccessor, allBindingsAccessor) {
+ var writeValue = function(valueToWrite) {
+ var modelValue = valueAccessor();
+ if (valueToWrite == ko.utils.unwrapObservable(modelValue))
+ return;
+
+ if (ko.isWriteableObservable(modelValue))
+ modelValue(valueToWrite);
+ else {
+ var allBindings = allBindingsAccessor();
+ if (allBindings['_ko_property_writers'] && allBindings['_ko_property_writers']['hasfocus']) {
+ allBindings['_ko_property_writers']['hasfocus'](valueToWrite);
+ }
+ }
+ };
+ ko.utils.registerEventHandler(element, "focus", function() { writeValue(true) });
+ ko.utils.registerEventHandler(element, "focusin", function() { writeValue(true) }); // For IE
+ ko.utils.registerEventHandler(element, "blur", function() { writeValue(false) });
+ ko.utils.registerEventHandler(element, "focusout", function() { writeValue(false) }); // For IE
},
- this.isTemplateRewritten = function (templateName) {
- throw "Override isTemplateRewritten in your ko.templateEngine subclass";
+ 'update': function(element, valueAccessor) {
+ var value = ko.utils.unwrapObservable(valueAccessor());
+ value ? element.focus() : element.blur();
+ ko.utils.triggerEvent(element, value ? "focusin" : "focusout"); // For IE, which doesn't reliably fire "focus" or "blur" events synchronously
+ }
+};
+
+// "with: someExpression" is equivalent to "template: { if: someExpression, data: someExpression }"
+ko.bindingHandlers['with'] = {
+ makeTemplateValueAccessor: function(valueAccessor) {
+ return function() { var value = valueAccessor(); return { 'if': value, 'data': value, 'templateEngine': ko.nativeTemplateEngine.instance } };
},
- this.rewriteTemplate = function (templateName, rewriterCallback) {
- throw "Override rewriteTemplate in your ko.templateEngine subclass";
+ 'init': function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
+ return ko.bindingHandlers['template']['init'](element, ko.bindingHandlers['with'].makeTemplateValueAccessor(valueAccessor));
},
- this.createJavaScriptEvaluatorBlock = function (script) {
- throw "Override createJavaScriptEvaluatorBlock in your ko.templateEngine subclass";
+ 'update': function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
+ return ko.bindingHandlers['template']['update'](element, ko.bindingHandlers['with'].makeTemplateValueAccessor(valueAccessor), allBindingsAccessor, viewModel, bindingContext);
}
-};/// <reference path="templateEngine.js" />
+};
+ko.jsonExpressionRewriting.bindingRewriteValidators['with'] = false; // Can't rewrite control flow bindings
+ko.virtualElements.allowedBindings['with'] = true;
+// "if: someExpression" is equivalent to "template: { if: someExpression }"
+ko.bindingHandlers['if'] = {
+ makeTemplateValueAccessor: function(valueAccessor) {
+ return function() { return { 'if': valueAccessor(), 'templateEngine': ko.nativeTemplateEngine.instance } };
+ },
+ 'init': function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
+ return ko.bindingHandlers['template']['init'](element, ko.bindingHandlers['if'].makeTemplateValueAccessor(valueAccessor));
+ },
+ 'update': function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
+ return ko.bindingHandlers['template']['update'](element, ko.bindingHandlers['if'].makeTemplateValueAccessor(valueAccessor), allBindingsAccessor, viewModel, bindingContext);
+ }
+};
+ko.jsonExpressionRewriting.bindingRewriteValidators['if'] = false; // Can't rewrite control flow bindings
+ko.virtualElements.allowedBindings['if'] = true;
+
+// "ifnot: someExpression" is equivalent to "template: { ifnot: someExpression }"
+ko.bindingHandlers['ifnot'] = {
+ makeTemplateValueAccessor: function(valueAccessor) {
+ return function() { return { 'ifnot': valueAccessor(), 'templateEngine': ko.nativeTemplateEngine.instance } };
+ },
+ 'init': function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
+ return ko.bindingHandlers['template']['init'](element, ko.bindingHandlers['ifnot'].makeTemplateValueAccessor(valueAccessor));
+ },
+ 'update': function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
+ return ko.bindingHandlers['template']['update'](element, ko.bindingHandlers['ifnot'].makeTemplateValueAccessor(valueAccessor), allBindingsAccessor, viewModel, bindingContext);
+ }
+};
+ko.jsonExpressionRewriting.bindingRewriteValidators['ifnot'] = false; // Can't rewrite control flow bindings
+ko.virtualElements.allowedBindings['ifnot'] = true;
+
+// "foreach: someExpression" is equivalent to "template: { foreach: someExpression }"
+// "foreach: { data: someExpression, afterAdd: myfn }" is equivalent to "template: { foreach: someExpression, afterAdd: myfn }"
+ko.bindingHandlers['foreach'] = {
+ makeTemplateValueAccessor: function(valueAccessor) {
+ return function() {
+ var bindingValue = ko.utils.unwrapObservable(valueAccessor());
+
+ // If bindingValue is the array, just pass it on its own
+ if ((!bindingValue) || typeof bindingValue.length == "number")
+ return { 'foreach': bindingValue, 'templateEngine': ko.nativeTemplateEngine.instance };
+
+ // If bindingValue.data is the array, preserve all relevant options
+ return {
+ 'foreach': bindingValue['data'],
+ 'includeDestroyed': bindingValue['includeDestroyed'],
+ 'afterAdd': bindingValue['afterAdd'],
+ 'beforeRemove': bindingValue['beforeRemove'],
+ 'afterRender': bindingValue['afterRender'],
+ 'templateEngine': ko.nativeTemplateEngine.instance
+ };
+ };
+ },
+ 'init': function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
+ return ko.bindingHandlers['template']['init'](element, ko.bindingHandlers['foreach'].makeTemplateValueAccessor(valueAccessor));
+ },
+ 'update': function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
+ return ko.bindingHandlers['template']['update'](element, ko.bindingHandlers['foreach'].makeTemplateValueAccessor(valueAccessor), allBindingsAccessor, viewModel, bindingContext);
+ }
+};
+ko.jsonExpressionRewriting.bindingRewriteValidators['foreach'] = false; // Can't rewrite control flow bindings
+ko.virtualElements.allowedBindings['foreach'] = true;
+ko.exportSymbol('ko.allowedVirtualElementBindings', ko.virtualElements.allowedBindings);// If you want to make a custom template engine,
+//
+// [1] Inherit from this class (like ko.nativeTemplateEngine does)
+// [2] Override 'renderTemplateSource', supplying a function with this signature:
+//
+// function (templateSource, bindingContext, options) {
+// // - templateSource.text() is the text of the template you should render
+// // - bindingContext.$data is the data you should pass into the template
+// // - you might also want to make bindingContext.$parent, bindingContext.$parents,
+// // and bindingContext.$root available in the template too
+// // - options gives you access to any other properties set on "data-bind: { template: options }"
+// //
+// // Return value: an array of DOM nodes
+// }
+//
+// [3] Override 'createJavaScriptEvaluatorBlock', supplying a function with this signature:
+//
+// function (script) {
+// // Return value: Whatever syntax means "Evaluate the JavaScript statement 'script' and output the result"
+// // For example, the jquery.tmpl template engine converts 'someScript' to '${ someScript }'
+// }
+//
+// This is only necessary if you want to allow data-bind attributes to reference arbitrary template variables.
+// If you don't want to allow that, you can set the property 'allowTemplateRewriting' to false (like ko.nativeTemplateEngine does)
+// and then you don't need to override 'createJavaScriptEvaluatorBlock'.
+
+ko.templateEngine = function () { };
+
+ko.templateEngine.prototype['renderTemplateSource'] = function (templateSource, bindingContext, options) {
+ throw "Override renderTemplateSource";
+};
+
+ko.templateEngine.prototype['createJavaScriptEvaluatorBlock'] = function (script) {
+ throw "Override createJavaScriptEvaluatorBlock";
+};
+
+ko.templateEngine.prototype['makeTemplateSource'] = function(template) {
+ // Named template
+ if (typeof template == "string") {
+ var elem = document.getElementById(template);
+ if (!elem)
+ throw new Error("Cannot find template with ID " + template);
+ return new ko.templateSources.domElement(elem);
+ } else if ((template.nodeType == 1) || (template.nodeType == 8)) {
+ // Anonymous template
+ return new ko.templateSources.anonymousTemplate(template);
+ } else
+ throw new Error("Unknown template type: " + template);
+};
+
+ko.templateEngine.prototype['renderTemplate'] = function (template, bindingContext, options) {
+ var templateSource = this['makeTemplateSource'](template);
+ return this['renderTemplateSource'](templateSource, bindingContext, options);
+};
+
+ko.templateEngine.prototype['isTemplateRewritten'] = function (template) {
+ // Skip rewriting if requested
+ if (this['allowTemplateRewriting'] === false)
+ return true;
+
+ // Perf optimisation - see below
+ if (this.knownRewrittenTemplates && this.knownRewrittenTemplates[template])
+ return true;
+
+ return this['makeTemplateSource'](template)['data']("isRewritten");
+};
+
+ko.templateEngine.prototype['rewriteTemplate'] = function (template, rewriterCallback) {
+ var templateSource = this['makeTemplateSource'](template);
+ var rewritten = rewriterCallback(templateSource['text']());
+ templateSource['text'](rewritten);
+ templateSource['data']("isRewritten", true);
+
+ // Perf optimisation - for named templates, track which ones have been rewritten so we can
+ // answer 'isTemplateRewritten' *without* having to use getElementById (which is slow on IE < 8)
+ if (typeof template == "string") {
+ this.knownRewrittenTemplates = this.knownRewrittenTemplates || {};
+ this.knownRewrittenTemplates[template] = true;
+ }
+};
+
+ko.exportSymbol('ko.templateEngine', ko.templateEngine);
ko.templateRewriting = (function () {
- var memoizeBindingAttributeSyntaxRegex = /(<[a-z]+(\s+(?!data-bind=)[a-z0-9]+(=(\"[^\"]*\"|\'[^\']*\'))?)*\s+)data-bind=(["'])(.*?)\5/g;
+ var memoizeDataBindingAttributeSyntaxRegex = /(<[a-z]+\d*(\s+(?!data-bind=)[a-z0-9\-]+(=(\"[^\"]*\"|\'[^\']*\'))?)*\s+)data-bind=(["'])([\s\S]*?)\5/gi;
+ var memoizeVirtualContainerBindingSyntaxRegex = /<!--\s*ko\b\s*([\s\S]*?)\s*-->/g;
+ function validateDataBindValuesForRewriting(keyValueArray) {
+ var allValidators = ko.jsonExpressionRewriting.bindingRewriteValidators;
+ for (var i = 0; i < keyValueArray.length; i++) {
+ var key = keyValueArray[i]['key'];
+ if (allValidators.hasOwnProperty(key)) {
+ var validator = allValidators[key];
+
+ if (typeof validator === "function") {
+ var possibleErrorMessage = validator(keyValueArray[i]['value']);
+ if (possibleErrorMessage)
+ throw new Error(possibleErrorMessage);
+ } else if (!validator) {
+ throw new Error("This template engine does not support the '" + key + "' binding within its templates");
+ }
+ }
+ }
+ }
+
+ function constructMemoizedTagReplacement(dataBindAttributeValue, tagToRetain, templateEngine) {
+ var dataBindKeyValueArray = ko.jsonExpressionRewriting.parseObjectLiteral(dataBindAttributeValue);
+ validateDataBindValuesForRewriting(dataBindKeyValueArray);
+ var rewrittenDataBindAttributeValue = ko.jsonExpressionRewriting.insertPropertyAccessorsIntoJson(dataBindKeyValueArray);
+
+ // For no obvious reason, Opera fails to evaluate rewrittenDataBindAttributeValue unless it's wrapped in an additional
+ // anonymous function, even though Opera's built-in debugger can evaluate it anyway. No other browser requires this
+ // extra indirection.
+ var applyBindingsToNextSiblingScript = "ko.templateRewriting.applyMemoizedBindingsToNextSibling(function() { \
+ return (function() { return { " + rewrittenDataBindAttributeValue + " } })() \
+ })";
+ return templateEngine['createJavaScriptEvaluatorBlock'](applyBindingsToNextSiblingScript) + tagToRetain;
+ }
+
return {
ensureTemplateIsRewritten: function (template, templateEngine) {
- if (!templateEngine.isTemplateRewritten(template))
- templateEngine.rewriteTemplate(template, function (htmlString) {
+ if (!templateEngine['isTemplateRewritten'](template))
+ templateEngine['rewriteTemplate'](template, function (htmlString) {
return ko.templateRewriting.memoizeBindingAttributeSyntax(htmlString, templateEngine);
});
},
memoizeBindingAttributeSyntax: function (htmlString, templateEngine) {
- return htmlString.replace(memoizeBindingAttributeSyntaxRegex, function () {
- var tagToRetain = arguments[1];
- var dataBindAttributeValue = arguments[6];
-
- dataBindAttributeValue = ko.jsonExpressionRewriting.insertPropertyAccessorsIntoJson(dataBindAttributeValue);
-
- // For no obvious reason, Opera fails to evaluate dataBindAttributeValue unless it's wrapped in an additional anonymous function,
- // even though Opera's built-in debugger can evaluate it anyway. No other browser requires this extra indirection.
- var applyBindingsToNextSiblingScript = "ko.templateRewriting.applyMemoizedBindingsToNextSibling(function() { \
- return (function() { return { " + dataBindAttributeValue + " } })() \
- })";
- return templateEngine.createJavaScriptEvaluatorBlock(applyBindingsToNextSiblingScript) + tagToRetain;
+ return htmlString.replace(memoizeDataBindingAttributeSyntaxRegex, function () {
+ return constructMemoizedTagReplacement(/* dataBindAttributeValue: */ arguments[6], /* tagToRetain: */ arguments[1], templateEngine);
+ }).replace(memoizeVirtualContainerBindingSyntaxRegex, function() {
+ return constructMemoizedTagReplacement(/* dataBindAttributeValue: */ arguments[1], /* tagToRetain: */ "<!-- ko -->", templateEngine);
});
},
applyMemoizedBindingsToNextSibling: function (bindings) {
- return ko.memoization.memoize(function (domNode) {
+ return ko.memoization.memoize(function (domNode, bindingContext) {
if (domNode.nextSibling)
- ko.applyBindingsToNode(domNode.nextSibling, bindings, null);
+ ko.applyBindingsToNode(domNode.nextSibling, bindings, bindingContext);
});
}
}
-})();/// <reference path="templating.js" />
-/// <reference path="../subscribables/dependentObservable.js" />
+})();
+ko.exportSymbol('ko.templateRewriting', ko.templateRewriting);
+ko.exportSymbol('ko.templateRewriting.applyMemoizedBindingsToNextSibling', ko.templateRewriting.applyMemoizedBindingsToNextSibling); // Exported only because it has to be referenced by string lookup from within rewritten template
+(function() {
+ // A template source represents a read/write way of accessing a template. This is to eliminate the need for template loading/saving
+ // logic to be duplicated in every template engine (and means they can all work with anonymous templates, etc.)
+ //
+ // Two are provided by default:
+ // 1. ko.templateSources.domElement - reads/writes the text content of an arbitrary DOM element
+ // 2. ko.templateSources.anonymousElement - uses ko.utils.domData to read/write text *associated* with the DOM element, but
+ // without reading/writing the actual element text content, since it will be overwritten
+ // with the rendered template output.
+ // You can implement your own template source if you want to fetch/store templates somewhere other than in DOM elements.
+ // Template sources need to have the following functions:
+ // text() - returns the template text from your storage location
+ // text(value) - writes the supplied template text to your storage location
+ // data(key) - reads values stored using data(key, value) - see below
+ // data(key, value) - associates "value" with this template and the key "key". Is used to store information like "isRewritten".
+ //
+ // Once you've implemented a templateSource, make your template engine use it by subclassing whatever template engine you were
+ // using and overriding "makeTemplateSource" to return an instance of your custom template source.
+
+ ko.templateSources = {};
+
+ // ---- ko.templateSources.domElement -----
+
+ ko.templateSources.domElement = function(element) {
+ this.domElement = element;
+ }
+
+ ko.templateSources.domElement.prototype['text'] = function(/* valueToWrite */) {
+ if (arguments.length == 0) {
+ return this.domElement.tagName.toLowerCase() == "script" ? this.domElement.text : this.domElement.innerHTML;
+ } else {
+ var valueToWrite = arguments[0];
+ if (this.domElement.tagName.toLowerCase() == "script")
+ this.domElement.text = valueToWrite;
+ else
+ ko.utils.setHtml(this.domElement, valueToWrite);
+ }
+ };
+
+ ko.templateSources.domElement.prototype['data'] = function(key /*, valueToWrite */) {
+ if (arguments.length === 1) {
+ return ko.utils.domData.get(this.domElement, "templateSourceData_" + key);
+ } else {
+ ko.utils.domData.set(this.domElement, "templateSourceData_" + key, arguments[1]);
+ }
+ };
+
+ // ---- ko.templateSources.anonymousTemplate -----
+
+ var anonymousTemplatesDomDataKey = "__ko_anon_template__";
+ ko.templateSources.anonymousTemplate = function(element) {
+ this.domElement = element;
+ }
+ ko.templateSources.anonymousTemplate.prototype = new ko.templateSources.domElement();
+ ko.templateSources.anonymousTemplate.prototype['text'] = function(/* valueToWrite */) {
+ if (arguments.length == 0) {
+ return ko.utils.domData.get(this.domElement, anonymousTemplatesDomDataKey);
+ } else {
+ var valueToWrite = arguments[0];
+ ko.utils.domData.set(this.domElement, anonymousTemplatesDomDataKey, valueToWrite);
+ }
+ };
+
+ ko.exportSymbol('ko.templateSources', ko.templateSources);
+ ko.exportSymbol('ko.templateSources.domElement', ko.templateSources.domElement);
+ ko.exportSymbol('ko.templateSources.anonymousTemplate', ko.templateSources.anonymousTemplate);
+})();
(function () {
var _templateEngine;
ko.setTemplateEngine = function (templateEngine) {
if ((templateEngine != undefined) && !(templateEngine instanceof ko.templateEngine))
throw "templateEngine must inherit from ko.templateEngine";
_templateEngine = templateEngine;
}
+ function invokeForEachNodeOrCommentInParent(nodeArray, parent, action) {
+ for (var i = 0; node = nodeArray[i]; i++) {
+ if (node.parentNode !== parent) // Skip anything that has been removed during binding
+ continue;
+ if ((node.nodeType === 1) || (node.nodeType === 8))
+ action(node);
+ }
+ }
+
+ ko.activateBindingsOnTemplateRenderedNodes = function(nodeArray, bindingContext) {
+ // To be used on any nodes that have been rendered by a template and have been inserted into some parent element.
+ // Safely iterates through nodeArray (being tolerant of any changes made to it during binding, e.g.,
+ // if a binding inserts siblings), and for each:
+ // (1) Does a regular "applyBindings" to associate bindingContext with this node and to activate any non-memoized bindings
+ // (2) Unmemoizes any memos in the DOM subtree (e.g., to activate bindings that had been memoized during template rewriting)
+
+ var nodeArrayClone = ko.utils.arrayPushAll([], nodeArray); // So we can tolerate insertions/deletions during binding
+ var commonParentElement = (nodeArray.length > 0) ? nodeArray[0].parentNode : null; // All items must be in the same parent, so this is OK
+
+ // Need to applyBindings *before* unmemoziation, because unmemoization might introduce extra nodes (that we don't want to re-bind)
+ // whereas a regular applyBindings won't introduce new memoized nodes
+
+ invokeForEachNodeOrCommentInParent(nodeArrayClone, commonParentElement, function(node) {
+ ko.applyBindings(bindingContext, node);
+ });
+ invokeForEachNodeOrCommentInParent(nodeArrayClone, commonParentElement, function(node) {
+ ko.memoization.unmemoizeDomNodeAndDescendants(node, [bindingContext]);
+ });
+ }
+
function getFirstNodeFromPossibleArray(nodeOrNodeArray) {
return nodeOrNodeArray.nodeType ? nodeOrNodeArray
: nodeOrNodeArray.length > 0 ? nodeOrNodeArray[0]
: null;
}
- function executeTemplate(targetNodeOrNodeArray, renderMode, template, data, options) {
- var dataForTemplate = ko.utils.unwrapObservable(data);
-
+ function executeTemplate(targetNodeOrNodeArray, renderMode, template, bindingContext, options) {
options = options || {};
- var templateEngineToUse = (options.templateEngine || _templateEngine);
+ var templateEngineToUse = (options['templateEngine'] || _templateEngine);
ko.templateRewriting.ensureTemplateIsRewritten(template, templateEngineToUse);
- var renderedNodesArray = templateEngineToUse.renderTemplate(template, dataForTemplate, options);
+ var renderedNodesArray = templateEngineToUse['renderTemplate'](template, bindingContext, options);
// Loosely check result is an array of DOM nodes
if ((typeof renderedNodesArray.length != "number") || (renderedNodesArray.length > 0 && typeof renderedNodesArray[0].nodeType != "number"))
throw "Template engine must return an array of DOM nodes";
- if (renderedNodesArray)
- ko.utils.arrayForEach(renderedNodesArray, function (renderedNode) {
- ko.memoization.unmemoizeDomNodeAndDescendants(renderedNode);
- });
-
+ var haveAddedNodesToParent = false;
switch (renderMode) {
- case "replaceChildren": ko.utils.setDomNodeChildren(targetNodeOrNodeArray, renderedNodesArray); break;
- case "replaceNode": ko.utils.replaceDomNodes(targetNodeOrNodeArray, renderedNodesArray); break;
+ case "replaceChildren":
+ ko.virtualElements.setDomNodeChildren(targetNodeOrNodeArray, renderedNodesArray);
+ haveAddedNodesToParent = true;
+ break;
+ case "replaceNode":
+ ko.utils.replaceDomNodes(targetNodeOrNodeArray, renderedNodesArray);
+ haveAddedNodesToParent = true;
+ break;
case "ignoreTargetNode": break;
- default: throw new Error("Unknown renderMode: " + renderMode);
+ default:
+ throw new Error("Unknown renderMode: " + renderMode);
}
+ if (haveAddedNodesToParent) {
+ ko.activateBindingsOnTemplateRenderedNodes(renderedNodesArray, bindingContext);
+ if (options['afterRender'])
+ options['afterRender'](renderedNodesArray, bindingContext['$data']);
+ }
+
return renderedNodesArray;
}
- ko.renderTemplate = function (template, data, options, targetNodeOrNodeArray, renderMode) {
+ ko.renderTemplate = function (template, dataOrBindingContext, options, targetNodeOrNodeArray, renderMode) {
options = options || {};
- if ((options.templateEngine || _templateEngine) == undefined)
+ if ((options['templateEngine'] || _templateEngine) == undefined)
throw "Set a template engine before calling renderTemplate";
renderMode = renderMode || "replaceChildren";
if (targetNodeOrNodeArray) {
var firstTargetNode = getFirstNodeFromPossibleArray(targetNodeOrNodeArray);
- var whenToDispose = function () { return (!firstTargetNode) || !ko.utils.domNodeIsAttachedToDocument(firstTargetNode); };
-
+
+ var whenToDispose = function () { return (!firstTargetNode) || !ko.utils.domNodeIsAttachedToDocument(firstTargetNode); }; // Passive disposal (on next evaluation)
+ var activelyDisposeWhenNodeIsRemoved = (firstTargetNode && renderMode == "replaceNode") ? firstTargetNode.parentNode : firstTargetNode;
+
return new ko.dependentObservable( // So the DOM is automatically updated when any dependency changes
function () {
- var renderedNodesArray = executeTemplate(targetNodeOrNodeArray, renderMode, template, data, options);
+ // Ensure we've got a proper binding context to work with
+ var bindingContext = (dataOrBindingContext && (dataOrBindingContext instanceof ko.bindingContext))
+ ? dataOrBindingContext
+ : new ko.bindingContext(ko.utils.unwrapObservable(dataOrBindingContext));
+
+ // Support selecting template as a function of the data being rendered
+ var templateName = typeof(template) == 'function' ? template(bindingContext['$data']) : template;
+
+ var renderedNodesArray = executeTemplate(targetNodeOrNodeArray, renderMode, templateName, bindingContext, options);
if (renderMode == "replaceNode") {
targetNodeOrNodeArray = renderedNodesArray;
firstTargetNode = getFirstNodeFromPossibleArray(targetNodeOrNodeArray);
}
},
null,
- { disposeWhen: whenToDispose }
+ { 'disposeWhen': whenToDispose, 'disposeWhenNodeIsRemoved': activelyDisposeWhenNodeIsRemoved }
);
} else {
// 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
return ko.memoization.memoize(function (domNode) {
- ko.renderTemplate(template, data, options, domNode, "replaceNode");
+ ko.renderTemplate(template, dataOrBindingContext, options, domNode, "replaceNode");
});
}
};
- ko.renderTemplateForEach = function (template, arrayOrObservableArray, options, targetNode) {
- var whenToDispose = function () { return !ko.utils.domNodeIsAttachedToDocument(targetNode); };
+ ko.renderTemplateForEach = function (template, arrayOrObservableArray, options, targetNode, parentBindingContext) {
+ var createInnerBindingContext = function(arrayValue) {
+ return parentBindingContext['createChildContext'](ko.utils.unwrapObservable(arrayValue));
+ };
- new ko.dependentObservable(function () {
- var unwrappedArray = ko.utils.unwrapObservable(arrayOrObservableArray);
+ // This will be called whenever setDomNodeChildrenFromArrayMapping has added nodes to targetNode
+ var activateBindingsCallback = function(arrayValue, addedNodesArray) {
+ var bindingContext = createInnerBindingContext(arrayValue);
+ ko.activateBindingsOnTemplateRenderedNodes(addedNodesArray, bindingContext);
+ if (options['afterRender'])
+ options['afterRender'](addedNodesArray, bindingContext['$data']);
+ };
+
+ return new ko.dependentObservable(function () {
+ var unwrappedArray = ko.utils.unwrapObservable(arrayOrObservableArray) || [];
if (typeof unwrappedArray.length == "undefined") // Coerce single value into array
unwrappedArray = [unwrappedArray];
// Filter out any entries marked as destroyed
- var filteredArray = ko.utils.arrayFilter(unwrappedArray, function(item) {
- return options.includeDestroyed || !item._destroy;
+ var filteredArray = ko.utils.arrayFilter(unwrappedArray, function(item) {
+ return options['includeDestroyed'] || item === undefined || item === null || !ko.utils.unwrapObservable(item['_destroy']);
});
ko.utils.setDomNodeChildrenFromArrayMapping(targetNode, filteredArray, function (arrayValue) {
- return executeTemplate(null, "ignoreTargetNode", template, arrayValue, options);
- }, options);
- }, null, { disposeWhen: whenToDispose });
+ // Support selecting template as a function of the data being rendered
+ var templateName = typeof(template) == 'function' ? template(arrayValue) : template;
+ return executeTemplate(null, "ignoreTargetNode", templateName, createInnerBindingContext(arrayValue), options);
+ }, options, activateBindingsCallback);
+
+ }, null, { 'disposeWhenNodeIsRemoved': targetNode });
};
- ko.bindingHandlers.template = {
- update: function (element, bindingValue, allBindings, viewModel) {
- var templateName = typeof bindingValue == "string" ? bindingValue : bindingValue.name;
-
- if (typeof bindingValue.foreach != "undefined") {
- // Render once for each data point
- ko.renderTemplateForEach(templateName, bindingValue.foreach || [], { afterAdd: bindingValue.afterAdd, beforeRemove: bindingValue.beforeRemove, includeDestroyed: bindingValue.includeDestroyed }, element);
+ var templateSubscriptionDomDataKey = '__ko__templateSubscriptionDomDataKey__';
+ function disposeOldSubscriptionAndStoreNewOne(element, newSubscription) {
+ var oldSubscription = ko.utils.domData.get(element, templateSubscriptionDomDataKey);
+ if (oldSubscription && (typeof(oldSubscription.dispose) == 'function'))
+ oldSubscription.dispose();
+ ko.utils.domData.set(element, templateSubscriptionDomDataKey, newSubscription);
+ }
+
+ ko.bindingHandlers['template'] = {
+ 'init': function(element, valueAccessor) {
+ // Support anonymous templates
+ var bindingValue = ko.utils.unwrapObservable(valueAccessor());
+ if ((typeof bindingValue != "string") && (!bindingValue.name) && (element.nodeType == 1)) {
+ // It's an anonymous template - store the element contents, then clear the element
+ new ko.templateSources.anonymousTemplate(element).text(element.innerHTML);
+ ko.utils.emptyDomNode(element);
}
- else {
- // Render once for this single data point (or use the viewModel if no data was provided)
- var templateData = bindingValue.data;
- ko.renderTemplate(templateName, typeof templateData == "undefined" ? viewModel : templateData, null, element);
+ return { 'controlsDescendantBindings': true };
+ },
+ 'update': function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
+ var bindingValue = ko.utils.unwrapObservable(valueAccessor());
+ var templateName;
+ var shouldDisplay = true;
+
+ if (typeof bindingValue == "string") {
+ templateName = bindingValue;
+ } else {
+ templateName = bindingValue.name;
+
+ // Support "if"/"ifnot" conditions
+ if ('if' in bindingValue)
+ shouldDisplay = shouldDisplay && ko.utils.unwrapObservable(bindingValue['if']);
+ if ('ifnot' in bindingValue)
+ shouldDisplay = shouldDisplay && !ko.utils.unwrapObservable(bindingValue['ifnot']);
+ }
+
+ var templateSubscription = null;
+
+ if ((typeof bindingValue === 'object') && ('foreach' in bindingValue)) { // Note: can't use 'in' operator on strings
+ // Render once for each data point (treating data set as empty if shouldDisplay==false)
+ var dataArray = (shouldDisplay && bindingValue['foreach']) || [];
+ templateSubscription = ko.renderTemplateForEach(templateName || element, dataArray, /* options: */ bindingValue, element, bindingContext);
+ } else {
+ if (shouldDisplay) {
+ // Render once for this single data point (or use the viewModel if no data was provided)
+ var innerBindingContext = (typeof bindingValue == 'object') && ('data' in bindingValue)
+ ? bindingContext['createChildContext'](ko.utils.unwrapObservable(bindingValue['data'])) // Given an explitit 'data' value, we create a child binding context for it
+ : bindingContext; // Given no explicit 'data' value, we retain the same binding context
+ templateSubscription = ko.renderTemplate(templateName || element, innerBindingContext, /* options: */ bindingValue, element);
+ } else
+ ko.virtualElements.emptyNode(element);
}
+
+ // It only makes sense to have a single template subscription per element (otherwise which one should have its output displayed?)
+ disposeOldSubscriptionAndStoreNewOne(element, templateSubscription);
}
};
-})();/// <reference path="../../utils.js" />
-// Simple calculation based on Levenshtein distance.
-(function () {
+ // Anonymous templates can't be rewritten. Give a nice error message if you try to do it.
+ ko.jsonExpressionRewriting.bindingRewriteValidators['template'] = function(bindingValue) {
+ var parsedBindingValue = ko.jsonExpressionRewriting.parseObjectLiteral(bindingValue);
+ if ((parsedBindingValue.length == 1) && parsedBindingValue[0]['unknown'])
+ return null; // It looks like a string literal, not an object literal, so treat it as a named template (which is allowed for rewriting)
+
+ if (ko.jsonExpressionRewriting.keyValueArrayContainsKey(parsedBindingValue, "name"))
+ return null; // Named templates can be rewritten, so return "no error"
+ return "This template engine does not support anonymous templates nested within its templates";
+ };
+
+ ko.virtualElements.allowedBindings['template'] = true;
+})();
+
+ko.exportSymbol('ko.setTemplateEngine', ko.setTemplateEngine);
+ko.exportSymbol('ko.renderTemplate', ko.renderTemplate);
+(function () {
+ // Simple calculation based on Levenshtein distance.
function calculateEditDistanceMatrix(oldArray, newArray, maxAllowedDistance) {
var distances = [];
for (var i = 0; i <= newArray.length; i++)
distances[i] = [];
@@ -1284,45 +2966,78 @@
oldArray = oldArray || [];
newArray = newArray || [];
var editDistanceMatrix = calculateEditDistanceMatrix(oldArray, newArray, maxEditsToConsider);
return findEditScriptFromEditDistanceMatrix(editDistanceMatrix, oldArray, newArray);
}
- };
-})();/// <reference path="compareArrays.js" />
+ };
+})();
+ko.exportSymbol('ko.utils.compareArrays', ko.utils.compareArrays);
+
(function () {
// Objective:
// * Given an input array, a container DOM node, and a function from array elements to arrays of DOM nodes,
// map the array elements to arrays of DOM nodes, concatenate together all these arrays, and use them to populate the container DOM node
// * Next time we're given the same combination of things (with the array possibly having mutated), update the container DOM node
// 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
// previously mapped - retain those nodes, and just insert/delete other ones
- function mapNodeAndRefreshWhenChanged(mapping, valueToMap) {
+ // "callbackAfterAddingNodes" will be invoked after any "mapping"-generated nodes are inserted into the container node
+ // You can use this, for example, to activate bindings on those nodes.
+
+ function fixUpVirtualElements(contiguousNodeArray) {
+ // Ensures that contiguousNodeArray really *is* an array of contiguous siblings, even if some of the interior
+ // ones have changed since your array was first built (e.g., because your array contains virtual elements, and
+ // their virtual children changed when binding was applied to them).
+ // This is needed so that we can reliably remove or update the nodes corresponding to a given array item
+
+ if (contiguousNodeArray.length > 2) {
+ // Build up the actual new contiguous node set
+ var current = contiguousNodeArray[0], last = contiguousNodeArray[contiguousNodeArray.length - 1], newContiguousSet = [current];
+ while (current !== last) {
+ current = current.nextSibling;
+ if (!current) // Won't happen, except if the developer has manually removed some DOM elements (then we're in an undefined scenario)
+ return;
+ newContiguousSet.push(current);
+ }
+
+ // ... then mutate the input array to match this.
+ // (The following line replaces the contents of contiguousNodeArray with newContiguousSet)
+ Array.prototype.splice.apply(contiguousNodeArray, [0, contiguousNodeArray.length].concat(newContiguousSet));
+ }
+ }
+
+ function mapNodeAndRefreshWhenChanged(containerNode, mapping, valueToMap, callbackAfterAddingNodes) {
// Map this array value inside a dependentObservable so we re-map when any dependency changes
var mappedNodes = [];
- ko.dependentObservable(function() {
+ var dependentObservable = ko.dependentObservable(function() {
var newMappedNodes = mapping(valueToMap) || [];
// On subsequent evaluations, just replace the previously-inserted DOM nodes
- if (mappedNodes.length > 0)
+ if (mappedNodes.length > 0) {
+ fixUpVirtualElements(mappedNodes);
ko.utils.replaceDomNodes(mappedNodes, newMappedNodes);
+ if (callbackAfterAddingNodes)
+ callbackAfterAddingNodes(valueToMap, newMappedNodes);
+ }
// Replace the contents of the mappedNodes array, thereby updating the record
// of which nodes would be deleted if valueToMap was itself later removed
mappedNodes.splice(0, mappedNodes.length);
ko.utils.arrayPushAll(mappedNodes, newMappedNodes);
- }, null, { disposeWhen: function() { return (mappedNodes.length == 0) || !ko.utils.domNodeIsAttachedToDocument(mappedNodes[0]) } });
- return mappedNodes;
- }
+ }, null, { 'disposeWhenNodeIsRemoved': containerNode, 'disposeWhen': function() { return (mappedNodes.length == 0) || !ko.utils.domNodeIsAttachedToDocument(mappedNodes[0]) } });
+ return { mappedNodes : mappedNodes, dependentObservable : dependentObservable };
+ }
+
+ var lastMappingResultDomDataKey = "setDomNodeChildrenFromArrayMapping_lastMappingResult";
- ko.utils.setDomNodeChildrenFromArrayMapping = function (domNode, array, mapping, options) {
+ ko.utils.setDomNodeChildrenFromArrayMapping = function (domNode, array, mapping, options, callbackAfterAddingNodes) {
// Compare the provided array against the previous one
array = array || [];
options = options || {};
- var isFirstExecution = ko.utils.domData.get(domNode, "setDomNodeChildrenFromArrayMapping_lastMappingResult") === undefined;
- var lastMappingResult = ko.utils.domData.get(domNode, "setDomNodeChildrenFromArrayMapping_lastMappingResult") || [];
+ var isFirstExecution = ko.utils.domData.get(domNode, lastMappingResultDomDataKey) === undefined;
+ var lastMappingResult = ko.utils.domData.get(domNode, lastMappingResultDomDataKey) || [];
var lastArray = ko.utils.arrayMap(lastMappingResult, function (x) { return x.arrayEntry; });
var editScript = ko.utils.compareArrays(lastArray, array);
// Build the new mapping result
var newMappingResult = [];
@@ -1340,118 +3055,169 @@
insertAfterNode = dataToRetain.domNodes[dataToRetain.domNodes.length - 1];
lastMappingResultIndex++;
break;
case "deleted":
+ // Stop tracking changes to the mapping for these nodes
+ lastMappingResult[lastMappingResultIndex].dependentObservable.dispose();
+
// Queue these nodes for later removal
+ fixUpVirtualElements(lastMappingResult[lastMappingResultIndex].domNodes);
ko.utils.arrayForEach(lastMappingResult[lastMappingResultIndex].domNodes, function (node) {
- nodesToDelete.push(node);
+ nodesToDelete.push({
+ element: node,
+ index: i,
+ value: editScript[i].value
+ });
insertAfterNode = node;
});
lastMappingResultIndex++;
break;
case "added":
- var mappedNodes = mapNodeAndRefreshWhenChanged(mapping, editScript[i].value);
- // On the first evaluation, insert the nodes at the current insertion point
- newMappingResult.push({ arrayEntry: editScript[i].value, domNodes: mappedNodes });
- for (var nodeIndex = 0, nodeIndexMax = mappedNodes.length; nodeIndex < nodeIndexMax; nodeIndex++) {
- var node = mappedNodes[nodeIndex];
- nodesAdded.push(node);
- if (insertAfterNode == null) {
- // Insert at beginning
- if (domNode.firstChild)
- domNode.insertBefore(node, domNode.firstChild);
- else
- domNode.appendChild(node);
- } else {
- // Insert after insertion point
- if (insertAfterNode.nextSibling)
- domNode.insertBefore(node, insertAfterNode.nextSibling);
- else
- domNode.appendChild(node);
- }
- insertAfterNode = node;
- }
+ var valueToMap = editScript[i].value;
+ var mapData = mapNodeAndRefreshWhenChanged(domNode, mapping, valueToMap, callbackAfterAddingNodes);
+ var mappedNodes = mapData.mappedNodes;
+
+ // On the first evaluation, insert the nodes at the current insertion point
+ newMappingResult.push({ arrayEntry: editScript[i].value, domNodes: mappedNodes, dependentObservable: mapData.dependentObservable });
+ for (var nodeIndex = 0, nodeIndexMax = mappedNodes.length; nodeIndex < nodeIndexMax; nodeIndex++) {
+ var node = mappedNodes[nodeIndex];
+ nodesAdded.push({
+ element: node,
+ index: i,
+ value: editScript[i].value
+ });
+ if (insertAfterNode == null) {
+ // Insert "node" (the newly-created node) as domNode's first child
+ ko.virtualElements.prepend(domNode, node);
+ } else {
+ // Insert "node" into "domNode" immediately after "insertAfterNode"
+ ko.virtualElements.insertAfter(domNode, node, insertAfterNode);
+ }
+ insertAfterNode = node;
+ }
+ if (callbackAfterAddingNodes)
+ callbackAfterAddingNodes(valueToMap, mappedNodes);
break;
}
}
- ko.utils.arrayForEach(nodesToDelete, function (node) { ko.utils.domData.cleanNodeAndDescendants(node); });
+ ko.utils.arrayForEach(nodesToDelete, function (node) { ko.cleanNode(node.element) });
var invokedBeforeRemoveCallback = false;
if (!isFirstExecution) {
- if (options.afterAdd)
- options.afterAdd(nodesAdded);
- if (options.beforeRemove) {
- options.beforeRemove(nodesToDelete);
+ if (options['afterAdd']) {
+ for (var i = 0; i < nodesAdded.length; i++)
+ options['afterAdd'](nodesAdded[i].element, nodesAdded[i].index, nodesAdded[i].value);
+ }
+ if (options['beforeRemove']) {
+ for (var i = 0; i < nodesToDelete.length; i++)
+ options['beforeRemove'](nodesToDelete[i].element, nodesToDelete[i].index, nodesToDelete[i].value);
invokedBeforeRemoveCallback = true;
}
}
if (!invokedBeforeRemoveCallback)
ko.utils.arrayForEach(nodesToDelete, function (node) {
- if (node.parentNode)
- node.parentNode.removeChild(node);
+ ko.removeNode(node.element);
});
// Store a copy of the array items we just considered so we can difference it next time
- ko.utils.domData.set(domNode, "setDomNodeChildrenFromArrayMapping_lastMappingResult", newMappingResult);
+ ko.utils.domData.set(domNode, lastMappingResultDomDataKey, newMappingResult);
}
-})();/// <reference path="../templating.js" />
+})();
-ko.jqueryTmplTemplateEngine = function () {
- function getTemplateNode(template) {
- var templateNode = document.getElementById(template);
- if (templateNode == null)
- throw new Error("Cannot find template with ID=" + template);
- return templateNode;
- }
+ko.exportSymbol('ko.utils.setDomNodeChildrenFromArrayMapping', ko.utils.setDomNodeChildrenFromArrayMapping);
+ko.nativeTemplateEngine = function () {
+ this['allowTemplateRewriting'] = false;
+}
- var aposMarker = "__ko_apos__";
- var aposRegex = new RegExp(aposMarker, "g");
- this.renderTemplate = function (template, data, options) {
- // jquery.tmpl doesn't like it if the template returns just text content or nothing - it only likes you to return DOM nodes.
- // To make things more flexible, we can wrap the whole template in a <script> node so that jquery.tmpl just processes it as
- // text and doesn't try to parse the output. Then, since jquery.tmpl has jQuery as a dependency anyway, we can use jQuery to
- // parse that text into a document fragment using jQuery.clean().
- var templateTextInWrapper = "<script type=\"text/html\">" + getTemplateNode(template).text + "</script>";
- var renderedMarkupInWrapper = $.tmpl(templateTextInWrapper, data);
- var renderedMarkup = renderedMarkupInWrapper[0].text.replace(aposRegex, "'");
- return jQuery.clean([renderedMarkup], document);
- },
+ko.nativeTemplateEngine.prototype = new ko.templateEngine();
+ko.nativeTemplateEngine.prototype['renderTemplateSource'] = function (templateSource, bindingContext, options) {
+ var templateText = templateSource.text();
+ return ko.utils.parseHtmlFragment(templateText);
+};
- this.isTemplateRewritten = function (template) {
- return getTemplateNode(template).isRewritten === true;
- },
+ko.nativeTemplateEngine.instance = new ko.nativeTemplateEngine();
+ko.setTemplateEngine(ko.nativeTemplateEngine.instance);
- this.rewriteTemplate = function (template, rewriterCallback) {
- var templateNode = getTemplateNode(template);
- var rewritten = rewriterCallback(templateNode.text);
+ko.exportSymbol('ko.nativeTemplateEngine', ko.nativeTemplateEngine);(function() {
+ ko.jqueryTmplTemplateEngine = function () {
+ // Detect which version of jquery-tmpl you're using. Unfortunately jquery-tmpl
+ // doesn't expose a version number, so we have to infer it.
+ // Note that as of Knockout 1.3, we only support jQuery.tmpl 1.0.0pre and later,
+ // which KO internally refers to as version "2", so older versions are no longer detected.
+ var jQueryTmplVersion = this.jQueryTmplVersion = (function() {
+ if ((typeof(jQuery) == "undefined") || !(jQuery['tmpl']))
+ return 0;
+ // Since it exposes no official version number, we use our own numbering system. To be updated as jquery-tmpl evolves.
+ try {
+ if (jQuery['tmpl']['tag']['tmpl']['open'].toString().indexOf('__') >= 0) {
+ // Since 1.0.0pre, custom tags should append markup to an array called "__"
+ return 2; // Final version of jquery.tmpl
+ }
+ } catch(ex) { /* Apparently not the version we were looking for */ }
+
+ return 1; // Any older version that we don't support
+ })();
- // jquery.tmpl falls over if you use single-quotes, so replace these with a temporary marker for template rendering,
- // and then replace back after the template was rendered. This is slightly complicated by the fact that we must not interfere
- // with any code blocks - only replace apos characters outside code blocks.
- rewritten = ko.utils.stringTrim(rewritten);
- rewritten = rewritten.replace(/([\s\S]*?)(\${[\s\S]*?}|{{[\=a-z][\s\S]*?}}|$)/g, function(match) {
- // Called for each non-code-block followed by a code block (or end of template)
- var nonCodeSnippet = arguments[1];
- var codeSnippet = arguments[2];
- return nonCodeSnippet.replace(/\'/g, aposMarker) + codeSnippet;
- });
+ function ensureHasReferencedJQueryTemplates() {
+ if (jQueryTmplVersion < 2)
+ throw new Error("Your version of jQuery.tmpl is too old. Please upgrade to jQuery.tmpl 1.0.0pre or later.");
+ }
+
+ function executeTemplate(compiledTemplate, data, jQueryTemplateOptions) {
+ return jQuery['tmpl'](compiledTemplate, data, jQueryTemplateOptions);
+ }
- templateNode.text = rewritten;
- templateNode.isRewritten = true;
- },
+ this['renderTemplateSource'] = function(templateSource, bindingContext, options) {
+ options = options || {};
+ ensureHasReferencedJQueryTemplates();
+
+ // Ensure we have stored a precompiled version of this template (don't want to reparse on every render)
+ var precompiled = templateSource['data']('precompiled');
+ if (!precompiled) {
+ var templateText = templateSource.text() || "";
+ // Wrap in "with($whatever.koBindingContext) { ... }"
+ templateText = "{{ko_with $item.koBindingContext}}" + templateText + "{{/ko_with}}";
- this.createJavaScriptEvaluatorBlock = function (script) {
- return "{{= " + script + "}}";
- },
+ precompiled = jQuery['template'](null, templateText);
+ templateSource['data']('precompiled', precompiled);
+ }
+
+ var data = [bindingContext['$data']]; // Prewrap the data in an array to stop jquery.tmpl from trying to unwrap any arrays
+ var jQueryTemplateOptions = jQuery['extend']({ 'koBindingContext': bindingContext }, options['templateOptions']);
- // 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
- this.addTemplate = function (templateName, templateMarkup) {
- document.write("<script type='text/html' id='" + templateName + "'>" + templateMarkup + "</script>");
- }
-};
-ko.jqueryTmplTemplateEngine.prototype = new ko.templateEngine();
-
-// Use this one by default
-ko.setTemplateEngine(new ko.jqueryTmplTemplateEngine());
\ No newline at end of file
+ var resultNodes = executeTemplate(precompiled, data, jQueryTemplateOptions);
+ resultNodes['appendTo'](document.createElement("div")); // Using "appendTo" forces jQuery/jQuery.tmpl to perform necessary cleanup work
+ jQuery['fragments'] = {}; // Clear jQuery's fragment cache to avoid a memory leak after a large number of template renders
+ return resultNodes;
+ };
+
+ this['createJavaScriptEvaluatorBlock'] = function(script) {
+ return "{{ko_code ((function() { return " + script + " })()) }}";
+ };
+
+ this['addTemplate'] = function(templateName, templateMarkup) {
+ document.write("<script type='text/html' id='" + templateName + "'>" + templateMarkup + "</script>");
+ };
+
+ if (jQueryTmplVersion > 0) {
+ jQuery['tmpl']['tag']['ko_code'] = {
+ open: "__.push($1 || '');"
+ };
+ jQuery['tmpl']['tag']['ko_with'] = {
+ open: "with($1) {",
+ close: "} "
+ };
+ }
+ };
+
+ ko.jqueryTmplTemplateEngine.prototype = new ko.templateEngine();
+
+ // Use this one by default *only if jquery.tmpl is referenced*
+ var jqueryTmplTemplateEngineInstance = new ko.jqueryTmplTemplateEngine();
+ if (jqueryTmplTemplateEngineInstance.jQueryTmplVersion > 0)
+ ko.setTemplateEngine(jqueryTmplTemplateEngineInstance);
+
+ ko.exportSymbol('ko.jqueryTmplTemplateEngine', ko.jqueryTmplTemplateEngine);
+})();})(window);