forked from mbest/knockout-switch-case
-
Notifications
You must be signed in to change notification settings - Fork 0
/
knockout-switch-case.min.js
11 lines (11 loc) · 2.39 KB
/
knockout-switch-case.min.js
1
2
3
4
5
6
7
8
9
10
11
/*
SWITCH/CASE binding for Knockout http://knockoutjs.com/
(c) Michael Best
License: MIT (http://www.opensource.org/licenses/mit-license.php)
Version 2.0.1
*/
(function(b,h){"function"===typeof define&&define.amd?define(["knockout"],h):h(b.ko)})(this,function(b){function h(a,c){var f=p(c.$switchValueAccessor());return"boolean"==typeof f?a?f:!f:"boolean"==typeof a?a:a instanceof Array?-1!==b.utils.arrayIndexOf(a,f):a==f}function v(a,b){return!h(a,b)}function w(a){return function(){return a}}function g(a,c,f){var q=c?v:h;a||(a="if");f||(f=w);return{flags:d[a].flags,init:function(f,c,q,k,e){if(!e.$switchSkipNextArray)throw Error("case binding must only be used with a switch binding");
if(e.$switchIndex!==m)throw Error("case binding cannot be nested");e.$switchIndex=e.$switchSkipNextArray.push(b.observable(!1))-1;if(d[a].init)return d[a].init(f,function(){return{}},q,k,e)},update:function(b,c,k,h,e){var g=e.$switchIndex,m=g===e.$switchSkipNextArray.length-1,n,l;g&&e.$switchSkipNextArray[g-1]()?(c=!1,n=!0):(c=p(c()),c===e.$else?(c=e.$switchDefault()||m,n=!1):l=n=c=q(c,e));d[a].update(b,f(c),k,h,e);e.$switchSkipNextArray[g](n);l?e.$switchDefault(!1):!n&&m&&e.$switchDefault(!0)}}}
function r(a,c,b){k.allowedBindings[a]&&(k.allowedBindings[b]=!0);return g(a,"casenot"===c)}function x(a,c,b){return r(c,a,b)}function s(a,b){d[a]=g("if",b);t[a]=!1;k.allowedBindings[a]=!0;d[a].makeSubkeyHandler=x;d[a].getNamespacedHandler=r}var m;if(!b.virtualElements)throw Error("Switch-case requires at least Knockout 2.1");var k=b.virtualElements,l=b.bindingFlags||{},t=b.bindingRewriteValidators||b.jsonExpressionRewriting.bindingRewriteValidators,p=b.utils.unwrapObservable,d=b.bindingHandlers,
u={};d["switch"]={flags:l.contentBind|l.canUseVirtual|l.noValue,init:function(a,c,f,d,g){var h={$switchSkipNextArray:[],$switchValueAccessor:c,$switchDefault:b.observable(!0),$default:u,$else:u},l=[];b.computed(function(){var a=p(c());h.$value=a;b.utils.arrayForEach(l,function(b){b.$value=a})},null,{disposeWhenNodeIsRemoved:a});for(f=k.firstChild(a);a=f;)switch(f=k.nextSibling(a),a.nodeType){case 1:case 8:d=g.extend(h),d.$switchIndex=m,b.applyBindings(d,a),d.$switchIndex!==m&&l.push(d)}return{controlsDescendantBindings:!0}},
preprocess:function(a){return a||"true"}};t["switch"]=!1;k.allowedBindings["switch"]=!0;s("case");s("casenot",!0);d["case.visible"]=g("visible");d["casenot.visible"]=g("visible",!0);d["switch"].makeCaseHandler=g});