|
| 1 | +// Stuff to access what chunks are collapsed, so animation can depend on |
| 2 | +// that (eg show less/more information). There was an accumulation |
| 3 | +// of different versions of this for different algorithms. This is a |
| 4 | +// more generic one that requires some extra some code in algorithm files. |
| 5 | +// Possibly should be put elsewhere, eg, src/context/ |
| 6 | +// NOTE: any algorithm controller file that imports this must be listed |
| 7 | +// below, otherwise hooks into what happens when blocks are expanded or |
| 8 | +// contracted are not enabled. |
| 9 | +const importsThis = ['quickSort', 'quickSortM3', 'msort_arr_td', 'transitiveClosure']; |
| 10 | + |
| 11 | +// eslint-disable-next-line import/no-cycle |
| 12 | +// See also accompanying mods/hooks in src/context/GlobalState.js and |
| 13 | +// src/context/actions.js |
| 14 | +import { GlobalActions } from '../../context/actions'; |
| 15 | + |
| 16 | +let algorithmGetter = () => null; |
| 17 | + |
| 18 | +function getGlobalAlgorithm() { |
| 19 | + return algorithmGetter(); |
| 20 | +} |
| 21 | + |
| 22 | +window.getGlobalAlgorithm = getGlobalAlgorithm; |
| 23 | +export function initGlobalAlgorithmGetter(getter) { |
| 24 | + algorithmGetter = getter; |
| 25 | +} |
| 26 | + |
| 27 | +// Checks if list of pseudocode blocks are all currently expanded. Note |
| 28 | +// that "inner" blocks can be expanded even when "outer" ones are not, eg, |
| 29 | +// for msort_arr_td has block Merge inside MergeCopy. To check if Merge |
| 30 | +// code is visible (so certain variables should be displayed), we should |
| 31 | +// check both Merge and MergeCopy are expanded. Hopefully, using a list |
| 32 | +// of blocks in this interface will help remind programmers to include the |
| 33 | +// block we are interested in *plus enclosing blocks* (Main is always |
| 34 | +// expanded so that is not needed). |
| 35 | +export function areExpanded(Blocks) { |
| 36 | + const algorithm = getGlobalAlgorithm(); |
| 37 | + const alg_name = algorithm.id.name; |
| 38 | + const { bookmark, pseudocode, collapse } = algorithm; |
| 39 | + return Blocks.reduce((acc, curr) => |
| 40 | + (acc && collapse[alg_name.sort][curr]), true); |
| 41 | +} |
| 42 | + |
| 43 | +// Trigger refresh of display when code is expanded/collapsed. |
| 44 | +// Not so efficient - runs through all the chunks from the start. XXX |
| 45 | +// However, it seems to work fine and is pretty general. We could |
| 46 | +// possibly add a shortcut for algorithms that never change display |
| 47 | +// depending on what blocks are collapsed. XXX |
| 48 | +export function onCollapseChange(chunker) { |
| 49 | + // const algorithm = getGlobalAlgorithm(); |
| 50 | + chunker.refresh(); |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +///////////////////////////////////////////////////// |
| 55 | + |
| 56 | +export function isMergeCopyExpanded() { |
| 57 | + return areExpanded(['MergeCopy']); |
| 58 | +} |
| 59 | + |
| 60 | +export function isMergeExpanded() { |
| 61 | + return areExpanded(['MergeCopy', 'Merge']); |
| 62 | +} |
| 63 | + |
| 64 | +// checks if either recursive call is expanded (needed to determine if i |
| 65 | +// should be displayed) |
| 66 | +export function isRecursionExpanded() { |
| 67 | + return areExpanded(['MergesortL']) || areExpanded(['MergesortR']); |
| 68 | +} |
0 commit comments