2017-11-05 12:41:00 +01:00
|
|
|
|
$('#save-batterytracking-button').on('click', function(e)
|
|
|
|
|
{
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
2020-12-31 13:45:14 +01:00
|
|
|
|
if ($(".combobox-menu-visible").length)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-05 12:41:00 +01:00
|
|
|
|
var jsonForm = $('#batterytracking-form').serializeJSON();
|
2018-11-24 19:40:50 +01:00
|
|
|
|
Grocy.FrontendHelpers.BeginUiBusy("batterytracking-form");
|
2017-11-05 12:41:00 +01:00
|
|
|
|
|
2019-01-19 00:37:21 -07:00
|
|
|
|
Grocy.Api.Get('batteries/' + jsonForm.battery_id,
|
2020-08-30 12:18:16 +02:00
|
|
|
|
function(batteryDetails)
|
2017-11-05 12:41:00 +01:00
|
|
|
|
{
|
2019-01-19 14:51:51 +01:00
|
|
|
|
Grocy.Api.Post('batteries/' + jsonForm.battery_id + '/charge', { 'tracked_time': $('#tracked_time').find('input').val() },
|
2017-11-05 12:41:00 +01:00
|
|
|
|
function(result)
|
|
|
|
|
{
|
2018-11-24 19:40:50 +01:00
|
|
|
|
Grocy.FrontendHelpers.EndUiBusy("batterytracking-form");
|
2019-05-05 14:13:50 +02:00
|
|
|
|
toastr.success(__t('Tracked charge cycle of battery %1$s on %2$s', batteryDetails.battery.name, $('#tracked_time').find('input').val()) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoChargeCycle(' + result.id + ')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>');
|
2020-12-20 15:04:46 +01:00
|
|
|
|
Grocy.Components.BatteryCard.Refresh($('#battery_id').val());
|
2017-11-05 12:41:00 +01:00
|
|
|
|
|
|
|
|
|
$('#battery_id').val('');
|
|
|
|
|
$('#battery_id_text_input').focus();
|
|
|
|
|
$('#battery_id_text_input').val('');
|
2018-07-11 19:43:05 +02:00
|
|
|
|
$('#tracked_time').find('input').val(moment().format('YYYY-MM-DD HH:mm:ss'));
|
2017-11-05 12:41:00 +01:00
|
|
|
|
$('#battery_id_text_input').trigger('change');
|
2018-07-11 19:43:05 +02:00
|
|
|
|
Grocy.FrontendHelpers.ValidateForm('batterytracking-form');
|
2017-11-05 12:41:00 +01:00
|
|
|
|
},
|
|
|
|
|
function(xhr)
|
|
|
|
|
{
|
2018-11-24 19:40:50 +01:00
|
|
|
|
Grocy.FrontendHelpers.EndUiBusy("batterytracking-form");
|
2017-11-05 12:41:00 +01:00
|
|
|
|
console.error(xhr);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
function(xhr)
|
|
|
|
|
{
|
2018-11-24 19:40:50 +01:00
|
|
|
|
Grocy.FrontendHelpers.EndUiBusy("batterytracking-form");
|
2017-11-05 12:41:00 +01:00
|
|
|
|
console.error(xhr);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$('#battery_id').on('change', function(e)
|
|
|
|
|
{
|
2018-07-22 09:54:06 +02:00
|
|
|
|
var input = $('#battery_id_text_input').val().toString();
|
|
|
|
|
$('#battery_id_text_input').val(input);
|
|
|
|
|
$('#battery_id').data('combobox').refresh();
|
2017-11-05 12:41:00 +01:00
|
|
|
|
|
2018-07-22 09:54:06 +02:00
|
|
|
|
var batteryId = $(e.target).val();
|
2017-11-05 12:41:00 +01:00
|
|
|
|
if (batteryId)
|
|
|
|
|
{
|
2018-04-14 11:10:38 +02:00
|
|
|
|
Grocy.Components.BatteryCard.Refresh(batteryId);
|
2021-08-27 20:54:27 +02:00
|
|
|
|
|
|
|
|
|
setTimeout(function()
|
|
|
|
|
{
|
|
|
|
|
$('#tracked_time').find('input').focus();
|
|
|
|
|
}, 200);
|
|
|
|
|
|
2018-08-04 07:45:24 +02:00
|
|
|
|
Grocy.FrontendHelpers.ValidateForm('batterytracking-form');
|
2018-04-16 19:11:32 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
2017-11-05 12:41:00 +01:00
|
|
|
|
|
2018-04-16 19:11:32 +02:00
|
|
|
|
$('.combobox').combobox({
|
2018-09-24 16:50:30 +02:00
|
|
|
|
appendId: '_text_input',
|
2021-07-13 19:29:23 +02:00
|
|
|
|
bsVersion: '4',
|
|
|
|
|
clearIfNoMatch: false
|
2018-04-16 19:11:32 +02:00
|
|
|
|
});
|
2017-11-05 12:41:00 +01:00
|
|
|
|
|
2018-04-16 19:11:32 +02:00
|
|
|
|
$('#battery_id').val('');
|
|
|
|
|
$('#battery_id_text_input').focus();
|
|
|
|
|
$('#battery_id_text_input').val('');
|
|
|
|
|
$('#battery_id_text_input').trigger('change');
|
2019-03-04 17:43:12 +01:00
|
|
|
|
Grocy.Components.DateTimePicker.GetInputElement().trigger('input');
|
2018-07-11 19:43:05 +02:00
|
|
|
|
Grocy.FrontendHelpers.ValidateForm('batterytracking-form');
|
2017-11-05 12:41:00 +01:00
|
|
|
|
|
2020-08-30 12:18:16 +02:00
|
|
|
|
$('#batterytracking-form input').keyup(function(event)
|
2018-07-11 19:43:05 +02:00
|
|
|
|
{
|
|
|
|
|
Grocy.FrontendHelpers.ValidateForm('batterytracking-form');
|
|
|
|
|
});
|
2017-11-05 12:41:00 +01:00
|
|
|
|
|
2018-04-16 19:11:32 +02:00
|
|
|
|
$('#batterytracking-form input').keydown(function(event)
|
|
|
|
|
{
|
|
|
|
|
if (event.keyCode === 13) //Enter
|
2017-11-05 12:41:00 +01:00
|
|
|
|
{
|
2018-09-29 13:41:56 +02:00
|
|
|
|
event.preventDefault();
|
2019-01-19 00:37:21 -07:00
|
|
|
|
|
2018-07-11 19:43:05 +02:00
|
|
|
|
if (document.getElementById('batterytracking-form').checkValidity() === false) //There is at least one validation error
|
2017-11-05 12:41:00 +01:00
|
|
|
|
{
|
2018-04-16 19:11:32 +02:00
|
|
|
|
return false;
|
2017-11-05 12:41:00 +01:00
|
|
|
|
}
|
2018-07-11 19:43:05 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$('#save-batterytracking-button').click();
|
|
|
|
|
}
|
2018-04-16 19:11:32 +02:00
|
|
|
|
}
|
2017-11-05 12:41:00 +01:00
|
|
|
|
});
|
|
|
|
|
|
2020-08-30 12:18:16 +02:00
|
|
|
|
$('#tracked_time').find('input').on('keypress', function(e)
|
2017-11-05 12:41:00 +01:00
|
|
|
|
{
|
2018-07-11 19:43:05 +02:00
|
|
|
|
Grocy.FrontendHelpers.ValidateForm('batterytracking-form');
|
2017-11-05 12:41:00 +01:00
|
|
|
|
});
|
|
|
|
|
|
2021-07-13 19:29:23 +02:00
|
|
|
|
$(document).on("Grocy.BarcodeScanned", function(e, barcode, target)
|
|
|
|
|
{
|
|
|
|
|
if (!(target == "@batterypicker" || target == "undefined" || target == undefined)) // Default target
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 17:48:45 +02:00
|
|
|
|
// Don't know why the blur event does not fire immediately ... this works...
|
|
|
|
|
$("#battery_id_text_input").focusout();
|
|
|
|
|
$("#battery_id_text_input").focus();
|
|
|
|
|
$("#battery_id_text_input").blur();
|
|
|
|
|
|
|
|
|
|
$("#battery_id_text_input").val(barcode);
|
|
|
|
|
|
|
|
|
|
setTimeout(function()
|
|
|
|
|
{
|
|
|
|
|
$("#battery_id_text_input").focusout();
|
|
|
|
|
$("#battery_id_text_input").focus();
|
|
|
|
|
$("#battery_id_text_input").blur();
|
2021-08-27 20:54:27 +02:00
|
|
|
|
$('#tracked_time').find('input').focus();
|
2021-08-17 17:48:45 +02:00
|
|
|
|
}, 200);
|
2021-07-13 19:29:23 +02:00
|
|
|
|
});
|
|
|
|
|
|
2018-10-27 17:26:00 +02:00
|
|
|
|
function UndoChargeCycle(chargeCycleId)
|
|
|
|
|
{
|
2020-08-30 12:18:16 +02:00
|
|
|
|
Grocy.Api.Post('batteries/charge-cycles/' + chargeCycleId.toString() + '/undo', {},
|
2018-10-27 17:26:00 +02:00
|
|
|
|
function(result)
|
|
|
|
|
{
|
2019-05-01 20:19:18 +02:00
|
|
|
|
toastr.success(__t("Charge cycle successfully undone"));
|
2018-10-27 17:26:00 +02:00
|
|
|
|
},
|
|
|
|
|
function(xhr)
|
|
|
|
|
{
|
|
|
|
|
console.error(xhr);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
};
|
2021-07-13 19:29:23 +02:00
|
|
|
|
|
|
|
|
|
$('#battery_id_text_input').on('blur', function(e)
|
|
|
|
|
{
|
|
|
|
|
if ($('#battery_id').hasClass("combobox-menu-visible"))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var input = $('#battery_id_text_input').val().toString();
|
|
|
|
|
var possibleOptionElement = [];
|
|
|
|
|
|
|
|
|
|
// grocycode handling
|
|
|
|
|
if (input.startsWith("grcy"))
|
|
|
|
|
{
|
|
|
|
|
var gc = input.split(":");
|
|
|
|
|
if (gc[1] == "b")
|
|
|
|
|
{
|
|
|
|
|
possibleOptionElement = $("#battery_id option[value=\"" + gc[2] + "\"]").first();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-04 17:06:40 +02:00
|
|
|
|
|
|
|
|
|
if (possibleOptionElement.length > 0)
|
|
|
|
|
{
|
|
|
|
|
$('#battery_id').val(possibleOptionElement.val());
|
|
|
|
|
$('#battery_id').data('combobox').refresh();
|
|
|
|
|
$('#battery_id').trigger('change');
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$('#battery_id').val(null);
|
|
|
|
|
$('#battery_id_text_input').val("");
|
|
|
|
|
$('#battery_id').data('combobox').refresh();
|
|
|
|
|
$('#battery_id').trigger('change');
|
|
|
|
|
}
|
2021-07-13 19:29:23 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
2021-08-27 20:54:27 +02:00
|
|
|
|
|
|
|
|
|
$("#tracked_time").find("input").on("focus", function(e)
|
|
|
|
|
{
|
|
|
|
|
$(this).select();
|
|
|
|
|
});
|