update: making join leave works

This commit is contained in:
Jean-Baptiste Pasquier
2019-11-14 13:27:04 +01:00
parent a342e5d431
commit 0c3b74bcaf
10 changed files with 145 additions and 146 deletions

View File

@ -1,11 +1,45 @@
function querySelectorParentChild(el, selector) {
while(el !== document.body) {
if(el.querySelector(selector)) {
break;
function recursiveAdaptWidgets(prefix, element, user) {
//- This function is a workaround for the currently unworking populate
//- Feel free to see examples on page-circles-
element.querySelectorAll('[' + prefix + '-user-id]').forEach((el) => {
el.setAttribute(el.getAttribute(prefix + '-user-id'), user['@id']);
});
if (element != document) {
//- Allow to bind-user on selected attribute
if (element.getAttribute(prefix + '-user-id')) {
element.setAttribute(element.getAttribute(prefix + '-user-id'), user['@id']);
}
//- Allow to set data-src to a children sib-form
if (element.getAttribute(prefix + '-bind-resources')) {
let form = element.querySelector(element.getAttribute(prefix + '-bind-resources') + " sib-form");
if (form) {
form.setAttribute('data-src', element.component.resourceId.replace('members/', ''));
}
}
//- Allow to put user on a targetted search field
if (element.getAttribute(prefix + '-bind-user2input')) {
element.querySelector(element.getAttribute(prefix + '-bind-user2input')).value = user['@id'];
}
//- In case your sib-display use a multiple, you have to target sub-sib-display auto-generated
if (element.getAttribute(prefix + '-widgets-multiple') !== null) {
element.querySelectorAll('div > sib-display').forEach((el) => {
el.setAttribute(prefix + '-widgets', "");
});
}
el = el.parentNode;
}
return el.querySelector(selector);
//- This function is hooked every time a sib-something with prefix+"-widgets" is populated
element.querySelectorAll('[' + prefix + '-widgets]').forEach((elementChild) => {
elementChild.addEventListener('populate', () => {
recursiveAdaptWidgets(prefix, elementChild, user);
});
});
}
document.addEventListener('DOMContentLoaded', function (event) {
@ -35,44 +69,18 @@ document.addEventListener('DOMContentLoaded', function (event) {
})
});
//- Watch every sib-forms & update data-src of linked sib-display
document.querySelectorAll('sib-form[data-src], sib-form[bind-resource]').forEach(function(el) {
el.addEventListener("save", function() {
let dataSrc = el.getAttribute('data-src');
document.querySelectorAll('sib-display[data-src="'+dataSrc+'"]').forEach(function(e) {
e.resource.clearCache();
});
});
});
document.querySelector('sib-auth').getUser().then(user => {
if(user !== null) {
if (user !== null) {
// Hide login button if already logged
document.querySelector('button[role="log in"]').setAttribute('style', 'display:none !important');
// Set current user id on set-user-id of sib-display
for(sibDisplay of document.querySelectorAll('sib-display[set-user-id]')) {
sibDisplay.setAttribute(sibDisplay.getAttribute('set-user-id'), user['@id']);
}
for(sibForm of document.querySelectorAll('[set-user-id-select]')) {
sibForm.addEventListener('populate', () => {
// BUG: Populate event trigger before the view is fully loaded! - https://git.happy-dev.fr/startinblox/framework/sib-core/issues/521
document.querySelectorAll('sib-ac-checker:not([hidden]) > sib-delete[data-label^="Leave"]').forEach((el)=>{
querySelectorParentChild(el, 'sib-form[submit-button^="Join"]').setAttribute('style', 'display:none !important');
});
for(select of sibForm.querySelectorAll('select[name="'+sibForm.getAttribute('set-user-id-select')+'"]')) {
for(option of select.options) {
option.selected = (option.getAttribute('value') == '{"@id": "'+user['@id']+'"}');
}
}
});
}
for(leaveBtn of document.querySelectorAll('admin-circle-leave > sib-ac-checker:not([hidden])')) {
recursiveAdaptWidgets("hd-inherit", document, user);
for (leaveBtn of document.querySelectorAll('admin-circle-leave > sib-ac-checker:not([hidden])')) {
leaveBtn.parentNode.parentNode.parentNode.nextElementSibling.setAttribute('style', 'display:none !important'); // Hide Join button
}
}
}).catch(error => console.log(error));
});