Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to add exclude list for v-click-outside #983

Merged
merged 1 commit into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion l10n/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ msgstr ""
msgid "Select a tag"
msgstr ""

#: src/components/AppNavigationSettings/AppNavigationSettings.vue:53
#: src/components/AppNavigationSettings/AppNavigationSettings.vue:57
msgid "Settings"
msgstr ""

Expand Down
10 changes: 9 additions & 1 deletion src/components/AppNavigationSettings/AppNavigationSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<template>
<div id="app-settings"
v-click-outside="closeMenu"
v-click-outside="clickOutsideConfig"
:class="{ open }">
<div id="app-settings-header">
<button class="settings-button"
Expand All @@ -41,11 +41,15 @@
<script>
import { directive as ClickOutside } from 'v-click-outside'
import { t } from '../../l10n'
import { excludeClickOutsideClasses } from '../../mixins'

export default {
directives: {
ClickOutside,
},
mixins: [
excludeClickOutsideClasses,
],
props: {
title: {
type: String,
Expand All @@ -56,6 +60,10 @@ export default {
data() {
return {
open: false,
clickOutsideConfig: {
handler: this.closeMenu,
middleware: this.clickOutsideMiddleware,
},
}
},
methods: {
Expand Down
72 changes: 72 additions & 0 deletions src/mixins/excludeClickOutsideClasses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* @copyright Copyright (c) 2020 Georg Ehrke <[email protected]>
*
* @author Georg Ehrke <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

export default {
props: {
/**
* A class-name or an array of class-names
* to be ignored when clicking outside
* an element
*/
excludeClickOutsideClasses: {
type: String | Array,
default: [],
},
},
methods: {
/**
* Middleware Handler for V-Click-Outside
*
* @param {Event} event The click event
* @returns {Boolean}
*/
clickOutsideMiddleware(event) {
const excludedClassList = Array.isArray(this.excludeClickOutsideClasses)
? this.excludeClickOutsideClasses
: [this.excludeClickOutsideClasses]

// No need to iterate through all parents
// if class-list is empty
if (excludedClassList.length === 0) {
return true
}

return !this.hasNodeOrAnyParentClass(event.target, excludedClassList)
},
/**
* Checks if given node or any of it's parents have a class of classArray
*
* @param {Element} node Node to test
* @param {Array} classArray List of classes to check for
* @returns {Boolean}
*/
hasNodeOrAnyParentClass(node, classArray) {
for (const className of classArray) {
if (node.classList.contains(className)) {
return true
}
}

return !!node.parentElement && this.hasNodeOrAnyParentClass(node.parentElement, classArray)
},
},
}
2 changes: 2 additions & 0 deletions src/mixins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
*
*/

import excludeClickOutsideClasses from './excludeClickOutsideClasses.js'
import isFullscreen from './isFullscreen'
import isMobile from './isMobile'

export {
excludeClickOutsideClasses,
isFullscreen,
isMobile,
}