mirror of
https://github.com/grocy/grocy.git
synced 2025-08-20 04:12:59 +00:00
Added possibility to manage rechargeable batteries
This commit is contained in:
43
views/batteries.js
Normal file
43
views/batteries.js
Normal file
@@ -0,0 +1,43 @@
|
||||
$(document).on('click', '.battery-delete-button', function(e)
|
||||
{
|
||||
bootbox.confirm({
|
||||
message: 'Delete battery <strong>' + $(e.target).attr('data-battery-name') + '</strong>?',
|
||||
buttons: {
|
||||
confirm: {
|
||||
label: 'Yes',
|
||||
className: 'btn-success'
|
||||
},
|
||||
cancel: {
|
||||
label: 'No',
|
||||
className: 'btn-danger'
|
||||
}
|
||||
},
|
||||
callback: function(result)
|
||||
{
|
||||
if (result === true)
|
||||
{
|
||||
Grocy.FetchJson('/api/delete-object/batteries/' + $(e.target).attr('data-battery-id'),
|
||||
function(result)
|
||||
{
|
||||
window.location.href = '/batteries';
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$(function()
|
||||
{
|
||||
$('#batteries-table').DataTable({
|
||||
'pageLength': 50,
|
||||
'order': [[1, 'asc']],
|
||||
'columnDefs': [
|
||||
{ 'orderable': false, 'targets': 0 }
|
||||
]
|
||||
});
|
||||
});
|
46
views/batteries.php
Normal file
46
views/batteries.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
|
||||
|
||||
<h1 class="page-header">
|
||||
Batteries
|
||||
<a class="btn btn-default" href="/battery/new" role="button">
|
||||
<i class="fa fa-plus"></i> Add
|
||||
</a>
|
||||
</h1>
|
||||
|
||||
<div class="table-responsive">
|
||||
<table id="batteries-table" class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>#</th>
|
||||
<th>Name</th>
|
||||
<th>Description</th>
|
||||
<th>Used in</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($batteries as $battery) : ?>
|
||||
<tr>
|
||||
<td class="fit-content">
|
||||
<a class="btn btn-info" href="/battery/<?php echo $battery->id; ?>" role="button">
|
||||
<i class="fa fa-pencil"></i>
|
||||
</a>
|
||||
<a class="btn btn-danger battery-delete-button" href="#" role="button" data-battery-id="<?php echo $battery->id; ?>" data-battery-name="<?php echo $battery->name; ?>">
|
||||
<i class="fa fa-trash"></i>
|
||||
</a>
|
||||
</td>
|
||||
<td>
|
||||
<?php echo $battery->name; ?>
|
||||
</td>
|
||||
<td>
|
||||
<?php echo $battery->description; ?>
|
||||
</td>
|
||||
<td>
|
||||
<?php echo $battery->used_in; ?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
</div>
|
7
views/batteriesoverview.js
Normal file
7
views/batteriesoverview.js
Normal file
@@ -0,0 +1,7 @@
|
||||
$(function()
|
||||
{
|
||||
$('#habits-overview-table').DataTable({
|
||||
'pageLength': 50,
|
||||
'order': [[1, 'desc']]
|
||||
});
|
||||
});
|
38
views/batteriesoverview.php
Normal file
38
views/batteriesoverview.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
|
||||
|
||||
<h1 class="page-header">Batteries overview</h1>
|
||||
|
||||
<div class="table-responsive">
|
||||
<table id="batteries-overview-table" class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Battery</th>
|
||||
<th>Last charged</th>
|
||||
<th>Next planned charge cycle</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($current as $curentBatteryEntry) : ?>
|
||||
<tr class="<?php if (GrocyPhpHelper::FindObjectInArrayByPropertyValue($batteries, 'id', $curentBatteryEntry->battery_id)->charge_interval_days > 0 && GrocyLogicBatteries::GetNextChargeTime($curentBatteryEntry->battery_id) < date('Y-m-d H:i:s')) echo 'error-bg'; ?>">
|
||||
<td>
|
||||
<?php echo GrocyPhpHelper::FindObjectInArrayByPropertyValue($batteries, 'id', $curentBatteryEntry->battery_id)->name; ?>
|
||||
</td>
|
||||
<td>
|
||||
<?php echo $curentBatteryEntry->last_tracked_time; ?>
|
||||
<time class="timeago timeago-contextual" datetime="<?php echo $curentBatteryEntry->last_tracked_time; ?>"></time>
|
||||
</td>
|
||||
<td>
|
||||
<?php if (GrocyPhpHelper::FindObjectInArrayByPropertyValue($batteries, 'id', $curentBatteryEntry->battery_id)->charge_interval_days > 0): ?>
|
||||
<?php echo GrocyLogicBatteries::GetNextChargeTime($curentBatteryEntry->battery_id); ?>
|
||||
<time class="timeago timeago-contextual" datetime="<?php echo GrocyLogicBatteries::GetNextChargeTime($curentBatteryEntry->battery_id); ?>"></time>
|
||||
<?php else: ?>
|
||||
Whenever you want...
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
</div>
|
38
views/batteryform.js
Normal file
38
views/batteryform.js
Normal file
@@ -0,0 +1,38 @@
|
||||
$('#save-battery-button').on('click', function(e)
|
||||
{
|
||||
e.preventDefault();
|
||||
|
||||
if (Grocy.EditMode === 'create')
|
||||
{
|
||||
Grocy.PostJson('/api/add-object/batteries', $('#battery-form').serializeJSON(),
|
||||
function(result)
|
||||
{
|
||||
window.location.href = '/batteries';
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
Grocy.PostJson('/api/edit-object/batteries/' + Grocy.EditObjectId, $('#battery-form').serializeJSON(),
|
||||
function(result)
|
||||
{
|
||||
window.location.href = '/batteries';
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
$(function()
|
||||
{
|
||||
$('#name').focus();
|
||||
$('#battery-form').validator();
|
||||
$('#battery-form').validator('validate');
|
||||
});
|
33
views/batteryform.php
Normal file
33
views/batteryform.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<div class="col-sm-3 col-sm-offset-3 col-md-4 col-md-offset-2 main">
|
||||
|
||||
<h1 class="page-header"><?php echo $title; ?></h1>
|
||||
|
||||
<script>Grocy.EditMode = '<?php echo $mode; ?>';</script>
|
||||
|
||||
<?php if ($mode == 'edit') : ?>
|
||||
<script>Grocy.EditObjectId = <?php echo $battery->id; ?>;</script>
|
||||
<?php endif; ?>
|
||||
|
||||
<form id="battery-form">
|
||||
|
||||
<div class="form-group">
|
||||
<label for="name">Name</label>
|
||||
<input type="text" class="form-control" required id="name" name="name" value="<?php if ($mode == 'edit') echo $battery->name; ?>">
|
||||
<div class="help-block with-errors"></div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="description">Description</label>
|
||||
<input type="text" class="form-control" id="description" name="description" value="<?php if ($mode == 'edit') echo $battery->description; ?>">
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="name">Used in</label>
|
||||
<input type="text" class="form-control" id="used_in" name="used_in" value="<?php if ($mode == 'edit') echo $battery->used_in; ?>">
|
||||
</div>
|
||||
|
||||
<button id="save-battery-button" type="submit" class="btn btn-default">Save</button>
|
||||
|
||||
</form>
|
||||
|
||||
</div>
|
167
views/batterytracking.js
Normal file
167
views/batterytracking.js
Normal file
@@ -0,0 +1,167 @@
|
||||
$('#save-batterytracking-button').on('click', function(e)
|
||||
{
|
||||
e.preventDefault();
|
||||
|
||||
var jsonForm = $('#batterytracking-form').serializeJSON();
|
||||
|
||||
Grocy.FetchJson('/api/batteries/get-battery-details/' + jsonForm.battery_id,
|
||||
function (batteryDetails)
|
||||
{
|
||||
Grocy.FetchJson('/api/batteries/track-charge-cycle/' + jsonForm.battery_id + '?tracked_time=' + $('#tracked_time').val(),
|
||||
function(result)
|
||||
{
|
||||
toastr.success('Tracked charge cylce of battery ' + batteryDetails.battery.name + ' on ' + $('#tracked_time').val());
|
||||
|
||||
$('#battery_id').val('');
|
||||
$('#battery_id_text_input').focus();
|
||||
$('#battery_id_text_input').val('');
|
||||
$('#tracked_time').val(moment().format('YYYY-MM-DD HH:mm:ss'));
|
||||
$('#tracked_time').trigger('change');
|
||||
$('#battery_id_text_input').trigger('change');
|
||||
$('#batterytracking-form').validator('validate');
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
$('#battery_id').on('change', function(e)
|
||||
{
|
||||
var batteryId = $(e.target).val();
|
||||
|
||||
if (batteryId)
|
||||
{
|
||||
Grocy.FetchJson('/api/batteries/get-battery-details/' + batteryId,
|
||||
function(batteryDetails)
|
||||
{
|
||||
$('#selected-battery-name').text(batteryDetails.battery.name);
|
||||
$('#selected-battery-last-charged').text((batteryDetails.last_charged || 'never'));
|
||||
$('#selected-battery-last-charged-timeago').text($.timeago(batteryDetails.last_charged || ''));
|
||||
$('#selected-battery-charge-cycles-count').text((batteryDetails.charge_cycles_count || '0'));
|
||||
|
||||
$('#tracked_time').focus();
|
||||
|
||||
Grocy.EmptyElementWhenMatches('#selected-battery-last-charged-timeago', 'NaN years ago');
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
$(function()
|
||||
{
|
||||
$('.datetimepicker').datetimepicker(
|
||||
{
|
||||
format: 'YYYY-MM-DD HH:mm:ss',
|
||||
showTodayButton: true,
|
||||
calendarWeeks: true,
|
||||
maxDate: moment()
|
||||
});
|
||||
|
||||
$('#tracked_time').val(moment().format('YYYY-MM-DD HH:mm:ss'));
|
||||
$('#tracked_time').trigger('change');
|
||||
|
||||
$('#tracked_time').on('focus', function(e)
|
||||
{
|
||||
if ($('#battery_id_text_input').val().length === 0)
|
||||
{
|
||||
$('#battery_id_text_input').focus();
|
||||
}
|
||||
});
|
||||
|
||||
$('.combobox').combobox({
|
||||
appendId: '_text_input'
|
||||
});
|
||||
|
||||
$('#battery_id').val('');
|
||||
$('#battery_id_text_input').focus();
|
||||
$('#battery_id_text_input').val('');
|
||||
$('#battery_id_text_input').trigger('change');
|
||||
|
||||
$('#batterytracking-form').validator();
|
||||
$('#batterytracking-form').validator('validate');
|
||||
|
||||
$('#batterytracking-form input').keydown(function(event)
|
||||
{
|
||||
if (event.keyCode === 13) //Enter
|
||||
{
|
||||
if ($('#batterytracking-form').validator('validate').has('.has-error').length !== 0) //There is at least one validation error
|
||||
{
|
||||
event.preventDefault();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$('#tracked_time').on('change', function(e)
|
||||
{
|
||||
var value = $('#tracked_time').val();
|
||||
var now = new Date();
|
||||
var centuryStart = Number.parseInt(now.getFullYear().toString().substring(0, 2) + '00');
|
||||
var centuryEnd = Number.parseInt(now.getFullYear().toString().substring(0, 2) + '99');
|
||||
|
||||
if (value === 'x' || value === 'X') {
|
||||
value = '29991231';
|
||||
}
|
||||
|
||||
if (value.length === 4 && !(Number.parseInt(value) > centuryStart && Number.parseInt(value) < centuryEnd))
|
||||
{
|
||||
value = (new Date()).getFullYear().toString() + value;
|
||||
}
|
||||
|
||||
if (value.length === 8 && $.isNumeric(value))
|
||||
{
|
||||
value = value.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3');
|
||||
$('#tracked_time').val(value);
|
||||
$('#batterytracking-form').validator('validate');
|
||||
}
|
||||
});
|
||||
|
||||
$('#tracked_time').on('keypress', function(e)
|
||||
{
|
||||
var element = $(e.target);
|
||||
var value = element.val();
|
||||
var dateObj = moment(element.val(), 'YYYY-MM-DD', true);
|
||||
|
||||
$('.datepicker').datepicker('hide');
|
||||
|
||||
//If input is empty and any arrow key is pressed, set date to today
|
||||
if (value.length === 0 && (e.keyCode === 38 || e.keyCode === 40 || e.keyCode === 37 || e.keyCode === 39))
|
||||
{
|
||||
dateObj = moment(new Date(), 'YYYY-MM-DD', true);
|
||||
}
|
||||
|
||||
if (dateObj.isValid())
|
||||
{
|
||||
if (e.keyCode === 38) //Up
|
||||
{
|
||||
element.val(dateObj.add(-1, 'days').format('YYYY-MM-DD'));
|
||||
}
|
||||
else if (e.keyCode === 40) //Down
|
||||
{
|
||||
element.val(dateObj.add(1, 'days').format('YYYY-MM-DD'));
|
||||
}
|
||||
else if (e.keyCode === 37) //Left
|
||||
{
|
||||
element.val(dateObj.add(-1, 'weeks').format('YYYY-MM-DD'));
|
||||
}
|
||||
else if (e.keyCode === 39) //Right
|
||||
{
|
||||
element.val(dateObj.add(1, 'weeks').format('YYYY-MM-DD'));
|
||||
}
|
||||
}
|
||||
|
||||
$('#batterytracking-form').validator('validate');
|
||||
});
|
42
views/batterytracking.php
Normal file
42
views/batterytracking.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<div class="col-sm-3 col-sm-offset-3 col-md-3 col-md-offset-2 main">
|
||||
|
||||
<h1 class="page-header">Battery tracking</h1>
|
||||
|
||||
<form id="batterytracking-form">
|
||||
|
||||
<div class="form-group">
|
||||
<label for="battery_id">Battery</label>
|
||||
<select class="form-control combobox" id="battery_id" name="battery_id" required>
|
||||
<option value=""></option>
|
||||
<?php foreach ($batteries as $battery) : ?>
|
||||
<option value="<?php echo $battery->id; ?>"><?php echo $battery->name; ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<div id="battery-error" class="help-block with-errors"></div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="tracked_time">Tracked time</label>
|
||||
<div class="input-group date datetimepicker">
|
||||
<input type="text" class="form-control" id="tracked_time" name="tracked_time" required >
|
||||
<span class="input-group-addon">
|
||||
<span class="fa fa-calendar"></span>
|
||||
</span>
|
||||
</div>
|
||||
<div class="help-block with-errors"></div>
|
||||
</div>
|
||||
|
||||
<button id="save-batterytracking-button" type="submit" class="btn btn-default">OK</button>
|
||||
|
||||
</form>
|
||||
|
||||
</div>
|
||||
|
||||
<div class="col-sm-6 col-md-5 col-lg-3 main well">
|
||||
<h3>Battery overview <strong><span id="selected-battery-name"></span></strong></h3>
|
||||
|
||||
<p>
|
||||
<strong>Charge cycles count:</strong> <span id="selected-battery-charge-cycles-count"></span><br>
|
||||
<strong>Last charged:</strong> <span id="selected-battery-last-charged"></span> <time id="selected-battery-last-charged-timeago" class="timeago timeago-contextual"></time><br>
|
||||
</p>
|
||||
</div>
|
@@ -58,6 +58,9 @@
|
||||
<li data-nav-for-page="habitsoverview.php">
|
||||
<a class="discrete-link" href="/habitsoverview"><i class="fa fa-tachometer fa-fw"></i> Habits overview</a>
|
||||
</li>
|
||||
<li data-nav-for-page="batteriesoverview.php">
|
||||
<a class="discrete-link" href="/batteriesoverview"><i class="fa fa-tachometer fa-fw"></i> Batteries overview</a>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
@@ -76,6 +79,9 @@
|
||||
<li data-nav-for-page="habittracking.php">
|
||||
<a class="discrete-link" href="/habittracking"><i class="fa fa-play fa-fw"></i> Habit tracking</a>
|
||||
</li>
|
||||
<li data-nav-for-page="batterytracking.php">
|
||||
<a class="discrete-link" href="/batterytracking"><i class="fa fa-fire fa-fw"></i> Track battery charge cycle</a>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
@@ -92,6 +98,9 @@
|
||||
<li data-nav-for-page="habits.php">
|
||||
<a class="discrete-link" href="/habits"><i class="fa fa-refresh fa-fw"></i> Habits</a>
|
||||
</li>
|
||||
<li data-nav-for-page="batteries.php">
|
||||
<a class="discrete-link" href="/batteries"><i class="fa fa-battery-three-quarters fa-fw"></i> Batteries</a>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
@@ -116,6 +125,9 @@
|
||||
<li data-nav-for-page="habitsoverview.php">
|
||||
<a class="discrete-link" href="/habitsoverview"><i class="fa fa-tachometer fa-fw"></i> Habits overview</a>
|
||||
</li>
|
||||
<li data-nav-for-page="batteriesoverview.php">
|
||||
<a class="discrete-link" href="/batteriesoverview"><i class="fa fa-tachometer fa-fw"></i> Batteries overview</a>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<ul class="nav nav-sidebar">
|
||||
@@ -134,6 +146,9 @@
|
||||
<li data-nav-for-page="habittracking.php">
|
||||
<a class="discrete-link" href="/habittracking"><i class="fa fa-play fa-fw"></i> Habit tracking</a>
|
||||
</li>
|
||||
<li data-nav-for-page="batterytracking.php">
|
||||
<a class="discrete-link" href="/batterytracking"><i class="fa fa-fire fa-fw"></i> Track battery charge cycle</a>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<ul class="nav nav-sidebar">
|
||||
@@ -150,6 +165,9 @@
|
||||
<li data-nav-for-page="habits.php">
|
||||
<a class="discrete-link" href="/habits"><i class="fa fa-refresh fa-fw"></i> Habits</a>
|
||||
</li>
|
||||
<li data-nav-for-page="batteries.php">
|
||||
<a class="discrete-link" href="/batteries"><i class="fa fa-battery-three-quarters fa-fw"></i> Batteries</a>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<div class="nav-copyright nav nav-sidebar">
|
||||
|
Reference in New Issue
Block a user