metacode selector working nicely

This commit is contained in:
Connor Turland 2017-01-12 00:04:49 -05:00
parent 616a489ae4
commit 9c13f5a281
5 changed files with 29 additions and 102 deletions

View file

@ -5,48 +5,20 @@
.metacodeSelect { .metacodeSelect {
border-top: 1px solid #DDD; border-top: 1px solid #DDD;
padding: 0; padding: 0;
background: #FFF;
.tabList { .metacodeFilterInput {
float: left; width: 100px;
background: #FFF; outline: none;
border: 0;
div { padding: 8px;
border-right: 1px solid #DDD; font-size: 14px;
border-bottom: 1px solid #DDD; line-height: 14px;
} color: #424242;
font-family: 'din-medium', helvetica, sans-serif;
div:last-child {
border-bottom: none;
}
div.active {
border-right: none;
background: #EEE;
.metacodeFilterInput {
background: #EEE;
}
}
.metacodeFilterInput {
width: 100px;
outline: none;
border: 0;
padding: 8px;
font-size: 14px;
line-height: 14px;
color: #424242;
font-family: 'din-medium', helvetica, sans-serif;
}
span {
padding: 8px;
display: block;
}
} }
.metacodeList { .metacodeList {
float:left;
list-style: none; list-style: none;
background: #FFF; background: #FFF;
min-height: 107px; min-height: 107px;
@ -57,7 +29,7 @@
cursor: pointer; cursor: pointer;
&:hover, &.keySelect { &:hover, &.keySelect {
background: #EEE; background: #4CAF50;
} }
} }

View file

@ -22,7 +22,7 @@
<span><%= user_metacode().name %></span> <span><%= user_metacode().name %></span>
<div class="downArrow"></div> <div class="downArrow"></div>
</div> </div>
<%= form.text_field :name, :maxlength => 140, :placeholder => "title..." %> <%= form.text_field :name, :maxlength => 140, :placeholder => "what are you thinking..." %>
<div class="clearfloat"></div> <div class="clearfloat"></div>
<div id="metacodeSelector"></div> <div id="metacodeSelector"></div>
<div class="clearfloat"></div> <div class="clearfloat"></div>

View file

@ -253,9 +253,7 @@ const Create = {
Create.newTopic.hideSelector() Create.newTopic.hideSelector()
$('#topic_name').focus() $('#topic_name').focus()
}, },
metacodes: DataModel.Metacodes.models, metacodes: DataModel.Metacodes.models
recent: Create.recentMetacodes,
mostUsed: Create.mostUsedMetacodes
}), }),
document.getElementById('metacodeSelector') document.getElementById('metacodeSelector')
) )

View file

