Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <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>
<div>
  Credit: Jean-Jacques Dubray
</div>
<div>
  Reference: <a href="https://bitbucket.org/snippets/jdubray/9dgKp/sam-sample">https://bitbucket.org/snippets/jdubray/9dgKp/sam-sample</a>
</div>
            
          
!

CSS

            
              
            
          
!

JS

            
              ////////////////////////////////////////////////////////////////////////////////
// Model 
//
const COUNTER_MAX = 10 ;

var model = {
		counter: COUNTER_MAX, 
		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 ;
		}
	}
	state.render(model) ;
}


////////////////////////////////////////////////////////////////////////////////
// View
//
var view = {} ;

// Initial State
view.init = function(model) {
	return view.ready(model) ;
}

// State representation of the ready state
view.ready = function(model) {
	return (
			"<p>Counter:"+model.counter+"</p>\n\
			<form onSubmit=\"JavaScript:actions.start({});return false;\">\n\
				<input type=\"submit\" value=\"Start\">\n\
			</form>"
		) ;

}

// State representation of the counting state
view.counting = function(model) {

	return (
			"<p>Count down:"+model.counter+"</p>\n\
			 <form onSubmit=\"JavaScript:actions.abort({});return false;\">\n\
				<input type=\"submit\" value=\"Abort\">\n\
			</form>"
		) ;

}

// State representation of the aborted state
view.aborted = function(model) {

	return (
			"<p>Aborted at Counter:"+model.counter+"</p>\n"
		) ;

}

// State representation of the launched state
view.launched = function(model) {

	return (
			"<p>Launched</p>"
		) ;

}

//display the state representation
view.display = function(representation) {
	var stateRepresentation = document.getElementById("representation");
	stateRepresentation.innerHTML = representation;
}

// Display initial state
view.display(view.init(model)) ;



////////////////////////////////////////////////////////////////////////////////
// State
//
var state =  { view: view} ;

model.state = state ;

// Derive the state representation as a function of the systen
// control state
state.representation = function(model) {
	var representation = 'oops... something went wrong, the system is in an invalid state' ;

	if (state.ready(model)) {
		representation = state.view.ready(model) ;
	} 

	if (state.counting(model)) {
		representation = state.view.counting(model) ;
	}

	if (state.launched(model)) {
		representation = state.view.launched(model) ;
	}

	if (state.aborted(model)) {
		representation = state.view.aborted(model) ;
	}

	state.view.display(representation) ;
}

// Derive the current state of the system
state.ready = function(model) {
	return ((model.counter === COUNTER_MAX) && !model.started && !model.launched && !model.aborted) ;
}

state.counting = function(model) {
	var status = ((model.counter <= COUNTER_MAX) && (model.counter >= 0) && model.started && !model.launched && !model.aborted) ;
	return status ;
}

state.launched = function(model) {
	return ((model.counter == 0) && model.started && model.launched && !model.aborted) ;
}

state.aborted = function(model) {
	return (
		(  model.counter <= COUNTER_MAX) && (model.counter >= 0) 
		&& model.started && !model.launched && model.aborted ) ;
}


// state.getView = function(model) {

// 	if (state.ready(model)) {
// 		return view.ready(model) ;
// 	}

// 	if (state.counting(model)) {
// 		return view.counting(model) ;
// 	}

// 	if (state.launched(model)) {
// 		return view.launched(model) ;
// 	}

// 	if (state.counting(model)) {
// 		return view.counting(model) ;
// 	}

// }


// Next action predicate, derives whether
// the system is in a (control) state where
// an action needs to be invoked

state.nextAction = function (model) {
	if (state.counting(model)) {
		if (model.counter>0) {
			actions.decrement({counter: model.counter},model.present) ;
		}

		if (model.counter === 0) {
			actions.launch({},model.present) ;
		}
	}
}

state.render = function(model) {
	state.representation(model)
	state.nextAction(model) ;
}


////////////////////////////////////////////////////////////////////////////////
// Actions
//

var actions = {} ;

actions.start = function(data, present) {
	present = present || model.present ;
	data.started = true ;
	present(data) ;
}

var tick = function(data, present) {

	var d = data ;
	var p = present ;
	return function() {
		d.counter = d.counter -1 ;
		p(d) ;
	}
}

actions.decrement = function(data, present) {
	present = present || model.present ;
	data = data || {} ;
	data.counter = data.counter || 10 ;
	var d = data ;
	var p = present ;
	setTimeout(function() {
		d.counter = d.counter - 1 ;
		p(d) ;
	}, 1000) ;
}

actions.launch = function(data, present) {
	present = present || model.present ;
	data.launched = true ;
	present(data) ;
}

actions.abort = function(data, present) {
	present = present || model.present ;
	data.aborted = true ;
	present(data) ;
}
            
          
!
999px

Console