diff --git a/app/account/views/note-detail.js b/app/account/views/note-detail.js index 106b806..a60af3f 100644 --- a/app/account/views/note-detail.js +++ b/app/account/views/note-detail.js @@ -109,13 +109,9 @@ module.exports = Zeppelin.FormView.extend({ }, onNoteTaskChange: function(event) { - var $task = $(event.currentTarget), - taskUpdatedText = '', - taskOriginalText = $task.attr('data-original-text'); - + var $task = $(event.currentTarget); this.changeFromNote = true; - taskUpdatedText = this.model.updateTask(taskOriginalText); - $task.attr('data-original-text', taskUpdatedText); + this.model.updateTask(_.parseInt($task.attr('data-index')), $task.prop('checked')); } }); diff --git a/app/core/models/note.js b/app/core/models/note.js index 91a60b1..02bc641 100644 --- a/app/core/models/note.js +++ b/app/core/models/note.js @@ -7,18 +7,30 @@ module.exports = Card.extend({ }, Card.prototype.defaults()); }, - updateTask: function(task) { + updateTask: function(index, checked) { var self = this, - content = this.get('content'), - originalTask = task; + note = this.get('content'), + tasks = note.match(/(\s*\[[x ]\]\s*).+/gm), + changingTask = tasks[index], + tasksCounter = 0, + originalTask = changingTask; - if (/^\s*\[ \]\s*/.test(task)) { - task = task.replace('[ ]', '[x]'); + if (checked) { + changingTask = changingTask.replace('[ ]', '[x]'); } else { - task = task.replace('[x]', '[ ]'); + changingTask = changingTask.replace('[x]', '[ ]'); } - this.save({ content: content.replace(originalTask, task) }); - return task; + this.save({ + content: note.replace(/(\s*\[[x ]\]\s*).+/gm, function(match) { + if (tasksCounter === index) { + tasksCounter += 1; + return changingTask; + } else { + tasksCounter += 1; + return match; + } + }) + }); } }); diff --git a/app/lib/helpers.js b/app/lib/helpers.js index 9fe2309..11fbf77 100644 --- a/app/lib/helpers.js +++ b/app/lib/helpers.js @@ -58,7 +58,8 @@ _.mixin({'decodeJWT': function(token) { _.mixin({'markdown': function(text) { var parser, - renderer = new marked.Renderer(); + renderer = new marked.Renderer(), + noteTaskIndex = 0; renderer.link = function(href, title, text) { if (title) { @@ -83,9 +84,10 @@ _.mixin({'markdown': function(text) { if (/^\s*\[[x ]\]\s*/.test(text)) { text = text - .replace(/^\s*\[ \]\s*/, '') - .replace(/^\s*\[x\]\s*/, '') + .replace(/^\s*\[ \]\s*/, '') + .replace(/^\s*\[x\]\s*/, '') + noteTaskIndex += 1; return '