don't allow clicking through to status if click is on toolbar

This commit is contained in:
Nolan Lawson 2018-04-04 18:53:52 -07:00
parent cbe7192928
commit 760cafdeff
2 changed files with 38 additions and 9 deletions

View file

@ -113,8 +113,12 @@
import { goto } from 'sapper/runtime.js' import { goto } from 'sapper/runtime.js'
import { registerClickDelegate, unregisterClickDelegate } from '../../_utils/delegate' import { registerClickDelegate, unregisterClickDelegate } from '../../_utils/delegate'
import { classname } from '../../_utils/classname' import { classname } from '../../_utils/classname'
import { checkDomAncestors } from '../../_utils/checkDomAncestors'
const INPUT_TAGS = new Set(['a', 'button', 'input', 'textarea']) const INPUT_TAGS = new Set(['a', 'button', 'input', 'textarea'])
const isUserInputElement = node => INPUT_TAGS.has(node.localName)
const isToolbar = node => node.classList.contains('status-toolbar')
const isStatusArticle = node => node.classList.contains('status-article')
export default { export default {
oncreate() { oncreate() {
@ -146,17 +150,26 @@
store: () => store, store: () => store,
methods: { methods: {
onClickOrKeydown(e) { onClickOrKeydown(e) {
let { type, keyCode } = e let { type, keyCode, target } = e
let { localName, parentElement } = e.target
if ((type === 'click' || (type === 'keydown' && keyCode === 13)) && let isClick = type === 'click'
!INPUT_TAGS.has(localName) && let isEnter = type === 'keydown' && keyCode === 13
!INPUT_TAGS.has(parentElement.localName) && if (!isClick && !isEnter) {
!INPUT_TAGS.has(parentElement.parentElement.localName)) { return
e.preventDefault()
e.stopPropagation()
goto(`/statuses/${this.get('originalStatusId')}`)
} }
if (checkDomAncestors(target, isUserInputElement, isStatusArticle)) {
// this element or any ancestors up to the status-article element is
// a user input element
return
}
if (checkDomAncestors(target, isToolbar, isStatusArticle)) {
// this element or any of its ancestors is the toolbar
return
}
e.preventDefault()
e.stopPropagation()
goto(`/statuses/${this.get('originalStatusId')}`)
} }
}, },
computed: { computed: {

View file

@ -0,0 +1,16 @@
// Check if some condition applies for a node or any of its ancestors,
// stopping at an element that returns true for the given stopFunc. Returns
// false if none match
export function checkDomAncestors (node, checkFunc, stopFunc) {
let thisNode = node
while (thisNode) {
if (stopFunc(thisNode)) {
break
}
if (checkFunc(thisNode)) {
return true
}
thisNode = thisNode.parentElement
}
return false
}