[show/hide] add events to module

This commit is contained in:
Julien Fastré 2020-01-24 15:42:50 +01:00
parent 99632344a6
commit d46ee72fb1
2 changed files with 45 additions and 15 deletions

View File

@ -102,3 +102,4 @@ Branch CRUD-Init
- add Tabs parametric feature to easily render tabs panels
- css: add a margin on the button "delete entry" in collection
- module `show_hide`: add the possibility to launch a show hide manually and not on page loading. Useful when show/hide occurs in collection.
- module `show_hide`: add events to module

View File

@ -25,7 +25,8 @@ var ShowHide = function(options) {
event_name = 'event_name' in options ? options.event_name : 'change',
container_content = [],
debug = 'debug' in options ? options.debug : false,
load_event = 'load_event' in options ? options.load_event : 'load';
load_event = 'load_event' in options ? options.load_event : 'load',
id = 'uid' in options ? options.id : Math.random();
var bootstrap = function(event) {
if (debug) {
@ -59,26 +60,19 @@ var ShowHide = function(options) {
var onChange = function (event) {
var result = test(froms, event);
var result = test(froms, event), me;
if (result === true) {
if (is_shown === false) {
for (let i of container_content.keys()) {
var contents = container_content[i];
for (let el of contents.values()) {
container[i].appendChild(el);
}
}
is_shown = true;
forceShow();
me = new CustomEvent('show-hide-show', { detail: { id: id, container: container, froms: froms } });
window.dispatchEvent(me);
}
} else if (result === false) {
if (is_shown) {
for (let contents of container_content.values()) {
for (let el of contents.values()) {
el.remove();
}
}
is_shown = false;
forceHide();
me = new CustomEvent('show-hide-hide', { detail: { id: id, container: container, froms: froms } });
window.dispatchEvent(me);
}
} else {
throw "the result of test is not a boolean";
@ -86,12 +80,47 @@ var ShowHide = function(options) {
};
var forceHide = function() {
if (debug) {
console.log('force hide');
}
for (let contents of container_content.values()) {
for (let el of contents.values()) {
el.remove();
}
}
is_shown = false;
};
var forceShow = function() {
if (debug) {
console.log('show');
}
for (let i of container_content.keys()) {
var contents = container_content[i];
for (let el of contents.values()) {
container[i].appendChild(el);
}
}
is_shown = true;
};
var forceCompute = function(event) {
onChange(event);
};
if (load_event !== null) {
window.addEventListener('load', bootstrap);
} else {
bootstrap(null);
}
return {
forceHide: forceHide,
forceShow: forceShow,
forceCompute: forceCompute,
};
};
export {ShowHide};