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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rivets implementation + CR changes from #1226 + merged #1295

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions examples/rivets/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
node_modules/todomvc-common/*
!node_modules/todomvc-common/base.css
!node_modules/todomvc-common/base.js

node_modules/todomvc-app-css/*
!node_modules/todomvc-app-css/index.css

node_modules/director/*
!node_modules/director/build/director.js

node_modules/rivets/*
!node_modules/rivets/dist/
node_modules/rivets/dist/*
!node_modules/rivets/dist/rivets.bundled.min.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The gitignore looks good but this isn't actually committed.

122 changes: 122 additions & 0 deletions examples/rivets/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<!doctype html>
<html lang="en" data-framework="rivets">
<head>
<meta charset="utf-8">
<title>Rivets • TodoMVC</title>
<link rel="stylesheet" href="node_modules/todomvc-common/base.css">
<link rel="stylesheet" href="node_modules/todomvc-app-css/index.css">
</head>
<body>
<script type="text/template" id="todos">
<header class="header">
<h1>todos</h1>
<form rv-on-submit="addTodo">
<input
class="new-todo"
placeholder="What needs to be done?"
rv-value="newTodo"
autofocus>
</form>
</header>

<section class="main" rv-show="todos.all | length">
<input
class="toggle-all"
type="checkbox"
rv-prop-checked="todos.isAllCompleted < all"
rv-on-change="toggleAllCompleted">

<label for="toggle-all">Mark all as complete</label>

<ul class="todo-list">
<li
rv-each-todo="todos.filtered < all statusFilter"
rv-class-completed="todo.completed"
rv-class-editing="todos.editing | eq todo">

<todo todo="todo"></todo>
</li>
</ul>
</section>

<footer class="footer" rv-show="todos.all | length">
<filter-bar todos="todos"></filter-bar>
</footer>
</script>

<script type="text/template" id="todo">
<div>
<div class="view">
<input
class="toggle"
type="checkbox"
rv-prop-checked="todo.completed"
rv-on-change="toggle">

<label rv-on-dblclick="edit">{ todo.title }</label>
<button class="destroy" rv-on-click="remove"></button>
</div>

<form rv-on-submit="save">
<input
class="edit"
rv-value="newTitle"
rv-escape="revert"
rv-on-blur="save"
rv-focus="todos.editing | eq undefined | not">
</form>
</div>
</script>

<script type="text/template" id="filter-bar">
<span class="todo-count">
<strong>{ todos.active < all | length }</strong>
{ todos.active < all | length | eq 1 | switch 'item' 'items' }
left
</span>

<ul class="filters">
<li rv-each-filter="filters">
<a
rv-class-selected="todos.statusFilter | eq filter.id"
rv-href="'#/' | append filter.id">

{ filter.title }
</a>
</li>
</ul>

<button class="clear-completed" rv-on-click="clearCompleted" rv-show="todos.completed < all | length">
Clear completed
</button>
</script>

<section class="todoapp"></section>

<footer class="info">
<p>Double-click to edit a todo</p>

<p>
Written by <a href="https://github.com/mikeric">Michael Richards</a>
and <a href="http://altio.us">Andrew Gurinovich</a>
</p>

<p>Part of <a href="http://todomvc.com">TodoMVC</a></p>
</footer>

<script src="node_modules/todomvc-common/base.js"></script>
<script src="node_modules/director/build/director.js"></script>
<script src="node_modules/rivets/dist/rivets.bundled.min.js"></script>

<script src="js/formatters.js"></script>
<script src="js/binders/escape.js"></script>
<script src="js/binders/focus.js"></script>
<script src="js/binders/prop.js"></script>
<script src="js/stores/todo-store.js"></script>
<script src="js/components/todos.js"></script>
<script src="js/components/todo.js"></script>
<script src="js/components/filter-bar.js"></script>
<script src="js/routes.js"></script>
<script src="js/app.js"></script>
</body>
</html>
6 changes: 6 additions & 0 deletions examples/rivets/js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*global rivets */
(function (rivets) {
'use strict';

rivets.init('todos', document.querySelector('.todoapp'));
})(rivets);
31 changes: 31 additions & 0 deletions examples/rivets/js/binders/escape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*global rivets */
(function (rivets) {
'use strict';

var ESCAPE_KEY = 27;
var EVENT = 'keydown';

rivets.binders.escape = {
function: true,

unbind: function (el) {
if (this.handler) {
el.removeEventListener(EVENT, this.handler);
}
},

routine: function (el, value) {
if (this.handler) {
el.removeEventListener(EVENT, this.handler);
}

this.handler = this.eventHandler(function (ev) {
if (ev.keyCode === ESCAPE_KEY) {
value.apply(this, arguments);
}
});

el.addEventListener(EVENT, this.handler);
}
};
})(rivets);
10 changes: 10 additions & 0 deletions examples/rivets/js/binders/focus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*global rivets */
(function (rivets) {
'use strict';

rivets.binders.focus = function (el, value) {
if (value) {
el.focus();
}
};
})(rivets);
8 changes: 8 additions & 0 deletions examples/rivets/js/binders/prop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*global rivets */
(function (rivets) {
'use strict';

rivets.binders['prop-*'] = function (el, value) {
el[this.args[0]] = value;
};
})(rivets);
31 changes: 31 additions & 0 deletions examples/rivets/js/components/filter-bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*global rivets, TodoStore */
(function (rivets, TodoStore) {
'use strict';

// FilterBar component constructor.
var FilterBar = function (todos) {
this.todos = todos;

this.filters = [
{id: '', title: 'All'},
{id: 'active', title: 'Active'},
{id: 'completed', title: 'Completed'}
];
};

// Clears all completed todo items.
FilterBar.prototype.clearCompleted = function () {
TodoStore.clearCompleted();
};

// Register <filter-bar> component.
rivets.components['filter-bar'] = {
template: function () {
return document.querySelector('#filter-bar').innerHTML;
},

initialize: function (el, data) {
return new FilterBar(data.todos);
}
};
})(rivets, TodoStore);
64 changes: 64 additions & 0 deletions examples/rivets/js/components/todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*global rivets, TodoStore */
(function (rivets, TodoStore) {
'use strict';

// Todo component constructor.
var Todo = function (todo) {
this.edit = this.edit.bind(this);
this.save = this.save.bind(this);
this.toggle = this.toggle.bind(this);
this.revert = this.revert.bind(this);
this.remove = this.remove.bind(this);
this.todos = TodoStore;

this.todo = todo;
};

// Starts edit mode for the todo item.
Todo.prototype.edit = function () {
this.newTitle = this.todo.title;
TodoStore.editing = this.todo;
};

// Saves the todo item's new title. If the new title is empty, then the todo
// item will be removed instead.
Todo.prototype.save = function (ev) {
ev.preventDefault();

if (this.newTitle.trim()) {
this.todo.title = this.newTitle.trim();
TodoStore.save();
} else {
this.remove();
}

this.revert();
};

// Toggles the item as complete / active.
Todo.prototype.toggle = function () {
TodoStore.toggle(this.todo);
};

// Reverts changes made for the new title and leaves edit mode.
Todo.prototype.revert = function () {
this.newTitle = this.todo.title;
TodoStore.editing = null;
};

// Removes the todo item.
Todo.prototype.remove = function () {
TodoStore.remove(this.todo);
};

// Register <todo> component.
rivets.components.todo = {
template: function () {
return document.querySelector('#todo').innerHTML;
},

initialize: function (el, data) {
return new Todo(data.todo);
}
};
})(rivets, TodoStore);
41 changes: 41 additions & 0 deletions examples/rivets/js/components/todos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*global rivets, TodoStore */
(function (rivets, TodoStore) {
'use strict';

// Todos component constructor.
var Todos = function () {
this.addTodo = this.addTodo.bind(this);
this.todos = TodoStore;
this.newTodo = '';
};

// Adds a new todo item.
Todos.prototype.addTodo = function (ev) {
ev.preventDefault();

if (this.newTodo.trim().length) {
TodoStore.add({
title: this.newTodo.trim(),
completed: false
});
}

this.newTodo = '';
};

// Toggles all todo items as complete / active.
Todos.prototype.toggleAllCompleted = function () {
TodoStore.markAll(!TodoStore.isAllCompleted());
};

// Register <todos> component.
rivets.components.todos = {
template: function () {
return document.querySelector('#todos').innerHTML;
},

initialize: function () {
return new Todos();
}
};
})(rivets, TodoStore);
24 changes: 24 additions & 0 deletions examples/rivets/js/formatters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*global rivets */
(function (rivets) {
'use strict';

rivets.formatters.length = function (value) {
return value.length;
};

rivets.formatters.not = function (value) {
return !value;
};

rivets.formatters.eq = function (value, eqValue) {
return value === eqValue;
};

rivets.formatters.append = function (value, appValue) {
return value + appValue;
};

rivets.formatters.switch = function (value, tValue, fValue) {
return value ? tValue : fValue;
};
})(rivets);
26 changes: 26 additions & 0 deletions examples/rivets/js/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*global Router, TodoStore */
(function (Router, TodoStore) {
'use strict';

var routes = {
'/': function () {
TodoStore.statusFilter = '';
},

'/active': function () {
TodoStore.statusFilter = 'active';
},

'/completed': function () {
TodoStore.statusFilter = 'completed';
}
};

var router = new Router(routes).configure({
notfound: function () {
TodoStore.statusFilter = '';
}
});

router.init();
})(Router, TodoStore);
Loading