@tarojs/components
Version:
1 lines • 8.77 kB
JavaScript
import{h as t,r as a,c as r,H as o,g as e}from"./p-9137dcab.js";import s from"@tarojs/taro";import{stripSuffix as i,stripBasename as h,addLeadingSlash as n,getCurrentPage as b}from"@tarojs/router/dist/utils";import{c as l}from"./p-de951a46.js";import{i as d}from"./p-d3c7f87d.js";const c=t=>{let a,r=t||"";const o={path:null,query:null,fragment:null};return a=r.indexOf("#"),a>-1&&(o.fragment=r.substring(a+1),r=r.substring(0,a)),a=r.indexOf("?"),a>-1&&(o.query=r.substring(a+1),r=r.substring(0,a)),o.path=r,o};function _(t){return"/"===t.charAt(0)}function u(t,a){for(var r=a,o=r+1,e=t.length;o<e;r+=1,o+=1)t[r]=t[o];t.pop()}const g=({index:a,isSelected:r=!1,textColor:o,iconPath:e,badgeText:i,showRedDot:h=!1,pagePath:n,text:b,onSelect:c})=>{const _=l("weui-tabbar__item",{"weui-bar__item_on":r});return t("a",{key:a,href:"javascript:;",class:_,onClick:function(){const t=s.getCurrentPages().shift();"function"==typeof(null==t?void 0:t.onTabItemTap)&&d(this)&&t.onTabItemTap({index:a,pagePath:n,text:b}),c(a)}},t("span",{style:{display:"inline-block",position:"relative"}},t("img",{src:e,alt:"",class:"weui-tabbar__icon"}),!!i&&t("span",{class:"weui-badge taro-tabbar-badge",style:{position:"absolute",top:"-2px",right:"-13px"}},i),h&&t("span",{class:"weui-badge weui-badge_dot",style:{position:"absolute",top:"0",right:"-6px"}})),t("p",{class:"weui-tabbar__label",style:{color:o}},b))},p="taro-tabbar__tabbar",f=class{constructor(t){a(this,t),this.onLongPress=r(this,"longpress",7),this.homePage="",this.customRoutes=[],this.tabbarPos="bottom",this.getOriginUrl=t=>{const a=this.customRoutes.filter((([,a])=>c(a).path===c(t).path));return i(a.length?a[0][0]:t,".html")},this.getSelectedIndex=t=>{let a=-1;return this.list.forEach((({pagePath:r},o)=>{c(t).path===c(r).path&&(a=o)})),a},this.switchTab=t=>{this.selectedIndex=t,s.switchTab({url:this.list[t].pagePath})},this.switchTabHandler=({url:t,successHandler:a,errorHandler:r})=>{const o=function(t,a){void 0===a&&(a="");var r,o=t&&t.split("/")||[],e=a&&a.split("/")||[],s=t&&_(t),i=a&&_(a),h=s||i;if(t&&_(t)?e=o:o.length&&(e.pop(),e=e.concat(o)),!e.length)return"/";if(e.length){var n=e[e.length-1];r="."===n||".."===n||""===n}else r=!1;for(var b=0,l=e.length;l>=0;l--){var d=e[l];"."===d?u(e,l):".."===d?(u(e,l),b++):b&&(u(e,l),b--)}if(!h)for(;b--;b)e.unshift("..");!h||""===e[0]||e[0]&&_(e[0])||e.unshift("");var c=e.join("/");return r&&"/"!==c.substr(-1)&&(c+="/"),c}(t,this.getOriginUrl(this.getCurrentUrl()||this.homePage)),e=this.getSelectedIndex(o);e>-1?(this.switchTab(e),a({errMsg:"switchTab:ok"})):r({errMsg:`switchTab:fail page "${o}" is not found`})},this.routerChangeHandler=t=>{var a;const r=null===(a=null==t?void 0:t.toLocation)||void 0===a?void 0:a.path;let o;if("string"==typeof r){const t=this.conf.basename||"/";o=h(n(r||this.homePage),t)||"/"}else o=this.getCurrentUrl();this.selectedIndex=this.getSelectedIndex(this.getOriginUrl(o))},this.setTabBarBadgeHandler=({index:t,text:a,successHandler:r,errorHandler:o})=>{const e=[...this.list];t in e?(e[t].showRedDot=!1,e[t].badgeText=a,r({errMsg:"setTabBarBadge:ok"})):o({errMsg:"setTabBarBadge:fail tabbar item not found"}),this.list=e},this.removeTabBarBadgeHandler=({index:t,successHandler:a,errorHandler:r})=>{const o=[...this.list];t in o?(o[t].badgeText=null,o[t].badgeText=null,a({errMsg:"removeTabBarBadge:ok"})):r({errMsg:"removeTabBarBadge:fail tabbar item not found"}),this.list=o},this.showTabBarRedDotHandler=({index:t,successHandler:a,errorHandler:r})=>{const o=[...this.list];t in o?(o[t].badgeText=null,o[t].showRedDot=!0,a({errMsg:"showTabBarRedDot:ok"})):r({errMsg:"showTabBarRedDot:fail tabbar item not found"}),this.list=o},this.hideTabBarRedDotHandler=({index:t,successHandler:a,errorHandler:r})=>{const o=[...this.list];t in o?(o[t].showRedDot=!1,a({errMsg:"hideTabBarRedDot:ok"})):r({errMsg:"hideTabBarRedDot:fail tabbar item not found"}),this.list=o},this.showTabBarHandler=({successHandler:t})=>{this.status=0,t({errMsg:"showTabBar:ok"})},this.hideTabBarHandler=({animation:t,successHandler:a})=>{this.status=t?2:1,a({errMsg:"hideTabBar:ok"})},this.setTabBarStyleHandler=({color:t,selectedColor:a,backgroundColor:r,borderStyle:o,successHandler:e})=>{r&&(this.backgroundColor=r),o&&(this.borderStyle=o),t&&(this.color=t),a&&(this.selectedColor=a),e({errMsg:"setTabBarStyle:ok"})},this.setTabBarItemHandler=({index:t,iconPath:a,selectedIconPath:r,text:o,successHandler:e,errorHandler:s})=>{const i=[...this.list];t in i?(a&&(i[t].iconPath=a),r&&(i[t].selectedIconPath=r),o&&(i[t].text=o),e({errMsg:"setTabBarItem:ok"})):s({errMsg:"setTabBarItem:fail tabbar item not found"}),this.list=i},this.conf=void 0,this.list=void 0,this.borderStyle=void 0,this.backgroundColor=void 0,this.color=void 0,this.selectedColor=void 0,this.selectedIndex=-1,this.status=0}componentWillLoad(){var t,a;const r=(null===(t=this.conf)||void 0===t?void 0:t.list)||[],o=(null===(a=this.conf)||void 0===a?void 0:a.customRoutes)||{};if("[object Array]"!==Object.prototype.toString.call(r)||r.length<2||r.length>5)throw new Error("tabBar 配置错误");this.homePage=n(this.conf.homePage);for(let t in o){const a=o[t];t=n(t),"string"==typeof a?this.customRoutes.push([t,n(a)]):(null==a?void 0:a.length)>0&&this.customRoutes.push(...a.map((a=>[t,n(a)])))}r.forEach((t=>{0!==t.pagePath.indexOf("/")&&(t.pagePath="/"+t.pagePath)})),this.list=r,this.borderStyle=this.conf.borderStyle,this.backgroundColor=this.conf.backgroundColor,this.color=this.conf.color,this.selectedColor=this.conf.selectedColor}getCurrentUrl(){const t=b(this.conf.mode,this.conf.basename);return decodeURI("/"===t?this.homePage:t)}bindEvent(){s.eventCenter.on("__taroRouterChange",this.routerChangeHandler),s.eventCenter.on("__taroSwitchTab",this.switchTabHandler),s.eventCenter.on("__taroSetTabBarBadge",this.setTabBarBadgeHandler),s.eventCenter.on("__taroRemoveTabBarBadge",this.removeTabBarBadgeHandler),s.eventCenter.on("__taroShowTabBarRedDotHandler",this.showTabBarRedDotHandler),s.eventCenter.on("__taroHideTabBarRedDotHandler",this.hideTabBarRedDotHandler),s.eventCenter.on("__taroShowTabBar",this.showTabBarHandler),s.eventCenter.on("__taroHideTabBar",this.hideTabBarHandler),s.eventCenter.on("__taroSetTabBarStyle",this.setTabBarStyleHandler),s.eventCenter.on("__taroSetTabBarItem",this.setTabBarItemHandler)}removeEvent(){s.eventCenter.off("__taroRouterChange",this.routerChangeHandler),s.eventCenter.off("__taroSwitchTab",this.switchTabHandler),s.eventCenter.off("__taroSetTabBarBadge",this.setTabBarBadgeHandler),s.eventCenter.off("__taroRemoveTabBarBadge",this.removeTabBarBadgeHandler),s.eventCenter.off("__taroShowTabBarRedDotHandler",this.showTabBarRedDotHandler),s.eventCenter.off("__taroHideTabBarRedDotHandler",this.hideTabBarRedDotHandler),s.eventCenter.off("__taroShowTabBar",this.showTabBarHandler),s.eventCenter.off("__taroHideTabBar",this.hideTabBarHandler),s.eventCenter.off("__taroSetTabBarStyle",this.setTabBarStyleHandler),s.eventCenter.off("__taroSetTabBarItem",this.setTabBarItemHandler)}componentDidLoad(){this.tabbarPos=this.tabbar.nextElementSibling?"top":"bottom",this.bindEvent(),this.routerChangeHandler()}disconnectedCallback(){this.removeEvent()}render(){const{tabbarPos:a="bottom"}=this,r=this.status,e=l("weui-tabbar",{[`taro-tabbar__border-${this.borderStyle||"black"}`]:!0});return t(o,{class:l(p,`${p}-${a}`,{"taro-tabbar__tabbar-hide":-1===this.selectedIndex||1===r,"taro-tabbar__tabbar-slideout":2===r})},t("div",{class:e,style:{backgroundColor:this.backgroundColor||"",height:"inherit"}},this.list.map(((a,r)=>{const o=this.selectedIndex===r;let e,s;return o?(e=this.selectedColor||"",s=a.selectedIconPath):(e=this.color||"",s=a.iconPath),t(g,{index:r,onSelect:this.switchTab.bind(this),isSelected:o,textColor:e,iconPath:s,pagePath:a.pagePath,text:a.text,badgeText:a.badgeText,showRedDot:a.showRedDot})}))))}get tabbar(){return e(this)}};f.style="html,body{height:100%}:root{--taro-tabbar-height:50px}#app{height:100%}.taro-tabbar__border-white::before{border-top-color:#fff !important}.taro-tabbar__container{display:-ms-flexbox;display:flex;overflow:hidden;-ms-flex-direction:column;flex-direction:column;height:100%}.taro-tabbar__panel{overflow:auto;position:relative;-ms-flex:1;flex:1;-webkit-overflow-scrolling:auto}.taro-tabbar__tabbar{position:relative;width:100%;height:var(--taro-tabbar-height);-webkit-transition:bottom 0.2s, top 0.2s;transition:bottom 0.2s, top 0.2s}.taro-tabbar__tabbar-top{top:0}.taro-tabbar__tabbar-bottom{bottom:0;margin-bottom:constant(safe-area-inset-bottom);margin-bottom:env(safe-area-inset-bottom)}.taro-tabbar__tabbar-hide{display:none}.taro-tabbar__tabbar-slideout{top:-52px;-ms-flex:0 0;flex:0 0}.taro-tabbar__panel+.taro-tabbar__tabbar-slideout{top:auto;bottom:-52px}";export{f as taro_tabbar}