-
Notifications
You must be signed in to change notification settings - Fork 0
/
koTristateCheckboxes.js
375 lines (313 loc) · 13.2 KB
/
koTristateCheckboxes.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
;(function(window, ko, undefined) {
/////////// HELPER FUNCTIONS ///////////
//full credit goes to http://johndyer.name/native-browser-get-set-properties-in-javascript/ (15.02.2013)
// Super amazing, cross browser property function, based on http://thewikies.com/
function addProperty(obj, name, onGet, onSet) {
// wrapper functions
var oldValue = obj[name], getFn = function() {
return onGet.apply(obj, [oldValue]);
}, setFn = function(newValue) {
return oldValue = onSet.apply(obj, [newValue]);
};
// Modern browsers, IE9+, and IE8 (must be a DOM object),
if (Object.defineProperty) {
Object.defineProperty(obj, name, {
get : getFn,
set : setFn
});
// Older Mozilla
} else if (obj.__defineGetter__) {
obj.__defineGetter__(name, getFn);
obj.__defineSetter__(name, setFn);
// IE6-7
// must be a real DOM object (to have attachEvent) and must be attached to document (for onpropertychange to fire)
} else {
var onPropertyChange = function(e) {
if (event.propertyName == name) {
// temporarily remove the event so it doesn't fire again and create a loop
obj.detachEvent("onpropertychange", onPropertyChange);
// get the changed value, run it through the set function
var newValue = setFn(obj[name]);
// restore the get function
obj[name] = getFn;
obj[name].toString = getFn;
// restore the event
obj.attachEvent("onpropertychange", onPropertyChange);
}
};
obj[name] = getFn;
obj[name].toString = getFn;
obj.attachEvent("onpropertychange", onPropertyChange);
}
};
//full credit goes to http://stackoverflow.com/a/1955160 (17.02.2013)
function getStyle(el, styleProp) {
var camelize = function(str) {
return str.replace(/\-(\w)/g, function(str, letter) {
return letter.toUpperCase();
});
};
if (el.currentStyle) {
return el.currentStyle[camelize(styleProp)];
} else if (document.defaultView && document.defaultView.getComputedStyle) {
return document.defaultView.getComputedStyle(el, null).getPropertyValue(styleProp);
} else {
return el.style[camelize(styleProp)];
}
};
/////////// TRISTATE BOOLEAN ///////////
var getValidTristateBooleanValue = function(val) {
switch(typeof val) {
case "boolean":
case "undefined":
return val;
break;
default:
if (val === null) {
return undefined;
} else {
return !!val;
}
}
};
var isTristateBoolean = function(_val) {
return typeof _val === "function" && _val.__tristate__;
};
var unwrapTristateBoolean = function(bool, peek) {
var b;
if (peek && ko.isObservable(bool)) {
b = bool.peek();
} else {
b = ko.utils.unwrapObservable(bool);
}
return isTristateBoolean(b) ? b() : b;
}
var triToState = {
"true" : 1,
"undefined" : 0.5,
"false" : 0
};
var stateToTri = {
"1" : true,
"0.5" : undefined,
"0" : false
};
var tristateBoolean = function(initialValue, defaultVal) {
var _latestValHelper = initialValue;
var _latestVal = function(peek) {
if (!peek && !ko.isObservable(_latestValHelper)) {
ko.dependencyDetection.registerDependency(tristate);
}
return getValidTristateBooleanValue(unwrapTristateBoolean(_latestValHelper, peek));
};
var _defaultValHelper = defaultVal;
var _defaultVal = function(peek) {
if (!peek && !ko.isObservable(_latestValHelper)) {
ko.dependencyDetection.registerDependency(tristate);
}
return !!unwrapTristateBoolean(_defaultValHelper, peek);
};
function tristate() {
if (arguments.length > 0) {
//write
if (ko.isObservable(_latestValHelper)) {
_latestValHelper(arguments[0]);
} else if ((!tristate['equalityComparer']) || !tristate['equalityComparer'](_latestValHelper, arguments[0])) {
tristate.valueWillMutate();
_latestValHelper = arguments[0];
tristate.valueHasMutated();
}
return this;
} else {
//read
return _latestVal();
}
};
tristate.defaultValue = function() {
if (arguments.length > 0) {
//write
if (ko.isObservable(_defaultValHelper)) {
_defaultValHelper(arguments[0])
} else if ((!tristate['equalityComparer']) || !tristate['equalityComparer'](_defaultValHelper, arguments[0])) {
tristate.valueWillMutate();
_defaultValHelper = arguments[0];
tristate.valueHasMutated();
}
return this;
} else {
//read
return _defaultVal();
}
};
tristate.hasValue = function() {
return typeof _latestVal() === "boolean";
};
tristate.getWithDefaultifNull = function() {
//to make a valueChange event also happen when default is changed
_defaultVal();
return typeof _latestVal() === "boolean" ? _latestVal() : arguments.length ? !!unwrapTristateBoolean(arguments[0]) : _defaultVal();
};
tristate.__tristate__ = true;
tristate.and = function(bool) {
return stateToTri[Math.min(triToState[_latestVal()], triToState[getValidTristateBooleanValue(unwrapTristateBoolean(bool))])];
};
tristate.or = function(bool) {
return stateToTri[Math.max(triToState[_latestVal()], triToState[getValidTristateBooleanValue(unwrapTristateBoolean(bool))])];
};
tristate.not = function() {
return stateToTri[1 - triToState[_latestVal()]];
}
//immitate ko.observable behaviour
ko.subscribable.call(tristate);
tristate.peek = function() {
return _latestVal(true);
};
tristate.valueHasMutated = function() {
tristate["notifySubscribers"](_latestVal(true));
}
tristate.valueWillMutate = function() {
tristate["notifySubscribers"](_latestVal(true), "beforeChange");
}
ko.utils.extend(tristate, ko.observable['fn']);
ko.exportProperty(tristate, 'peek', tristate.peek);
ko.exportProperty(tristate, "valueHasMutated", tristate.valueHasMutated);
ko.exportProperty(tristate, "valueWillMutate", tristate.valueWillMutate);
tristate[ko.observable.protoProperty] = tristateBoolean;
return tristate;
};
tristateBoolean[ko.observable.protoProperty] = ko.observable;
//exports
ko["isTristateBoolean"] = isTristateBoolean;
ko["utils"]["unwrapTristateBoolean"] = unwrapTristateBoolean;
ko["utils"]["getValidTristateBooleanValue"] = getValidTristateBooleanValue;
ko["tristateBoolean"] = tristateBoolean;
/////////// TRISTATE INPUT ELEMENT CREATION ///////////
createTristateCheckbox = function(element, fontSizeInitValue) {
if (element.nodeType !== 1 || element.nodeName.toLowerCase() !== "input" || element.type !== "checkbox") {
return false;
}
var cleanRegEx = /^, ?|, ?(?=,)|, ?$/gm;
var elemBinding = element.getAttribute("data-bind") || "";
var withBinding = elemBinding.match(/with ?: ?(?:[^\{][^,\s]*[^\}\s,]|\{(?:\S|\s)*\}) ?/g);
elemBinding = elemBinding.replace(cleanRegEx, '');
elemBinding = elemBinding.replace(/(text|html|foreach|if|ifnot|template) ?: ?(?:[^\{][^,\s]*[^\}\s,]|\{(?:\S|\s)*\}) ?/g, '');
elemBinding = elemBinding.replace(cleanRegEx, '');
var rewrittenElemBinding = elemBinding.replace(/(tristate|value|click|event|submit|enable|disable|hasfocus|checked|options|selectedOptions|uniqueName) ?: ?(?:[^\{][^,\s]*[^\}\s,]|\{(?:\S|\s)*\}) ?/g, '');
rewrittenElemBinding = rewrittenElemBinding.replace(cleanRegEx, '');
if (withBinding != null) {
rewrittenElemBinding += (/^\s*$/.test(rewrittenElemBinding) ? '' : ', ') + withBinding[0];
}
var parent = element.parentNode;
var tristateCont = document.createElement('span');
tristateCont.setAttribute("class", "tristate-checkbox");
rewrittenElemBinding && tristateCont.setAttribute("data-bind", rewrittenElemBinding);
var tristateBox = document.createElement('span');
tristateBox.setAttribute("class", "tristateBox");
var overlay = document.createElement('span');
overlay.setAttribute("class", "tristateOverlay");
tristateBox.appendChild(overlay);
tristateCont.appendChild(tristateBox);
parent.replaceChild(tristateCont, element);
tristateCont.insertBefore(element, tristateBox);
if (elemBinding) {
element.setAttribute("data-bind", elemBinding)
} else {
element.removeAttribute("data-bind")
};
var style = getStyle(overlay, 'font-size'), parsed;
if (!style || !( parsed = parseFloat(style, 10)) || parsed < 1) {
tristateCont.style.fontSize = fontSizeInitValue || "16px";
}
if(parsed && parsed < 8){
tristateCont.style.fontSize = fontSizeInitValue || "8px";
}
style = getStyle(overlay, 'font-size');
if(fontSizeInitValue && (!( parsed = parseFloat(style, 10)) || parsed < 8)){
tristateCont.style.fontSize = "8px";
}
return true;
}
/////////// KNOCKOUT BINDINGHANDLER STUFF ///////////
/**
*
* <input type="checkbox" data-bind="tristate: someTristate" />
*
*/
var flipNext = {
"undefined" : true,
"false" : undefined,
"true" : false
};
var initFunc = function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var setAppropriateState = function() {
};
var fontSizeInitValue = ko.utils.unwrapObservable(allBindingsAccessor().tristateFontSize);
var tristate = ko.computed({
read : function() {
return valueAccessor();
},
write : function(nv) {
var _v = valueAccessor();
_v(nv);
},
disposeWhenNodeIsRemoved : element
});
if (!ko.utils.domData.get(element, "isInited") && isTristateBoolean(tristate.peek()) && createTristateCheckbox(element, fontSizeInitValue)) {
ko.utils.domData.set(element, "isInited", true);
var flip = tristate.peek()();
( setAppropriateState = function(e) {
if("___setState" in e){
flip = e.___setState;
} else {
flip = flipNext[flip];
}
tristate.peek()(flip);
switch(flip) {
case true:
element.setAttribute('checked', 'checked');
element.removeAttribute('tristate');
break;
case false:
element.removeAttribute('checked');
element.removeAttribute('tristate');
break;
case undefined:
element.setAttribute('tristate', 'tristate');
element.removeAttribute('checked');
break;
}
return true;
})({
___setState : flip
});
tristate.subscribe(function(nv) {
setAppropriateState({
___setState : nv()
})
});
ko.utils.registerEventHandler(element, 'change', setAppropriateState);
addProperty(element, "tristate", function() {
return tristate.peek();
}, function(newTristate) {
if (isTristateBoolean(newTristate)) {
tristate(newTristate);
} else {
tristate.peek()(newTristate);
}
})
addProperty(element, "checked", function() {
return tristate.peek().getWithDefaultifNull();
}, function(newValue) {
tristate.peek()(newValue);
});
addProperty(element, "checkedAsTristate", function() {
return tristate.peek();
}, function(newValue) {
tristate.peek()(newValue);
});
}
}
ko.bindingHandlers["tristate"] = {
"init" : initFunc
};
})(window, ko);