Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML

              
                
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Simple Switch Demo</title>
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

 <script src="https://assets.codepen.io/2527236/compress.js"></script> 

  <script>

    var app;

    $(document).ready(function() {
      app = TrafficLight();
      app.start(); 
    });

  </script>
</head>
<body>


  
<div class="flex_container">
 <table style="width:100%">
  <tr>
    <th>Demo</th>
    <th>UML Documentation</th>
    <th>Log</th>
    <th>Frame Spec <button id="full_spec_btn" class="btn btn-primary" data-toggle="modal" data-target="#full_spec_modal">View Full Spec</button></th>
    <th>JavaScript</th>
  </tr>
  <tr>
  <td>
      <div id="greeting">
  <button onClick="app.systemError()">System Error</button>
  <button onClick="app.systemRestart()">System Restart</button>
</div>
      
      <img id='light_img'  class="elem" src="https://assets.codepen.io/2527236/GreenLight.png" />
    </td>
        <td>
      <img id="uml_target" class="elem" >
    </td>
    <td>
      <textarea id='output' cols=30 rows=10>         </textarea>
    </td>
    <td>
      <pre id="frame_code"></pre>
    </td>
    <td>
       <pre id="js_code"></pre>   
    </td>


  </tr>
</table>
 </div>
  
    
  <!-- Modal -->
<div class="modal fade" id="full_spec_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">The Full Monte</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <pre>
--- try this code at http://framepiler.frame-lang.org/

#TrafficLightSpec

    -interface-

    start @(|>>|)
    timer
    systemError
    systemRestart

    -machine-

    $Begin
        |>>|
            startWorkingTimer()
            -> $Red ^

    $Red => $Working
        |>|
            enterRed() ^
        |timer|
            -> $Green ^

    $Green => $Working
        |>|
            enterGreen() ^
        |timer|
            -> $Yellow ^

    $Yellow => $Working
        |>|
            enterYellow() ^
        |timer|
            -> $Red ^

    $FlashingRed
        |>|
            enterFlashingRed()
            stopWorkingTimer()
            startFlashingTimer() ^
        |<|
            exitFlashingRed()
            stopFlashingTimer()
            startWorkingTimer() ^
        |timer|
            changeFlashingAnimation() ^
        |systemRestart|
            -> $Red  ^

    $Working
        |systemError|
            -> $FlashingRed ^

    -actions-

    enterRed
    enterGreen
    enterYellow
    enterFlashingRed
    exitFlashingRed
    startWorkingTimer
    stopWorkingTimer
    startFlashingTimer
    stopFlashingTimer
    changeFlashingAnimation
    log [msg]

    -domain-

    var timer = null
    var flashColor = null
##
        
        </pre>
      </div>
    </div>
  </div>
 
    
  
  
</body>
</html>


              
            
!

CSS

              
                .flex_container {
  display: flex;
  height: 400px; /* Or whatever */
}

.elem {
  height:400px;
}

table {
  margin-top: 10px; 
}

table, th, td {
  border: 1px solid lightgrey;
  border-collapse: collapse;
  text-align: center !important;
}

td {
  padding-top: 10px;
  text-align: center
}

#frame_code, #js_code {
  width: 325px;
  margin: 0px;
  padding: 0px;
  text-align: left
}

#output {
  width: 150px;
}

tbody {
    vertical-align: top
}
              
            
!

JS

              
                 //-------------------------------------------------//
 //                                                 //
 //                 Implementation                  //
 //                                                 //
 //-------------------------------------------------//


 var FrameEvent = function(message, parameters) {

    var that = {};

    that._message = message;
    that._parameters = parameters;
    that._return = null;

    return that;
};

