-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08a9494
commit 930cf4d
Showing
7 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<!doctype html> | ||
<html lang="en" ng-app="todomvc" data-framework="angularjs"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>AngularJS • TodoMVC</title> | ||
<link rel="stylesheet" href="node_modules/todomvc-common/base.css"> | ||
<link rel="stylesheet" href="node_modules/todomvc-app-css/index.css"> | ||
<style>[ng-cloak] { display: none; }</style> | ||
</head> | ||
<body> | ||
<section id="todoapp" ng-controller="TodoCtrl"> | ||
<header id="header"> | ||
<h1>todos</h1> | ||
<form id="todo-form" ng-submit="addTodo()"> | ||
<input id="new-todo" placeholder="What needs to be done?" ng-model="newTodo" autofocus> | ||
</form> | ||
</header> | ||
<section id="main" ng-show="todos.length" ng-cloak> | ||
<input id="toggle-all" type="checkbox" ng-model="allChecked" ng-click="markAll(allChecked)"> | ||
<label for="toggle-all">Mark all as complete</label> | ||
<ul id="todo-list"> | ||
<li ng-repeat="todo in todos | filter:statusFilter track by $index" ng-class="{completed: todo.completed, editing: todo == editedTodo}"> | ||
<div class="view"> | ||
<input class="toggle" type="checkbox" ng-model="todo.completed" ng-change="todoCompleted(todo)"> | ||
<label ng-dblclick="editTodo(todo)">{{todo.title}}</label> | ||
<button class="destroy" ng-click="removeTodo(todo)"></button> | ||
</div> | ||
<form ng-submit="doneEditing(todo)"> | ||
<input class="edit" ng-trim="false" ng-model="todo.title" ng-blur="doneEditing(todo)" todo-escape="revertEditing(todo)" todo-focus="todo == editedTodo"> | ||
</form> | ||
</li> | ||
</ul> | ||
</section> | ||
<footer id="footer" ng-show="todos.length" ng-cloak> | ||
<span id="todo-count"><strong>{{remainingCount}}</strong> | ||
<ng-pluralize count="remainingCount" when="{ one: 'item left', other: 'items left' }"></ng-pluralize> | ||
</span> | ||
<ul id="filters"> | ||
<li> | ||
<a ng-class="{selected: location.path() == '/'} " href="#/">All</a> | ||
</li> | ||
<li> | ||
<a ng-class="{selected: location.path() == '/active'}" href="#/active">Active</a> | ||
</li> | ||
<li> | ||
<a ng-class="{selected: location.path() == '/completed'}" href="#/completed">Completed</a> | ||
</li> | ||
</ul> | ||
<button id="clear-completed" ng-click="clearCompletedTodos()" ng-show="remainingCount < todos.length">Clear completed</button> | ||
</footer> | ||
</section> | ||
<footer id="info"> | ||
<p>Double-click to edit a todo</p> | ||
<p>Credits: | ||
<a href="http://twitter.com/cburgdorf">Christoph Burgdorf</a>, | ||
<a href="http://ericbidelman.com">Eric Bidelman</a>, | ||
<a href="http://jacobmumm.com">Jacob Mumm</a> and | ||
<a href="http://igorminar.com">Igor Minar</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/angular/angular.js"></script> | ||
<script src="js/app.js"></script> | ||
<script src="js/controllers/todoCtrl.js"></script> | ||
<script src="js/services/todoStorage.js"></script> | ||
<script src="js/directives/todoFocus.js"></script> | ||
<script src="js/directives/todoEscape.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
'use strict'; | ||
|
||
var todomvc = angular.module('todomvc', []); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/*global todomvc, angular */ | ||
'use strict'; | ||
|
||
/** | ||
* The main controller for the app. The controller: | ||
* - retrieves and persists the model via the todoStorage service | ||
* - exposes the model to the template and provides event handlers | ||
*/ | ||
todomvc.controller('TodoCtrl', function TodoCtrl($scope, $location, $filter, todoStorage) { | ||
var todos = $scope.todos = todoStorage.get(); | ||
|
||
$scope.newTodo = ''; | ||
$scope.remainingCount = $filter('filter')(todos, {completed: false}).length; | ||
$scope.editedTodo = null; | ||
|
||
if ($location.path() === '') { | ||
$location.path('/'); | ||
} | ||
|
||
$scope.location = $location; | ||
|
||
$scope.$watch('location.path()', function (path) { | ||
$scope.statusFilter = { '/active': {completed: false}, '/completed': {completed: true} }[path]; | ||
}); | ||
|
||
$scope.$watch('remainingCount == 0', function (val) { | ||
$scope.allChecked = val; | ||
}); | ||
|
||
$scope.addTodo = function () { | ||
var newTodo = $scope.newTodo.trim(); | ||
if (newTodo.length === 0) { | ||
return; | ||
} | ||
|
||
todos.push({ | ||
title: newTodo, | ||
completed: false | ||
}); | ||
todoStorage.put(todos); | ||
|
||
$scope.newTodo = ''; | ||
$scope.remainingCount++; | ||
}; | ||
|
||
$scope.editTodo = function (todo) { | ||
$scope.editedTodo = todo; | ||
// Clone the original todo to restore it on demand. | ||
$scope.originalTodo = angular.extend({}, todo); | ||
}; | ||
|
||
$scope.doneEditing = function (todo) { | ||
$scope.editedTodo = null; | ||
todo.title = todo.title.trim(); | ||
|
||
if (!todo.title) { | ||
$scope.removeTodo(todo); | ||
} | ||
|
||
todoStorage.put(todos); | ||
}; | ||
|
||
$scope.revertEditing = function (todo) { | ||
todos[todos.indexOf(todo)] = $scope.originalTodo; | ||
$scope.doneEditing($scope.originalTodo); | ||
}; | ||
|
||
$scope.removeTodo = function (todo) { | ||
$scope.remainingCount -= todo.completed ? 0 : 1; | ||
todos.splice(todos.indexOf(todo), 1); | ||
todoStorage.put(todos); | ||
}; | ||
|
||
$scope.todoCompleted = function (todo) { | ||
$scope.remainingCount += todo.completed ? -1 : 1; | ||
todoStorage.put(todos); | ||
}; | ||
|
||
$scope.clearCompletedTodos = function () { | ||
$scope.todos = todos = todos.filter(function (val) { | ||
return !val.completed; | ||
}); | ||
todoStorage.put(todos); | ||
}; | ||
|
||
$scope.markAll = function (completed) { | ||
todos.forEach(function (todo) { | ||
todo.completed = !completed; | ||
}); | ||
$scope.remainingCount = completed ? todos.length : 0; | ||
todoStorage.put(todos); | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
todomvc.directive('todoEscape', function () { | ||
var ESCAPE_KEY = 27; | ||
return function (scope, elem, attrs) { | ||
elem.bind('keydown', function (event) { | ||
if (event.keyCode === ESCAPE_KEY) { | ||
scope.$apply(attrs.todoEscape); | ||
} | ||
}); | ||
|
||
scope.$on('$destroy', function () { | ||
elem.unbind('keydown'); | ||
}); | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
todomvc.directive('todoFocus', function ($timeout) { | ||
return function (scope, elem, attrs) { | ||
scope.$watch(attrs.todoFocus, function (newVal) { | ||
if (newVal) { | ||
$timeout(function () { | ||
elem[0].focus(); | ||
}, 0, false); | ||
} | ||
}); | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
todomvc.factory('todoStorage', function () { | ||
var STORAGE_ID = 'todos-angularjs-perf'; | ||
|
||
return { | ||
get: function () { | ||
return JSON.parse(localStorage.getItem(STORAGE_ID) || '[]'); | ||
}, | ||
|
||
put: function (todos) { | ||
localStorage.setItem(STORAGE_ID, JSON.stringify(todos)); | ||
} | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"private": true, | ||
"dependencies": { | ||
"angular": "1.3.8", | ||
"todomvc-common": "^1.0.1", | ||
"todomvc-app-css": "^1.0.1" | ||
} | ||
} |