todomvc: Follow HTML template and file layout more closely; use new locationHash

This commit is contained in:
Tony Garnock-Jones 2016-05-12 20:10:16 -04:00
parent eafcff8f75
commit a7ad6355f5
5 changed files with 76 additions and 90 deletions

View File

@ -1,70 +1,67 @@
<!doctype html>
<html>
<head>
<title>Syndicate: TodoMVC</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="style.css" rel="stylesheet">
<link href="todomvc/base.css" rel="stylesheet">
<link href="todomvc/index.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/2.2.1/mustache.min.js"></script>
<script src="../../third-party/jquery-2.2.0.min.js"></script>
<script src="../../dist/syndicatecompiler.js"></script>
<script src="../../dist/syndicate.js"></script>
<script type="text/syndicate-js" src="index.js"></script>
</head>
<body>
<section class="todoapp">
<header class="header">
<h1>todos</h1>
<input class="new-todo" placeholder="What needs to be done?" autofocus>
</header>
<section class="main">
<input class="toggle-all" type="checkbox">
<label for="toggle-all">Mark all as complete</label>
<ul class="todo-list" id="todo-list">
<template id="todo-list-item-view-template">
<li data-id="{{id}}" class="{{hidden_class}} {{completed_class}}">
<div class="view">
<input class="toggle" type="checkbox" {{checked}}>
<label>{{title}}</label>
<button class="destroy"></button>
</div>
</li>
</template>
<template id="todo-list-item-edit-template">
<li data-id="{{id}}" class="editing">
<input value="{{title}}" class="edit">
</li>
</template>
</ul>
</section>
<footer class="footer">
<span class="todo-count"></span>
<ul class="filters">
<li>
<a href="#/" class="selected">All</a>
</li>
<li>
<a href="#/active">Active</a>
</li>
<li>
<a href="#/completed">Completed</a>
</li>
</ul>
<button class="clear-completed">Clear completed</button>
</footer>
</section>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Syndicate • TodoMVC</title>
<link rel="stylesheet" href="node_modules/todomvc-common/base.css">
<link rel="stylesheet" href="node_modules/todomvc-app-css/index.css">
<link rel="stylesheet" href="css/app.css">
<footer class="info">
<p>Double-click to edit a todo</p>
<p>Created by <a href="http://twitter.com/oscargodson">Oscar Godson</a></p>
<p>Refactored by <a href="https://github.com/cburgmer">Christoph Burgmer</a></p>
<p>Ported to Syndicate/js by <a href="http://twitter.com/leastfixedpoint">Tony Garnock-Jones</a></p>
<p>HTML structure and CSS taken from <a href="http://todomvc.com">TodoMVC</a></p>
</footer>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/2.2.1/mustache.min.js"></script>
<script src="../../dist/syndicatecompiler.js"></script>
<script src="../../dist/syndicate.js"></script>
<script type="text/syndicate-js" src="index.js"></script>
</head>
<body>
<section class="todoapp">
<header class="header">
<h1>todos</h1>
<input class="new-todo" placeholder="What needs to be done?" autofocus>
</header>
<section class="main">
<input class="toggle-all" type="checkbox">
<label for="toggle-all">Mark all as complete</label>
<ul class="todo-list">
<template id="todo-list-item-view-template">
<li data-id="{{id}}" class="{{hidden_class}} {{completed_class}}">
<div class="view">
<input class="toggle" type="checkbox" {{checked}}>
<label>{{title}}</label>
<button class="destroy"></button>
</div>
</li>
</template>
<template id="todo-list-item-edit-template">
<li data-id="{{id}}" class="editing">
<input value="{{title}}" class="edit">
</li>
</template>
</ul>
</section>
<footer class="footer">
<span class="todo-count"><strong>0</strong> item left</span>
<ul class="filters">
<li>
<a class="selected" href="#/">All</a>
</li>
<li>
<a href="#/active">Active</a>
</li>
<li>
<a href="#/completed">Completed</a>
</li>
</ul>
<button class="clear-completed">Clear completed</button>
</footer>
</section>
<footer class="info">
<p>Double-click to edit a todo</p>
<p>Created by <a href="http://twitter.com/leastfixedpoint">Tony Garnock-Jones</a></p>
<p>Part of <a href="http://todomvc.com">TodoMVC</a></p>
</footer>
<hr>
<pre id="ds-state"></pre>
</body>
<hr>
<pre id="ds-state"></pre>
</body>
</html>

View File

@ -1,7 +1,6 @@
assertion type todo(id, title, completed);
message type deleteTodo(id);
assertion type show(completed);
assertion type currentLocationHash(hash);
/*
To Do (ho ho ho)
@ -27,6 +26,10 @@ assertion type currentLocationHash(hash);
var ESCAPE_KEY_CODE = 27;
var ENTER_KEY_CODE = 13;
function getTemplate(id) {
return document.getElementById(id).innerHTML;
}
var nextId = 0;
function addTodo(title) {
title = title.trim();
@ -48,10 +51,10 @@ function addTodo(title) {
finally { this.visible = false; }
}
assert this.ui.html('#todo-list',
Mustache.render($(this.editing
? '#todo-list-item-edit-template'
: '#todo-list-item-view-template').html(),
assert this.ui.html('.todo-list',
Mustache.render(getTemplate(this.editing
? 'todo-list-item-edit-template'
: 'todo-list-item-view-template'),
{
completed_class: this.completed ? "completed" : "",
hidden_class: this.visible ? "" : "hidden",
@ -115,22 +118,8 @@ ground dataspace G {
}
actor {
this.hash = hashFrom(document.location.toString());
function hashFrom(u) {
var i = u.indexOf('#');
return (i !== -1) ? u.slice(i + 1) : '/';
}
react {
assert currentLocationHash(this.hash);
on message Syndicate.UI.windowEvent('hashchange', $e) {
this.hash = hashFrom(e.newURL);
}
}
}
actor {
react {
on asserted currentLocationHash($hash) {
on asserted Syndicate.UI.locationHash($hash) {
// TODO this is a bit icky too
var ns = document.querySelectorAll('ul.filters > li > a');
for (var i = 0; i < ns.length; i++) { ns[i].className = ''; }
@ -138,14 +127,14 @@ ground dataspace G {
n.className = 'selected';
}
during currentLocationHash('/') {
during Syndicate.UI.locationHash('/') {
assert show(true);
assert show(false);
}
during currentLocationHash('/active') {
during Syndicate.UI.locationHash('/active') {
assert show(false);
}
during currentLocationHash('/completed') {
during Syndicate.UI.locationHash('/completed') {
assert show(true);
}
}
@ -157,5 +146,5 @@ ground dataspace G {
}
G.dataspace.setOnStateChange(function (mux, patch) {
$("#ds-state").text(Syndicate.prettyTrie(mux.routingTable));
document.getElementById("ds-state").innerText = Syndicate.prettyTrie(mux.routingTable);
});