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.

Widget.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-01-15 19:17:52
  4. */
  5. import State from '@dc-modules/state/State'
  6. import WidgetType from './WidgetType'
  7. class Widget {
  8. constructor() {
  9. this._viewer = undefined
  10. this._enable = false
  11. this._wrapper = undefined
  12. this._ready = false
  13. }
  14. set enable(enable) {
  15. if (this._enable === enable) {
  16. return this
  17. }
  18. this._enable = enable
  19. this._state = this._enable ? State.ENABLED : State.DISABLED
  20. this._enableHook && this._enableHook()
  21. return this
  22. }
  23. get enable() {
  24. return this._enable
  25. }
  26. get state() {
  27. return this._state
  28. }
  29. /**
  30. * mount content
  31. * @private
  32. */
  33. _mountContent() {}
  34. /**
  35. * binds event
  36. * @private
  37. */
  38. _bindEvent() {}
  39. /**
  40. * Unbinds event
  41. * @private
  42. */
  43. _unbindEvent() {}
  44. /**
  45. * When enable modifies the hook executed, the subclass copies it as required
  46. * @private
  47. */
  48. _enableHook() {
  49. !this._ready && this._mountContent()
  50. if (this._enable) {
  51. !this._wrapper.parentNode &&
  52. this._viewer.dcContainer.appendChild(this._wrapper)
  53. this._bindEvent()
  54. } else {
  55. this._unbindEvent()
  56. this._wrapper.parentNode &&
  57. this._viewer.dcContainer.removeChild(this._wrapper)
  58. }
  59. }
  60. /**
  61. * Updating the Widget location requires subclass overrides
  62. * @param windowCoord
  63. * @private
  64. */
  65. _updateWindowCoord(windowCoord) {}
  66. /**
  67. * Hook for installed
  68. * @private
  69. */
  70. _installHook() {}
  71. /**
  72. * Installs to viewer
  73. * @param viewer
  74. */
  75. install(viewer) {
  76. this._viewer = viewer
  77. /**
  78. * do installHook
  79. */
  80. this._installHook && this._installHook()
  81. this._state = State.INSTALLED
  82. }
  83. /**
  84. * Setting wrapper
  85. * @param wrapper
  86. * @returns {Widget}
  87. */
  88. setWrapper(wrapper) {
  89. return this
  90. }
  91. /**
  92. * Setting widget content
  93. * @param content
  94. * @returns {Widget}
  95. */
  96. setContent(content) {
  97. if (content && typeof content === 'string') {
  98. this._wrapper.innerHTML = content
  99. } else if (content && content instanceof Element) {
  100. while (this._wrapper.hasChildNodes()) {
  101. this._wrapper.removeChild(this._wrapper.firstChild)
  102. }
  103. this._wrapper.appendChild(content)
  104. }
  105. return this
  106. }
  107. /**
  108. * hide widget
  109. */
  110. hide() {
  111. this._wrapper &&
  112. (this._wrapper.style.cssText = `
  113. visibility:hidden;
  114. `)
  115. }
  116. /**
  117. * Registers type
  118. * @param type
  119. */
  120. static registerType(type) {
  121. if (type) {
  122. WidgetType[type.toLocaleUpperCase()] = type.toLocaleLowerCase()
  123. }
  124. }
  125. /**
  126. *
  127. * @param type
  128. */
  129. static getWidgetType(type) {
  130. return WidgetType[type.toLocaleUpperCase()] || undefined
  131. }
  132. }
  133. export default Widget