Skip to content

Commit

Permalink
Fix note task list state toggle on tasks with formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
elving committed Jun 18, 2014
1 parent bf7763f commit 4f5cbe0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
8 changes: 2 additions & 6 deletions app/account/views/note-detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
});

28 changes: 20 additions & 8 deletions app/core/models/note.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
})
});
}
});
8 changes: 5 additions & 3 deletions app/lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -83,9 +84,10 @@ _.mixin({'markdown': function(text) {

if (/^\s*\[[x ]\]\s*/.test(text)) {
text = text
.replace(/^\s*\[ \]\s*/, '<input type="checkbox" class="markdown-task" data-original-text="' + originalText + '">')
.replace(/^\s*\[x\]\s*/, '<input type="checkbox" class="markdown-task" data-original-text="' + originalText + '" checked>')
.replace(/^\s*\[ \]\s*/, '<input type="checkbox" data-index="' + noteTaskIndex + '" class="markdown-task">')
.replace(/^\s*\[x\]\s*/, '<input type="checkbox" data-index="' + noteTaskIndex + '" class="markdown-task" checked>')

noteTaskIndex += 1;
return '<li class="has-task">' + text + '</li>';
} else {
return '<li>' + text + '</li>';
Expand Down

0 comments on commit 4f5cbe0

Please sign in to comment.