From 547d934379c51959af0a833b81c0be71c7c82f7a Mon Sep 17 00:00:00 2001 From: Joscha Rohmann Date: Thu, 7 Jan 2016 23:20:39 +0100 Subject: [PATCH] Implementation of checkbox groups. The 'checked'/'attr'-data-query with 'checked' as attribute will accept arrays/array-observables as the 'value' parameter and try to find their value in the array to set the value and add their value to the array when checked. --- src/query/DomQuery.js | 11 ++++++++++- src/query/VirtualElement.js | 8 ++++++-- src/query/dom.js | 6 +++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/query/DomQuery.js b/src/query/DomQuery.js index 1065edd..eaf796e 100644 --- a/src/query/DomQuery.js +++ b/src/query/DomQuery.js @@ -359,7 +359,16 @@ define([ var tagName = element.tagName.toLowerCase(); var type = element.getAttribute('type'); - if (type == 'checkbox') { + if (blocks.isArray(value()) && (type == 'checkbox' || type == 'radio')) { + var unwrapedValue = value(); + if (element.checked) { + if (unwrapedValue.indexOf(element.value) == -1) { + value.add(element.value); + } + } else if (unwrapedValue.indexOf(element.value) !== -1) { + value.splice(unwrapedValue.indexOf(element.value), 1); + } + } else if (type == 'checkbox') { value(element.checked); } else if (tagName == 'select' && element.getAttribute('multiple') !== null) { var values = []; diff --git a/src/query/VirtualElement.js b/src/query/VirtualElement.js index c551282..b3f7fd3 100644 --- a/src/query/VirtualElement.js +++ b/src/query/VirtualElement.js @@ -150,11 +150,15 @@ define([ if (attributeName == 'checked' && attributeValue != null && !this._fake) { if (this._attributes.type == 'radio' && typeof attributeValue == 'string' && - value != attributeValue && value != null) { + value != attributeValue && value != null && !blocks.isArray(attributeValue)) { attributeValue = null; } else { - attributeValue = attributeValue ? 'checked' : null; + if (blocks.isArray(attributeValue)) { + attributeValue = attributeValue.indexOf(value) !== -1 ? 'checked' : null; + } else { + attributeValue = attributeValue ? 'checked' : null; + } } } else if (attributeName == 'disabled') { attributeValue = attributeValue ? 'disabled' : null; diff --git a/src/query/dom.js b/src/query/dom.js index c9faada..8c25005 100644 --- a/src/query/dom.js +++ b/src/query/dom.js @@ -141,7 +141,11 @@ define([ attributeValue = false; } else { - attributeValue = !!attributeValue; + if (blocks.isArray(attributeValue)) { + attributeValue = attributeValue.indexOf(element.value) !== -1; + } else { + attributeValue = !!attributeValue; + } } }