選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Widget.js 2.7KB

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