You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.8 KiB

  1. <app-sidebar-button>
  2. <button class="button m-bottom-0" type="button" onclick={ (event) => { handleClick(event) } }></button>
  3. <script>
  4. /**
  5. * button to create customEvent and dispatch to element
  6. *
  7. * <app-sidebar-button event="" selector="" data={ }>
  8. *
  9. * </app-sidebar-button>
  10. *
  11. * @author Björn Hase
  12. *
  13. */
  14. export default {
  15. state: {
  16. element: undefined,
  17. data: []
  18. },
  19. /**
  20. *
  21. *
  22. */
  23. onBeforeMount() {
  24. // getting innerHtml before rendering component
  25. this.content = this.root.innerHTML;
  26. this.root.innerHTML = '';
  27. if (!this.props.event) {
  28. console.error('sidebar-button: attribute for name of custom event is missing')
  29. }
  30. if (!this.props.selector) {
  31. console.error('sidebar-button: attribute for selector to send custom event is missing')
  32. }
  33. },
  34. /**
  35. *
  36. *
  37. */
  38. onMounted() {
  39. this.state.element = document.querySelector(this.props.selector)
  40. // adding innerHtml to button
  41. this.$('button').innerHTML = this.content;
  42. },
  43. /**
  44. * create custom event with props
  45. *
  46. * @param {[type]} event
  47. * @return {[type]}
  48. */
  49. handleClick(event) {
  50. event.preventDefault()
  51. const customEvent = new CustomEvent(this.props.event)
  52. this.state.element.dispatchEvent(customEvent, this.state.data)
  53. }
  54. }
  55. </script>
  56. </app-sidebar-button>