more portable metacode selector component

This commit is contained in:
Devin Howard 2017-02-18 16:39:56 -08:00
parent d15ee27e2d
commit 7819c7fe3a
2 changed files with 57 additions and 116 deletions

View file

@ -5,6 +5,9 @@ import React, { PropTypes, Component } from 'react'
import Metacode from './Metacode' import Metacode from './Metacode'
import Permission from './Permission' import Permission from './Permission'
// TODO use a callback instead of an import
import Visualize from '../../Metamaps/Visualize'
const inmaps = (topic) => { const inmaps = (topic) => {
const inmapsArray = topic.get('inmaps') || [] const inmapsArray = topic.get('inmaps') || []
const inmapsLinks = topic.get('inmapsLinks') || [] const inmapsLinks = topic.get('inmapsLinks') || []
@ -60,18 +63,22 @@ class Links extends Component {
bindShowCardListeners(this.props.topic, this.props.ActiveMapper) bindShowCardListeners(this.props.topic, this.props.ActiveMapper)
} }
handleMetacodeClick = metacodeId => {
this.props.updateTopic({
metacode_id: metacodeId
})
Visualize.mGraph.plot()
}
render = () => { render = () => {
const { topic, ActiveMapper } = this.props const { topic, ActiveMapper } = this.props
const topicId = topic.isNew() ? topic.cid : topic.id // TODO should we really be using cid here?!?
const metacode = topic.getMetacode() const metacode = topic.getMetacode()
return ( return (
<div className="links"> <div className="links">
<Metacode <Metacode
topic={topic}
metacode={metacode} metacode={metacode}
ActiveMapper={ActiveMapper} onMetacodeClick={this.handleMetacodeClick}
updateTopic={this.props.updateTopic}
metacodeSets={this.props.metacodeSets} metacodeSets={this.props.metacodeSets}
/> />
<div className="linkItem contributor"> <div className="linkItem contributor">
@ -84,7 +91,7 @@ class Links extends Component {
<div className="hoverTip">Click to see which maps topic appears on</div> <div className="hoverTip">Click to see which maps topic appears on</div>
<div className="tip"><ul>{inmaps(topic)}</ul></div> <div className="tip"><ul>{inmaps(topic)}</ul></div>
</div> </div>
<a href={`/topics/${topicId}`} target="_blank" className="linkItem synapseCount"> <a href={`/topics/${topic.id}`} target="_blank" className="linkItem synapseCount">
<div className="synapseCountIcon"></div> <div className="synapseCountIcon"></div>
{topic.get('synapse_count').toString()} {topic.get('synapse_count').toString()}
<div className="tip">Click to see this topics synapses</div> <div className="tip">Click to see this topics synapses</div>

View file

@ -1,137 +1,73 @@
/* global $ */ /* global $ */
/*
* Metacode selector component
*
* This component takes in a callback (onMetacodeClick; takes one metacode id)
* and a list of metacode sets and renders them. If you click a metacode, it
* passes that metacode's id to the callback.
*/
import React, { PropTypes, Component } from 'react' import React, { PropTypes, Component } from 'react'
import DataModel from '../../Metamaps/DataModel'
import Visualize from '../../Metamaps/Visualize'
// TODO all of these should be largely turned into passed-in callbacks
const bindShowCardListeners = (topic, ActiveMapper) => {
var authorized = topic.authorizeToEdit(ActiveMapper)
var selectingMetacode = false
// attach the listener that shows the metacode title when you hover over the image
$('.showcard .metacodeImage').mouseenter(function() {
$('.showcard .icon').css('z-index', '4')
$('.showcard .metacodeTitle').show()
})
$('.showcard .linkItem.icon').mouseleave(function() {
if (!selectingMetacode) {
$('.showcard .metacodeTitle').hide()
$('.showcard .icon').css('z-index', '1')
}
})
var metacodeLiClick = function() {
selectingMetacode = false
var metacodeId = parseInt($(this).attr('data-id'))
var metacode = DataModel.Metacodes.get(metacodeId)
$('.CardOnGraph').find('.metacodeTitle').html(metacode.get('name'))
.append('<div class="expandMetacodeSelect"></div>')
.attr('class', 'metacodeTitle mbg' + metacode.id)
$('.CardOnGraph').find('.metacodeImage').css('background-image', 'url(' + metacode.get('icon') + ')')
topic.save({
metacode_id: metacode.id
})
Visualize.mGraph.plot()
$('.metacodeSelect').hide().removeClass('onRightEdge onBottomEdge')
$('.metacodeTitle').hide()
$('.showcard .icon').css('z-index', '1')
}
var openMetacodeSelect = function(event) {
var TOPICCARD_WIDTH = 300
var METACODESELECT_WIDTH = 404
var MAX_METACODELIST_HEIGHT = 270
if (!selectingMetacode) {
selectingMetacode = true
// this is to make sure the metacode
// select is accessible onscreen, when opened
// while topic card is close to the right
// edge of the screen
var windowWidth = $(window).width()
var showcardLeft = parseInt($('.showcard').css('left'))
var distanceFromEdge = windowWidth - (showcardLeft + TOPICCARD_WIDTH)
if (distanceFromEdge < METACODESELECT_WIDTH) {
$('.metacodeSelect').addClass('onRightEdge')
}
// this is to make sure the metacode
// select is accessible onscreen, when opened
// while topic card is close to the bottom
// edge of the screen
var windowHeight = $(window).height()
var showcardTop = parseInt($('.showcard').css('top'))
var topicTitleHeight = $('.showcard .title').height() + parseInt($('.showcard .title').css('padding-top')) + parseInt($('.showcard .title').css('padding-bottom'))
var distanceFromBottom = windowHeight - (showcardTop + topicTitleHeight)
if (distanceFromBottom < MAX_METACODELIST_HEIGHT) {
$('.metacodeSelect').addClass('onBottomEdge')
}
$('.metacodeSelect').show()
event.stopPropagation()
}
}
var hideMetacodeSelect = function() {
selectingMetacode = false
$('.metacodeSelect').hide().removeClass('onRightEdge onBottomEdge')
$('.metacodeTitle').hide()
$('.showcard .icon').css('z-index', '1')
}
if (authorized) {
$('.showcard .metacodeTitle').click(openMetacodeSelect)
$('.showcard').click(hideMetacodeSelect)
$('.metacodeSelect > ul > li').click(function(event) {
event.stopPropagation()
})
$('.metacodeSelect li li').click(metacodeLiClick)
}
}
class Metacode extends Component { class Metacode extends Component {
componentDidMount = () => { constructor(props) {
bindShowCardListeners(this.props.topic, this.props.ActiveMapper) super(props)
this.state = {
showMetacodeTitle: false,
showMetacodeSelect: false
}
} }
metacodeOptions = () => { metacodeOptions = () => {
return ( return (
<div id="metacodeOptions"> <div id="metacodeOptions">
<ul> <ul>
{this.props.metacodeSets.map(set => { {this.props.metacodeSets.map(set => (
return (<li key={set.name}> <li key={set.name}>
<span>{set.name}</span> <span>{set.name}</span>
<div className="expandMetacodeSet"></div> <div className="expandMetacodeSet"></div>
<ul> <ul>
{set.metacodes.map(m => { {set.metacodes.map(m => (
return (<li key={m.id} data-id={m.id}> <li key={m.id}
onClick={() => this.props.onMetacodeClick(m.id)}
>
<img width="24" height="24" src={m.icon_path} alt={m.name} /> <img width="24" height="24" src={m.icon_path} alt={m.name} />
<div className="mSelectName">{m.name}</div> <div className="mSelectName">{m.name}</div>
<div className="clearfloat"></div> <div className="clearfloat"></div>
</li>) </li>
})} ))}
</ul> </ul>
</li>) </li>
})} ))}
</ul> </ul>
</div> </div>
) )
} }
render = () => { render = () => {
const { metacode } = this.props
return ( return (
<div className="linkItem icon"> <div className="linkItem icon"
<div className={`metacodeTitle mbg${metacode.get('id')}`}> style={{ zIndex: this.state.showMetacodeTitle ? 4 : 1 }}
{metacode.get('name')} onMouseLeave={() => this.setState({ showMetacodeTitle: false, showMetacodeSelect: false })}
<div className="expandMetacodeSelect"></div> >
<div className={`metacodeTitle mbg${this.props.metacode.get('id')}`}
style={{ display: this.state.showMetacodeTitle ? 'block' : 'none' }}
>
{this.props.metacode.get('name')}
<div className="expandMetacodeSelect"
onClick={() => this.setState({ showMetacodeSelect: !this.state.showMetacodeSelect })}
/>
</div> </div>
<div className="metacodeImage" style={{backgroundImage: `url(${metacode.get('icon')})`}} title="click and drag to move card"></div> <div className="metacodeImage"
<div className="metacodeSelect"> style={{backgroundImage: `url(${this.props.metacode.get('icon')})`}}
title="click and drag to move card"
onMouseEnter={() => this.setState({ showMetacodeTitle: true })}
/>
<div className="metacodeSelect"
style={{ display: this.state.showMetacodeSelect ? 'block' : 'none' }}
>
{this.metacodeOptions()} {this.metacodeOptions()}
</div> </div>
</div> </div>
@ -140,10 +76,8 @@ class Metacode extends Component {
} }
Metacode.propTypes = { Metacode.propTypes = {
topic: PropTypes.object, // backbone object
metacode: PropTypes.object, // backbone object metacode: PropTypes.object, // backbone object
ActiveMapper: PropTypes.object, onMetacodeClick: PropTypes.func,
updateTopic: PropTypes.func,
metacodeSets: PropTypes.arrayOf(PropTypes.shape({ metacodeSets: PropTypes.arrayOf(PropTypes.shape({
name: PropTypes.string, name: PropTypes.string,
metacodes: PropTypes.arrayOf(PropTypes.shape({ metacodes: PropTypes.arrayOf(PropTypes.shape({