summaryrefslogtreecommitdiffstats
path: root/genome.mjs
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2021-01-03 18:34:19 -0500
committerBrian Cully <bjc@kublai.com>2021-02-06 10:39:32 -0500
commite92ad9f4b19a0670a80cdd293970c3a08c27a8b4 (patch)
treee8659dcdbf5f7ba3c55a118909d82dd8f0d0bcbd /genome.mjs
downloadmolsim-e92ad9f4b19a0670a80cdd293970c3a08c27a8b4.tar.gz
molsim-e92ad9f4b19a0670a80cdd293970c3a08c27a8b4.zip
Initial commit.
Diffstat (limited to 'genome.mjs')
-rw-r--r--genome.mjs80
1 files changed, 80 insertions, 0 deletions
diff --git a/genome.mjs b/genome.mjs
new file mode 100644
index 0000000..fcab4ff
--- /dev/null
+++ b/genome.mjs
@@ -0,0 +1,80 @@
+import Nucleotide from './nucleotide.mjs'
+import Die from './die.mjs'
+import { randomItem } from './utils.mjs'
+
+class Genome {
+ static *randomBase() {
+ for (const i of [...Array(Genome.size)]) {
+ yield randomItem(Nucleotide.bases)
+ }
+ }
+
+ constructor(gen) {
+ const nucleotideList = document.createElement('ol')
+ this._boundNucleotideClickedHandler =
+ this.nucleotideClickedHandler.bind(this)
+ this.nucleotides = [...gen].map(base => {
+ const n = new Nucleotide(base)
+ n.onClick = this._boundNucleotideClickedHandler
+ nucleotideList.appendChild(n.elt)
+ return n
+ })
+ this.elt.appendChild(nucleotideList)
+ this.lock()
+ }
+
+ get elt() {
+ if (this._elt === undefined) {
+ this._elt = document.createElement('li')
+ this._elt.classList.add('genome')
+ }
+ return this._elt
+ }
+
+ get onSelectionChanged() {
+ if (this._onSelectionChanged !== undefined) {
+ return this._onSelectionChanged
+ }
+ return () => {}
+ }
+
+ set onSelectionChanged(fn) {
+ this._onSelectionChanged = fn
+ }
+
+ lock() {
+ this.elt.classList.add('locked')
+ this.nucleotides.forEach(n => n.lock())
+ }
+
+ unlock() {
+ this.elt.classList.remove('locked')
+ this.nucleotides.forEach(n => n.unlock())
+ }
+
+ clone() {
+ return new Genome(this.nucleotides.map(n => n.value))
+ }
+
+ get selectedNucleotide() {
+ return this._selectedNucleotide
+ }
+
+ set selectedNucleotide(nucleotide) {
+ if (this.selectedNucleotide !== undefined) {
+ this.selectedNucleotide.deselect()
+ }
+ this._selectedNucleotide = nucleotide
+ this._selectedNucleotide.select()
+
+ const i = this.nucleotides.indexOf(this._selectedNucleotide)
+ this.onSelectionChanged(this._selectedNucleotide, i)
+ }
+
+ nucleotideClickedHandler(nucleotide) {
+ this.selectedNucleotide = nucleotide
+ }
+}
+Genome.size = 18
+
+export default Genome