index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/classNames'
  3. import styleToCssString from '../helpers/styleToCssString'
  4. const getPlacements = ([a, s, b] = rects, placement = 'top') => {
  5. switch (placement) {
  6. case 'topLeft':
  7. return {
  8. top: s.scrollTop + a.top - b.height - 4,
  9. left: s.scrollLeft + a.left,
  10. }
  11. case 'top':
  12. return {
  13. top: s.scrollTop + a.top - b.height - 4,
  14. left: s.scrollLeft + a.left + (a.width - b.width) / 2,
  15. }
  16. case 'topRight':
  17. return {
  18. top: s.scrollTop + a.top - b.height - 4,
  19. left: s.scrollLeft + a.left + a.width - b.width,
  20. }
  21. case 'rightTop':
  22. return {
  23. top: s.scrollTop + a.top,
  24. left: s.scrollLeft + a.left + a.width + 4,
  25. }
  26. case 'right':
  27. return {
  28. top: s.scrollTop + a.top + (a.height - b.height) / 2,
  29. left: s.scrollLeft + a.left + a.width + 4,
  30. }
  31. case 'rightBottom':
  32. return {
  33. top: s.scrollTop + a.top + a.height - b.height,
  34. left: s.scrollLeft + a.left + a.width + 4,
  35. }
  36. case 'bottomRight':
  37. return {
  38. top: s.scrollTop + a.top + a.height + 4,
  39. left: s.scrollLeft + a.left + a.width - b.width,
  40. }
  41. case 'bottom':
  42. return {
  43. top: s.scrollTop + a.top + a.height + 4,
  44. left: s.scrollLeft + a.left + (a.width - b.width) / 2,
  45. }
  46. case 'bottomLeft':
  47. return {
  48. top: s.scrollTop + a.top + a.height + 4,
  49. left: s.scrollLeft + a.left,
  50. }
  51. case 'leftBottom':
  52. return {
  53. top: s.scrollTop + a.top + a.height - b.height,
  54. left: s.scrollLeft + a.left - b.width - 4,
  55. }
  56. case 'left':
  57. return {
  58. top: s.scrollTop + a.top + (a.height - b.height) / 2,
  59. left: s.scrollLeft + a.left - b.width - 4,
  60. }
  61. case 'leftTop':
  62. return {
  63. top: s.scrollTop + a.top,
  64. left: s.scrollLeft + a.left - b.width - 4,
  65. }
  66. default:
  67. return {
  68. left: 0,
  69. top: 0,
  70. }
  71. }
  72. }
  73. baseComponent({
  74. properties: {
  75. prefixCls: {
  76. type: String,
  77. value: 'wux-popover',
  78. },
  79. classNames: {
  80. type: null,
  81. value: 'wux-animate--fadeIn',
  82. },
  83. theme: {
  84. type: String,
  85. value: 'light',
  86. },
  87. title: {
  88. type: String,
  89. value: '',
  90. },
  91. content: {
  92. type: String,
  93. value: '',
  94. },
  95. placement: {
  96. type: String,
  97. value: 'top',
  98. },
  99. trigger: {
  100. type: String,
  101. value: 'click',
  102. },
  103. bodyStyle: {
  104. type: [String, Object],
  105. value: '',
  106. observer(newVal) {
  107. this.setData({
  108. extStyle: styleToCssString(newVal),
  109. })
  110. },
  111. },
  112. defaultVisible: {
  113. type: Boolean,
  114. value: false,
  115. },
  116. visible: {
  117. type: Boolean,
  118. value: false,
  119. observer(newVal) {
  120. if (this.data.controlled) {
  121. this.setData({
  122. popoverVisible: newVal,
  123. })
  124. }
  125. },
  126. },
  127. controlled: {
  128. type: Boolean,
  129. value: false,
  130. },
  131. },
  132. data: {
  133. extStyle: '',
  134. popoverStyle: '',
  135. popoverVisible: false,
  136. },
  137. computed: {
  138. classes() {
  139. const { prefixCls, theme, placement } = this.data
  140. const wrap = classNames(prefixCls, {
  141. [`${prefixCls}--theme-${theme}`]: theme,
  142. [`${prefixCls}--placement-${placement}`]: placement,
  143. })
  144. const content = `${prefixCls}__content`
  145. const arrow = `${prefixCls}__arrow`
  146. const inner = `${prefixCls}__inner`
  147. const title = `${prefixCls}__title`
  148. const innerContent = `${prefixCls}__inner-content`
  149. const element = `${prefixCls}__element`
  150. return {
  151. wrap,
  152. content,
  153. arrow,
  154. inner,
  155. title,
  156. innerContent,
  157. element,
  158. }
  159. },
  160. },
  161. methods: {
  162. getPopoverStyle() {
  163. const { prefixCls } = this.data
  164. const query = wx.createSelectorQuery().in(this)
  165. query.select(`.${prefixCls}__element`).boundingClientRect()
  166. query.selectViewport().scrollOffset()
  167. query.select(`.${prefixCls}`).boundingClientRect()
  168. query.exec((rects) => {
  169. if (rects.filter((n) => !n).length) return
  170. const placements = getPlacements(rects, this.data.placement)
  171. this.setData({
  172. popoverStyle: styleToCssString(placements),
  173. })
  174. })
  175. },
  176. /**
  177. * 当组件进入过渡的开始状态时,设置气泡框位置信息
  178. */
  179. onEnter() {
  180. this.getPopoverStyle()
  181. },
  182. fireEvents() {
  183. const { popoverVisible, controlled } = this.data
  184. const nextVisible = !popoverVisible
  185. if (!controlled) {
  186. this.setData({
  187. popoverVisible: nextVisible,
  188. })
  189. }
  190. this.triggerEvent('change', { visible: nextVisible })
  191. },
  192. onClick() {
  193. if (this.data.trigger === 'click') {
  194. this.fireEvents()
  195. }
  196. },
  197. },
  198. attached() {
  199. const { defaultVisible, visible, controlled } = this.data
  200. const popoverVisible = controlled ? visible : defaultVisible
  201. this.setData({
  202. popoverVisible,
  203. })
  204. },
  205. })