MCPcopy Create free account
hub / github.com/cryptii/cryptii / applyModel

Method applyModel

src/Encoder/Enigma.js:513–572  ·  view source on GitHub ↗

* Updates the settings interface to reflect the given model. * @protected * @param {string} modelName Model name * @return {EnigmaEncoder} Fluent interface

(modelName)

Source from the content-addressed store, hash-verified

511 * @return {EnigmaEncoder} Fluent interface
512 */
513 applyModel (modelName) {
514 const model = EnigmaEncoder.getModel(modelName)
515 const maxSlotCount = EnigmaEncoder.getMaxSlotCount()
516
517 // Update setting options and layout for each slot
518 for (let i = 0; i < maxSlotCount; i++) {
519 const slot = i < model.slots.length ? model.slots[i] : null
520 const slotVisible = slot !== null
521
522 const rotorSetting = this.getSetting(`rotor${i + 1}`)
523 const ringSetting = this.getSetting(`ring${i + 1}`)
524 const positionSetting = this.getSetting(`position${i + 1}`)
525
526 // Hide or show slot settings depending on model
527 rotorSetting.setVisible(slotVisible)
528 ringSetting.setVisible(slotVisible)
529 positionSetting.setVisible(slotVisible)
530
531 if (slotVisible) {
532 // Configure rotor setting
533 const rotors = EnigmaEncoder.getRotors(slot.rotors)
534 rotorSetting.setElements(
535 rotors.map(rotor => rotor.name),
536 rotors.map(rotor => rotor.label),
537 null,
538 false)
539
540 // Apply slot default if current value is not available for this model
541 if (rotorSetting.validateValue(rotorSetting.getValue()) !== true) {
542 rotorSetting.setValue(rotors[0].name)
543 }
544 }
545 }
546
547 // Update reflector setting options
548 const reflectorRotors = EnigmaEncoder.getRotors(model.reflectorRotors)
549 const reflectorSetting = this.getSetting('reflector')
550
551 reflectorSetting.setElements(
552 reflectorRotors.map(rotor => rotor.name),
553 reflectorRotors.map(rotor => rotor.label),
554 null,
555 false)
556
557 // Apply first rotor if current one is not available for this model
558 if (reflectorSetting.validateValue(reflectorSetting.getValue()) !== true) {
559 reflectorSetting.setValue(reflectorRotors[0].name)
560 }
561
562 // Make reflector position and ring visible when it has a thumbwheel
563 reflectorSetting.setWidth(model.reflectorThumbwheel ? 4 : 12)
564 reflectorSetting.setVisible(
565 model.reflectorThumbwheel || reflectorRotors.length > 1)
566
567 this.getSetting('positionReflector').setVisible(model.reflectorThumbwheel)
568 this.getSetting('ringReflector').setVisible(model.reflectorThumbwheel)
569
570 // Only make plugboard visible when it is availabe for this model

Callers 2

constructorMethod · 0.95
settingValueDidChangeMethod · 0.95

Calls 10

getMaxSlotCountMethod · 0.80
getSettingMethod · 0.80
getRotorsMethod · 0.80
setElementsMethod · 0.80
setWidthMethod · 0.80
getModelMethod · 0.45
setVisibleMethod · 0.45
validateValueMethod · 0.45
getValueMethod · 0.45
setValueMethod · 0.45

Tested by

no test coverage detected