Implement generic id-nudging and use everywhere

This commit is contained in:
Gregor Kleen 2019-05-15 16:12:00 +02:00
parent e712552397
commit 3dcb5a2b19
5 changed files with 43 additions and 41 deletions

View File

@ -36,6 +36,34 @@
}); });
} }
function parseHTML(response, idPrefix) {
if (!idPrefix) {
idPrefix = Math.floor(Math.random() * 100000);
}
var contentType = response.headers.get("content-type");
if (contentType.indexOf("text/html") === -1) {
throw new Error('Server returned ' + contentType + ' when HTML was expected');
}
return response.text().then(function (responseText) {
var docFrag = document.createRange().createContextualFragment(responseText);
var idAttrs = ['id', 'for', 'data-conditional-input', 'data-modal-trigger'];
idAttrs.forEach(function(attr) {
Array.from(docFrag.querySelectorAll('[' + attr + ']')).forEach(function(input) {
var value = idPrefix + '__' + input.getAttribute(attr);
input.setAttribute(attr, value);
});
});
return Promise.resolve(docFrag);
},
function (error) {
return Promise.reject(error);
}).catch(function (error) { console.error(error); });
}
return { return {
get: function(url, headers) { get: function(url, headers) {
return _fetch(url, 'GET', headers); return _fetch(url, 'GET', headers);
@ -44,6 +72,7 @@
return _fetch(url, 'POST', headers, body); return _fetch(url, 'POST', headers, body);
}, },
addResponseInterceptor: addResponseInterceptor, addResponseInterceptor: addResponseInterceptor,
parseHTML: parseHTML,
} }
})(); })();
})(); })();

View File

@ -319,12 +319,9 @@
[asyncTableHeader]: asyncTableId [asyncTableHeader]: asyncTableId
}; };
HttpClient.get(url, headers).then(function(response) { HttpClient.get(url, headers).then(
if (!response.ok) { response => HttpClient.parseHTML(response, element.id)
throw new Error('Looks like there was a problem fetching ' + url.href + '. Status Code: ' + response.status); ).then(function(data) {
}
return response.text();
}).then(function(data) {
setLocalStorageParameter('currentTableUrl', url.href); setLocalStorageParameter('currentTableUrl', url.href);
// reset table // reset table
removeListeners(); removeListeners();
@ -345,8 +342,8 @@
function updateWrapperContents(newHtml) { function updateWrapperContents(newHtml) {
var newPage = document.createElement('div'); var newPage = document.createElement('div');
newPage.innerHTML = newHtml; newPage.appendChild(newHtml);
var newWrapperContents = newPage.querySelector('#' + element.id); var newWrapperContents = newPage.querySelector('#' + element.id + '__' + element.id);
element.innerHTML = newWrapperContents.innerHTML; element.innerHTML = newWrapperContents.innerHTML;
if (UtilRegistry) { if (UtilRegistry) {

View File

@ -125,9 +125,8 @@
url, url,
headers, headers,
requestBody, requestBody,
).then(response => HttpClient.parseHTML(response, element.id)
).then(function(response) { ).then(function(response) {
return response.text();
}).then(function(response) {
processResponse(response); processResponse(response);
if (isAddCell) { if (isAddCell) {
reFocusAddCell(); reFocusAddCell();
@ -164,26 +163,16 @@
} }
function processResponse(response) { function processResponse(response) {
element.innerHTML = response; element.innerHTML = "";
element.appendChild(response);
prefixInputIds(); reset();
reset()
if (UtilRegistry) { if (UtilRegistry) {
UtilRegistry.setupAll(element); UtilRegistry.setupAll(element);
} }
} }
function prefixInputIds() {
var idAttrs = ['id', 'for', 'data-conditional-input'];
idAttrs.forEach(function(attr) {
Array.from(element.querySelectorAll('[' + attr + ']')).forEach(function(input) {
var value = element.id + '__' + input.getAttribute(attr);
input.setAttribute(attr, value);
});
});
}
function serializeForm(submitButton, enctype) { function serializeForm(submitButton, enctype) {
var formData = new FormData(massInputForm); var formData = new FormData(massInputForm);

View File

@ -168,14 +168,13 @@
} }
HttpClient.get(url, MODAL_HEADERS) HttpClient.get(url, MODAL_HEADERS)
.then(function(response) { .then(response => HttpClient.parseHTML(response, element.id))
response.text().then(processResponse); .then(processResponse);
});
} }
function processResponse(responseBody) { function processResponse(responseFrag) {
var responseElement = document.createElement('div'); var responseElement = document.createElement('div');
responseElement.innerHTML = responseBody; responseElement.appendChild(responseFrag);
var modalContent = document.createElement('div'); var modalContent = document.createElement('div');
modalContent.classList.add(MODAL_CONTENT_CLASS); modalContent.classList.add(MODAL_CONTENT_CLASS);
@ -191,24 +190,12 @@
previousModalContent.remove(); previousModalContent.remove();
} }
modalContent = withPrefixedInputIDs(modalContent);
element.insertBefore(modalContent, null); element.insertBefore(modalContent, null);
// setup any newly arrived utils // setup any newly arrived utils
UtilRegistry.setupAll(element); UtilRegistry.setupAll(element);
} }
function withPrefixedInputIDs(modalContent) {
var idAttrs = ['id', 'for', 'data-conditional-input'];
idAttrs.forEach(function(attr) {
Array.from(modalContent.querySelectorAll('[' + attr + ']')).forEach(function(input) {
var value = element.id + '__' + input.getAttribute(attr);
input.setAttribute(attr, value);
});
});
return modalContent;
}
return _init(); return _init();
}; };

View File

@ -1,5 +1,5 @@
$newline never $newline never
<div uw-modal data-modal-trigger=##{triggerId'} data-modal-closeable> <div uw-modal data-modal-trigger=#{triggerId'} data-modal-closeable>
$case modalContent $case modalContent
$of Right content $of Right content
<div .modal__content> <div .modal__content>