Add role() method to state object
Change-Id: Ie98f15c83d94c6cb1915942bba6c2ba17ae321e0
diff --git a/dev/js/src/state.js b/dev/js/src/state.js
index a5ab7d8..69e2d54 100644
--- a/dev/js/src/state.js
+++ b/dev/js/src/state.js
@@ -8,9 +8,9 @@
*/
/*
* TODO:
- * Add a "roll" parameter, like "roll":["left","right","center"]
- * and a roll() method, that will switch through the states in the list
- * for flexible toggling.
+ * Require names for states, that should be quite short, so they
+ * can easily be serialized and kept between page turns (via cookie
+ * and/or query param)
*/
define(function () {
@@ -26,13 +26,21 @@
},
// Initialize
- _init : function (value) {
+ _init : function (values) {
this._assoc = [];
- this.value = value;
+ if (values == undefined) {
+ this.values = [false,true];
+ }
+ else if (Array.isArray(values)) {
+ this.values = values;
+ }
+ else {
+ this.values = [values];
+ }
+ this.value = this.values[0];
return this;
},
-
/**
* Associate the state with some objects.
*/
@@ -78,6 +86,21 @@
*/
clear : function () {
return this._assoc = [];
+ },
+
+ /**
+ * Roll to the next value.
+ * This may be used for toggling.
+ */
+ roll : function () {
+ let next = 0;
+ for (let i = 0; i < this.values.length - 1; i++) {
+ if (this.value == this.values[i]) {
+ next = i+1;
+ break;
+ };
+ };
+ this.set(this.values[next]);
}
}
});