JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
HTML Settings
Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.
<div id="app"></div>
.container {
padding: 30px;
}
.row {
text-align: center;
padding: 10px;
}
.pomodoroclock {
border-radius: 50%;
height: 200px;
width: 200px;
font-size: 48px;
}
.focusing {
color: rgb(217, 83, 79)
}
.break {
color: rgb(92, 184, 92)
}
.controls {
height: 30px;
width: 40px;
margin-left: 6px;
margin-right: 6px;
}
.start-stop-control {
height: 40px;
}
.counter {
margin: 2px;
display: inline-block;
vertical-align: top;
border-radius: 50%;
border: 8px solid;
height: 14px;
width: 14px;
}
.counter-line {
height: 14px;
}
.btn:focus,.btn:active {
outline: none !important;
}
//import React from 'react';
//import ReactDOM from 'react-dom';
const INTERVAL = {
FOCUSING: 0,
BREAK: 1
}
const STATUS = {
RUNNING: 0,
STOP: 1
}
const INITIAL_STATE = {
progress: 0,
defaultFocusing: 25,
defaultBreak: 5,
minutes: 25,
status: STATUS.STOP,
interval: INTERVAL.FOCUSING,
intervalId: null
};
const PomodoroProgress = (props) => {
let { progress } = props ;
var progressElements = [];
while(progress--) {
progressElements.push(<div className="counter focusing"></div>);
}
return (
<div className="counter-line">{progressElements}</div>
);
}
const PomodoroClock = (props) => {
const {
minutes,
interval,
onStartFocusing,
onStartBreak,
status
} = props;
const className = ((interval === INTERVAL.FOCUSING) ? "btn-danger" : "btn-success") + " btn pomodoroclock";
let onClick = onStartFocusing;
if (status === STATUS.RUNNING) {
onClick = null;
} else if (interval === INTERVAL.BREAK) {
onClick = onStartBreak;
}
return (
<div>
<button onClick={onClick} type="button" className={className}>{minutes}</button>
</div>
);
}
const PomodoroSettings = (props) => {
const { status, onDefaultsUp, onDefaultsDown } = props;
const className = ((status !== STATUS.STOP) ? "invisible" : "") + " btn btn-warning controls";
return (
<div>
<button
onClick={onDefaultsDown}
type="button" className={className}><span className="glyphicon glyphicon-chevron-down"></span></button>
<button
onClick={onDefaultsUp}
type="button" className={className}><span className="glyphicon glyphicon-chevron-up"></span></button>
</div>
)
}
const PomodoroControls = (props) => {
const {
status,
interval,
onResetProgress,
onStartFocusing,
onStartBreak,
onStopInterval
} = props;
// Start Focusing by default
let intervalClass = " btn-danger";
let label = "Start Focusing";
let onClick = onStartFocusing;
if (status === STATUS.RUNNING) {
intervalClass = " btn-warning";
label = "Stop";
onClick = onStopInterval;
} else if (interval === INTERVAL.BREAK) {
intervalClass = " btn-success";
label = "Start Break";
onClick = onStartBreak;
}
const className = "btn start-stop-control" + intervalClass;
const classNameCaret = "btn start-stop-control dropdown-toggle" + intervalClass;
return (
<div>
<div className="btn-group dropup">
<button type="button" onClick={onClick} className={className}>{label}</button>
<button type="button" className={classNameCaret} data-toggle="dropdown"> <span className="caret"></span>
</button>
<ul className="dropdown-menu" role="menu">
<li><a href="#" onClick={onStartFocusing}>Start Focusing</a></li>
<li><a href="#" onClick={onStartBreak}>Start Break</a></li>
<li role="separator" className="divider"></li>
<li><a href="#" onClick={onResetProgress}>Reset</a></li>
<li><a href="#" onClick={onStopInterval}>Stop</a></li>
</ul>
</div>
</div>
);
}
class PomodoroApp extends React.Component {
constructor(props) {
super(props);
this.state = INITIAL_STATE;
}
defaultsChange(delta) {
const {
minutes,
status,
interval,
defaultBreak,
defaultFocusing
} = this.state;
// delta can be +1 or -1 so that it increases or decreases
// the values (set by caller defaultsUp and defaultsDown)
if (status === STATUS.STOP) {
if (interval === INTERVAL.FOCUSING) {
if (defaultFocusing+delta > 0) {
this.setState({
defaultFocusing: defaultFocusing+delta,
minutes: defaultFocusing+delta
});
}
} if (interval === INTERVAL.BREAK) {
if (defaultBreak+delta > 0) {
this.setState({
defaultBreak: defaultBreak+delta,
minutes: defaultBreak+delta
});
}
}
}
}
defaultsUp() {
this.defaultsChange(+1);
}
defaultsDown() {
this.defaultsChange(-1);
}
completeInterval() {
const {
progress,
defaultFocusing,
defaultBreak,
minutes,
status,
interval,
intervalId
} = this.state;
clearInterval(intervalId); // stop interval
// switch to the next interval (focusing / break)
const nextInterval = (interval === INTERVAL.FOCUSING) ? INTERVAL.BREAK : INTERVAL.FOCUSING;
const nextProgress = (interval === INTERVAL.FOCUSING) ? progress + 1 : progress;
this.setState({
progress: nextProgress,
minutes: (nextInterval === INTERVAL.FOCUSING)? defaultFocusing: defaultBreak,
status: STATUS.STOP,
interval: nextInterval,
intervalId: null
});
if (interval === INTERVAL.FOCUSING) {
this.refs.audioEndFocusing.play();
} else {
this.refs.audioEndBreak.play();
}
}
intervalRun() {
const { minutes } = this.state;
if (minutes-1 === 0) {
this.completeInterval();
} else {
this.setState({minutes: minutes-1});
}
}
startFocusing() {
const { defaultFocusing } = this.state;
this.startInterval(defaultFocusing, INTERVAL.FOCUSING);
}
startBreak() {
const { defaultBreak } = this.state;
this.startInterval(defaultBreak, INTERVAL.BREAK);
}
startInterval(totalMinutes, intervalToStart) {
const { intervalId } = this.state;
// clear eventually bug-like previous timer ID
if (intervalId) {
clearInterval(intervalId);
}
this.setState({
minutes: totalMinutes,
interval: intervalToStart,
status: STATUS.RUNNING
});
const newIntervalId = setInterval(() => this.intervalRun(), 60000);
this.setState({
intervalId: newIntervalId
});
}
resetProgress() {
//TODO: ask confirmation to reset!
clearInterval(this.state.intervalId);
this.setState(INITIAL_STATE);
}
stopInterval() {
const {
defaultFocusing,
defaultBreak,
interval,
intervalId
} = this.state;
clearInterval(intervalId); // stop interval
this.setState({
minutes: (interval === INTERVAL.FOCUSING)? defaultFocusing: defaultBreak,
status: STATUS.STOP,
intervalId: null
});
}
render() {
const { progress, minutes, status, interval } = this.state;
return (
<div>
<div className="container">
<div className="row">
<div className="col-sm-4">
</div>
<div className="col-sm-4">
<div className="row">
<PomodoroProgress progress={progress} />
</div>
<div className="row">
<PomodoroClock
status={status}
minutes={minutes}
interval={interval}
onStartBreak={() => this.startBreak()}
onStartFocusing={() => this.startFocusing()}
/>
</div>
<div className="row">
<PomodoroSettings
status={status}
onDefaultsUp={() => this.defaultsUp()}
onDefaultsDown={() => this.defaultsDown()}
/>
</div>
<div className="row">
<PomodoroControls
status={status}
interval={interval}
onStartFocusing={() => this.startFocusing()}
onResetProgress={() => this.resetProgress()}
onStartBreak={() => this.startBreak()}
onStopInterval={() => this.stopInterval()}
/>
</div>
<div className="row">
<h5>React Pomodoro Clock</h5>
<audio ref="audioEndFocusing" preload="auto" src="http://archive.soundarchive.online/2-1-10044.mp3?_=1" type="audio/mpeg"></audio>
<audio ref="audioEndBreak" preload="auto" src="http://archive.soundarchive.online/3-5-10001.mp3?_=9" type="audio/mpeg"></audio>
</div>
</div>
<div className="col-sm-4">
</div>
</div>
</div>
</div>
)
}
}
ReactDOM.render(
<PomodoroApp/>,
document.getElementById('app')
);
Also see: Tab Triggers