Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

a gh page for exercises #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
133 changes: 133 additions & 0 deletions index.html
@@ -0,0 +1,133 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Rxjs</title>
<style>
body {
margin: 0;
padding: 0;
}
nav {
text-align: left;
background-color: #4679bd;
height: 44px;
margin-bottom: 10px;
}
nav a {
padding: 0 5px;
display: inline-block;
line-height: 40px;
text-decoration: none;
color: white;
}
.logo {
width: 200px;
font-weight: bold;
font-size: 20px;
}
textarea {
overflow: auto;
margin: 0px;
height: 304px;
width: 100%;
height: 100%;
box-sizing: border-box;
resize: horizontal;
white-space: nowrap;
border: 0;
}
.solution {
overflow: auto;
}
h3, h1 {
margin: 0;
}
.clearfix::before {
content: "";
display: block;
visibility: hidden;
clear: both;
}
.container {
width: 90vw;
height: 90vh;
margin: 0 auto;
}
.upperhalf{
height: 40%;
width: 100%;
display: table;
}
.cells {
display: table-cell;
width: 50%;
border: 1px solid #bbb;
position: relative;
}
.lowerhalf {
height: 60%;
width: 100%;
display: table;
}
.window-label {
position: absolute;
top: 5px;
right: 5px;
border: 1px solid #ddd;
color: #777;
padding: 0 5px;
display: inline-block;
z-index: 100;
opacity: 0.9;
}
#eval{
background-color: #fff;
width: 45px;
text-align: center;
color: #4679bd;
}
</style>
</head>
<body>
<nav class="clearfix">
<a class="logo">RxJs</a>
<a id="q"></a>
<a href="#" id="prev" data-pos="-1">Previous</a>
<a href="#" id="start" data-pos="0">Restart</a>
<a href="#" id="next" data-pos="1">Next</a>
<a href="#" id="eval">Run</a>
</nav>
<div class="container">

<div class="upperhalf">
<div class="cells">
<textarea class="question"></textarea>
<span class="window-label">Question</span>
</div>
<div class="cells">
<div class="solution"></div>
<span class="window-label" readonly>Result</span>
</div>
</div>

<div class="lowerhalf">
<div class="cells">
<textarea class="js"></textarea>
<span class="window-label">JavaScript</span>
</div>
<div class="cells">
<textarea class="html"></textarea>
<span class="window-label">HTML</span>
</div>
</div>
</div>

<script src="lib/rx.all.js"></script>
<script src="lib/console.js"></script>
<script src="lib/utils.js"></script>
<script src="lib/storage.js"></script>
<script src="lib/exercises.js"></script>
<script src="lib/script.js"></script>
</body>
</html>
41 changes: 41 additions & 0 deletions lib/console.js
@@ -0,0 +1,41 @@
function _try( obj, key, value ) {
if( arguments.length == 3 )
obj && (obj[ key ] = obj[ key ] || value);
return !!(obj && obj.hasOwnProperty( key ));
}

(function(global, logger) {
'use strict';
global.Console = logger();
})(window, function() {
var __consoleApi = {
history: [],
clear: function() {
window.console.clear();
this.history = [];
},
log: function() {
var args = [].slice.call(arguments);
this.history = this.history || [];
this.history.push( args );
_console.log( args );
return args[0];
},
xhr: function( url, options ) {
options || (options = {});
var xmlht2p = new XMLHttpRequest;

[ 'type', 'params', "handler" ].forEach(function(key) {
_try( options, key, options[ key ] );
});

if(typeof options.handler !== 'function')
options.handler = function() { return this; };

xmlht2p.open(options.type || "get", url, true);
xmlht2p.onreadystatechange = options.handler;
xmlht2p.send();
}
}
return __consoleApi;
});
46 changes: 46 additions & 0 deletions lib/exercises.js
@@ -0,0 +1,46 @@
var latest = lolDb.fetch();

var chapters = {
1 : "imperative-array.js",
2 : "functional-array.js",
3 : "functional-event-stream.js",
4 : ["promise-vs-observable-1.js", "promise-vs-observable-2.js"],
5 : "dom-events.js",
6 : "bmi.js",
7 : "combinators-and-or.js",
8 : "correlational-combinators.js",
9 : "cold-and-hot.js",
10 : "flat-map.js",
11 : "cycle-checkbox.js",
12 : "cycle-counter.js",
13 : "cycle-bmi.js"
};

var makeKey = function(key) {
return key < 10 ? '0' + key : '' + key
};

var urls = Object.keys( chapters ).map(function( key ) {
if( Array.isArray( chapters[key] ) ) {
return chapters[key].map(function(data) {
return [ makeKey(key), data ].join('/');
});
}
return [makeKey(key), chapters[ key ]].join('/');
}).flatten();

function indirectEval( cb ) {
var code = new Function('exports', cb );
var exports = {};
code( exports );
return exports;
}

$('#eval').onclick = function(e) {
e.preventDefault();
var html = $('.html').value.trim();
var code = $('.js').value.trim();
code = code.replace(/^function\s*([^)]+)\)\s*{/, '').replace(/\}$/, '');
$('.solution').innerHTML = html.replace(/\n/g, '<br/>');
indirectEval( code );
}