Skip to content

Instantly share code, notes, and snippets.

@uk-ar
Created March 21, 2013 21:36
Show Gist options
  • Save uk-ar/5217021 to your computer and use it in GitHub Desktop.
Save uk-ar/5217021 to your computer and use it in GitHub Desktop.
var raw_data;
var positions = [];
$(function(){
$('#wiki-body').each(function(){
var txt = $(this).html();
var checkbox = '<input class="task-list" type="checkbox" '
$(this).html(
txt.replace(/\[x\]/g, checkbox + 'checked>').
replace(/\[ \]/g, checkbox + '>')
);
});
var task_list = $('input.task-list');
task_list.each(function(i){
if ($(this).parent()[0].nodeName != "LABEL"){
$(this).parent().contents().wrapAll("<label></label>");
}
$(this).attr({index: i})
});
});
$(document).off("change", 'input.task-list[type=checkbox]');
function change(){
var i = $(this).attr("index");
var state = ($(this).attr("checked") == "checked")? "[x]": "[ ]";
console.log();
var path = location.pathname.split("/");
path.shift();
$.get("/data" + location.pathname, function(data){
var checkbox_re=/\[ \]|\[x\]/g;
var result;
while ((result = checkbox_re.exec(data)) !== null) {
positions.push([result.index, checkbox_re.lastIndex]);
}
raw_data = data;
$.post("/edit" + location.pathname,
{page: path.pop(),
path: path.join("/"),
format: "markdown",
content: raw_data.slice(0, positions[i][0]) + state + raw_data.slice(positions[i][1]),
message: "Updated "
})
});
}
$(document).on("change", 'input.task-list[type=checkbox]', change);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment