navbar.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 { getRect, systemInfo } from '../common/utils';
  9. import config from '../common/config';
  10. import props from './props';
  11. const { prefix } = config;
  12. const name = `${prefix}-navbar`;
  13. let Navbar = class Navbar extends SuperComponent {
  14. constructor() {
  15. super(...arguments);
  16. this.externalClasses = [
  17. `${prefix}-class`,
  18. `${prefix}-class-placeholder`,
  19. `${prefix}-class-content`,
  20. `${prefix}-class-title`,
  21. `${prefix}-class-left`,
  22. `${prefix}-class-center`,
  23. `${prefix}-class-left-icon`,
  24. `${prefix}-class-home-icon`,
  25. `${prefix}-class-capsule`,
  26. `${prefix}-class-nav-btn`,
  27. ];
  28. this.timer = null;
  29. this.options = {
  30. multipleSlots: true,
  31. };
  32. this.properties = props;
  33. this.observers = {
  34. visible(visible) {
  35. const { animation } = this.properties;
  36. const visibleClass = `${name}${visible ? '--visible' : '--hide'}`;
  37. this.setData({
  38. visibleClass: `${visibleClass}${animation ? '-animation' : ''}`,
  39. });
  40. if (this.timer) {
  41. clearTimeout(this.timer);
  42. }
  43. if (animation) {
  44. this.timer = setTimeout(() => {
  45. this.setData({
  46. visibleClass,
  47. });
  48. }, 300);
  49. }
  50. },
  51. 'title,titleMaxLength'() {
  52. const { title } = this.properties;
  53. const titleMaxLength = this.properties.titleMaxLength || Number.MAX_SAFE_INTEGER;
  54. let temp = title.slice(0, titleMaxLength);
  55. if (titleMaxLength < title.length)
  56. temp += '...';
  57. this.setData({
  58. showTitle: temp,
  59. });
  60. },
  61. };
  62. this.data = {
  63. prefix,
  64. classPrefix: name,
  65. boxStyle: '',
  66. showTitle: '',
  67. hideLeft: false,
  68. hideCenter: false,
  69. _menuRect: null,
  70. _leftRect: null,
  71. _boxStyle: {},
  72. };
  73. this.methods = {
  74. initStyle() {
  75. this.getMenuRect();
  76. const { _menuRect, _leftRect } = this.data;
  77. if (!_menuRect || !_leftRect || !systemInfo)
  78. return;
  79. const _boxStyle = {
  80. '--td-navbar-padding-top': `${systemInfo.statusBarHeight}px`,
  81. '--td-navbar-right': `${systemInfo.windowWidth - _menuRect.left}px`,
  82. '--td-navbar-left-max-width': `${_menuRect.left}px`,
  83. '--td-navbar-capsule-height': `${_menuRect.height}px`,
  84. '--td-navbar-capsule-width': `${_menuRect.width}px`,
  85. '--td-navbar-height': `${(_menuRect.top - systemInfo.statusBarHeight) * 2 + _menuRect.height}px`,
  86. };
  87. this.calcCenterStyle(_leftRect, _menuRect, _boxStyle);
  88. },
  89. calcCenterStyle(leftRect, menuRect, defaultStyle) {
  90. const maxSpacing = Math.max(leftRect.right, systemInfo.windowWidth - menuRect.left);
  91. const _boxStyle = Object.assign(Object.assign({}, defaultStyle), { '--td-navbar-center-left': `${maxSpacing}px`, '--td-navbar-center-width': `${Math.max(menuRect.left - maxSpacing, 0)}px` });
  92. const boxStyle = Object.entries(_boxStyle)
  93. .map(([k, v]) => `${k}: ${v}`)
  94. .join('; ');
  95. this.setData({
  96. boxStyle,
  97. _boxStyle,
  98. });
  99. },
  100. getLeftRect() {
  101. getRect(this, `.${name}__left`).then((res) => {
  102. if (res.right > this.data._leftRect.right) {
  103. this.calcCenterStyle(res, this.data._menuRect, this.data._boxStyle);
  104. }
  105. });
  106. },
  107. getMenuRect() {
  108. if (wx.getMenuButtonBoundingClientRect) {
  109. const rect = wx.getMenuButtonBoundingClientRect();
  110. this.setData({
  111. _menuRect: rect,
  112. _leftRect: {
  113. right: systemInfo.windowWidth - rect.left,
  114. },
  115. });
  116. }
  117. },
  118. onMenuButtonBoundingClientRectWeightChange() {
  119. if (wx.onMenuButtonBoundingClientRectWeightChange) {
  120. wx.onMenuButtonBoundingClientRectWeightChange((res) => this.queryElements(res));
  121. }
  122. },
  123. offMenuButtonBoundingClientRectWeightChange() {
  124. if (wx.offMenuButtonBoundingClientRectWeightChange) {
  125. wx.offMenuButtonBoundingClientRectWeightChange((res) => this.queryElements(res));
  126. }
  127. },
  128. queryElements(capsuleRect) {
  129. Promise.all([
  130. getRect(this, `.${this.data.classPrefix}__left`),
  131. getRect(this, `.${this.data.classPrefix}__center`),
  132. ]).then(([leftRect, centerRect]) => {
  133. if (Math.round(leftRect.right) > capsuleRect.left) {
  134. this.setData({
  135. hideLeft: true,
  136. hideCenter: true,
  137. });
  138. }
  139. else if (Math.round(centerRect.right) > capsuleRect.left) {
  140. this.setData({
  141. hideLeft: false,
  142. hideCenter: true,
  143. });
  144. }
  145. else {
  146. this.setData({
  147. hideLeft: false,
  148. hideCenter: false,
  149. });
  150. }
  151. });
  152. },
  153. goBack() {
  154. const { delta } = this.data;
  155. const that = this;
  156. this.triggerEvent('go-back');
  157. if (delta > 0) {
  158. wx.navigateBack({
  159. delta,
  160. fail(e) {
  161. that.triggerEvent('fail', e);
  162. },
  163. complete(e) {
  164. that.triggerEvent('complete', e);
  165. },
  166. success(e) {
  167. that.triggerEvent('success', e);
  168. },
  169. });
  170. }
  171. },
  172. };
  173. }
  174. attached() {
  175. this.initStyle();
  176. this.getLeftRect();
  177. this.onMenuButtonBoundingClientRectWeightChange();
  178. }
  179. detached() {
  180. this.offMenuButtonBoundingClientRectWeightChange();
  181. }
  182. };
  183. Navbar = __decorate([
  184. wxComponent()
  185. ], Navbar);
  186. export default Navbar;