|
|
@@ -2,84 +2,3582 @@
|
|
|
// (c) Steven Sanderson - http://knockoutjs.com/
|
|
|
// License: MIT (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
|
|
-(function() {function j(w){throw w;}var m=!0,p=null,r=!1;function u(w){return function(){return w}};var x=window,y=document,ga=navigator,F=window.jQuery,I=void 0;
|
|
|
-function L(w){function ha(a,d,c,e,f){var g=[];a=b.j(function(){var a=d(c,f)||[];0<g.length&&(b.a.Ya(M(g),a),e&&b.r.K(e,p,[c,a,f]));g.splice(0,g.length);b.a.P(g,a)},p,{W:a,Ka:function(){return 0==g.length||!b.a.X(g[0])}});return{M:g,j:a.pa()?a:I}}function M(a){for(;a.length&&!b.a.X(a[0]);)a.splice(0,1);if(1<a.length){for(var d=a[0],c=a[a.length-1],e=[d];d!==c;){d=d.nextSibling;if(!d)return;e.push(d)}Array.prototype.splice.apply(a,[0,a.length].concat(e))}return a}function S(a,b,c,e,f){var g=Math.min,
|
|
|
-h=Math.max,k=[],l,n=a.length,q,s=b.length,v=s-n||1,G=n+s+1,J,A,z;for(l=0;l<=n;l++){A=J;k.push(J=[]);z=g(s,l+v);for(q=h(0,l-1);q<=z;q++)J[q]=q?l?a[l-1]===b[q-1]?A[q-1]:g(A[q]||G,J[q-1]||G)+1:q+1:l+1}g=[];h=[];v=[];l=n;for(q=s;l||q;)s=k[l][q]-1,q&&s===k[l][q-1]?h.push(g[g.length]={status:c,value:b[--q],index:q}):l&&s===k[l-1][q]?v.push(g[g.length]={status:e,value:a[--l],index:l}):(g.push({status:"retained",value:b[--q]}),--l);if(h.length&&v.length){a=10*n;var t;for(b=c=0;(f||b<a)&&(t=h[c]);c++){for(e=
|
|
|
-0;k=v[e];e++)if(t.value===k.value){t.moved=k.index;k.moved=t.index;v.splice(e,1);b=e=0;break}b+=e}}return g.reverse()}function T(a,d,c,e,f){f=f||{};var g=a&&N(a),g=g&&g.ownerDocument,h=f.templateEngine||O;b.za.vb(c,h,g);c=h.renderTemplate(c,e,f,g);("number"!=typeof c.length||0<c.length&&"number"!=typeof c[0].nodeType)&&j(Error("Template engine must return an array of DOM nodes"));g=r;switch(d){case "replaceChildren":b.e.N(a,c);g=m;break;case "replaceNode":b.a.Ya(a,c);g=m;break;case "ignoreTargetNode":break;
|
|
|
-default:j(Error("Unknown renderMode: "+d))}g&&(U(c,e),f.afterRender&&b.r.K(f.afterRender,p,[c,e.$data]));return c}function N(a){return a.nodeType?a:0<a.length?a[0]:p}function U(a,d){if(a.length){var c=a[0],e=a[a.length-1];V(c,e,function(a){b.Da(d,a)});V(c,e,function(a){b.s.ib(a,[d])})}}function V(a,d,c){var e;for(d=b.e.nextSibling(d);a&&(e=a)!==d;)a=b.e.nextSibling(e),(1===e.nodeType||8===e.nodeType)&&c(e)}function W(a,d,c){a=b.g.aa(a);for(var e=b.g.Q,f=0;f<a.length;f++){var g=a[f].key;if(e.hasOwnProperty(g)){var h=
|
|
|
-e[g];"function"===typeof h?(g=h(a[f].value))&&j(Error(g)):h||j(Error("This template engine does not support the '"+g+"' binding within its templates"))}}a="ko.__tr_ambtns(function($context,$element){return(function(){return{ "+b.g.ba(a)+" } })()})";return c.createJavaScriptEvaluatorBlock(a)+d}function X(a,d,c,e){function f(a){return function(){return k[a]}}function g(){return k}var h=0,k,l;b.j(function(){var n=c&&c instanceof b.z?c:new b.z(b.a.d(c)),q=n.$data;e&&b.eb(a,n);if(k=("function"==typeof d?
|
|
|
-d(n,a):d)||b.J.instance.getBindings(a,n)){if(0===h){h=1;for(var s in k){var v=b.c[s];v&&8===a.nodeType&&!b.e.I[s]&&j(Error("The binding '"+s+"' cannot be used with virtual elements"));if(v&&"function"==typeof v.init&&(v=(0,v.init)(a,f(s),g,q,n))&&v.controlsDescendantBindings)l!==I&&j(Error("Multiple bindings ("+l+" and "+s+") are trying to control descendant bindings of the same element. You cannot use these bindings together on the same element.")),l=s}h=2}if(2===h)for(s in k)(v=b.c[s])&&"function"==
|
|
|
-typeof v.update&&(0,v.update)(a,f(s),g,q,n)}},p,{W:a});return{Nb:l===I}}function Y(a,d,c){var e=m,f=1===d.nodeType;f&&b.e.Ta(d);if(f&&c||b.J.instance.nodeHasBindings(d))e=X(d,p,a,c).Nb;e&&Z(a,d,!f)}function Z(a,d,c){for(var e=b.e.firstChild(d);d=e;)e=b.e.nextSibling(d),Y(a,d,c)}function $(a,b){var c=aa(a,b);return c?0<c.length?c[c.length-1].nextSibling:a.nextSibling:p}function aa(a,b){for(var c=a,e=1,f=[];c=c.nextSibling;){if(H(c)&&(e--,0===e))return f;f.push(c);B(c)&&e++}b||j(Error("Cannot find closing comment tag to match: "+
|
|
|
-a.nodeValue));return p}function H(a){return 8==a.nodeType&&(K?a.text:a.nodeValue).match(ia)}function B(a){return 8==a.nodeType&&(K?a.text:a.nodeValue).match(ja)}function P(a,b){for(var c=p;a!=c;)c=a,a=a.replace(ka,function(a,c){return b[c]});return a}function la(){var a=[],d=[];this.save=function(c,e){var f=b.a.i(a,c);0<=f?d[f]=e:(a.push(c),d.push(e))};this.get=function(c){c=b.a.i(a,c);return 0<=c?d[c]:I}}function ba(a,b,c){function e(e){var g=b(a[e]);switch(typeof g){case "boolean":case "number":case "string":case "function":f[e]=
|
|
|
-g;break;case "object":case "undefined":var h=c.get(g);f[e]=h!==I?h:ba(g,b,c)}}c=c||new la;a=b(a);if(!("object"==typeof a&&a!==p&&a!==I&&!(a instanceof Date)))return a;var f=a instanceof Array?[]:{};c.save(a,f);var g=a;if(g instanceof Array){for(var h=0;h<g.length;h++)e(h);"function"==typeof g.toJSON&&e("toJSON")}else for(h in g)e(h);return f}function ca(a,d){if(a)if(8==a.nodeType){var c=b.s.Ua(a.nodeValue);c!=p&&d.push({sb:a,Fb:c})}else if(1==a.nodeType)for(var c=0,e=a.childNodes,f=e.length;c<f;c++)ca(e[c],
|
|
|
-d)}function Q(a,d,c,e){b.c[a]={init:function(a){b.a.f.set(a,da,{});return{controlsDescendantBindings:m}},update:function(a,g,h,k,l){h=b.a.f.get(a,da);g=b.a.d(g());k=!c!==!g;var n=!h.Za;if(n||d||k!==h.qb)n&&(h.Za=b.a.Ia(b.e.childNodes(a),m)),k?(n||b.e.N(a,b.a.Ia(h.Za)),b.Ea(e?e(l,g):l,a)):b.e.Y(a),h.qb=k}};b.g.Q[a]=r;b.e.I[a]=m}function ea(a,d,c){c&&d!==b.k.q(a)&&b.k.T(a,d);d!==b.k.q(a)&&b.r.K(b.a.Ba,p,[a,"change"])}var b="undefined"!==typeof w?w:{};b.b=function(a,d){for(var c=a.split("."),e=b,f=0;f<
|
|
|
-c.length-1;f++)e=e[c[f]];e[c[c.length-1]]=d};b.p=function(a,b,c){a[b]=c};b.version="2.2.1";b.b("version",b.version);b.a=new function(){function a(a,d){if("input"!==b.a.u(a)||!a.type||"click"!=d.toLowerCase())return r;var c=a.type;return"checkbox"==c||"radio"==c}var d=/^(\s|\u00A0)+|(\s|\u00A0)+$/g,c={},e={};c[/Firefox\/2/i.test(ga.userAgent)?"KeyboardEvent":"UIEvents"]=["keyup","keydown","keypress"];c.MouseEvents="click dblclick mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave".split(" ");
|
|
|
-for(var f in c){var g=c[f];if(g.length)for(var h=0,k=g.length;h<k;h++)e[g[h]]=f}var l={propertychange:m},n,c=3;f=y.createElement("div");for(g=f.getElementsByTagName("i");f.innerHTML="\x3c!--[if gt IE "+ ++c+"]><i></i><![endif]--\x3e",g[0];);n=4<c?c:I;return{Na:["authenticity_token",/^__RequestVerificationToken(_.*)?$/],o:function(a,b){for(var d=0,c=a.length;d<c;d++)b(a[d])},i:function(a,b){if("function"==typeof Array.prototype.indexOf)return Array.prototype.indexOf.call(a,b);for(var d=0,c=a.length;d<
|
|
|
-c;d++)if(a[d]===b)return d;return-1},lb:function(a,b,d){for(var c=0,e=a.length;c<e;c++)if(b.call(d,a[c]))return a[c];return p},ga:function(a,d){var c=b.a.i(a,d);0<=c&&a.splice(c,1)},Ga:function(a){a=a||[];for(var d=[],c=0,e=a.length;c<e;c++)0>b.a.i(d,a[c])&&d.push(a[c]);return d},V:function(a,b){a=a||[];for(var d=[],c=0,e=a.length;c<e;c++)d.push(b(a[c]));return d},fa:function(a,b){a=a||[];for(var d=[],c=0,e=a.length;c<e;c++)b(a[c])&&d.push(a[c]);return d},P:function(a,b){if(b instanceof Array)a.push.apply(a,
|
|
|
-b);else for(var d=0,c=b.length;d<c;d++)a.push(b[d]);return a},extend:function(a,b){if(b)for(var d in b)b.hasOwnProperty(d)&&(a[d]=b[d]);return a},ka:function(a){for(;a.firstChild;)b.removeNode(a.firstChild)},Hb:function(a){a=b.a.L(a);for(var d=y.createElement("div"),c=0,e=a.length;c<e;c++)d.appendChild(b.A(a[c]));return d},Ia:function(a,d){for(var c=0,e=a.length,g=[];c<e;c++){var f=a[c].cloneNode(m);g.push(d?b.A(f):f)}return g},N:function(a,d){b.a.ka(a);if(d)for(var c=0,e=d.length;c<e;c++)a.appendChild(d[c])},
|
|
|
-Ya:function(a,d){var c=a.nodeType?[a]:a;if(0<c.length){for(var e=c[0],g=e.parentNode,f=0,h=d.length;f<h;f++)g.insertBefore(d[f],e);f=0;for(h=c.length;f<h;f++)b.removeNode(c[f])}},bb:function(a,b){7>n?a.setAttribute("selected",b):a.selected=b},D:function(a){return(a||"").replace(d,"")},Rb:function(a,d){for(var c=[],e=(a||"").split(d),f=0,g=e.length;f<g;f++){var h=b.a.D(e[f]);""!==h&&c.push(h)}return c},Ob:function(a,b){a=a||"";return b.length>a.length?r:a.substring(0,b.length)===b},tb:function(a,b){if(b.compareDocumentPosition)return 16==
|
|
|
-(b.compareDocumentPosition(a)&16);for(;a!=p;){if(a==b)return m;a=a.parentNode}return r},X:function(a){return b.a.tb(a,a.ownerDocument)},u:function(a){return a&&a.tagName&&a.tagName.toLowerCase()},n:function(b,d,c){var e=n&&l[d];if(!e&&"undefined"!=typeof F){if(a(b,d)){var f=c;c=function(a,b){var d=this.checked;b&&(this.checked=b.nb!==m);f.call(this,a);this.checked=d}}F(b).bind(d,c)}else!e&&"function"==typeof b.addEventListener?b.addEventListener(d,c,r):"undefined"!=typeof b.attachEvent?b.attachEvent("on"+
|
|
|
-d,function(a){c.call(b,a)}):j(Error("Browser doesn't support addEventListener or attachEvent"))},Ba:function(b,d){(!b||!b.nodeType)&&j(Error("element must be a DOM node when calling triggerEvent"));if("undefined"!=typeof F){var c=[];a(b,d)&&c.push({nb:b.checked});F(b).trigger(d,c)}else"function"==typeof y.createEvent?"function"==typeof b.dispatchEvent?(c=y.createEvent(e[d]||"HTMLEvents"),c.initEvent(d,m,m,x,0,0,0,0,0,r,r,r,r,0,b),b.dispatchEvent(c)):j(Error("The supplied element doesn't support dispatchEvent")):
|
|
|
-"undefined"!=typeof b.fireEvent?(a(b,d)&&(b.checked=b.checked!==m),b.fireEvent("on"+d)):j(Error("Browser doesn't support triggering events"))},d:function(a){return b.$(a)?a():a},ua:function(a){return b.$(a)?a.t():a},da:function(a,d,c){if(d){var e=/[\w-]+/g,f=a.className.match(e)||[];b.a.o(d.match(e),function(a){var d=b.a.i(f,a);0<=d?c||f.splice(d,1):c&&f.push(a)});a.className=f.join(" ")}},cb:function(a,d){var c=b.a.d(d);if(c===p||c===I)c="";if(3===a.nodeType)a.data=c;else{var e=b.e.firstChild(a);
|
|
|
-!e||3!=e.nodeType||b.e.nextSibling(e)?b.e.N(a,[y.createTextNode(c)]):e.data=c;b.a.wb(a)}},ab:function(a,b){a.name=b;if(7>=n)try{a.mergeAttributes(y.createElement("<input name='"+a.name+"'/>"),r)}catch(d){}},wb:function(a){9<=n&&(a=1==a.nodeType?a:a.parentNode,a.style&&(a.style.zoom=a.style.zoom))},ub:function(a){if(9<=n){var b=a.style.width;a.style.width=0;a.style.width=b}},Lb:function(a,d){a=b.a.d(a);d=b.a.d(d);for(var c=[],e=a;e<=d;e++)c.push(e);return c},L:function(a){for(var b=[],d=0,c=a.length;d<
|
|
|
-c;d++)b.push(a[d]);return b},Pb:6===n,Qb:7===n,Z:n,Oa:function(a,d){for(var c=b.a.L(a.getElementsByTagName("input")).concat(b.a.L(a.getElementsByTagName("textarea"))),e="string"==typeof d?function(a){return a.name===d}:function(a){return d.test(a.name)},f=[],g=c.length-1;0<=g;g--)e(c[g])&&f.push(c[g]);return f},Ib:function(a){return"string"==typeof a&&(a=b.a.D(a))?x.JSON&&x.JSON.parse?x.JSON.parse(a):(new Function("return "+a))():p},xa:function(a,d,c){("undefined"==typeof JSON||"undefined"==typeof JSON.stringify)&&
|
|
|
-j(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(b.a.d(a),d,c)},Jb:function(a,d,c){c=c||{};var e=c.params||{},f=c.includeFields||this.Na,g=a;if("object"==typeof a&&"form"===b.a.u(a))for(var g=a.action,h=f.length-1;0<=h;h--)for(var k=b.a.Oa(a,f[h]),l=k.length-1;0<=l;l--)e[k[l].name]=k[l].value;d=b.a.d(d);var n=y.createElement("form");
|
|
|
-n.style.display="none";n.action=g;n.method="post";for(var w in d)a=y.createElement("input"),a.name=w,a.value=b.a.xa(b.a.d(d[w])),n.appendChild(a);for(w in e)a=y.createElement("input"),a.name=w,a.value=e[w],n.appendChild(a);y.body.appendChild(n);c.submitter?c.submitter(n):n.submit();setTimeout(function(){n.parentNode.removeChild(n)},0)}}};b.b("utils",b.a);b.b("utils.arrayForEach",b.a.o);b.b("utils.arrayFirst",b.a.lb);b.b("utils.arrayFilter",b.a.fa);b.b("utils.arrayGetDistinctValues",b.a.Ga);b.b("utils.arrayIndexOf",
|
|
|
-b.a.i);b.b("utils.arrayMap",b.a.V);b.b("utils.arrayPushAll",b.a.P);b.b("utils.arrayRemoveItem",b.a.ga);b.b("utils.extend",b.a.extend);b.b("utils.fieldsIncludedWithJsonPost",b.a.Na);b.b("utils.getFormFields",b.a.Oa);b.b("utils.peekObservable",b.a.ua);b.b("utils.postJson",b.a.Jb);b.b("utils.parseJson",b.a.Ib);b.b("utils.registerEventHandler",b.a.n);b.b("utils.stringifyJson",b.a.xa);b.b("utils.range",b.a.Lb);b.b("utils.toggleDomNodeCssClass",b.a.da);b.b("utils.triggerEvent",b.a.Ba);b.b("utils.unwrapObservable",
|
|
|
-b.a.d);Function.prototype.bind||(Function.prototype.bind=function(a){var b=this,c=Array.prototype.slice.call(arguments);a=c.shift();return function(){return b.apply(a,c.concat(Array.prototype.slice.call(arguments)))}});b.a.f=new function(){var a=0,d="__ko__"+(new Date).getTime(),c={};return{get:function(a,d){var c=b.a.f.la(a,r);return c===I?I:c[d]},set:function(a,d,c){c===I&&b.a.f.la(a,r)===I||(b.a.f.la(a,m)[d]=c)},la:function(b,f){var g=b[d];if(!g||!("null"!==g&&c[g])){if(!f)return I;g=b[d]="ko"+
|
|
|
-a++;c[g]={}}return c[g]},clear:function(a){var b=a[d];return b?(delete c[b],a[d]=p,m):r}}};b.b("utils.domData",b.a.f);b.b("utils.domData.clear",b.a.f.clear);b.a.F=new function(){function a(a,d){var e=b.a.f.get(a,c);e===I&&d&&(e=[],b.a.f.set(a,c,e));return e}function d(c){var e=a(c,r);if(e)for(var e=e.slice(0),k=0;k<e.length;k++)e[k](c);b.a.f.clear(c);"function"==typeof F&&"function"==typeof F.cleanData&&F.cleanData([c]);if(f[c.nodeType])for(e=c.firstChild;c=e;)e=c.nextSibling,8===c.nodeType&&d(c)}
|
|
|
-var c="__ko_domNodeDisposal__"+(new Date).getTime(),e={1:m,8:m,9:m},f={1:m,9:m};return{Ca:function(b,d){"function"!=typeof d&&j(Error("Callback must be a function"));a(b,m).push(d)},Xa:function(d,e){var f=a(d,r);f&&(b.a.ga(f,e),0==f.length&&b.a.f.set(d,c,I))},A:function(a){if(e[a.nodeType]&&(d(a),f[a.nodeType])){var c=[];b.a.P(c,a.getElementsByTagName("*"));for(var k=0,l=c.length;k<l;k++)d(c[k])}return a},removeNode:function(a){b.A(a);a.parentNode&&a.parentNode.removeChild(a)}}};b.A=b.a.F.A;b.removeNode=
|
|
|
-b.a.F.removeNode;b.b("cleanNode",b.A);b.b("removeNode",b.removeNode);b.b("utils.domNodeDisposal",b.a.F);b.b("utils.domNodeDisposal.addDisposeCallback",b.a.F.Ca);b.b("utils.domNodeDisposal.removeDisposeCallback",b.a.F.Xa);b.a.ta=function(a){var d;if("undefined"!=typeof F)if(F.parseHTML)d=F.parseHTML(a);else{if((d=F.clean([a]))&&d[0]){for(a=d[0];a.parentNode&&11!==a.parentNode.nodeType;)a=a.parentNode;a.parentNode&&a.parentNode.removeChild(a)}}else{var c=b.a.D(a).toLowerCase();d=y.createElement("div");
|
|
|
-c=c.match(/^<(thead|tbody|tfoot)/)&&[1,"<table>","</table>"]||!c.indexOf("<tr")&&[2,"<table><tbody>","</tbody></table>"]||(!c.indexOf("<td")||!c.indexOf("<th"))&&[3,"<table><tbody><tr>","</tr></tbody></table>"]||[0,"",""];a="ignored<div>"+c[1]+a+c[2]+"</div>";for("function"==typeof x.innerShiv?d.appendChild(x.innerShiv(a)):d.innerHTML=a;c[0]--;)d=d.lastChild;d=b.a.L(d.lastChild.childNodes)}return d};b.a.ca=function(a,d){b.a.ka(a);d=b.a.d(d);if(d!==p&&d!==I)if("string"!=typeof d&&(d=d.toString()),
|
|
|
-"undefined"!=typeof F)F(a).html(d);else for(var c=b.a.ta(d),e=0;e<c.length;e++)a.appendChild(c[e])};b.b("utils.parseHtmlFragment",b.a.ta);b.b("utils.setHtml",b.a.ca);var R={};b.s={ra:function(a){"function"!=typeof a&&j(Error("You can only pass a function to ko.memoization.memoize()"));var b=(4294967296*(1+Math.random())|0).toString(16).substring(1)+(4294967296*(1+Math.random())|0).toString(16).substring(1);R[b]=a;return"\x3c!--[ko_memo:"+b+"]--\x3e"},hb:function(a,b){var c=R[a];c===I&&j(Error("Couldn't find any memo with ID "+
|
|
|
-a+". Perhaps it's already been unmemoized."));try{return c.apply(p,b||[]),m}finally{delete R[a]}},ib:function(a,d){var c=[];ca(a,c);for(var e=0,f=c.length;e<f;e++){var g=c[e].sb,h=[g];d&&b.a.P(h,d);b.s.hb(c[e].Fb,h);g.nodeValue="";g.parentNode&&g.parentNode.removeChild(g)}},Ua:function(a){return(a=a.match(/^\[ko_memo\:(.*?)\]$/))?a[1]:p}};b.b("memoization",b.s);b.b("memoization.memoize",b.s.ra);b.b("memoization.unmemoize",b.s.hb);b.b("memoization.parseMemoText",b.s.Ua);b.b("memoization.unmemoizeDomNodeAndDescendants",
|
|
|
-b.s.ib);b.Ma={throttle:function(a,d){a.throttleEvaluation=d;var c=p;return b.j({read:a,write:function(b){clearTimeout(c);c=setTimeout(function(){a(b)},d)}})},notify:function(a,d){a.equalityComparer="always"==d?u(r):b.m.fn.equalityComparer;return a}};b.b("extenders",b.Ma);b.fb=function(a,d,c){this.target=a;this.ha=d;this.rb=c;b.p(this,"dispose",this.B)};b.fb.prototype.B=function(){this.Cb=m;this.rb()};b.S=function(){this.w={};b.a.extend(this,b.S.fn);b.p(this,"subscribe",this.ya);b.p(this,"extend",
|
|
|
-this.extend);b.p(this,"getSubscriptionsCount",this.yb)};b.S.fn={ya:function(a,d,c){c=c||"change";var e=new b.fb(this,d?a.bind(d):a,function(){b.a.ga(this.w[c],e)}.bind(this));this.w[c]||(this.w[c]=[]);this.w[c].push(e);return e},notifySubscribers:function(a,d){d=d||"change";this.w[d]&&b.r.K(function(){b.a.o(this.w[d].slice(0),function(b){b&&b.Cb!==m&&b.ha(a)})},this)},yb:function(){var a=0,b;for(b in this.w)this.w.hasOwnProperty(b)&&(a+=this.w[b].length);return a},extend:function(a){var d=this;if(a)for(var c in a){var e=
|
|
|
-b.Ma[c];"function"==typeof e&&(d=e(d,a[c]))}return d}};b.Qa=function(a){return"function"==typeof a.ya&&"function"==typeof a.notifySubscribers};b.b("subscribable",b.S);b.b("isSubscribable",b.Qa);var C=[];b.r={mb:function(a){C.push({ha:a,La:[]})},end:function(){C.pop()},Wa:function(a){b.Qa(a)||j(Error("Only subscribable things can act as dependencies"));if(0<C.length){var d=C[C.length-1];d&&!(0<=b.a.i(d.La,a))&&(d.La.push(a),d.ha(a))}},K:function(a,b,c){try{return C.push(p),a.apply(b,c||[])}finally{C.pop()}}};
|
|
|
-var ma={undefined:m,"boolean":m,number:m,string:m};b.m=function(a){function d(){if(0<arguments.length){if(!d.equalityComparer||!d.equalityComparer(c,arguments[0]))d.H(),c=arguments[0],d.G();return this}b.r.Wa(d);return c}var c=a;b.S.call(d);d.t=function(){return c};d.G=function(){d.notifySubscribers(c)};d.H=function(){d.notifySubscribers(c,"beforeChange")};b.a.extend(d,b.m.fn);b.p(d,"peek",d.t);b.p(d,"valueHasMutated",d.G);b.p(d,"valueWillMutate",d.H);return d};b.m.fn={equalityComparer:function(a,
|
|
|
-b){return a===p||typeof a in ma?a===b:r}};var E=b.m.Kb="__ko_proto__";b.m.fn[E]=b.m;b.ma=function(a,d){return a===p||a===I||a[E]===I?r:a[E]===d?m:b.ma(a[E],d)};b.$=function(a){return b.ma(a,b.m)};b.Ra=function(a){return"function"==typeof a&&a[E]===b.m||"function"==typeof a&&a[E]===b.j&&a.zb?m:r};b.b("observable",b.m);b.b("isObservable",b.$);b.b("isWriteableObservable",b.Ra);b.R=function(a){0==arguments.length&&(a=[]);a!==p&&(a!==I&&!("length"in a))&&j(Error("The argument passed when initializing an observable array must be an array, or null, or undefined."));
|
|
|
-var d=b.m(a);b.a.extend(d,b.R.fn);return d};b.R.fn={remove:function(a){for(var b=this.t(),c=[],e="function"==typeof a?a:function(b){return b===a},f=0;f<b.length;f++){var g=b[f];e(g)&&(0===c.length&&this.H(),c.push(g),b.splice(f,1),f--)}c.length&&this.G();return c},removeAll:function(a){if(a===I){var d=this.t(),c=d.slice(0);this.H();d.splice(0,d.length);this.G();return c}return!a?[]:this.remove(function(d){return 0<=b.a.i(a,d)})},destroy:function(a){var b=this.t(),c="function"==typeof a?a:function(b){return b===
|
|
|
-a};this.H();for(var e=b.length-1;0<=e;e--)c(b[e])&&(b[e]._destroy=m);this.G()},destroyAll:function(a){return a===I?this.destroy(u(m)):!a?[]:this.destroy(function(d){return 0<=b.a.i(a,d)})},indexOf:function(a){var d=this();return b.a.i(d,a)},replace:function(a,b){var c=this.indexOf(a);0<=c&&(this.H(),this.t()[c]=b,this.G())}};b.a.o("pop push reverse shift sort splice unshift".split(" "),function(a){b.R.fn[a]=function(){var b=this.t();this.H();b=b[a].apply(b,arguments);this.G();return b}});b.a.o(["slice"],
|
|
|
-function(a){b.R.fn[a]=function(){var b=this();return b[a].apply(b,arguments)}});b.b("observableArray",b.R);b.j=function(a,d,c){function e(){b.a.o(z,function(a){a.B()});z=[]}function f(){var a=h.throttleEvaluation;a&&0<=a?(clearTimeout(t),t=setTimeout(g,a)):g()}function g(){if(!q)if(n&&w())A();else{q=m;try{var a=b.a.V(z,function(a){return a.target});b.r.mb(function(c){var d;0<=(d=b.a.i(a,c))?a[d]=I:z.push(c.ya(f))});for(var c=s.call(d),e=a.length-1;0<=e;e--)a[e]&&z.splice(e,1)[0].B();n=m;h.notifySubscribers(l,
|
|
|
-"beforeChange");l=c}finally{b.r.end()}h.notifySubscribers(l);q=r;z.length||A()}}function h(){if(0<arguments.length)return"function"===typeof v?v.apply(d,arguments):j(Error("Cannot write a value to a ko.computed unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters.")),this;n||g();b.r.Wa(h);return l}function k(){return!n||0<z.length}var l,n=r,q=r,s=a;s&&"object"==typeof s?(c=s,s=c.read):(c=c||{},s||(s=c.read));"function"!=typeof s&&j(Error("Pass a function that returns the value of the ko.computed"));
|
|
|
-var v=c.write,G=c.disposeWhenNodeIsRemoved||c.W||p,w=c.disposeWhen||c.Ka||u(r),A=e,z=[],t=p;d||(d=c.owner);h.t=function(){n||g();return l};h.xb=function(){return z.length};h.zb="function"===typeof c.write;h.B=function(){A()};h.pa=k;b.S.call(h);b.a.extend(h,b.j.fn);b.p(h,"peek",h.t);b.p(h,"dispose",h.B);b.p(h,"isActive",h.pa);b.p(h,"getDependenciesCount",h.xb);c.deferEvaluation!==m&&g();if(G&&k()){A=function(){b.a.F.Xa(G,arguments.callee);e()};b.a.F.Ca(G,A);var D=w,w=function(){return!b.a.X(G)||D()}}return h};
|
|
|
-b.Bb=function(a){return b.ma(a,b.j)};w=b.m.Kb;b.j[w]=b.m;b.j.fn={};b.j.fn[w]=b.j;b.b("dependentObservable",b.j);b.b("computed",b.j);b.b("isComputed",b.Bb);b.gb=function(a){0==arguments.length&&j(Error("When calling ko.toJS, pass the object you want to convert."));return ba(a,function(a){for(var c=0;b.$(a)&&10>c;c++)a=a();return a})};b.toJSON=function(a,d,c){a=b.gb(a);return b.a.xa(a,d,c)};b.b("toJS",b.gb);b.b("toJSON",b.toJSON);b.k={q:function(a){switch(b.a.u(a)){case "option":return a.__ko__hasDomDataOptionValue__===
|
|
|
-m?b.a.f.get(a,b.c.options.sa):7>=b.a.Z?a.getAttributeNode("value").specified?a.value:a.text:a.value;case "select":return 0<=a.selectedIndex?b.k.q(a.options[a.selectedIndex]):I;default:return a.value}},T:function(a,d){switch(b.a.u(a)){case "option":switch(typeof d){case "string":b.a.f.set(a,b.c.options.sa,I);"__ko__hasDomDataOptionValue__"in a&&delete a.__ko__hasDomDataOptionValue__;a.value=d;break;default:b.a.f.set(a,b.c.options.sa,d),a.__ko__hasDomDataOptionValue__=m,a.value="number"===typeof d?
|
|
|
-d:""}break;case "select":for(var c=a.options.length-1;0<=c;c--)if(b.k.q(a.options[c])==d){a.selectedIndex=c;break}break;default:if(d===p||d===I)d="";a.value=d}}};b.b("selectExtensions",b.k);b.b("selectExtensions.readValue",b.k.q);b.b("selectExtensions.writeValue",b.k.T);var ka=/\@ko_token_(\d+)\@/g,na=["true","false"],oa=/^(?:[$_a-z][$\w]*|(.+)(\.\s*[$_a-z][$\w]*|\[.+\]))$/i;b.g={Q:[],aa:function(a){var d=b.a.D(a);if(3>d.length)return[];"{"===d.charAt(0)&&(d=d.substring(1,d.length-1));a=[];for(var c=
|
|
|
-p,e,f=0;f<d.length;f++){var g=d.charAt(f);if(c===p)switch(g){case '"':case "'":case "/":c=f,e=g}else if(g==e&&"\\"!==d.charAt(f-1)){g=d.substring(c,f+1);a.push(g);var h="@ko_token_"+(a.length-1)+"@",d=d.substring(0,c)+h+d.substring(f+1),f=f-(g.length-h.length),c=p}}e=c=p;for(var k=0,l=p,f=0;f<d.length;f++){g=d.charAt(f);if(c===p)switch(g){case "{":c=f;l=g;e="}";break;case "(":c=f;l=g;e=")";break;case "[":c=f,l=g,e="]"}g===l?k++:g===e&&(k--,0===k&&(g=d.substring(c,f+1),a.push(g),h="@ko_token_"+(a.length-
|
|
|
-1)+"@",d=d.substring(0,c)+h+d.substring(f+1),f-=g.length-h.length,c=p))}e=[];d=d.split(",");c=0;for(f=d.length;c<f;c++)k=d[c],l=k.indexOf(":"),0<l&&l<k.length-1?(g=k.substring(l+1),e.push({key:P(k.substring(0,l),a),value:P(g,a)})):e.push({unknown:P(k,a)});return e},ba:function(a){var d="string"===typeof a?b.g.aa(a):a,c=[];a=[];for(var e,f=0;e=d[f];f++)if(0<c.length&&c.push(","),e.key){var g;a:{g=e.key;var h=b.a.D(g);switch(h.length&&h.charAt(0)){case "'":case '"':break a;default:g="'"+h+"'"}}e=e.value;
|
|
|
-c.push(g);c.push(":");c.push(e);e=b.a.D(e);0<=b.a.i(na,b.a.D(e).toLowerCase())?e=r:(h=e.match(oa),e=h===p?r:h[1]?"Object("+h[1]+")"+h[2]:e);e&&(0<a.length&&a.push(", "),a.push(g+" : function(__ko_value) { "+e+" = __ko_value; }"))}else e.unknown&&c.push(e.unknown);d=c.join("");0<a.length&&(d=d+", '_ko_property_writers' : { "+a.join("")+" } ");return d},Eb:function(a,d){for(var c=0;c<a.length;c++)if(b.a.D(a[c].key)==d)return m;return r},ea:function(a,d,c,e,f){if(!a||!b.Ra(a)){if((a=d()._ko_property_writers)&&
|
|
|
-a[c])a[c](e)}else(!f||a.t()!==e)&&a(e)}};b.b("expressionRewriting",b.g);b.b("expressionRewriting.bindingRewriteValidators",b.g.Q);b.b("expressionRewriting.parseObjectLiteral",b.g.aa);b.b("expressionRewriting.preProcessBindings",b.g.ba);b.b("jsonExpressionRewriting",b.g);b.b("jsonExpressionRewriting.insertPropertyAccessorsIntoJson",b.g.ba);var K="\x3c!--test--\x3e"===y.createComment("test").text,ja=K?/^\x3c!--\s*ko(?:\s+(.+\s*\:[\s\S]*))?\s*--\x3e$/:/^\s*ko(?:\s+(.+\s*\:[\s\S]*))?\s*$/,ia=K?/^\x3c!--\s*\/ko\s*--\x3e$/:
|
|
|
-/^\s*\/ko\s*$/,pa={ul:m,ol:m};b.e={I:{},childNodes:function(a){return B(a)?aa(a):a.childNodes},Y:function(a){if(B(a)){a=b.e.childNodes(a);for(var d=0,c=a.length;d<c;d++)b.removeNode(a[d])}else b.a.ka(a)},N:function(a,d){if(B(a)){b.e.Y(a);for(var c=a.nextSibling,e=0,f=d.length;e<f;e++)c.parentNode.insertBefore(d[e],c)}else b.a.N(a,d)},Va:function(a,b){B(a)?a.parentNode.insertBefore(b,a.nextSibling):a.firstChild?a.insertBefore(b,a.firstChild):a.appendChild(b)},Pa:function(a,d,c){c?B(a)?a.parentNode.insertBefore(d,
|
|
|
-c.nextSibling):c.nextSibling?a.insertBefore(d,c.nextSibling):a.appendChild(d):b.e.Va(a,d)},firstChild:function(a){return!B(a)?a.firstChild:!a.nextSibling||H(a.nextSibling)?p:a.nextSibling},nextSibling:function(a){B(a)&&(a=$(a));return a.nextSibling&&H(a.nextSibling)?p:a.nextSibling},jb:function(a){return(a=B(a))?a[1]:p},Ta:function(a){if(pa[b.a.u(a)]){var d=a.firstChild;if(d){do if(1===d.nodeType){var c;c=d.firstChild;var e=p;if(c){do if(e)e.push(c);else if(B(c)){var f=$(c,m);f?c=f:e=[c]}else H(c)&&
|
|
|
-(e=[c]);while(c=c.nextSibling)}if(c=e){e=d.nextSibling;for(f=0;f<c.length;f++)e?a.insertBefore(c[f],e):a.appendChild(c[f])}}while(d=d.nextSibling)}}}};b.b("virtualElements",b.e);b.b("virtualElements.allowedBindings",b.e.I);b.b("virtualElements.emptyNode",b.e.Y);b.b("virtualElements.insertAfter",b.e.Pa);b.b("virtualElements.prepend",b.e.Va);b.b("virtualElements.setDomNodeChildren",b.e.N);b.J=function(){this.Ha={}};b.a.extend(b.J.prototype,{nodeHasBindings:function(a){switch(a.nodeType){case 1:return a.getAttribute("data-bind")!=
|
|
|
-p;case 8:return b.e.jb(a)!=p;default:return r}},getBindings:function(a,b){var c=this.getBindingsString(a,b);return c?this.parseBindingsString(c,b,a):p},getBindingsString:function(a){switch(a.nodeType){case 1:return a.getAttribute("data-bind");case 8:return b.e.jb(a);default:return p}},parseBindingsString:function(a,d,c){try{var e;if(!(e=this.Ha[a])){var f=this.Ha,g,h="with($context){with($data||{}){return{"+b.g.ba(a)+"}}}";g=new Function("$context","$element",h);e=f[a]=g}return e(d,c)}catch(k){j(Error("Unable to parse bindings.\nMessage: "+
|
|
|
-k+";\nBindings value: "+a))}}});b.J.instance=new b.J;b.b("bindingProvider",b.J);b.c={};b.z=function(a,d,c){d?(b.a.extend(this,d),this.$parentContext=d,this.$parent=d.$data,this.$parents=(d.$parents||[]).slice(0),this.$parents.unshift(this.$parent)):(this.$parents=[],this.$root=a,this.ko=b);this.$data=a;c&&(this[c]=a)};b.z.prototype.createChildContext=function(a,d){return new b.z(a,this,d)};b.z.prototype.extend=function(a){var d=b.a.extend(new b.z,this);return b.a.extend(d,a)};b.eb=function(a,d){if(2==
|
|
|
-arguments.length)b.a.f.set(a,"__ko_bindingContext__",d);else return b.a.f.get(a,"__ko_bindingContext__")};b.Fa=function(a,d,c){1===a.nodeType&&b.e.Ta(a);return X(a,d,c,m)};b.Ea=function(a,b){(1===b.nodeType||8===b.nodeType)&&Z(a,b,m)};b.Da=function(a,b){b&&(1!==b.nodeType&&8!==b.nodeType)&&j(Error("ko.applyBindings: first parameter should be your view model; second parameter should be a DOM node"));b=b||x.document.body;Y(a,b,m)};b.ja=function(a){switch(a.nodeType){case 1:case 8:var d=b.eb(a);if(d)return d;
|
|
|
-if(a.parentNode)return b.ja(a.parentNode)}return I};b.pb=function(a){return(a=b.ja(a))?a.$data:I};b.b("bindingHandlers",b.c);b.b("applyBindings",b.Da);b.b("applyBindingsToDescendants",b.Ea);b.b("applyBindingsToNode",b.Fa);b.b("contextFor",b.ja);b.b("dataFor",b.pb);var fa={"class":"className","for":"htmlFor"};b.c.attr={update:function(a,d){var c=b.a.d(d())||{},e;for(e in c)if("string"==typeof e){var f=b.a.d(c[e]),g=f===r||f===p||f===I;g&&a.removeAttribute(e);8>=b.a.Z&&e in fa?(e=fa[e],g?a.removeAttribute(e):
|
|
|
-a[e]=f):g||a.setAttribute(e,f.toString());"name"===e&&b.a.ab(a,g?"":f.toString())}}};b.c.checked={init:function(a,d,c){b.a.n(a,"click",function(){var e;if("checkbox"==a.type)e=a.checked;else if("radio"==a.type&&a.checked)e=a.value;else return;var f=d(),g=b.a.d(f);"checkbox"==a.type&&g instanceof Array?(e=b.a.i(g,a.value),a.checked&&0>e?f.push(a.value):!a.checked&&0<=e&&f.splice(e,1)):b.g.ea(f,c,"checked",e,m)});"radio"==a.type&&!a.name&&b.c.uniqueName.init(a,u(m))},update:function(a,d){var c=b.a.d(d());
|
|
|
-"checkbox"==a.type?a.checked=c instanceof Array?0<=b.a.i(c,a.value):c:"radio"==a.type&&(a.checked=a.value==c)}};b.c.css={update:function(a,d){var c=b.a.d(d());if("object"==typeof c)for(var e in c){var f=b.a.d(c[e]);b.a.da(a,e,f)}else c=String(c||""),b.a.da(a,a.__ko__cssValue,r),a.__ko__cssValue=c,b.a.da(a,c,m)}};b.c.enable={update:function(a,d){var c=b.a.d(d());c&&a.disabled?a.removeAttribute("disabled"):!c&&!a.disabled&&(a.disabled=m)}};b.c.disable={update:function(a,d){b.c.enable.update(a,function(){return!b.a.d(d())})}};
|
|
|
-b.c.event={init:function(a,d,c,e){var f=d()||{},g;for(g in f)(function(){var f=g;"string"==typeof f&&b.a.n(a,f,function(a){var g,n=d()[f];if(n){var q=c();try{var s=b.a.L(arguments);s.unshift(e);g=n.apply(e,s)}finally{g!==m&&(a.preventDefault?a.preventDefault():a.returnValue=r)}q[f+"Bubble"]===r&&(a.cancelBubble=m,a.stopPropagation&&a.stopPropagation())}})})()}};b.c.foreach={Sa:function(a){return function(){var d=a(),c=b.a.ua(d);if(!c||"number"==typeof c.length)return{foreach:d,templateEngine:b.C.oa};
|
|
|
-b.a.d(d);return{foreach:c.data,as:c.as,includeDestroyed:c.includeDestroyed,afterAdd:c.afterAdd,beforeRemove:c.beforeRemove,afterRender:c.afterRender,beforeMove:c.beforeMove,afterMove:c.afterMove,templateEngine:b.C.oa}}},init:function(a,d){return b.c.template.init(a,b.c.foreach.Sa(d))},update:function(a,d,c,e,f){return b.c.template.update(a,b.c.foreach.Sa(d),c,e,f)}};b.g.Q.foreach=r;b.e.I.foreach=m;b.c.hasfocus={init:function(a,d,c){function e(e){a.__ko_hasfocusUpdating=m;var f=a.ownerDocument;"activeElement"in
|
|
|
-f&&(e=f.activeElement===a);f=d();b.g.ea(f,c,"hasfocus",e,m);a.__ko_hasfocusUpdating=r}var f=e.bind(p,m),g=e.bind(p,r);b.a.n(a,"focus",f);b.a.n(a,"focusin",f);b.a.n(a,"blur",g);b.a.n(a,"focusout",g)},update:function(a,d){var c=b.a.d(d());a.__ko_hasfocusUpdating||(c?a.focus():a.blur(),b.r.K(b.a.Ba,p,[a,c?"focusin":"focusout"]))}};b.c.html={init:function(){return{controlsDescendantBindings:m}},update:function(a,d){b.a.ca(a,d())}};var da="__ko_withIfBindingData";Q("if");Q("ifnot",r,m);Q("with",m,r,function(a,
|
|
|
-b){return a.createChildContext(b)});b.c.options={update:function(a,d,c){"select"!==b.a.u(a)&&j(Error("options binding applies only to SELECT elements"));for(var e=0==a.length,f=b.a.V(b.a.fa(a.childNodes,function(a){return a.tagName&&"option"===b.a.u(a)&&a.selected}),function(a){return b.k.q(a)||a.innerText||a.textContent}),g=a.scrollTop,h=b.a.d(d());0<a.length;)b.A(a.options[0]),a.remove(0);if(h){c=c();var k=c.optionsIncludeDestroyed;"number"!=typeof h.length&&(h=[h]);if(c.optionsCaption){var l=y.createElement("option");
|
|
|
-b.a.ca(l,c.optionsCaption);b.k.T(l,I);a.appendChild(l)}d=0;for(var n=h.length;d<n;d++){var q=h[d];if(!q||!q._destroy||k){var l=y.createElement("option"),s=function(a,b,c){var d=typeof b;return"function"==d?b(a):"string"==d?a[b]:c},v=s(q,c.optionsValue,q);b.k.T(l,b.a.d(v));q=s(q,c.optionsText,v);b.a.cb(l,q);a.appendChild(l)}}h=a.getElementsByTagName("option");d=k=0;for(n=h.length;d<n;d++)0<=b.a.i(f,b.k.q(h[d]))&&(b.a.bb(h[d],m),k++);a.scrollTop=g;e&&"value"in c&&ea(a,b.a.ua(c.value),m);b.a.ub(a)}}};
|
|
|
-b.c.options.sa="__ko.optionValueDomData__";b.c.selectedOptions={init:function(a,d,c){b.a.n(a,"change",function(){var e=d(),f=[];b.a.o(a.getElementsByTagName("option"),function(a){a.selected&&f.push(b.k.q(a))});b.g.ea(e,c,"value",f)})},update:function(a,d){"select"!=b.a.u(a)&&j(Error("values binding applies only to SELECT elements"));var c=b.a.d(d());c&&"number"==typeof c.length&&b.a.o(a.getElementsByTagName("option"),function(a){var d=0<=b.a.i(c,b.k.q(a));b.a.bb(a,d)})}};b.c.style={update:function(a,
|
|
|
-d){var c=b.a.d(d()||{}),e;for(e in c)if("string"==typeof e){var f=b.a.d(c[e]);a.style[e]=f||""}}};b.c.submit={init:function(a,d,c,e){"function"!=typeof d()&&j(Error("The value for a submit binding must be a function"));b.a.n(a,"submit",function(b){var c,h=d();try{c=h.call(e,a)}finally{c!==m&&(b.preventDefault?b.preventDefault():b.returnValue=r)}})}};b.c.text={update:function(a,d){b.a.cb(a,d())}};b.e.I.text=m;b.c.uniqueName={init:function(a,d){if(d()){var c="ko_unique_"+ ++b.c.uniqueName.ob;b.a.ab(a,
|
|
|
-c)}}};b.c.uniqueName.ob=0;b.c.value={init:function(a,d,c){function e(){h=r;var e=d(),f=b.k.q(a);b.g.ea(e,c,"value",f)}var f=["change"],g=c().valueUpdate,h=r;g&&("string"==typeof g&&(g=[g]),b.a.P(f,g),f=b.a.Ga(f));if(b.a.Z&&("input"==a.tagName.toLowerCase()&&"text"==a.type&&"off"!=a.autocomplete&&(!a.form||"off"!=a.form.autocomplete))&&-1==b.a.i(f,"propertychange"))b.a.n(a,"propertychange",function(){h=m}),b.a.n(a,"blur",function(){h&&e()});b.a.o(f,function(c){var d=e;b.a.Ob(c,"after")&&(d=function(){setTimeout(e,
|
|
|
-0)},c=c.substring(5));b.a.n(a,c,d)})},update:function(a,d){var c="select"===b.a.u(a),e=b.a.d(d()),f=b.k.q(a),g=e!=f;0===e&&(0!==f&&"0"!==f)&&(g=m);g&&(f=function(){b.k.T(a,e)},f(),c&&setTimeout(f,0));c&&0<a.length&&ea(a,e,r)}};b.c.visible={update:function(a,d){var c=b.a.d(d()),e="none"!=a.style.display;c&&!e?a.style.display="":!c&&e&&(a.style.display="none")}};b.c.click={init:function(a,d,c,e){return b.c.event.init.call(this,a,function(){var a={};a.click=d();return a},c,e)}};b.v=function(){};b.v.prototype.renderTemplateSource=
|
|
|
-function(){j(Error("Override renderTemplateSource"))};b.v.prototype.createJavaScriptEvaluatorBlock=function(){j(Error("Override createJavaScriptEvaluatorBlock"))};b.v.prototype.makeTemplateSource=function(a,d){if("string"==typeof a){d=d||y;var c=d.getElementById(a);c||j(Error("Cannot find template with ID "+a));return new b.l.h(c)}if(1==a.nodeType||8==a.nodeType)return new b.l.O(a);j(Error("Unknown template type: "+a))};b.v.prototype.renderTemplate=function(a,b,c,e){a=this.makeTemplateSource(a,e);
|
|
|
-return this.renderTemplateSource(a,b,c)};b.v.prototype.isTemplateRewritten=function(a,b){return this.allowTemplateRewriting===r?m:this.makeTemplateSource(a,b).data("isRewritten")};b.v.prototype.rewriteTemplate=function(a,b,c){a=this.makeTemplateSource(a,c);b=b(a.text());a.text(b);a.data("isRewritten",m)};b.b("templateEngine",b.v);var qa=/(<[a-z]+\d*(\s+(?!data-bind=)[a-z0-9\-]+(=(\"[^\"]*\"|\'[^\']*\'))?)*\s+)data-bind=(["'])([\s\S]*?)\5/gi,ra=/\x3c!--\s*ko\b\s*([\s\S]*?)\s*--\x3e/g;b.za={vb:function(a,
|
|
|
-d,c){d.isTemplateRewritten(a,c)||d.rewriteTemplate(a,function(a){return b.za.Gb(a,d)},c)},Gb:function(a,b){return a.replace(qa,function(a,e,f,g,h,k,l){return W(l,e,b)}).replace(ra,function(a,e){return W(e,"\x3c!-- ko --\x3e",b)})},kb:function(a){return b.s.ra(function(d,c){d.nextSibling&&b.Fa(d.nextSibling,a,c)})}};b.b("__tr_ambtns",b.za.kb);b.l={};b.l.h=function(a){this.h=a};b.l.h.prototype.text=function(){var a=b.a.u(this.h),a="script"===a?"text":"textarea"===a?"value":"innerHTML";if(0==arguments.length)return this.h[a];
|
|
|
-var d=arguments[0];"innerHTML"===a?b.a.ca(this.h,d):this.h[a]=d};b.l.h.prototype.data=function(a){if(1===arguments.length)return b.a.f.get(this.h,"templateSourceData_"+a);b.a.f.set(this.h,"templateSourceData_"+a,arguments[1])};b.l.O=function(a){this.h=a};b.l.O.prototype=new b.l.h;b.l.O.prototype.text=function(){if(0==arguments.length){var a=b.a.f.get(this.h,"__ko_anon_template__")||{};a.Aa===I&&a.ia&&(a.Aa=a.ia.innerHTML);return a.Aa}b.a.f.set(this.h,"__ko_anon_template__",{Aa:arguments[0]})};b.l.h.prototype.nodes=
|
|
|
-function(){if(0==arguments.length)return(b.a.f.get(this.h,"__ko_anon_template__")||{}).ia;b.a.f.set(this.h,"__ko_anon_template__",{ia:arguments[0]})};b.b("templateSources",b.l);b.b("templateSources.domElement",b.l.h);b.b("templateSources.anonymousTemplate",b.l.O);var O;b.wa=function(a){a!=I&&!(a instanceof b.v)&&j(Error("templateEngine must inherit from ko.templateEngine"));O=a};b.va=function(a,d,c,e,f){c=c||{};(c.templateEngine||O)==I&&j(Error("Set a template engine before calling renderTemplate"));
|
|
|
-f=f||"replaceChildren";if(e){var g=N(e);return b.j(function(){var h=d&&d instanceof b.z?d:new b.z(b.a.d(d)),k="function"==typeof a?a(h.$data,h):a,h=T(e,f,k,h,c);"replaceNode"==f&&(e=h,g=N(e))},p,{Ka:function(){return!g||!b.a.X(g)},W:g&&"replaceNode"==f?g.parentNode:g})}return b.s.ra(function(e){b.va(a,d,c,e,"replaceNode")})};b.Mb=function(a,d,c,e,f){function g(a,b){U(b,k);c.afterRender&&c.afterRender(b,a)}function h(d,e){k=f.createChildContext(b.a.d(d),c.as);k.$index=e;var g="function"==typeof a?
|
|
|
-a(d,k):a;return T(p,"ignoreTargetNode",g,k,c)}var k;return b.j(function(){var a=b.a.d(d)||[];"undefined"==typeof a.length&&(a=[a]);a=b.a.fa(a,function(a){return c.includeDestroyed||a===I||a===p||!b.a.d(a._destroy)});b.r.K(b.a.$a,p,[e,a,h,c,g])},p,{W:e})};b.c.template={init:function(a,d){var c=b.a.d(d());if("string"!=typeof c&&!c.name&&(1==a.nodeType||8==a.nodeType))c=1==a.nodeType?a.childNodes:b.e.childNodes(a),c=b.a.Hb(c),(new b.l.O(a)).nodes(c);return{controlsDescendantBindings:m}},update:function(a,
|
|
|
-d,c,e,f){d=b.a.d(d());c={};e=m;var g,h=p;"string"!=typeof d&&(c=d,d=c.name,"if"in c&&(e=b.a.d(c["if"])),e&&"ifnot"in c&&(e=!b.a.d(c.ifnot)),g=b.a.d(c.data));"foreach"in c?h=b.Mb(d||a,e&&c.foreach||[],c,a,f):e?(f="data"in c?f.createChildContext(g,c.as):f,h=b.va(d||a,f,c,a)):b.e.Y(a);f=h;(g=b.a.f.get(a,"__ko__templateComputedDomDataKey__"))&&"function"==typeof g.B&&g.B();b.a.f.set(a,"__ko__templateComputedDomDataKey__",f&&f.pa()?f:I)}};b.g.Q.template=function(a){a=b.g.aa(a);return 1==a.length&&a[0].unknown||
|
|
|
-b.g.Eb(a,"name")?p:"This template engine does not support anonymous templates nested within its templates"};b.e.I.template=m;b.b("setTemplateEngine",b.wa);b.b("renderTemplate",b.va);b.a.Ja=function(a,b,c){a=a||[];b=b||[];return a.length<=b.length?S(a,b,"added","deleted",c):S(b,a,"deleted","added",c)};b.b("utils.compareArrays",b.a.Ja);b.a.$a=function(a,d,c,e,f){function g(a,b){t=l[b];w!==b&&(z[a]=t);t.na(w++);M(t.M);s.push(t);A.push(t)}function h(a,c){if(a)for(var d=0,e=c.length;d<e;d++)c[d]&&b.a.o(c[d].M,
|
|
|
-function(b){a(b,d,c[d].U)})}d=d||[];e=e||{};var k=b.a.f.get(a,"setDomNodeChildrenFromArrayMapping_lastMappingResult")===I,l=b.a.f.get(a,"setDomNodeChildrenFromArrayMapping_lastMappingResult")||[],n=b.a.V(l,function(a){return a.U}),q=b.a.Ja(n,d),s=[],v=0,w=0,B=[],A=[];d=[];for(var z=[],n=[],t,D=0,C,E;C=q[D];D++)switch(E=C.moved,C.status){case "deleted":E===I&&(t=l[v],t.j&&t.j.B(),B.push.apply(B,M(t.M)),e.beforeRemove&&(d[D]=t,A.push(t)));v++;break;case "retained":g(D,v++);break;case "added":E!==I?
|
|
|
-g(D,E):(t={U:C.value,na:b.m(w++)},s.push(t),A.push(t),k||(n[D]=t))}h(e.beforeMove,z);b.a.o(B,e.beforeRemove?b.A:b.removeNode);for(var D=0,k=b.e.firstChild(a),H;t=A[D];D++){t.M||b.a.extend(t,ha(a,c,t.U,f,t.na));for(v=0;q=t.M[v];k=q.nextSibling,H=q,v++)q!==k&&b.e.Pa(a,q,H);!t.Ab&&f&&(f(t.U,t.M,t.na),t.Ab=m)}h(e.beforeRemove,d);h(e.afterMove,z);h(e.afterAdd,n);b.a.f.set(a,"setDomNodeChildrenFromArrayMapping_lastMappingResult",s)};b.b("utils.setDomNodeChildrenFromArrayMapping",b.a.$a);b.C=function(){this.allowTemplateRewriting=
|
|
|
-r};b.C.prototype=new b.v;b.C.prototype.renderTemplateSource=function(a){var d=!(9>b.a.Z)&&a.nodes?a.nodes():p;if(d)return b.a.L(d.cloneNode(m).childNodes);a=a.text();return b.a.ta(a)};b.C.oa=new b.C;b.wa(b.C.oa);b.b("nativeTemplateEngine",b.C);b.qa=function(){var a=this.Db=function(){if("undefined"==typeof F||!F.tmpl)return 0;try{if(0<=F.tmpl.tag.tmpl.open.toString().indexOf("__"))return 2}catch(a){}return 1}();this.renderTemplateSource=function(b,c,e){e=e||{};2>a&&j(Error("Your version of jQuery.tmpl is too old. Please upgrade to jQuery.tmpl 1.0.0pre or later."));
|
|
|
-var f=b.data("precompiled");f||(f=b.text()||"",f=F.template(p,"{{ko_with $item.koBindingContext}}"+f+"{{/ko_with}}"),b.data("precompiled",f));b=[c.$data];c=F.extend({koBindingContext:c},e.templateOptions);c=F.tmpl(f,b,c);c.appendTo(y.createElement("div"));F.fragments={};return c};this.createJavaScriptEvaluatorBlock=function(a){return"{{ko_code ((function() { return "+a+" })()) }}"};this.addTemplate=function(a,b){y.write("<script type='text/html' id='"+a+"'>"+b+"\x3c/script>")};0<a&&(F.tmpl.tag.ko_code=
|
|
|
-{open:"__.push($1 || '');"},F.tmpl.tag.ko_with={open:"with($1) {",close:"} "})};b.qa.prototype=new b.v;w=new b.qa;0<w.Db&&b.wa(w);b.b("jqueryTmplTemplateEngine",b.qa)}"function"===typeof require&&"object"===typeof exports&&"object"===typeof module?L(module.exports||exports):"function"===typeof define&&define.amd?define(["exports"],L):L(x.ko={});m;
|
|
|
+(function(){
|
|
|
+var DEBUG=true;
|
|
|
+(function(window,document,navigator,jQuery,undefined){
|
|
|
+!function(factory) {
|
|
|
+ // Support three module loading scenarios
|
|
|
+ if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
|
|
|
+ // [1] CommonJS/Node.js
|
|
|
+ var target = module['exports'] || exports; // module.exports is for Node.js
|
|
|
+ factory(target);
|
|
|
+ } else if (typeof define === 'function' && define['amd']) {
|
|
|
+ // [2] AMD anonymous module
|
|
|
+ define(['exports'], factory);
|
|
|
+ } else {
|
|
|
+ // [3] No module loader (plain <script> tag) - put directly in global namespace
|
|
|
+ factory(window['ko'] = {});
|
|
|
+ }
|
|
|
+}(function(koExports){
|
|
|
+// Internally, all KO objects are attached to koExports (even the non-exported ones whose names will be minified by the closure compiler).
|
|
|
+// In the future, the following "ko" variable may be made distinct from "koExports" so that private objects are not externally reachable.
|
|
|
+var ko = typeof koExports !== 'undefined' ? koExports : {};
|
|
|
+// Google Closure Compiler helpers (used only to make the minified file smaller)
|
|
|
+ko.exportSymbol = function(koPath, object) {
|
|
|
+ var tokens = koPath.split(".");
|
|
|
+
|
|
|
+ // In the future, "ko" may become distinct from "koExports" (so that non-exported objects are not reachable)
|
|
|
+ // At that point, "target" would be set to: (typeof koExports !== "undefined" ? koExports : ko)
|
|
|
+ var target = ko;
|
|
|
+
|
|
|
+ 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.version = "2.2.1";
|
|
|
+
|
|
|
+ko.exportSymbol('version', ko.version);
|
|
|
+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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ var eventsThatMustBeRegisteredUsingAttachEvent = { 'propertychange': true }; // Workaround for an IE9 issue - https://github.com/SteveSanderson/knockout/issues/406
|
|
|
+
|
|
|
+ // Detect IE versions for bug workarounds (uses IE conditionals, not UA string, for robustness)
|
|
|
+ // Note that, since IE 10 does not support conditional comments, the following logic only detects IE < 10.
|
|
|
+ // Currently this is by design, since IE 10+ behaves correctly when treated as a standard browser.
|
|
|
+ // If there is a future need to detect specific versions of IE10+, we will amend this.
|
|
|
+ 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 ((ko.utils.tagNameLower(element) !== "input") || !element.type) return false;
|
|
|
+ if (eventType.toLowerCase() != "click") return false;
|
|
|
+ var inputType = element.type;
|
|
|
+ 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.prototype.indexOf == "function")
|
|
|
+ return Array.prototype.indexOf.call(array, item);
|
|
|
+ for (var i = 0, j = array.length; i < j; i++)
|
|
|
+ if (array[i] === item)
|
|
|
+ return i;
|
|
|
+ return -1;
|
|
|
+ },
|
|
|
+
|
|
|
+ arrayFirst: function (array, predicate, predicateOwner) {
|
|
|
+ for (var i = 0, j = array.length; i < j; i++)
|
|
|
+ if (predicate.call(predicateOwner, array[i]))
|
|
|
+ return array[i];
|
|
|
+ return null;
|
|
|
+ },
|
|
|
+
|
|
|
+ arrayRemoveItem: function (array, itemToRemove) {
|
|
|
+ var index = ko.utils.arrayIndexOf(array, itemToRemove);
|
|
|
+ if (index >= 0)
|
|
|
+ array.splice(index, 1);
|
|
|
+ },
|
|
|
+
|
|
|
+ arrayGetDistinctValues: function (array) {
|
|
|
+ array = array || [];
|
|
|
+ var result = [];
|
|
|
+ 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++)
|
|
|
+ result.push(mapping(array[i]));
|
|
|
+ return result;
|
|
|
+ },
|
|
|
+
|
|
|
+ arrayFilter: function (array, predicate) {
|
|
|
+ array = array || [];
|
|
|
+ var result = [];
|
|
|
+ for (var i = 0, j = array.length; i < j; i++)
|
|
|
+ if (predicate(array[i]))
|
|
|
+ result.push(array[i]);
|
|
|
+ return result;
|
|
|
+ },
|
|
|
+
|
|
|
+ arrayPushAll: function (array, valuesToPush) {
|
|
|
+ if (valuesToPush instanceof Array)
|
|
|
+ array.push.apply(array, valuesToPush);
|
|
|
+ else
|
|
|
+ for (var i = 0, j = valuesToPush.length; i < j; i++)
|
|
|
+ array.push(valuesToPush[i]);
|
|
|
+ return array;
|
|
|
+ },
|
|
|
+
|
|
|
+ extend: function (target, source) {
|
|
|
+ if (source) {
|
|
|
+ for(var prop in source) {
|
|
|
+ if(source.hasOwnProperty(prop)) {
|
|
|
+ target[prop] = source[prop];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return target;
|
|
|
+ },
|
|
|
+
|
|
|
+ emptyDomNode: function (domNode) {
|
|
|
+ while (domNode.firstChild) {
|
|
|
+ ko.removeNode(domNode.firstChild);
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ moveCleanedNodesToContainerElement: function(nodes) {
|
|
|
+ // Ensure it's a real array, as we're about to reparent the nodes and
|
|
|
+ // we don't want the underlying collection to change while we're doing that.
|
|
|
+ var nodesArray = ko.utils.makeArray(nodes);
|
|
|
+
|
|
|
+ var container = document.createElement('div');
|
|
|
+ for (var i = 0, j = nodesArray.length; i < j; i++) {
|
|
|
+ container.appendChild(ko.cleanNode(nodesArray[i]));
|
|
|
+ }
|
|
|
+ return container;
|
|
|
+ },
|
|
|
+
|
|
|
+ cloneNodes: function (nodesArray, shouldCleanNodes) {
|
|
|
+ for (var i = 0, j = nodesArray.length, newNodesArray = []; i < j; i++) {
|
|
|
+ var clonedNode = nodesArray[i].cloneNode(true);
|
|
|
+ newNodesArray.push(shouldCleanNodes ? ko.cleanNode(clonedNode) : clonedNode);
|
|
|
+ }
|
|
|
+ return newNodesArray;
|
|
|
+ },
|
|
|
+
|
|
|
+ setDomNodeChildren: function (domNode, childNodes) {
|
|
|
+ ko.utils.emptyDomNode(domNode);
|
|
|
+ if (childNodes) {
|
|
|
+ for (var i = 0, j = childNodes.length; i < j; i++)
|
|
|
+ domNode.appendChild(childNodes[i]);
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ replaceDomNodes: function (nodeToReplaceOrNodeArray, newNodesArray) {
|
|
|
+ var nodesToReplaceArray = nodeToReplaceOrNodeArray.nodeType ? [nodeToReplaceOrNodeArray] : nodeToReplaceOrNodeArray;
|
|
|
+ if (nodesToReplaceArray.length > 0) {
|
|
|
+ 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.removeNode(nodesToReplaceArray[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ setOptionNodeSelectionState: function (optionNode, isSelected) {
|
|
|
+ // IE6 sometimes throws "unknown error" if you try to write to .selected directly, whereas Firefox struggles with setAttribute. Pick one based on browser.
|
|
|
+ if (ieVersion < 7)
|
|
|
+ optionNode.setAttribute("selected", isSelected);
|
|
|
+ else
|
|
|
+ optionNode.selected = isSelected;
|
|
|
+ },
|
|
|
+
|
|
|
+ stringTrim: function (string) {
|
|
|
+ return (string || "").replace(stringTrimRegex, "");
|
|
|
+ },
|
|
|
+
|
|
|
+ stringTokenize: function (string, delimiter) {
|
|
|
+ var result = [];
|
|
|
+ var tokens = (string || "").split(delimiter);
|
|
|
+ for (var i = 0, j = tokens.length; i < j; i++) {
|
|
|
+ var trimmed = ko.utils.stringTrim(tokens[i]);
|
|
|
+ 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;
|
|
|
+ },
|
|
|
+
|
|
|
+ domNodeIsContainedBy: function (node, containedByNode) {
|
|
|
+ if (containedByNode.compareDocumentPosition)
|
|
|
+ return (containedByNode.compareDocumentPosition(node) & 16) == 16;
|
|
|
+ while (node != null) {
|
|
|
+ if (node == containedByNode)
|
|
|
+ return true;
|
|
|
+ node = node.parentNode;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ },
|
|
|
+
|
|
|
+ domNodeIsAttachedToDocument: function (node) {
|
|
|
+ return ko.utils.domNodeIsContainedBy(node, node.ownerDocument);
|
|
|
+ },
|
|
|
+
|
|
|
+ tagNameLower: function(element) {
|
|
|
+ // For HTML elements, tagName will always be upper case; for XHTML elements, it'll be lower case.
|
|
|
+ // Possible future optimization: If we know it's an element from an XHTML document (not HTML),
|
|
|
+ // we don't need to do the .toLowerCase() as it will always be lower case anyway.
|
|
|
+ return element && element.tagName && element.tagName.toLowerCase();
|
|
|
+ },
|
|
|
+
|
|
|
+ registerEventHandler: function (element, eventType, handler) {
|
|
|
+ var mustUseAttachEvent = ieVersion && eventsThatMustBeRegisteredUsingAttachEvent[eventType];
|
|
|
+ if (!mustUseAttachEvent && 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 (!mustUseAttachEvent && 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);
|
|
|
+ });
|
|
|
+ else
|
|
|
+ throw new Error("Browser doesn't support addEventListener or attachEvent");
|
|
|
+ },
|
|
|
+
|
|
|
+ triggerEvent: function (element, eventType) {
|
|
|
+ if (!(element && element.nodeType))
|
|
|
+ throw new Error("element must be a DOM node when calling triggerEvent");
|
|
|
+
|
|
|
+ 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 });
|
|
|
+ }
|
|
|
+ jQuery(element)['trigger'](eventType, eventData);
|
|
|
+ } else if (typeof document.createEvent == "function") {
|
|
|
+ if (typeof element.dispatchEvent == "function") {
|
|
|
+ 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 (isClickOnCheckableElement(element, eventType))
|
|
|
+ element.checked = element.checked !== true;
|
|
|
+ element.fireEvent("on" + eventType);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ throw new Error("Browser doesn't support triggering events");
|
|
|
+ },
|
|
|
+
|
|
|
+ unwrapObservable: function (value) {
|
|
|
+ return ko.isObservable(value) ? value() : value;
|
|
|
+ },
|
|
|
+
|
|
|
+ peekObservable: function (value) {
|
|
|
+ return ko.isObservable(value) ? value.peek() : value;
|
|
|
+ },
|
|
|
+
|
|
|
+ toggleDomNodeCssClass: function (node, classNames, shouldHaveClass) {
|
|
|
+ if (classNames) {
|
|
|
+ var cssClassNameRegex = /[\w-]+/g,
|
|
|
+ currentClassNames = node.className.match(cssClassNameRegex) || [];
|
|
|
+ ko.utils.arrayForEach(classNames.match(cssClassNameRegex), function(className) {
|
|
|
+ var indexOfClass = ko.utils.arrayIndexOf(currentClassNames, className);
|
|
|
+ if (indexOfClass >= 0) {
|
|
|
+ if (!shouldHaveClass)
|
|
|
+ currentClassNames.splice(indexOfClass, 1);
|
|
|
+ } else {
|
|
|
+ if (shouldHaveClass)
|
|
|
+ currentClassNames.push(className);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ node.className = currentClassNames.join(" ");
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ setTextContent: function(element, textContent) {
|
|
|
+ var value = ko.utils.unwrapObservable(textContent);
|
|
|
+ if ((value === null) || (value === undefined))
|
|
|
+ value = "";
|
|
|
+
|
|
|
+ if (element.nodeType === 3) {
|
|
|
+ element.data = value;
|
|
|
+ } else {
|
|
|
+ // We need there to be exactly one child: a text node.
|
|
|
+ // If there are no children, more than one, or if it's not a text node,
|
|
|
+ // we'll clear everything and create a single text node.
|
|
|
+ var innerTextNode = ko.virtualElements.firstChild(element);
|
|
|
+ if (!innerTextNode || innerTextNode.nodeType != 3 || ko.virtualElements.nextSibling(innerTextNode)) {
|
|
|
+ ko.virtualElements.setDomNodeChildren(element, [document.createTextNode(value)]);
|
|
|
+ } else {
|
|
|
+ innerTextNode.data = value;
|
|
|
+ }
|
|
|
+
|
|
|
+ ko.utils.forceRefresh(element);
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ setElementName: function(element, name) {
|
|
|
+ element.name = name;
|
|
|
+
|
|
|
+ // 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 (ieVersion <= 7) {
|
|
|
+ try {
|
|
|
+ element.mergeAttributes(document.createElement("<input name='" + element.name + "'/>"), false);
|
|
|
+ }
|
|
|
+ catch(e) {} // For IE9 with doc mode "IE9 Standards" and browser mode "IE9 Compatibility View"
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ forceRefresh: function(node) {
|
|
|
+ // Workaround for an IE9 rendering bug - https://github.com/SteveSanderson/knockout/issues/209
|
|
|
+ if (ieVersion >= 9) {
|
|
|
+ // For text nodes and comment nodes (most likely virtual elements), we will have to refresh the container
|
|
|
+ var elem = node.nodeType == 1 ? node : node.parentNode;
|
|
|
+ if (elem.style)
|
|
|
+ elem.style.zoom = elem.style.zoom;
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ ensureSelectElementIsRenderedCorrectly: function(selectElement) {
|
|
|
+ // Workaround for IE9 rendering bug - it doesn't reliably display all the text in dynamically-added select boxes unless you force it to re-render by updating the width.
|
|
|
+ // (See https://github.com/SteveSanderson/knockout/issues/312, http://stackoverflow.com/questions/5908494/select-only-shows-first-char-of-selected-option)
|
|
|
+ if (ieVersion >= 9) {
|
|
|
+ var originalWidth = selectElement.style.width;
|
|
|
+ selectElement.style.width = 0;
|
|
|
+ selectElement.style.width = originalWidth;
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ range: function (min, max) {
|
|
|
+ min = ko.utils.unwrapObservable(min);
|
|
|
+ max = ko.utils.unwrapObservable(max);
|
|
|
+ var result = [];
|
|
|
+ for (var i = min; i <= max; i++)
|
|
|
+ result.push(i);
|
|
|
+ return result;
|
|
|
+ },
|
|
|
+
|
|
|
+ makeArray: function(arrayLikeObject) {
|
|
|
+ var result = [];
|
|
|
+ for (var i = 0, j = arrayLikeObject.length; i < j; i++) {
|
|
|
+ result.push(arrayLikeObject[i]);
|
|
|
+ };
|
|
|
+ return result;
|
|
|
+ },
|
|
|
+
|
|
|
+ isIe6 : isIe6,
|
|
|
+ isIe7 : isIe7,
|
|
|
+ ieVersion : ieVersion,
|
|
|
+
|
|
|
+ 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
|
|
|
+ var matches = [];
|
|
|
+ for (var i = fields.length - 1; i >= 0; i--) {
|
|
|
+ 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, replacer, space) { // replacer and space are optional
|
|
|
+ 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), replacer, space);
|
|
|
+ },
|
|
|
+
|
|
|
+ postJson: function (urlOrForm, data, options) {
|
|
|
+ options = options || {};
|
|
|
+ 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') && (ko.utils.tagNameLower(urlOrForm) === "form")) {
|
|
|
+ var originalForm = urlOrForm;
|
|
|
+ url = originalForm.action;
|
|
|
+ for (var i = includeFields.length - 1; i >= 0; i--) {
|
|
|
+ var fields = ko.utils.getFormFields(originalForm, includeFields[i]);
|
|
|
+ for (var j = fields.length - 1; j >= 0; j--)
|
|
|
+ params[fields[j].name] = fields[j].value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ data = ko.utils.unwrapObservable(data);
|
|
|
+ var form = document.createElement("form");
|
|
|
+ form.style.display = "none";
|
|
|
+ form.action = url;
|
|
|
+ form.method = "post";
|
|
|
+ for (var key in data) {
|
|
|
+ var input = document.createElement("input");
|
|
|
+ input.name = key;
|
|
|
+ input.value = ko.utils.stringifyJson(ko.utils.unwrapObservable(data[key]));
|
|
|
+ form.appendChild(input);
|
|
|
+ }
|
|
|
+ for (var key in params) {
|
|
|
+ var input = document.createElement("input");
|
|
|
+ input.name = key;
|
|
|
+ input.value = params[key];
|
|
|
+ form.appendChild(input);
|
|
|
+ }
|
|
|
+ document.body.appendChild(form);
|
|
|
+ options['submitter'] ? options['submitter'](form) : form.submit();
|
|
|
+ setTimeout(function () { form.parentNode.removeChild(form); }, 0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+})();
|
|
|
+
|
|
|
+ko.exportSymbol('utils', ko.utils);
|
|
|
+ko.exportSymbol('utils.arrayForEach', ko.utils.arrayForEach);
|
|
|
+ko.exportSymbol('utils.arrayFirst', ko.utils.arrayFirst);
|
|
|
+ko.exportSymbol('utils.arrayFilter', ko.utils.arrayFilter);
|
|
|
+ko.exportSymbol('utils.arrayGetDistinctValues', ko.utils.arrayGetDistinctValues);
|
|
|
+ko.exportSymbol('utils.arrayIndexOf', ko.utils.arrayIndexOf);
|
|
|
+ko.exportSymbol('utils.arrayMap', ko.utils.arrayMap);
|
|
|
+ko.exportSymbol('utils.arrayPushAll', ko.utils.arrayPushAll);
|
|
|
+ko.exportSymbol('utils.arrayRemoveItem', ko.utils.arrayRemoveItem);
|
|
|
+ko.exportSymbol('utils.extend', ko.utils.extend);
|
|
|
+ko.exportSymbol('utils.fieldsIncludedWithJsonPost', ko.utils.fieldsIncludedWithJsonPost);
|
|
|
+ko.exportSymbol('utils.getFormFields', ko.utils.getFormFields);
|
|
|
+ko.exportSymbol('utils.peekObservable', ko.utils.peekObservable);
|
|
|
+ko.exportSymbol('utils.postJson', ko.utils.postJson);
|
|
|
+ko.exportSymbol('utils.parseJson', ko.utils.parseJson);
|
|
|
+ko.exportSymbol('utils.registerEventHandler', ko.utils.registerEventHandler);
|
|
|
+ko.exportSymbol('utils.stringifyJson', ko.utils.stringifyJson);
|
|
|
+ko.exportSymbol('utils.range', ko.utils.range);
|
|
|
+ko.exportSymbol('utils.toggleDomNodeCssClass', ko.utils.toggleDomNodeCssClass);
|
|
|
+ko.exportSymbol('utils.triggerEvent', ko.utils.triggerEvent);
|
|
|
+ko.exportSymbol('utils.unwrapObservable', ko.utils.unwrapObservable);
|
|
|
+
|
|
|
+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) {
|
|
|
+ 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)));
|
|
|
+ };
|
|
|
+ };
|
|
|
+}
|
|
|
+
|
|
|
+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") && dataStore[dataStoreKey];
|
|
|
+ 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;
|
|
|
+ return true; // Exposing "did clean" flag purely so specs can infer whether things have been cleaned up as intended
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+})();
|
|
|
+
|
|
|
+ko.exportSymbol('utils.domData', ko.utils.domData);
|
|
|
+ko.exportSymbol('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();
|
|
|
+ var cleanableNodeTypes = { 1: true, 8: true, 9: true }; // Element, Comment, Document
|
|
|
+ var cleanableNodeTypesWithDescendants = { 1: true, 9: true }; // Element, Document
|
|
|
+
|
|
|
+ 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]);
|
|
|
+
|
|
|
+ // Also clear any immediate-child comment nodes, as these wouldn't have been found by
|
|
|
+ // node.getElementsByTagName("*") in cleanNode() (comment nodes aren't elements)
|
|
|
+ if (cleanableNodeTypesWithDescendants[node.nodeType])
|
|
|
+ cleanImmediateCommentTypeChildren(node);
|
|
|
+ }
|
|
|
+
|
|
|
+ function cleanImmediateCommentTypeChildren(nodeWithChildren) {
|
|
|
+ var child, nextChild = nodeWithChildren.firstChild;
|
|
|
+ while (child = nextChild) {
|
|
|
+ nextChild = child.nextSibling;
|
|
|
+ if (child.nodeType === 8)
|
|
|
+ cleanSingleNode(child);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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) {
|
|
|
+ // First clean this node, where applicable
|
|
|
+ if (cleanableNodeTypes[node.nodeType]) {
|
|
|
+ cleanSingleNode(node);
|
|
|
+
|
|
|
+ // ... then its descendants, where applicable
|
|
|
+ if (cleanableNodeTypesWithDescendants[node.nodeType]) {
|
|
|
+ // 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]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return node;
|
|
|
+ },
|
|
|
+
|
|
|
+ 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('cleanNode', ko.cleanNode);
|
|
|
+ko.exportSymbol('removeNode', ko.removeNode);
|
|
|
+ko.exportSymbol('utils.domNodeDisposal', ko.utils.domNodeDisposal);
|
|
|
+ko.exportSymbol('utils.domNodeDisposal.addDisposeCallback', ko.utils.domNodeDisposal.addDisposeCallback);
|
|
|
+ko.exportSymbol('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) {
|
|
|
+ // jQuery's "parseHTML" function was introduced in jQuery 1.8.0 and is a documented public API.
|
|
|
+ if (jQuery['parseHTML']) {
|
|
|
+ return jQuery['parseHTML'](html);
|
|
|
+ } else {
|
|
|
+ // For jQuery < 1.8.0, we fall back on the undocumented internal "clean" function.
|
|
|
+ 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);
|
|
|
+
|
|
|
+ // There's no legitimate reason to display a stringified observable without unwrapping it, so we'll unwrap it
|
|
|
+ html = ko.utils.unwrapObservable(html);
|
|
|
+
|
|
|
+ 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('utils.parseHtmlFragment', ko.utils.parseHtmlFragment);
|
|
|
+ko.exportSymbol('utils.setHtml', ko.utils.setHtml);
|
|
|
+
|
|
|
+ko.memoization = (function () {
|
|
|
+ var memos = {};
|
|
|
+
|
|
|
+ function randomMax8HexChars() {
|
|
|
+ return (((1 + Math.random()) * 0x100000000) | 0).toString(16).substring(1);
|
|
|
+ }
|
|
|
+ function generateRandomId() {
|
|
|
+ return randomMax8HexChars() + randomMax8HexChars();
|
|
|
+ }
|
|
|
+ function findMemoNodes(rootNode, appendToArray) {
|
|
|
+ if (!rootNode)
|
|
|
+ return;
|
|
|
+ if (rootNode.nodeType == 8) {
|
|
|
+ var memoId = ko.memoization.parseMemoText(rootNode.nodeValue);
|
|
|
+ if (memoId != null)
|
|
|
+ appendToArray.push({ domNode: rootNode, memoId: memoId });
|
|
|
+ } else if (rootNode.nodeType == 1) {
|
|
|
+ for (var i = 0, childNodes = rootNode.childNodes, j = childNodes.length; i < j; i++)
|
|
|
+ findMemoNodes(childNodes[i], appendToArray);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return {
|
|
|
+ memoize: function (callback) {
|
|
|
+ if (typeof callback != "function")
|
|
|
+ throw new Error("You can only pass a function to ko.memoization.memoize()");
|
|
|
+ var memoId = generateRandomId();
|
|
|
+ memos[memoId] = callback;
|
|
|
+ return "<!--[ko_memo:" + memoId + "]-->";
|
|
|
+ },
|
|
|
+
|
|
|
+ unmemoize: function (memoId, callbackParams) {
|
|
|
+ var callback = memos[memoId];
|
|
|
+ if (callback === undefined)
|
|
|
+ throw new Error("Couldn't find any memo with ID " + memoId + ". Perhaps it's already been unmemoized.");
|
|
|
+ try {
|
|
|
+ callback.apply(null, callbackParams || []);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ finally { delete memos[memoId]; }
|
|
|
+ },
|
|
|
+
|
|
|
+ unmemoizeDomNodeAndDescendants: function (domNode, extraCallbackParamsArray) {
|
|
|
+ var memos = [];
|
|
|
+ findMemoNodes(domNode, memos);
|
|
|
+ for (var i = 0, j = memos.length; i < j; i++) {
|
|
|
+ var node = memos[i].domNode;
|
|
|
+ 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)
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ parseMemoText: function (memoText) {
|
|
|
+ var match = memoText.match(/^\[ko_memo\:(.*?)\]$/);
|
|
|
+ return match ? match[1] : null;
|
|
|
+ }
|
|
|
+ };
|
|
|
+})();
|
|
|
+
|
|
|
+ko.exportSymbol('memoization', ko.memoization);
|
|
|
+ko.exportSymbol('memoization.memoize', ko.memoization.memoize);
|
|
|
+ko.exportSymbol('memoization.unmemoize', ko.memoization.unmemoize);
|
|
|
+ko.exportSymbol('memoization.parseMemoText', ko.memoization.parseMemoText);
|
|
|
+ko.exportSymbol('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('extenders', ko.extenders);
|
|
|
+
|
|
|
+ko.subscription = function (target, callback, disposeCallback) {
|
|
|
+ this.target = target;
|
|
|
+ this.callback = callback;
|
|
|
+ this.disposeCallback = disposeCallback;
|
|
|
+ ko.exportProperty(this, 'dispose', this.dispose);
|
|
|
+};
|
|
|
+ko.subscription.prototype.dispose = function () {
|
|
|
+ this.isDisposed = true;
|
|
|
+ this.disposeCallback();
|
|
|
+};
|
|
|
+
|
|
|
+ko.subscribable = function () {
|
|
|
+ this._subscriptions = {};
|
|
|
+
|
|
|
+ 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(this, boundCallback, function () {
|
|
|
+ ko.utils.arrayRemoveItem(this._subscriptions[event], subscription);
|
|
|
+ }.bind(this));
|
|
|
+
|
|
|
+ if (!this._subscriptions[event])
|
|
|
+ this._subscriptions[event] = [];
|
|
|
+ this._subscriptions[event].push(subscription);
|
|
|
+ return subscription;
|
|
|
+ },
|
|
|
+
|
|
|
+ "notifySubscribers": function (valueToNotify, event) {
|
|
|
+ event = event || defaultEvent;
|
|
|
+ if (this._subscriptions[event]) {
|
|
|
+ ko.dependencyDetection.ignore(function() {
|
|
|
+ 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 () {
|
|
|
+ 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";
|
|
|
+};
|
|
|
+
|
|
|
+ko.exportSymbol('subscribable', ko.subscribable);
|
|
|
+ko.exportSymbol('isSubscribable', ko.isSubscribable);
|
|
|
+
|
|
|
+ko.dependencyDetection = (function () {
|
|
|
+ var _frames = [];
|
|
|
+
|
|
|
+ return {
|
|
|
+ begin: function (callback) {
|
|
|
+ _frames.push({ callback: callback, distinctDependencies:[] });
|
|
|
+ },
|
|
|
+
|
|
|
+ end: function () {
|
|
|
+ _frames.pop();
|
|
|
+ },
|
|
|
+
|
|
|
+ registerDependency: function (subscribable) {
|
|
|
+ if (!ko.isSubscribable(subscribable))
|
|
|
+ throw new Error("Only subscribable things can act as dependencies");
|
|
|
+ if (_frames.length > 0) {
|
|
|
+ var topFrame = _frames[_frames.length - 1];
|
|
|
+ if (!topFrame || ko.utils.arrayIndexOf(topFrame.distinctDependencies, subscribable) >= 0)
|
|
|
+ return;
|
|
|
+ topFrame.distinctDependencies.push(subscribable);
|
|
|
+ topFrame.callback(subscribable);
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ ignore: function(callback, callbackTarget, callbackArgs) {
|
|
|
+ try {
|
|
|
+ _frames.push(null);
|
|
|
+ return callback.apply(callbackTarget, callbackArgs || []);
|
|
|
+ } finally {
|
|
|
+ _frames.pop();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+})();
|
|
|
+var primitiveTypes = { 'undefined':true, 'boolean':true, 'number':true, 'string':true };
|
|
|
+
|
|
|
+ko.observable = function (initialValue) {
|
|
|
+ var _latestValue = initialValue;
|
|
|
+
|
|
|
+ function observable() {
|
|
|
+ if (arguments.length > 0) {
|
|
|
+ // Write
|
|
|
+
|
|
|
+ // Ignore writes if the value hasn't changed
|
|
|
+ if ((!observable['equalityComparer']) || !observable['equalityComparer'](_latestValue, arguments[0])) {
|
|
|
+ observable.valueWillMutate();
|
|
|
+ _latestValue = arguments[0];
|
|
|
+ if (DEBUG) observable._latestValue = _latestValue;
|
|
|
+ observable.valueHasMutated();
|
|
|
+ }
|
|
|
+ return this; // Permits chained assignments
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // Read
|
|
|
+ ko.dependencyDetection.registerDependency(observable); // The caller only needs to be notified of changes if they did a "read" operation
|
|
|
+ return _latestValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (DEBUG) observable._latestValue = _latestValue;
|
|
|
+ ko.subscribable.call(observable);
|
|
|
+ observable.peek = function() { return _latestValue };
|
|
|
+ observable.valueHasMutated = function () { observable["notifySubscribers"](_latestValue); }
|
|
|
+ observable.valueWillMutate = function () { observable["notifySubscribers"](_latestValue, "beforeChange"); }
|
|
|
+ ko.utils.extend(observable, ko.observable['fn']);
|
|
|
+
|
|
|
+ ko.exportProperty(observable, 'peek', observable.peek);
|
|
|
+ ko.exportProperty(observable, "valueHasMutated", observable.valueHasMutated);
|
|
|
+ ko.exportProperty(observable, "valueWillMutate", observable.valueWillMutate);
|
|
|
+
|
|
|
+ return observable;
|
|
|
+}
|
|
|
+
|
|
|
+ko.observable['fn'] = {
|
|
|
+ "equalityComparer": function valuesArePrimitiveAndEqual(a, b) {
|
|
|
+ var oldValueIsPrimitive = (a === null) || (typeof(a) in primitiveTypes);
|
|
|
+ return oldValueIsPrimitive ? (a === b) : false;
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+var protoProperty = ko.observable.protoProperty = "__ko_proto__";
|
|
|
+ko.observable['fn'][protoProperty] = ko.observable;
|
|
|
+
|
|
|
+ko.hasPrototype = function(instance, prototype) {
|
|
|
+ if ((instance === null) || (instance === undefined) || (instance[protoProperty] === undefined)) return false;
|
|
|
+ if (instance[protoProperty] === prototype) return true;
|
|
|
+ return ko.hasPrototype(instance[protoProperty], prototype); // Walk the prototype chain
|
|
|
+};
|
|
|
+
|
|
|
+ko.isObservable = function (instance) {
|
|
|
+ return ko.hasPrototype(instance, ko.observable);
|
|
|
+}
|
|
|
+ko.isWriteableObservable = function (instance) {
|
|
|
+ // Observable
|
|
|
+ if ((typeof instance == "function") && instance[protoProperty] === ko.observable)
|
|
|
+ return true;
|
|
|
+ // Writeable dependent observable
|
|
|
+ if ((typeof instance == "function") && (instance[protoProperty] === ko.dependentObservable) && (instance.hasWriteFunction))
|
|
|
+ return true;
|
|
|
+ // Anything else
|
|
|
+ return false;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ko.exportSymbol('observable', ko.observable);
|
|
|
+ko.exportSymbol('isObservable', ko.isObservable);
|
|
|
+ko.exportSymbol('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 = ko.observable(initialValues);
|
|
|
+ ko.utils.extend(result, ko.observableArray['fn']);
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+ko.observableArray['fn'] = {
|
|
|
+ 'remove': function (valueOrPredicate) {
|
|
|
+ var underlyingArray = this.peek();
|
|
|
+ var removedValues = [];
|
|
|
+ var predicate = typeof valueOrPredicate == "function" ? valueOrPredicate : function (value) { return value === valueOrPredicate; };
|
|
|
+ for (var i = 0; i < underlyingArray.length; i++) {
|
|
|
+ var value = underlyingArray[i];
|
|
|
+ if (predicate(value)) {
|
|
|
+ if (removedValues.length === 0) {
|
|
|
+ this.valueWillMutate();
|
|
|
+ }
|
|
|
+ removedValues.push(value);
|
|
|
+ underlyingArray.splice(i, 1);
|
|
|
+ i--;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (removedValues.length) {
|
|
|
+ this.valueHasMutated();
|
|
|
+ }
|
|
|
+ return removedValues;
|
|
|
+ },
|
|
|
+
|
|
|
+ 'removeAll': function (arrayOfValues) {
|
|
|
+ // If you passed zero args, we remove everything
|
|
|
+ if (arrayOfValues === undefined) {
|
|
|
+ var underlyingArray = this.peek();
|
|
|
+ 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 this['remove'](function (value) {
|
|
|
+ return ko.utils.arrayIndexOf(arrayOfValues, value) >= 0;
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ 'destroy': function (valueOrPredicate) {
|
|
|
+ var underlyingArray = this.peek();
|
|
|
+ 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 this['destroy'](function (value) {
|
|
|
+ return ko.utils.arrayIndexOf(arrayOfValues, value) >= 0;
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ '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.peek()[index] = newItem;
|
|
|
+ this.valueHasMutated();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// Populate ko.observableArray.fn with read/write functions from native arrays
|
|
|
+// Important: Do not add any additional functions here that may reasonably be used to *read* data from the array
|
|
|
+// because we'll eval them without causing subscriptions, so ko.computed output could end up getting stale
|
|
|
+ko.utils.arrayForEach(["pop", "push", "reverse", "shift", "sort", "splice", "unshift"], function (methodName) {
|
|
|
+ ko.observableArray['fn'][methodName] = function () {
|
|
|
+ // Use "peek" to avoid creating a subscription in any computed that we're executing in the context of
|
|
|
+ // (for consistency with mutating regular observables)
|
|
|
+ var underlyingArray = this.peek();
|
|
|
+ this.valueWillMutate();
|
|
|
+ var methodCallResult = underlyingArray[methodName].apply(underlyingArray, arguments);
|
|
|
+ this.valueHasMutated();
|
|
|
+ return methodCallResult;
|
|
|
+ };
|
|
|
+});
|
|
|
+
|
|
|
+// 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.exportSymbol('observableArray', ko.observableArray);
|
|
|
+ko.dependentObservable = function (evaluatorFunctionOrOptions, evaluatorFunctionTarget, options) {
|
|
|
+ var _latestValue,
|
|
|
+ _hasBeenEvaluated = false,
|
|
|
+ _isBeingEvaluated = false,
|
|
|
+ readFunction = evaluatorFunctionOrOptions;
|
|
|
+
|
|
|
+ if (readFunction && typeof readFunction == "object") {
|
|
|
+ // Single-parameter syntax - everything is on this "options" param
|
|
|
+ options = readFunction;
|
|
|
+ readFunction = options["read"];
|
|
|
+ } else {
|
|
|
+ // Multi-parameter syntax - construct the options according to the params passed
|
|
|
+ options = options || {};
|
|
|
+ if (!readFunction)
|
|
|
+ readFunction = options["read"];
|
|
|
+ }
|
|
|
+ if (typeof readFunction != "function")
|
|
|
+ throw new Error("Pass a function that returns the value of the ko.computed");
|
|
|
+
|
|
|
+ function addSubscriptionToDependency(subscribable) {
|
|
|
+ _subscriptionsToDependencies.push(subscribable.subscribe(evaluatePossiblyAsync));
|
|
|
+ }
|
|
|
+
|
|
|
+ function disposeAllSubscriptionsToDependencies() {
|
|
|
+ ko.utils.arrayForEach(_subscriptionsToDependencies, function (subscription) {
|
|
|
+ subscription.dispose();
|
|
|
+ });
|
|
|
+ _subscriptionsToDependencies = [];
|
|
|
+ }
|
|
|
+
|
|
|
+ function evaluatePossiblyAsync() {
|
|
|
+ var throttleEvaluationTimeout = dependentObservable['throttleEvaluation'];
|
|
|
+ if (throttleEvaluationTimeout && throttleEvaluationTimeout >= 0) {
|
|
|
+ clearTimeout(evaluationTimeoutInstance);
|
|
|
+ evaluationTimeoutInstance = setTimeout(evaluateImmediate, throttleEvaluationTimeout);
|
|
|
+ } else
|
|
|
+ evaluateImmediate();
|
|
|
+ }
|
|
|
+
|
|
|
+ function evaluateImmediate() {
|
|
|
+ if (_isBeingEvaluated) {
|
|
|
+ // If the evaluation of a ko.computed causes side effects, it's possible that it will trigger its own re-evaluation.
|
|
|
+ // This is not desirable (it's hard for a developer to realise a chain of dependencies might cause this, and they almost
|
|
|
+ // certainly didn't intend infinite re-evaluations). So, for predictability, we simply prevent ko.computeds from causing
|
|
|
+ // their own re-evaluation. Further discussion at https://github.com/SteveSanderson/knockout/pull/387
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 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 && disposeWhen()) {
|
|
|
+ dispose();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ _isBeingEvaluated = true;
|
|
|
+ try {
|
|
|
+ // Initially, we assume that none of the subscriptions are still being used (i.e., all are candidates for disposal).
|
|
|
+ // Then, during evaluation, we cross off any that are in fact still being used.
|
|
|
+ var disposalCandidates = ko.utils.arrayMap(_subscriptionsToDependencies, function(item) {return item.target;});
|
|
|
+
|
|
|
+ ko.dependencyDetection.begin(function(subscribable) {
|
|
|
+ var inOld;
|
|
|
+ if ((inOld = ko.utils.arrayIndexOf(disposalCandidates, subscribable)) >= 0)
|
|
|
+ disposalCandidates[inOld] = undefined; // Don't want to dispose this subscription, as it's still being used
|
|
|
+ else
|
|
|
+ addSubscriptionToDependency(subscribable); // Brand new subscription - add it
|
|
|
+ });
|
|
|
+
|
|
|
+ var newValue = readFunction.call(evaluatorFunctionTarget);
|
|
|
+
|
|
|
+ // For each subscription no longer being used, remove it from the active subscriptions list and dispose it
|
|
|
+ for (var i = disposalCandidates.length - 1; i >= 0; i--) {
|
|
|
+ if (disposalCandidates[i])
|
|
|
+ _subscriptionsToDependencies.splice(i, 1)[0].dispose();
|
|
|
+ }
|
|
|
+ _hasBeenEvaluated = true;
|
|
|
+
|
|
|
+ dependentObservable["notifySubscribers"](_latestValue, "beforeChange");
|
|
|
+ _latestValue = newValue;
|
|
|
+ if (DEBUG) dependentObservable._latestValue = _latestValue;
|
|
|
+ } finally {
|
|
|
+ ko.dependencyDetection.end();
|
|
|
+ }
|
|
|
+
|
|
|
+ dependentObservable["notifySubscribers"](_latestValue);
|
|
|
+ _isBeingEvaluated = false;
|
|
|
+ if (!_subscriptionsToDependencies.length)
|
|
|
+ dispose();
|
|
|
+ }
|
|
|
+
|
|
|
+ function dependentObservable() {
|
|
|
+ if (arguments.length > 0) {
|
|
|
+ if (typeof writeFunction === "function") {
|
|
|
+ // Writing a value
|
|
|
+ writeFunction.apply(evaluatorFunctionTarget, arguments);
|
|
|
+ } else {
|
|
|
+ throw new Error("Cannot write a value to a ko.computed unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters.");
|
|
|
+ }
|
|
|
+ return this; // Permits chained assignments
|
|
|
+ } else {
|
|
|
+ // Reading the value
|
|
|
+ if (!_hasBeenEvaluated)
|
|
|
+ evaluateImmediate();
|
|
|
+ ko.dependencyDetection.registerDependency(dependentObservable);
|
|
|
+ return _latestValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function peek() {
|
|
|
+ if (!_hasBeenEvaluated)
|
|
|
+ evaluateImmediate();
|
|
|
+ return _latestValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ function isActive() {
|
|
|
+ return !_hasBeenEvaluated || _subscriptionsToDependencies.length > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // By here, "options" is always non-null
|
|
|
+ var writeFunction = options["write"],
|
|
|
+ disposeWhenNodeIsRemoved = options["disposeWhenNodeIsRemoved"] || options.disposeWhenNodeIsRemoved || null,
|
|
|
+ disposeWhen = options["disposeWhen"] || options.disposeWhen || function() { return false; },
|
|
|
+ dispose = disposeAllSubscriptionsToDependencies,
|
|
|
+ _subscriptionsToDependencies = [],
|
|
|
+ evaluationTimeoutInstance = null;
|
|
|
+
|
|
|
+ if (!evaluatorFunctionTarget)
|
|
|
+ evaluatorFunctionTarget = options["owner"];
|
|
|
+
|
|
|
+ dependentObservable.peek = peek;
|
|
|
+ dependentObservable.getDependenciesCount = function () { return _subscriptionsToDependencies.length; };
|
|
|
+ dependentObservable.hasWriteFunction = typeof options["write"] === "function";
|
|
|
+ dependentObservable.dispose = function () { dispose(); };
|
|
|
+ dependentObservable.isActive = isActive;
|
|
|
+
|
|
|
+ ko.subscribable.call(dependentObservable);
|
|
|
+ ko.utils.extend(dependentObservable, ko.dependentObservable['fn']);
|
|
|
+
|
|
|
+ ko.exportProperty(dependentObservable, 'peek', dependentObservable.peek);
|
|
|
+ ko.exportProperty(dependentObservable, 'dispose', dependentObservable.dispose);
|
|
|
+ ko.exportProperty(dependentObservable, 'isActive', dependentObservable.isActive);
|
|
|
+ ko.exportProperty(dependentObservable, 'getDependenciesCount', dependentObservable.getDependenciesCount);
|
|
|
+
|
|
|
+ // Evaluate, unless deferEvaluation is true
|
|
|
+ if (options['deferEvaluation'] !== true)
|
|
|
+ evaluateImmediate();
|
|
|
+
|
|
|
+ // Build "disposeWhenNodeIsRemoved" and "disposeWhenNodeIsRemovedCallback" option values.
|
|
|
+ // But skip if isActive is false (there will never be any dependencies to dispose).
|
|
|
+ // (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.)
|
|
|
+ if (disposeWhenNodeIsRemoved && isActive()) {
|
|
|
+ dispose = function() {
|
|
|
+ ko.utils.domNodeDisposal.removeDisposeCallback(disposeWhenNodeIsRemoved, arguments.callee);
|
|
|
+ disposeAllSubscriptionsToDependencies();
|
|
|
+ };
|
|
|
+ ko.utils.domNodeDisposal.addDisposeCallback(disposeWhenNodeIsRemoved, dispose);
|
|
|
+ var existingDisposeWhenFunction = disposeWhen;
|
|
|
+ disposeWhen = function () {
|
|
|
+ return !ko.utils.domNodeIsAttachedToDocument(disposeWhenNodeIsRemoved) || existingDisposeWhenFunction();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return dependentObservable;
|
|
|
+};
|
|
|
+
|
|
|
+ko.isComputed = function(instance) {
|
|
|
+ return ko.hasPrototype(instance, ko.dependentObservable);
|
|
|
+};
|
|
|
+
|
|
|
+var protoProp = ko.observable.protoProperty; // == "__ko_proto__"
|
|
|
+ko.dependentObservable[protoProp] = ko.observable;
|
|
|
+
|
|
|
+ko.dependentObservable['fn'] = {};
|
|
|
+ko.dependentObservable['fn'][protoProp] = ko.dependentObservable;
|
|
|
+
|
|
|
+ko.exportSymbol('dependentObservable', ko.dependentObservable);
|
|
|
+ko.exportSymbol('computed', ko.dependentObservable); // Make "ko.computed" an alias for "ko.dependentObservable"
|
|
|
+ko.exportSymbol('isComputed', ko.isComputed);
|
|
|
+
|
|
|
+(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, replacer, space) { // replacer and space are optional
|
|
|
+ var plainJavaScriptObject = ko.toJS(rootObject);
|
|
|
+ return ko.utils.stringifyJson(plainJavaScriptObject, replacer, space);
|
|
|
+ };
|
|
|
+
|
|
|
+ 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);
|
|
|
+
|
|
|
+ // For arrays, also respect toJSON property for custom mappings (fixes #278)
|
|
|
+ if (typeof rootObject['toJSON'] == 'function')
|
|
|
+ visitorCallback('toJSON');
|
|
|
+ } 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('toJS', ko.toJS);
|
|
|
+ko.exportSymbol('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) {
|
|
|
+ switch (ko.utils.tagNameLower(element)) {
|
|
|
+ case 'option':
|
|
|
+ if (element[hasDomDataExpandoProperty] === true)
|
|
|
+ return ko.utils.domData.get(element, ko.bindingHandlers.options.optionValueDomDataKey);
|
|
|
+ return ko.utils.ieVersion <= 7
|
|
|
+ ? (element.getAttributeNode('value').specified ? element.value : element.text)
|
|
|
+ : element.value;
|
|
|
+ case 'select':
|
|
|
+ return element.selectedIndex >= 0 ? ko.selectExtensions.readValue(element.options[element.selectedIndex]) : undefined;
|
|
|
+ default:
|
|
|
+ return element.value;
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ writeValue: function(element, value) {
|
|
|
+ switch (ko.utils.tagNameLower(element)) {
|
|
|
+ case '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;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 'select':
|
|
|
+ for (var i = element.options.length - 1; i >= 0; i--) {
|
|
|
+ if (ko.selectExtensions.readValue(element.options[i]) == value) {
|
|
|
+ element.selectedIndex = i;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ if ((value === null) || (value === undefined))
|
|
|
+ value = "";
|
|
|
+ element.value = value;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+})();
|
|
|
+
|
|
|
+ko.exportSymbol('selectExtensions', ko.selectExtensions);
|
|
|
+ko.exportSymbol('selectExtensions.readValue', ko.selectExtensions.readValue);
|
|
|
+ko.exportSymbol('selectExtensions.writeValue', ko.selectExtensions.writeValue);
|
|
|
+ko.expressionRewriting = (function () {
|
|
|
+ var restoreCapturedTokensRegex = /\@ko_token_(\d+)\@/g;
|
|
|
+ var javaScriptReservedWords = ["true", "false"];
|
|
|
+
|
|
|
+ // Matches something that can be assigned to--either an isolated identifier or something ending with a property accessor
|
|
|
+ // This is designed to be simple and avoid false negatives, but could produce false positives (e.g., a+b.c).
|
|
|
+ var javaScriptAssignmentTarget = /^(?:[$_a-z][$\w]*|(.+)(\.\s*[$_a-z][$\w]*|\[.+\]))$/i;
|
|
|
+
|
|
|
+ function restoreTokens(string, tokens) {
|
|
|
+ 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 getWriteableValue(expression) {
|
|
|
+ if (ko.utils.arrayIndexOf(javaScriptReservedWords, ko.utils.stringTrim(expression).toLowerCase()) >= 0)
|
|
|
+ return false;
|
|
|
+ var match = expression.match(javaScriptAssignmentTarget);
|
|
|
+ return match === null ? false : match[1] ? ('Object(' + match[1] + ')' + match[2]) : expression;
|
|
|
+ }
|
|
|
+
|
|
|
+ function ensureQuoted(key) {
|
|
|
+ var trimmedKey = ko.utils.stringTrim(key);
|
|
|
+ switch (trimmedKey.length && trimmedKey.charAt(0)) {
|
|
|
+ case "'":
|
|
|
+ case '"':
|
|
|
+ return key;
|
|
|
+ default:
|
|
|
+ return "'" + trimmedKey + "'";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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
|
|
|
+
|
|
|
+ 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 = 0; position < str.length; position++) {
|
|
|
+ var c = str.charAt(position);
|
|
|
+ if (tokenStart === null) {
|
|
|
+ switch (c) {
|
|
|
+ case '"':
|
|
|
+ case "'":
|
|
|
+ case "/":
|
|
|
+ tokenStart = position;
|
|
|
+ tokenEndChar = c;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ } 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) + "@";
|
|
|
+ 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 = 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 = 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;
|
|
|
+ },
|
|
|
+
|
|
|
+ preProcessBindings: function (objectLiteralStringOrKeyValueArray) {
|
|
|
+ var keyValueArray = typeof objectLiteralStringOrKeyValueArray === "string"
|
|
|
+ ? ko.expressionRewriting.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 (val = getWriteableValue(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']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var combinedResult = resultStrings.join("");
|
|
|
+ if (propertyAccessorResultStrings.length > 0) {
|
|
|
+ var allPropertyAccessors = propertyAccessorResultStrings.join("");
|
|
|
+ combinedResult = combinedResult + ", '_ko_property_writers' : { " + allPropertyAccessors + " } ";
|
|
|
+ }
|
|
|
+
|
|
|
+ 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;
|
|
|
+ },
|
|
|
+
|
|
|
+ // Internal, private KO utility for updating model properties from within bindings
|
|
|
+ // property: If the property being updated is (or might be) an observable, pass it here
|
|
|
+ // If it turns out to be a writable observable, it will be written to directly
|
|
|
+ // allBindingsAccessor: All bindings in the current execution context.
|
|
|
+ // This will be searched for a '_ko_property_writers' property in case you're writing to a non-observable
|
|
|
+ // key: The key identifying the property to be written. Example: for { hasFocus: myValue }, write to 'myValue' by specifying the key 'hasFocus'
|
|
|
+ // value: The value to be written
|
|
|
+ // checkIfDifferent: If true, and if the property being written is a writable observable, the value will only be written if
|
|
|
+ // it is !== existing value on that writable observable
|
|
|
+ writeValueToProperty: function(property, allBindingsAccessor, key, value, checkIfDifferent) {
|
|
|
+ if (!property || !ko.isWriteableObservable(property)) {
|
|
|
+ var propWriters = allBindingsAccessor()['_ko_property_writers'];
|
|
|
+ if (propWriters && propWriters[key])
|
|
|
+ propWriters[key](value);
|
|
|
+ } else if (!checkIfDifferent || property.peek() !== value) {
|
|
|
+ property(value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+})();
|
|
|
+
|
|
|
+ko.exportSymbol('expressionRewriting', ko.expressionRewriting);
|
|
|
+ko.exportSymbol('expressionRewriting.bindingRewriteValidators', ko.expressionRewriting.bindingRewriteValidators);
|
|
|
+ko.exportSymbol('expressionRewriting.parseObjectLiteral', ko.expressionRewriting.parseObjectLiteral);
|
|
|
+ko.exportSymbol('expressionRewriting.preProcessBindings', ko.expressionRewriting.preProcessBindings);
|
|
|
+
|
|
|
+// For backward compatibility, define the following aliases. (Previously, these function names were misleading because
|
|
|
+// they referred to JSON specifically, even though they actually work with arbitrary JavaScript object literal expressions.)
|
|
|
+ko.exportSymbol('jsonExpressionRewriting', ko.expressionRewriting);
|
|
|
+ko.exportSymbol('jsonExpressionRewriting.insertPropertyAccessorsIntoJson', ko.expressionRewriting.preProcessBindings);(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\S]*))?\s*-->$/ : /^\s*ko(?:\s+(.+\s*\:[\s\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 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 (!insertAfterNode) {
|
|
|
+ ko.virtualElements.prepend(containerNode, nodeToInsert);
|
|
|
+ } else 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);
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ firstChild: function(node) {
|
|
|
+ if (!isStartComment(node))
|
|
|
+ return node.firstChild;
|
|
|
+ if (!node.nextSibling || isEndComment(node.nextSibling))
|
|
|
+ return null;
|
|
|
+ return node.nextSibling;
|
|
|
+ },
|
|
|
+
|
|
|
+ nextSibling: function(node) {
|
|
|
+ if (isStartComment(node))
|
|
|
+ node = getMatchingEndComment(node);
|
|
|
+ if (node.nextSibling && isEndComment(node.nextSibling))
|
|
|
+ return null;
|
|
|
+ return node.nextSibling;
|
|
|
+ },
|
|
|
+
|
|
|
+ virtualNodeBindingValue: function(node) {
|
|
|
+ var regexMatch = isStartComment(node);
|
|
|
+ return regexMatch ? regexMatch[1] : null;
|
|
|
+ },
|
|
|
+
|
|
|
+ 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[ko.utils.tagNameLower(elementVerified)])
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+})();
|
|
|
+ko.exportSymbol('virtualElements', ko.virtualElements);
|
|
|
+ko.exportSymbol('virtualElements.allowedBindings', ko.virtualElements.allowedBindings);
|
|
|
+ko.exportSymbol('virtualElements.emptyNode', ko.virtualElements.emptyNode);
|
|
|
+//ko.exportSymbol('virtualElements.firstChild', ko.virtualElements.firstChild); // firstChild is not minified
|
|
|
+ko.exportSymbol('virtualElements.insertAfter', ko.virtualElements.insertAfter);
|
|
|
+//ko.exportSymbol('virtualElements.nextSibling', ko.virtualElements.nextSibling); // nextSibling is not minified
|
|
|
+ko.exportSymbol('virtualElements.prepend', ko.virtualElements.prepend);
|
|
|
+ko.exportSymbol('virtualElements.setDomNodeChildren', ko.virtualElements.setDomNodeChildren);
|
|
|
+(function() {
|
|
|
+ var defaultBindingAttributeName = "data-bind";
|
|
|
+
|
|
|
+ ko.bindingProvider = function() {
|
|
|
+ this.bindingCache = {};
|
|
|
+ };
|
|
|
+
|
|
|
+ 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, node) : 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, node) {
|
|
|
+ try {
|
|
|
+ var bindingFunction = createBindingsStringEvaluatorViaCache(bindingsString, this.bindingCache);
|
|
|
+ return bindingFunction(bindingContext, node);
|
|
|
+ } catch (ex) {
|
|
|
+ throw new Error("Unable to parse bindings.\nMessage: " + ex + ";\nBindings value: " + bindingsString);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ ko.bindingProvider['instance'] = new ko.bindingProvider();
|
|
|
+
|
|
|
+ function createBindingsStringEvaluatorViaCache(bindingsString, cache) {
|
|
|
+ var cacheKey = bindingsString;
|
|
|
+ return cache[cacheKey]
|
|
|
+ || (cache[cacheKey] = createBindingsStringEvaluator(bindingsString));
|
|
|
+ }
|
|
|
+
|
|
|
+ function createBindingsStringEvaluator(bindingsString) {
|
|
|
+ // Build the source for a function that evaluates "expression"
|
|
|
+ // For each scope variable, add an extra level of "with" nesting
|
|
|
+ // Example result: with(sc1) { with(sc0) { return (expression) } }
|
|
|
+ var rewrittenBindings = ko.expressionRewriting.preProcessBindings(bindingsString),
|
|
|
+ functionBody = "with($context){with($data||{}){return{" + rewrittenBindings + "}}}";
|
|
|
+ return new Function("$context", "$element", functionBody);
|
|
|
+ }
|
|
|
+})();
|
|
|
+
|
|
|
+ko.exportSymbol('bindingProvider', ko.bindingProvider);
|
|
|
+(function () {
|
|
|
+ ko.bindingHandlers = {};
|
|
|
+
|
|
|
+ ko.bindingContext = function(dataItem, parentBindingContext, dataItemAlias) {
|
|
|
+ if (parentBindingContext) {
|
|
|
+ ko.utils.extend(this, parentBindingContext); // Inherit $root and any custom properties
|
|
|
+ this['$parentContext'] = parentBindingContext;
|
|
|
+ this['$parent'] = parentBindingContext['$data'];
|
|
|
+ this['$parents'] = (parentBindingContext['$parents'] || []).slice(0);
|
|
|
+ this['$parents'].unshift(this['$parent']);
|
|
|
+ } else {
|
|
|
+ this['$parents'] = [];
|
|
|
+ this['$root'] = dataItem;
|
|
|
+ // Export 'ko' in the binding context so it will be available in bindings and templates
|
|
|
+ // even if 'ko' isn't exported as a global, such as when using an AMD loader.
|
|
|
+ // See https://github.com/SteveSanderson/knockout/issues/490
|
|
|
+ this['ko'] = ko;
|
|
|
+ }
|
|
|
+ this['$data'] = dataItem;
|
|
|
+ if (dataItemAlias)
|
|
|
+ this[dataItemAlias] = dataItem;
|
|
|
+ }
|
|
|
+ ko.bindingContext.prototype['createChildContext'] = function (dataItem, dataItemAlias) {
|
|
|
+ return new ko.bindingContext(dataItem, this, dataItemAlias);
|
|
|
+ };
|
|
|
+ ko.bindingContext.prototype['extend'] = function(properties) {
|
|
|
+ var clone = ko.utils.extend(new ko.bindingContext(), this);
|
|
|
+ return ko.utils.extend(clone, properties);
|
|
|
+ };
|
|
|
+
|
|
|
+ function validateThatBindingIsAllowedForVirtualElements(bindingName) {
|
|
|
+ var validator = ko.virtualElements.allowedBindings[bindingName];
|
|
|
+ if (!validator)
|
|
|
+ throw new Error("The binding '" + bindingName + "' cannot be used with virtual elements")
|
|
|
+ }
|
|
|
+
|
|
|
+ function applyBindingsToDescendantsInternal (viewModel, elementOrVirtualElement, bindingContextsMayDifferFromDomParentElement) {
|
|
|
+ var currentChild, nextInQueue = ko.virtualElements.firstChild(elementOrVirtualElement);
|
|
|
+ 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, bindingContextsMayDifferFromDomParentElement);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function applyBindingsToNodeAndDescendantsInternal (viewModel, nodeVerified, bindingContextMayDifferFromDomParentElement) {
|
|
|
+ var shouldBindDescendants = true;
|
|
|
+
|
|
|
+ // Perf optimisation: Apply bindings only if...
|
|
|
+ // (1) We need to store the binding context on this node (because it may differ from the DOM parent node's binding context)
|
|
|
+ // 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 && bindingContextMayDifferFromDomParentElement) // Case (1)
|
|
|
+ || ko.bindingProvider['instance']['nodeHasBindings'](nodeVerified); // Case (2)
|
|
|
+ if (shouldApplyBindings)
|
|
|
+ shouldBindDescendants = applyBindingsToNodeInternal(nodeVerified, null, viewModel, bindingContextMayDifferFromDomParentElement).shouldBindDescendants;
|
|
|
+
|
|
|
+ if (shouldBindDescendants) {
|
|
|
+ // We're recursing automatically into (real or virtual) child nodes without changing binding contexts. So,
|
|
|
+ // * For children of a *real* element, the binding context is certainly the same as on their DOM .parentNode,
|
|
|
+ // hence bindingContextsMayDifferFromDomParentElement is false
|
|
|
+ // * For children of a *virtual* element, we can't be sure. Evaluating .parentNode on those children may
|
|
|
+ // skip over any number of intermediate virtual elements, any of which might define a custom binding context,
|
|
|
+ // hence bindingContextsMayDifferFromDomParentElement is true
|
|
|
+ applyBindingsToDescendantsInternal(viewModel, nodeVerified, /* bindingContextsMayDifferFromDomParentElement: */ !isElement);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function applyBindingsToNodeInternal (node, bindings, viewModelOrBindingContext, bindingContextMayDifferFromDomParentElement) {
|
|
|
+ // 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
|
|
|
+
|
|
|
+ // 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;
|
|
|
+ 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'];
|
|
|
+
|
|
|
+ // Optimization: Don't store the binding context on this node if it's definitely the same as on node.parentNode, because
|
|
|
+ // we can easily recover it just by scanning up the node's ancestors in the DOM
|
|
|
+ // (note: here, parent node means "real DOM parent" not "virtual parent", as there's no O(1) way to find the virtual parent)
|
|
|
+ if (bindingContextMayDifferFromDomParentElement)
|
|
|
+ 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(bindingContextInstance, node) : bindings;
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ null,
|
|
|
+ { disposeWhenNodeIsRemoved : node }
|
|
|
+ );
|
|
|
+
|
|
|
+ return {
|
|
|
+ shouldBindDescendants: bindingHandlerThatControlsDescendantBindings === undefined
|
|
|
+ };
|
|
|
+ };
|
|
|
+
|
|
|
+ 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);
|
|
|
+ };
|
|
|
+
|
|
|
+ ko.applyBindingsToDescendants = function(viewModel, rootNode) {
|
|
|
+ if (rootNode.nodeType === 1 || rootNode.nodeType === 8)
|
|
|
+ applyBindingsToDescendantsInternal(viewModel, rootNode, true);
|
|
|
+ };
|
|
|
+
|
|
|
+ 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('bindingHandlers', ko.bindingHandlers);
|
|
|
+ ko.exportSymbol('applyBindings', ko.applyBindings);
|
|
|
+ ko.exportSymbol('applyBindingsToDescendants', ko.applyBindingsToDescendants);
|
|
|
+ ko.exportSymbol('applyBindingsToNode', ko.applyBindingsToNode);
|
|
|
+ ko.exportSymbol('contextFor', ko.contextFor);
|
|
|
+ ko.exportSymbol('dataFor', ko.dataFor);
|
|
|
+})();
|
|
|
+var attrHtmlToJavascriptMap = { 'class': 'className', 'for': 'htmlFor' };
|
|
|
+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.)
|
|
|
+ var toRemove = (attrValue === false) || (attrValue === null) || (attrValue === undefined);
|
|
|
+ if (toRemove)
|
|
|
+ element.removeAttribute(attrName);
|
|
|
+
|
|
|
+ // In IE <= 7 and IE8 Quirks Mode, you have to use the Javascript property name instead of the
|
|
|
+ // HTML attribute name for certain attributes. IE8 Standards Mode supports the correct behavior,
|
|
|
+ // but instead of figuring out the mode, we'll just set the attribute through the Javascript
|
|
|
+ // property for IE <= 8.
|
|
|
+ if (ko.utils.ieVersion <= 8 && attrName in attrHtmlToJavascriptMap) {
|
|
|
+ attrName = attrHtmlToJavascriptMap[attrName];
|
|
|
+ if (toRemove)
|
|
|
+ element.removeAttribute(attrName);
|
|
|
+ else
|
|
|
+ element[attrName] = attrValue;
|
|
|
+ } else if (!toRemove) {
|
|
|
+ element.setAttribute(attrName, attrValue.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ // Treat "name" specially - although you can think of it as an attribute, it also needs
|
|
|
+ // special handling on older versions of IE (https://github.com/SteveSanderson/knockout/pull/333)
|
|
|
+ // Deliberately being case-sensitive here because XHTML would regard "Name" as a different thing
|
|
|
+ // entirely, and there's no strong reason to allow for such casing in HTML.
|
|
|
+ if (attrName === "name") {
|
|
|
+ ko.utils.setElementName(element, toRemove ? "" : attrValue.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+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
|
|
|
+ }
|
|
|
+
|
|
|
+ var modelValue = valueAccessor(), unwrappedValue = ko.utils.unwrapObservable(modelValue);
|
|
|
+ if ((element.type == "checkbox") && (unwrappedValue 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(unwrappedValue, element.value);
|
|
|
+ if (element.checked && (existingEntryIndex < 0))
|
|
|
+ modelValue.push(element.value);
|
|
|
+ else if ((!element.checked) && (existingEntryIndex >= 0))
|
|
|
+ modelValue.splice(existingEntryIndex, 1);
|
|
|
+ } else {
|
|
|
+ ko.expressionRewriting.writeValueToProperty(modelValue, allBindingsAccessor, 'checked', valueToWrite, true);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ 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, function() { return true });
|
|
|
+ },
|
|
|
+ '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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+var classesWrittenByBindingKey = '__ko__cssValue';
|
|
|
+ko.bindingHandlers['css'] = {
|
|
|
+ 'update': function (element, valueAccessor) {
|
|
|
+ var value = ko.utils.unwrapObservable(valueAccessor());
|
|
|
+ if (typeof value == "object") {
|
|
|
+ for (var className in value) {
|
|
|
+ var shouldHaveClass = ko.utils.unwrapObservable(value[className]);
|
|
|
+ ko.utils.toggleDomNodeCssClass(element, className, shouldHaveClass);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ value = String(value || ''); // Make sure we don't try to store or set a non-string value
|
|
|
+ ko.utils.toggleDomNodeCssClass(element, element[classesWrittenByBindingKey], false);
|
|
|
+ element[classesWrittenByBindingKey] = value;
|
|
|
+ ko.utils.toggleDomNodeCssClass(element, value, true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+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, valueAccessor) {
|
|
|
+ ko.bindingHandlers['enable']['update'](element, function() { return !ko.utils.unwrapObservable(valueAccessor()) });
|
|
|
+ }
|
|
|
+};
|
|
|
+// 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}
|
|
|
+function makeEventHandlerShortcut(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();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ })();
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+// "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 modelValue = valueAccessor(),
|
|
|
+ unwrappedValue = ko.utils.peekObservable(modelValue); // Unwrap without setting a dependency here
|
|
|
+
|
|
|
+ // If unwrappedValue is the array, pass in the wrapped value on its own
|
|
|
+ // The value will be unwrapped and tracked within the template binding
|
|
|
+ // (See https://github.com/SteveSanderson/knockout/issues/523)
|
|
|
+ if ((!unwrappedValue) || typeof unwrappedValue.length == "number")
|
|
|
+ return { 'foreach': modelValue, 'templateEngine': ko.nativeTemplateEngine.instance };
|
|
|
+
|
|
|
+ // If unwrappedValue.data is the array, preserve all relevant options and unwrap again value so we get updates
|
|
|
+ ko.utils.unwrapObservable(modelValue);
|
|
|
+ return {
|
|
|
+ 'foreach': unwrappedValue['data'],
|
|
|
+ 'as': unwrappedValue['as'],
|
|
|
+ 'includeDestroyed': unwrappedValue['includeDestroyed'],
|
|
|
+ 'afterAdd': unwrappedValue['afterAdd'],
|
|
|
+ 'beforeRemove': unwrappedValue['beforeRemove'],
|
|
|
+ 'afterRender': unwrappedValue['afterRender'],
|
|
|
+ 'beforeMove': unwrappedValue['beforeMove'],
|
|
|
+ 'afterMove': unwrappedValue['afterMove'],
|
|
|
+ '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.expressionRewriting.bindingRewriteValidators['foreach'] = false; // Can't rewrite control flow bindings
|
|
|
+ko.virtualElements.allowedBindings['foreach'] = true;
|
|
|
+var hasfocusUpdatingProperty = '__ko_hasfocusUpdating';
|
|
|
+ko.bindingHandlers['hasfocus'] = {
|
|
|
+ 'init': function(element, valueAccessor, allBindingsAccessor) {
|
|
|
+ var handleElementFocusChange = function(isFocused) {
|
|
|
+ // Where possible, ignore which event was raised and determine focus state using activeElement,
|
|
|
+ // as this avoids phantom focus/blur events raised when changing tabs in modern browsers.
|
|
|
+ // However, not all KO-targeted browsers (Firefox 2) support activeElement. For those browsers,
|
|
|
+ // prevent a loss of focus when changing tabs/windows by setting a flag that prevents hasfocus
|
|
|
+ // from calling 'blur()' on the element when it loses focus.
|
|
|
+ // Discussion at https://github.com/SteveSanderson/knockout/pull/352
|
|
|
+ element[hasfocusUpdatingProperty] = true;
|
|
|
+ var ownerDoc = element.ownerDocument;
|
|
|
+ if ("activeElement" in ownerDoc) {
|
|
|
+ isFocused = (ownerDoc.activeElement === element);
|
|
|
+ }
|
|
|
+ var modelValue = valueAccessor();
|
|
|
+ ko.expressionRewriting.writeValueToProperty(modelValue, allBindingsAccessor, 'hasfocus', isFocused, true);
|
|
|
+ element[hasfocusUpdatingProperty] = false;
|
|
|
+ };
|
|
|
+ var handleElementFocusIn = handleElementFocusChange.bind(null, true);
|
|
|
+ var handleElementFocusOut = handleElementFocusChange.bind(null, false);
|
|
|
+
|
|
|
+ ko.utils.registerEventHandler(element, "focus", handleElementFocusIn);
|
|
|
+ ko.utils.registerEventHandler(element, "focusin", handleElementFocusIn); // For IE
|
|
|
+ ko.utils.registerEventHandler(element, "blur", handleElementFocusOut);
|
|
|
+ ko.utils.registerEventHandler(element, "focusout", handleElementFocusOut); // For IE
|
|
|
+ },
|
|
|
+ 'update': function(element, valueAccessor) {
|
|
|
+ var value = ko.utils.unwrapObservable(valueAccessor());
|
|
|
+ if (!element[hasfocusUpdatingProperty]) {
|
|
|
+ value ? element.focus() : element.blur();
|
|
|
+ ko.dependencyDetection.ignore(ko.utils.triggerEvent, null, [element, value ? "focusin" : "focusout"]); // For IE, which doesn't reliably fire "focus" or "blur" events synchronously
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+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) {
|
|
|
+ // setHtml will unwrap the value if needed
|
|
|
+ ko.utils.setHtml(element, valueAccessor());
|
|
|
+ }
|
|
|
+};
|
|
|
+var withIfDomDataKey = '__ko_withIfBindingData';
|
|
|
+// Makes a binding like with or if
|
|
|
+function makeWithIfBinding(bindingKey, isWith, isNot, makeContextCallback) {
|
|
|
+ ko.bindingHandlers[bindingKey] = {
|
|
|
+ 'init': function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
|
|
|
+ ko.utils.domData.set(element, withIfDomDataKey, {});
|
|
|
+ return { 'controlsDescendantBindings': true };
|
|
|
+ },
|
|
|
+ 'update': function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
|
|
|
+ var withIfData = ko.utils.domData.get(element, withIfDomDataKey),
|
|
|
+ dataValue = ko.utils.unwrapObservable(valueAccessor()),
|
|
|
+ shouldDisplay = !isNot !== !dataValue, // equivalent to isNot ? !dataValue : !!dataValue
|
|
|
+ isFirstRender = !withIfData.savedNodes,
|
|
|
+ needsRefresh = isFirstRender || isWith || (shouldDisplay !== withIfData.didDisplayOnLastUpdate);
|
|
|
+
|
|
|
+ if (needsRefresh) {
|
|
|
+ if (isFirstRender) {
|
|
|
+ withIfData.savedNodes = ko.utils.cloneNodes(ko.virtualElements.childNodes(element), true /* shouldCleanNodes */);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (shouldDisplay) {
|
|
|
+ if (!isFirstRender) {
|
|
|
+ ko.virtualElements.setDomNodeChildren(element, ko.utils.cloneNodes(withIfData.savedNodes));
|
|
|
+ }
|
|
|
+ ko.applyBindingsToDescendants(makeContextCallback ? makeContextCallback(bindingContext, dataValue) : bindingContext, element);
|
|
|
+ } else {
|
|
|
+ ko.virtualElements.emptyNode(element);
|
|
|
+ }
|
|
|
+
|
|
|
+ withIfData.didDisplayOnLastUpdate = shouldDisplay;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ ko.expressionRewriting.bindingRewriteValidators[bindingKey] = false; // Can't rewrite control flow bindings
|
|
|
+ ko.virtualElements.allowedBindings[bindingKey] = true;
|
|
|
+}
|
|
|
+
|
|
|
+// Construct the actual binding handlers
|
|
|
+makeWithIfBinding('if');
|
|
|
+makeWithIfBinding('ifnot', false /* isWith */, true /* isNot */);
|
|
|
+makeWithIfBinding('with', true /* isWith */, false /* isNot */,
|
|
|
+ function(bindingContext, dataValue) {
|
|
|
+ return bindingContext['createChildContext'](dataValue);
|
|
|
+ }
|
|
|
+);
|
|
|
+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.dependencyDetection.ignore(ko.utils.triggerEvent, null, [element, "change"]);
|
|
|
+};
|
|
|
+
|
|
|
+ko.bindingHandlers['options'] = {
|
|
|
+ 'update': function (element, valueAccessor, allBindingsAccessor) {
|
|
|
+ if (ko.utils.tagNameLower(element) !== "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 && (ko.utils.tagNameLower(node) === "option") && node.selected;
|
|
|
+ }), function (node) {
|
|
|
+ return ko.selectExtensions.readValue(node) || node.innerText || node.textContent;
|
|
|
+ });
|
|
|
+ var previousScrollTop = element.scrollTop;
|
|
|
+
|
|
|
+ var value = ko.utils.unwrapObservable(valueAccessor());
|
|
|
+ var selectedValue = element.value;
|
|
|
+
|
|
|
+ // 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(),
|
|
|
+ includeDestroyed = allBindings['optionsIncludeDestroyed'];
|
|
|
+
|
|
|
+ if (typeof value.length != "number")
|
|
|
+ value = [value];
|
|
|
+ if (allBindings['optionsCaption']) {
|
|
|
+ var option = document.createElement("option");
|
|
|
+ ko.utils.setHtml(option, allBindings['optionsCaption']);
|
|
|
+ ko.selectExtensions.writeValue(option, undefined);
|
|
|
+ element.appendChild(option);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (var i = 0, j = value.length; i < j; i++) {
|
|
|
+ // Skip destroyed items
|
|
|
+ var arrayEntry = value[i];
|
|
|
+ if (arrayEntry && arrayEntry['_destroy'] && !includeDestroyed)
|
|
|
+ continue;
|
|
|
+
|
|
|
+ var option = document.createElement("option");
|
|
|
+
|
|
|
+ function applyToObject(object, predicate, defaultValue) {
|
|
|
+ var predicateType = typeof predicate;
|
|
|
+ if (predicateType == "function") // Given a function; run it against the data value
|
|
|
+ return predicate(object);
|
|
|
+ else if (predicateType == "string") // Given a string; treat it as a property name on the data value
|
|
|
+ return object[predicate];
|
|
|
+ else // Given no optionsText arg; use the data value itself
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Apply a value to the option element
|
|
|
+ var optionValue = applyToObject(arrayEntry, allBindings['optionsValue'], arrayEntry);
|
|
|
+ ko.selectExtensions.writeValue(option, ko.utils.unwrapObservable(optionValue));
|
|
|
+
|
|
|
+ // Apply some text to the option element
|
|
|
+ var optionText = applyToObject(arrayEntry, allBindings['optionsText'], optionValue);
|
|
|
+ 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.
|
|
|
+ var newOptions = element.getElementsByTagName("option");
|
|
|
+ var countSelectionsRetained = 0;
|
|
|
+ for (var i = 0, j = newOptions.length; i < j; i++) {
|
|
|
+ if (ko.utils.arrayIndexOf(previousSelectedValues, ko.selectExtensions.readValue(newOptions[i])) >= 0) {
|
|
|
+ ko.utils.setOptionNodeSelectionState(newOptions[i], true);
|
|
|
+ countSelectionsRetained++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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.peekObservable(allBindings['value']), /* preferModelValue */ true);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Workaround for IE9 bug
|
|
|
+ ko.utils.ensureSelectElementIsRenderedCorrectly(element);
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+ko.bindingHandlers['options'].optionValueDomDataKey = '__ko.optionValueDomData__';
|
|
|
+ko.bindingHandlers['selectedOptions'] = {
|
|
|
+ 'init': function (element, valueAccessor, allBindingsAccessor) {
|
|
|
+ ko.utils.registerEventHandler(element, "change", function () {
|
|
|
+ var value = valueAccessor(), valueToWrite = [];
|
|
|
+ ko.utils.arrayForEach(element.getElementsByTagName("option"), function(node) {
|
|
|
+ if (node.selected)
|
|
|
+ valueToWrite.push(ko.selectExtensions.readValue(node));
|
|
|
+ });
|
|
|
+ ko.expressionRewriting.writeValueToProperty(value, allBindingsAccessor, 'value', valueToWrite);
|
|
|
+ });
|
|
|
+ },
|
|
|
+ 'update': function (element, valueAccessor) {
|
|
|
+ if (ko.utils.tagNameLower(element) != "select")
|
|
|
+ throw new Error("values binding applies only to SELECT elements");
|
|
|
+
|
|
|
+ var newValue = ko.utils.unwrapObservable(valueAccessor());
|
|
|
+ if (newValue && typeof newValue.length == "number") {
|
|
|
+ ko.utils.arrayForEach(element.getElementsByTagName("option"), function(node) {
|
|
|
+ var isSelected = ko.utils.arrayIndexOf(newValue, ko.selectExtensions.readValue(node)) >= 0;
|
|
|
+ ko.utils.setOptionNodeSelectionState(node, isSelected);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+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['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) {
|
|
|
+ var handlerReturnValue;
|
|
|
+ var value = valueAccessor();
|
|
|
+ try { handlerReturnValue = value.call(viewModel, element); }
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+};
|
|
|
+ko.bindingHandlers['text'] = {
|
|
|
+ 'update': function (element, valueAccessor) {
|
|
|
+ ko.utils.setTextContent(element, valueAccessor());
|
|
|
+ }
|
|
|
+};
|
|
|
+ko.virtualElements.allowedBindings['text'] = true;
|
|
|
+ko.bindingHandlers['uniqueName'] = {
|
|
|
+ 'init': function (element, valueAccessor) {
|
|
|
+ if (valueAccessor()) {
|
|
|
+ var name = "ko_unique_" + (++ko.bindingHandlers['uniqueName'].currentIndex);
|
|
|
+ ko.utils.setElementName(element, name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+ko.bindingHandlers['uniqueName'].currentIndex = 0;
|
|
|
+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"];
|
|
|
+ var propertyChangedFired = false;
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ var valueUpdateHandler = function() {
|
|
|
+ propertyChangedFired = false;
|
|
|
+ var modelValue = valueAccessor();
|
|
|
+ var elementValue = ko.selectExtensions.readValue(element);
|
|
|
+ ko.expressionRewriting.writeValueToProperty(modelValue, allBindingsAccessor, 'value', elementValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Workaround for https://github.com/SteveSanderson/knockout/issues/122
|
|
|
+ // IE doesn't fire "change" events on textboxes if the user selects a value from its autocomplete list
|
|
|
+ var ieAutoCompleteHackNeeded = ko.utils.ieVersion && element.tagName.toLowerCase() == "input" && element.type == "text"
|
|
|
+ && element.autocomplete != "off" && (!element.form || element.form.autocomplete != "off");
|
|
|
+ if (ieAutoCompleteHackNeeded && ko.utils.arrayIndexOf(eventsToCatch, "propertychange") == -1) {
|
|
|
+ ko.utils.registerEventHandler(element, "propertychange", function () { propertyChangedFired = true });
|
|
|
+ ko.utils.registerEventHandler(element, "blur", function() {
|
|
|
+ if (propertyChangedFired) {
|
|
|
+ valueUpdateHandler();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 handler = valueUpdateHandler;
|
|
|
+ if (ko.utils.stringStartsWith(eventName, "after")) {
|
|
|
+ handler = function() { setTimeout(valueUpdateHandler, 0) };
|
|
|
+ eventName = eventName.substring("after".length);
|
|
|
+ }
|
|
|
+ ko.utils.registerEventHandler(element, eventName, handler);
|
|
|
+ });
|
|
|
+ },
|
|
|
+ 'update': function (element, valueAccessor) {
|
|
|
+ var valueIsSelectOption = ko.utils.tagNameLower(element) === "select";
|
|
|
+ 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 = valueIsSelectOption;
|
|
|
+ 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 (valueIsSelectOption && (element.length > 0))
|
|
|
+ ensureDropdownSelectionIsConsistentWithModelValue(element, newValue, /* preferModelValue */ false);
|
|
|
+ }
|
|
|
+};
|
|
|
+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";
|
|
|
+ }
|
|
|
+};
|
|
|
+// 'click' is just a shorthand for the usual full-length event:{click:handler}
|
|
|
+makeEventHandlerShortcut('click');
|
|
|
+// 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 new Error("Override renderTemplateSource");
|
|
|
+};
|
|
|
+
|
|
|
+ko.templateEngine.prototype['createJavaScriptEvaluatorBlock'] = function (script) {
|
|
|
+ throw new Error("Override createJavaScriptEvaluatorBlock");
|
|
|
+};
|
|
|
+
|
|
|
+ko.templateEngine.prototype['makeTemplateSource'] = function(template, templateDocument) {
|
|
|
+ // Named template
|
|
|
+ if (typeof template == "string") {
|
|
|
+ templateDocument = templateDocument || document;
|
|
|
+ var elem = templateDocument.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, templateDocument) {
|
|
|
+ var templateSource = this['makeTemplateSource'](template, templateDocument);
|
|
|
+ return this['renderTemplateSource'](templateSource, bindingContext, options);
|
|
|
+};
|
|
|
+
|
|
|
+ko.templateEngine.prototype['isTemplateRewritten'] = function (template, templateDocument) {
|
|
|
+ // Skip rewriting if requested
|
|
|
+ if (this['allowTemplateRewriting'] === false)
|
|
|
+ return true;
|
|
|
+ return this['makeTemplateSource'](template, templateDocument)['data']("isRewritten");
|
|
|
+};
|
|
|
+
|
|
|
+ko.templateEngine.prototype['rewriteTemplate'] = function (template, rewriterCallback, templateDocument) {
|
|
|
+ var templateSource = this['makeTemplateSource'](template, templateDocument);
|
|
|
+ var rewritten = rewriterCallback(templateSource['text']());
|
|
|
+ templateSource['text'](rewritten);
|
|
|
+ templateSource['data']("isRewritten", true);
|
|
|
+};
|
|
|
+
|
|
|
+ko.exportSymbol('templateEngine', ko.templateEngine);
|
|
|
+
|
|
|
+ko.templateRewriting = (function () {
|
|
|
+ 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.expressionRewriting.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.expressionRewriting.parseObjectLiteral(dataBindAttributeValue);
|
|
|
+ validateDataBindValuesForRewriting(dataBindKeyValueArray);
|
|
|
+ var rewrittenDataBindAttributeValue = ko.expressionRewriting.preProcessBindings(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.__tr_ambtns(function($context,$element){return(function(){return{ " + rewrittenDataBindAttributeValue + " } })()})";
|
|
|
+ return templateEngine['createJavaScriptEvaluatorBlock'](applyBindingsToNextSiblingScript) + tagToRetain;
|
|
|
+ }
|
|
|
+
|
|
|
+ return {
|
|
|
+ ensureTemplateIsRewritten: function (template, templateEngine, templateDocument) {
|
|
|
+ if (!templateEngine['isTemplateRewritten'](template, templateDocument))
|
|
|
+ templateEngine['rewriteTemplate'](template, function (htmlString) {
|
|
|
+ return ko.templateRewriting.memoizeBindingAttributeSyntax(htmlString, templateEngine);
|
|
|
+ }, templateDocument);
|
|
|
+ },
|
|
|
+
|
|
|
+ memoizeBindingAttributeSyntax: function (htmlString, templateEngine) {
|
|
|
+ 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, bindingContext) {
|
|
|
+ if (domNode.nextSibling)
|
|
|
+ ko.applyBindingsToNode(domNode.nextSibling, bindings, bindingContext);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+})();
|
|
|
+
|
|
|
+
|
|
|
+// Exported only because it has to be referenced by string lookup from within rewritten template
|
|
|
+ko.exportSymbol('__tr_ambtns', ko.templateRewriting.applyMemoizedBindingsToNextSibling);
|
|
|
+(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".
|
|
|
+ //
|
|
|
+ // Optionally, template sources can also have the following functions:
|
|
|
+ // nodes() - returns a DOM element containing the nodes of this template, where available
|
|
|
+ // nodes(value) - writes the given DOM element to your storage location
|
|
|
+ // If a DOM element is available for a given template source, template engines are encouraged to use it in preference over text()
|
|
|
+ // for improved speed. However, all templateSources must supply text() even if they don't supply nodes().
|
|
|
+ //
|
|
|
+ // 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 */) {
|
|
|
+ var tagNameLower = ko.utils.tagNameLower(this.domElement),
|
|
|
+ elemContentsProperty = tagNameLower === "script" ? "text"
|
|
|
+ : tagNameLower === "textarea" ? "value"
|
|
|
+ : "innerHTML";
|
|
|
+
|
|
|
+ if (arguments.length == 0) {
|
|
|
+ return this.domElement[elemContentsProperty];
|
|
|
+ } else {
|
|
|
+ var valueToWrite = arguments[0];
|
|
|
+ if (elemContentsProperty === "innerHTML")
|
|
|
+ ko.utils.setHtml(this.domElement, valueToWrite);
|
|
|
+ else
|
|
|
+ this.domElement[elemContentsProperty] = 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 -----
|
|
|
+ // Anonymous templates are normally saved/retrieved as DOM nodes through "nodes".
|
|
|
+ // For compatibility, you can also read "text"; it will be serialized from the nodes on demand.
|
|
|
+ // Writing to "text" is still supported, but then the template data will not be available as DOM nodes.
|
|
|
+
|
|
|
+ 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) {
|
|
|
+ var templateData = ko.utils.domData.get(this.domElement, anonymousTemplatesDomDataKey) || {};
|
|
|
+ if (templateData.textData === undefined && templateData.containerData)
|
|
|
+ templateData.textData = templateData.containerData.innerHTML;
|
|
|
+ return templateData.textData;
|
|
|
+ } else {
|
|
|
+ var valueToWrite = arguments[0];
|
|
|
+ ko.utils.domData.set(this.domElement, anonymousTemplatesDomDataKey, {textData: valueToWrite});
|
|
|
+ }
|
|
|
+ };
|
|
|
+ ko.templateSources.domElement.prototype['nodes'] = function(/* valueToWrite */) {
|
|
|
+ if (arguments.length == 0) {
|
|
|
+ var templateData = ko.utils.domData.get(this.domElement, anonymousTemplatesDomDataKey) || {};
|
|
|
+ return templateData.containerData;
|
|
|
+ } else {
|
|
|
+ var valueToWrite = arguments[0];
|
|
|
+ ko.utils.domData.set(this.domElement, anonymousTemplatesDomDataKey, {containerData: valueToWrite});
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ ko.exportSymbol('templateSources', ko.templateSources);
|
|
|
+ ko.exportSymbol('templateSources.domElement', ko.templateSources.domElement);
|
|
|
+ ko.exportSymbol('templateSources.anonymousTemplate', ko.templateSources.anonymousTemplate);
|
|
|
+})();
|
|
|
+(function () {
|
|
|
+ var _templateEngine;
|
|
|
+ ko.setTemplateEngine = function (templateEngine) {
|
|
|
+ if ((templateEngine != undefined) && !(templateEngine instanceof ko.templateEngine))
|
|
|
+ throw new Error("templateEngine must inherit from ko.templateEngine");
|
|
|
+ _templateEngine = templateEngine;
|
|
|
+ }
|
|
|
+
|
|
|
+ function invokeForEachNodeOrCommentInContinuousRange(firstNode, lastNode, action) {
|
|
|
+ var node, nextInQueue = firstNode, firstOutOfRangeNode = ko.virtualElements.nextSibling(lastNode);
|
|
|
+ while (nextInQueue && ((node = nextInQueue) !== firstOutOfRangeNode)) {
|
|
|
+ nextInQueue = ko.virtualElements.nextSibling(node);
|
|
|
+ if (node.nodeType === 1 || node.nodeType === 8)
|
|
|
+ action(node);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function activateBindingsOnContinuousNodeArray(continuousNodeArray, bindingContext) {
|
|
|
+ // To be used on any nodes that have been rendered by a template and have been inserted into some parent element
|
|
|
+ // Walks through continuousNodeArray (which *must* be continuous, i.e., an uninterrupted sequence of sibling nodes, because
|
|
|
+ // the algorithm for walking them relies on this), and for each top-level item in the virtual-element sense,
|
|
|
+ // (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)
|
|
|
+
|
|
|
+ if (continuousNodeArray.length) {
|
|
|
+ var firstNode = continuousNodeArray[0], lastNode = continuousNodeArray[continuousNodeArray.length - 1];
|
|
|
+
|
|
|
+ // 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
|
|
|
+ invokeForEachNodeOrCommentInContinuousRange(firstNode, lastNode, function(node) {
|
|
|
+ ko.applyBindings(bindingContext, node);
|
|
|
+ });
|
|
|
+ invokeForEachNodeOrCommentInContinuousRange(firstNode, lastNode, 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, bindingContext, options) {
|
|
|
+ options = options || {};
|
|
|
+ var firstTargetNode = targetNodeOrNodeArray && getFirstNodeFromPossibleArray(targetNodeOrNodeArray);
|
|
|
+ var templateDocument = firstTargetNode && firstTargetNode.ownerDocument;
|
|
|
+ var templateEngineToUse = (options['templateEngine'] || _templateEngine);
|
|
|
+ ko.templateRewriting.ensureTemplateIsRewritten(template, templateEngineToUse, templateDocument);
|
|
|
+ var renderedNodesArray = templateEngineToUse['renderTemplate'](template, bindingContext, options, templateDocument);
|
|
|
+
|
|
|
+ // Loosely check result is an array of DOM nodes
|
|
|
+ if ((typeof renderedNodesArray.length != "number") || (renderedNodesArray.length > 0 && typeof renderedNodesArray[0].nodeType != "number"))
|
|
|
+ throw new Error("Template engine must return an array of DOM nodes");
|
|
|
+
|
|
|
+ var haveAddedNodesToParent = false;
|
|
|
+ switch (renderMode) {
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (haveAddedNodesToParent) {
|
|
|
+ activateBindingsOnContinuousNodeArray(renderedNodesArray, bindingContext);
|
|
|
+ if (options['afterRender'])
|
|
|
+ ko.dependencyDetection.ignore(options['afterRender'], null, [renderedNodesArray, bindingContext['$data']]);
|
|
|
+ }
|
|
|
+
|
|
|
+ return renderedNodesArray;
|
|
|
+ }
|
|
|
+
|
|
|
+ ko.renderTemplate = function (template, dataOrBindingContext, options, targetNodeOrNodeArray, renderMode) {
|
|
|
+ options = options || {};
|
|
|
+ if ((options['templateEngine'] || _templateEngine) == undefined)
|
|
|
+ throw new Error("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); }; // Passive disposal (on next evaluation)
|
|
|
+ var activelyDisposeWhenNodeIsRemoved = (firstTargetNode && renderMode == "replaceNode") ? firstTargetNode.parentNode : firstTargetNode;
|
|
|
+
|
|
|
+ return ko.dependentObservable( // So the DOM is automatically updated when any dependency changes
|
|
|
+ function () {
|
|
|
+ // 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'], bindingContext) : template;
|
|
|
+
|
|
|
+ var renderedNodesArray = executeTemplate(targetNodeOrNodeArray, renderMode, templateName, bindingContext, options);
|
|
|
+ if (renderMode == "replaceNode") {
|
|
|
+ targetNodeOrNodeArray = renderedNodesArray;
|
|
|
+ firstTargetNode = getFirstNodeFromPossibleArray(targetNodeOrNodeArray);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ null,
|
|
|
+ { 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, dataOrBindingContext, options, domNode, "replaceNode");
|
|
|
+ });
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ ko.renderTemplateForEach = function (template, arrayOrObservableArray, options, targetNode, parentBindingContext) {
|
|
|
+ // Since setDomNodeChildrenFromArrayMapping always calls executeTemplateForArrayItem and then
|
|
|
+ // activateBindingsCallback for added items, we can store the binding context in the former to use in the latter.
|
|
|
+ var arrayItemContext;
|
|
|
+
|
|
|
+ // This will be called by setDomNodeChildrenFromArrayMapping to get the nodes to add to targetNode
|
|
|
+ var executeTemplateForArrayItem = function (arrayValue, index) {
|
|
|
+ // Support selecting template as a function of the data being rendered
|
|
|
+ arrayItemContext = parentBindingContext['createChildContext'](ko.utils.unwrapObservable(arrayValue), options['as']);
|
|
|
+ arrayItemContext['$index'] = index;
|
|
|
+ var templateName = typeof(template) == 'function' ? template(arrayValue, arrayItemContext) : template;
|
|
|
+ return executeTemplate(null, "ignoreTargetNode", templateName, arrayItemContext, options);
|
|
|
+ }
|
|
|
+
|
|
|
+ // This will be called whenever setDomNodeChildrenFromArrayMapping has added nodes to targetNode
|
|
|
+ var activateBindingsCallback = function(arrayValue, addedNodesArray, index) {
|
|
|
+ activateBindingsOnContinuousNodeArray(addedNodesArray, arrayItemContext);
|
|
|
+ if (options['afterRender'])
|
|
|
+ options['afterRender'](addedNodesArray, arrayValue);
|
|
|
+ };
|
|
|
+
|
|
|
+ return 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 === undefined || item === null || !ko.utils.unwrapObservable(item['_destroy']);
|
|
|
+ });
|
|
|
+
|
|
|
+ // Call setDomNodeChildrenFromArrayMapping, ignoring any observables unwrapped within (most likely from a callback function).
|
|
|
+ // If the array items are observables, though, they will be unwrapped in executeTemplateForArrayItem and managed within setDomNodeChildrenFromArrayMapping.
|
|
|
+ ko.dependencyDetection.ignore(ko.utils.setDomNodeChildrenFromArrayMapping, null, [targetNode, filteredArray, executeTemplateForArrayItem, options, activateBindingsCallback]);
|
|
|
+
|
|
|
+ }, null, { disposeWhenNodeIsRemoved: targetNode });
|
|
|
+ };
|
|
|
+
|
|
|
+ var templateComputedDomDataKey = '__ko__templateComputedDomDataKey__';
|
|
|
+ function disposeOldComputedAndStoreNewOne(element, newComputed) {
|
|
|
+ var oldComputed = ko.utils.domData.get(element, templateComputedDomDataKey);
|
|
|
+ if (oldComputed && (typeof(oldComputed.dispose) == 'function'))
|
|
|
+ oldComputed.dispose();
|
|
|
+ ko.utils.domData.set(element, templateComputedDomDataKey, (newComputed && newComputed.isActive()) ? newComputed : undefined);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 || element.nodeType == 8)) {
|
|
|
+ // It's an anonymous template - store the element contents, then clear the element
|
|
|
+ var templateNodes = element.nodeType == 1 ? element.childNodes : ko.virtualElements.childNodes(element),
|
|
|
+ container = ko.utils.moveCleanedNodesToContainerElement(templateNodes); // This also removes the nodes from their current parent
|
|
|
+ new ko.templateSources.anonymousTemplate(element)['nodes'](container);
|
|
|
+ }
|
|
|
+ return { 'controlsDescendantBindings': true };
|
|
|
+ },
|
|
|
+ 'update': function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
|
|
|
+ var templateName = ko.utils.unwrapObservable(valueAccessor()),
|
|
|
+ options = {},
|
|
|
+ shouldDisplay = true,
|
|
|
+ dataValue,
|
|
|
+ templateComputed = null;
|
|
|
+
|
|
|
+ if (typeof templateName != "string") {
|
|
|
+ options = templateName;
|
|
|
+ templateName = options['name'];
|
|
|
+
|
|
|
+ // Support "if"/"ifnot" conditions
|
|
|
+ if ('if' in options)
|
|
|
+ shouldDisplay = ko.utils.unwrapObservable(options['if']);
|
|
|
+ if (shouldDisplay && 'ifnot' in options)
|
|
|
+ shouldDisplay = !ko.utils.unwrapObservable(options['ifnot']);
|
|
|
+
|
|
|
+ dataValue = ko.utils.unwrapObservable(options['data']);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ('foreach' in options) {
|
|
|
+ // Render once for each data point (treating data set as empty if shouldDisplay==false)
|
|
|
+ var dataArray = (shouldDisplay && options['foreach']) || [];
|
|
|
+ templateComputed = ko.renderTemplateForEach(templateName || element, dataArray, options, element, bindingContext);
|
|
|
+ } else if (!shouldDisplay) {
|
|
|
+ ko.virtualElements.emptyNode(element);
|
|
|
+ } else {
|
|
|
+ // Render once for this single data point (or use the viewModel if no data was provided)
|
|
|
+ var innerBindingContext = ('data' in options) ?
|
|
|
+ bindingContext['createChildContext'](dataValue, options['as']) : // 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
|
|
|
+ templateComputed = ko.renderTemplate(templateName || element, innerBindingContext, options, element);
|
|
|
+ }
|
|
|
+
|
|
|
+ // It only makes sense to have a single template computed per element (otherwise which one should have its output displayed?)
|
|
|
+ disposeOldComputedAndStoreNewOne(element, templateComputed);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ // Anonymous templates can't be rewritten. Give a nice error message if you try to do it.
|
|
|
+ ko.expressionRewriting.bindingRewriteValidators['template'] = function(bindingValue) {
|
|
|
+ var parsedBindingValue = ko.expressionRewriting.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.expressionRewriting.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('setTemplateEngine', ko.setTemplateEngine);
|
|
|
+ko.exportSymbol('renderTemplate', ko.renderTemplate);
|
|
|
+
|
|
|
+ko.utils.compareArrays = (function () {
|
|
|
+ var statusNotInOld = 'added', statusNotInNew = 'deleted';
|
|
|
+
|
|
|
+ // Simple calculation based on Levenshtein distance.
|
|
|
+ function compareArrays(oldArray, newArray, dontLimitMoves) {
|
|
|
+ oldArray = oldArray || [];
|
|
|
+ newArray = newArray || [];
|
|
|
+
|
|
|
+ if (oldArray.length <= newArray.length)
|
|
|
+ return compareSmallArrayToBigArray(oldArray, newArray, statusNotInOld, statusNotInNew, dontLimitMoves);
|
|
|
+ else
|
|
|
+ return compareSmallArrayToBigArray(newArray, oldArray, statusNotInNew, statusNotInOld, dontLimitMoves);
|
|
|
+ }
|
|
|
+
|
|
|
+ function compareSmallArrayToBigArray(smlArray, bigArray, statusNotInSml, statusNotInBig, dontLimitMoves) {
|
|
|
+ var myMin = Math.min,
|
|
|
+ myMax = Math.max,
|
|
|
+ editDistanceMatrix = [],
|
|
|
+ smlIndex, smlIndexMax = smlArray.length,
|
|
|
+ bigIndex, bigIndexMax = bigArray.length,
|
|
|
+ compareRange = (bigIndexMax - smlIndexMax) || 1,
|
|
|
+ maxDistance = smlIndexMax + bigIndexMax + 1,
|
|
|
+ thisRow, lastRow,
|
|
|
+ bigIndexMaxForRow, bigIndexMinForRow;
|
|
|
+
|
|
|
+ for (smlIndex = 0; smlIndex <= smlIndexMax; smlIndex++) {
|
|
|
+ lastRow = thisRow;
|
|
|
+ editDistanceMatrix.push(thisRow = []);
|
|
|
+ bigIndexMaxForRow = myMin(bigIndexMax, smlIndex + compareRange);
|
|
|
+ bigIndexMinForRow = myMax(0, smlIndex - 1);
|
|
|
+ for (bigIndex = bigIndexMinForRow; bigIndex <= bigIndexMaxForRow; bigIndex++) {
|
|
|
+ if (!bigIndex)
|
|
|
+ thisRow[bigIndex] = smlIndex + 1;
|
|
|
+ else if (!smlIndex) // Top row - transform empty array into new array via additions
|
|
|
+ thisRow[bigIndex] = bigIndex + 1;
|
|
|
+ else if (smlArray[smlIndex - 1] === bigArray[bigIndex - 1])
|
|
|
+ thisRow[bigIndex] = lastRow[bigIndex - 1]; // copy value (no edit)
|
|
|
+ else {
|
|
|
+ var northDistance = lastRow[bigIndex] || maxDistance; // not in big (deletion)
|
|
|
+ var westDistance = thisRow[bigIndex - 1] || maxDistance; // not in small (addition)
|
|
|
+ thisRow[bigIndex] = myMin(northDistance, westDistance) + 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var editScript = [], meMinusOne, notInSml = [], notInBig = [];
|
|
|
+ for (smlIndex = smlIndexMax, bigIndex = bigIndexMax; smlIndex || bigIndex;) {
|
|
|
+ meMinusOne = editDistanceMatrix[smlIndex][bigIndex] - 1;
|
|
|
+ if (bigIndex && meMinusOne === editDistanceMatrix[smlIndex][bigIndex-1]) {
|
|
|
+ notInSml.push(editScript[editScript.length] = { // added
|
|
|
+ 'status': statusNotInSml,
|
|
|
+ 'value': bigArray[--bigIndex],
|
|
|
+ 'index': bigIndex });
|
|
|
+ } else if (smlIndex && meMinusOne === editDistanceMatrix[smlIndex - 1][bigIndex]) {
|
|
|
+ notInBig.push(editScript[editScript.length] = { // deleted
|
|
|
+ 'status': statusNotInBig,
|
|
|
+ 'value': smlArray[--smlIndex],
|
|
|
+ 'index': smlIndex });
|
|
|
+ } else {
|
|
|
+ editScript.push({
|
|
|
+ 'status': "retained",
|
|
|
+ 'value': bigArray[--bigIndex] });
|
|
|
+ --smlIndex;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (notInSml.length && notInBig.length) {
|
|
|
+ // Set a limit on the number of consecutive non-matching comparisons; having it a multiple of
|
|
|
+ // smlIndexMax keeps the time complexity of this algorithm linear.
|
|
|
+ var limitFailedCompares = smlIndexMax * 10, failedCompares,
|
|
|
+ a, d, notInSmlItem, notInBigItem;
|
|
|
+ // Go through the items that have been added and deleted and try to find matches between them.
|
|
|
+ for (failedCompares = a = 0; (dontLimitMoves || failedCompares < limitFailedCompares) && (notInSmlItem = notInSml[a]); a++) {
|
|
|
+ for (d = 0; notInBigItem = notInBig[d]; d++) {
|
|
|
+ if (notInSmlItem['value'] === notInBigItem['value']) {
|
|
|
+ notInSmlItem['moved'] = notInBigItem['index'];
|
|
|
+ notInBigItem['moved'] = notInSmlItem['index'];
|
|
|
+ notInBig.splice(d,1); // This item is marked as moved; so remove it from notInBig list
|
|
|
+ failedCompares = d = 0; // Reset failed compares count because we're checking for consecutive failures
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ failedCompares += d;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return editScript.reverse();
|
|
|
+ }
|
|
|
+
|
|
|
+ return compareArrays;
|
|
|
+})();
|
|
|
+
|
|
|
+ko.exportSymbol('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
|
|
|
+
|
|
|
+ // "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 fixUpNodesToBeMovedOrRemoved(contiguousNodeArray) {
|
|
|
+ // Before moving, deleting, or replacing a set of nodes that were previously outputted by the "map" function, we have to reconcile
|
|
|
+ // them against what is in the DOM right now. It may be that some of the nodes have already been removed from the document,
|
|
|
+ // or that new nodes might have been inserted in the middle, for example by a binding. Also, there may previously have been
|
|
|
+ // leading comment nodes (created by rewritten string-based templates) that have since been removed during binding.
|
|
|
+ // So, this function translates the old "map" output array into its best guess of what set of current DOM nodes should be removed.
|
|
|
+ //
|
|
|
+ // Rules:
|
|
|
+ // [A] Any leading nodes that aren't in the document any more should be ignored
|
|
|
+ // These most likely correspond to memoization nodes that were already removed during binding
|
|
|
+ // See https://github.com/SteveSanderson/knockout/pull/440
|
|
|
+ // [B] We want to output a contiguous series of nodes that are still in the document. So, ignore any nodes that
|
|
|
+ // have already been removed, and include any nodes that have been inserted among the previous collection
|
|
|
+
|
|
|
+ // Rule [A]
|
|
|
+ while (contiguousNodeArray.length && !ko.utils.domNodeIsAttachedToDocument(contiguousNodeArray[0]))
|
|
|
+ contiguousNodeArray.splice(0, 1);
|
|
|
+
|
|
|
+ // Rule [B]
|
|
|
+ if (contiguousNodeArray.length > 1) {
|
|
|
+ // 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));
|
|
|
+ }
|
|
|
+ return contiguousNodeArray;
|
|
|
+ }
|
|
|
+
|
|
|
+ function mapNodeAndRefreshWhenChanged(containerNode, mapping, valueToMap, callbackAfterAddingNodes, index) {
|
|
|
+ // Map this array value inside a dependentObservable so we re-map when any dependency changes
|
|
|
+ var mappedNodes = [];
|
|
|
+ var dependentObservable = ko.dependentObservable(function() {
|
|
|
+ var newMappedNodes = mapping(valueToMap, index) || [];
|
|
|
+
|
|
|
+ // On subsequent evaluations, just replace the previously-inserted DOM nodes
|
|
|
+ if (mappedNodes.length > 0) {
|
|
|
+ ko.utils.replaceDomNodes(fixUpNodesToBeMovedOrRemoved(mappedNodes), newMappedNodes);
|
|
|
+ if (callbackAfterAddingNodes)
|
|
|
+ ko.dependencyDetection.ignore(callbackAfterAddingNodes, null, [valueToMap, newMappedNodes, index]);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 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, { disposeWhenNodeIsRemoved: containerNode, disposeWhen: function() { return (mappedNodes.length == 0) || !ko.utils.domNodeIsAttachedToDocument(mappedNodes[0]) } });
|
|
|
+ return { mappedNodes : mappedNodes, dependentObservable : (dependentObservable.isActive() ? dependentObservable : undefined) };
|
|
|
+ }
|
|
|
+
|
|
|
+ var lastMappingResultDomDataKey = "setDomNodeChildrenFromArrayMapping_lastMappingResult";
|
|
|
+
|
|
|
+ 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, 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 = [];
|
|
|
+ var lastMappingResultIndex = 0;
|
|
|
+ var newMappingResultIndex = 0;
|
|
|
+
|
|
|
+ var nodesToDelete = [];
|
|
|
+ var itemsToProcess = [];
|
|
|
+ var itemsForBeforeRemoveCallbacks = [];
|
|
|
+ var itemsForMoveCallbacks = [];
|
|
|
+ var itemsForAfterAddCallbacks = [];
|
|
|
+ var mapData;
|
|
|
+
|
|
|
+ function itemMovedOrRetained(editScriptIndex, oldPosition) {
|
|
|
+ mapData = lastMappingResult[oldPosition];
|
|
|
+ if (newMappingResultIndex !== oldPosition)
|
|
|
+ itemsForMoveCallbacks[editScriptIndex] = mapData;
|
|
|
+ // Since updating the index might change the nodes, do so before calling fixUpNodesToBeMovedOrRemoved
|
|
|
+ mapData.indexObservable(newMappingResultIndex++);
|
|
|
+ fixUpNodesToBeMovedOrRemoved(mapData.mappedNodes);
|
|
|
+ newMappingResult.push(mapData);
|
|
|
+ itemsToProcess.push(mapData);
|
|
|
+ }
|
|
|
+
|
|
|
+ function callCallback(callback, items) {
|
|
|
+ if (callback) {
|
|
|
+ for (var i = 0, n = items.length; i < n; i++) {
|
|
|
+ if (items[i]) {
|
|
|
+ ko.utils.arrayForEach(items[i].mappedNodes, function(node) {
|
|
|
+ callback(node, i, items[i].arrayEntry);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for (var i = 0, editScriptItem, movedIndex; editScriptItem = editScript[i]; i++) {
|
|
|
+ movedIndex = editScriptItem['moved'];
|
|
|
+ switch (editScriptItem['status']) {
|
|
|
+ case "deleted":
|
|
|
+ if (movedIndex === undefined) {
|
|
|
+ mapData = lastMappingResult[lastMappingResultIndex];
|
|
|
+
|
|
|
+ // Stop tracking changes to the mapping for these nodes
|
|
|
+ if (mapData.dependentObservable)
|
|
|
+ mapData.dependentObservable.dispose();
|
|
|
+
|
|
|
+ // Queue these nodes for later removal
|
|
|
+ nodesToDelete.push.apply(nodesToDelete, fixUpNodesToBeMovedOrRemoved(mapData.mappedNodes));
|
|
|
+ if (options['beforeRemove']) {
|
|
|
+ itemsForBeforeRemoveCallbacks[i] = mapData;
|
|
|
+ itemsToProcess.push(mapData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ lastMappingResultIndex++;
|
|
|
+ break;
|
|
|
+
|
|
|
+ case "retained":
|
|
|
+ itemMovedOrRetained(i, lastMappingResultIndex++);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case "added":
|
|
|
+ if (movedIndex !== undefined) {
|
|
|
+ itemMovedOrRetained(i, movedIndex);
|
|
|
+ } else {
|
|
|
+ mapData = { arrayEntry: editScriptItem['value'], indexObservable: ko.observable(newMappingResultIndex++) };
|
|
|
+ newMappingResult.push(mapData);
|
|
|
+ itemsToProcess.push(mapData);
|
|
|
+ if (!isFirstExecution)
|
|
|
+ itemsForAfterAddCallbacks[i] = mapData;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Call beforeMove first before any changes have been made to the DOM
|
|
|
+ callCallback(options['beforeMove'], itemsForMoveCallbacks);
|
|
|
+
|
|
|
+ // Next remove nodes for deleted items (or just clean if there's a beforeRemove callback)
|
|
|
+ ko.utils.arrayForEach(nodesToDelete, options['beforeRemove'] ? ko.cleanNode : ko.removeNode);
|
|
|
+
|
|
|
+ // Next add/reorder the remaining items (will include deleted items if there's a beforeRemove callback)
|
|
|
+ for (var i = 0, nextNode = ko.virtualElements.firstChild(domNode), lastNode, node; mapData = itemsToProcess[i]; i++) {
|
|
|
+ // Get nodes for newly added items
|
|
|
+ if (!mapData.mappedNodes)
|
|
|
+ ko.utils.extend(mapData, mapNodeAndRefreshWhenChanged(domNode, mapping, mapData.arrayEntry, callbackAfterAddingNodes, mapData.indexObservable));
|
|
|
+
|
|
|
+ // Put nodes in the right place if they aren't there already
|
|
|
+ for (var j = 0; node = mapData.mappedNodes[j]; nextNode = node.nextSibling, lastNode = node, j++) {
|
|
|
+ if (node !== nextNode)
|
|
|
+ ko.virtualElements.insertAfter(domNode, node, lastNode);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Run the callbacks for newly added nodes (for example, to apply bindings, etc.)
|
|
|
+ if (!mapData.initialized && callbackAfterAddingNodes) {
|
|
|
+ callbackAfterAddingNodes(mapData.arrayEntry, mapData.mappedNodes, mapData.indexObservable);
|
|
|
+ mapData.initialized = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // If there's a beforeRemove callback, call it after reordering.
|
|
|
+ // Note that we assume that the beforeRemove callback will usually be used to remove the nodes using
|
|
|
+ // some sort of animation, which is why we first reorder the nodes that will be removed. If the
|
|
|
+ // callback instead removes the nodes right away, it would be more efficient to skip reordering them.
|
|
|
+ // Perhaps we'll make that change in the future if this scenario becomes more common.
|
|
|
+ callCallback(options['beforeRemove'], itemsForBeforeRemoveCallbacks);
|
|
|
+
|
|
|
+ // Finally call afterMove and afterAdd callbacks
|
|
|
+ callCallback(options['afterMove'], itemsForMoveCallbacks);
|
|
|
+ callCallback(options['afterAdd'], itemsForAfterAddCallbacks);
|
|
|
+
|
|
|
+ // Store a copy of the array items we just considered so we can difference it next time
|
|
|
+ ko.utils.domData.set(domNode, lastMappingResultDomDataKey, newMappingResult);
|
|
|
+ }
|
|
|
+})();
|
|
|
+
|
|
|
+ko.exportSymbol('utils.setDomNodeChildrenFromArrayMapping', ko.utils.setDomNodeChildrenFromArrayMapping);
|
|
|
+ko.nativeTemplateEngine = function () {
|
|
|
+ this['allowTemplateRewriting'] = false;
|
|
|
+}
|
|
|
+
|
|
|
+ko.nativeTemplateEngine.prototype = new ko.templateEngine();
|
|
|
+ko.nativeTemplateEngine.prototype['renderTemplateSource'] = function (templateSource, bindingContext, options) {
|
|
|
+ var useNodesIfAvailable = !(ko.utils.ieVersion < 9), // IE<9 cloneNode doesn't work properly
|
|
|
+ templateNodesFunc = useNodesIfAvailable ? templateSource['nodes'] : null,
|
|
|
+ templateNodes = templateNodesFunc ? templateSource['nodes']() : null;
|
|
|
+
|
|
|
+ if (templateNodes) {
|
|
|
+ return ko.utils.makeArray(templateNodes.cloneNode(true).childNodes);
|
|
|
+ } else {
|
|
|
+ var templateText = templateSource['text']();
|
|
|
+ return ko.utils.parseHtmlFragment(templateText);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+ko.nativeTemplateEngine.instance = new ko.nativeTemplateEngine();
|
|
|
+ko.setTemplateEngine(ko.nativeTemplateEngine.instance);
|
|
|
+
|
|
|
+ko.exportSymbol('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
|
|
|
+ })();
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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}}";
|
|
|
+
|
|
|
+ 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']);
|
|
|
+
|
|
|
+ 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('jqueryTmplTemplateEngine', ko.jqueryTmplTemplateEngine);
|
|
|
+})();
|
|
|
+});
|
|
|
+})(window,document,navigator,window["jQuery"]);
|
|
|
})();
|