-
Notifications
You must be signed in to change notification settings - Fork 26
/
knockout-switch-case.js
209 lines (182 loc) · 9.27 KB
/
knockout-switch-case.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* @license SWITCH/CASE binding for Knockout http://knockoutjs.com/
* (c) Michael Best
* License: MIT (http://www.opensource.org/licenses/mit-license.php)
* Version 2.1.0
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['knockout'], factory);
} else {
// Browser globals
factory(root.ko);
}
}(this, function(ko) {
var undefined;
if (!ko.virtualElements)
throw Error('Switch-case requires at least Knockout 2.1');
var ko_virtualElements = ko.virtualElements;
var ko_bindingFlags = ko.bindingFlags || {};
var ko_bindingRewriteValidators = ko.bindingRewriteValidators || ko.jsonExpressionRewriting.bindingRewriteValidators;
var ko_unwrap = ko.utils.unwrapObservable;
var ko_bindingHandlers = ko.bindingHandlers;
var defaultvalue = {};
ko_bindingHandlers['switch'] = {
flags: ko_bindingFlags.contentBind | ko_bindingFlags.canUseVirtual | ko_bindingFlags.noValue,
init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
var switchSkipNextArray = [],
switchBindings = {
// these properties are internal
$switchSkipNextArray: switchSkipNextArray,
$switchValueAccessor: valueAccessor,
$switchDefault: ko.observable(true),
// these properties are public
$default: defaultvalue,
$else: defaultvalue
},
contexts = [];
// Update $value in each context when it changes
ko.computed(function() {
var value = ko_unwrap(valueAccessor());
switchBindings.$value = value;
ko.utils.arrayForEach(contexts, function(context) {
context.$value = value;
});
}, null, {disposeWhenNodeIsRemoved: element});
// Each child element gets a new binding context so it can set its own $switchIndex property.
// The other properties will be shared since they're objects.
var node, nextInQueue = ko_virtualElements.firstChild(element);
while (node = nextInQueue) {
nextInQueue = ko_virtualElements.nextSibling(node);
switch (node.nodeType) {
case 1: case 8:
var newContext = bindingContext.extend(switchBindings);
// Set initial value of context.$switchIndex to undefined
newContext.$switchIndex = undefined;
ko.applyBindings(newContext, node);
// Add the context to the list to be updated if this section contained a case binding
if (newContext.$switchIndex !== undefined) {
contexts.push(newContext);
}
break;
}
}
return { controlsDescendantBindings: true };
},
preprocess: function(value) {
return value || 'true';
}
};
ko_bindingRewriteValidators['switch'] = false; // Can't rewrite control flow bindings
ko_virtualElements.allowedBindings['switch'] = true;
function checkCase(value, bindingContext) {
// Check value and determine result:
// If the control value is boolean, the result is the matching truthiness of the value
// If value is boolean, the result is the value (allows expressions instead of just simple matching)
// If value is an array, the result is true if the control value matches (strict) an item in the array
// Otherwise, the result is true if value matches the control value (loose)
var switchValue = ko_unwrap(bindingContext.$switchValueAccessor());
return (typeof switchValue == 'boolean')
? (value ? switchValue : !switchValue)
: (typeof value == 'boolean')
? value
: (value instanceof Array)
? (ko.utils.arrayIndexOf(value, switchValue) !== -1)
: (value == switchValue);
}
function checkNotCase(value, bindingContext) {
return !checkCase(value, bindingContext);
}
function defaultMakeValueAccessor(ifValue) {
return function() { return ifValue };
}
function makeCaseHandler(binding, isNot, makeValueAccessor) {
var checkFunction = isNot ? checkNotCase : checkCase;
binding || (binding = 'if');
makeValueAccessor || (makeValueAccessor = defaultMakeValueAccessor);
return {
// Inherit flags from the binding we're wrapping
flags: ko_bindingHandlers[binding].flags,
init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
if (!bindingContext.$switchSkipNextArray)
throw Error("case binding must only be used with a switch binding");
if (bindingContext.$switchIndex !== undefined)
throw Error("case binding cannot be nested");
// Initialize $switchIndex and push a new observable to $switchSkipNextArray
bindingContext.$switchIndex = bindingContext.$switchSkipNextArray.push(ko.observable(false)) - 1;
bindingContext.$caseValue = ko.observable();
ko.computed(function() {
var index = bindingContext.$switchIndex,
isLast = (index === bindingContext.$switchSkipNextArray.length - 1),
result, skipNext, noDefault;
if (index && bindingContext.$switchSkipNextArray[index-1]()) {
// An earlier case binding matched: skip this one (and subsequent ones)
result = false;
skipNext = true;
} else {
var value = ko_unwrap(valueAccessor());
if (value === bindingContext.$else) {
// If value is the special object $else, the result depends on the other case values.
// If we're the last *case* item, the value must be true. $switchDefault will get
// updated to *true* below, but that won't necessarily update us because it would
// require a recursive update.
result = bindingContext.$switchDefault() || isLast;
skipNext = false;
} else {
// If result is true, we will skip the subsequent cases (and any default cases)
noDefault = skipNext = result = checkFunction(value, bindingContext);
}
}
// Set the observable used by the wrapped binding function
bindingContext.$caseValue(result);
// Update the observable "skip next" value; if the value is changed, this will update the
// subsequent case item.
bindingContext.$switchSkipNextArray[index](skipNext);
// Update $switchDefault to false if a non-default case item has matched.
// Update it to true if we're the last item and none of items have matched.
// (Initially, every item will be the last, but it doesn't matter.)
if (noDefault)
bindingContext.$switchDefault(false);
else if (!skipNext && isLast)
bindingContext.$switchDefault(true);
},
null, { disposeWhenNodeIsRemoved: element });
// Call init with the observable result value
if (ko_bindingHandlers[binding].init) {
return ko_bindingHandlers[binding].init(element,
makeValueAccessor(bindingContext.$caseValue), allBindings, viewModel, bindingContext);
}
},
update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
// Call update with the observable result value
if (ko_bindingHandlers[binding].update) {
return ko_bindingHandlers[binding].update(element,
makeValueAccessor(bindingContext.$caseValue), allBindings, viewModel, bindingContext);
}
}
};
}
// Support dynamically creating new case binding when using Punches plugin
function getNamespacedHandler(bindingName, namespace, bindingKey) {
if (ko_virtualElements.allowedBindings[bindingName])
ko_virtualElements.allowedBindings[bindingKey] = true;
return makeCaseHandler(bindingName, namespace === 'casenot');
}
// Support dynamically creating new case binding when using key.subkey plugin
function makeSubkeyHandler(baseKey, subKey, bindingKey) {
return getNamespacedHandler(subKey, baseKey, bindingKey);
}
function makeBaseHandler(name, isNot) {
ko_bindingHandlers[name] = makeCaseHandler('if', isNot);
ko_bindingRewriteValidators[name] = false; // Can't rewrite control flow bindings
ko_virtualElements.allowedBindings[name] = true;
ko_bindingHandlers[name].makeSubkeyHandler = makeSubkeyHandler;
ko_bindingHandlers[name].getNamespacedHandler = getNamespacedHandler;
}
makeBaseHandler('case');
makeBaseHandler('casenot', true /*isNot*/);
ko_bindingHandlers['case.visible'] = makeCaseHandler('visible');
ko_bindingHandlers['casenot.visible'] = makeCaseHandler('visible', true /*isNot*/);
ko_bindingHandlers['switch'].makeCaseHandler = makeCaseHandler;
}));