message.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import { SuperComponent, wxComponent } from '../common/src/index';
  8. import config from '../common/config';
  9. import props from './props';
  10. const { prefix } = config;
  11. const name = `${prefix}-message`;
  12. // 展示动画持续时间
  13. const SHOW_DURATION = 500;
  14. let Message = class Message extends SuperComponent {
  15. constructor() {
  16. super(...arguments);
  17. this.externalClasses = ['t-class', 't-class-content', 't-class-icon', 't-class-action', 't-class-close-btn'];
  18. this.options = {
  19. styleIsolation: 'apply-shared',
  20. multipleSlots: true,
  21. };
  22. // 组件的对外属性
  23. this.properties = Object.assign({}, props);
  24. // 组件的内部数据
  25. this.data = {
  26. prefix,
  27. classPrefix: name,
  28. visible: false,
  29. loop: -1,
  30. animation: [],
  31. showAnimation: [],
  32. iconName: '',
  33. wrapTop: -92,
  34. };
  35. this.observers = {
  36. marquee(val) {
  37. if (JSON.stringify(val) === '{}') {
  38. this.setData({
  39. marquee: {
  40. speed: 50,
  41. loop: -1,
  42. delay: 5000,
  43. },
  44. });
  45. }
  46. },
  47. };
  48. /** 延时关闭句柄 */
  49. this.closeTimeoutContext = 0;
  50. /** 动画句柄 */
  51. this.nextAnimationContext = 0;
  52. this.resetAnimation = wx.createAnimation({
  53. duration: 0,
  54. timingFunction: 'linear',
  55. });
  56. // 入场动画
  57. this.showAnimation = wx
  58. .createAnimation({ duration: SHOW_DURATION, timingFunction: 'ease' })
  59. .translateY(0)
  60. .opacity(1)
  61. .step()
  62. .export();
  63. // 出场动画
  64. this.hideAnimation = wx
  65. .createAnimation({ duration: SHOW_DURATION, timingFunction: 'ease' })
  66. .translateY(this.data.wrapTop)
  67. .opacity(0)
  68. .step()
  69. .export();
  70. }
  71. ready() {
  72. this.memoInitalData();
  73. this.setIcon();
  74. }
  75. /** 记录组件设置的项目 */
  76. memoInitalData() {
  77. this.initalData = Object.assign(Object.assign({}, this.properties), this.data);
  78. }
  79. resetData(cb) {
  80. this.setData(Object.assign({}, this.initalData), cb);
  81. }
  82. detached() {
  83. this.clearMessageAnimation();
  84. }
  85. /** icon 值设置 */
  86. setIcon(icon = this.properties.icon) {
  87. // 使用空值
  88. if (!icon) {
  89. this.setData({ iconName: '' });
  90. return;
  91. }
  92. // 固定值
  93. if (typeof icon === 'string') {
  94. this.setData({
  95. iconName: `${icon}`,
  96. });
  97. return;
  98. }
  99. // 使用默认值
  100. if (icon) {
  101. let nextValue = 'notification';
  102. const { theme } = this.properties;
  103. const themeMessage = {
  104. info: 'error-circle',
  105. success: 'check-circle',
  106. warning: 'error-circle',
  107. error: 'error-circle',
  108. };
  109. nextValue = themeMessage[theme];
  110. this.setData({ iconName: nextValue });
  111. }
  112. }
  113. /** 检查是否需要开启一个新的动画循环 */
  114. checkAnimation() {
  115. const speeding = this.properties.marquee.speed;
  116. if (!this.properties.marquee) {
  117. return;
  118. }
  119. if (this.data.loop > 0) {
  120. this.data.loop -= 1;
  121. }
  122. else if (this.data.loop === 0) {
  123. // 动画回到初始位置
  124. this.setData({ animation: this.resetAnimation.translateX(0).step().export() });
  125. return;
  126. }
  127. if (this.nextAnimationContext) {
  128. this.clearMessageAnimation();
  129. }
  130. const warpID = `#${name}__text-wrap`;
  131. const nodeID = `#${name}__text`;
  132. Promise.all([this.queryWidth(nodeID), this.queryWidth(warpID)]).then(([nodeWidth, warpWidth]) => {
  133. this.setData({
  134. animation: this.resetAnimation.translateX(warpWidth).step().export(),
  135. }, () => {
  136. const durationTime = ((nodeWidth + warpWidth) / speeding) * 1000;
  137. const nextAnimation = wx
  138. .createAnimation({
  139. // 默认50px/s
  140. duration: durationTime,
  141. })
  142. .translateX(-nodeWidth)
  143. .step()
  144. .export();
  145. // 这里就只能用 setTimeout/20, nextTick 没用
  146. // 不用这个的话会出现reset动画没跑完就开始跑这个等的奇怪问题
  147. setTimeout(() => {
  148. this.nextAnimationContext = setTimeout(this.checkAnimation.bind(this), durationTime);
  149. this.setData({ animation: nextAnimation });
  150. }, 20);
  151. });
  152. });
  153. }
  154. /** 获取元素宽度 */
  155. queryWidth(queryName) {
  156. return new Promise((resolve) => {
  157. this.createSelectorQuery()
  158. .select(queryName)
  159. .boundingClientRect(({ width }) => {
  160. resolve(width);
  161. })
  162. .exec();
  163. });
  164. }
  165. /** 获取元素长度 */
  166. queryHeight(queryName) {
  167. return new Promise((resolve) => {
  168. this.createSelectorQuery()
  169. .select(queryName)
  170. .boundingClientRect(({ height }) => {
  171. resolve(height);
  172. })
  173. .exec();
  174. });
  175. }
  176. /** 清理动画循环 */
  177. clearMessageAnimation() {
  178. clearTimeout(this.nextAnimationContext);
  179. this.nextAnimationContext = 0;
  180. }
  181. show() {
  182. const { duration, icon } = this.properties;
  183. this.setData({ visible: true, loop: this.properties.marquee.loop });
  184. this.reset();
  185. this.setIcon(icon);
  186. this.checkAnimation();
  187. if (duration && duration > 0) {
  188. this.closeTimeoutContext = setTimeout(() => {
  189. this.hide();
  190. this.triggerEvent('durationEnd', { self: this });
  191. }, duration);
  192. }
  193. const wrapID = `#${name}`;
  194. this.queryHeight(wrapID).then((wrapHeight) => {
  195. // 先根据 message 的实际高度设置绝对定位的 top 值,再开始显示动画
  196. this.setData({ wrapTop: -wrapHeight }, () => {
  197. this.setData({ showAnimation: this.showAnimation });
  198. });
  199. });
  200. }
  201. hide() {
  202. this.reset();
  203. this.setData({ showAnimation: this.hideAnimation });
  204. setTimeout(() => {
  205. this.setData({ visible: false, animation: [] });
  206. }, SHOW_DURATION);
  207. }
  208. // 重置定时器
  209. reset() {
  210. if (this.nextAnimationContext) {
  211. this.clearMessageAnimation();
  212. }
  213. clearTimeout(this.closeTimeoutContext);
  214. this.closeTimeoutContext = 0;
  215. }
  216. handleClose() {
  217. this.hide();
  218. this.triggerEvent('closeBtnClick');
  219. }
  220. handleBtnClick() {
  221. this.triggerEvent('actionBtnClick', { self: this });
  222. }
  223. };
  224. Message = __decorate([
  225. wxComponent()
  226. ], Message);
  227. export default Message;
  228. //# sourceMappingURL=message.js.map