Support default value for state
Change-Id: I566a16753016dd2673ee1d298baca61a90175757
diff --git a/Changes b/Changes
index b01a274..c3356f3 100755
--- a/Changes
+++ b/Changes
@@ -1,4 +1,4 @@
-0.43 2021-11-04
+0.43 2021-11-05
- New menu class that has an entry at the very end,
similar to the input text prefix,
that is always available. (lerepp)
@@ -40,6 +40,7 @@
- Update intro.js (#109; hebasta)
- Introduce pagination panel.
- Support for inactive buttongroup items.
+ - Support default values for state.
0.42 2021-06-18
- Added GitHub based CI for perl.
diff --git a/dev/js/runner/stateSpec.html b/dev/js/runner/stateSpec.html
new file mode 100644
index 0000000..1003e12
--- /dev/null
+++ b/dev/js/runner/stateSpec.html
@@ -0,0 +1,41 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8">
+ <title>Spec Runner for Guided Tour</title>
+ <link rel="shortcut icon" type="image/png" href="../lib/jasmine-2.1.1/jasmine_favicon.png">
+ <link rel="stylesheet" href="../lib/jasmine-2.1.1/jasmine.css">
+ <script src="../lib/require.js"></script>
+ <script>
+ require.config({
+ baseUrl: "../src",
+ paths: {
+ "lib" : "../lib",
+ "spec" : "../spec",
+ "jlib" : "lib/jasmine-2.1.1",
+ "jasmine" : ['../lib/jasmine-2.1.1/jasmine'],
+ "jasmine-html": ['../lib/jasmine-2.1.1/jasmine-html'],
+ "jasmine-boot": ['../lib/jasmine-2.1.1/boot']
+ },
+ shim : {
+ 'jasmine-html' : {
+ deps : ['jasmine']
+ },
+ 'jasmine-boot' : {
+ deps : ['jasmine','jasmine-html']
+ }
+ }
+ });
+ require(['jasmine-boot'], function () {
+ require([
+ 'spec/stateSpec'
+ ],
+ function () {
+ window.onload();
+ });
+ });
+ </script>
+</head>
+<body>
+</body>
+</html>
diff --git a/dev/js/spec/stateSpec.js b/dev/js/spec/stateSpec.js
index fec337c..09ab274 100644
--- a/dev/js/spec/stateSpec.js
+++ b/dev/js/spec/stateSpec.js
@@ -16,6 +16,20 @@
expect(s.get()).toBeTruthy();
});
+ it('should accept a default value', function () {
+ let s = stateClass.create([true, false]);
+ expect(s.get()).toBeTruthy();
+ s.set(false);
+ expect(s.get()).toBeFalsy();
+
+ s = stateClass.create([true, false]);
+ s.setIfNotYet(false);
+ expect(s.get()).toBeFalsy();
+
+ s.setIfNotYet(true);
+ expect(s.get()).toBeFalsy();
+ });
+
it('should be associatable', function () {
let s = stateClass.create();
diff --git a/dev/js/src/state.js b/dev/js/src/state.js
index 7d0d25e..f68408f 100644
--- a/dev/js/src/state.js
+++ b/dev/js/src/state.js
@@ -20,8 +20,8 @@
/**
* Constructor
*/
- create : function (value) {
- return Object.create(this)._init(value);
+ create : function (values) {
+ return Object.create(this)._init(values);
},
@@ -38,7 +38,6 @@
else {
t.values = [values];
}
- t.value = t.values[0];
return t;
},
@@ -71,9 +70,24 @@
/**
+ * Set the state to a default value.
+ * This will only be set, if no other value is set yet.
+ */
+ setIfNotYet : function (value) {
+ if (this.value == undefined) {
+ this.set(value);
+ };
+ },
+
+
+ /**
* Get the state value
*/
get : function () {
+ if (this.value == undefined) {
+ this.value = this.values[0];
+ };
+
return this.value;
},
@@ -101,7 +115,7 @@
roll : function () {
let next = 0;
for (let i = 0; i < this.values.length - 1; i++) {
- if (this.value == this.values[i]) {
+ if (this.get() == this.values[i]) {
next = i+1;
break;
};