Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F22425463
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
10 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/frontend/lib/components/input.jsx b/frontend/lib/components/input.jsx
index 5784d5e..649466c 100644
--- a/frontend/lib/components/input.jsx
+++ b/frontend/lib/components/input.jsx
@@ -1,60 +1,73 @@
var React = require('react');
var PropTypes = React.PropTypes;
var TypingGolf = require('../typing-golf-components.js');
+var de = require("deep-equal");
+
var Input = React.createClass({
componentDidMount: function() {
this.selectText(this.props.state);
},
handleChange: function(event) {
+ if(event.type=="keydown") return;
if(event.nativeEvent.button != undefined){
return event.preventDefault();
}
+
+ var eventDirection = event.target.selectionDirection[0];
+ var new_state = {
+ text: event.target.value,
+ start: event.target.selectionStart,
+ end: event.target.selectionEnd,
+ direction: eventDirection
+ }
+
+ if(!de(new_state, this.props.state)){
+ console.log(event);
+ console.log(new_state, this.props.state);
+ this.props.onChange(new_state)
+ }
- var eventDirection = ((event.target.selectionDirection).localeCompare("backward") == 0) ? "b" : "f";
- var new_state = {
- text: event.target.value,
- start: event.target.selectionStart,
- end: event.target.selectionEnd,
- direction: eventDirection
- }
- this.props.onChange(new_state)
+ event.stopPropagation();
},
selectText: function(state){
this.refs.input.focus()
- var direction;
- if (state.start <= state.end) direction = "forward"
- else direction = "backward"
+ var direction;
+ if(state.direction == "b"){
+ direction = "backward";
+ }else{
+ direction = "forward";
+ }
this.refs.input.setSelectionRange(state.start, state.end, direction);
},
focusInput: function(){ this.selectText(this.props.state); },
clickInput: function(e) {
e.preventDefault();
this.selectText(this.props.state);
},
render: function() {
return (
<div className="flex-container">
<div className="content">
<h3>{this.props.title || ""}</h3>
<textarea
className="input"
type="text"
rows="1"
value={this.props.state.text}
onChange={this.handleChange}
onSelect={this.handleChange}
onFocus={this.focusInput}
onClick={this.clickInput}
onBlur={this.props.onBlur}
ref="input" />
</div>
</div>
)
}
});
module.exports = Input;
diff --git a/frontend/lib/components/tasks.jsx b/frontend/lib/components/tasks.jsx
index 4bd4768..a3d55a2 100644
--- a/frontend/lib/components/tasks.jsx
+++ b/frontend/lib/components/tasks.jsx
@@ -1,57 +1,43 @@
var React = require("react");
var qwest = require("qwest");
var Task = require("./task-li.jsx");
var Tasks = React.createClass({
getInitialState: function(){
return {
loaded: false,
data: []
};
},
refresh: function(){
var self = this;
self.setState({
loaded: false
});
qwest.get("/api/v1/resources/task")
.then(function(xhr, response){
self.setState({
data: response,
loaded: true
});
});
},
componentDidMount: function(){
this.refresh();
},
render: function(){
- if (this.props.params.id !== undefined) {
- var data = this.state.data;
- var choosen_task;
- for (var i = 0; i < data.length; i++) {
- if (data[i].id === this.props.params.id) choosen_task = data[i]
- }
+ if(!this.state.loaded){
+ return <div className="content"> Loading...</div>
+ }else{
+ var elements = this.state.data.map(function(task){return <Task data={task} key={task.id}/>});
return (
- <div>
- {React.cloneElement(this.props.children, { task: choosen_task })}
+ <div className="content">
+ {elements}
</div>
)
- } else {
-
- if(!this.state.loaded){
- return <div className="content"> Loading...</div>
- }else{
- var elements = this.state.data.map(function(task){return <Task data={task} key={task.id}/>});
- return (
- <div className="content">
- {elements}
- </div>
- )
- }
}
}
});
module.exports = Tasks;
diff --git a/frontend/lib/components/view-task.jsx b/frontend/lib/components/view-task.jsx
index 88d0df4..986363a 100644
--- a/frontend/lib/components/view-task.jsx
+++ b/frontend/lib/components/view-task.jsx
@@ -1,105 +1,122 @@
-
var React = require('react');
var PropTypes = React.PropTypes;
var router = require('react-router');
var equal = require('deep-equal');
var TypingGolf = require('../typing-golf-components.js')
+var qwest = require("qwest");
+
var ViewTask = React.createClass({
contextTypes: {
router: React.PropTypes.object.isRequired
},
- componentWillMount: function() {
- if (this.props.task === undefined) {
- this.context.router.push('/tasks')
- }
- },
-
+ fetch: function(){
+ this.setState({loaded: false});
+ var self = this;
+ qwest.get("/api/v1/resources/task/" + this.props.params.id)
+ .then(function(xhr, data){
+ var task = JSON.parse(data.body.json);
+ self.setState({
+ loaded: true,
+ title: task.title,
+ to: task.to,
+ from: task.from
+ });
+ });
+ },
getInitialState: function() {
- if (this.props.task !== undefined) {
- var body = JSON.parse(this.props.task.body.json);
- return {
- from: body.from,
- to: body.to,
- solution: body.solution,
- title: body.title,
- resolved: false,
- counter: 0,
- };
- } else {
- return null;
- }
+ return {
+ to: {},
+ from: {},
+ title: "",
+ solution: [],
+ resolved: false,
+ counter: 0,
+ };
},
+ componentDidMount: function(){
+ console.log("hello!");
+ },
componentDidUpdate: function(prevProps, prevState) {
- console.log('from',this.state.from)
- console.log('to ',this.state.to)
- if (equal(this.state.from, this.state.to) ) {
+ try{
+ if (equal(this.state.from, this.state.to) && this.state.loaded ) {
if (this.state.resolved === false) {
this.setState({
resolved: true
})
- alert(`You've done this task in `+this.state.counter+` steps`)
- this.context.router.push('/tasks')
+ alert("You've done this task in "+this.state.counter+" steps")
+// this.context.router.push('/tasks')
}
}
+ }catch(e){
+ console.log(e);
+ }
},
- handleChange: function(event) {
-
- console.log(event.direction)
- var direction = ((event.direction).localeCompare("b") == 0) ? "b" : "f";
+ handleChange: function(new_data) {
+ var direction = ((new_data.direction).localeCompare("b") == 0) ? "b" : "f";
var counter = this.state.counter;
- if (event.text !== this.state.from.text ||
- event.start !== this.state.from.start ||
- event.end !== this.state.from.end ||
+ if (new_data.text !== this.state.from.text ||
+ new_data.start !== this.state.from.start ||
+ new_data.end !== this.state.from.end ||
direction !== this.state.from.direction) {
counter += 1;
}
var new_state = {
- text: event.text,
- start: event.start,
- end: event.end,
+ text: new_data.text,
+ start: new_data.start,
+ end: new_data.end,
direction: direction
}
this.setState({
from: new_state,
counter: counter
});
- },
+ },
+ componentDidMount: function(){
+ this.fetch();
+ },
render: function() {
- if (this.props.task !== undefined) {
- var body = this.state;
- var direction = ""
- if (this.state.to.start !== this.state.to.end) {
- if (this.state.to.direction === "b") direction += "(Direction selection: ←)"
- else direction += "(Direction selection: →)"
- }
- return (
- <div>
- <div className="content">
- <h2>Task: {body.title}</h2>
- </div>
- <div>
- <TypingGolf.Input
- state={body.from}
- onChange={this.handleChange}
- onSelect={this.handleChange}
- title="Turn this:"/>
- <TypingGolf.Target
- state={body.to}
- title={"Into this state below in "+body.solution.length+" steps: " +direction}/>
- <p className="end-text-details">You've done {this.state.counter} steps</p>
- </div>
- </div>
- );
- } else {
- return null;
- }
+ try{
+ var ret;
+ if(!this.state.loaded){
+ ret = <div className="content">Loading...</div>
+ }else{
+ var body = this.state;
+ var direction = ""
+ if (body.to.start !== body.to.end) {
+ if (body.to.direction === "b") direction += "(Direction selection: ←)"
+ else direction += "(Direction selection: →)"
+ }
+ ret = (
+ <div>
+ <div className="content">
+ <h2>Task: {body.title}</h2>
+ </div>
+ <div>
+ <TypingGolf.Input
+ state={body.from}
+ onChange={this.handleChange}
+ onSelect={this.handleChange}
+ title="Turn this:"/>
+ <p className="end-text-details">You've done {body.counter} steps</p>
+ <TypingGolf.Target
+ state={body.to}
+ title={"Into this state below in "+body.solution.length+" steps: " +direction}
+ />
+ </div>
+ </div>
+ );
+ }
+ return ret;
+ }catch(e){
+ console.log(e);
+ }
}
});
module.exports = ViewTask;
diff --git a/frontend/lib/routes.jsx b/frontend/lib/routes.jsx
index 4b36174..5006e48 100644
--- a/frontend/lib/routes.jsx
+++ b/frontend/lib/routes.jsx
@@ -1,21 +1,22 @@
var React = require("react");
var ReactDOM = require("react-dom");
var ReactRouter = require('react-router');
var Router = ReactRouter.Router;
var Route = ReactRouter.Route;
var IndexRoute = ReactRouter.IndexRoute;
var DefaultRoute = ReactRouter.DefaultRoute;
var TypingGolf = require('./typing-golf-components.js');
var hashHistory = ReactRouter.hashHistory;
ReactDOM.render(
<Router history={hashHistory}>
<Route path="/" component={TypingGolf.Container}>
<Route path="new-task" component={TypingGolf.NewTask}/>
- <Route path="tasks" component={TypingGolf.Tasks}>
+ <Route path="tasks">
+ <IndexRoute component={TypingGolf.Tasks}/>
<Route path=":id" component={TypingGolf.ViewTask}/>
</Route>
</Route>
</Router>,
document.getElementById('app')
);
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Nov 28, 04:23 (2 h, 49 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1230273
Default Alt Text
(10 KB)
Attached To
Mode
rTG Typing Golf
Attached
Detach File
Event Timeline