Commit ad60850c authored by Dave Methvin's avatar Dave Methvin

thorax_lumbar: Cancel editing using Escape key

Ref #789
parent 5cd9e6d6
...@@ -91,6 +91,7 @@ module.routes = {"":"setFilter",":filter":"setFilter"}; ...@@ -91,6 +91,7 @@ module.routes = {"":"setFilter",":filter":"setFilter"};
}()); }());
;; ;;
/*global Thorax, ENTER_KEY, ESCAPE_KEY*/
$(function () { $(function () {
'use strict'; 'use strict';
...@@ -111,7 +112,7 @@ $(function () { ...@@ -111,7 +112,7 @@ $(function () {
'click .toggle': 'toggleCompleted', 'click .toggle': 'toggleCompleted',
'dblclick label': 'edit', 'dblclick label': 'edit',
'click .destroy': 'clear', 'click .destroy': 'clear',
'keypress .edit': 'updateOnEnter', 'keyup .edit': 'keyListener',
'blur .edit': 'close', 'blur .edit': 'close',
// The "rendered" event is triggered by Thorax each time render() // The "rendered" event is triggered by Thorax each time render()
// is called and the result of the template has been appended // is called and the result of the template has been appended
...@@ -129,11 +130,16 @@ $(function () { ...@@ -129,11 +130,16 @@ $(function () {
// Switch this view into `"editing"` mode, displaying the input field. // Switch this view into `"editing"` mode, displaying the input field.
edit: function () { edit: function () {
this.$el.addClass('editing'); 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 () { close: function () {
// If editing was cancelled, don't save
if (!this.$el.hasClass('editing')) {
return;
}
var value = this.$('.edit').val().trim(); var value = this.$('.edit').val().trim();
if (value) { if (value) {
...@@ -145,10 +151,17 @@ $(function () { ...@@ -145,10 +151,17 @@ $(function () {
this.$el.removeClass('editing'); this.$el.removeClass('editing');
}, },
// If you hit `enter`, we're through editing the item. // User cancelled editing, don't update the todo.
updateOnEnter: function (e) { cancelEdits: function () {
this.$el.removeClass('editing');
},
// Enter completes the editing, Escape cancels it
keyListener: function (e) {
if (e.which === ENTER_KEY) { if (e.which === ENTER_KEY) {
this.close(); this.close();
} else if (e.which === ESCAPE_KEY) {
this.cancelEdits();
} }
}, },
...@@ -305,7 +318,10 @@ Thorax.templates['src/templates/app'] = Handlebars.compile('<section id=\"todoap ...@@ -305,7 +318,10 @@ Thorax.templates['src/templates/app'] = Handlebars.compile('<section id=\"todoap
}()); }());
;; ;;
/*global Thorax, Backbone*/
/*jshint unused:false*/
var ENTER_KEY = 13; var ENTER_KEY = 13;
var ESCAPE_KEY = 27;
$(function () { $(function () {
// Kick things off by creating the **App**. // Kick things off by creating the **App**.
......
/*global Thorax, Backbone*/
/*jshint unused:false*/
var ENTER_KEY = 13; var ENTER_KEY = 13;
var ESCAPE_KEY = 27;
$(function () { $(function () {
// Kick things off by creating the **App**. // Kick things off by creating the **App**.
......
/*global Thorax, ENTER_KEY, ESCAPE_KEY*/
$(function () { $(function () {
'use strict'; 'use strict';
...@@ -18,7 +19,7 @@ $(function () { ...@@ -18,7 +19,7 @@ $(function () {
'click .toggle': 'toggleCompleted', 'click .toggle': 'toggleCompleted',
'dblclick label': 'edit', 'dblclick label': 'edit',
'click .destroy': 'clear', 'click .destroy': 'clear',
'keypress .edit': 'updateOnEnter', 'keyup .edit': 'keyListener',
'blur .edit': 'close', 'blur .edit': 'close',
// The "rendered" event is triggered by Thorax each time render() // The "rendered" event is triggered by Thorax each time render()
// is called and the result of the template has been appended // is called and the result of the template has been appended
...@@ -36,11 +37,16 @@ $(function () { ...@@ -36,11 +37,16 @@ $(function () {
// Switch this view into `"editing"` mode, displaying the input field. // Switch this view into `"editing"` mode, displaying the input field.
edit: function () { edit: function () {
this.$el.addClass('editing'); 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 () { close: function () {
// If editing was cancelled, don't save
if (!this.$el.hasClass('editing')) {
return;
}
var value = this.$('.edit').val().trim(); var value = this.$('.edit').val().trim();
if (value) { if (value) {
...@@ -52,10 +58,17 @@ $(function () { ...@@ -52,10 +58,17 @@ $(function () {
this.$el.removeClass('editing'); this.$el.removeClass('editing');
}, },
// If you hit `enter`, we're through editing the item. // User cancelled editing, don't update the todo.
updateOnEnter: function (e) { cancelEdits: function () {
this.$el.removeClass('editing');
},
// Enter completes the editing, Escape cancels it
keyListener: function (e) {
if (e.which === ENTER_KEY) { if (e.which === ENTER_KEY) {
this.close(); 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