Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a abort button to editor form if autosave and autoform is true #1512

Merged
merged 2 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions src/controls/editor/editform.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@ const createForm = function createForm(obj) {
const maxLengthText = maxLength ? `, max ${obj.maxLength} tecken` : '';
switch (type) {
case 'text':
el = `<div class="validate ${cls}"><label>${label}</label><br><input type="text" name="text${maxLengthText}" id="${id}" value="${val}"${maxLength}${readonly}${required}></div>`;
el = `<div class="validate ${cls}"><label>${label}<br><input type="text" name="text${maxLengthText}" id="${id}" value="${val}"${maxLength}${readonly}${required}></label></div>`;
break;
case 'textarea':
el = `<div class="validate ${cls}"><label>${label}</label><br><textarea name="textarea${maxLengthText}" id="${id}" rows="3" ${maxLength}${readonly}${required}>${val}</textarea></div>`;
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}</label><input type="checkbox" id="${id}" value="${val}"${checked}${disabled}></div>`;
el = `<div class="o-form-checkbox ${cls}"><label for="${id}">${label}<input type="checkbox" id="${id}" value="${val}"${checked}${disabled}></label></div>`;
break;
case 'dropdown':
if (val) {
firstOption = `<option value="${val}">${val}</option>`;
} else {
firstOption = '<option value="">Välj</option>';
}
el = `<div class="validate ${cls}"><label>${label}</label><br><select id=${id}${disabled}${required}>${firstOption}`;
el = `<div class="validate ${cls}"><label>${label}<br><select id=${id}${disabled}${required}>${firstOption}`;
for (let i = 0; i < dropdownOptions.length; i += 1) {
el += `<option value="${dropdownOptions[i]}">${dropdownOptions[i]}</option>`;
}
el += '</select></div>';
el += '</select></label></div>';
break;
case 'searchList':
elDiv.id = obj.type;
Expand Down Expand Up @@ -77,10 +77,10 @@ const createForm = function createForm(obj) {
break;
case 'image': {
const imageClass = val ? '' : 'o-hidden';
el = `<div class="validate ${cls}"><label>${label}</label><br>`;
el = `<div class="validate ${cls}"><label>${label}<br>`;
el += `<img src="${val}" id="image-upload" class="${imageClass}"/>`;
el += `<input type="file" name="bildfil" id="${id}" value="${val}" accept="image/*"${disabled}>`;
el += `<input id="o-delete-image-button" class="${imageClass}" type="button" value="Ta bort bild"${disabled}>`;
el += `<input type="file" name="bildfil" id="${id}" value="${val}" accept="image/*"${disabled}></label>`;
el += `<input id="o-delete-image-button" class="${imageClass}" type="button" aria-label="Ta bort bild" value="Ta bort bild"${disabled}>`;
el += '</div>';
break;
}
Expand All @@ -95,7 +95,7 @@ const createForm = function createForm(obj) {
if (val.length > 10) {
val = val.slice(0, 10);
}
el = `<div class="validate ${cls}"><label>${label}</label><br><input type="date" name="datum" id="${id}" placeholder="åååå-MM-dd" value="${val}"${readonly}${required}></div>`;
el = `<div class="validate ${cls}"><label>${label}<br><input type="date" name="datum" id="${id}" placeholder="åååå-MM-dd" value="${val}"${readonly}${required}></label></div>`;
break;
case 'time':
if (!val) {
Expand All @@ -110,7 +110,7 @@ const createForm = function createForm(obj) {
} else if (val.length > 8) {
val = val.slice(11, 19);
}
el = `<div class="validate ${cls}"><label>${label}</label><br><input type="time" name="timmar, minuter och sekunder" id="${id}" step="1" placeholder="--:--:--" value="${val}"${readonly}${required}></div>`;
el = `<div class="validate ${cls}"><label>${label}<br><input type="time" name="timmar, minuter och sekunder" id="${id}" step="1" placeholder="--:--:--" value="${val}"${readonly}${required}></label></div>`;
break;
case 'datetime':
if (!val) {
Expand All @@ -125,25 +125,25 @@ const createForm = function createForm(obj) {
} else if (val.length > 19) {
val = val.slice(0, 19);
}
el = `<div class="validate"><label>${label}</label><br><input type="datetime-local" name="datum och tid" id="${id}" step="1" placeholder="åååå-MM-dd --:--:--" value="${val}"${readonly}${required}></div>`;
el = `<div class="validate"><label>${label}<br><input type="datetime-local" name="datum och tid" id="${id}" step="1" placeholder="åååå-MM-dd --:--:--" value="${val}"${readonly}${required}></label></div>`;
break;
case 'color':
if (!val) {
val = obj.defaultColor ? obj.defaultColor : '';
}
el = `<div class="validate ${cls}"><label>${label}</label><br><input type="color" name="hexadecimal" id="${id}" value="${val}"${readonly}></div>`;
el = `<div class="validate ${cls}"><label>${label}<br><input type="color" name="hexadecimal" id="${id}" value="${val}"${readonly}></label></div>`;
break;
case 'email':
el = `<div class="validate ${cls}"><label>${label}</label><br><input type="email" name="epost" id="${id}" value="${val}"${readonly}${required}></div>`;
el = `<div class="validate ${cls}"><label>${label}<br><input type="email" name="epost" id="${id}" value="${val}"${readonly}${required}></label></div>`;
break;
case 'url':
el = `<div class="validate ${cls}"><label>${label}</label><br><input type="url" name="hemsida" id="${id}" value="${val}"${readonly}${required}></div>`;
el = `<div class="validate ${cls}"><label>${label}<br><input type="url" name="hemsida" id="${id}" value="${val}"${readonly}${required}></label></div>`;
break;
case 'integer':
el = `<div class="validate ${cls}"><label>${label}</label><br><input type="number" step="1" min="0" name="heltal" id="${id}" value="${val}"${readonly}${required}></div>`;
el = `<div class="validate ${cls}"><label>${label}<br><input type="number" step="1" min="0" name="heltal" id="${id}" value="${val}"${readonly}${required}></label></div>`;
break;
case 'decimal':
el = `<div class="validate ${cls}"><label>${label}</label><br><input type="number" step="0.01" min="0" name="decimaltal" id="${id}" value="${val}"${readonly}${required}></div>`;
el = `<div class="validate ${cls}"><label>${label}<br><input type="number" step="0.01" min="0" name="decimaltal" id="${id}" value="${val}"${readonly}${required}></label></div>`;
break;
case 'hidden':
el = `<input type="hidden ${cls}" id="${id}" value="${val}">`;
Expand Down
29 changes: 28 additions & 1 deletion src/controls/editor/edithandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ let allowEditAttributes;
let allowEditGeometry;
/** List that tracks the state when editing related tables */
let breadcrumbs = [];
let autoCreatedFeature = false;

function isActive() {
if (modify === undefined || select === undefined) {
Expand Down Expand Up @@ -216,6 +217,7 @@ async function addFeature(feature) {
hasDraw = false;
dispatcher.emitChangeEdit('draw', false);
if (autoForm) {
autoCreatedFeature = true;
// eslint-disable-next-line no-use-before-define
editAttributes(feature);
}
Expand Down Expand Up @@ -671,6 +673,26 @@ function attributesSaveHandler(features, formEl) {
}
}

/**
* Sets up an eventlistener on the attribute editor form abort button.
* @param {Collection} features The features that shouldn't be updated
*/
function onAttributesAbort(features) {
const abortBtnEl = document.getElementById(`o-abort-button-${currentLayer}`);
if (abortBtnEl !== null) {
abortBtnEl.addEventListener('click', (e) => {
abortBtnEl.blur();
features.forEach((feature) => {
deleteFeature(feature, editLayers[currentLayer]).then(() => select.getFeatures().clear());
});
modal.closeModal();
// The modal does not fire close event when it is closed externally
onModalClosed();
e.preventDefault();
});
}
}

/**
* Sets up an eventlistener on the attribute editor form save button.
* @param {Collection} features The features that should be updated
Expand Down Expand Up @@ -1075,7 +1097,11 @@ function editAttributes(feat) {
attachmentsForm = `<div id="o-attach-form-${currentLayer}"></div>`;
}

const form = `<div id="o-form">${formElement}${relatedTablesFormHTML}${attachmentsForm}<br><div class="o-form-save"><input id="o-save-button-${currentLayer}" type="button" value="Ok"></input></div></div>`;
let form = `<div id="o-form">${formElement}${relatedTablesFormHTML}${attachmentsForm}<br><div class="o-form-save"><input id="o-save-button-${currentLayer}" type="button" value="OK" aria-label="OK"></input></div></div>`;
if (autoCreatedFeature) {
form = `<div id="o-form">${formElement}${relatedTablesFormHTML}${attachmentsForm}<br><div class="o-form-save"><input id="o-save-button-${currentLayer}" type="button" value="Spara" aria-label="Spara"></input><input id="o-abort-button-${currentLayer}" type="button" value="Ta bort" aria-label="Ta bort"></input></div></div>`;
autoCreatedFeature = false;
}

modal = Modal({
title: dlgTitle,
Expand Down Expand Up @@ -1125,6 +1151,7 @@ function editAttributes(feat) {
});

onAttributesSave(features, attributeObjects);
onAttributesAbort(features);
}
}

Expand Down