/*************************************************************

--- try on https://frame-lang.org

#TrafficLightSpec

    -interface-

    start @(|>>|)
    timer
    systemError
    systemRestart

    -machine-

    $Begin
        |>>|
            startWorkingTimer()
            -> $Red ^

    $Red => $Working
        |>|
            enterRed() ^
        |timer|
            -> $Green ^

    $Green => $Working
        |>|
            enterGreen() ^
        |timer|
            -> $Yellow ^

    $Yellow => $Working
        |>|
            enterYellow() ^
        |timer|
            -> $Red ^

    $FlashingRed
        |>|
            enterFlashingRed()
            stopWorkingTimer()
            startFlashingTimer() ^
        |<|
            exitFlashingRed()
            stopFlashingTimer()
            startWorkingTimer() ^
        |timer|
            changeFlashingAnimation() ^
        |systemRestart|
            -> $Red  ^

    $Working
        |systemError|
            -> $FlashingRed ^

    -actions-

    enterRed
    enterGreen
    enterYellow
    enterFlashingRed
    exitFlashingRed
    startWorkingTimer
    stopWorkingTimer
    startFlashingTimer
    stopFlashingTimer
    changeColor [color]
    startFlashing
    stopFlashing
    changeFlashingAnimation
    log [msg]

    -domain-

    var timer = null
    var flashColor = null
##


***************************************************************/

// emitted from framec_v0.3.36
let TrafficLightSpec = function () {
    
    let that = {};
    that.constructor = TrafficLightSpec;
    
    //===================== Interface Block ===================//
    
    that.start = function () {
        let e = FrameEvent(">>",null);
        _state_(e);
    }
    
    that.timer = function () {
        let e = FrameEvent("timer",null);
        _state_(e);
    }
    
    that.systemError = function () {
        let e = FrameEvent("systemError",null);
        _state_(e);
    }
    
    that.systemRestart = function () {
        let e = FrameEvent("systemRestart",null);
        _state_(e);
    }
    
    //===================== Machine Block ===================//
    
    let _sBegin_ = function (e) {
        if (e._message == ">>") {
            that.startWorkingTimer_do();
            _transition_(_sRed_);
            return;
        }
    }
    
    let _sRed_ = function (e) {
        if (e._message == ">") {
            that.enterRed_do();
            return;
        }
        else if (e._message == "timer") {
            _transition_(_sGreen_);
            return;
        }
        _sWorking_(e);
        
    }
    
    let _sGreen_ = function (e) {
        if (e._message == ">") {
            that.enterGreen_do();
            return;
        }
        else if (e._message == "timer") {
            _transition_(_sYellow_);
            return;
        }
        _sWorking_(e);
        
    }
    
    let _sYellow_ = function (e) {
        if (e._message == ">") {
            that.enterYellow_do();
            return;
        }
        else if (e._message == "timer") {
            _transition_(_sRed_);
            return;
        }
        _sWorking_(e);
        
    }
    
    let _sFlashingRed_ = function (e) {
        if (e._message == ">") {
            that.enterFlashingRed_do();
            that.stopWorkingTimer_do();
            that.startFlashingTimer_do();
            return;
        }
        else if (e._message == "<") {
            that.exitFlashingRed_do();
            that.stopFlashingTimer_do();
            that.startWorkingTimer_do();
            return;
        }
        else if (e._message == "timer") {
            that.changeFlashingAnimation_do();
            return;
        }
        else if (e._message == "systemRestart") {
            _transition_(_sRed_);
            return;
        }
    }
    
    let _sWorking_ = function (e) {
        if (e._message == "systemError") {
            _transition_(_sFlashingRed_);
            return;
        }
    }
    
    //===================== Actions Block ===================//
    
    that.enterRed_do = function () {}
    that.enterGreen_do = function () {}
    that.enterYellow_do = function () {}
    that.enterFlashingRed_do = function () {}
    that.exitFlashingRed_do = function () {}
    that.startWorkingTimer_do = function () {}
    that.stopWorkingTimer_do = function () {}
    that.startFlashingTimer_do = function () {}
    that.stopFlashingTimer_do = function () {}
    that.changeFlashingAnimation_do = function () {}
    that.log_do = function (msg) {}
    
    //===================== Domain Block ===================//
    
    let timer = null;
    let flashColor = null;
    
    //=============== Machinery and Mechanisms ==============//
    
    let _state_ = _sBegin_;
    
    let _transition_ = function(newState) {
        let exitEvent = FrameEvent("<",null);
        _state_(exitEvent);
        _state_ = newState;
        let enterEvent = FrameEvent(">",null);
        _state_(enterEvent);
    }
    
    return that; 
};

