Source: view/viewScreens/ViewEntitiesSample.js

  1. /** @module */
  2. import CandidateDnViewList from '../vizCandidateDns/CandidateDnViewList.js'
  3. import VoterViewList from '../vizVoters/VoterViewList.js'
  4. import ViewBase from './ViewBase.js'
  5. /**
  6. * Simulate many sample elections with
  7. * candidates in random positions within a distribution, and
  8. * voters in a distribution that will be summed over.
  9. * @param {Screen} screen
  10. * @param {Menu} menu
  11. * @param {Changes} changes
  12. * @param {ViewSettings} viewSettings
  13. * @constructor
  14. */
  15. export default function ViewEntitiesSample(entities, screen, menu, changes, simOptions, electionOptionsMan, viewMode, viewSettings) {
  16. const self = this
  17. viewMode.viewModes.sample.attach(self)
  18. ViewBase.call(self, screen, changes, viewSettings)
  19. // Entities //
  20. const { candidateDnList, voterShapeList } = entities
  21. // eslint-disable-next-line max-len
  22. const candidateDnViewList = new CandidateDnViewList(viewSettings, candidateDnList, screen, simOptions, electionOptionsMan)
  23. const voterViewList = new VoterViewList(viewSettings, voterShapeList, screen, simOptions, electionOptionsMan)
  24. candidateDnViewList.attachNewG(self.dragm)
  25. voterViewList.attachNewG(self.dragm)
  26. // Main State Machine Functions //
  27. self.update = (simData) => {
  28. const { samplingResult } = simData
  29. // Update players. Run an election. Get result. Visualize result.
  30. // The election handles any changes.
  31. // The samplingResult communicates how to visualize the election.
  32. if (changes.check(['draggables', 'dimensions', 'mode'])) {
  33. voterViewList.updateViewXY()
  34. candidateDnViewList.updateViewXY()
  35. }
  36. const { pointsChanged, partyWinFraction } = samplingResult
  37. if (pointsChanged) {
  38. candidateDnViewList.setCandidateDnWins(partyWinFraction)
  39. }
  40. self.clearForeground()
  41. self.renderForeground()
  42. }
  43. self.clickEmpty = () => { }
  44. self.testVoteView = () => null
  45. self.renderForeground = () => {
  46. voterViewList.renderForeground()
  47. candidateDnViewList.renderForeground()
  48. }
  49. self.clearForeground = () => {
  50. screen.clearForeground()
  51. }
  52. }