navbar.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. 't-class',
  17. 't-class-title',
  18. 't-class-left',
  19. 't-class-center',
  20. 't-class-left-icon',
  21. 't-class-home-icon',
  22. 't-class-capsule',
  23. 't-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. fixed(fixed) {
  50. this.setData({
  51. fixedClass: fixed ? `${name}--fixed` : '',
  52. });
  53. },
  54. background(background) {
  55. const list = [];
  56. if (background)
  57. list.push(`background: ${background}`);
  58. this.setData({
  59. contentStyle: list.join(';'),
  60. });
  61. },
  62. 'homeIcon, leftIcon'() {
  63. this.calcLeftBtn();
  64. },
  65. 'title,titleMaxLength'() {
  66. const { title } = this.properties;
  67. const titleMaxLength = this.properties.titleMaxLength || Number.MAX_SAFE_INTEGER;
  68. let temp = title.slice(0, titleMaxLength);
  69. if (titleMaxLength < title.length)
  70. temp += '...';
  71. this.setData({
  72. showTitle: temp,
  73. });
  74. },
  75. };
  76. this.data = {
  77. hasHomeIcon: false,
  78. hasBackIcon: false,
  79. classPrefix: name,
  80. fixedClass: `${name}--fixed`,
  81. contentStyle: '',
  82. boxStyle: '',
  83. opacity: 0.1,
  84. ios: false,
  85. delta: 1,
  86. showTitle: '',
  87. };
  88. }
  89. attached() {
  90. this.calcLeftBtn(); // 根据页面栈来决定展示返回按钮还是home按钮
  91. // 场景值为1177(视频号直播间)和1175 (视频号profile页)时,小程序禁用了 wx.getMenuButtonBoundingClientRect
  92. let rect = null;
  93. if (wx.getMenuButtonBoundingClientRect) {
  94. rect = wx.getMenuButtonBoundingClientRect();
  95. }
  96. wx.getSystemInfo({
  97. success: (res) => {
  98. const ios = !!(res.system.toLowerCase().search('ios') + 1);
  99. const navbarHeight = ios ? 44 : 48;
  100. const boxStyleList = [];
  101. boxStyleList.push(`--narbar-padding-top:${(rect.bottom + rect.top) / 2 - navbarHeight / 2}px;`);
  102. if (rect && (res === null || res === void 0 ? void 0 : res.windowWidth)) {
  103. boxStyleList.push(`--navbar-right:${res.windowWidth - rect.left}px;`); // 导航栏右侧小程序胶囊按钮宽度
  104. }
  105. boxStyleList.push(`--capsule-height:${rect.height}px;`); // 胶囊高度
  106. boxStyleList.push(`--capsule-wight:${rect.width}px;`); // 胶囊宽度
  107. boxStyleList.push(`--navbar-height:${navbarHeight}px;`); // navbar高度
  108. this.setData({
  109. ios,
  110. boxStyle: boxStyleList.join(';'),
  111. });
  112. },
  113. fail: (err) => {
  114. console.error('navbar 获取系统信息失败', err);
  115. },
  116. });
  117. }
  118. calcLeftBtn() {
  119. const { homeIcon, leftIcon } = this.properties;
  120. let home = false;
  121. let back = false;
  122. if (homeIcon)
  123. home = true;
  124. if (leftIcon)
  125. back = true;
  126. this.setData({
  127. hasHomeIcon: home,
  128. hasBackIcon: back,
  129. });
  130. }
  131. goHome() {
  132. this.triggerEvent('go-home');
  133. }
  134. goBack() {
  135. const { delta } = this.data;
  136. // eslint-disable-next-line
  137. const that = this;
  138. this.triggerEvent('go-back');
  139. if (delta > 0) {
  140. wx.navigateBack({
  141. delta,
  142. fail(e) {
  143. that.triggerEvent('fail', e);
  144. },
  145. complete(e) {
  146. that.triggerEvent('complete', e);
  147. },
  148. success(e) {
  149. that.triggerEvent('success', e);
  150. },
  151. });
  152. }
  153. }
  154. };
  155. Navbar = __decorate([
  156. wxComponent()
  157. ], Navbar);
  158. export default Navbar;
  159. //# sourceMappingURL=navbar.js.map