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 {
get: function(url, headers) {
return _fetch(url, 'GET', headers);
@ -44,6 +72,7 @@
return _fetch(url, 'POST', headers, body);
},
addResponseInterceptor: addResponseInterceptor,
parseHTML: parseHTML,
}
})();
})();

View File

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

View File

@ -125,9 +125,8 @@
url,
headers,
requestBody,
).then(response => HttpClient.parseHTML(response, element.id)
).then(function(response) {
return response.text();
}).then(function(response) {
processResponse(response);
if (isAddCell) {
reFocusAddCell();
@ -164,26 +163,16 @@
}
function processResponse(response) {
element.innerHTML = response;
element.innerHTML = "";
element.appendChild(response);
prefixInputIds();
reset()
reset();
if (UtilRegistry) {
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) {
var formData = new FormData(massInputForm);

View File

@ -168,14 +168,13 @@
}
HttpClient.get(url, MODAL_HEADERS)
.then(function(response) {
response.text().then(processResponse);
});
.then(response => HttpClient.parseHTML(response, element.id))
.then(processResponse);
}
function processResponse(responseBody) {
function processResponse(responseFrag) {
var responseElement = document.createElement('div');
responseElement.innerHTML = responseBody;
responseElement.appendChild(responseFrag);
var modalContent = document.createElement('div');
modalContent.classList.add(MODAL_CONTENT_CLASS);
@ -191,24 +190,12 @@
previousModalContent.remove();
}
modalContent = withPrefixedInputIDs(modalContent);
element.insertBefore(modalContent, null);
// setup any newly arrived utils
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();
};

View File

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