Commit 6eb745d2 authored by Arthur Verschaeve's avatar Arthur Verschaeve

Merge pull request #1287 from podefr/fix-esc-key

Olives: fix editing isn't cancelled on ESC #789
parents be58d7ce 2d258c72
...@@ -5,17 +5,19 @@ var BindPlugin = require('olives')['Bind.plugin']; ...@@ -5,17 +5,19 @@ var BindPlugin = require('olives')['Bind.plugin'];
var Tools = require('../lib/Tools'); var Tools = require('../lib/Tools');
module.exports = function listInit(view, model, stats) { module.exports = function listInit(view, model, stats) {
// The OObject (the controller) inits with a default model which is a simple store // The OObject (the controller) initializes with a default model which is a simple store
// But it can be init'ed with any other store, like the LocalStore // But it can be initialized with any other store, like the LocalStore
var list = new OObject(model); var list = new OObject(model);
var modelPlugin = new BindPlugin(model, {
toggleClass: Tools.toggleClass
});
var ENTER_KEY = 13; var ENTER_KEY = 13;
var ESC_KEY = 27;
// The plugins // The plugins
list.seam.addAll({ list.seam.addAll({
event: new EventPlugin(list), event: new EventPlugin(list),
model: new BindPlugin(model, { model: modelPlugin,
toggleClass: Tools.toggleClass
}),
stats: new BindPlugin(stats, { stats: new BindPlugin(stats, {
toggleClass: Tools.toggleClass, toggleClass: Tools.toggleClass,
toggleCheck: function (value) { toggleCheck: function (value) {
...@@ -40,18 +42,18 @@ module.exports = function listInit(view, model, stats) { ...@@ -40,18 +42,18 @@ module.exports = function listInit(view, model, stats) {
// Enter edit mode // Enter edit mode
list.startEdit = function (event, node) { list.startEdit = function (event, node) {
var taskId = node.getAttribute('data-model_id'); var taskId = modelPlugin.getItemIndex(node);
Tools.toggleClass.call(view.querySelector('li[data-model_id="' + taskId + '"]'), true, 'editing'); toggleEditing(taskId, true);
view.querySelector('input.edit[data-model_id="' + taskId + '"]').focus(); getElementByModelId('input.edit', taskId).focus();
}; };
// Leave edit mode // Leave edit mode
list.stopEdit = function (event, node) { list.stopEdit = function (event, node) {
var taskId = node.getAttribute('data-model_id'); var taskId = modelPlugin.getItemIndex(node);
var value; var value;
if (event.keyCode === ENTER_KEY) { if (event.keyCode === ENTER_KEY || event.type === 'blur') {
value = node.value.trim(); value = node.value.trim();
if (value) { if (value) {
...@@ -62,13 +64,24 @@ module.exports = function listInit(view, model, stats) { ...@@ -62,13 +64,24 @@ module.exports = function listInit(view, model, stats) {
// When task #n is removed, #n+1 becomes #n, the dom node is updated to the new value, so editing mode should exit anyway // When task #n is removed, #n+1 becomes #n, the dom node is updated to the new value, so editing mode should exit anyway
if (model.has(taskId)) { if (model.has(taskId)) {
Tools.toggleClass.call(view.querySelector('li[data-model_id="' + taskId + '"]'), false, 'editing'); toggleEditing(taskId, false);
} }
} else if (event.type === 'blur') { } else if (event.keyCode === ESC_KEY) {
Tools.toggleClass.call(view.querySelector('li[data-model_id="' + taskId + '"]'), false, 'editing'); toggleEditing(taskId, false);
// Also reset the input field to the previous value so that the blur event doesn't pick up the discarded one
node.value = model.get(taskId).title;
} }
}; };
// Alive applies the plugins to the HTML view // Alive applies the plugins to the HTML view
list.alive(view); list.alive(view);
function toggleEditing(taskId, bool) {
var li = getElementByModelId('li', taskId);
Tools.toggleClass.call(li, bool, 'editing');
}
function getElementByModelId(selector, taskId) {
return view.querySelector(selector + '[data-model_id="' + taskId + '"]');
}
}; };
This diff is collapsed.
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