-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.jsx
More file actions
133 lines (117 loc) · 4.59 KB
/
index.jsx
File metadata and controls
133 lines (117 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import React from 'react'
import classNames from 'classnames'
import t from 'patchwork-translations'
function cls (selected, hasNew) {
return classNames({ 'channel-list-item': true, flex: true, selected: selected, unread: hasNew })
}
class ChannelListItem extends React.Component {
static contextTypes = {
ssb: React.PropTypes.object,
emit: React.PropTypes.func
}
shouldComponentUpdate(nextProps) {
return this.props.selected !== nextProps.selected || this.props.channel !== nextProps.channel
}
onPin(e) {
e.preventDefault()
e.stopPropagation()
this.context.ssb.patchwork.toggleChannelPinned(this.props.channel.name, err => {
if (err)
this.context.emit('error', err)
})
}
render() {
const channel = this.props.channel
const onSelect = () => this.props.onSelect(channel)
return <div className={cls(this.props.selected, channel.hasNew)} onClick={onSelect}>
<div className="flex-fill"><i className="fa fa-hashtag" /> { channel.name }</div>
<div className="ctrls">
<a href='javascript:;' className={classNames({ pin: true, pinned: channel.pinned })} onClick={this.onPin.bind(this)}><i className="fa fa-thumb-tack" /></a>
</div>
</div>
}
}
export default class ChannelList extends React.Component {
static propTypes = {
channels: React.PropTypes.array.isRequired,
onSelect: React.PropTypes.func.isRequired,
selected: React.PropTypes.string
}
static contextTypes = {
ssb: React.PropTypes.object,
emit: React.PropTypes.func
}
constructor(props) {
super(props)
this.state = { searchText: '', searchQuery: false }
}
onSearchChange(e) {
const v = e.target.value
this.setState({ searchText: v, searchQuery: (v) ? new RegExp(v, 'i') : false })
}
onSearchKeyDown(e) {
if (e.keyCode == 13) {
e.preventDefault()
e.stopPropagation()
if (this.state.searchText.trim())
this.props.onSelect({ name: this.state.searchText })
this.onClearSearch()
}
}
onClearSearch() {
this.setState({ searchText: '', searchQuery: false })
}
onClickOpen() {
this.props.onSelect({ name: this.state.searchText })
this.onClearSearch()
}
onClickCreate() {
// Pin, then open
this.context.ssb.patchwork.pinChannel(this.state.searchText, err => {
if (err)
return this.context.emit('error', err)
this.props.onSelect({ name: this.state.searchText })
this.onClearSearch()
})
}
render() {
const selected = this.props.selected
const search = this.state.searchText
// predicates
const isPartialMatch = channel => ((this.state.searchQuery) ? this.state.searchQuery.test(channel.name) : true)
const isExactMatch = channel => ((this.state.searchText) ? this.state.searchText === channel.name : false)
const isPinned = b => channel => (!!channel.pinned == b)
// filtered channels
const pinnedChannels = this.props.channels.filter(isPinned(true)).filter(isPartialMatch)
const unpinnedChannels = this.props.channels.filter(isPinned(false)).filter(isPartialMatch)
// render
const hasExactMatch = this.props.channels.filter(isExactMatch).length > 0
const renderChannel = channel => <ChannelListItem key={channel.name} channel={channel} selected={channel.name === selected} onSelect={this.props.onSelect} />
return <div className="channel-list">
<div className="channel-list-ctrls">
<div className="search">
<i className="fa fa-hashtag" />
<input ref="searchInput" type="text" placeholder={t('channelList.NewChannel')} value={search} onChange={this.onSearchChange.bind(this)} onKeyDown={this.onSearchKeyDown.bind(this)} />
</div>
</div>
{ pinnedChannels.length ? <div className="channel-list-heading">{t('channelList.Channels')}</div> : '' }
{ pinnedChannels.map(renderChannel) }
<hr/>
{ unpinnedChannels.map(renderChannel) }
<hr/>
<div style={{fontWeight: 'normal', color: 'gray', padding: '0 10px'}}>
<p><small>{t('channelList.ChannelsInfo')}</small></p>
<p>
{ search
? (hasExactMatch
? <small><a href='javascript:;' onClick={this.onClickOpen.bind(this)}>{t('channelList.Open', {channel: search})}</a> | </small>
: <small><a href='javascript:;' onClick={this.onClickCreate.bind(this)}>{t('channelList.Create', {channel: search})}</a> | </small>)
: '' }
{ search
? <small><a href='javascript:;' onClick={this.onClearSearch.bind(this)}>{t('channelList.ClearFilter')}</a></small>
: '' }
</p>
</div>
</div>
}
}