From f9b90d31b7730103e296b0c2509885b07000eac4 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Sat, 14 Apr 2018 20:39:59 -0400 Subject: round-trip editable fields and checkboxes --- print/resources/inkstitch.js | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'print/resources') diff --git a/print/resources/inkstitch.js b/print/resources/inkstitch.js index 15cf494a..8123a31f 100644 --- a/print/resources/inkstitch.js +++ b/print/resources/inkstitch.js @@ -1,3 +1,12 @@ +$.postJSON = function(url, data, success=null) { + return $.ajax(url, { + type: 'POST', + data: JSON.stringify(data), + contentType: 'application/json', + success: success + }); +}; + function ping() { $.get("/ping") .done(function() { setTimeout(ping, 1000) }) @@ -49,13 +58,22 @@ $(function() { var content = $(this).html(); var field_name = $(this).attr('data-field-name'); $('[data-field-name="' + field_name + '"]').text(content); - $.post('/metadata/' + field_name + '/set', {value: content}); + $.postJSON('/metadata/' + field_name + '/set', {value: content}); }); // load up initial metadata values $.getJSON('/metadata', function(metadata) { $.each(metadata, function(field_name, value) { - $('[data-field-name="' + field_name + '"]').text(value); + $('[data-field-name="' + field_name + '"]').each(function(i, item) { + console.log(item); + if ($(item).is(':checkbox')) { + console.log("is a checkbox"); + $(item).prop('checked', value).trigger('change'); + } else { + console.log("is not a checkbox"); + $(item).text(value); + } + }); }); }); @@ -108,9 +126,14 @@ $(function() { //Checkbox $(':checkbox').change(function() { - $('.' + this.id).toggle(); + var checked = $(this).prop('checked'); + var field_name = $(this).attr('data-field-name'); + + $('.' + field_name).toggle(checked); setPageNumbers(); scaleInksimulation(); + + $.postJSON('/metadata/' + field_name + '/set', {value: checked}); }); }); -- cgit v1.2.3