DirectChat.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * --------------------------------------------
  3. * AdminLTE DirectChat.js
  4. * License MIT
  5. * --------------------------------------------
  6. */
  7. import $ from 'jquery'
  8. /**
  9. * Constants
  10. * ====================================================
  11. */
  12. const NAME = 'DirectChat'
  13. const DATA_KEY = 'lte.directchat'
  14. const EVENT_KEY = `.${DATA_KEY}`
  15. const JQUERY_NO_CONFLICT = $.fn[NAME]
  16. const EVENT_TOGGLED = `toggled${EVENT_KEY}`
  17. const SELECTOR_DATA_TOGGLE = '[data-widget="chat-pane-toggle"]'
  18. const SELECTOR_DIRECT_CHAT = '.direct-chat'
  19. const CLASS_NAME_DIRECT_CHAT_OPEN = 'direct-chat-contacts-open'
  20. /**
  21. * Class Definition
  22. * ====================================================
  23. */
  24. class DirectChat {
  25. constructor(element) {
  26. this._element = element
  27. }
  28. toggle() {
  29. $(this._element).parents(SELECTOR_DIRECT_CHAT).first().toggleClass(CLASS_NAME_DIRECT_CHAT_OPEN)
  30. $(this._element).trigger($.Event(EVENT_TOGGLED))
  31. }
  32. // Static
  33. static _jQueryInterface(config) {
  34. return this.each(function () {
  35. let data = $(this).data(DATA_KEY)
  36. if (!data) {
  37. data = new DirectChat($(this))
  38. $(this).data(DATA_KEY, data)
  39. } else if (typeof config === 'string') {
  40. if (typeof data[config] === 'undefined') {
  41. throw new TypeError(`No method named "${config}"`)
  42. }
  43. data[config]()
  44. } else if (typeof config === 'undefined') {
  45. data._init()
  46. }
  47. })
  48. }
  49. }
  50. /**
  51. *
  52. * Data Api implementation
  53. * ====================================================
  54. */
  55. $(document).on('click', SELECTOR_DATA_TOGGLE, function (event) {
  56. if (event) {
  57. event.preventDefault()
  58. }
  59. DirectChat._jQueryInterface.call($(this), 'toggle')
  60. })
  61. /**
  62. * jQuery API
  63. * ====================================================
  64. */
  65. $.fn[NAME] = DirectChat._jQueryInterface
  66. $.fn[NAME].Constructor = DirectChat
  67. $.fn[NAME].noConflict = function () {
  68. $.fn[NAME] = JQUERY_NO_CONFLICT
  69. return DirectChat._jQueryInterface
  70. }
  71. export default DirectChat