forked from adaptlearning/adapt-contrib-resources
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
222 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import Adapt from 'core/js/adapt'; | ||
import a11y from 'core/js/a11y'; | ||
import { classes, templates } from 'core/js/reactHelpers'; | ||
|
||
export default function Resources (props) { | ||
const { | ||
resources | ||
} = props; | ||
|
||
const _globals = Adapt.course.get('_globals'); | ||
const resourceTypes = ['all', 'document', 'media', 'link']; // must contain 'all' | ||
|
||
function resourcesHasMultipleTypes(resources) { | ||
if (resources.length === 1) return false; | ||
|
||
const allSameType = resources.every(_.matcher({ _type: resources[0]._type })); | ||
return !allSameType; | ||
} | ||
|
||
function resourcesGetColumnCount(resources) { | ||
return _.uniq(_.pluck(resources, '_type')).length + 1; // add 1 for the 'All' button column | ||
} | ||
|
||
const [selectedFilter, setSelectedFilter] = useState('all'); | ||
const [selectedId, setSelectedId] = useState('resources__show-all'); | ||
const [focusFlag, setFocusFlag] = useState(false); | ||
|
||
useEffect(() => { | ||
if (focusFlag) { | ||
let $items; | ||
if (selectedFilter === 'all') { | ||
$items = $('.resources__item'); | ||
} else { | ||
$items = $('.resources__item.is-' + selectedFilter); | ||
} | ||
|
||
if ($items.length < 0) return; | ||
|
||
a11y.focusFirst($items); | ||
|
||
setFocusFlag(false); | ||
} | ||
}, [focusFlag]); | ||
|
||
const onFilterClicked = e => { | ||
if (e && e.preventDefault) e.preventDefault(); | ||
|
||
const $clickedButton = this.$(e.currentTarget); | ||
const filter = $clickedButton.data('filter'); | ||
const id = $clickedButton.attr('id'); | ||
|
||
setSelectedFilter(filter); | ||
setSelectedId(id); | ||
setFocusFlag(true); | ||
}; | ||
|
||
return ( | ||
<div className="component__inner resources__inner"> | ||
|
||
<templates.header {...props} /> | ||
|
||
{resourcesHasMultipleTypes(resources) && | ||
<div className={classes([ | ||
'resources__filter', | ||
`has-${resourcesGetColumnCount(resources)}-columns` | ||
])}> | ||
<div className="resources__filter-inner" role="tablist"> | ||
|
||
<div className="aria-label" aria-label={_globals._extensions._resources.resources} /> | ||
|
||
{resourceTypes.map((type, index) => | ||
<templates.resourcesFilterButton {...props} | ||
key={index} | ||
resources={resources} | ||
_filter={type} | ||
onClick={onFilterClicked} | ||
selected={selectedFilter} /> | ||
)} | ||
|
||
</div> | ||
</div> | ||
} | ||
|
||
<div id="resources" className="resources__item-container" role="tabpanel" aria-labelledby={selectedId}> | ||
|
||
<div role="list"> | ||
|
||
{resources.map(({ title, description, _link, _type, _isGlobal, filename, _forceDownload }, index) => | ||
<templates.resourcesItem {...props} | ||
key={index} | ||
title={title} | ||
description={description} | ||
_link={_link} | ||
_type={_type} | ||
_isGlobal={_isGlobal} | ||
selectedFilter={selectedFilter} | ||
filename={ filename} | ||
_forceDownload={_forceDownload} /> | ||
)}; | ||
|
||
</div> | ||
|
||
</div> | ||
|
||
</div> | ||
|
||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React from 'react'; | ||
import { classes } from 'core/js/reactHelpers'; | ||
|
||
export default function ResourcesFilterButton (props) { | ||
const { | ||
model, | ||
onClick, | ||
resources, | ||
selected, | ||
_filter | ||
} = props; | ||
|
||
const buttonText = model._filterButtons[_filter]; | ||
const ariaLabel = model._filterAria[`${_filter}Aria`]; | ||
|
||
function resourcesHasType(resources, type) { | ||
return resources.some(_.matcher({ _type: type })); | ||
} | ||
|
||
if (!resourcesHasType(resources, _filter) && _filter !== 'all') return null; | ||
|
||
return ( | ||
<button | ||
id={`resources__show-${_filter}`} | ||
className={classes([ | ||
'resources__filter-btn', | ||
selected === _filter && 'is-selected' | ||
])} | ||
onClick={onClick} | ||
data-filter={_filter} | ||
aria-label={ariaLabel} | ||
role="tab" | ||
aria-selected={selected === _filter} | ||
aria-controls="resources" | ||
dangerouslySetInnerHTML={{ __html: buttonText }} /> | ||
); | ||
} |
Oops, something went wrong.