﻿(function (win) {
    'use strict';

    if (!win.refreshAjax) return;
    var edit = false;

    refreshAjax.navigating(function (req, resp) {
        edit = req.data && req.data.action == 'edit';
    });

    refreshAjax.updating(function (req, resp) {
        if (req.target.id === 'todo-form') {
            var newTodoId = 0;
            for (var id in resp.panels) {
                var todoId = Number(id.slice(4));
                if (todoId && newTodoId < todoId)
                    newTodoId = todoId;
            }
            if (newTodoId && !document.getElementById('todo' + newTodoId)) {
                document.getElementById('todo-list')
                    .insertAdjacentHTML('beforeend', '<li><span id="todo' + newTodoId + '" /></li>');
            }
        }
        if (req.data && req.data.action === 'edit' && !edit)
            resp.panels = null;
    });

    refreshAjax.updated(function (req, resp) {
        if (req.target.getAttribute) {
            var todoId = req.target.getAttribute('data-todo');
            if (todoId && !resp.panels['todo' + todoId]) {
                var todo = document.getElementById('todo' + todoId);
                if (todo) {
                    var listItem = todo.parentNode;
                    listItem.parentNode.removeChild(listItem);
                }
            }
        }
        initEdit(req);
    });

    function initEdit(req) {
        var el = document.querySelector('#todo-list input[type="text"]');
        if (el) {
            el.focus();
            el.value = el.value;
            el.addEventListener('blur', function (e) {
                if (!edit) {
                    refreshAjax.navigate({ action: 'edit', Title: el.value }, el);
                    edit = true;
                }
            });
            el.addEventListener('keyup', function (e) {
                cancel(e);
            });
            el.addEventListener('keypress', function (e) {
                cancel(e);
            });
        } else if (req.target.id === 'todo-form') {
            document.getElementById('new-todo').focus();
        }

        function cancel(e) {
            if (!edit && e.keyCode == 27) {
                refreshAjax.navigate({ action: 'clear' }, el);
                edit = true;
            }
        }
    }

    initEdit({ target: { id: 'todo-form' } });
})(window);

@@ -1914,123 +1914,160 @@
     
-cancel(e);%0D%0A            %7D);%0D%0A            el.addEventListener('keypress', function (e) %7B%0D%0A                cancel(e);
+if (!edit && e.keyCode == 27) %7B%0D%0A                    refreshAjax.navigate(%7B action: 'clear' %7D, el);%0D%0A                    edit = true;%0D%0A                %7D
 %0D%0A  
@@ -2239,162 +2239,8 @@
  %7B%0D%0A
-            if (!edit && e.keyCode == 27) %7B%0D%0A                refreshAjax.navigate(%7B action: 'clear' %7D, el);%0D%0A                edit = true;%0D%0A            %7D%0D%0A
     

Put it back because it affected firefox and ie. Rather have it a bit odd in opera