navbar.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. showTitle: '',
  86. };
  87. this.methods = {
  88. calcLeftBtn() {
  89. const { homeIcon, leftIcon } = this.properties;
  90. let home = false;
  91. let back = false;
  92. if (homeIcon)
  93. home = true;
  94. if (leftIcon)
  95. back = true;
  96. this.setData({
  97. hasHomeIcon: home,
  98. hasBackIcon: back,
  99. });
  100. },
  101. goHome() {
  102. this.triggerEvent('go-home');
  103. },
  104. goBack() {
  105. const { delta } = this.data;
  106. const that = this;
  107. this.triggerEvent('go-back');
  108. if (delta > 0) {
  109. wx.navigateBack({
  110. delta,
  111. fail(e) {
  112. that.triggerEvent('fail', e);
  113. },
  114. complete(e) {
  115. that.triggerEvent('complete', e);
  116. },
  117. success(e) {
  118. that.triggerEvent('success', e);
  119. },
  120. });
  121. }
  122. },
  123. };
  124. }
  125. attached() {
  126. this.calcLeftBtn();
  127. let rect = null;
  128. if (wx.getMenuButtonBoundingClientRect) {
  129. rect = wx.getMenuButtonBoundingClientRect();
  130. }
  131. if (!rect)
  132. return;
  133. wx.getSystemInfo({
  134. success: (res) => {
  135. const ios = !!(res.system.toLowerCase().search('ios') + 1);
  136. const navbarHeight = ios ? 44 : 48;
  137. const boxStyleList = [];
  138. boxStyleList.push(`--narbar-padding-top:${(rect.bottom + rect.top) / 2 - navbarHeight / 2}px;`);
  139. if (rect && (res === null || res === void 0 ? void 0 : res.windowWidth)) {
  140. boxStyleList.push(`--navbar-right:${res.windowWidth - rect.left}px;`);
  141. }
  142. boxStyleList.push(`--capsule-height:${rect.height}px;`);
  143. boxStyleList.push(`--capsule-width:${rect.width}px;`);
  144. boxStyleList.push(`--navbar-height:${navbarHeight}px;`);
  145. this.setData({
  146. ios,
  147. boxStyle: boxStyleList.join(';'),
  148. });
  149. },
  150. fail: (err) => {
  151. console.error('navbar 获取系统信息失败', err);
  152. },
  153. });
  154. }
  155. };
  156. Navbar = __decorate([
  157. wxComponent()
  158. ], Navbar);
  159. export default Navbar;