JSFiddle - React, Tailwind, and code Playground
HTML
<!--
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
-->
<div id="representation">
<blockquote>Problem is (follow me closely here, the science is pretty complicated), if I cut a hole in the Hab, the air won't stay inside anymore.</blockquote><br/>
- Andy Weir, The Martian
</div>
<script type="text/html" id="ready_template">
<label>
Count From:
<input data-bind="value: countFrom"/>
<button data-bind="click: setCountFrom">Set</button>
</label>
<p>Counter:<span data-bind="text:model.counter"></span></p>
<button data-bind="click: start">Start</button>
</script>
<script type="text/html" id="counting_template">
<p>Count down:<span data-bind="text:model.counter"></span></p>
<button data-bind="click: abort">Abort</button>
</script>
<script type="text/html" id="aborted_template">
<p>Aborted at...
JavaScript
////////////////////////////////////////////////////////////////////////////////
// Model
//
var model = {
counter: 10,
started: false,
launched: false,
aborted: false} ;
model.present = function(data) {
if (state.counting(model)) {
if (model.counter === 0) {
model.launched = data.launched || false ;
} else {
model.aborted = data.aborted || false ;
if (data.counter !== undefined) { model.counter = data.counter ; }
}
} else {
if (state.ready(model)) {
model.started = data.started || false ;
if(!isNaN(data.counter)) model.counter = data.counter;
}
}
state.render(model) ;
}
////////////////////////////////////////////////////////////////////////////////
// State
//
var state = { } ;
model.state = state ;
//display the state representation
state.display = function(representation) {
var stateRepresentation = document.getElementById("representation");
//clean up representation
while (stateRepresentation.hasChildNodes()) {
stateRepresentation.removeChild(stateRepresentation.lastChild);
}
//rebuild representation
var representationDiv = document.createElement('div');
stateRepresentation.appendChild(representationDiv);
representationDiv.innerHTML = representation;
//apply view model to representation (knockout.js)
var viewModel = state.viewModel(model);
ko.applyBindings(viewModel, representationDiv);
}
state.viewModel = function(model) {
//prepare view model
var viewModel = { model: model };
viewModel.countFrom = ko.observable(model.counter);
viewModel.setCountFrom = function() {
actions.settings({ counter: parseInt(viewModel.countFrom()) });
};
viewModel.abort = function () {
actions.abort({});
};
viewModel.start = function () {
actions.start({});
};
return viewModel;
}
// Derive the state representation as a function of the systen
// control state
state.representation = function(model) {
var representation = 'oops... something went wrong, the...