navbar.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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}-navbar`;
  12. let Navbar = class Navbar extends SuperComponent {
  13. constructor() {
  14. super(...arguments);
  15. this.externalClasses = [
  16. `${prefix}-class`,
  17. `${prefix}-class-title`,
  18. `${prefix}-class-left`,
  19. `${prefix}-class-center`,
  20. `${prefix}-class-left-icon`,
  21. `${prefix}-class-home-icon`,
  22. `${prefix}-class-capsule`,
  23. `${prefix}-class-nav-btn`,
  24. ];
  25. this.timer = null;
  26. this.options = {
  27. addGlobalClass: true,
  28. multipleSlots: true,
  29. };
  30. this.properties = props;
  31. this.observers = {
  32. visible(visible) {
  33. const { animation } = this.properties;
  34. const visibleClass = `${name}${visible ? '--visible' : '--hide'}`;
  35. this.setData({
  36. visibleClass: `${visibleClass}${animation ? '-animation' : ''}`,
  37. });
  38. if (this.timer) {
  39. clearTimeout(this.timer);
  40. }
  41. if (animation) {
  42. this.timer = setTimeout(() => {
  43. this.setData({
  44. visibleClass,
  45. });
  46. }, 300);
  47. }
  48. },
  49. 'title,titleMaxLength'() {
  50. const { title } = this.properties;
  51. const titleMaxLength = this.properties.titleMaxLength || Number.MAX_SAFE_INTEGER;
  52. let temp = title.slice(0, titleMaxLength);
  53. if (titleMaxLength < title.length)
  54. temp += '...';
  55. this.setData({
  56. showTitle: temp,
  57. });
  58. },
  59. };
  60. this.data = {
  61. prefix,
  62. classPrefix: name,
  63. boxStyle: '',
  64. showTitle: '',
  65. };
  66. this.methods = {
  67. goBack() {
  68. const { delta } = this.data;
  69. const that = this;
  70. this.triggerEvent('go-back');
  71. if (delta > 0) {
  72. wx.navigateBack({
  73. delta,
  74. fail(e) {
  75. that.triggerEvent('fail', e);
  76. },
  77. complete(e) {
  78. that.triggerEvent('complete', e);
  79. },
  80. success(e) {
  81. that.triggerEvent('success', e);
  82. },
  83. });
  84. }
  85. },
  86. };
  87. }
  88. attached() {
  89. let rect = null;
  90. if (wx.getMenuButtonBoundingClientRect) {
  91. rect = wx.getMenuButtonBoundingClientRect();
  92. }
  93. if (!rect)
  94. return;
  95. wx.getSystemInfo({
  96. success: (res) => {
  97. const boxStyleList = [];
  98. const { statusBarHeight } = wx.getSystemInfoSync();
  99. boxStyleList.push(`--td-navbar-padding-top:${statusBarHeight}px`);
  100. if (rect && (res === null || res === void 0 ? void 0 : res.windowWidth)) {
  101. boxStyleList.push(`--td-navbar-right:${res.windowWidth - rect.left}px`);
  102. }
  103. boxStyleList.push(`--td-navbar-capsule-height: ${rect.height}px`);
  104. boxStyleList.push(`--td-navbar-capsule-width:${rect.width}px`);
  105. this.setData({
  106. boxStyle: `${boxStyleList.join('; ')}`,
  107. });
  108. },
  109. fail: (err) => {
  110. console.error('navbar 获取系统信息失败', err);
  111. },
  112. });
  113. }
  114. };
  115. Navbar = __decorate([
  116. wxComponent()
  117. ], Navbar);
  118. export default Navbar;