Commit 4869d71a authored by Pascal Hartig's avatar Pascal Hartig

angularjs: add escape behavior

parent ba9a2fc9
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
<button class="destroy" ng-click="removeTodo(todo)"></button> <button class="destroy" ng-click="removeTodo(todo)"></button>
</div> </div>
<form ng-submit="doneEditing(todo)"> <form ng-submit="doneEditing(todo)">
<input class="edit" ng-model="todo.title" todo-blur="doneEditing(todo)" todo-focus="todo == editedTodo"> <input class="edit" ng-model="todo.title" todo-escape="revertEditing(todo)" todo-blur="doneEditing(todo)" todo-focus="todo == editedTodo">
</form> </form>
</li> </li>
</ul> </ul>
...@@ -66,5 +66,6 @@ ...@@ -66,5 +66,6 @@
<script src="js/services/todoStorage.js"></script> <script src="js/services/todoStorage.js"></script>
<script src="js/directives/todoFocus.js"></script> <script src="js/directives/todoFocus.js"></script>
<script src="js/directives/todoBlur.js"></script> <script src="js/directives/todoBlur.js"></script>
<script src="js/directives/todoEscape.js"></script>
</body> </body>
</html> </html>
/*global todomvc */ /*global todomvc, angular */
'use strict'; 'use strict';
/** /**
...@@ -47,6 +47,8 @@ todomvc.controller('TodoCtrl', function TodoCtrl($scope, $location, todoStorage, ...@@ -47,6 +47,8 @@ todomvc.controller('TodoCtrl', function TodoCtrl($scope, $location, todoStorage,
$scope.editTodo = function (todo) { $scope.editTodo = function (todo) {
$scope.editedTodo = todo; $scope.editedTodo = todo;
// Clone the original todo to restore it on demand.
$scope.originalTodo = angular.extend({}, todo);
}; };
$scope.doneEditing = function (todo) { $scope.doneEditing = function (todo) {
...@@ -58,6 +60,11 @@ todomvc.controller('TodoCtrl', function TodoCtrl($scope, $location, todoStorage, ...@@ -58,6 +60,11 @@ todomvc.controller('TodoCtrl', function TodoCtrl($scope, $location, todoStorage,
} }
}; };
$scope.revertEditing = function (todo) {
todos[todos.indexOf(todo)] = $scope.originalTodo;
$scope.doneEditing($scope.originalTodo);
};
$scope.removeTodo = function (todo) { $scope.removeTodo = function (todo) {
todos.splice(todos.indexOf(todo), 1); todos.splice(todos.indexOf(todo), 1);
}; };
......
/*global todomvc */
'use strict';
/**
* Directive that executes an expression when the element it is applied to gets
* an `escape` keydown event.
*/
todomvc.directive('todoBlur', function () {
var ESCAPE_KEY = 27;
return function (scope, elem, attrs) {
elem.bind('keydown', function (event) {
if (event.keyCode === ESCAPE_KEY) {
scope.$apply(attrs.todoEscape);
}
});
};
});
...@@ -172,6 +172,15 @@ ...@@ -172,6 +172,15 @@
scope.$digest(); scope.$digest();
expect(scope.completedCount).toBe(5); expect(scope.completedCount).toBe(5);
}); });
it('revertTodo() get a Todo to its previous state', function () {
var todo = todoList[0];
scope.editTodo(todo);
todo.title = 'Unicorn sparkly skypuffles.';
scope.revertEditing(todo);
scope.$digest();
expect(scope.todos[0].title).toBe('Uncompleted Item 0');
});
}); });
}); });
}()); }());
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment