Ocelot-Social/webapp/plugins/vue-directives.js
Raphael Beer 15d5933821
Change: v-focus directive looking for input +
Searches children and uses first <input> child.
  if target of v-focus is not instanceof HTMLInputElement

  Also, adds vue-directives to storybook and tests
2020-03-25 12:51:38 +01:00

41 lines
1.0 KiB
JavaScript

import Vue from 'vue'
export default ({ app }) => {
Vue.directive('focus', {
// When the bound element is inserted into the DOM...
inserted: (el, binding) => {
// Focus the element
Vue.nextTick(() => {
if (binding.value !== false) {
let target = el
const isInput = target instanceof HTMLInputElement
if (!isInput) {
target = el.querySelector('input')
}
if (target && typeof target.focus === 'function') {
target.focus()
}
}
})
},
})
Vue.directive('router-link', {
bind: (el, binding) => {
binding.clickEventListener = (e) => {
if (!e.metaKey && !e.ctrlKey) {
e.preventDefault()
app.router.push(el.getAttribute('href'))
}
}
el.addEventListener('click', binding.clickEventListener)
},
unbind: (el, binding) => {
// cleanup
if (binding.clickEventListener) {
el.removeEventListener('click', binding.clickEventListener)
}
},
})
}