Skip to content

Commit 7deb8c1

Browse files
authored
Merge pull request #138 from Coding/zhengxinqi/refractor_sidebar
重构 SideBar 模块
2 parents c935163 + 65a4f7a commit 7deb8c1

File tree

11 files changed

+343
-158
lines changed

11 files changed

+343
-158
lines changed

.eslintrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"no-plusplus": 0,
1414
"no-return-assign": 0,
1515
"no-underscore-dangle": 0,
16+
"react/require-default-props": 0,
17+
"no-multi-assign": 0,
1618
"semi": ["error", "never"],
1719
"one-var": 0,
1820
"one-var-declaration-per-line": 0,

app/CodingSDK.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import { CreateI18n } from 'utils/createI18n'
12
import { request } from './utils'
23
import store from './store'
34
import config from './config'
45
import * as Modal from './components/Modal/actions'
56
import { notify, NOTIFY_TYPE } from './components/Notification/actions'
6-
import { addComToSideBar } from './components/Panel/actions'
7-
import { CreateI18n } from 'utils/createI18n'
7+
import { addComToSideBar } from './components/Panel/SideBar/actions'
88

99

1010
export default class {

app/commands/commandBindings/misc.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as Modal from 'components/Modal/actions'
22
import * as Panel from 'components/Panel/actions'
3+
import * as SideBar from 'components/Panel/SideBar/actions'
4+
35

46
const getComponentByName = name => window.refs[name].getWrappedInstance()
57
export default {
@@ -31,7 +33,7 @@ export default {
3133
// 'tools:terminal:clear_scrollback_buffer':
3234
// 'tools:terminal:reset':
3335
'tools:terminal:new_terminal': (c) => {
34-
Panel.activateSidePanelView('bottom_0')
36+
SideBar.activateSidePanelView('bottom_0')
3537
// $d(Tab.createTabInGroup('tab_group_terminal'))
3638
}
3739
}

app/components/Panel/PanelContent.jsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import StatusBar from '../StatusBar'
55
import PanesContainer from '../Pane'
66
import FileTree from '../FileTree'
77
import TerminalContainer from '../Terminal'
8-
import SideBar from './SideBar'
9-
import { SidePanelContainer, SidePanelView } from './SidePanel'
8+
import SideBar from './SideBar/SideBar'
9+
import { SidePanelContainer, SidePanelView } from './SideBar/SidePanel'
1010
import GitGraph from 'components/Git/GitGraph'
1111

1212

@@ -38,7 +38,7 @@ const PanelContent = ({ panel }) => {
3838
case 'PANEL_LEFT':
3939
return (
4040
<SidePanelContainer side='left'>
41-
<SidePanelView label={{ text: i18n`panel.left.project`, icon: 'octicon octicon-code' }} active >
41+
<SidePanelView key='project' label={{ text: i18n`panel.left.project`, icon: 'octicon octicon-code' }} active >
4242
<FileTree />
4343
</SidePanelView>
4444
</SidePanelContainer>
@@ -51,14 +51,13 @@ const PanelContent = ({ panel }) => {
5151
}
5252
return (
5353
<SidePanelContainer side='bottom'>
54-
<SidePanelView label={labels.terminal} >
54+
<SidePanelView key='terminal' label={labels.terminal} >
5555
<TerminalContainer />
5656
</SidePanelView>
5757

58-
<SidePanelView label={labels.gitGraph} >
58+
<SidePanelView key='gitGraph' label={labels.gitGraph} >
5959
<GitGraph />
6060
</SidePanelView>
61-
6261
</SidePanelContainer>
6362
)
6463
default:

app/components/Panel/SideBar.jsx

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import React, { PropTypes } from 'react'
2+
import cx from 'classnames'
3+
4+
import { inject, observer } from 'mobx-react'
5+
import { toggleSidePanelView } from './actions'
6+
import state, { labelShape, labelsShape } from './state'
7+
8+
/* shape of label
9+
label = {
10+
text: String,
11+
icon: String,
12+
viewId: String,
13+
}
14+
*/
15+
16+
17+
const SideBarLabel = ({ label, isActive, onClick }) => (
18+
<div className={
19+
cx('side-bar-label', {
20+
active: isActive
21+
})}
22+
onClick={onClick}
23+
>
24+
<div className='side-bar-label-container'>
25+
<div className='side-bar-label-content'>
26+
<i className={cx('icon', label.icon)} />
27+
<span>{label.text}</span>
28+
</div>
29+
</div>
30+
</div>
31+
)
32+
33+
SideBarLabel.propTypes = {
34+
label: labelShape,
35+
isActive: PropTypes.bool,
36+
onClick: PropTypes.func
37+
}
38+
39+
const _SideBar = observer(({ labels, side, activeViewId, activateView }) => {
40+
return (
41+
<div className={`bar side-bar ${side}`}>
42+
{
43+
labels
44+
.map(label =>
45+
<SideBarLabel
46+
key={label.viewId}
47+
label={label}
48+
onClick={() => activateView(label.viewId)}
49+
isActive={activeViewId === label.viewId}
50+
/>
51+
)}
52+
</div>)
53+
})
54+
55+
_SideBar.propTypes = {
56+
labels: labelsShape,
57+
side: PropTypes,
58+
activeViewId: PropTypes.string,
59+
activateView: PropTypes.func
60+
}
61+
62+
const SideBar = inject((__, { side }) => {
63+
const labels = state.labels.values().filter(label => label.side === side)
64+
65+
const activeViewId = state.activeStatus.get(side)
66+
return {
67+
side,
68+
labels,
69+
activeViewId,
70+
activateView: toggleSidePanelView // toggle action
71+
}
72+
})(_SideBar)
73+
74+
export default SideBar
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import React, { Component, PropTypes } from 'react'
2+
import { inject } from 'mobx-react'
3+
import _ from 'lodash'
4+
5+
import {
6+
registerSideBarView
7+
} from './actions'
8+
import state, { labelsShape } from './state'
9+
10+
11+
@inject((__, { side }) => {
12+
const labels = state.labels.values().filter(label => label.side === side)
13+
const activeViewId = state.activeStatus.get(side)
14+
return { activeViewId, labels, side }
15+
})
16+
class SidePanelContainer extends Component {
17+
componentWillMount () {
18+
const children = this.getChildren()
19+
const { side } = this.props
20+
const mapChildrenToSidebar = children.map((child, idx) => ({
21+
side,
22+
key: child.key || `${side}_${idx}`,
23+
isActive: child.props.active || false,
24+
label: child.props.label,
25+
view: child
26+
}))
27+
registerSideBarView(mapChildrenToSidebar)
28+
}
29+
getChildren () {
30+
if (!this.props.children) return []
31+
return Array.isArray(this.props.children) ? this.props.children : [this.props.children]
32+
}
33+
render () {
34+
const { labels = {}, activeViewId } = this.props
35+
return (<div style={{ height: '100%' }}>
36+
{labels
37+
.sort((a, b) => a.weight || 1 - b.weight || 1)
38+
.map(label =>
39+
<SidePanelViewContent key={label.viewId}
40+
view={state.views[label.viewId]}
41+
isActive={activeViewId === label.viewId}
42+
/>
43+
)}
44+
</div>)
45+
}
46+
}
47+
SidePanelContainer.propTypes = {
48+
labels: labelsShape,
49+
children: PropTypes.node,
50+
activeViewId: PropTypes.string,
51+
side: PropTypes.string
52+
}
53+
54+
const SidePanelViewContent = ({ isActive, view }) =>
55+
<div style={{ height: '100%', display: isActive ? 'block' : 'none' }}>
56+
{view}
57+
</div>
58+
59+
SidePanelViewContent.propTypes = {
60+
isActive: PropTypes.bool,
61+
view: PropTypes.node
62+
}
63+
64+
class SidePanelView extends Component {
65+
constructor (props) {
66+
super(props)
67+
}
68+
69+
render () {
70+
return this.props.children
71+
}
72+
}
73+
SidePanelView.propTypes = {
74+
children: PropTypes.node
75+
}
76+
77+
78+
export { SidePanelContainer, SidePanelView }

0 commit comments

Comments
 (0)