/********************
let TrafficLightSpec = function () {
	let that = TrafficLightSpec.call(this);
	that.enterRed_do = function () {}
	that.enterGreen_do = function () {}
	that.enterYellow_do = function () {}
	that.enterFlashingRed_do = function () {}
	that.exitFlashingRed_do = function () {}
	that.startWorkingTimer_do = function () {}
	that.stopWorkingTimer_do = function () {}
	that.startFlashingTimer_do = function () {}
	that.stopFlashingTimer_do = function () {}
	that.changeFlashingAnimation_do = function () {}
	that.log_do = function (msg) {}
	return that;
};
********************/


/********************
let TrafficLightSpec = function () {
	let that = TrafficLightSpec.call(this);
	that.enterRed_do = function () {}
	that.enterGreen_do = function () {}
	that.enterYellow_do = function () {}
	that.enterFlashingRed_do = function () {}
	that.exitFlashingRed_do = function () {}
	that.startWorkingTimer_do = function () {}
	that.stopWorkingTimer_do = function () {}
	that.startFlashingTimer_do = function () {}
	that.stopFlashingTimer_do = function () {}
	that.changeColor_do = function (color) {}
	that.startFlashing_do = function () {}
	that.stopFlashing_do = function () {}
	that.changeFlashingAnimation_do = function () {}
	that.log_do = function (msg) {}
	return that;
};
********************/



