mirror of
https://github.com/the-djmaze/snappymail.git
synced 2024-12-26 09:03:48 +08:00
Added missing knockoutjs checked.js binding
This commit is contained in:
parent
08a082a190
commit
bc196bcd1a
1 changed files with 57 additions and 0 deletions
57
vendors/knockout/src/binding/defaultBindings/checked.js
vendored
Normal file
57
vendors/knockout/src/binding/defaultBindings/checked.js
vendored
Normal file
|
@ -0,0 +1,57 @@
|
|||
(()=>{
|
||||
|
||||
ko.bindingHandlers['checked'] = {
|
||||
'after': ['value', 'attr'],
|
||||
'init': function (element, valueAccessor, allBindings) {
|
||||
var isCheckbox = element.type == "checkbox",
|
||||
isRadio = element.type == "radio";
|
||||
|
||||
// Only bind to check boxes and radio buttons
|
||||
if (isCheckbox || isRadio) {
|
||||
const checkedValue = ko.pureComputed(()=>{
|
||||
if (isRadio) {
|
||||
return allBindings['has']('value')
|
||||
? ko.utils.unwrapObservable(allBindings.get('value'))
|
||||
: element.value;
|
||||
}
|
||||
});
|
||||
|
||||
// Set up two computeds to update the binding:
|
||||
|
||||
// The first responds to element clicks
|
||||
element.addEventListener("click", () => {
|
||||
// When we're first setting up this computed, don't change any model state.
|
||||
if (ko.dependencyDetection.isInitial()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// This updates the model value from the view value.
|
||||
// It runs in response to DOM events (click) and changes in checkedValue.
|
||||
var isChecked = element.checked;
|
||||
|
||||
// We can ignore unchecked radio buttons, because some other radio
|
||||
// button will be checked, and that one can take care of updating state.
|
||||
// Also ignore value changes to an already unchecked checkbox.
|
||||
if (!isChecked && (isRadio || ko.dependencyDetection.getDependenciesCount())) {
|
||||
return;
|
||||
}
|
||||
|
||||
var elemValue = isCheckbox ? isChecked : checkedValue(),
|
||||
modelValue = ko.dependencyDetection.ignore(valueAccessor);
|
||||
ko.expressionRewriting.writeValueToProperty(modelValue, allBindings, 'checked', elemValue, true);
|
||||
}),
|
||||
|
||||
// The second responds to changes in the model value (the one associated with the checked binding)
|
||||
ko.computed(() => {
|
||||
// This updates the view value from the model value.
|
||||
// It runs in response to changes in the bound (checked) value.
|
||||
var modelValue = ko.utils.unwrapObservable(valueAccessor());
|
||||
element.checked = isCheckbox ? !!modelValue : (checkedValue() === modelValue);
|
||||
}, null, { disposeWhenNodeIsRemoved: element });
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ko.expressionRewriting.twoWayBindings['checked'] = true;
|
||||
|
||||
})();
|
Loading…
Reference in a new issue