Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 53 additions & 87 deletions media/js/src/editors/TemplateGraphEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,110 +3,77 @@ import PropTypes from 'prop-types';
import { MathJaxProvider, MathJaxFormula } from 'mathjax3-react';
import { create, all } from 'mathjs';

import EditableControl from '../form-components/EditableControl.jsx';
import {handleFormUpdate} from '../utils.js';


const math = create(all, {});


export default class TemplateGraphEditor extends React.Component {
/**
* checkFormula()
*
* Check that the given expression (as a string) is a valid
* expression with the x scope.
*
* Returns a boolean, true if valid.
*/
checkFormula(expression) {
try {
math.evaluate(expression, { x: 1 });
return false;
} catch (e) {
return true;
} catch (e) {
return false;
}
}

render() {
return (
<>
<div className="d-flex flex-column">
<div className="row">
<div className="col d-flex">
<label
className="form-check-label me-2 flex-shrink-1 d-flex align-self-center"
htmlFor="gExpression">
<MathJaxProvider>
<MathJaxFormula formula="$$y = $$" />
</MathJaxProvider>
</label>
<EditableControl
id="gExpression"
name="Expression"
className="w-100"
value={this.props.gExpression}
valueEditable={true}
onBlur={true}
isInstructor={this.props.isInstructor}
disabled={this.props.disabled}
updateGraph={this.props.updateGraph} />
{
this.checkFormula(this.props.gExpression) &&
<p className='text-danger mt-2'>Formula Error</p>
}
</div>
</div>
</div>

<div className="d-flex flex-column mt-2">
<div className="row">
<div className="col d-flex">
<label
className="form-check-label me-2 flex-shrink-1 d-flex align-self-center"
htmlFor="gExpression2">
<MathJaxProvider>
<MathJaxFormula formula="$$y = $$" />
</MathJaxProvider>
renderExpressionInput(index=1, value) {
let name = 'Expression';
if (index > 1) {
name = `Expression${index}`;
}

</label>
<EditableControl
id="gExpression2"
name="Expression 2"
className="w-100"
value={this.props.gExpression2}
valueEditable={true}
onBlur={true}
isInstructor={this.props.isInstructor}
disabled={this.props.disabled}
updateGraph={this.props.updateGraph} />
{
this.checkFormula(this.props.gExpression2) &&
<p className='text-danger mt-2'>Formula Error</p>
}
</div>
</div>
</div>
const isInvalid = !this.checkFormula(value);
let invalidClass = '';
if (isInvalid) {
invalidClass = 'is-invalid';
}

<div className="d-flex flex-column mt-2">
<div className="row">
<div className="col d-flex">
<label
className="form-check-label me-2 flex-shrink-1 d-flex align-self-center"
htmlFor="gExpression3">
<MathJaxProvider>
<MathJaxFormula formula="$$y = $$" />
</MathJaxProvider>
return (
<div className="d-flex flex-column mb-2">
<div className="row">
<label className="w-100" htmlFor={`g${name}`}>
{`Expression ${index}`}
</label>
<div className="col d-flex">

</label>
<EditableControl
id="gExpression3"
name="Expression 3"
className="w-100"
value={this.props.gExpression3}
valueEditable={true}
onBlur={true}
isInstructor={this.props.isInstructor}
disabled={this.props.disabled}
updateGraph={this.props.updateGraph} />
{
this.checkFormula(this.props.gExpression3) &&
<p className='text-danger mt-2'>Formula Error</p>
}
</div>
<label
className="form-check-label me-2 flex-shrink-1 d-flex align-self-center"
htmlFor={`g${name}`}>
<MathJaxProvider>
<MathJaxFormula formula="$$y = $$" />
</MathJaxProvider>
</label>
<input
type="text"
className={`form-control ${invalidClass}`}
id={`g${name}`}
name={name}
defaultValue={value}
onBlur={handleFormUpdate.bind(this)}
disabled={this.props.disabled} />
</div>
</div>
</div>
);
}

render() {
return (
<>
{this.renderExpressionInput(1, this.props.gExpression)}
{this.renderExpressionInput(2, this.props.gExpression2)}
{this.renderExpressionInput(3, this.props.gExpression3)}
</>
);
}
Expand All @@ -115,7 +82,6 @@ export default class TemplateGraphEditor extends React.Component {
TemplateGraphEditor.propTypes = {
gType: PropTypes.number,
updateGraph: PropTypes.func.isRequired,
isInstructor: PropTypes.bool.isRequired,
disabled: PropTypes.bool,

gExpression: PropTypes.string,
Expand Down
23 changes: 13 additions & 10 deletions media/js/src/form-components/EditableControl.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ import {handleFormUpdate} from '../utils.js';
export default class EditableControl extends React.Component {

render() {
const commonAttrs = {
name: this.props.id,
className: `form-control ${this.props.inputClass}`,
type: 'text',
maxLength: this.props.maxLength || 60,
disabled: this.props.disabled,
};

let input = (
<input
name={this.props.id}
className="form-control"
type="text"
maxLength={this.props.maxLength || 60}
disabled={this.props.disabled}
{...commonAttrs}
value={this.props.value}
onChange={handleFormUpdate.bind(this)} />
);

if (this.props.onBlur) {
input = (
<input
name={this.props.id}
className="form-control"
type="text"
maxLength={this.props.maxLength || 60}
disabled={this.props.disabled}
{...commonAttrs}
defaultValue={this.props.value}
onBlur={handleFormUpdate.bind(this)} />
);
Expand Down Expand Up @@ -64,6 +64,9 @@ EditableControl.propTypes = {
// Custom classes for the parent element of this component.
className: PropTypes.string,

// Custom classes for the input element of this component.
inputClass: PropTypes.string,

onBlur: PropTypes.bool,
disabled: PropTypes.bool,
maxLength: PropTypes.number
Expand Down