Skip to content

Commit dbbc8b0

Browse files
authored
Merge pull request #37 from oslabs-beta/rydang/locking
locking and pause button implemented
2 parents 7174d17 + afcc97b commit dbbc8b0

File tree

12 files changed

+146
-86
lines changed

12 files changed

+146
-86
lines changed

package-lock.json

Lines changed: 53 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"webpack-cli": "^3.3.6"
4646
},
4747
"dependencies": {
48+
"auto-bind": "^2.1.0",
4849
"prop-types": "^15.7.2",
4950
"rc-slider": "^8.6.13",
5051
"rc-tooltip": "^3.7.3",

package/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const snapShot = { tree: null };
22

3-
const mode = { jumping: false };
3+
const mode = { jumping: false, paused: false, locked: false };
44

55
const linkFiber = require('./linkFiber')(snapShot, mode);
66
const timeJump = require('./timeJump')(snapShot, mode);
@@ -12,11 +12,12 @@ window.addEventListener('message', ({ data: { action, payload } }) => {
1212
case 'jumpToSnap':
1313
timeJump(payload);
1414
break;
15-
case 'stepToSnap': {
16-
const { steps, speed } = payload;
17-
console.log('i would like to step through here');
15+
case 'setLock':
16+
mode.locked = payload;
17+
break;
18+
case 'setPause':
19+
mode.paused = payload;
1820
break;
19-
}
2021
default:
2122
}
2223
});

