migrating old version react to last version

This commit is contained in:
Hongarc 2019-04-23 00:49:15 +07:00
parent 634d0ad5e0
commit 64eb8ab773
10 changed files with 124 additions and 109 deletions

View file

@ -34,9 +34,12 @@
"dependencies": { "dependencies": {
"backbone": "^1.4.0", "backbone": "^1.4.0",
"flux": "^3.1.3", "flux": "^3.1.3",
"jquery": "^3.4.0",
"markdown": "^0.5.0", "markdown": "^0.5.0",
"prop-types": "^15.7.2",
"q": "^1.5.1", "q": "^1.5.1",
"react": "^16.8.6", "react": "^16.8.6",
"react-dom": "^16.8.6",
"underscore": "~1.4.3" "underscore": "~1.4.3"
} }
} }

View file

@ -1,6 +1,7 @@
var Backbone = require('backbone'); var Backbone = require('backbone');
var EventEmitter = require('events').EventEmitter; var EventEmitter = require('events').EventEmitter;
var React = require('react'); var React = require('react');
var ReactDOM = require('react-dom');
var util = require('../util'); var util = require('../util');
var intl = require('../intl'); var intl = require('../intl');
@ -251,7 +252,7 @@ var initDemo = function(sandbox) {
} }
if (params.hasOwnProperty('STARTREACT')) { if (params.hasOwnProperty('STARTREACT')) {
/* /*
React.render( ReactDOM.render(
React.createElement(CommandView, {}), React.createElement(CommandView, {}),
document.getElementById(params['STARTREACT']) document.getElementById(params['STARTREACT'])
);*/ );*/
@ -315,11 +316,11 @@ function CommandUI() {
el: $('#commandLineBar') el: $('#commandLineBar')
}); });
React.render( ReactDOM.render(
React.createElement(MainHelperBarView), React.createElement(MainHelperBarView),
document.getElementById('helperBarMount') document.getElementById('helperBarMount')
); );
React.render( ReactDOM.render(
React.createElement( React.createElement(
CommandHistoryView, CommandHistoryView,
{ commandCollection: this.commandCollection } { commandCollection: this.commandCollection }

View file

@ -1,6 +1,8 @@
var React = require('react');
var PropTypes = require('prop-types');
var CommandView = require('../react_views/CommandView.jsx'); var CommandView = require('../react_views/CommandView.jsx');
var Main = require('../app'); var Main = require('../app');
var React = require('react');
var _subscribeEvents = [ var _subscribeEvents = [
'add', 'add',
@ -9,14 +11,9 @@ var _subscribeEvents = [
'all' 'all'
]; ];
var CommandHistoryView = React.createClass({ class CommandHistoryView extends React.Component {
propTypes: { componentDidMount() {
// the backbone command model collection
commandCollection: React.PropTypes.object.isRequired
},
componentDidMount: function() {
for (var i = 0; i < _subscribeEvents.length; i++) { for (var i = 0; i < _subscribeEvents.length; i++) {
this.props.commandCollection.on( this.props.commandCollection.on(
_subscribeEvents[i], _subscribeEvents[i],
@ -28,9 +25,9 @@ var CommandHistoryView = React.createClass({
this.props.commandCollection.on('change', this.scrollDown, this); this.props.commandCollection.on('change', this.scrollDown, this);
Main.getEvents().on('commandScrollDown', this.scrollDown, this); Main.getEvents().on('commandScrollDown', this.scrollDown, this);
Main.getEvents().on('clearOldCommands', this.clearOldCommands, this); Main.getEvents().on('clearOldCommands', this.clearOldCommands, this);
}, }
componentWillUnmount: function() { componentWillUnmount() {
for (var i = 0; i < _subscribeEvents.length; i++) { for (var i = 0; i < _subscribeEvents.length; i++) {
this.props.commandCollection.off( this.props.commandCollection.off(
_subscribeEvents[i], _subscribeEvents[i],
@ -38,13 +35,13 @@ var CommandHistoryView = React.createClass({
this this
); );
} }
}, }
updateFromCollection: function() { updateFromCollection() {
this.forceUpdate(); this.forceUpdate();
}, }
render: function() { render() {
var allCommands = []; var allCommands = [];
this.props.commandCollection.each(function(command, index) { this.props.commandCollection.each(function(command, index) {
allCommands.push( allCommands.push(
@ -60,9 +57,9 @@ var CommandHistoryView = React.createClass({
{allCommands} {allCommands}
</div> </div>
); );
}, }
scrollDown: function() { scrollDown() {
var cD = document.getElementById('commandDisplay'); var cD = document.getElementById('commandDisplay');
var t = document.getElementById('terminal'); var t = document.getElementById('terminal');
@ -81,9 +78,9 @@ var CommandHistoryView = React.createClass({
if (shouldScroll) { if (shouldScroll) {
t.scrollTop = t.scrollHeight; t.scrollTop = t.scrollHeight;
} }
}, }
clearOldCommands: function() { clearOldCommands() {
// go through and get rid of every command that is "processed" or done // go through and get rid of every command that is "processed" or done
var toDestroy = []; var toDestroy = [];
@ -100,6 +97,11 @@ var CommandHistoryView = React.createClass({
this.scrollDown(); this.scrollDown();
} }
}); }
CommandHistoryView.propTypes = {
// the backbone command model collection
commandCollection: PropTypes.object.isRequired
};
module.exports = CommandHistoryView; module.exports = CommandHistoryView;

View file

@ -1,4 +1,5 @@
var React = require('react'); var React = require('react');
var PropTypes = require('prop-types');
var reactUtil = require('../util/reactUtil'); var reactUtil = require('../util/reactUtil');
var keyMirror = require('react/lib/keyMirror'); var keyMirror = require('react/lib/keyMirror');
@ -9,26 +10,20 @@ var STATUSES = keyMirror({
finished: null finished: null
}); });
var CommandView = React.createClass({ class CommandView extends React.Component{
propTypes: { componentDidMount() {
// the backbone command model
command: React.PropTypes.object.isRequired,
id: React.PropTypes.string,
},
componentDidMount: function() {
this.props.command.on('change', this.updateStateFromModel, this); this.props.command.on('change', this.updateStateFromModel, this);
this.props.command.on('destroy', this.onModelDestroy, this); this.props.command.on('destroy', this.onModelDestroy, this);
this.updateStateFromModel(); this.updateStateFromModel();
}, }
componentWillUnmount: function() { componentWillUnmount() {
this.props.command.off('change', this.updateStateFromModel, this); this.props.command.off('change', this.updateStateFromModel, this);
this.props.command.off('destroy', this.onModelDestroy, this); this.props.command.off('destroy', this.onModelDestroy, this);
}, }
onModelDestroy: function() { onModelDestroy() {
if (!this.isMounted()) { if (!this.isMounted()) {
return; return;
} }
@ -39,9 +34,9 @@ var CommandView = React.createClass({
} }
React.unmountComponentAtNode(this.getDOMNode().parentNode); React.unmountComponentAtNode(this.getDOMNode().parentNode);
}, }
updateStateFromModel: function() { updateStateFromModel() {
var commandJSON = this.props.command.toJSON(); var commandJSON = this.props.command.toJSON();
this.setState({ this.setState({
status: commandJSON.status, status: commandJSON.status,
@ -49,18 +44,19 @@ var CommandView = React.createClass({
warnings: commandJSON.warnings, warnings: commandJSON.warnings,
result: commandJSON.result result: commandJSON.result
}); });
}, }
getInitialState: function() { constructor(props, context) {
return { super(props, context);
this.state = {
status: STATUSES.inqueue, status: STATUSES.inqueue,
rawStr: 'git commit', rawStr: 'git commit',
warnings: [], warnings: [],
result: '' result: ''
}; };
}, }
render: function() { render() {
var commandClass = reactUtil.joinClasses([ var commandClass = reactUtil.joinClasses([
this.state.status, this.state.status,
'commandLine', 'commandLine',
@ -89,9 +85,9 @@ var CommandView = React.createClass({
</div> </div>
</div> </div>
); );
}, }
renderResult: function() { renderResult() {
if (!this.state.result) { if (!this.state.result) {
return null; return null;
} }
@ -126,9 +122,9 @@ var CommandView = React.createClass({
{result} {result}
</div> </div>
); );
}, }
renderFormattedWarnings: function() { renderFormattedWarnings() {
var warnings = this.state.warnings; var warnings = this.state.warnings;
var result = []; var result = [];
for (var i = 0; i < warnings.length; i++) { for (var i = 0; i < warnings.length; i++) {
@ -141,6 +137,12 @@ var CommandView = React.createClass({
} }
return result; return result;
} }
}); };
CommandView.propTypes = {
// the backbone command model
command: PropTypes.object.isRequired,
id: PropTypes.string,
};
module.exports = CommandView; module.exports = CommandView;

View file

@ -1,31 +1,27 @@
var React = require('react');
var PropTypes = require('prop-types');
var HelperBarView = require('../react_views/HelperBarView.jsx'); var HelperBarView = require('../react_views/HelperBarView.jsx');
var Main = require('../app'); var Main = require('../app');
var React = require('react');
var log = require('../log'); var log = require('../log');
var CommandsHelperBarView = React.createClass({ class CommandsHelperBarView extends React.Component {
propTypes: { render() {
shown: React.PropTypes.bool.isRequired,
onExit: React.PropTypes.func.isRequired
},
render: function() {
return ( return (
<HelperBarView <HelperBarView
items={this.getItems()} items={this.getItems()}
shown={this.props.shown} shown={this.props.shown}
/> />
); );
}, }
fireCommand: function(command) { fireCommand(command) {
log.viewInteracted('commandHelperBar'); log.viewInteracted('commandHelperBar');
Main.getEventBaton().trigger('commandSubmitted', command); Main.getEventBaton().trigger('commandSubmitted', command);
}, }
getItems: function() { getItems() {
return [{ return [{
text: 'Levels', text: 'Levels',
onClick: function() { onClick: function() {
@ -64,6 +60,11 @@ var CommandsHelperBarView = React.createClass({
}]; }];
} }
}); };
CommandsHelperBarView.propTypes = {
shown: PropTypes.bool.isRequired,
onExit: PropTypes.func.isRequired
};
module.exports = CommandsHelperBarView; module.exports = CommandsHelperBarView;

View file

@ -1,16 +1,11 @@
var React = require('react'); var React = require('react');
var PropTypes = require('prop-types');
var reactUtil = require('../util/reactUtil'); var reactUtil = require('../util/reactUtil');
var HelperBarView = React.createClass({ class HelperBarView extends React.Component {
propTypes: { render() {
className: React.PropTypes.string,
shown: React.PropTypes.bool.isRequired,
items: React.PropTypes.array.isRequired
},
render: function() {
var topClassName = reactUtil.joinClasses([ var topClassName = reactUtil.joinClasses([
'helperBar', 'helperBar',
'transitionAll', 'transitionAll',
@ -32,9 +27,9 @@ var HelperBarView = React.createClass({
}.bind(this))} }.bind(this))}
</div> </div>
); );
}, }
renderItem: function(item, index) { renderItem(item, index) {
var testID = item.icon || item.testID || var testID = item.icon || item.testID ||
item.text.toLowerCase(); item.text.toLowerCase();
if (item.newPageLink) { if (item.newPageLink) {
@ -63,6 +58,13 @@ var HelperBarView = React.createClass({
); );
} }
}); };
HelperBarView.propTypes = {
className: PropTypes.string,
shown: PropTypes.bool.isRequired,
items: PropTypes.array.isRequired
};
module.exports = HelperBarView; module.exports = HelperBarView;

View file

@ -1,32 +1,29 @@
var PropTypes = require('prop-types');
var HelperBarView = require('../react_views/HelperBarView.jsx'); var HelperBarView = require('../react_views/HelperBarView.jsx');
var Main = require('../app'); var Main = require('../app');
var React = require('react'); var React = require('react');
var log = require('../log'); var log = require('../log');
var IntlHelperBarView = React.createClass({ class IntlHelperBarView extends React.Component{
propTypes: { render() {
shown: React.PropTypes.bool.isRequired,
onExit: React.PropTypes.func.isRequired
},
render: function() {
return ( return (
<HelperBarView <HelperBarView
items={this.getItems()} items={this.getItems()}
shown={this.props.shown} shown={this.props.shown}
/> />
); );
}, }
fireCommand: function(command) { fireCommand(command) {
log.viewInteracted('intlSelect'); log.viewInteracted('intlSelect');
Main.getEventBaton().trigger('commandSubmitted', command); Main.getEventBaton().trigger('commandSubmitted', command);
this.props.onExit(); this.props.onExit();
}, }
getItems: function() { getItems() {
return [{ return [{
text: 'Git Branching', text: 'Git Branching',
testID: 'english', testID: 'english',
@ -107,6 +104,11 @@ var IntlHelperBarView = React.createClass({
}]; }];
} }
}); };
IntlHelperBarView.propTypes = {
shown: PropTypes.bool.isRequired,
onExit: PropTypes.func.isRequired
}
module.exports = IntlHelperBarView; module.exports = IntlHelperBarView;

View file

@ -1,26 +1,20 @@
var React = require('react'); var React = require('react');
var PropTypes = React.PropTypes; var PropTypes = require('prop-types');
var intl = require('../intl'); var intl = require('../intl');
var reactUtil = require('../util/reactUtil'); var reactUtil = require('../util/reactUtil');
var LevelToolbarView = React.createClass({ class LevelToolbarView extends React.Component {
propTypes: { constructor(props, context) {
name: PropTypes.string.isRequired, super(props, context);
onGoalClick: PropTypes.func.isRequired, this.state = {
onObjectiveClick: PropTypes.func.isRequired,
parent: PropTypes.object.isRequired
},
getInitialState: function() {
return {
isHidden: true, isHidden: true,
isGoalExpanded: this.props.parent.getIsGoalExpanded() isGoalExpanded: this.props.parent.getIsGoalExpanded()
}; };
}, }
componentDidMount: function() { componentDidMount() {
this.setState({ this.setState({
isHidden: this.props.parent.getIsGoalExpanded() isHidden: this.props.parent.getIsGoalExpanded()
}); });
@ -33,9 +27,9 @@ var LevelToolbarView = React.createClass({
isGoalExpanded: this.props.parent.getIsGoalExpanded() isGoalExpanded: this.props.parent.getIsGoalExpanded()
}); });
}.bind(this)); }.bind(this));
}, }
render: function() { render() {
return ( return (
<div className={reactUtil.joinClasses([ <div className={reactUtil.joinClasses([
'toolbar', 'toolbar',
@ -78,6 +72,13 @@ var LevelToolbarView = React.createClass({
); );
} }
}); };
LevelToolbarView.propTypes = {
name: PropTypes.string.isRequired,
onGoalClick: PropTypes.func.isRequired,
onObjectiveClick: PropTypes.func.isRequired,
parent: PropTypes.object.isRequired
}
module.exports = LevelToolbarView; module.exports = LevelToolbarView;

View file

@ -14,15 +14,16 @@ var BARS = keyMirror({
COMMANDS: null COMMANDS: null
}); });
var MainHelperBarView = React.createClass({ class MainHelperBarView extends React.Component {
getInitialState: function() { constructor(props, context) {
return { super(props, context);
this.state = {
shownBar: BARS.SELF shownBar: BARS.SELF
}; };
}, }
render: function() { render() {
return ( return (
<div> <div>
<HelperBarView <HelperBarView
@ -40,15 +41,15 @@ var MainHelperBarView = React.createClass({
/> />
</div> </div>
); );
}, }
showSelf: function() { showSelf() {
this.setState({ this.setState({
shownBar: BARS.SELF shownBar: BARS.SELF
}); });
}, }
getItems: function() { getItems() {
return [{ return [{
icon: 'question-sign', icon: 'question-sign',
onClick: function() { onClick: function() {
@ -74,6 +75,6 @@ var MainHelperBarView = React.createClass({
}]; }];
} }
}); };
module.exports = MainHelperBarView; module.exports = MainHelperBarView;

View file

@ -55,7 +55,7 @@ var AnimationQueue = Backbone.Model.extend({
}, },
add: function(animation) { add: function(animation) {
if (!animation instanceof Animation) { if (!(animation instanceof Animation)) {
throw new Error("Need animation not something else"); throw new Error("Need animation not something else");
} }