2016-06-27 21:08:25 +02:00
|
|
|
import React, { PropTypes } from 'react';
|
2016-08-17 22:09:20 +02:00
|
|
|
import { Link } from 'react-router';
|
2016-07-22 18:42:38 +02:00
|
|
|
const InlineSVG = require('react-inlinesvg');
|
2016-06-24 00:29:55 +02:00
|
|
|
const playUrl = require('../../../images/play.svg');
|
|
|
|
const logoUrl = require('../../../images/p5js-logo.svg');
|
|
|
|
const stopUrl = require('../../../images/stop.svg');
|
|
|
|
const preferencesUrl = require('../../../images/preferences.svg');
|
2016-10-26 23:40:10 +02:00
|
|
|
const editProjectNameUrl = require('../../../images/pencil.svg');
|
2016-07-22 18:42:38 +02:00
|
|
|
import classNames from 'classnames';
|
2016-06-24 00:29:55 +02:00
|
|
|
|
2016-08-15 18:42:13 +02:00
|
|
|
class Toolbar extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.handleKeyPress = this.handleKeyPress.bind(this);
|
|
|
|
this.handleProjectNameChange = this.handleProjectNameChange.bind(this);
|
|
|
|
}
|
2016-06-24 00:29:55 +02:00
|
|
|
|
2016-08-15 18:42:13 +02:00
|
|
|
handleKeyPress(event) {
|
|
|
|
if (event.key === 'Enter') {
|
|
|
|
this.props.hideEditProjectName();
|
|
|
|
}
|
|
|
|
}
|
2016-07-31 04:46:48 +02:00
|
|
|
|
2016-08-15 18:42:13 +02:00
|
|
|
handleProjectNameChange(event) {
|
|
|
|
this.props.setProjectName(event.target.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
validateProjectName() {
|
|
|
|
if (this.props.project.name === '') {
|
|
|
|
this.props.setProjectName(this.originalProjectName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 23:54:14 +01:00
|
|
|
canEditProjectName() {
|
|
|
|
return (this.props.owner && this.props.owner.username
|
|
|
|
&& this.props.owner.username === this.props.currentUser)
|
|
|
|
|| !this.props.owner || !this.props.owner.username;
|
|
|
|
}
|
|
|
|
|
2016-08-15 18:42:13 +02:00
|
|
|
render() {
|
|
|
|
let playButtonClass = classNames({
|
|
|
|
'toolbar__play-button': true,
|
|
|
|
'toolbar__play-button--selected': this.props.isPlaying
|
|
|
|
});
|
|
|
|
let stopButtonClass = classNames({
|
|
|
|
'toolbar__stop-button': true,
|
|
|
|
'toolbar__stop-button--selected': !this.props.isPlaying
|
|
|
|
});
|
|
|
|
let preferencesButtonClass = classNames({
|
|
|
|
'toolbar__preferences-button': true,
|
|
|
|
'toolbar__preferences-button--selected': this.props.preferencesIsVisible
|
|
|
|
});
|
|
|
|
let nameContainerClass = classNames({
|
|
|
|
'toolbar__project-name-container': true,
|
|
|
|
'toolbar__project-name-container--editing': this.props.project.isEditingName
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="toolbar">
|
2016-10-18 04:57:29 +02:00
|
|
|
<InlineSVG className="toolbar__logo" src={logoUrl} alt="p5js Logo" />
|
2016-08-16 03:09:47 +02:00
|
|
|
<button
|
|
|
|
className="toolbar__play-sketch-button"
|
2016-09-29 06:54:35 +02:00
|
|
|
onClick={() => {
|
2017-01-31 22:45:47 +01:00
|
|
|
this.props.clearConsole();
|
2016-09-29 06:54:35 +02:00
|
|
|
this.props.startTextOutput();
|
|
|
|
this.props.startSketchAndRefresh();
|
|
|
|
}}
|
2016-08-25 23:59:02 +02:00
|
|
|
aria-label="play sketch"
|
2016-09-24 06:46:06 +02:00
|
|
|
disabled={this.props.infiniteLoop}
|
2016-08-16 03:09:47 +02:00
|
|
|
>
|
2016-08-25 23:59:02 +02:00
|
|
|
<InlineSVG src={playUrl} alt="Play Sketch" />
|
|
|
|
</button>
|
2017-01-31 22:45:47 +01:00
|
|
|
<button
|
|
|
|
className={playButtonClass}
|
|
|
|
onClick={() => {
|
|
|
|
this.props.clearConsole();
|
|
|
|
this.props.startSketchAndRefresh();
|
|
|
|
}}
|
|
|
|
aria-label="play only visual sketch"
|
|
|
|
disabled={this.props.infiniteLoop}
|
|
|
|
>
|
2016-08-25 23:59:02 +02:00
|
|
|
<InlineSVG src={playUrl} alt="Play only visual Sketch" />
|
2016-08-16 03:09:47 +02:00
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
className={stopButtonClass}
|
|
|
|
onClick={() => { this.props.stopTextOutput(); this.props.stopSketch(); }}
|
|
|
|
aria-label="stop sketch"
|
|
|
|
>
|
2016-08-15 18:42:13 +02:00
|
|
|
<InlineSVG src={stopUrl} alt="Stop Sketch" />
|
|
|
|
</button>
|
2016-10-06 21:45:26 +02:00
|
|
|
<div className="toolbar__autorefresh">
|
2016-09-28 19:15:50 +02:00
|
|
|
<input
|
|
|
|
id="autorefresh"
|
|
|
|
type="checkbox"
|
2016-09-28 20:12:01 +02:00
|
|
|
checked={this.props.autorefresh}
|
|
|
|
onChange={(event) => {
|
|
|
|
this.props.setAutorefresh(event.target.checked);
|
|
|
|
}}
|
2016-09-28 19:15:50 +02:00
|
|
|
/>
|
|
|
|
<label htmlFor="autorefresh" className="toolbar__autorefresh-label">
|
|
|
|
Auto-refresh
|
|
|
|
</label>
|
2016-10-06 21:45:26 +02:00
|
|
|
</div>
|
2016-08-15 18:42:13 +02:00
|
|
|
<div className={nameContainerClass}>
|
|
|
|
<a
|
|
|
|
className="toolbar__project-name"
|
2016-12-01 23:12:34 +01:00
|
|
|
href={this.props.owner ? `/${this.props.owner.username}/sketches/${this.props.project.id}` : ''}
|
2016-11-04 23:54:14 +01:00
|
|
|
onClick={(e) => {
|
|
|
|
if (this.canEditProjectName()) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.originalProjectName = this.props.project.name;
|
|
|
|
this.props.showEditProjectName();
|
|
|
|
setTimeout(() => this.refs.projectNameInput.focus(), 0);
|
|
|
|
}
|
2016-08-15 18:42:13 +02:00
|
|
|
}}
|
2016-11-04 23:54:14 +01:00
|
|
|
>
|
|
|
|
{this.props.project.name}
|
|
|
|
{this.canEditProjectName() && <InlineSVG className="toolbar__edit-name-button" src={editProjectNameUrl} alt="Edit Project Name" />}
|
|
|
|
</a>
|
2016-08-15 18:42:13 +02:00
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
className="toolbar__project-name-input"
|
|
|
|
value={this.props.project.name}
|
|
|
|
onChange={this.handleProjectNameChange}
|
|
|
|
ref="projectNameInput"
|
|
|
|
onBlur={() => {
|
|
|
|
this.validateProjectName();
|
|
|
|
this.props.hideEditProjectName();
|
2016-10-19 17:47:58 +02:00
|
|
|
if (this.props.project.id) {
|
|
|
|
this.props.saveProject();
|
|
|
|
}
|
2016-08-15 18:42:13 +02:00
|
|
|
}}
|
|
|
|
onKeyPress={this.handleKeyPress}
|
|
|
|
/>
|
|
|
|
{(() => { // eslint-disable-line
|
|
|
|
if (this.props.owner) {
|
|
|
|
return (
|
2016-08-17 22:09:20 +02:00
|
|
|
<p className="toolbar__project-owner">
|
|
|
|
by <Link to={`/${this.props.owner.username}/sketches`}>{this.props.owner.username}</Link>
|
|
|
|
</p>
|
2016-08-15 18:42:13 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
})()}
|
|
|
|
</div>
|
|
|
|
<button
|
|
|
|
className={preferencesButtonClass}
|
|
|
|
onClick={this.props.openPreferences}
|
2016-08-25 23:59:02 +02:00
|
|
|
aria-label="preferences"
|
2016-06-27 21:08:25 +02:00
|
|
|
>
|
2016-08-25 23:59:02 +02:00
|
|
|
<InlineSVG src={preferencesUrl} alt="Preferences" />
|
2016-08-15 18:42:13 +02:00
|
|
|
</button>
|
2016-06-24 00:29:55 +02:00
|
|
|
</div>
|
2016-08-15 18:42:13 +02:00
|
|
|
);
|
|
|
|
}
|
2016-06-24 00:29:55 +02:00
|
|
|
}
|
|
|
|
|
2016-06-27 21:08:25 +02:00
|
|
|
Toolbar.propTypes = {
|
|
|
|
isPlaying: PropTypes.bool.isRequired,
|
2016-08-01 19:55:49 +02:00
|
|
|
preferencesIsVisible: PropTypes.bool.isRequired,
|
2016-06-27 21:08:25 +02:00
|
|
|
startSketch: PropTypes.func.isRequired,
|
|
|
|
stopSketch: PropTypes.func.isRequired,
|
2016-08-12 22:37:38 +02:00
|
|
|
startTextOutput: PropTypes.func.isRequired,
|
|
|
|
stopTextOutput: PropTypes.func.isRequired,
|
2016-06-27 21:08:25 +02:00
|
|
|
setProjectName: PropTypes.func.isRequired,
|
2016-07-15 17:54:47 +02:00
|
|
|
openPreferences: PropTypes.func.isRequired,
|
2016-07-15 19:11:50 +02:00
|
|
|
owner: PropTypes.shape({
|
|
|
|
username: PropTypes.string
|
2016-08-15 18:42:13 +02:00
|
|
|
}),
|
|
|
|
project: PropTypes.shape({
|
|
|
|
name: PropTypes.string.isRequired,
|
2016-10-19 17:47:58 +02:00
|
|
|
isEditingName: PropTypes.bool,
|
|
|
|
id: PropTypes.string
|
2016-08-15 18:42:13 +02:00
|
|
|
}).isRequired,
|
|
|
|
showEditProjectName: PropTypes.func.isRequired,
|
2016-09-24 06:46:06 +02:00
|
|
|
hideEditProjectName: PropTypes.func.isRequired,
|
2016-09-28 20:12:01 +02:00
|
|
|
infiniteLoop: PropTypes.bool.isRequired,
|
|
|
|
autorefresh: PropTypes.bool.isRequired,
|
2016-09-29 06:54:35 +02:00
|
|
|
setAutorefresh: PropTypes.func.isRequired,
|
2016-10-19 17:47:58 +02:00
|
|
|
startSketchAndRefresh: PropTypes.func.isRequired,
|
2016-11-04 23:54:14 +01:00
|
|
|
saveProject: PropTypes.func.isRequired,
|
2017-01-31 22:45:47 +01:00
|
|
|
currentUser: PropTypes.string,
|
|
|
|
clearConsole: PropTypes.func.isRequired
|
2016-06-27 21:08:25 +02:00
|
|
|
};
|
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
export default Toolbar;
|