From 02b6c3b721d909925eca628b30d256f420c5f886 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Sun, 13 May 2018 09:38:22 +0200 Subject: [PATCH] Make it also possible to directly execute/track battery charge cycles and habits from overview pages --- localization/de.php | 2 ++ public/js/grocy.js | 6 +++++- public/viewjs/batteriesoverview.js | 30 +++++++++++++++++++++++++++++- public/viewjs/habitsoverview.js | 30 +++++++++++++++++++++++++++++- views/batteriesoverview.blade.php | 16 ++++++++++++++-- views/habitsoverview.blade.php | 16 ++++++++++++++-- 6 files changed, 93 insertions(+), 7 deletions(-) diff --git a/localization/de.php b/localization/de.php index 6002de8b..630ed2d7 100644 --- a/localization/de.php +++ b/localization/de.php @@ -127,6 +127,8 @@ return array( 'Tracked charge cylce of battery #1 on #2' => 'Ladezyklus für Batterie #1 am #2 erfasst', 'Consume all #1 which are currently in stock' => 'Verbrauche den kompletten Bestand von #1', 'All' => 'Alle', + 'Track charge cycle of battery #1' => 'Erfasse einen Ladezyklus für Batterie #1', + 'Track execution of habit #1' => 'Erfasse eine Ausführung von #1', //Constants 'manually' => 'Manuell', diff --git a/public/js/grocy.js b/public/js/grocy.js index 8070909f..d63d236c 100644 --- a/public/js/grocy.js +++ b/public/js/grocy.js @@ -26,7 +26,11 @@ if (!Grocy.ActiveNav.isEmpty()) } $.timeago.settings.allowFuture = true; -$('time.timeago').timeago(); +RefreshContextualTimeago = function() +{ + $('time.timeago').timeago(); +} +RefreshContextualTimeago(); toastr.options = { toastClass: 'alert', diff --git a/public/viewjs/batteriesoverview.js b/public/viewjs/batteriesoverview.js index d1f63bad..b11d0970 100644 --- a/public/viewjs/batteriesoverview.js +++ b/public/viewjs/batteriesoverview.js @@ -1,5 +1,33 @@ $('#batteries-overview-table').DataTable({ 'pageLength': 50, - 'order': [[1, 'desc']], + 'order': [[2, 'desc']], + 'columnDefs': [ + { 'orderable': false, 'targets': 0 } + ], 'language': JSON.parse(L('datatables_localization')) }); + +$(document).on('click', '.track-charge-cycle-button', function(e) +{ + var batteryId = $(e.currentTarget).attr('data-battery-id'); + var batteryName = $(e.currentTarget).attr('data-battery-name'); + var trackedTime = moment().format('YYYY-MM-DD HH:mm:ss'); + + Grocy.Api.Get('batteries/track-charge-cycle/' + batteryId + '?tracked_time=' + trackedTime, + function(result) + { + $('#battery-' + batteryId + '-last-tracked-time').parent().effect('highlight', {}, 500); + $('#battery-' + batteryId + '-last-tracked-time').fadeOut(500, function () { + $(this).text(trackedTime).fadeIn(500); + }); + $('#battery-' + batteryId + '-last-tracked-time-timeago').attr('datetime', trackedTime); + RefreshContextualTimeago(); + + toastr.success(L('Tracked charge cylce of battery #1 on #2', batteryName, trackedTime)); + }, + function(xhr) + { + console.error(xhr); + } + ); +}); diff --git a/public/viewjs/habitsoverview.js b/public/viewjs/habitsoverview.js index 1f2bb50c..973295cd 100644 --- a/public/viewjs/habitsoverview.js +++ b/public/viewjs/habitsoverview.js @@ -1,5 +1,33 @@ $('#habits-overview-table').DataTable({ 'pageLength': 50, - 'order': [[1, 'desc']], + 'order': [[2, 'desc']], + 'columnDefs': [ + { 'orderable': false, 'targets': 0 } + ], 'language': JSON.parse(L('datatables_localization')) }); + +$(document).on('click', '.track-habit-button', function(e) +{ + var habitId = $(e.currentTarget).attr('data-habit-id'); + var habitName = $(e.currentTarget).attr('data-habit-name'); + var trackedTime = moment().format('YYYY-MM-DD HH:mm:ss'); + + Grocy.Api.Get('habits/track-habit-execution/' + habitId + '?tracked_time=' + trackedTime, + function(result) + { + $('#habit-' + habitId + '-last-tracked-time').parent().effect('highlight', {}, 500); + $('#habit-' + habitId + '-last-tracked-time').fadeOut(500, function () { + $(this).text(trackedTime).fadeIn(500); + }); + $('#habit-' + habitId + '-last-tracked-time-timeago').attr('datetime', trackedTime); + RefreshContextualTimeago(); + + toastr.success(L('Tracked execution of habit #1 on #2', habitName, trackedTime)); + }, + function(xhr) + { + console.error(xhr); + } + ); +}); diff --git a/views/batteriesoverview.blade.php b/views/batteriesoverview.blade.php index ae6e4ac6..923873a5 100644 --- a/views/batteriesoverview.blade.php +++ b/views/batteriesoverview.blade.php @@ -4,6 +4,10 @@ @section('activeNav', 'batteriesoverview') @section('viewJsName', 'batteriesoverview') +@push('pageScripts') + +@endpush + @section('content')

@yield('title')

@@ -20,6 +24,7 @@ + @@ -28,12 +33,19 @@ @foreach($current as $curentBatteryEntry) +
# {{ $L('Battery') }} {{ $L('Last charged') }} {{ $L('Next planned charge cycle') }}
+ + + + {{ FindObjectInArrayByPropertyValue($batteries, 'id', $curentBatteryEntry->battery_id)->name }} - {{ $curentBatteryEntry->last_tracked_time }} - + {{ $curentBatteryEntry->last_tracked_time }} + @if(FindObjectInArrayByPropertyValue($batteries, 'id', $curentBatteryEntry->battery_id)->charge_interval_days > 0) diff --git a/views/habitsoverview.blade.php b/views/habitsoverview.blade.php index 2ba666e3..58c7adac 100644 --- a/views/habitsoverview.blade.php +++ b/views/habitsoverview.blade.php @@ -4,6 +4,10 @@ @section('activeNav', 'habitsoverview') @section('viewJsName', 'habitsoverview') +@push('pageScripts') + +@endpush + @section('content')

@yield('title')

@@ -20,6 +24,7 @@ + @@ -28,6 +33,13 @@ @foreach($currentHabits as $curentHabitEntry) + @@ -40,8 +52,8 @@ @endif @endforeach
# {{ $L('Habit') }} {{ $L('Next estimated tracking') }} {{ $L('Last tracked') }}
+ + + + {{ FindObjectInArrayByPropertyValue($habits, 'id', $curentHabitEntry->habit_id)->name }} - {{ $curentHabitEntry->last_tracked_time }} - + {{ $curentHabitEntry->last_tracked_time }} +