From fc3f9feeddd755d357fa76a28ce5d4a327e0c2e1 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Fri, 19 Feb 2021 09:56:03 -0500 Subject: Get nucleotide selection working again. --- genome.mjs | 32 +++++++++++++++++++++++--------- rules.mjs | 4 ++-- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/genome.mjs b/genome.mjs index 581e9a8..7303b40 100644 --- a/genome.mjs +++ b/genome.mjs @@ -5,8 +5,6 @@ import { randomItem } from './utils.mjs' class Genome { constructor(nucleotides) { const codonList = document.createElement('ol') - this._boundNucleotideClickedHandler = - this.nucleotideClickedHandler.bind(this) this.codons = [] let tmpCodon = [] @@ -20,7 +18,15 @@ class Genome { } }) this.elt.appendChild(codonList) + this.lock() + this._boundNucleotideClickedHandler = + this.nucleotideClickedHandler.bind(this) + this.codons.forEach(c => { + c.bases.forEach(b => { + b.onClick = this._boundNucleotideClickedHandler + }) + }) } get elt() { @@ -31,15 +37,15 @@ class Genome { return this._elt } - get onSelectionChanged() { - if (this._onSelectionChanged !== undefined) { - return this._onSelectionChanged + get onNucleotideSelectionChanged() { + if (this._onNucleotideSelectionChanged !== undefined) { + return this._onNucleotideSelectionChanged } return () => {} } - set onSelectionChanged(fn) { - this._onSelectionChanged = fn + set onNucleotideSelectionChanged(fn) { + this._onNucleotideSelectionChanged = fn } lock() { @@ -67,8 +73,16 @@ class Genome { this._selectedNucleotide = nucleotide this._selectedNucleotide.select() - const i = this.nucleotides.indexOf(this._selectedNucleotide) - this.onSelectionChanged(this._selectedNucleotide, i) + let i = 0 + for (const c of this.codons) { + for (const b of c.bases) { + if (b === this._selectedNucleotide) { + this.onNucleotideSelectionChanged(this._selectedNucleotide, i) + return + } + i++ + } + } } nucleotideClickedHandler(nucleotide) { diff --git a/rules.mjs b/rules.mjs index f726a5a..8f038bc 100644 --- a/rules.mjs +++ b/rules.mjs @@ -77,14 +77,14 @@ class NucleotideSelect { this.rules.instructions.querySelector('#select-number').innerHTML = `${this.want}${ordinalSuffix(this.want)}` - this.rules.currentGenome.onSelectionChanged = + this.rules.currentGenome.onNucleotideSelectionChanged = this.handleSelectionChanged.bind(this) this.rules.currentGenome.unlock() } exit() { this.rules.currentGenome.lock() - this.rules.currentGenome.onSelectionChanged = undefined; + this.rules.currentGenome.onNucleotideSelectionChanged = undefined; } handleSelectionChanged(nucleotide, i) { -- cgit v1.2.3