Source: sim/geometry/Districts.js

  1. /** @module */
  2. import makeGeography, { makeTracts, updateCensus, updateDistricts, updateVotersByDistrict, updateVotersByTract } from '@paretoman/votekit-make-geography'
  3. /**
  4. * @constructor
  5. */
  6. export default function Districts(voterShapeList, changes, electionOptionsMan, simOptions) {
  7. const self = this
  8. self.init = () => {
  9. const optionsBag = electionOptionsMan.getOptions()
  10. const { numTracts, numDistricts } = optionsBag
  11. const { dimensions } = simOptions
  12. const voterGeoms = voterShapeList.getGeoms(dimensions)
  13. self.geography = makeGeography(numTracts, numDistricts, voterGeoms, dimensions)
  14. }
  15. let firstRun = true
  16. // Update call from sim //
  17. self.update = () => {
  18. if (changes.checkNone()) return
  19. if (firstRun) {
  20. firstRun = false
  21. self.init()
  22. return
  23. }
  24. const optionsBag = electionOptionsMan.getOptions()
  25. if (changes.check(['numTracts'])) {
  26. const { numTracts } = optionsBag
  27. self.geography = makeTracts(self.geography, numTracts)
  28. }
  29. if (changes.check(['numDistricts'])) {
  30. const { numDistricts } = optionsBag
  31. self.geography = updateDistricts(self.geography, numDistricts)
  32. }
  33. if (changes.check(['numDistricts', 'numTracts'])) {
  34. self.geography = updateCensus(self.geography)
  35. }
  36. if (changes.check(['voters', 'dimensions', 'numTracts'])) {
  37. const { dimensions } = simOptions
  38. const voterGeoms = voterShapeList.getGeoms(dimensions)
  39. self.geography = updateVotersByTract(self.geography, voterGeoms, dimensions)
  40. // todo: maybe make this only trigger when voters change
  41. }
  42. if (changes.check(['voters', 'dimensions', 'numTracts', 'numDistricts'])) {
  43. self.geography = updateVotersByDistrict(self.geography)
  44. // todo: maybe make this only trigger when voters change
  45. }
  46. }
  47. }