@ -725,7 +725,7 @@ const JIT = {
const creatingMap = GlobalUI.lightbox const creatingMap = GlobalUI.lightbox
if (creatingMap === 'newmap' || creatingMap === 'forkmap') { if (creatingMap === 'newmap' || creatingMap === 'forkmap') {
GlobalUI.CreateMap.submit() GlobalUI.CreateMap.submit()
} else if (Create.newTopic.beingCreated) { } else if (Create.newTopic.beingCreated && !Create.newTopic.metacodeSelectorOpen) {
Topic.createTopicLocally() Topic.createTopicLocally()
} else if (Create.newSynapse.beingCreated) { } else if (Create.newSynapse.beingCreated) {
Synapse.createSynapseLocally() Synapse.createSynapseLocally()

View file

@ -25,7 +25,6 @@ class MetacodeSelect extends Component {
super(props) super(props)
this.state = { this.state = {
filterText: '', filterText: '',
activeTab: 0,
selectingSection: true, selectingSection: true,
underCursor: 0 underCursor: 0
} }
@ -46,112 +45,72 @@ class MetacodeSelect extends Component {
this.setState({ filterText: e.target.value, underCursor: 0 }) this.setState({ filterText: e.target.value, underCursor: 0 })
} }
changeDisplay (activeTab) {
this.setState({ activeTab, underCursor: 0 })
}
getSelectMetacodes () { getSelectMetacodes () {
const { metacodes, recent, mostUsed } = this.props const { metacodes, recent, mostUsed } = this.props
const { filterText, activeTab } = this.state const { filterText, activeTab } = this.state
let selectMetacodes = [] let selectMetacodes = metacodes
if (activeTab == 0) { // search if (filterText.length > 1) { // search
selectMetacodes = filterText.length > 1 ? metacodes.filter(m => { selectMetacodes = filterText.length > 1 ? metacodes.filter(m => {
return m.get('name').toLowerCase().search(filterText.toLowerCase()) > -1 return m.get('name').toLowerCase().search(filterText.toLowerCase()) > -1
}) : [] }) : []
} else if (activeTab == 1) { // recent
selectMetacodes = recent.map(id => {
return metacodes.find(m => m.id == id)
}).filter(m => m)
} else if (activeTab == 2) { // mostUsed
selectMetacodes = mostUsed.map(id => {
return metacodes.find(m => m.id == id)
}).filter(m => m)
} }
return selectMetacodes return selectMetacodes
} }
handleKeyUp (e) { handleKeyUp (e) {
const { close } = this.props const { close } = this.props
const { activeTab, underCursor, selectingSection } = this.state const { underCursor } = this.state
const selectMetacodes = this.getSelectMetacodes() const selectMetacodes = this.getSelectMetacodes()
let nextIndex let nextIndex
switch (e.which) { switch (e.which) {
case ENTER_KEY: case ENTER_KEY:
if (selectMetacodes.length && !selectingSection) this.resetAndClick(selectMetacodes[underCursor].id) if (selectMetacodes.length) this.resetAndClick(selectMetacodes[underCursor].id)
break break
case UP_ARROW: case UP_ARROW:
if (selectingSection && activeTab == 0) { if (underCursor == 0) {
close() close()
break break
} }
else if (selectingSection) {
nextIndex = activeTab - 1
this.changeDisplay(nextIndex)
break
}
nextIndex = underCursor == 0 ? selectMetacodes.length - 1 : underCursor - 1 nextIndex = underCursor == 0 ? selectMetacodes.length - 1 : underCursor - 1
this.setState({ underCursor: nextIndex }) this.setState({ underCursor: nextIndex })
break break
case DOWN_ARROW: case DOWN_ARROW:
if (selectingSection) {
nextIndex = activeTab == 2 ? 0 : activeTab + 1
this.changeDisplay(nextIndex)
break
}
nextIndex = underCursor == selectMetacodes.length - 1 ? 0 : underCursor + 1 nextIndex = underCursor == selectMetacodes.length - 1 ? 0 : underCursor + 1
this.setState({ underCursor: nextIndex }) this.setState({ underCursor: nextIndex })
break break
case RIGHT_ARROW:
if (selectingSection) this.setState({ selectingSection: false })
break
case LEFT_ARROW:
if (!selectingSection) this.setState({ selectingSection: true })
break
} }
} }
resetAndClick (id) { resetAndClick (id) {
const { onClick } = this.props const { onClick } = this.props
this.setState({ filterText: '', underCursor: 0 }) this.setState({ filterText: '', underCursor: 0 })
this.changeDisplay(0)
onClick(id) onClick(id)
} }
render () { render () {
const { onClick, close, recent, mostUsed } = this.props const { onClick, close } = this.props
const { filterText, activeTab, underCursor, selectingSection } = this.state const { filterText, underCursor } = this.state
const selectMetacodes = this.getSelectMetacodes() const selectMetacodes = this.getSelectMetacodes()
return <div className='metacodeSelect'> return <div className='metacodeSelect'>
<div className='tabList'> <div className='tabList'>
<div className={ activeTab == 0 ? 'active' : '' } <input type='text'
onClick={() => { this.changeDisplay(0) }}>
<input type='text'
className='metacodeFilterInput' className='metacodeFilterInput'
placeholder='Search...' placeholder='Search...'
ref='input' ref='input'
value={ filterText } value={ filterText }
onChange={ this.changeFilterText.bind(this) } /> onChange={ this.changeFilterText.bind(this) } />
</div> <ul className='metacodeList'>
<div className={ activeTab == 1 ? 'active' : '' } { selectMetacodes.map((m, index) => {
onClick={() => { this.changeDisplay(1) }}> return <Metacode underCursor={underCursor == index}
<span>Recent</span>
</div>
<div className={ activeTab == 2 ? 'active' : '' }
onClick={() => { this.changeDisplay(2) }}>
<span>Most Used</span>
</div>
</div>
<ul className='metacodeList'>
{ selectMetacodes.map((m, index) => {
return <Metacode underCursor={!selectingSection && underCursor == index}
key={m.id} key={m.id}
m={m} m={m}
onClick={this.resetAndClick.bind(this)} /> onClick={this.resetAndClick.bind(this)} />
})} })}
</ul> </ul>
<div className='clearfloat'></div> <div className='clearfloat'></div>
</div>
</div> </div>
} }
} }
@ -160,8 +119,6 @@ MetacodeSelect.propTypes = {
onClick: PropTypes.func.isRequired, onClick: PropTypes.func.isRequired,
close: PropTypes.func.isRequired, close: PropTypes.func.isRequired,
metacodes: PropTypes.array.isRequired, metacodes: PropTypes.array.isRequired,
recent: PropTypes.array.isRequired,
mostUsed: PropTypes.array.isRequired
} }
export default MetacodeSelect export default MetacodeSelect