Skip to content

Instantly share code, notes, and snippets.

@uk-ar
Last active December 15, 2015 10:59
Show Gist options
  • Save uk-ar/5249315 to your computer and use it in GitHub Desktop.
Save uk-ar/5249315 to your computer and use it in GitHub Desktop.
$(function(){
var raw_data;
var positions = [];
$('#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]": "[ ]";
var path = location.pathname.split("/");
path.shift();
//$.get("/data" + location.pathname).done(function(data){
$.ajax({url: "/data" + location.pathname,
cache: false,
type: "GET"
}).done(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