package/linkFiber.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ module.exports = (snap, mode) => {
66
let fiberRoot = null;
77

88
function sendSnapshot() {
9-
// don't send messages while jumping
10-
if (mode.jumping) return;
9+
// don't send messages while jumping or while paused
10+
if (mode.jumping || mode.paused) return;
1111
const payload = snap.tree.getCopy();
1212
window.postMessage({
1313
action: 'recordSnap',
@@ -23,6 +23,8 @@ module.exports = (snap, mode) => {
2323
const oldSetState = component.setState.bind(component);
2424

2525
function newSetState(state, callback = () => { }) {
26+
// dont do anything if state is locked
27+
if (mode.locked) return;
2628
// continue normal setState functionality, except add sending message middleware
2729
oldSetState(state, () => {
2830
updateSnapShotTree();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React, { Component } from 'react';
2+
3+
const autoBind = require('auto-bind');
4+
5+
class ButtonsContainer extends Component {
6+
constructor(props) {
7+
super(props);
8+
9+
this.state = {
10+
paused: false,
11+
locked: false,
12+
};
13+
14+
autoBind(this);
15+
}
16+
17+
togglePause(port) {
18+
const { paused } = this.state;
19+
port.postMessage({ action: 'setPause', payload: !paused });
20+
this.setState({ paused: !paused });
21+
}
22+
23+
toggleLock(port) {
24+
const { locked } = this.state;
25+
port.postMessage({ action: 'setLock', payload: !locked });
26+
this.setState({ locked: !locked });
27+
}
28+
29+
render() {
30+
const { paused, locked } = this.state;
31+
const { port } = this.props;
32+
return (
33+
<div className="buttons-container">
34+
<div className="pause-button" onClick={() => this.togglePause(port)}>{(paused) ? 'Resume' : 'Pause'}</div>
35+
<div className="lock-button" onClick={() => this.toggleLock(port)}>{(locked) ? 'Unlock' : 'Lock'}</div>
36+
</div>
37+
);
38+
}
39+
}
40+
export default ButtonsContainer;

src/app/containers/MainContainer.jsx

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import HeadContainer from './HeadContainer';
33
import ActionContainer from './ActionContainer';
44
import StateContainer from './StateContainer';
55
import TravelContainer from './TravelContainer';
6+
import ButtonsContainer from './ButtonsContainer';
7+
8+
const autoBind = require('auto-bind');
69

710
class MainContainer extends Component {
811
constructor(props) {
@@ -13,13 +16,8 @@ class MainContainer extends Component {
1316
currentIndex: null,
1417
port: null,
1518
};
16-
this.handleChangeSnapshot = this.handleChangeSnapshot.bind(this);
17-
this.handleSendSnapshot = this.handleSendSnapshot.bind(this);
18-
this.handleJumpSnapshot = this.handleJumpSnapshot.bind(this);
19-
this.emptySnapshot = this.emptySnapshot.bind(this);
20-
this.moveBackward = this.moveBackward.bind(this);
21-
this.moveForward = this.moveForward.bind(this);
22-
this.playForward = this.playForward.bind(this);
19+
20+
autoBind(this);
2321
}
2422

2523
componentDidMount() {
@@ -63,20 +61,20 @@ class MainContainer extends Component {
6361
}
6462

6563
playForward() {
66-
var play = setInterval(() => {
64+
const play = setInterval(() => {
6765
const { snapshots, snapshotIndex } = this.state;
6866
if (snapshotIndex < snapshots.length - 1) {
6967
const newIndex = snapshotIndex + 1;
7068
this.handleJumpSnapshot(newIndex);
7169
this.setState({ snapshotIndex: newIndex });
7270
} else clearInterval(play);
73-
}, 1000)
71+
}, 1000);
7472
play();
7573
}
7674

7775
emptySnapshot() {
78-
const { port } = this.state;
79-
this.setState({ snapshots: [], snapshotIndex: 0 });
76+
const { port, snapshots } = this.state;
77+
this.setState({ snapshots: [snapshots[0]], snapshotIndex: 0 });
8078
port.postMessage({ action: 'emptySnap' });
8179
}
8280

@@ -94,37 +92,8 @@ class MainContainer extends Component {
9492
port.postMessage({ action: 'jumpToSnap', payload: snapshots[snapshotIndex] });
9593
}
9694

97-
// when the jump button is clicked, send a message to npm package with the selected snapshot
98-
handleSendSnapshot(snapshotIndex) {
99-
const { snapshots, port } = this.state;
100-
let { currentIndex } = this.state;
101-
const payload = [];
102-
103-
// currentIndex is initialized to null
104-
// currentIndex = null means that the user hasn't jumped yet
105-
currentIndex = currentIndex === null ? snapshots.length - 1 : currentIndex;
106-
107-
// if the user wants to jump backward
108-
if (currentIndex > snapshotIndex) {
109-
for (let i = currentIndex - 1; i >= snapshotIndex; i -= 1) {
110-
payload.push(snapshots[i]);
111-
}
112-
} else {
113-
// if the user wants to jump forward
114-
for (let i = currentIndex + 1; i <= snapshotIndex; i += 1) {
115-
payload.push(snapshots[i]);
116-
}
117-
}
118-
119-
// currentIndex should be changed to index the user jumped to
120-
this.setState({ currentIndex: snapshotIndex });
121-
122-
// send the arrays of snapshots to background script
123-
port.postMessage({ action: 'stepToSnap', payload });
124-
}
125-
12695
render() {
127-
const { snapshots, snapshotIndex } = this.state;
96+
const { snapshots, snapshotIndex, port } = this.state;
12897
return (
12998
<div className="main-container">
13099
<HeadContainer />
@@ -146,6 +115,7 @@ class MainContainer extends Component {
146115
moveForward={this.moveForward}
147116
playForward={this.playForward}
148117
/>
118+
<ButtonsContainer port={port} />
149119
</div>
150120
</div>
151121
);

src/app/styles/components/_buttons.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@
4545
margin: 7px;
4646
}
4747

48+
.lock-button {
49+
@extend %button-shared;
50+
}
51+
52+
.pause-button {
53+
@extend %button-shared;
54+
}
55+
4856
%button-shared {
4957
display: flex;
5058
justify-content: center;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.buttons-container {
2+
margin: 0 1% 0 1%;
3+
display: grid;
4+
grid-template-columns: 1fr 1fr;
5+
grid-gap: 1%;
6+
padding: 1% 0 1% 0;
7+
}

src/app/styles/main.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
@import 'base/base', 'base/fonts', 'base/helpers', 'base/typography';
88

99
// 4. Layout-related sections
10-
@import 'layout/actionContainer', 'layout/stateContainer', 'layout/travelContainer';
10+
@import 'layout/actionContainer', 'layout/stateContainer', 'layout/travelContainer', 'layout/buttonsContainer';
1111

1212
// 5. Components
1313
@import 'components/buttons', 'components/actionComponent',
@@ -17,4 +17,4 @@
1717
@import 'styles';
1818

1919
// slider component
20-
@import './components/rc-slider', './components/sliderHandle';
20+
@import './components/rc-slider', './components/sliderHandle';

0 commit comments

Comments
 (0)