refactor(confirm): support modal transform origin

This commit is contained in:
07akioni 2019-08-20 15:38:40 +08:00
parent 96fd16957c
commit a8d62d0296
6 changed files with 76 additions and 29 deletions

View File

@ -27,13 +27,13 @@ export default {
} }
}, },
methods: { methods: {
handleConfirm () { handleConfirm (e) {
const confirmInstance = this.$NModal.confirm({ const confirmInstance = this.$NModal.confirm({
title: 'Confirm', title: 'Confirm',
content: 'Are u sure to ...?', content: 'Are u sure to ...?',
okText: 'Yes', // ok errorsuccess使 okText: 'Yes', // ok errorsuccess使
cancelText: 'No', // cancelText: 'No', //
activateEvent: e,
onOk: () => { onOk: () => {
console.log('click on ok', confirmInstance) console.log('click on ok', confirmInstance)
this.$NMessage.info('will be ok in 3 seconds') this.$NMessage.info('will be ok in 3 seconds')

View File

@ -33,13 +33,13 @@ export default {
} }
}, },
methods: { methods: {
handleConfirm () { handleConfirm (e) {
const confirmInstance = this.$NModal.confirm({ const confirmInstance = this.$NModal.confirm({
title: 'Confirm', title: 'Confirm',
content: '<b>Are u sure to ...?', content: '<b>Are u sure to ...?',
activateEvent: e,
onOk: () => { onOk: () => {
console.log('click on ok', confirmInstance) console.log('click on ok', confirmInstance)
this.$NMessage.success('sure') this.$NMessage.success('sure')
}, },
onCancel: () => { onCancel: () => {
@ -47,10 +47,11 @@ export default {
} }
}) })
}, },
handleSuccess () { handleSuccess (e) {
const confirmInstance = this.$NModal.success({ const confirmInstance = this.$NModal.success({
title: 'Success', title: 'Success',
content: 'Premium designed icons for use in web, iOS, Android, and desktop apps. Support for SVG and web font. Completely open source, MIT licensed and built by the Ionic Framework team.', content: 'Premium designed icons for use in web, iOS, Android, and desktop apps. Support for SVG and web font. Completely open source, MIT licensed and built by the Ionic Framework team.',
activateEvent: e,
onOk: () => { onOk: () => {
console.log('click on ok', confirmInstance) console.log('click on ok', confirmInstance)
@ -58,13 +59,13 @@ export default {
} }
}) })
}, },
handleError () { handleError (e) {
const confirmInstance = this.$NModal.error({ const confirmInstance = this.$NModal.error({
title: 'Error', title: 'Error',
content: '<b>这是一个测试?</b>hhahhaahahhahahsdiusah ashdusadu asdsadsadsadsadsa', content: '<b>这是一个测试?</b>hhahhaahahhahahsdiusah ashdusadu asdsadsadsadsadsa',
activateEvent: e,
onOk: () => { onOk: () => {
console.log('click on ok', confirmInstance) console.log('click on ok', confirmInstance)
this.$NMessage.success('I know..') this.$NMessage.success('I know..')
} }
}) })

View File

@ -1,6 +1,7 @@
<template> <template>
<n-modal <n-modal
v-model="isActive" v-model="isActive"
:activate-event="activateEvent"
@toggle="toggle" @toggle="toggle"
> >
<!-- <transition name="n-modal-content--transition"> --> <!-- <transition name="n-modal-content--transition"> -->
@ -16,7 +17,6 @@
/> />
{{ title }} {{ title }}
</span> </span>
<n-icon <n-icon
type="md-close" type="md-close"
size="22" size="22"
@ -74,6 +74,7 @@ export default {
type: 'error', type: 'error',
title: 'title', title: 'title',
loading: null, loading: null,
activateEvent: null,
onCancel: () => {}, onCancel: () => {},
onOk: () => {} onOk: () => {}
} }
@ -111,15 +112,6 @@ export default {
this.onOk() this.onOk()
if (this.loading !== true) { this.isActive = false } if (this.loading !== true) { this.isActive = false }
} }
// updateData (data) {
// Object.keys(data).forEach((key) => {
// this[key] = data[key]
// })
// },
} }
} }
</script> </script>
<style scoped>
</style>

View File

@ -27,6 +27,7 @@
<script> <script>
import NScrollbar from '../../Scrollbar' import NScrollbar from '../../Scrollbar'
export default { export default {
components: { components: {
NScrollbar NScrollbar
@ -35,6 +36,12 @@ export default {
active: { active: {
type: Boolean, type: Boolean,
default: false default: false
},
activateEvent: {
validator (e) {
return e instanceof MouseEvent
},
default: null
} }
}, },
data () { data () {
@ -67,21 +74,40 @@ export default {
// console.log('afterEnter', this.$refs.scrollbar.enableScrollbar()) // console.log('afterEnter', this.$refs.scrollbar.enableScrollbar())
}, },
updateTransformOrigin () { updateTransformOrigin () {
if (
(!this.$parent.$refs.activator ||
!this.$parent.$refs.activator.childElementCount) &&
!this.activateEvent
) {
return
}
const scrollTop = this.$refs.scrollbar.containerScrollTop const scrollTop = this.$refs.scrollbar.containerScrollTop
console.log(scrollTop)
const { const {
offsetLeft, offsetLeft,
offsetTop offsetTop
} = this.$refs.contentInner } = this.$refs.contentInner
if (
this.$parent.$refs.activator &&
this.$parent.$refs.activator.childElementCount
) {
const { const {
left: activatorLeft, left: activatorLeft,
top: activatorTop, top: activatorTop,
width: activatorWidth width: activatorWidth,
height: activatorHeight
} = this.$parent.$refs.activator.getBoundingClientRect() } = this.$parent.$refs.activator.getBoundingClientRect()
const transformOriginX = -(offsetLeft - activatorLeft - activatorWidth / 2) const transformOriginX = -(offsetLeft - activatorLeft - activatorWidth / 2)
const transformOriginY = -(offsetTop - activatorTop - scrollTop - activatorTop / 2) const transformOriginY = -(offsetTop - activatorTop - scrollTop - activatorHeight / 2)
this.$refs.contentInner.style.transformOrigin = `${transformOriginX}px ${transformOriginY}px` this.$refs.contentInner.style.transformOrigin = `${transformOriginX}px ${transformOriginY}px`
console.log(this.$refs.contentInner.style.transformOrigin) } else {
const activatorTop = this.activateEvent.clientY
const activatorLeft = this.activateEvent.clientX
const activatorWidth = 0
const activatorHeight = 0
const transformOriginX = -(offsetLeft - activatorLeft - activatorWidth / 2)
const transformOriginY = -(offsetTop - activatorTop - scrollTop - activatorHeight / 2)
this.$refs.contentInner.style.transformOrigin = `${transformOriginX}px ${transformOriginY}px`
}
}, },
handleBeforeLeave () { handleBeforeLeave () {
this.updateTransformOrigin() this.updateTransformOrigin()
@ -127,6 +153,6 @@ export default {
.n-modal-content--transition-enter, .n-modal-content--transition-leave-to { .n-modal-content--transition-enter, .n-modal-content--transition-leave-to {
opacity: 0; opacity: 0;
transform: scale(.75); transform: scale(.5);
} }
</style> </style>

View File

@ -13,6 +13,14 @@ export default {
detachable, detachable,
zindexable zindexable
], ],
props: {
activateEvent: {
validator (e) {
return e instanceof MouseEvent
},
default: null
}
},
data () { data () {
return { return {
mousedownTarget: null mousedownTarget: null
@ -38,7 +46,7 @@ export default {
h(NModalContent, h(NModalContent,
{ {
ref: 'content', ref: 'content',
props: { active: this.active }, props: { active: this.active, activateEvent: this.activateEvent },
on: { on: {
mousedown: (e) => { mousedown: (e) => {
this.mousedownTarget = e.target this.mousedownTarget = e.target

View File

@ -0,0 +1,20 @@
/**
* This file is currently of no use
*/
let memorizedClickEvent = null
class ClickTracer {
constructor () {
window.removeEventListener('click', ClickTracer.traceClickEvent, true)
window.addEventListener('click', ClickTracer.traceClickEvent, true)
}
static traceClickEvent (e) {
memorizedClickEvent = e
}
get memorizedClickEvent () {
return memorizedClickEvent
}
}
export default new ClickTracer()