Skip to content

Commit

Permalink
Merge pull request #1533 from ornskoldsvikskommun/editor-checkbox-value
Browse files Browse the repository at this point in the history
Editor checkbox value
  • Loading branch information
johnnyblasta authored Jun 1, 2022
2 parents 94bcf75 + 46fda16 commit a46dce2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/controls/editor/editform.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const createForm = function createForm(obj) {
const required = obj.required ? ' required' : '';
const name = obj.name ? obj.name : '';
let el;
let checked;
let firstOption;
let checked;
const maxLengthText = maxLength ? `, max ${obj.maxLength} tecken` : '';
switch (type) {
case 'text':
Expand All @@ -31,8 +31,8 @@ const createForm = function createForm(obj) {
el = `<div class="validate ${cls}"><label>${label}<br><textarea name="textarea${maxLengthText}" id="${id}" rows="3" ${maxLength}${readonly}${required}>${val}</textarea></label></div>`;
break;
case 'checkbox':
checked = val ? ' checked' : '';
el = `<div class="o-form-checkbox ${cls}"><label for="${id}">${label}<input type="checkbox" id="${id}" value="${val}"${checked}${disabled}></label></div>`;
checked = (obj.config && obj.config.uncheckedValue ? obj.config.uncheckedValue !== val : val) ? ' checked' : '';
el = `<div class="o-form-checkbox ${cls}"><label for="${id}"><input type="checkbox" id="${id}" value="${val}"${checked}${disabled}/>${label}</label></div>`;
break;
case 'dropdown':
if (val) {
Expand Down
19 changes: 14 additions & 5 deletions src/controls/editor/edithandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function getFeaturesByIds(type, layer, ids) {
/**
* Helper that calculates the default value for one attribute
* @param {any} attribConf The list entry from "attributes"-configuration that default value should be calculated for
* @returns The default value for provided attribute
* @returns The default value for provided attribute or undefined if no default value
*/
function getDefaultValueForAttribute(attribConf) {
const defaultsConfig = attribConf.defaultValue;
Expand Down Expand Up @@ -150,8 +150,12 @@ function getDefaultValueForAttribute(attribConf) {
return isoDate.slice(0, 19);
}
}
} else if (attribConf.type === 'checkbox' && attribConf.config && attribConf.config.uncheckedValue) {
// Checkboxes defaults to unchecked value if no default value is specified. If no uncheckedValue is specified it
// will default to unchecked by some magic javascript falsly comparison later.
return attribConf.config.uncheckedValue;
}
// Consistent return
// This attribute has no default value
return undefined;
}

Expand All @@ -161,10 +165,13 @@ function getDefaultValueForAttribute(attribConf) {
* @returns {object} An object with attributes names as properties and the default value as value.
*/
function getDefaultValues(attrs) {
return attrs.filter(attribute => attribute.name && attribute.defaultValue)
return attrs.filter(attribute => attribute.name)
.reduce((prev, curr) => {
const previous = prev;
previous[curr.name] = getDefaultValueForAttribute(curr);
const defaultValue = getDefaultValueForAttribute(curr);
if (defaultValue !== undefined) {
previous[curr.name] = defaultValue;
}
return previous;
}, {});
}
Expand Down Expand Up @@ -768,7 +775,9 @@ function onAttributesSave(features, attrs) {
if (!document.querySelector(containerClass) || document.querySelector(containerClass).classList.contains('o-hidden') === false) {
// Check if checkbox. If checkbox read state.
if (inputType === 'checkbox') {
editEl[attribute.name] = document.getElementById(attribute.elId).checked ? 1 : 0;
const checkedValue = (attribute.config && attribute.config.checkedValue) || 1;
const uncheckedValue = (attribute.config && attribute.config.uncheckedValue) || 0;
editEl[attribute.name] = document.getElementById(attribute.elId).checked ? checkedValue : uncheckedValue;
} else { // Read value from input text, textarea or select
editEl[attribute.name] = inputValue;
}
Expand Down

0 comments on commit a46dce2

Please sign in to comment.