运维小程序
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.

navbar.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 } 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. };
  70. this.methods = {
  71. queryElements(capsuleRect) {
  72. Promise.all([
  73. getRect(this, `.${this.data.classPrefix}__left`),
  74. getRect(this, `.${this.data.classPrefix}__center`),
  75. ]).then(([leftRect, centerRect]) => {
  76. if (leftRect.right > capsuleRect.left) {
  77. this.setData({
  78. hideLeft: true,
  79. hideCenter: true,
  80. });
  81. }
  82. else if (centerRect.right > capsuleRect.left) {
  83. this.setData({
  84. hideLeft: false,
  85. hideCenter: true,
  86. });
  87. }
  88. else {
  89. this.setData({
  90. hideLeft: false,
  91. hideCenter: false,
  92. });
  93. }
  94. });
  95. },
  96. goBack() {
  97. const { delta } = this.data;
  98. const that = this;
  99. this.triggerEvent('go-back');
  100. if (delta > 0) {
  101. wx.navigateBack({
  102. delta,
  103. fail(e) {
  104. that.triggerEvent('fail', e);
  105. },
  106. complete(e) {
  107. that.triggerEvent('complete', e);
  108. },
  109. success(e) {
  110. that.triggerEvent('success', e);
  111. },
  112. });
  113. }
  114. },
  115. };
  116. }
  117. attached() {
  118. let rect = null;
  119. if (wx.getMenuButtonBoundingClientRect) {
  120. rect = wx.getMenuButtonBoundingClientRect();
  121. }
  122. if (!rect)
  123. return;
  124. wx.getSystemInfo({
  125. success: (res) => {
  126. const boxStyleList = [];
  127. boxStyleList.push(`--td-navbar-padding-top: ${res.statusBarHeight}px`);
  128. if (rect && (res === null || res === void 0 ? void 0 : res.windowWidth)) {
  129. boxStyleList.push(`--td-navbar-right: ${res.windowWidth - rect.left}px`);
  130. }
  131. boxStyleList.push(`--td-navbar-capsule-height: ${rect.height}px`);
  132. boxStyleList.push(`--td-navbar-capsule-width: ${rect.width}px`);
  133. boxStyleList.push(`--td-navbar-height: ${(rect.top - res.statusBarHeight) * 2 + rect.height}px`);
  134. this.setData({
  135. boxStyle: `${boxStyleList.join('; ')}`,
  136. });
  137. if (wx.onMenuButtonBoundingClientRectWeightChange) {
  138. wx.onMenuButtonBoundingClientRectWeightChange((res) => this.queryElements(res));
  139. }
  140. },
  141. fail: (err) => {
  142. console.error('navbar 获取系统信息失败', err);
  143. },
  144. });
  145. }
  146. detached() {
  147. if (wx.offMenuButtonBoundingClientRectWeightChange) {
  148. wx.offMenuButtonBoundingClientRectWeightChange((res) => this.queryElements(res));
  149. }
  150. }
  151. };
  152. Navbar = __decorate([
  153. wxComponent()
  154. ], Navbar);
  155. export default Navbar;