2
0
mirror of https://github.com/tenrok/vue-context.git synced 2026-06-20 13:30:33 +03:00

Merge pull request #11 from rawilk/CloseEvent

This commit is contained in:
Randall Wilk
2018-10-15 09:14:52 -05:00
committed by GitHub
8 changed files with 57 additions and 26 deletions
+5
View File
@@ -1,5 +1,10 @@
# Changelog # Changelog
# 3.3.0 (Oct 15, 2018)
## Added
- Context menu now emits events when opened and closed ([issue #10](https://github.com/rawilk/vue-context/issues/10))
# 3.2.0 (Sep 12, 2018) # 3.2.0 (Sep 12, 2018)
### Added ### Added
+11 -4
View File
@@ -146,10 +146,17 @@ new Vue({
## Props ## Props
| Property | Type | Default | Description | Property | Type | Default | Description |
| -------- | ---- | ------- | ----------- | --- | --- | --- | --- |
| `closeOnClick` | Boolean | `true` | If set to false, context menu will not automatically close when clicked on. | `closeOnClick` | Boolean | `true` | If set to false, context menu will not automatically close when clicked on. |
| `closeOnScroll` | Boolean | `true` | If set to true, context menu will automatically close on window scroll. | `closeOnScroll` | Boolean | `true` | If set to true, context menu will automatically close on window scroll. |
## Events
| Event | Params | Description |
| --- | --- | --- |
| close | none | Emits when the context menu is closed |
| open | `event`, `data`, `top`, `left` | Emits when the menu is opened. The event, context menu data, top and left position are all sent through as parameters as well. |
## Credits ## Credits
+1 -1
View File
File diff suppressed because one or more lines are too long
+19 -11
View File
@@ -45,7 +45,7 @@
return this.show return this.show
? { top: `${this.top}px`, left: `${this.left}px` } ? { top: `${this.top}px`, left: `${this.left}px` }
: null; : null;
}, }
}, },
data () { data () {
@@ -77,22 +77,28 @@
window.addEventListener('scroll', this.close); window.addEventListener('scroll', this.close);
}, },
/** /**
* Close the context menu. * Close the context menu.
*/ *
close () { * @param {boolean|Event} emit Used to prevent event being emitted twice from when menu is clicked and closed
this.top = null; */
this.left = null; close (emit = true) {
this.data = null; this.top = null;
this.show = false; this.left = null;
}, this.data = null;
this.show = false;
if (emit) {
this.$emit('close');
}
},
/** /**
* Close the menu if `closeOnClick` is set to true. * Close the menu if `closeOnClick` is set to true.
*/ */
onClick () { onClick () {
if (this.closeOnClick) { if (this.closeOnClick) {
this.close(); this.close(false);
} }
}, },
@@ -109,6 +115,8 @@
this.$nextTick(() => { this.$nextTick(() => {
this.positionMenu(event.clientY, event.clientX); this.positionMenu(event.clientY, event.clientX);
this.$el.focus(); this.$el.focus();
this.$emit('open', event, this.data, this.top, this.left);
}); });
}, },
+4 -1
View File
@@ -10,7 +10,10 @@
Right click on me Right click on me
</p> </p>
<vue-context ref="menu" :close-on-scroll="close" :close-on-click="false"> <vue-context ref="menu" :close-on-scroll="close"
@close="onClose"
@open="onOpen"
>
<ul slot-scope="child"> <ul slot-scope="child">
<li @click="onClick(child.data)">Option 1 {{ child.data && child.data.foo }}</li> <li @click="onClick(child.data)">Option 1 {{ child.data && child.data.foo }}</li>
<li>Option 2</li> <li>Option 2</li>
+2 -2
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+9 -1
View File
@@ -12,7 +12,15 @@ new Vue({
methods: { methods: {
onClick (data) { onClick (data) {
console.log(data); // console.log(data);
}, },
onClose () {
console.log('closing');
},
onOpen (event, data, top, left) {
console.log(data, top, left);
}
}, },
}).$mount('#app'); }).$mount('#app');