Skip to content

Commit

Permalink
fix: map grid item event params
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoluoboding committed Jan 7, 2021
1 parent d40dde0 commit 194a09a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
3 changes: 2 additions & 1 deletion app/views/WidgetWithGrid.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
:margin="[15, 15]"
:is-static="isStatic"
@layout-updated="onLayoutUpdated"
@move="onMove"
@moved="onMove"
@resized="onResize"
>
<smart-widget slot="0" simple>
<div class="layout-center">
Expand Down
1 change: 1 addition & 0 deletions src/packages/SmartWidget.vue
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ export default {
// overwirte vue-grid-layout styles
.vue-grid-item {
touch-action: none;
box-sizing: border-box;
}
.vue-grid-item.vue-grid-placeholder {
background: #7CBEFF;
Expand Down
27 changes: 16 additions & 11 deletions src/packages/SmartWidgetGrid.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<script>
import { GridLayout, GridItem } from 'vue-grid-layout'
import { pick } from '../utils/index'
import { pick, mapObject } from '../utils/index'
export default {
name: 'SmartWidgetGrid',
Expand Down Expand Up @@ -84,7 +84,8 @@ export default {
// responsiveLayouts: {},
// breakpoints: { lg: 1200, md: 996, sm: 768, xs: 480, xxs: 0 },
...this.$attrs
}
},
gridLayoutItemEvents: {}
}
},
created () {
Expand All @@ -100,19 +101,23 @@ export default {
'breakpoint-changed'
]
const layoutItemEventList = [
'move',
'resize',
'moved',
'resized',
'container-resized'
]
const layoutItemEventMap = {
move: ['i', 'newX', 'newY'],
resize: ['i', 'newH', 'newW', 'newHPx', 'newWPx'],
moved: ['i', 'newX', 'newY'],
resized: ['i', 'newH', 'newW', 'newHPx', 'newWPx'],
'container-resized': ['i', 'newH', 'newW', 'newHPx', 'newWPx']
}
this.gridLayoutEvents = pick(listeners, layoutEventList)
layoutItemEventList.forEach(item => {
Object.keys(layoutItemEventMap).forEach(event => {
const eventParams = layoutItemEventMap[event]
this.gridLayoutItemEvents = Object.assign(this.gridLayoutItemEvents, {
[item]: (...args) => this.$emit(item, { ...args })
[event]: (...args) => {
const params = mapObject(eventParams, (key, idx) => args[idx])
this.$emit(event, params)
}
})
})
}
Expand Down
7 changes: 7 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ export const pick = (obj, arr) => {
return (curr in obj && (acc[curr] = obj[curr]), acc)
}, {})
}

// Maps the values of an array to an object using a function.
export const mapObject = (arr, fn) =>
arr.reduce((acc, el, i) => {
acc[el] = fn(el, i, arr)
return acc
}, {})

0 comments on commit 194a09a

Please sign in to comment.