UNPKG

8.41 kBJavaScriptView Raw
1import{h as t,r as a,c as r,H as e,g as s}from"./p-f05e1558.js";import o from"@tarojs/taro";import{c as i}from"./p-7e5d2d7a.js";const n=t=>{let a,r=t||"";const e={path:null,query:null,fragment:null};return a=r.indexOf("#"),a>-1&&(e.fragment=r.substring(a+1),r=r.substring(0,a)),a=r.indexOf("?"),a>-1&&(e.query=r.substring(a+1),r=r.substring(0,a)),e.path=r,e};function h(t){return"/"===t.charAt(0)}function b(t,a){for(var r=a,e=r+1,s=t.length;e<s;r+=1,e+=1)t[r]=t[e];t.pop()}const l=({index:a,isSelected:r=!1,textColor:e,iconPath:s,badgeText:o,showRedDot:n=!1,text:h,onSelect:b})=>{const l=i("weui-tabbar__item",{"weui-bar__item_on":r});return t("a",{key:a,href:"javascript:;",class:l,onClick:function(){b(a)}},t("span",{style:{display:"inline-block",position:"relative"}},t("img",{src:s,alt:"",class:"weui-tabbar__icon"}),!!o&&t("span",{class:"weui-badge taro-tabbar-badge",style:{position:"absolute",top:"-2px",right:"-13px"}},o),n&&t("span",{class:"weui-badge weui-badge_dot",style:{position:"absolute",top:"0",right:"-6px"}})),t("p",{class:"weui-tabbar__label",style:{color:e}},h))},d=t=>"/"===t[0]?t:`/${t}`,c=(t,a)=>((t,a)=>new RegExp("^"+a+"(\\/|\\?|#|$)","i").test(t))(t,a)?t.substr(a.length):t;let _=class{constructor(t){a(this,t),this.onLongPress=r(this,"longpress",7),this.homePage="",this.customRoutes=[],this.tabbarPos="bottom",this.selectedIndex=-1,this.status=0,this.getOriginUrl=t=>{const a=this.customRoutes.filter((([,a])=>n(a).path===n(t).path));return a.length?a[0][0]:t},this.getSelectedIndex=t=>{let a=-1;return this.list.forEach((({pagePath:r},e)=>{n(t).path===n(r).path&&(a=e)})),a},this.switchTab=t=>{this.selectedIndex=t,o.switchTab({url:this.list[t].pagePath})},this.switchTabHandler=({url:t,successHandler:a,errorHandler:r})=>{const e=function(t,a){void 0===a&&(a="");var r,e=t&&t.split("/")||[],s=a&&a.split("/")||[],o=t&&h(t),i=a&&h(a),n=o||i;if(t&&h(t)?s=e:e.length&&(s.pop(),s=s.concat(e)),!s.length)return"/";if(s.length){var l=s[s.length-1];r="."===l||".."===l||""===l}else r=!1;for(var d=0,c=s.length;c>=0;c--){var _=s[c];"."===_?b(s,c):".."===_?(b(s,c),d++):d&&(b(s,c),d--)}if(!n)for(;d--;d)s.unshift("..");!n||""===s[0]||s[0]&&h(s[0])||s.unshift("");var u=s.join("/");return r&&"/"!==u.substr(-1)&&(u+="/"),u}(t,this.getOriginUrl(this.getCurrentUrl()||this.homePage)),s=this.getSelectedIndex(e);s>-1?(this.switchTab(s),a({errMsg:"switchTab:ok"})):r({errMsg:`switchTab:fail page "${e}" 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 e;if("string"==typeof r){const t=this.conf.basename||"/";e=c(d(r||this.homePage),t)||"/"}else e=this.getCurrentUrl();this.selectedIndex=this.getSelectedIndex(this.getOriginUrl(e))},this.setTabBarBadgeHandler=({index:t,text:a,successHandler:r,errorHandler:e})=>{const s=[...this.list];t in s?(s[t].showRedDot=!1,s[t].badgeText=a,r({errMsg:"setTabBarBadge:ok"})):e({errMsg:"setTabBarBadge:fail tabbar item not found"}),this.list=s},this.removeTabBarBadgeHandler=({index:t,successHandler:a,errorHandler:r})=>{const e=[...this.list];t in e?(e[t].badgeText=null,e[t].badgeText=null,a({errMsg:"removeTabBarBadge:ok"})):r({errMsg:"removeTabBarBadge:fail tabbar item not found"}),this.list=e},this.showTabBarRedDotHandler=({index:t,successHandler:a,errorHandler:r})=>{const e=[...this.list];t in e?(e[t].badgeText=null,e[t].showRedDot=!0,a({errMsg:"showTabBarRedDot:ok"})):r({errMsg:"showTabBarRedDot:fail tabbar item not found"}),this.list=e},this.hideTabBarRedDotHandler=({index:t,successHandler:a,errorHandler:r})=>{const e=[...this.list];t in e?(e[t].showRedDot=!1,a({errMsg:"hideTabBarRedDot:ok"})):r({errMsg:"hideTabBarRedDot:fail tabbar item not found"}),this.list=e},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:e,successHandler:s})=>{r&&(this.backgroundColor=r),e&&(this.borderStyle=e),t&&(this.color=t),a&&(this.selectedColor=a),s({errMsg:"setTabBarStyle:ok"})},this.setTabBarItemHandler=({index:t,iconPath:a,selectedIconPath:r,text:e,successHandler:s,errorHandler:o})=>{const i=[...this.list];t in i?(a&&(i[t].iconPath=a),r&&(i[t].selectedIconPath=r),e&&(i[t].text=e),s({errMsg:"setTabBarItem:ok"})):o({errMsg:"setTabBarItem:fail tabbar item not found"}),this.list=i};const e=this.conf.list,s=this.conf.customRoutes;if("[object Array]"!==Object.prototype.toString.call(e)||e.length<2||e.length>5)throw new Error("tabBar 配置错误");this.homePage=d(this.conf.homePage);for(let t in s){const a=s[t];t=d(t),"string"==typeof a?this.customRoutes.push([t,d(a)]):(null==a?void 0:a.length)>0&&this.customRoutes.push(...a.map((a=>[t,d(a)])))}e.forEach((t=>{0!==t.pagePath.indexOf("/")&&(t.pagePath="/"+t.pagePath)})),this.list=e,this.borderStyle=this.conf.borderStyle,this.backgroundColor=this.conf.backgroundColor,this.color=this.conf.color,this.selectedColor=this.conf.selectedColor}getCurrentUrl(){const t=this.conf.basename||"/";let a;if("hash"===this.conf.mode){const t=window.location.href,r=t.indexOf("#");a=-1===r?"":t.substring(r+1)}else a=location.pathname;const r=d(c(a,t));return"/"===r?this.homePage:r}bindEvent(){o.eventCenter.on("__taroRouterChange",this.routerChangeHandler),o.eventCenter.on("__taroSwitchTab",this.switchTabHandler),o.eventCenter.on("__taroSetTabBarBadge",this.setTabBarBadgeHandler),o.eventCenter.on("__taroRemoveTabBarBadge",this.removeTabBarBadgeHandler),o.eventCenter.on("__taroShowTabBarRedDotHandler",this.showTabBarRedDotHandler),o.eventCenter.on("__taroHideTabBarRedDotHandler",this.hideTabBarRedDotHandler),o.eventCenter.on("__taroShowTabBar",this.showTabBarHandler),o.eventCenter.on("__taroHideTabBar",this.hideTabBarHandler),o.eventCenter.on("__taroSetTabBarStyle",this.setTabBarStyleHandler),o.eventCenter.on("__taroSetTabBarItem",this.setTabBarItemHandler)}removeEvent(){o.eventCenter.off("__taroRouterChange",this.routerChangeHandler),o.eventCenter.off("__taroSwitchTab",this.switchTabHandler),o.eventCenter.off("__taroSetTabBarBadge",this.setTabBarBadgeHandler),o.eventCenter.off("__taroRemoveTabBarBadge",this.removeTabBarBadgeHandler),o.eventCenter.off("__taroShowTabBarRedDotHandler",this.showTabBarRedDotHandler),o.eventCenter.off("__taroHideTabBarRedDotHandler",this.hideTabBarRedDotHandler),o.eventCenter.off("__taroShowTabBar",this.showTabBarHandler),o.eventCenter.off("__taroHideTabBar",this.hideTabBarHandler),o.eventCenter.off("__taroSetTabBarStyle",this.setTabBarStyleHandler),o.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,s=i("weui-tabbar",{[`taro-tabbar__border-${this.borderStyle||"black"}`]:!0});return t(e,{class:i("taro-tabbar__tabbar",`taro-tabbar__tabbar-${a}`,{"taro-tabbar__tabbar-hide":-1===this.selectedIndex||1===r,"taro-tabbar__tabbar-slideout":2===r})},t("div",{class:s,style:{backgroundColor:this.backgroundColor||"",height:"inherit"}},this.list.map(((a,r)=>{const e=this.selectedIndex===r;let s,o;return e?(s=this.selectedColor||"",o=a.selectedIconPath):(s=this.color||"",o=a.iconPath),t(l,{index:r,onSelect:this.switchTab.bind(this),isSelected:e,textColor:s,iconPath:o,text:a.text,badgeText:a.badgeText,showRedDot:a.showRedDot})}))))}get tabbar(){return s(this)}};_.style="html,body{height:100%}#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:50px;-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{_ as taro_tabbar}
\No newline at end of file