now it's possible to add comment to the weight, closes #54

This commit is contained in:
azivner 2018-02-17 11:47:22 -05:00
parent 1d0220b03d
commit 08bc2afb49

View file

@ -1,4 +1,4 @@
<form id="weight-form" style="display: flex; width: 500px; justify-content: space-around; align-items: flex-end;"> <form id="weight-form" style="display: flex; width: 700px; justify-content: space-around; align-items: flex-end;">
<div> <div>
<label for="weight-date">Date</label> <label for="weight-date">Date</label>
<input type="text" id="weight-date" class="form-control" style="width: 150px; text-align: center;" /> <input type="text" id="weight-date" class="form-control" style="width: 150px; text-align: center;" />
@ -7,6 +7,10 @@
<label for="weight">Weight</label> <label for="weight">Weight</label>
<input type="number" id="weight" value="80.0" step="0.1" class="form-control" style="text-align: center; width: 100px;" /> <input type="number" id="weight" value="80.0" step="0.1" class="form-control" style="text-align: center; width: 100px;" />
</div> </div>
<div>
<label for="comment">Comment</label>
<input type="text" id="comment" class="form-control" style="width: 200px;" />
</div>
<button type="submit" class="btn btn-primary">Add</button> <button type="submit" class="btn btn-primary">Add</button>
</form> </form>
@ -17,27 +21,37 @@
<script> <script>
(async function() { (async function() {
const dateEl = $("#weight-date"); const $form = $("#weight-form");
const weightEl = $("#weight"); const $date = $("#weight-date");
const $weight = $("#weight");
const $comment = $("#comment");
let chart; let chart;
dateEl.datepicker(); $date.datepicker();
dateEl.datepicker('option', 'dateFormat', 'yy-mm-dd'); $date.datepicker('option', 'dateFormat', 'yy-mm-dd');
dateEl.datepicker('setDate', new Date()); $date.datepicker('setDate', new Date());
async function saveWeight() { async function saveWeight() {
await server.exec([dateEl.val(), weightEl.val()], async (date, weight) => { await server.exec([$date.val(), $weight.val(), $comment.val()], async (date, weight, comment) => {
const dataNote = await this.getNoteWithAttribute('date_data', date); const dataNote = await this.getNoteWithAttribute('date_data', date);
if (dataNote) { if (dataNote) {
dataNote.jsonContent.weight = weight; dataNote.jsonContent.weight = weight;
if (comment) {
dataNote.jsonContent.weight_comment = comment;
}
await this.updateEntity(dataNote); await this.updateEntity(dataNote);
} }
else { else {
const parentNoteId = await this.getDateNoteId(date); const parentNoteId = await this.getDateNoteId(date);
const jsonContent = { weight: weight }; const jsonContent = { weight: weight };
if (comment) {
jsonContent.weight_comment = comment;
}
await this.createNote(parentNoteId, 'data', jsonContent, { await this.createNote(parentNoteId, 'data', jsonContent, {
json: true, json: true,
attributes: { attributes: {
@ -50,9 +64,7 @@
showMessage("Weight has been saved"); showMessage("Weight has been saved");
const data = await getData(); chart.data = await getData();
chart.data = data;
chart.update(); chart.update();
} }
@ -63,7 +75,25 @@
chart = new Chart(ctx, { chart = new Chart(ctx, {
type: 'line', type: 'line',
data: data data: data,
options: {
tooltips: {
enabled: true,
mode: 'single',
callbacks: {
label: function (tooltipItem, data) {
const multistringText = [tooltipItem.yLabel];
const comment = data.comments[tooltipItem['index']];
if (comment) {
multistringText.push(comment);
}
return multistringText;
}
}
},
}
}); });
} }
@ -77,7 +107,8 @@
data.push({ data.push({
date: dateAttr.value, date: dateAttr.value,
weight: note.jsonContent.weight weight: note.jsonContent.weight,
comment: note.jsonContent.weight_comment
}); });
} }
@ -86,23 +117,30 @@
return data; return data;
}); });
const colors = data.map(row => row.comment ? 'darkred' : 'red');
const datasets = [{ const datasets = [{
label: "Weight", label: "Weight",
backgroundColor: 'red', backgroundColor: 'red',
borderColor: 'red', borderColor: 'red',
data: data.map(row => row.weight), data: data.map(row => row.weight),
// this is to emphasize points with color
pointBackgroundColor: colors,
pointBorderColor: colors,
fill: false fill: false
}]; }];
const labels = data.map(row => row.date); const labels = data.map(row => row.date);
const comments = data.map(row => row.comment);
return { return {
labels: labels, labels: labels,
datasets: datasets datasets: datasets,
comments: comments
}; };
} }
$("#weight-form").submit(event => { $form.submit(event => {
saveWeight(); saveWeight();
event.preventDefault(); event.preventDefault();