Commit 5cd9e6d6 authored by Arthur Verschaeve's avatar Arthur Verschaeve

Merge pull request #1531 from dmethvin/thorax-escape

thorax: Cancel editing with the Escape key
parents f50d5507 66a4eee8
......@@ -26,16 +26,17 @@
"excludeFiles": [
"**/node_modules/**",
"**/bower_components/**",
"examples/vanilladart/**/*.js",
"examples/duel/www/**",
"examples/duel/src/main/webapp/js/lib/**",
"**/generated/**",
"examples/ampersand/todomvc.bundle.js",
"examples/angular2/**/*.js",
"examples/polymer/elements/elements.build.js",
"examples/duel/www/**",
"examples/duel/src/main/webapp/js/lib/**",
"examples/humble/js/**",
"examples/js_of_ocaml/js/*.js",
"examples/polymer/elements/elements.build.js",
"examples/thorax/js/lib/backbone-localstorage.js",
"examples/typescript-*/js/**/*.js",
"**/generated/**",
"examples/humble/js/**"
"examples/vanilladart/**/*.js"
],
"requireSpaceBeforeBlockStatements": true,
"requireParenthesesAroundIIFE": true,
......
/*global Thorax, Backbone*/
/*jshint unused:false*/
var ENTER_KEY = 13;
var ESCAPE_KEY = 27;
(function () {
'use strict';
......
/*global Thorax, ENTER_KEY*/
/*global Thorax, ENTER_KEY, ESCAPE_KEY*/
(function () {
'use strict';
......@@ -19,7 +19,7 @@
'click .toggle': 'toggleCompleted',
'dblclick label': 'edit',
'click .destroy': 'clear',
'keypress .edit': 'updateOnEnter',
'keyup .edit': 'keyListener',
'blur .edit': 'close',
// The "rendered" event is triggered by Thorax each time render()
// is called and the result of the template has been appended
......@@ -37,11 +37,16 @@
// Switch this view into `"editing"` mode, displaying the input field.
edit: function () {
this.$el.addClass('editing');
this.$('.edit').focus();
this.$('.edit').val(this.model.get('title')).focus();
},
// Close the `"editing"` mode, saving changes to the todo.
// Close the `"editing"` mode.
close: function () {
// If editing was cancelled, don't save
if (!this.$el.hasClass('editing')) {
return;
}
var value = this.$('.edit').val().trim();
if (value) {
......@@ -53,10 +58,17 @@
this.$el.removeClass('editing');
},
// If you hit `enter`, we're through editing the item.
updateOnEnter: function (e) {
// User cancelled editing, don't update the todo.
cancelEdits: function () {
this.$el.removeClass('editing');
},
// Enter completes the editing, Escape cancels it
keyListener: function (e) {
if (e.which === ENTER_KEY) {
this.close();
} else if (e.which === ESCAPE_KEY) {
this.cancelEdits();
}
},
......
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