diff --git a/CHANGELOG.md b/CHANGELOG.md index b98eaa500..b06b6469d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Resources/public/modules/show_hide/show_hide.js b/Resources/public/modules/show_hide/show_hide.js index f22064433..5f65e2b18 100644 --- a/Resources/public/modules/show_hide/show_hide.js +++ b/Resources/public/modules/show_hide/show_hide.js @@ -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};