direct-chat.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * --------------------------------------------
  3. * @file AdminLTE direct-chat.ts
  4. * @description Direct chat for AdminLTE.
  5. * @license MIT
  6. * --------------------------------------------
  7. */
  8. import {
  9. onDOMContentLoaded
  10. } from './util/index'
  11. /**
  12. * Constants
  13. * ====================================================
  14. */
  15. const DATA_KEY = 'lte.direct-chat'
  16. const EVENT_KEY = `.${DATA_KEY}`
  17. const EVENT_EXPANDED = `expanded${EVENT_KEY}`
  18. const EVENT_COLLAPSED = `collapsed${EVENT_KEY}`
  19. const SELECTOR_DATA_TOGGLE = '[data-lte-toggle="chat-pane"]'
  20. const SELECTOR_DIRECT_CHAT = '.direct-chat'
  21. const CLASS_NAME_DIRECT_CHAT_OPEN = 'direct-chat-contacts-open'
  22. /**
  23. * Class Definition
  24. * ====================================================
  25. */
  26. class DirectChat {
  27. _element: HTMLElement
  28. constructor(element: HTMLElement) {
  29. this._element = element
  30. }
  31. toggle(): void {
  32. if (this._element.classList.contains(CLASS_NAME_DIRECT_CHAT_OPEN)) {
  33. const event = new Event(EVENT_COLLAPSED)
  34. this._element.classList.remove(CLASS_NAME_DIRECT_CHAT_OPEN)
  35. this._element.dispatchEvent(event)
  36. } else {
  37. const event = new Event(EVENT_EXPANDED)
  38. this._element.classList.add(CLASS_NAME_DIRECT_CHAT_OPEN)
  39. this._element.dispatchEvent(event)
  40. }
  41. }
  42. }
  43. /**
  44. *
  45. * Data Api implementation
  46. * ====================================================
  47. */
  48. onDOMContentLoaded(() => {
  49. const button = document.querySelectorAll(SELECTOR_DATA_TOGGLE)
  50. button.forEach(btn => {
  51. btn.addEventListener('click', event => {
  52. event.preventDefault()
  53. const target = event.target as HTMLElement
  54. const chatPane = target.closest(SELECTOR_DIRECT_CHAT) as HTMLElement | undefined
  55. if (chatPane) {
  56. const data = new DirectChat(chatPane)
  57. data.toggle()
  58. }
  59. })
  60. })
  61. })
  62. export default DirectChat