pleroma-fe/src/components/tab_switcher/tab_switcher.js

157 行
4.1 KiB
JavaScript
Raw 通常表示 履歴

import Vue from 'vue'
import { mapState } from 'vuex'
2020-10-21 04:54:43 +09:00
import { FontAwesomeIcon as FAIcon } from '@fortawesome/vue-fontawesome'
import './tab_switcher.scss'
export default Vue.component('tab-switcher', {
name: 'TabSwitcher',
props: {
renderOnlyFocused: {
required: false,
type: Boolean,
default: false
},
onSwitch: {
required: false,
2019-11-09 01:14:01 +09:00
type: Function,
default: undefined
},
activeTab: {
required: false,
2019-11-09 01:14:01 +09:00
type: String,
default: undefined
},
scrollableTabs: {
required: false,
type: Boolean,
default: false
2020-05-03 23:36:12 +09:00
},
sideTabBar: {
required: false,
type: Boolean,
default: false
}
},
data () {
return {
active: this.$slots.default.findIndex(_ => _.tag)
}
},
2019-08-11 03:48:05 +09:00
computed: {
activeIndex () {
// In case of controlled component
if (this.activeTab) {
return this.$slots.default.findIndex(slot => this.activeTab === slot.key)
} else {
return this.active
}
},
settingsModalVisible () {
return this.settingsModalState === 'visible'
},
...mapState({
settingsModalState: state => state.interface.settingsModalState
})
2019-08-11 03:48:05 +09:00
},
2019-07-05 16:02:14 +09:00
beforeUpdate () {
const currentSlot = this.$slots.default[this.active]
if (!currentSlot.tag) {
this.active = this.$slots.default.findIndex(_ => _.tag)
}
},
methods: {
clickTab (index) {
return (e) => {
e.preventDefault()
this.setTab(index)
}
},
setTab (index) {
if (typeof this.onSwitch === 'function') {
this.onSwitch.call(null, this.$slots.default[index].key)
}
this.active = index
if (this.scrollableTabs) {
this.$refs.contents.scrollTop = 0
}
}
},
render (h) {
const tabs = this.$slots.default
2019-07-05 16:02:14 +09:00
.map((slot, index) => {
if (!slot.tag) return
const classesTab = ['tab', 'button-default']
2019-07-05 16:02:14 +09:00
const classesWrapper = ['tab-wrapper']
2019-08-11 03:48:05 +09:00
if (this.activeIndex === index) {
2019-07-05 16:02:14 +09:00
classesTab.push('active')
classesWrapper.push('active')
}
if (slot.data.attrs.image) {
return (
2019-08-10 13:26:29 +09:00
<div class={classesWrapper.join(' ')}>
<button
disabled={slot.data.attrs.disabled}
onClick={this.clickTab(index)}
class={classesTab.join(' ')}
type="button"
>
<img src={slot.data.attrs.image} title={slot.data.attrs['image-tooltip']}/>
{slot.data.attrs.label ? '' : slot.data.attrs.label}
</button>
</div>
)
}
2019-07-05 16:02:14 +09:00
return (
2019-08-10 13:26:29 +09:00
<div class={classesWrapper.join(' ')}>
2019-07-05 16:02:14 +09:00
<button
disabled={slot.data.attrs.disabled}
onClick={this.clickTab(index)}
2020-05-25 22:10:14 +09:00
class={classesTab.join(' ')}
type="button"
2020-05-25 22:10:14 +09:00
>
2020-10-21 04:54:43 +09:00
{!slot.data.attrs.icon ? '' : (<FAIcon class="tab-icon" size="2x" fixed-width icon={slot.data.attrs.icon}/>)}
<span class="text">
{slot.data.attrs.label}
</span>
2020-05-25 22:10:14 +09:00
</button>
2019-07-05 16:02:14 +09:00
</div>
)
})
const contents = this.$slots.default.map((slot, index) => {
if (!slot.tag) return
2019-08-11 03:48:05 +09:00
const active = this.activeIndex === index
const classes = [ active ? 'active' : 'hidden' ]
if (slot.data.attrs.fullHeight) {
classes.push('full-height')
}
2020-05-29 03:26:33 +09:00
const renderSlot = (!this.renderOnlyFocused || active)
? slot
: ''
return (
<div class={classes}>
{
this.sideTabBar
? <h1 class="mobile-label">{slot.data.attrs.label}</h1>
: ''
}
2020-05-29 03:26:33 +09:00
{renderSlot}
</div>
)
})
return (
2020-05-03 23:36:12 +09:00
<div class={'tab-switcher ' + (this.sideTabBar ? 'side-tabs' : 'top-tabs')}>
2019-01-18 05:05:58 +09:00
<div class="tabs">
{tabs}
</div>
<div ref="contents" class={'contents' + (this.scrollableTabs ? ' scrollable-tabs' : '')} v-body-scroll-lock={this.settingsModalVisible}>
2019-01-18 05:05:58 +09:00
{contents}
</div>
2018-11-22 04:08:27 +09:00
</div>
)
}
})