Skip to content

Commit

Permalink
#554 Added source model to information available to nav items to enab…
Browse files Browse the repository at this point in the history
…le variable substitution in the nav area.
  • Loading branch information
FrenjaminBanklin committed Oct 17, 2023
1 parent 1206b75 commit 1a7b13a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const mapTypeToDescription = {
[PICK_LIST]: 'Generate a sub-list by randomly choosing from a list'
}

const SERIES_TYPE_OPTIONS = ['', 'arithmetic', 'geometric']

export {
STATIC_VALUE,
RANDOM_NUMBER,
Expand All @@ -42,6 +44,7 @@ export {
RANDOM_SEQUENCE,
PICK_ONE,
PICK_LIST,
SERIES_TYPE_OPTIONS,
typeList,
mapTypeToString,
mapTypeToDescription
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Common from 'obojobo-document-engine/src/scripts/common'
import VariableProperty from './variable-property/variable-property'
import NewVariable from './new-variable/new-variable'
import VariableBlock from './variable-block'
import { RANDOM_NUMBER, RANDOM_LIST } from './constants'
import { getParsedRange } from '../../../common/util/range-parsing'
import { changeVariableToType, validateVariableValue, validateMultipleVariables } from './variable-util'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
RANDOM_LIST,
RANDOM_SEQUENCE,
PICK_ONE,
PICK_LIST
PICK_LIST,
SERIES_TYPE_OPTIONS
} from '../constants'

const VariableValues = props => {
Expand Down Expand Up @@ -93,6 +94,10 @@ const VariableValues = props => {
return className
}

const constrainSeriesTypeOption = value => {
return SERIES_TYPE_OPTIONS.includes(value) ? value : ''
}

switch (variable.type) {
case STATIC_VALUE:
return (
Expand Down Expand Up @@ -312,7 +317,7 @@ const VariableValues = props => {
<select
className={getInputClassForType('seriesType', true)}
name="seriesType"
value={variable.seriesType || ''}
value={constrainSeriesTypeOption(variable.seriesType) || ''}
onChange={onChange}
>
<option disabled value="">
Expand All @@ -322,6 +327,13 @@ const VariableValues = props => {
<option value="geometric">Geometric (Multiply)</option>
</select>
</label>
{variable.errors
&& variable.errors.seriesType
&& (! SERIES_TYPE_OPTIONS.includes(variable.seriesType)) ?
<span className="invalid-value-warning">
Invalid option &quot;{variable.seriesType}&quot;
</span>
: null}

<label className="variable-values--group">
<label>Step by: </label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@
margin: 0 0.3em;
}
}
.invalid-value-warning {
display: block;
margin-left: 11.5em;
font-size: 0.75em;
color: $color-dangerous;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,36 @@ export default class Nav extends React.Component {
FocusUtil.focusOnNavTarget()
}

renderLabel(label) {
return label instanceof StyleableText ? label.value : label
renderLabel(label, sourceModel) {
return this.substituteLabelVariables((label instanceof StyleableText ? label.value : label), sourceModel)
}

renderLinkButton(label, ariaLabel, isDisabled, refId = null) {
substituteLabelVariables(label, sourceModel) {
// Quickly exit if the text has no double brackets, since we
// know there are no variables to substitue:
if (!sourceModel || label.indexOf('{{') === -1) {
return label
}

const varRegex = /\{\{(.+?)\}\}/g
const match = varRegex.exec(label)

if (match !== null) {
const event = { text: '', isNav: true }

// Ask the system if anything is aware of `variableText`:
Dispatcher.trigger('getTextForVariable', event, match[1], sourceModel)
if (event.text !== null) {
return event.text
}
}
return label
}

renderLinkButton(label, ariaLabel, isDisabled, sourceModel, refId = null) {
return (
<button ref={refId} aria-disabled={isDisabled} aria-label={ariaLabel}>
{this.renderLabel(label)}
{this.renderLabel(label, sourceModel)}
</button>
)
}
Expand Down Expand Up @@ -147,7 +169,7 @@ export default class Nav extends React.Component {

return (
<li key={index} onClick={this.onClick.bind(this, item)} className={className}>
{this.renderLinkButton(item.label, ariaLabel, isItemDisabled, item.id)}
{this.renderLinkButton(item.label, ariaLabel, isItemDisabled, item.sourceModel, item.id)}
{item.flags.assessment ? (
<span className="assessment-info">{this.getAssessmentInfo(item)}</span>
) : null}
Expand Down Expand Up @@ -175,7 +197,7 @@ export default class Nav extends React.Component {

return (
<li key={index} onClick={this.onClick.bind(this, item)} className={className}>
{this.renderLinkButton(item.label, ariaLabel, isItemDisabled)}
{this.renderLinkButton(item.label, ariaLabel, isItemDisabled, item.sourceModel)}
{lockEl}
</li>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ class NavStore extends Store {
path: '',
showChildren: true,
showChildrenOnNavigation: true,
parent: null
parent: null,
sourceModel: model
},
navItem
)
Expand Down

0 comments on commit 1a7b13a

Please sign in to comment.