let TrafficLight = function () {
  
	let that = TrafficLightSpec.call(this);

  that.enterRed_do = function () {
    that.log_do("Enter $Red")
        $("#light_img").attr("src", "https://assets.codepen.io/2527236/RedLight.png");
    let s = `
@startuml
state Begin {
}
state FlashingRed {
}
state Working {
    state Red #red {
    }
    state Green {
    }
    state Yellow {
    }
}
[*] --> Begin
Begin --> Red : &#124;>>&#124;
Red --> Green : &#124;timer&#124;
Green --> Yellow : &#124;timer&#124;
Yellow --> Red : &#124;timer&#124;
FlashingRed --> Red : &#124;systemRestart&#124;
Working --> FlashingRed : &#124;systemError&#124;
@enduml
`
   compress(s)
    
    let frame_code = `
    $Red => $Working
        |>|
            enterRed() ^
        |timer|
            -> $Green ^
    `
    $("#frame_code").text(frame_code)
    
    let js_code = `
    let _sRed_ = function (e) {
        if (e._message == ">") {
            that.enterRed_do();
            return;
        }
        else if (e._message == "timer") {
            _transition_(_sGreen_);
            return;
        }
        _sWorking_(e);
        
    }`
      
    $("#js_code").text(js_code)
  }


 

	that.enterGreen_do = function () {
    that.log_do("Enter $Green")
    $("#light_img").attr("src", "https://assets.codepen.io/2527236/GreenLight.png");
        let s = `
@startuml
state Begin {
}
state FlashingRed {
}
state Working {
    state Red {
    }
    state Green #green  {
    }
    state Yellow {
    }
}
[*] --> Begin
Begin --> Red : &#124;>>&#124;
Red --> Green : &#124;timer&#124;
Green --> Yellow : &#124;timer&#124;
Yellow --> Red : &#124;timer&#124;
FlashingRed --> Red : &#124;systemRestart&#124;
Working --> FlashingRed : &#124;systemError&#124;
@enduml
`
   compress(s)
    
        let frame_code = `
    $Green => $Working
        |>|
            enterGreen() ^
        |timer|
            -> $Yellow ^
    `
    $("#frame_code").text(frame_code)
    let js_code = `
    let _sGreen_ = function (e) {
        if (e._message == ">") {
            that.enterGreen_do();
            return;
        }
        else if (e._message == "timer") {
            _transition_(_sYellow_);
            return;
        }
        _sWorking_(e);
        
    }
    `
      
    $("#js_code").text(js_code)
  }
  
	that.enterYellow_do = function () {
        that.log_do("Enter $Yellow")
        $("#light_img").attr("src", "https://assets.codepen.io/2527236/YellowLight.png");
            let s = `
@startuml
state Begin {
}
state FlashingRed {
}
state Working {
    state Red {
    }
    state Green {
    }
    state Yellow #yellow {
    }
}
[*] --> Begin
Begin --> Red : &#124;>>&#124;
Red --> Green : &#124;timer&#124;
Green --> Yellow : &#124;timer&#124;
Yellow --> Red : &#124;timer&#124;
FlashingRed --> Red : &#124;systemRestart&#124;
Working --> FlashingRed : &#124;systemError&#124;
@enduml
`
   compress(s)
    
   let frame_code = `
    $Yellow => $Working
        |>|
            enterYellow() ^
        |timer|
            -> $Red ^
    `
    $("#frame_code").text(frame_code)
    let js_code = `
    let _sYellow_ = function (e) {
        if (e._message == ">") {
            that.enterYellow_do();
            return;
        }
        else if (e._message == "timer") {
            _transition_(_sRed_);
            return;
        }
        _sWorking_(e);
        
    }
    `
      
    $("#js_code").text(js_code)
  }
  
	that.enterFlashingRed_do = function () {
    self.flashColor = "red";
    that.log_do("Enter $FlashingRed")
    // $("#light_img").src = ""
            let s = `
@startuml
state Begin {
}
state FlashingRed #magenta {
}
state Working {
    state Red {
    }
    state Green   {
    }
    state Yellow  {
    }
}
[*] --> Begin
Begin --> Red : &#124;>>&#124;
Red --> Green : &#124;timer&#124;
Green --> Yellow : &#124;timer&#124;
Yellow --> Red : &#124;timer&#124;
FlashingRed --> Red : &#124;systemRestart&#124;
Working --> FlashingRed : &#124;systemError&#124;
@enduml
`
   compress(s)
    
    let frame_code = `
    $FlashingRed
        |>|
            enterFlashingRed()
            stopWorkingTimer()
            startFlashingTimer() ^
        |<|
            exitFlashingRed()
            stopFlashingTimer()
            startWorkingTimer() ^
        |timer|
            changeFlashingAnimation() ^
        |systemRestart|
            -> $Red  ^
    `
    $("#frame_code").text(frame_code)
    let js_code = `
    let _sFlashingRed_ = function (e) {
        if (e._message == ">") {
            that.enterFlashingRed_do();
            that.stopWorkingTimer_do();
            that.startFlashingTimer_do();
            return;
        }
        else if (e._message == "<") {
            that.exitFlashingRed_do();
            that.stopFlashingTimer_do();
            that.startWorkingTimer_do();
            return;
        }
        else if (e._message == "timer") {
            that.changeFlashingAnimation_do();
            return;
        }
        else if (e._message == "systemRestart") {
            _transition_(_sRed_);
            return;
        }
    }
    `
      
    $("#js_code").text(js_code)
  }
	that.exitFlashingRed_do = function () {
    that.log_do("Exit $FlashingRed")
  }
  
	that.startWorkingTimer_do = function () {
    timer = setInterval(that.timer, 3000);  
  }
  
	that.stopWorkingTimer_do = function () {
    clearInterval(timer)
  }
  
	that.startFlashingTimer_do = function () {
    timer = setInterval(that.timer, 500);      
  }
  
	that.stopFlashingTimer_do = function () {
    clearInterval(timer)
  }
  
	that.startTimer_do = function () {
    var myVar = setInterval(that.timer, 5000);  
  }

  that.changeFlashingAnimation_do = function () {
    flashColor = flashColor == "red" ? "black" : "red";
    that.log_do("Flashcolor = " + flashColor)
    if (flashColor == "red") {
              $("#light_img").attr("src", "https://assets.codepen.io/2527236/RedLight.png");
    } else {
      $("#light_img").attr("src", "https://assets.codepen.io/2527236/BlackLight.png");
    }
  }
  
  that.log_do = function (msg) {
    var currentVal =  $("#output").val()
    $("#output").val(msg + "\n" + currentVal)
    console.log(msg)
  }
	return that;
  
};
              
            
!
999px

Console