improves custom v-closable directive

This commit is contained in:
2019-06-12 21:35:35 +02:00
parent a57ffdb6c6
commit 3232710a4c
2 changed files with 16 additions and 7 deletions

View File

@@ -8,7 +8,7 @@
<h1 class="title">Coffre de groupe</h1>
</div>
</div>
<Chest player=0></Chest>
<Chest :player="0"></Chest>
</section>
</template>

View File

@@ -15,7 +15,7 @@
</a>
</div>
<div class="dropdown-menu" id="dropdown-menu" role="menu"
v-closable="{ exclude: ['dropdown_btn'], handler: 'closeDropdown' }">
v-closable="{ exclude: ['dropdown_btn'], handler: 'closeDropdown', visible: show_dropdown }">
<div class="dropdown-content">
<a v-for="(p,i) in player_list" :key="i"
@click="setActivePlayer(i)"
@@ -75,6 +75,7 @@
],
show_dropdown: false,
edit_wealth: false,
handleOutsideClick: null,
};
},
computed: {
@@ -109,7 +110,6 @@
'closable': {
bind: function(el, binding, vnode) {
handleOutsideClick = (e) => {
console.log("outsideClick");
e.stopPropagation();
const { exclude, handler } = binding.value;
let excludedElClicked = false;
@@ -125,12 +125,21 @@
vnode.context[handler]()
}
};
},
// Bind custom handler only when dropdown is visible
update: function(el, binding, vnode, _) {
const { visible } = binding.value;
if (visible) {
document.addEventListener('click', handleOutsideClick);
document.addEventListener('touchstart', handleOutsideClick);
} else {
document.removeEventListener('click', handleOutsideClick);
document.removeEventListener('touchstart', handleOutsideClick);
}
},
unbind: function() { console.log("unbind");
document.removeEventListener('click', handleOustideClick);
document.removeEventListener('touchstart', handleOustideClick);
document.removeEventListener('click', handleOutsideClick);
document.removeEventListener('touchstart', handleOutsideClick);
}
}
}