From fd61d1ef62f493cd566df1734d322144c5a2e312 Mon Sep 17 00:00:00 2001 From: Katharina Bogad Date: Tue, 22 Jun 2021 22:38:55 +0200 Subject: [PATCH] viewjs: Wrap in fuctions This commit's contents were auto-generated. They wrap all viewjs in functions and define a common prologue. New file contents: ```js function VIEWNAMEView(Grocy, scope = null) { var $scope = $; if(scope != null) { $scope = $(scope).find; } // original contents indented with \t } ``` --- js/viewjs/about.js | 24 +- js/viewjs/barcodescannertesting.js | 180 +-- js/viewjs/batteries.js | 82 +- js/viewjs/batteriesjournal.js | 86 +- js/viewjs/batteriesoverview.js | 244 ++-- js/viewjs/batteriessettings.js | 18 +- js/viewjs/batteryform.js | 176 +-- js/viewjs/batterytracking.js | 198 +-- js/viewjs/calendar.js | 174 +-- js/viewjs/choreform.js | 468 +++---- js/viewjs/chores.js | 80 +- js/viewjs/choresjournal.js | 86 +- js/viewjs/choresoverview.js | 354 ++--- js/viewjs/choressettings.js | 18 +- js/viewjs/choretracking.js | 216 +-- js/viewjs/consume.js | 1153 ++++++++-------- js/viewjs/equipment.js | 176 +-- js/viewjs/equipmentform.js | 292 ++-- js/viewjs/inventory.js | 680 +++++----- js/viewjs/locationcontentsheet.js | 34 +- js/viewjs/locationform.js | 174 +-- js/viewjs/locations.js | 43 +- js/viewjs/login.js | 20 +- js/viewjs/manageapikeys.js | 94 +- js/viewjs/mealplan.js | 1632 ++++++++++++----------- js/viewjs/openapiui.js | 56 +- js/viewjs/productbarcodeform.js | 208 +-- js/viewjs/productform.js | 936 ++++++------- js/viewjs/productgroupform.js | 146 +- js/viewjs/productgroups.js | 58 +- js/viewjs/products.js | 156 ++- js/viewjs/purchase.js | 1044 ++++++++------- js/viewjs/quantityunitconversionform.js | 346 ++--- js/viewjs/quantityunitform.js | 346 ++--- js/viewjs/quantityunitpluraltesting.js | 78 +- js/viewjs/quantityunits.js | 43 +- js/viewjs/recipeform.js | 608 ++++----- js/viewjs/recipeposform.js | 322 ++--- js/viewjs/recipes.js | 514 +++---- js/viewjs/recipessettings.js | 22 +- js/viewjs/shoppinglist.js | 1050 +++++++-------- js/viewjs/shoppinglistform.js | 136 +- js/viewjs/shoppinglistitemform.js | 498 +++---- js/viewjs/shoppinglistsettings.js | 30 +- js/viewjs/shoppinglocationform.js | 174 +-- js/viewjs/shoppinglocations.js | 43 +- js/viewjs/stockentries.js | 520 ++++---- js/viewjs/stockentryform.js | 226 ++-- js/viewjs/stockjournal.js | 100 +- js/viewjs/stockjournalsummary.js | 37 +- js/viewjs/stockoverview.js | 604 ++++----- js/viewjs/stocksettings.js | 92 +- js/viewjs/taskcategories.js | 43 +- js/viewjs/taskcategoryform.js | 170 +-- js/viewjs/taskform.js | 186 +-- js/viewjs/tasks.js | 304 +++-- js/viewjs/taskssettings.js | 18 +- js/viewjs/transfer.js | 888 ++++++------ js/viewjs/userentities.js | 43 +- js/viewjs/userentityform.js | 196 +-- js/viewjs/userfieldform.js | 222 +-- js/viewjs/userfields.js | 88 +- js/viewjs/userform.js | 260 ++-- js/viewjs/userobjectform.js | 134 +- js/viewjs/userobjects.js | 43 +- js/viewjs/userpermissions.js | 142 +- js/viewjs/users.js | 43 +- js/viewjs/usersettings.js | 14 +- 68 files changed, 9280 insertions(+), 8609 deletions(-) diff --git a/js/viewjs/about.js b/js/viewjs/about.js index a4228cc4..2f1dcb7a 100644 --- a/js/viewjs/about.js +++ b/js/viewjs/about.js @@ -1,10 +1,20 @@ -$('[data-toggle="collapse-next"]').on("click", function(e) +function aboutView(Grocy, scope = null) { - e.preventDefault(); - $(this).parent().next().collapse("toggle"); -}); + var $scope = $; + if (scope != null) + { + $scope = $(scope).find; + } -if ((typeof GetUriParam("tab") !== "undefined" && GetUriParam("tab") === "changelog")) -{ - $(".nav-tabs a[href='#changelog']").tab("show"); + $('[data-toggle="collapse-next"]').on("click", function(e) + { + e.preventDefault(); + $(this).parent().next().collapse("toggle"); + }); + + if ((typeof GetUriParam("tab") !== "undefined" && GetUriParam("tab") === "changelog")) + { + $(".nav-tabs a[href='#changelog']").tab("show"); + } + } diff --git a/js/viewjs/barcodescannertesting.js b/js/viewjs/barcodescannertesting.js index d208867e..52104d67 100644 --- a/js/viewjs/barcodescannertesting.js +++ b/js/viewjs/barcodescannertesting.js @@ -1,97 +1,107 @@ -Grocy.Use("barcodescanner"); -Grocy.BarCodeScannerTestingHitCount = 0; -Grocy.BarCodeScannerTestingMissCount = 0; - -$("#scanned_barcode").on("blur", function(e) +function barcodescannertestingView(Grocy, scope = null) { - OnBarcodeScanned($("#scanned_barcode").val()); -}); - -$("#scanned_barcode").keydown(function(event) -{ - if (event.keyCode === 13) //Enter + var $scope = $; + if (scope != null) + { + $scope = $(scope).find; + } + + Grocy.Use("barcodescanner"); + Grocy.BarCodeScannerTestingHitCount = 0; + Grocy.BarCodeScannerTestingMissCount = 0; + + $("#scanned_barcode").on("blur", function(e) { - event.preventDefault(); OnBarcodeScanned($("#scanned_barcode").val()); - } -}); - -$("#expected_barcode").on("keyup", function(e) -{ - if ($("#expected_barcode").val().length > 1) + }); + + $("#scanned_barcode").keydown(function(event) { - $("#scanned_barcode").removeAttr("disabled"); - $("#barcodescanner-start-button").removeAttr("disabled"); - $("#barcodescanner-start-button").removeClass("disabled"); - } - else + if (event.keyCode === 13) //Enter + { + event.preventDefault(); + OnBarcodeScanned($("#scanned_barcode").val()); + } + }); + + $("#expected_barcode").on("keyup", function(e) + { + if ($("#expected_barcode").val().length > 1) + { + $("#scanned_barcode").removeAttr("disabled"); + $("#barcodescanner-start-button").removeAttr("disabled"); + $("#barcodescanner-start-button").removeClass("disabled"); + } + else + { + $("#scanned_barcode").attr("disabled", ""); + $("#barcodescanner-start-button").attr("disabled", ""); + $("#barcodescanner-start-button").addClass("disabled"); + } + }); + + $("#expected_barcode").focus(); + setTimeout(function() { - $("#scanned_barcode").attr("disabled", ""); $("#barcodescanner-start-button").attr("disabled", ""); $("#barcodescanner-start-button").addClass("disabled"); - } -}); - -$("#expected_barcode").focus(); -setTimeout(function() -{ - $("#barcodescanner-start-button").attr("disabled", ""); - $("#barcodescanner-start-button").addClass("disabled"); -}, 200); - -if (GetUriParam("barcode") !== undefined) -{ - $("#expected_barcode").val(GetUriParam("barcode")); - setTimeout(function() - { - $("#expected_barcode").keyup(); - $("#scanned_barcode").focus(); }, 200); -} - -function OnBarcodeScanned(barcode) -{ - if (barcode.length === 0) + + if (GetUriParam("barcode") !== undefined) { - return; - } - - var bgClass = ""; - if (barcode != $("#expected_barcode").val()) - { - Grocy.BarCodeScannerTestingMissCount++; - bgClass = "bg-danger"; - - $("#miss-count").text(Grocy.BarCodeScannerTestingMissCount); - animateCSS("#miss-count", "pulse"); - } - else - { - Grocy.BarCodeScannerTestingHitCount++; - bgClass = "bg-success"; - - $("#hit-count").text(Grocy.BarCodeScannerTestingHitCount); - animateCSS("#hit-count", "pulse"); - } - - $("#scanned_codes").prepend(""); - setTimeout(function() - { - $("#scanned_barcode").val(""); - - if (!$(":focus").is($("#expected_barcode"))) + $("#expected_barcode").val(GetUriParam("barcode")); + setTimeout(function() { + $("#expected_barcode").keyup(); $("#scanned_barcode").focus(); - } - }, 200); -} - -$(document).on("Grocy.BarcodeScanned", function(e, barcode, target) -{ - if (target !== "#scanned_barcode") - { - return; + }, 200); } - - OnBarcodeScanned(barcode); -}); + + function OnBarcodeScanned(barcode) + { + if (barcode.length === 0) + { + return; + } + + var bgClass = ""; + if (barcode != $("#expected_barcode").val()) + { + Grocy.BarCodeScannerTestingMissCount++; + bgClass = "bg-danger"; + + $("#miss-count").text(Grocy.BarCodeScannerTestingMissCount); + animateCSS("#miss-count", "pulse"); + } + else + { + Grocy.BarCodeScannerTestingHitCount++; + bgClass = "bg-success"; + + $("#hit-count").text(Grocy.BarCodeScannerTestingHitCount); + animateCSS("#hit-count", "pulse"); + } + + $("#scanned_codes").prepend(""); + setTimeout(function() + { + $("#scanned_barcode").val(""); + + if (!$(":focus").is($("#expected_barcode"))) + { + $("#scanned_barcode").focus(); + } + }, 200); + } + + $(document).on("Grocy.BarcodeScanned", function(e, barcode, target) + { + if (target !== "#scanned_barcode") + { + return; + } + + OnBarcodeScanned(barcode); + }); + +} diff --git a/js/viewjs/batteries.js b/js/viewjs/batteries.js index dfef8a34..334c782c 100644 --- a/js/viewjs/batteries.js +++ b/js/viewjs/batteries.js @@ -1,41 +1,51 @@ -var batteriesTable = $('#batteries-table').DataTable({ - 'order': [[1, 'asc']], - 'columnDefs': [ - { 'orderable': false, 'targets': 0 }, - { 'searchable': false, "targets": 0 }, - { "type": "num", "targets": 4 } - ].concat($.fn.dataTable.defaults.columnDefs) -}); -$('#batteries-table tbody').removeClass("d-none"); -Grocy.FrontendHelpers.InitDataTable(batteriesTable, null, function() +function batteriesView(Grocy, scope = null) { - $("#search").val(""); - batteriesTable.search("").draw(); - $("#show-disabled").prop('checked', false); -}); - -Grocy.FrontendHelpers.MakeDeleteConfirmBox( - 'Are you sure to delete battery "%s"?', - '.battery-delete-button', - 'data-battery-name', - 'data-battery-id', - 'objects/batteries/', - '/batteries' -); - -$("#show-disabled").change(function() -{ - if (this.checked) + var $scope = $; + if (scope != null) { - window.location.href = U('/batteries?include_disabled'); + $scope = $(scope).find; } - else - { - window.location.href = U('/batteries'); - } -}); -if (GetUriParam('include_disabled')) -{ - $("#show-disabled").prop('checked', true); + var batteriesTable = $('#batteries-table').DataTable({ + 'order': [[1, 'asc']], + 'columnDefs': [ + { 'orderable': false, 'targets': 0 }, + { 'searchable': false, "targets": 0 }, + { "type": "num", "targets": 4 } + ].concat($.fn.dataTable.defaults.columnDefs) + }); + $('#batteries-table tbody').removeClass("d-none"); + Grocy.FrontendHelpers.InitDataTable(batteriesTable, null, function() + { + $("#search").val(""); + batteriesTable.search("").draw(); + $("#show-disabled").prop('checked', false); + }); + + Grocy.FrontendHelpers.MakeDeleteConfirmBox( + 'Are you sure to delete battery "%s"?', + '.battery-delete-button', + 'data-battery-name', + 'data-battery-id', + 'objects/batteries/', + '/batteries' + ); + + $("#show-disabled").change(function() + { + if (this.checked) + { + window.location.href = U('/batteries?include_disabled'); + } + else + { + window.location.href = U('/batteries'); + } + }); + + if (GetUriParam('include_disabled')) + { + $("#show-disabled").prop('checked', true); + } + } diff --git a/js/viewjs/batteriesjournal.js b/js/viewjs/batteriesjournal.js index 6d5d3e70..9e64f6f2 100644 --- a/js/viewjs/batteriesjournal.js +++ b/js/viewjs/batteriesjournal.js @@ -1,40 +1,50 @@ -var batteriesJournalTable = $('#batteries-journal-table').DataTable({ - 'paginate': true, - 'order': [[2, 'desc']], - 'columnDefs': [ - { 'orderable': false, 'targets': 0 }, - { 'searchable': false, "targets": 0 } - ].concat($.fn.dataTable.defaults.columnDefs) -}); -$('#batteries-journal-table tbody').removeClass("d-none"); -Grocy.FrontendHelpers.InitDataTable(batteriesJournalTable); -Grocy.FrontendHelpers.MakeFilterForColumn("#battery-filter", 1, batteriesJournalTable); - -if (typeof GetUriParam("battery") !== "undefined") +function batteriesjournalView(Grocy, scope = null) { - $("#battery-filter").val(GetUriParam("battery")); - $("#battery-filter").trigger("change"); + var $scope = $; + if (scope != null) + { + $scope = $(scope).find; + } + + var batteriesJournalTable = $('#batteries-journal-table').DataTable({ + 'paginate': true, + 'order': [[2, 'desc']], + 'columnDefs': [ + { 'orderable': false, 'targets': 0 }, + { 'searchable': false, "targets": 0 } + ].concat($.fn.dataTable.defaults.columnDefs) + }); + $('#batteries-journal-table tbody').removeClass("d-none"); + Grocy.FrontendHelpers.InitDataTable(batteriesJournalTable); + Grocy.FrontendHelpers.MakeFilterForColumn("#battery-filter", 1, batteriesJournalTable); + + if (typeof GetUriParam("battery") !== "undefined") + { + $("#battery-filter").val(GetUriParam("battery")); + $("#battery-filter").trigger("change"); + } + + $(document).on('click', '.undo-battery-execution-button', function(e) + { + e.preventDefault(); + + var element = $(e.currentTarget); + var chargeCycleId = $(e.currentTarget).attr('data-charge-cycle-id'); + + Grocy.Api.Post('batteries/charge-cycles/' + chargeCycleId.toString() + '/undo', {}, + function(result) + { + element.closest("tr").addClass("text-muted"); + element.parent().siblings().find("span.name-anchor").addClass("text-strike-through").after("
" + __t("Undone on") + " " + moment().format("YYYY-MM-DD HH:mm:ss") + " "); + element.closest(".undo-battery-execution-button").addClass("disabled"); + RefreshContextualTimeago("#charge-cycle-" + chargeCycleId + "-row"); + toastr.success(__t("Charge cycle successfully undone")); + }, + function(xhr) + { + console.error(xhr); + } + ); + }); + } - -$(document).on('click', '.undo-battery-execution-button', function(e) -{ - e.preventDefault(); - - var element = $(e.currentTarget); - var chargeCycleId = $(e.currentTarget).attr('data-charge-cycle-id'); - - Grocy.Api.Post('batteries/charge-cycles/' + chargeCycleId.toString() + '/undo', {}, - function(result) - { - element.closest("tr").addClass("text-muted"); - element.parent().siblings().find("span.name-anchor").addClass("text-strike-through").after("
" + __t("Undone on") + " " + moment().format("YYYY-MM-DD HH:mm:ss") + " "); - element.closest(".undo-battery-execution-button").addClass("disabled"); - RefreshContextualTimeago("#charge-cycle-" + chargeCycleId + "-row"); - toastr.success(__t("Charge cycle successfully undone")); - }, - function(xhr) - { - console.error(xhr); - } - ); -}); diff --git a/js/viewjs/batteriesoverview.js b/js/viewjs/batteriesoverview.js index b891ca41..be0bbd53 100644 --- a/js/viewjs/batteriesoverview.js +++ b/js/viewjs/batteriesoverview.js @@ -1,122 +1,132 @@ -Grocy.Use("batterycard"); - -var batteriesOverviewTable = $('#batteries-overview-table').DataTable({ - 'order': [[4, 'asc']], - 'columnDefs': [ - { 'orderable': false, 'targets': 0 }, - { 'searchable': false, "targets": 0 }, - { "type": "html", "targets": 3 }, - { "type": "html", "targets": 4 } - ].concat($.fn.dataTable.defaults.columnDefs) -}); -$('#batteries-overview-table tbody').removeClass("d-none"); - -Grocy.FrontendHelpers.InitDataTable(batteriesOverviewTable); -Grocy.FrontendHelpers.MakeStatusFilter(batteriesOverviewTable, 5); - -$(document).on('click', '.track-charge-cycle-button', function(e) +function batteriesoverviewView(Grocy, scope = null) { - e.preventDefault(); + var $scope = $; + if (scope != null) + { + $scope = $(scope).find; + } - // Remove the focus from the current button - // to prevent that the tooltip stays until clicked anywhere else - document.activeElement.blur(); - - Grocy.FrontendHelpers.BeginUiBusy(); - - 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.Post('batteries/' + batteryId + '/charge', { 'tracked_time': trackedTime }, - function() - { - Grocy.Api.Get('batteries/' + batteryId, - function(result) - { - var batteryRow = $('#battery-' + batteryId + '-row'); - var nextXDaysThreshold = moment().add($("#info-due-batteries").data("next-x-days"), "days"); - var now = moment(); - var nextExecutionTime = moment(result.next_estimated_charge_time); - - batteryRow.removeClass("table-warning"); - batteryRow.removeClass("table-danger"); - if (nextExecutionTime.isBefore(now)) - { - batteryRow.addClass("table-danger"); - } - else if (nextExecutionTime.isBefore(nextXDaysThreshold)) - { - batteryRow.addClass("table-warning"); - } - - animateCSS("#battery-" + batteryId + "-row td:not(:first)", "shake"); - - $('#battery-' + batteryId + '-last-tracked-time').text(trackedTime); - $('#battery-' + batteryId + '-last-tracked-time-timeago').attr('datetime', trackedTime); - if (result.battery.charge_interval_days != 0) - { - $('#battery-' + batteryId + '-next-charge-time').text(result.next_estimated_charge_time); - $('#battery-' + batteryId + '-next-charge-time-timeago').attr('datetime', result.next_estimated_charge_time); - } - - Grocy.FrontendHelpers.EndUiBusy(); - toastr.success(__t('Tracked charge cycle of battery %1$s on %2$s', batteryName, trackedTime)); - RefreshContextualTimeago("#battery-" + batteryId + "-row"); - RefreshStatistics(); - }, - function(xhr) - { - Grocy.FrontendHelpers.EndUiBusy(); - console.error(xhr); - } - ); - }, - function(xhr) - { - Grocy.FrontendHelpers.EndUiBusy(); - console.error(xhr); - } - ); -}); - -$(document).on("click", ".battery-name-cell", function(e) -{ - Grocy.Components.BatteryCard.Refresh($(e.currentTarget).attr("data-battery-id")); - $("#batteriesoverview-batterycard-modal").modal("show"); -}); - -function RefreshStatistics() -{ - var nextXDays = $("#info-due-batteries").data("next-x-days"); - Grocy.Api.Get('batteries', - function(result) - { - var dueCount = 0; - var overdueCount = 0; - var now = moment(); - var nextXDaysThreshold = moment().add(nextXDays, "days"); - result.forEach(element => + Grocy.Use("batterycard"); + + var batteriesOverviewTable = $('#batteries-overview-table').DataTable({ + 'order': [[4, 'asc']], + 'columnDefs': [ + { 'orderable': false, 'targets': 0 }, + { 'searchable': false, "targets": 0 }, + { "type": "html", "targets": 3 }, + { "type": "html", "targets": 4 } + ].concat($.fn.dataTable.defaults.columnDefs) + }); + $('#batteries-overview-table tbody').removeClass("d-none"); + + Grocy.FrontendHelpers.InitDataTable(batteriesOverviewTable); + Grocy.FrontendHelpers.MakeStatusFilter(batteriesOverviewTable, 5); + + $(document).on('click', '.track-charge-cycle-button', function(e) + { + e.preventDefault(); + + // Remove the focus from the current button + // to prevent that the tooltip stays until clicked anywhere else + document.activeElement.blur(); + + Grocy.FrontendHelpers.BeginUiBusy(); + + 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.Post('batteries/' + batteryId + '/charge', { 'tracked_time': trackedTime }, + function() { - var date = moment(element.next_estimated_charge_time); - if (date.isBefore(now)) + Grocy.Api.Get('batteries/' + batteryId, + function(result) + { + var batteryRow = $('#battery-' + batteryId + '-row'); + var nextXDaysThreshold = moment().add($("#info-due-batteries").data("next-x-days"), "days"); + var now = moment(); + var nextExecutionTime = moment(result.next_estimated_charge_time); + + batteryRow.removeClass("table-warning"); + batteryRow.removeClass("table-danger"); + if (nextExecutionTime.isBefore(now)) + { + batteryRow.addClass("table-danger"); + } + else if (nextExecutionTime.isBefore(nextXDaysThreshold)) + { + batteryRow.addClass("table-warning"); + } + + animateCSS("#battery-" + batteryId + "-row td:not(:first)", "shake"); + + $('#battery-' + batteryId + '-last-tracked-time').text(trackedTime); + $('#battery-' + batteryId + '-last-tracked-time-timeago').attr('datetime', trackedTime); + if (result.battery.charge_interval_days != 0) + { + $('#battery-' + batteryId + '-next-charge-time').text(result.next_estimated_charge_time); + $('#battery-' + batteryId + '-next-charge-time-timeago').attr('datetime', result.next_estimated_charge_time); + } + + Grocy.FrontendHelpers.EndUiBusy(); + toastr.success(__t('Tracked charge cycle of battery %1$s on %2$s', batteryName, trackedTime)); + RefreshContextualTimeago("#battery-" + batteryId + "-row"); + RefreshStatistics(); + }, + function(xhr) + { + Grocy.FrontendHelpers.EndUiBusy(); + console.error(xhr); + } + ); + }, + function(xhr) + { + Grocy.FrontendHelpers.EndUiBusy(); + console.error(xhr); + } + ); + }); + + $(document).on("click", ".battery-name-cell", function(e) + { + Grocy.Components.BatteryCard.Refresh($(e.currentTarget).attr("data-battery-id")); + $("#batteriesoverview-batterycard-modal").modal("show"); + }); + + function RefreshStatistics() + { + var nextXDays = $("#info-due-batteries").data("next-x-days"); + Grocy.Api.Get('batteries', + function(result) + { + var dueCount = 0; + var overdueCount = 0; + var now = moment(); + var nextXDaysThreshold = moment().add(nextXDays, "days"); + result.forEach(element => { - overdueCount++; - } - else if (date.isBefore(nextXDaysThreshold)) - { - dueCount++; - } - }); - - $("#info-due-batteries").html('' + dueCount + ' ' + __n(dueCount, '%s battery is due to be charged', '%s batteries are due to be charged') + ' ' + __n(nextXDays, 'within the next day', 'within the next %s days')); - $("#info-overdue-batteries").html('' + overdueCount + ' ' + __n(overdueCount, '%s battery is overdue to be charged', '%s batteries are overdue to be charged')); - }, - function(xhr) - { - console.error(xhr); - } - ); + var date = moment(element.next_estimated_charge_time); + if (date.isBefore(now)) + { + overdueCount++; + } + else if (date.isBefore(nextXDaysThreshold)) + { + dueCount++; + } + }); + + $("#info-due-batteries").html('' + dueCount + ' ' + __n(dueCount, '%s battery is due to be charged', '%s batteries are due to be charged') + ' ' + __n(nextXDays, 'within the next day', 'within the next %s days')); + $("#info-overdue-batteries").html('' + overdueCount + ' ' + __n(overdueCount, '%s battery is overdue to be charged', '%s batteries are overdue to be charged')); + }, + function(xhr) + { + console.error(xhr); + } + ); + } + + RefreshStatistics(); + } - -RefreshStatistics(); diff --git a/js/viewjs/batteriessettings.js b/js/viewjs/batteriessettings.js index b0b44a17..e2651692 100644 --- a/js/viewjs/batteriessettings.js +++ b/js/viewjs/batteriessettings.js @@ -1,5 +1,15 @@ -Grocy.Use("numberpicker"); +function batteriessettingsView(Grocy, scope = null) +{ + var $scope = $; + if (scope != null) + { + $scope = $(scope).find; + } -$("#batteries_due_soon_days").val(Grocy.UserSettings.batteries_due_soon_days); - -RefreshLocaleNumberInput(); + Grocy.Use("numberpicker"); + + $("#batteries_due_soon_days").val(Grocy.UserSettings.batteries_due_soon_days); + + RefreshLocaleNumberInput(); + +} diff --git a/js/viewjs/batteryform.js b/js/viewjs/batteryform.js index ebbfe45e..116e5d97 100644 --- a/js/viewjs/batteryform.js +++ b/js/viewjs/batteryform.js @@ -1,93 +1,103 @@ -import { WindowMessageBag } from '../helpers/messagebag'; - -Grocy.Use("numberpicker"); -Grocy.Use("userfieldsform"); - -$('#save-battery-button').on('click', function(e) +function batteryformView(Grocy, scope = null) { - e.preventDefault(); - - if ($(".combobox-menu-visible").length) + var $scope = $; + if (scope != null) { - return; + $scope = $(scope).find; } - var jsonData = $('#battery-form').serializeJSON(); - Grocy.FrontendHelpers.BeginUiBusy("battery-form"); - - if (Grocy.EditMode === 'create') + import { WindowMessageBag } from '../helpers/messagebag'; + + Grocy.Use("numberpicker"); + Grocy.Use("userfieldsform"); + + $('#save-battery-button').on('click', function(e) { - Grocy.Api.Post('objects/batteries', jsonData, - function(result) - { - Grocy.EditObjectId = result.created_object_id; - Grocy.Components.UserfieldsForm.Save(function() - { - if (GetUriParam("embedded") !== undefined) - { - window.parent.postMessage(WindowMessageBag("Reload"), Grocy.BaseUrl); - } - else - { - window.location.href = U('/batteries'); - } - }); - }, - function(xhr) - { - Grocy.FrontendHelpers.EndUiBusy("battery-form"); - Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response) - } - ); - } - else - { - Grocy.Api.Put('objects/batteries/' + Grocy.EditObjectId, jsonData, - function(result) - { - Grocy.Components.UserfieldsForm.Save(function() - { - if (GetUriParam("embedded") !== undefined) - { - window.parent.postMessage(WindowMessageBag("Reload"), Grocy.BaseUrl); - } - else - { - window.location.href = U('/batteries'); - } - }); - }, - function(xhr) - { - Grocy.FrontendHelpers.EndUiBusy("battery-form"); - Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response) - } - ); - } -}); - -$('#battery-form input').keyup(function(event) -{ - Grocy.FrontendHelpers.ValidateForm('battery-form'); -}); - -$('#battery-form input').keydown(function(event) -{ - if (event.keyCode === 13) //Enter - { - event.preventDefault(); - - if (document.getElementById('battery-form').checkValidity() === false) //There is at least one validation error + e.preventDefault(); + + if ($(".combobox-menu-visible").length) { - return false; + return; + } + + var jsonData = $('#battery-form').serializeJSON(); + Grocy.FrontendHelpers.BeginUiBusy("battery-form"); + + if (Grocy.EditMode === 'create') + { + Grocy.Api.Post('objects/batteries', jsonData, + function(result) + { + Grocy.EditObjectId = result.created_object_id; + Grocy.Components.UserfieldsForm.Save(function() + { + if (GetUriParam("embedded") !== undefined) + { + window.parent.postMessage(WindowMessageBag("Reload"), Grocy.BaseUrl); + } + else + { + window.location.href = U('/batteries'); + } + }); + }, + function(xhr) + { + Grocy.FrontendHelpers.EndUiBusy("battery-form"); + Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response) + } + ); } else { - $('#save-battery-button').click(); + Grocy.Api.Put('objects/batteries/' + Grocy.EditObjectId, jsonData, + function(result) + { + Grocy.Components.UserfieldsForm.Save(function() + { + if (GetUriParam("embedded") !== undefined) + { + window.parent.postMessage(WindowMessageBag("Reload"), Grocy.BaseUrl); + } + else + { + window.location.href = U('/batteries'); + } + }); + }, + function(xhr) + { + Grocy.FrontendHelpers.EndUiBusy("battery-form"); + Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response) + } + ); } - } -}); - -Grocy.Components.UserfieldsForm.Load(); -$('#name').focus(); -Grocy.FrontendHelpers.ValidateForm('battery-form'); + }); + + $('#battery-form input').keyup(function(event) + { + Grocy.FrontendHelpers.ValidateForm('battery-form'); + }); + + $('#battery-form input').keydown(function(event) + { + if (event.keyCode === 13) //Enter + { + event.preventDefault(); + + if (document.getElementById('battery-form').checkValidity() === false) //There is at least one validation error + { + return false; + } + else + { + $('#save-battery-button').click(); + } + } + }); + + Grocy.Components.UserfieldsForm.Load(); + $('#name').focus(); + Grocy.FrontendHelpers.ValidateForm('battery-form'); + +} diff --git a/js/viewjs/batterytracking.js b/js/viewjs/batterytracking.js index 901ee912..0410b6b7 100644 --- a/js/viewjs/batterytracking.js +++ b/js/viewjs/batterytracking.js @@ -1,103 +1,113 @@ -Grocy.Use("batterycard"); -Grocy.Use("datetimepicker"); - -$('#save-batterytracking-button').on('click', function(e) +function batterytrackingView(Grocy, scope = null) { - e.preventDefault(); - - if ($(".combobox-menu-visible").length) + var $scope = $; + if (scope != null) { - return; + $scope = $(scope).find; } - var jsonForm = $('#batterytracking-form').serializeJSON(); - Grocy.FrontendHelpers.BeginUiBusy("batterytracking-form"); - - Grocy.Api.Get('batteries/' + jsonForm.battery_id, - function(batteryDetails) - { - Grocy.Api.Post('batteries/' + jsonForm.battery_id + '/charge', { 'tracked_time': $('#tracked_time').find('input').val() }, - function(result) - { - Grocy.FrontendHelpers.EndUiBusy("batterytracking-form"); - toastr.success(__t('Tracked charge cycle of battery %1$s on %2$s', batteryDetails.battery.name, $('#tracked_time').find('input').val()) + '
' + __t("Undo") + ''); - Grocy.Components.BatteryCard.Refresh($('#battery_id').val()); - - $('#battery_id').val(''); - $('#battery_id_text_input').focus(); - $('#battery_id_text_input').val(''); - $('#tracked_time').find('input').val(moment().format('YYYY-MM-DD HH:mm:ss')); - $('#tracked_time').find('input').trigger('change'); - $('#battery_id_text_input').trigger('change'); - Grocy.FrontendHelpers.ValidateForm('batterytracking-form'); - }, - function(xhr) - { - Grocy.FrontendHelpers.EndUiBusy("batterytracking-form"); - console.error(xhr); - } - ); - }, - function(xhr) - { - Grocy.FrontendHelpers.EndUiBusy("batterytracking-form"); - console.error(xhr); - } - ); -}); - -$('#battery_id').on('change', function(e) -{ - var input = $('#battery_id_text_input').val().toString(); - $('#battery_id_text_input').val(input); - $('#battery_id').data('combobox').refresh(); - - var batteryId = $(e.target).val(); - if (batteryId) + Grocy.Use("batterycard"); + Grocy.Use("datetimepicker"); + + $('#save-batterytracking-button').on('click', function(e) + { + e.preventDefault(); + + if ($(".combobox-menu-visible").length) + { + return; + } + + var jsonForm = $('#batterytracking-form').serializeJSON(); + Grocy.FrontendHelpers.BeginUiBusy("batterytracking-form"); + + Grocy.Api.Get('batteries/' + jsonForm.battery_id, + function(batteryDetails) + { + Grocy.Api.Post('batteries/' + jsonForm.battery_id + '/charge', { 'tracked_time': $('#tracked_time').find('input').val() }, + function(result) + { + Grocy.FrontendHelpers.EndUiBusy("batterytracking-form"); + toastr.success(__t('Tracked charge cycle of battery %1$s on %2$s', batteryDetails.battery.name, $('#tracked_time').find('input').val()) + '
' + __t("Undo") + ''); + Grocy.Components.BatteryCard.Refresh($('#battery_id').val()); + + $('#battery_id').val(''); + $('#battery_id_text_input').focus(); + $('#battery_id_text_input').val(''); + $('#tracked_time').find('input').val(moment().format('YYYY-MM-DD HH:mm:ss')); + $('#tracked_time').find('input').trigger('change'); + $('#battery_id_text_input').trigger('change'); + Grocy.FrontendHelpers.ValidateForm('batterytracking-form'); + }, + function(xhr) + { + Grocy.FrontendHelpers.EndUiBusy("batterytracking-form"); + console.error(xhr); + } + ); + }, + function(xhr) + { + Grocy.FrontendHelpers.EndUiBusy("batterytracking-form"); + console.error(xhr); + } + ); + }); + + $('#battery_id').on('change', function(e) + { + var input = $('#battery_id_text_input').val().toString(); + $('#battery_id_text_input').val(input); + $('#battery_id').data('combobox').refresh(); + + var batteryId = $(e.target).val(); + if (batteryId) + { + Grocy.Components.BatteryCard.Refresh(batteryId); + $('#tracked_time').find('input').focus(); + Grocy.FrontendHelpers.ValidateForm('batterytracking-form'); + } + }); + + $('.combobox').combobox({ + appendId: '_text_input', + bsVersion: '4' + }); + + $('#battery_id').val(''); + $('#battery_id_text_input').focus(); + $('#battery_id_text_input').val(''); + $('#battery_id_text_input').trigger('change'); + Grocy.Components.DateTimePicker.GetInputElement().trigger('input'); + Grocy.FrontendHelpers.ValidateForm('batterytracking-form'); + + $('#batterytracking-form input').keyup(function(event) { - Grocy.Components.BatteryCard.Refresh(batteryId); - $('#tracked_time').find('input').focus(); Grocy.FrontendHelpers.ValidateForm('batterytracking-form'); - } -}); - -$('.combobox').combobox({ - appendId: '_text_input', - bsVersion: '4' -}); - -$('#battery_id').val(''); -$('#battery_id_text_input').focus(); -$('#battery_id_text_input').val(''); -$('#battery_id_text_input').trigger('change'); -Grocy.Components.DateTimePicker.GetInputElement().trigger('input'); -Grocy.FrontendHelpers.ValidateForm('batterytracking-form'); - -$('#batterytracking-form input').keyup(function(event) -{ - Grocy.FrontendHelpers.ValidateForm('batterytracking-form'); -}); - -$('#batterytracking-form input').keydown(function(event) -{ - if (event.keyCode === 13) //Enter + }); + + $('#batterytracking-form input').keydown(function(event) { - event.preventDefault(); - - if (document.getElementById('batterytracking-form').checkValidity() === false) //There is at least one validation error + if (event.keyCode === 13) //Enter { - return false; + event.preventDefault(); + + if (document.getElementById('batterytracking-form').checkValidity() === false) //There is at least one validation error + { + return false; + } + else + { + $('#save-batterytracking-button').click(); + } } - else - { - $('#save-batterytracking-button').click(); - } - } -}); - -$('#tracked_time').find('input').on('keypress', function(e) -{ - Grocy.FrontendHelpers.ValidateForm('batterytracking-form'); -}); - - + }); + + $('#tracked_time').find('input').on('keypress', function(e) + { + Grocy.FrontendHelpers.ValidateForm('batterytracking-form'); + }); + + + +} diff --git a/js/viewjs/calendar.js b/js/viewjs/calendar.js index 8cea570a..ea0e2a1a 100644 --- a/js/viewjs/calendar.js +++ b/js/viewjs/calendar.js @@ -1,92 +1,102 @@ -/* global fullcalendarEventSources */ - -import { Calendar } from '@fullcalendar/core'; -import dayGridPlugin from '@fullcalendar/daygrid'; -import bootstrapPlugin from '@fullcalendar/bootstrap'; -import listPlugin from '@fullcalendar/list'; -import timeGridPlugin from '@fullcalendar/timegrid'; -import { QrCodeImgHtml } from "../helpers/qrcode"; - -import '@fullcalendar/core/main.css'; -import '@fullcalendar/daygrid/main.css'; -import '@fullcalendar/timegrid/main.css'; -import '@fullcalendar/list/main.css'; -import '@fullcalendar/bootstrap/main.css'; - -var calendarOptions = { - plugins: [bootstrapPlugin, dayGridPlugin, listPlugin, timeGridPlugin], - themeSystem: "bootstrap", - header: { - left: "dayGridMonth,timeGridWeek,timeGridDay,listWeek", - center: "title", - right: "prev,next" - }, - weekNumbers: Grocy.CalendarShowWeekNumbers, - defaultView: ($(window).width() < 768) ? "timeGridDay" : "dayGridMonth", - firstDay: firstDay, - eventLimit: false, - height: "auto", - events: fullcalendarEventSources, - // fullcalendar 4 doesn't translate the default view names (?) - // so we have to supply our own. - views: { - dayGridMonth: { buttonText: __t("Month") }, - timeGridWeek: { buttonText: __t("Week") }, - timeGridDay: { buttonText: __t("Day") }, - listWeek: { buttonText: __t("List") } - }, - eventClick: function(info) +function calendarView(Grocy, scope = null) +{ + var $scope = $; + if (scope != null) { - window.location.href = info.link; + $scope = $(scope).find; } -}; -if (__t('fullcalendar_locale').replace(" ", "") !== "" && __t('fullcalendar_locale') != 'x') -{ - $.getScript(U('/js/locales/fullcalendar-core/' + __t('fullcalendar_locale') + '.js')); - calendarOptions.locale = __t('fullcalendar_locale'); -} - -var firstDay = null; -if (!Grocy.CalendarFirstDayOfWeek.isEmpty()) -{ - firstDay = parseInt(Grocy.CalendarFirstDayOfWeek); -} - -var calendar = new Calendar(document.getElementById("calendar"), calendarOptions); -calendar.render(); - -$("#ical-button").on("click", function(e) -{ - e.preventDefault(); - - Grocy.Api.Get('calendar/ical/sharing-link', - function(result) - { - bootbox.alert({ - title: __t('Share/Integrate calendar (iCal)'), - message: __t('Use the following (public) URL to share or integrate the calendar in iCal format') + '

' - + QrCodeImgHtml(result.url) + "

", - closeButton: false - }); + /* global fullcalendarEventSources */ + + import { Calendar } from '@fullcalendar/core'; + import dayGridPlugin from '@fullcalendar/daygrid'; + import bootstrapPlugin from '@fullcalendar/bootstrap'; + import listPlugin from '@fullcalendar/list'; + import timeGridPlugin from '@fullcalendar/timegrid'; + import { QrCodeImgHtml } from "../helpers/qrcode"; + + import '@fullcalendar/core/main.css'; + import '@fullcalendar/daygrid/main.css'; + import '@fullcalendar/timegrid/main.css'; + import '@fullcalendar/list/main.css'; + import '@fullcalendar/bootstrap/main.css'; + + var calendarOptions = { + plugins: [bootstrapPlugin, dayGridPlugin, listPlugin, timeGridPlugin], + themeSystem: "bootstrap", + header: { + left: "dayGridMonth,timeGridWeek,timeGridDay,listWeek", + center: "title", + right: "prev,next" }, - function(xhr) + weekNumbers: Grocy.CalendarShowWeekNumbers, + defaultView: ($(window).width() < 768) ? "timeGridDay" : "dayGridMonth", + firstDay: firstDay, + eventLimit: false, + height: "auto", + events: fullcalendarEventSources, + // fullcalendar 4 doesn't translate the default view names (?) + // so we have to supply our own. + views: { + dayGridMonth: { buttonText: __t("Month") }, + timeGridWeek: { buttonText: __t("Week") }, + timeGridDay: { buttonText: __t("Day") }, + listWeek: { buttonText: __t("List") } + }, + eventClick: function(info) { - console.error(xhr); + window.location.href = info.link; } - ); -}); - -$(window).one("resize", function() -{ - // Automatically switch the calendar to "basicDay" view on small screens - // and to "month" otherwise - if ($(window).width() < 768) + }; + + if (__t('fullcalendar_locale').replace(" ", "") !== "" && __t('fullcalendar_locale') != 'x') { - calendar.changeView("timeGridDay"); + $.getScript(U('/js/locales/fullcalendar-core/' + __t('fullcalendar_locale') + '.js')); + calendarOptions.locale = __t('fullcalendar_locale'); } - else + + var firstDay = null; + if (!Grocy.CalendarFirstDayOfWeek.isEmpty()) { - calendar.changeView("dayGridMonth"); + firstDay = parseInt(Grocy.CalendarFirstDayOfWeek); } -}); + + var calendar = new Calendar(document.getElementById("calendar"), calendarOptions); + calendar.render(); + + $("#ical-button").on("click", function(e) + { + e.preventDefault(); + + Grocy.Api.Get('calendar/ical/sharing-link', + function(result) + { + bootbox.alert({ + title: __t('Share/Integrate calendar (iCal)'), + message: __t('Use the following (public) URL to share or integrate the calendar in iCal format') + '

' + + QrCodeImgHtml(result.url) + "

", + closeButton: false + }); + }, + function(xhr) + { + console.error(xhr); + } + ); + }); + + $(window).one("resize", function() + { + // Automatically switch the calendar to "basicDay" view on small screens + // and to "month" otherwise + if ($(window).width() < 768) + { + calendar.changeView("timeGridDay"); + } + else + { + calendar.changeView("dayGridMonth"); + } + }); + +} diff --git a/js/viewjs/choreform.js b/js/viewjs/choreform.js index c562dc9d..7fec5809 100644 --- a/js/viewjs/choreform.js +++ b/js/viewjs/choreform.js @@ -1,242 +1,252 @@ -Grocy.Use("numberpicker"); -Grocy.Use("userfieldsform"); - -$('#save-chore-button').on('click', function(e) +function choreformView(Grocy, scope = null) { - e.preventDefault(); - - if ($(".combobox-menu-visible").length) + var $scope = $; + if (scope != null) { - return; + $scope = $(scope).find; } - var jsonData = $('#chore-form').serializeJSON(); - if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_CHORES_ASSIGNMENTS) + Grocy.Use("numberpicker"); + Grocy.Use("userfieldsform"); + + $('#save-chore-button').on('click', function(e) { - jsonData.assignment_config = $("#assignment_config").val().join(","); - } - - Grocy.FrontendHelpers.BeginUiBusy("chore-form"); - - if (Grocy.EditMode === 'create') - { - Grocy.Api.Post('objects/chores', jsonData, - function(result) - { - Grocy.EditObjectId = result.created_object_id; - Grocy.Components.UserfieldsForm.Save(function() - { - Grocy.Api.Post('chores/executions/calculate-next-assignments', { "chore_id": Grocy.EditObjectId }, - function(result) - { - window.location.href = U('/chores'); - }, - function(xhr) - { - Grocy.FrontendHelpers.EndUiBusy(); - console.error(xhr); - } - ); - }); - }, - function(xhr) - { - Grocy.FrontendHelpers.EndUiBusy("chore-form"); - Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response) - } - ); - } - else - { - Grocy.Api.Put('objects/chores/' + Grocy.EditObjectId, jsonData, - function(result) - { - Grocy.Components.UserfieldsForm.Save(function() - { - Grocy.Api.Post('chores/executions/calculate-next-assignments', { "chore_id": Grocy.EditObjectId }, - function(result) - { - window.location.href = U('/chores'); - }, - function(xhr) - { - Grocy.FrontendHelpers.EndUiBusy(); - console.error(xhr); - } - ); - }); - }, - function(xhr) - { - Grocy.FrontendHelpers.EndUiBusy("chore-form"); - Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response) - } - ); - } -}); - -$('#chore-form input').keyup(function(event) -{ - Grocy.FrontendHelpers.ValidateForm('chore-form'); -}); - -$('#chore-form input').keydown(function(event) -{ - if (event.keyCode === 13) //Enter - { - event.preventDefault(); - - if (document.getElementById('chore-form').checkValidity() === false) //There is at least one validation error + e.preventDefault(); + + if ($(".combobox-menu-visible").length) { - return false; + return; + } + + var jsonData = $('#chore-form').serializeJSON(); + if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_CHORES_ASSIGNMENTS) + { + jsonData.assignment_config = $("#assignment_config").val().join(","); + } + + Grocy.FrontendHelpers.BeginUiBusy("chore-form"); + + if (Grocy.EditMode === 'create') + { + Grocy.Api.Post('objects/chores', jsonData, + function(result) + { + Grocy.EditObjectId = result.created_object_id; + Grocy.Components.UserfieldsForm.Save(function() + { + Grocy.Api.Post('chores/executions/calculate-next-assignments', { "chore_id": Grocy.EditObjectId }, + function(result) + { + window.location.href = U('/chores'); + }, + function(xhr) + { + Grocy.FrontendHelpers.EndUiBusy(); + console.error(xhr); + } + ); + }); + }, + function(xhr) + { + Grocy.FrontendHelpers.EndUiBusy("chore-form"); + Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response) + } + ); } else { - $('#save-chore-button').click(); + Grocy.Api.Put('objects/chores/' + Grocy.EditObjectId, jsonData, + function(result) + { + Grocy.Components.UserfieldsForm.Save(function() + { + Grocy.Api.Post('chores/executions/calculate-next-assignments', { "chore_id": Grocy.EditObjectId }, + function(result) + { + window.location.href = U('/chores'); + }, + function(xhr) + { + Grocy.FrontendHelpers.EndUiBusy(); + console.error(xhr); + } + ); + }); + }, + function(xhr) + { + Grocy.FrontendHelpers.EndUiBusy("chore-form"); + Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response) + } + ); + } + }); + + $('#chore-form input').keyup(function(event) + { + Grocy.FrontendHelpers.ValidateForm('chore-form'); + }); + + $('#chore-form input').keydown(function(event) + { + if (event.keyCode === 13) //Enter + { + event.preventDefault(); + + if (document.getElementById('chore-form').checkValidity() === false) //There is at least one validation error + { + return false; + } + else + { + $('#save-chore-button').click(); + } + } + }); + + var checkboxValues = $("#period_config").val().split(","); + for (var i = 0; i < checkboxValues.length; i++) + { + if (!checkboxValues[i].isEmpty()) + { + $("#" + checkboxValues[i]).prop('checked', true); } } -}); - -var checkboxValues = $("#period_config").val().split(","); -for (var i = 0; i < checkboxValues.length; i++) -{ - if (!checkboxValues[i].isEmpty()) + + Grocy.Components.UserfieldsForm.Load(); + $('#name').focus(); + Grocy.FrontendHelpers.ValidateForm('chore-form'); + + setTimeout(function() { - $("#" + checkboxValues[i]).prop('checked', true); - } + $(".input-group-chore-period-type").trigger("change"); + $(".input-group-chore-assignment-type").trigger("change"); + + // Click twice to trigger on-click but not change the actual checked state + $("#consume_product_on_execution").click(); + $("#consume_product_on_execution").click(); + + Grocy.Components.ProductPicker.GetPicker().trigger('change'); + }, 100); + + $('.input-group-chore-period-type').on('change', function(e) + { + var periodType = $('#period_type').val(); + var periodDays = $('#period_days').val(); + var periodInterval = $('#period_interval').val(); + + $(".period-type-input").addClass("d-none"); + $(".period-type-" + periodType).removeClass("d-none"); + $('#chore-period-type-info').attr("data-original-title", ""); + $("#period_config").val(""); + + if (periodType === 'manually') + { + $('#chore-period-type-info').attr("data-original-title", __t('This means the next execution of this chore is not scheduled')); + } + else if (periodType === 'dynamic-regular') + { + $("label[for='period_days']").text(__t("Period days")); + $("#period_days").attr("min", "0"); + $("#period_days").removeAttr("max"); + $('#chore-period-type-info').attr("data-original-title", __t('This means the next execution of this chore is scheduled %s days after the last execution', periodDays.toString())); + } + else if (periodType === 'daily') + { + $('#chore-period-type-info').attr("data-original-title", __t('This means the next execution of this chore is scheduled 1 day after the last execution')); + $('#chore-period-interval-info').attr("data-original-title", __t('This means the next execution of this chore should only be scheduled every %s days', periodInterval.toString())); + } + else if (periodType === 'weekly') + { + $('#chore-period-type-info').attr("data-original-title", __t('This means the next execution of this chore is scheduled 1 day after the last execution, but only for the weekdays selected below')); + $("#period_config").val($(".period-type-weekly input:checkbox:checked").map(function() { return this.value; }).get().join(",")); + $('#chore-period-interval-info').attr("data-original-title", __t('This means the next execution of this chore should only be scheduled every %s weeks', periodInterval.toString())); + } + else if (periodType === 'monthly') + { + $('#chore-period-type-info').attr("data-original-title", __t('This means the next execution of this chore is scheduled on the below selected day of each month')); + $("label[for='period_days']").text(__t("Day of month")); + $("#period_days").attr("min", "1"); + $("#period_days").attr("max", "31"); + $('#chore-period-interval-info').attr("data-original-title", __t('This means the next execution of this chore should only be scheduled every %s months', periodInterval.toString())); + } + else if (periodType === 'yearly') + { + $('#chore-period-type-info').attr("data-original-title", __t('This means the next execution of this chore is scheduled 1 year after the last execution')); + $('#chore-period-interval-info').attr("data-original-title", __t('This means the next execution of this chore should only be scheduled every %s years', periodInterval.toString())); + } + + Grocy.FrontendHelpers.ValidateForm('chore-form'); + }); + + $('.input-group-chore-assignment-type').on('change', function(e) + { + var assignmentType = $('#assignment_type').val(); + + $('#chore-period-assignment-info').text(""); + $("#assignment_config").removeAttr("required"); + $("#assignment_config").attr("disabled", ""); + + if (assignmentType === 'no-assignment') + { + $('#chore-assignment-type-info').attr("data-original-title", __t('This means the next execution of this chore will not be assigned to anyone')); + } + else if (assignmentType === 'who-least-did-first') + { + $('#chore-assignment-type-info').attr("data-original-title", __t('This means the next execution of this chore will be assigned to the one who executed it least')); + $("#assignment_config").attr("required", ""); + $("#assignment_config").removeAttr("disabled"); + } + else if (assignmentType === 'random') + { + $('#chore-assignment-type-info').attr("data-original-title", __t('This means the next execution of this chore will be assigned randomly')); + $("#assignment_config").attr("required", ""); + $("#assignment_config").removeAttr("disabled"); + } + else if (assignmentType === 'in-alphabetical-order') + { + $('#chore-assignment-type-info').attr("data-original-title", __t('This means the next execution of this chore will be assigned to the next one in alphabetical order')); + $("#assignment_config").attr("required", ""); + $("#assignment_config").removeAttr("disabled"); + } + + Grocy.FrontendHelpers.ValidateForm('chore-form'); + }); + + $("#consume_product_on_execution").on("click", function() + { + if (this.checked) + { + Grocy.Components.ProductPicker.Enable(); + $("#product_amount").removeAttr("disabled"); + } + else + { + Grocy.Components.ProductPicker.Disable(); + $("#product_amount").attr("disabled", ""); + } + + Grocy.FrontendHelpers.ValidateForm("chore-form"); + }); + + Grocy.Components.ProductPicker.GetPicker().on('change', function(e) + { + var productId = $(e.target).val(); + + if (productId) + { + Grocy.Api.Get('stock/products/' + productId, + function(productDetails) + { + $('#amount_qu_unit').text(productDetails.quantity_unit_stock.name); + }, + function(xhr) + { + console.error(xhr); + } + ); + } + }); + } - -Grocy.Components.UserfieldsForm.Load(); -$('#name').focus(); -Grocy.FrontendHelpers.ValidateForm('chore-form'); - -setTimeout(function() -{ - $(".input-group-chore-period-type").trigger("change"); - $(".input-group-chore-assignment-type").trigger("change"); - - // Click twice to trigger on-click but not change the actual checked state - $("#consume_product_on_execution").click(); - $("#consume_product_on_execution").click(); - - Grocy.Components.ProductPicker.GetPicker().trigger('change'); -}, 100); - -$('.input-group-chore-period-type').on('change', function(e) -{ - var periodType = $('#period_type').val(); - var periodDays = $('#period_days').val(); - var periodInterval = $('#period_interval').val(); - - $(".period-type-input").addClass("d-none"); - $(".period-type-" + periodType).removeClass("d-none"); - $('#chore-period-type-info').attr("data-original-title", ""); - $("#period_config").val(""); - - if (periodType === 'manually') - { - $('#chore-period-type-info').attr("data-original-title", __t('This means the next execution of this chore is not scheduled')); - } - else if (periodType === 'dynamic-regular') - { - $("label[for='period_days']").text(__t("Period days")); - $("#period_days").attr("min", "0"); - $("#period_days").removeAttr("max"); - $('#chore-period-type-info').attr("data-original-title", __t('This means the next execution of this chore is scheduled %s days after the last execution', periodDays.toString())); - } - else if (periodType === 'daily') - { - $('#chore-period-type-info').attr("data-original-title", __t('This means the next execution of this chore is scheduled 1 day after the last execution')); - $('#chore-period-interval-info').attr("data-original-title", __t('This means the next execution of this chore should only be scheduled every %s days', periodInterval.toString())); - } - else if (periodType === 'weekly') - { - $('#chore-period-type-info').attr("data-original-title", __t('This means the next execution of this chore is scheduled 1 day after the last execution, but only for the weekdays selected below')); - $("#period_config").val($(".period-type-weekly input:checkbox:checked").map(function() { return this.value; }).get().join(",")); - $('#chore-period-interval-info').attr("data-original-title", __t('This means the next execution of this chore should only be scheduled every %s weeks', periodInterval.toString())); - } - else if (periodType === 'monthly') - { - $('#chore-period-type-info').attr("data-original-title", __t('This means the next execution of this chore is scheduled on the below selected day of each month')); - $("label[for='period_days']").text(__t("Day of month")); - $("#period_days").attr("min", "1"); - $("#period_days").attr("max", "31"); - $('#chore-period-interval-info').attr("data-original-title", __t('This means the next execution of this chore should only be scheduled every %s months', periodInterval.toString())); - } - else if (periodType === 'yearly') - { - $('#chore-period-type-info').attr("data-original-title", __t('This means the next execution of this chore is scheduled 1 year after the last execution')); - $('#chore-period-interval-info').attr("data-original-title", __t('This means the next execution of this chore should only be scheduled every %s years', periodInterval.toString())); - } - - Grocy.FrontendHelpers.ValidateForm('chore-form'); -}); - -$('.input-group-chore-assignment-type').on('change', function(e) -{ - var assignmentType = $('#assignment_type').val(); - - $('#chore-period-assignment-info').text(""); - $("#assignment_config").removeAttr("required"); - $("#assignment_config").attr("disabled", ""); - - if (assignmentType === 'no-assignment') - { - $('#chore-assignment-type-info').attr("data-original-title", __t('This means the next execution of this chore will not be assigned to anyone')); - } - else if (assignmentType === 'who-least-did-first') - { - $('#chore-assignment-type-info').attr("data-original-title", __t('This means the next execution of this chore will be assigned to the one who executed it least')); - $("#assignment_config").attr("required", ""); - $("#assignment_config").removeAttr("disabled"); - } - else if (assignmentType === 'random') - { - $('#chore-assignment-type-info').attr("data-original-title", __t('This means the next execution of this chore will be assigned randomly')); - $("#assignment_config").attr("required", ""); - $("#assignment_config").removeAttr("disabled"); - } - else if (assignmentType === 'in-alphabetical-order') - { - $('#chore-assignment-type-info').attr("data-original-title", __t('This means the next execution of this chore will be assigned to the next one in alphabetical order')); - $("#assignment_config").attr("required", ""); - $("#assignment_config").removeAttr("disabled"); - } - - Grocy.FrontendHelpers.ValidateForm('chore-form'); -}); - -$("#consume_product_on_execution").on("click", function() -{ - if (this.checked) - { - Grocy.Components.ProductPicker.Enable(); - $("#product_amount").removeAttr("disabled"); - } - else - { - Grocy.Components.ProductPicker.Disable(); - $("#product_amount").attr("disabled", ""); - } - - Grocy.FrontendHelpers.ValidateForm("chore-form"); -}); - -Grocy.Components.ProductPicker.GetPicker().on('change', function(e) -{ - var productId = $(e.target).val(); - - if (productId) - { - Grocy.Api.Get('stock/products/' + productId, - function(productDetails) - { - $('#amount_qu_unit').text(productDetails.quantity_unit_stock.name); - }, - function(xhr) - { - console.error(xhr); - } - ); - } -}); diff --git a/js/viewjs/chores.js b/js/viewjs/chores.js index 8792f9f0..e1d39024 100644 --- a/js/viewjs/chores.js +++ b/js/viewjs/chores.js @@ -1,40 +1,50 @@ -var choresTable = $('#chores-table').DataTable({ - 'order': [[1, 'asc']], - 'columnDefs': [ - { 'orderable': false, 'targets': 0 }, - { 'searchable': false, "targets": 0 } - ].concat($.fn.dataTable.defaults.columnDefs) -}); -$('#chores-table tbody').removeClass("d-none"); -Grocy.FrontendHelpers.InitDataTable(choresTable, null, function() +function choresView(Grocy, scope = null) { - $("#search").val(""); - choresTable.search("").draw(); - $("#show-disabled").prop('checked', false); -}); - -Grocy.FrontendHelpers.MakeDeleteConfirmBox( - 'Are you sure to delete chore "%s"?', - '.core-delete-button', - 'data-chore-name', - 'data-chore-id', - 'objects/chores/', - '/chroes' -); - -$("#show-disabled").change(function() -{ - if (this.checked) + var $scope = $; + if (scope != null) { - window.location.href = U('/chores?include_disabled'); + $scope = $(scope).find; } - else - { - window.location.href = U('/chores'); - } -}); -if (GetUriParam('include_disabled')) -{ - $("#show-disabled").prop('checked', true); + var choresTable = $('#chores-table').DataTable({ + 'order': [[1, 'asc']], + 'columnDefs': [ + { 'orderable': false, 'targets': 0 }, + { 'searchable': false, "targets": 0 } + ].concat($.fn.dataTable.defaults.columnDefs) + }); + $('#chores-table tbody').removeClass("d-none"); + Grocy.FrontendHelpers.InitDataTable(choresTable, null, function() + { + $("#search").val(""); + choresTable.search("").draw(); + $("#show-disabled").prop('checked', false); + }); + + Grocy.FrontendHelpers.MakeDeleteConfirmBox( + 'Are you sure to delete chore "%s"?', + '.core-delete-button', + 'data-chore-name', + 'data-chore-id', + 'objects/chores/', + '/chroes' + ); + + $("#show-disabled").change(function() + { + if (this.checked) + { + window.location.href = U('/chores?include_disabled'); + } + else + { + window.location.href = U('/chores'); + } + }); + + if (GetUriParam('include_disabled')) + { + $("#show-disabled").prop('checked', true); + } + } diff --git a/js/viewjs/choresjournal.js b/js/viewjs/choresjournal.js index ea5e3055..62b59a7c 100644 --- a/js/viewjs/choresjournal.js +++ b/js/viewjs/choresjournal.js @@ -1,40 +1,50 @@ -var choresJournalTable = $('#chores-journal-table').DataTable({ - 'paginate': true, - 'order': [[2, 'desc']], - 'columnDefs': [ - { 'orderable': false, 'targets': 0 }, - { 'searchable': false, "targets": 0 } - ].concat($.fn.dataTable.defaults.columnDefs) -}); -$('#chores-journal-table tbody').removeClass("d-none"); -Grocy.FrontendHelpers.InitDataTable(choresJournalTable); -Grocy.FrontendHelpers.MakeFilterForColumn("#chore-filter", 1, choresJournalTable); - -if (typeof GetUriParam("chore") !== "undefined") +function choresjournalView(Grocy, scope = null) { - $("#chore-filter").val(GetUriParam("chore")); - $("#chore-filter").trigger("change"); + var $scope = $; + if (scope != null) + { + $scope = $(scope).find; + } + + var choresJournalTable = $('#chores-journal-table').DataTable({ + 'paginate': true, + 'order': [[2, 'desc']], + 'columnDefs': [ + { 'orderable': false, 'targets': 0 }, + { 'searchable': false, "targets": 0 } + ].concat($.fn.dataTable.defaults.columnDefs) + }); + $('#chores-journal-table tbody').removeClass("d-none"); + Grocy.FrontendHelpers.InitDataTable(choresJournalTable); + Grocy.FrontendHelpers.MakeFilterForColumn("#chore-filter", 1, choresJournalTable); + + if (typeof GetUriParam("chore") !== "undefined") + { + $("#chore-filter").val(GetUriParam("chore")); + $("#chore-filter").trigger("change"); + } + + $(document).on('click', '.undo-chore-execution-button', function(e) + { + e.preventDefault(); + + var element = $(e.currentTarget); + var executionId = element.attr('data-execution-id'); + + Grocy.Api.Post('chores/executions/' + executionId.toString() + '/undo', {}, + function(result) + { + element.closest("tr").addClass("text-muted"); + element.parent().siblings().find("span.name-anchor").addClass("text-strike-through").after("
" + __t("Undone on") + " " + moment().format("YYYY-MM-DD HH:mm:ss") + " "); + element.closest(".undo-stock-booking-button").addClass("disabled"); + RefreshContextualTimeago("#chore-execution-" + executionId + "-row"); + toastr.success(__t("Chore execution successfully undone")); + }, + function(xhr) + { + console.error(xhr); + } + ); + }); + } - -$(document).on('click', '.undo-chore-execution-button', function(e) -{ - e.preventDefault(); - - var element = $(e.currentTarget); - var executionId = element.attr('data-execution-id'); - - Grocy.Api.Post('chores/executions/' + executionId.toString() + '/undo', {}, - function(result) - { - element.closest("tr").addClass("text-muted"); - element.parent().siblings().find("span.name-anchor").addClass("text-strike-through").after("
" + __t("Undone on") + " " + moment().format("YYYY-MM-DD HH:mm:ss") + " "); - element.closest(".undo-stock-booking-button").addClass("disabled"); - RefreshContextualTimeago("#chore-execution-" + executionId + "-row"); - toastr.success(__t("Chore execution successfully undone")); - }, - function(xhr) - { - console.error(xhr); - } - ); -}); diff --git a/js/viewjs/choresoverview.js b/js/viewjs/choresoverview.js index 82adff67..767743b8 100644 --- a/js/viewjs/choresoverview.js +++ b/js/viewjs/choresoverview.js @@ -1,182 +1,192 @@ -Grocy.Use("chorecard"); - -var choresOverviewTable = $('#chores-overview-table').DataTable({ - 'order': [[2, 'asc']], - 'columnDefs': [ - { 'orderable': false, 'targets': 0 }, - { 'searchable': false, "targets": 0 }, - { "type": "html", "targets": 5 }, - { "type": "html", "targets": 2 }, - { "type": "html", "targets": 3 } - ].concat($.fn.dataTable.defaults.columnDefs) -}); -$('#chores-overview-table tbody').removeClass("d-none"); -Grocy.FrontendHelpers.InitDataTable(choresOverviewTable); -Grocy.FrontendHelpers.MakeValueFilter("status", 5, choresOverviewTable); -Grocy.FrontendHelpers.MakeValueFilter("user", 6, choresOverviewTable, ""); - -$("#user-filter").on("change", function() +function choresoverviewView(Grocy, scope = null) { - var user = $(this).val(); - if (user !== null && !user.isEmpty()) + var $scope = $; + if (scope != null) { - UpdateUriParam("user", $("#user-filter option:selected").data("user-id")); + $scope = $(scope).find; } - else + + Grocy.Use("chorecard"); + + var choresOverviewTable = $('#chores-overview-table').DataTable({ + 'order': [[2, 'asc']], + 'columnDefs': [ + { 'orderable': false, 'targets': 0 }, + { 'searchable': false, "targets": 0 }, + { "type": "html", "targets": 5 }, + { "type": "html", "targets": 2 }, + { "type": "html", "targets": 3 } + ].concat($.fn.dataTable.defaults.columnDefs) + }); + $('#chores-overview-table tbody').removeClass("d-none"); + Grocy.FrontendHelpers.InitDataTable(choresOverviewTable); + Grocy.FrontendHelpers.MakeValueFilter("status", 5, choresOverviewTable); + Grocy.FrontendHelpers.MakeValueFilter("user", 6, choresOverviewTable, ""); + + $("#user-filter").on("change", function() { - RemoveUriParam("user") - } -}); - -$(document).on('click', '.track-chore-button', function(e) -{ - e.preventDefault(); - - // Remove the focus from the current button - // to prevent that the tooltip stays until clicked anywhere else - document.activeElement.blur(); - - Grocy.FrontendHelpers.BeginUiBusy(); - - var choreId = $(e.currentTarget).attr('data-chore-id'); - var choreName = $(e.currentTarget).attr('data-chore-name'); - - Grocy.Api.Get('objects/chores/' + choreId, - function(chore) + var user = $(this).val(); + if (user !== null && !user.isEmpty()) { - var trackedTime = moment().format('YYYY-MM-DD HH:mm:ss'); - if (chore.track_date_only == 1) + UpdateUriParam("user", $("#user-filter option:selected").data("user-id")); + } + else + { + RemoveUriParam("user") + } + }); + + $(document).on('click', '.track-chore-button', function(e) + { + e.preventDefault(); + + // Remove the focus from the current button + // to prevent that the tooltip stays until clicked anywhere else + document.activeElement.blur(); + + Grocy.FrontendHelpers.BeginUiBusy(); + + var choreId = $(e.currentTarget).attr('data-chore-id'); + var choreName = $(e.currentTarget).attr('data-chore-name'); + + Grocy.Api.Get('objects/chores/' + choreId, + function(chore) { - trackedTime = moment().format('YYYY-MM-DD'); + var trackedTime = moment().format('YYYY-MM-DD HH:mm:ss'); + if (chore.track_date_only == 1) + { + trackedTime = moment().format('YYYY-MM-DD'); + } + + Grocy.Api.Post('chores/' + choreId + '/execute', { 'tracked_time': trackedTime }, + function() + { + Grocy.Api.Get('chores/' + choreId, + function(result) + { + var choreRow = $('#chore-' + choreId + '-row'); + var nextXDaysThreshold = moment().add($("#info-due-chores").data("next-x-days"), "days"); + var now = moment(); + var nextExecutionTime = moment(result.next_estimated_execution_time); + + choreRow.removeClass("table-warning"); + choreRow.removeClass("table-danger"); + $('#chore-' + choreId + '-due-filter-column').html(""); + if (nextExecutionTime.isBefore(now)) + { + choreRow.addClass("table-danger"); + $('#chore-' + choreId + '-due-filter-column').html("overdue"); + } + else if (nextExecutionTime.isBefore(nextXDaysThreshold)) + { + choreRow.addClass("table-warning"); + $('#chore-' + choreId + '-due-filter-column').html("duesoon"); + } + + animateCSS("#chore-" + choreId + "-row td:not(:first)", "shake"); + + $('#chore-' + choreId + '-last-tracked-time').text(trackedTime); + $('#chore-' + choreId + '-last-tracked-time-timeago').attr('datetime', trackedTime); + + if (result.chore.period_type == "dynamic-regular") + { + $('#chore-' + choreId + '-next-execution-time').text(result.next_estimated_execution_time); + $('#chore-' + choreId + '-next-execution-time-timeago').attr('datetime', result.next_estimated_execution_time); + } + + if (result.chore.next_execution_assigned_to_user_id != null) + { + $('#chore-' + choreId + '-next-execution-assigned-user').text(result.next_execution_assigned_user.display_name); + } + + Grocy.FrontendHelpers.EndUiBusy(); + toastr.success(__t('Tracked execution of chore %1$s on %2$s', choreName, trackedTime)); + RefreshStatistics(); + + // Delay due to delayed/animated set of new timestamps above + setTimeout(function() + { + RefreshContextualTimeago("#chore-" + choreId + "-row"); + + // Refresh the DataTable to re-apply filters + choresOverviewTable.rows().invalidate().draw(false); + $(".input-group-filter").trigger("change"); + }, 550); + }, + function(xhr) + { + Grocy.FrontendHelpers.EndUiBusy(); + console.error(xhr); + } + ); + }, + function(xhr) + { + Grocy.FrontendHelpers.EndUiBusy(); + console.error(xhr); + } + ); + }, + function(xhr) + { + Grocy.FrontendHelpers.EndUiBusy("choretracking-form"); + console.error(xhr); } - - Grocy.Api.Post('chores/' + choreId + '/execute', { 'tracked_time': trackedTime }, - function() - { - Grocy.Api.Get('chores/' + choreId, - function(result) - { - var choreRow = $('#chore-' + choreId + '-row'); - var nextXDaysThreshold = moment().add($("#info-due-chores").data("next-x-days"), "days"); - var now = moment(); - var nextExecutionTime = moment(result.next_estimated_execution_time); - - choreRow.removeClass("table-warning"); - choreRow.removeClass("table-danger"); - $('#chore-' + choreId + '-due-filter-column').html(""); - if (nextExecutionTime.isBefore(now)) - { - choreRow.addClass("table-danger"); - $('#chore-' + choreId + '-due-filter-column').html("overdue"); - } - else if (nextExecutionTime.isBefore(nextXDaysThreshold)) - { - choreRow.addClass("table-warning"); - $('#chore-' + choreId + '-due-filter-column').html("duesoon"); - } - - animateCSS("#chore-" + choreId + "-row td:not(:first)", "shake"); - - $('#chore-' + choreId + '-last-tracked-time').text(trackedTime); - $('#chore-' + choreId + '-last-tracked-time-timeago').attr('datetime', trackedTime); - - if (result.chore.period_type == "dynamic-regular") - { - $('#chore-' + choreId + '-next-execution-time').text(result.next_estimated_execution_time); - $('#chore-' + choreId + '-next-execution-time-timeago').attr('datetime', result.next_estimated_execution_time); - } - - if (result.chore.next_execution_assigned_to_user_id != null) - { - $('#chore-' + choreId + '-next-execution-assigned-user').text(result.next_execution_assigned_user.display_name); - } - - Grocy.FrontendHelpers.EndUiBusy(); - toastr.success(__t('Tracked execution of chore %1$s on %2$s', choreName, trackedTime)); - RefreshStatistics(); - - // Delay due to delayed/animated set of new timestamps above - setTimeout(function() - { - RefreshContextualTimeago("#chore-" + choreId + "-row"); - - // Refresh the DataTable to re-apply filters - choresOverviewTable.rows().invalidate().draw(false); - $(".input-group-filter").trigger("change"); - }, 550); - }, - function(xhr) - { - Grocy.FrontendHelpers.EndUiBusy(); - console.error(xhr); - } - ); - }, - function(xhr) - { - Grocy.FrontendHelpers.EndUiBusy(); - console.error(xhr); - } - ); - }, - function(xhr) - { - Grocy.FrontendHelpers.EndUiBusy("choretracking-form"); - console.error(xhr); - } - ); -}); - -$(document).on("click", ".chore-name-cell", function(e) -{ - Grocy.Components.ChoreCard.Refresh($(e.currentTarget).attr("data-chore-id")); - $("#choresoverview-chorecard-modal").modal("show"); -}); - -function RefreshStatistics() -{ - var nextXDays = $("#info-due-chores").data("next-x-days"); - Grocy.Api.Get('chores', - function(result) - { - var dueCount = 0; - var overdueCount = 0; - var assignedToMeCount = 0; - var now = moment(); - var nextXDaysThreshold = moment().add(nextXDays, "days"); - result.forEach(element => + ); + }); + + $(document).on("click", ".chore-name-cell", function(e) + { + Grocy.Components.ChoreCard.Refresh($(e.currentTarget).attr("data-chore-id")); + $("#choresoverview-chorecard-modal").modal("show"); + }); + + function RefreshStatistics() + { + var nextXDays = $("#info-due-chores").data("next-x-days"); + Grocy.Api.Get('chores', + function(result) { - var date = moment(element.next_estimated_execution_time); - if (date.isBefore(now)) + var dueCount = 0; + var overdueCount = 0; + var assignedToMeCount = 0; + var now = moment(); + var nextXDaysThreshold = moment().add(nextXDays, "days"); + result.forEach(element => { - overdueCount++; - } - else if (date.isBefore(nextXDaysThreshold)) - { - dueCount++; - } - - if (parseInt(element.next_execution_assigned_to_user_id) == Grocy.UserId) - { - assignedToMeCount++; - } - }); - - $("#info-due-chores").html('' + dueCount + ' ' + __n(dueCount, '%s chore is due to be done', '%s chores are due to be done') + ' ' + __n(nextXDays, 'within the next day', 'within the next %s days')); - $("#info-overdue-chores").html('' + overdueCount + ' ' + __n(overdueCount, '%s chore is overdue to be done', '%s chores are overdue to be done')); - $("#info-assigned-to-me-chores").html('' + assignedToMeCount + ' ' + __n(assignedToMeCount, '%s chore is assigned to me', '%s chores are assigned to me')); - }, - function(xhr) - { - console.error(xhr); - } - ); + var date = moment(element.next_estimated_execution_time); + if (date.isBefore(now)) + { + overdueCount++; + } + else if (date.isBefore(nextXDaysThreshold)) + { + dueCount++; + } + + if (parseInt(element.next_execution_assigned_to_user_id) == Grocy.UserId) + { + assignedToMeCount++; + } + }); + + $("#info-due-chores").html('' + dueCount + ' ' + __n(dueCount, '%s chore is due to be done', '%s chores are due to be done') + ' ' + __n(nextXDays, 'within the next day', 'within the next %s days')); + $("#info-overdue-chores").html('' + overdueCount + ' ' + __n(overdueCount, '%s chore is overdue to be done', '%s chores are overdue to be done')); + $("#info-assigned-to-me-chores").html('' + assignedToMeCount + ' ' + __n(assignedToMeCount, '%s chore is assigned to me', '%s chores are assigned to me')); + }, + function(xhr) + { + console.error(xhr); + } + ); + } + + if (GetUriParam("user") !== undefined) + { + $("#user-filter").val("xx" + GetUriParam("user") + "xx"); + $("#user-filter").trigger("change"); + } + + RefreshStatistics(); + } - -if (GetUriParam("user") !== undefined) -{ - $("#user-filter").val("xx" + GetUriParam("user") + "xx"); - $("#user-filter").trigger("change"); -} - -RefreshStatistics(); diff --git a/js/viewjs/choressettings.js b/js/viewjs/choressettings.js index 2c0360d8..49c0a9a5 100644 --- a/js/viewjs/choressettings.js +++ b/js/viewjs/choressettings.js @@ -1,5 +1,15 @@ -Grocy.Use("numberpicker"); +function choressettingsView(Grocy, scope = null) +{ + var $scope = $; + if (scope != null) + { + $scope = $(scope).find; + } -$("#chores_due_soon_days").val(Grocy.UserSettings.chores_due_soon_days); - -RefreshLocaleNumberInput(); + Grocy.Use("numberpicker"); + + $("#chores_due_soon_days").val(Grocy.UserSettings.chores_due_soon_days); + + RefreshLocaleNumberInput(); + +} diff --git a/js/viewjs/choretracking.js b/js/viewjs/choretracking.js index e5c7ecb6..a8937ee4 100644 --- a/js/viewjs/choretracking.js +++ b/js/viewjs/choretracking.js @@ -1,119 +1,129 @@ -Grocy.Use("chorecard"); -Grocy.Use("datetimepicker"); -Grocy.Use("userpicker"); - -$('#save-choretracking-button').on('click', function(e) +function choretrackingView(Grocy, scope = null) { - e.preventDefault(); - - if ($(".combobox-menu-visible").length) + var $scope = $; + if (scope != null) { - return; + $scope = $(scope).find; } - var jsonForm = $('#choretracking-form').serializeJSON(); - Grocy.FrontendHelpers.BeginUiBusy("choretracking-form"); - - Grocy.Api.Get('chores/' + jsonForm.chore_id, - function(choreDetails) - { - Grocy.Api.Post('chores/' + jsonForm.chore_id + '/execute', { 'tracked_time': Grocy.Components.DateTimePicker.GetValue(), 'done_by': $("#user_id").val() }, - function(result) - { - Grocy.FrontendHelpers.EndUiBusy("choretracking-form"); - toastr.success(__t('Tracked execution of chore %1$s on %2$s', choreDetails.chore.name, Grocy.Components.DateTimePicker.GetValue()) + '
' + __t("Undo") + ''); - Grocy.Components.ChoreCard.Refresh($('#chore_id').val()); - - $('#chore_id').val(''); - $('#chore_id_text_input').focus(); - $('#chore_id_text_input').val(''); - Grocy.Components.DateTimePicker.SetValue(moment().format('YYYY-MM-DD HH:mm:ss')); - $('#chore_id_text_input').trigger('change'); - Grocy.FrontendHelpers.ValidateForm('choretracking-form'); - }, - function(xhr) - { - Grocy.FrontendHelpers.EndUiBusy("choretracking-form"); - console.error(xhr); - } - ); - }, - function(xhr) - { - Grocy.FrontendHelpers.EndUiBusy("choretracking-form"); - console.error(xhr); - } - ); -}); - -$('#chore_id').on('change', function(e) -{ - var input = $('#chore_id_text_input').val().toString(); - $('#chore_id_text_input').val(input); - $('#chore_id').data('combobox').refresh(); - - var choreId = $(e.target).val(); - if (choreId) + Grocy.Use("chorecard"); + Grocy.Use("datetimepicker"); + Grocy.Use("userpicker"); + + $('#save-choretracking-button').on('click', function(e) { - Grocy.Api.Get('objects/chores/' + choreId, - function(chore) + e.preventDefault(); + + if ($(".combobox-menu-visible").length) + { + return; + } + + var jsonForm = $('#choretracking-form').serializeJSON(); + Grocy.FrontendHelpers.BeginUiBusy("choretracking-form"); + + Grocy.Api.Get('chores/' + jsonForm.chore_id, + function(choreDetails) { - if (chore.track_date_only == 1) - { - Grocy.Components.DateTimePicker.ChangeFormat("YYYY-MM-DD"); - Grocy.Components.DateTimePicker.SetValue(moment().format("YYYY-MM-DD")); - } - else - { - Grocy.Components.DateTimePicker.ChangeFormat("YYYY-MM-DD HH:mm:ss"); - Grocy.Components.DateTimePicker.SetValue(moment().format("YYYY-MM-DD HH:mm:ss")); - } + Grocy.Api.Post('chores/' + jsonForm.chore_id + '/execute', { 'tracked_time': Grocy.Components.DateTimePicker.GetValue(), 'done_by': $("#user_id").val() }, + function(result) + { + Grocy.FrontendHelpers.EndUiBusy("choretracking-form"); + toastr.success(__t('Tracked execution of chore %1$s on %2$s', choreDetails.chore.name, Grocy.Components.DateTimePicker.GetValue()) + '
' + __t("Undo") + ''); + Grocy.Components.ChoreCard.Refresh($('#chore_id').val()); + + $('#chore_id').val(''); + $('#chore_id_text_input').focus(); + $('#chore_id_text_input').val(''); + Grocy.Components.DateTimePicker.SetValue(moment().format('YYYY-MM-DD HH:mm:ss')); + $('#chore_id_text_input').trigger('change'); + Grocy.FrontendHelpers.ValidateForm('choretracking-form'); + }, + function(xhr) + { + Grocy.FrontendHelpers.EndUiBusy("choretracking-form"); + console.error(xhr); + } + ); }, function(xhr) { + Grocy.FrontendHelpers.EndUiBusy("choretracking-form"); console.error(xhr); } ); - - Grocy.Components.ChoreCard.Refresh(choreId); - Grocy.Components.DateTimePicker.GetInputElement().focus(); - Grocy.FrontendHelpers.ValidateForm('choretracking-form'); - } -}); - -$('.combobox').combobox({ - appendId: '_text_input', - bsVersion: '4' -}); - -$('#chore_id_text_input').focus(); -$('#chore_id_text_input').trigger('change'); -Grocy.Components.DateTimePicker.GetInputElement().trigger('input'); -Grocy.FrontendHelpers.ValidateForm('choretracking-form'); - -$('#choretracking-form input').keyup(function(event) -{ - Grocy.FrontendHelpers.ValidateForm('choretracking-form'); -}); - -$('#choretracking-form input').keydown(function(event) -{ - if (event.keyCode === 13) //Enter + }); + + $('#chore_id').on('change', function(e) { - event.preventDefault(); - - if (document.getElementById('choretracking-form').checkValidity() === false) //There is at least one validation error + var input = $('#chore_id_text_input').val().toString(); + $('#chore_id_text_input').val(input); + $('#chore_id').data('combobox').refresh(); + + var choreId = $(e.target).val(); + if (choreId) { - return false; + Grocy.Api.Get('objects/chores/' + choreId, + function(chore) + { + if (chore.track_date_only == 1) + { + Grocy.Components.DateTimePicker.ChangeFormat("YYYY-MM-DD"); + Grocy.Components.DateTimePicker.SetValue(moment().format("YYYY-MM-DD")); + } + else + { + Grocy.Components.DateTimePicker.ChangeFormat("YYYY-MM-DD HH:mm:ss"); + Grocy.Components.DateTimePicker.SetValue(moment().format("YYYY-MM-DD HH:mm:ss")); + } + }, + function(xhr) + { + console.error(xhr); + } + ); + + Grocy.Components.ChoreCard.Refresh(choreId); + Grocy.Components.DateTimePicker.GetInputElement().focus(); + Grocy.FrontendHelpers.ValidateForm('choretracking-form'); } - else - { - $('#save-choretracking-button').click(); - } - } -}); - -Grocy.Components.DateTimePicker.GetInputElement().on('keypress', function(e) -{ + }); + + $('.combobox').combobox({ + appendId: '_text_input', + bsVersion: '4' + }); + + $('#chore_id_text_input').focus(); + $('#chore_id_text_input').trigger('change'); + Grocy.Components.DateTimePicker.GetInputElement().trigger('input'); Grocy.FrontendHelpers.ValidateForm('choretracking-form'); -}); + + $('#choretracking-form input').keyup(function(event) + { + Grocy.FrontendHelpers.ValidateForm('choretracking-form'); + }); + + $('#choretracking-form input').keydown(function(event) + { + if (event.keyCode === 13) //Enter + { + event.preventDefault(); + + if (document.getElementById('choretracking-form').checkValidity() === false) //There is at least one validation error + { + return false; + } + else + { + $('#save-choretracking-button').click(); + } + } + }); + + Grocy.Components.DateTimePicker.GetInputElement().on('keypress', function(e) + { + Grocy.FrontendHelpers.ValidateForm('choretracking-form'); + }); + +} diff --git a/js/viewjs/consume.js b/js/viewjs/consume.js index 28af6a55..8188e2e9 100644 --- a/js/viewjs/consume.js +++ b/js/viewjs/consume.js @@ -1,113 +1,200 @@ -import { BoolVal } from '../helpers/extensions'; -import { WindowMessageBag } from '../helpers/messagebag'; - -Grocy.Use("productamountpicker"); -Grocy.Use("productcard"); -Grocy.Use("productpicker"); -Grocy.Use("recipepicker"); - -$('#save-consume-button').on('click', function(e) +function consumeView(Grocy, scope = null) { - e.preventDefault(); - - if ($(".combobox-menu-visible").length) + var $scope = $; + if (scope != null) { - return; + $scope = $(scope).find; } - var jsonForm = $('#consume-form').serializeJSON(); - Grocy.FrontendHelpers.BeginUiBusy("consume-form"); - - var apiUrl = 'stock/products/' + jsonForm.product_id + '/consume'; - - var jsonData = {}; - jsonData.amount = jsonForm.amount; - jsonData.exact_amount = $('#consume-exact-amount').is(':checked'); - jsonData.spoiled = $('#spoiled').is(':checked'); - jsonData.allow_subproduct_substitution = true; - - if ($("#use_specific_stock_entry").is(":checked")) + import { BoolVal } from '../helpers/extensions'; + import { WindowMessageBag } from '../helpers/messagebag'; + + Grocy.Use("productamountpicker"); + Grocy.Use("productcard"); + Grocy.Use("productpicker"); + Grocy.Use("recipepicker"); + + $('#save-consume-button').on('click', function(e) { - jsonData.stock_entry_id = jsonForm.specific_stock_entry; - } - - if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_LOCATION_TRACKING) - { - jsonData.location_id = $("#location_id").val(); - } - - if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_RECIPES && Grocy.Components.RecipePicker.GetValue().toString().length > 0) - { - jsonData.recipe_id = Grocy.Components.RecipePicker.GetValue(); - } - - var bookingResponse = null; - Grocy.Api.Get('stock/products/' + jsonForm.product_id, - function(productDetails) + e.preventDefault(); + + if ($(".combobox-menu-visible").length) { - Grocy.Api.Post(apiUrl, jsonData, - function(result) - { - if (BoolVal(Grocy.UserSettings.scan_mode_consume_enabled)) + return; + } + + var jsonForm = $('#consume-form').serializeJSON(); + Grocy.FrontendHelpers.BeginUiBusy("consume-form"); + + var apiUrl = 'stock/products/' + jsonForm.product_id + '/consume'; + + var jsonData = {}; + jsonData.amount = jsonForm.amount; + jsonData.exact_amount = $('#consume-exact-amount').is(':checked'); + jsonData.spoiled = $('#spoiled').is(':checked'); + jsonData.allow_subproduct_substitution = true; + + if ($("#use_specific_stock_entry").is(":checked")) + { + jsonData.stock_entry_id = jsonForm.specific_stock_entry; + } + + if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_LOCATION_TRACKING) + { + jsonData.location_id = $("#location_id").val(); + } + + if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_RECIPES && Grocy.Components.RecipePicker.GetValue().toString().length > 0) + { + jsonData.recipe_id = Grocy.Components.RecipePicker.GetValue(); + } + + var bookingResponse = null; + Grocy.Api.Get('stock/products/' + jsonForm.product_id, + function(productDetails) + { + Grocy.Api.Post(apiUrl, jsonData, + function(result) { - Grocy.UISound.Success(); - } - - bookingResponse = result; - - if (GetUriParam("flow") === "InplaceAddBarcodeToExistingProduct") - { - var jsonDataBarcode = {}; - jsonDataBarcode.barcode = GetUriParam("barcode"); - jsonDataBarcode.product_id = jsonForm.product_id; - - Grocy.Api.Post('objects/product_barcodes', jsonDataBarcode, - function(result) + if (BoolVal(Grocy.UserSettings.scan_mode_consume_enabled)) + { + Grocy.UISound.Success(); + } + + bookingResponse = result; + + if (GetUriParam("flow") === "InplaceAddBarcodeToExistingProduct") + { + var jsonDataBarcode = {}; + jsonDataBarcode.barcode = GetUriParam("barcode"); + jsonDataBarcode.product_id = jsonForm.product_id; + + Grocy.Api.Post('objects/product_barcodes', jsonDataBarcode, + function(result) + { + $("#flow-info-InplaceAddBarcodeToExistingProduct").addClass("d-none"); + $('#barcode-lookup-disabled-hint').addClass('d-none'); + $('#barcode-lookup-hint').removeClass('d-none'); + window.history.replaceState({}, document.title, U("/consume")); + }, + function(xhr) + { + Grocy.FrontendHelpers.EndUiBusy("consume-form"); + Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response); + } + ); + } + + $("#specific_stock_entry").find("option").remove().end().append(""); + if ($("#use_specific_stock_entry").is(":checked")) + { + $("#use_specific_stock_entry").click(); + } + + var successMessage = null; + if (productDetails.product.enable_tare_weight_handling == 1 && !jsonData.exact_amount) + { + successMessage = __t('Removed %1$s of %2$s from stock', Math.abs(jsonForm.amount - (parseFloat(productDetails.product.tare_weight) + parseFloat(productDetails.stock_amount))) + " " + __n(jsonForm.amount, productDetails.quantity_unit_stock.name, productDetails.quantity_unit_stock.name_plural), productDetails.product.name) + '
' + __t("Undo") + ''; + } + else + { + successMessage = __t('Removed %1$s of %2$s from stock', Math.abs(jsonForm.amount) + " " + __n(jsonForm.amount, productDetails.quantity_unit_stock.name, productDetails.quantity_unit_stock.name_plural), productDetails.product.name) + '
' + __t("Undo") + ''; + } + + if (GetUriParam("embedded") !== undefined) + { + window.parent.postMessage(WindowMessageBag("ProductChanged", jsonForm.product_id), Grocy.BaseUrl); + window.parent.postMessage(WindowMessageBag("ShowSuccessMessage", successMessage), Grocy.BaseUrl); + window.parent.postMessage(WindowMessageBag("CloseAllModals"), Grocy.BaseUrl); + } + else + { + Grocy.FrontendHelpers.EndUiBusy("consume-form"); + toastr.success(successMessage); + Grocy.Components.ProductPicker.FinishFlow(); + + Grocy.Components.ProductAmountPicker.Reset(); + $("#display_amount").attr("min", Grocy.DefaultMinAmount); + $("#display_amount").removeAttr("max"); + if (BoolVal(Grocy.UserSettings.stock_default_consume_amount_use_quick_consume_amount)) { - $("#flow-info-InplaceAddBarcodeToExistingProduct").addClass("d-none"); - $('#barcode-lookup-disabled-hint').addClass('d-none'); - $('#barcode-lookup-hint').removeClass('d-none'); - window.history.replaceState({}, document.title, U("/consume")); - }, - function(xhr) - { - Grocy.FrontendHelpers.EndUiBusy("consume-form"); - Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response); + $('#display_amount').val(productDetails.product.quick_consume_amount); } - ); - } - - $("#specific_stock_entry").find("option").remove().end().append(""); - if ($("#use_specific_stock_entry").is(":checked")) - { - $("#use_specific_stock_entry").click(); - } - - var successMessage = null; - if (productDetails.product.enable_tare_weight_handling == 1 && !jsonData.exact_amount) - { - successMessage = __t('Removed %1$s of %2$s from stock', Math.abs(jsonForm.amount - (parseFloat(productDetails.product.tare_weight) + parseFloat(productDetails.stock_amount))) + " " + __n(jsonForm.amount, productDetails.quantity_unit_stock.name, productDetails.quantity_unit_stock.name_plural), productDetails.product.name) + '
' + __t("Undo") + ''; - } - else - { - successMessage = __t('Removed %1$s of %2$s from stock', Math.abs(jsonForm.amount) + " " + __n(jsonForm.amount, productDetails.quantity_unit_stock.name, productDetails.quantity_unit_stock.name_plural), productDetails.product.name) + '
' + __t("Undo") + ''; - } - - if (GetUriParam("embedded") !== undefined) - { - window.parent.postMessage(WindowMessageBag("ProductChanged", jsonForm.product_id), Grocy.BaseUrl); - window.parent.postMessage(WindowMessageBag("ShowSuccessMessage", successMessage), Grocy.BaseUrl); - window.parent.postMessage(WindowMessageBag("CloseAllModals"), Grocy.BaseUrl); - } - else + else + { + $('#display_amount').val(parseFloat(Grocy.UserSettings.stock_default_consume_amount)); + } + RefreshLocaleNumberInput(); + $(".input-group-productamountpicker").trigger("change"); + $("#tare-weight-handling-info").addClass("d-none"); + Grocy.Components.ProductPicker.Clear(); + if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_RECIPES) + { + Grocy.Components.RecipePicker.Clear(); + } + if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_LOCATION_TRACKING) + { + $("#location_id").find("option").remove().end().append(""); + } + Grocy.Components.ProductPicker.GetInputElement().focus(); + Grocy.Components.ProductCard.Refresh(jsonForm.product_id); + Grocy.FrontendHelpers.ValidateForm('consume-form'); + $("#consume-exact-amount-group").addClass("d-none"); + } + }, + function(xhr) { Grocy.FrontendHelpers.EndUiBusy("consume-form"); - toastr.success(successMessage); - Grocy.Components.ProductPicker.FinishFlow(); - - Grocy.Components.ProductAmountPicker.Reset(); - $("#display_amount").attr("min", Grocy.DefaultMinAmount); - $("#display_amount").removeAttr("max"); + console.error(xhr); + } + ); + }, + function(xhr) + { + Grocy.FrontendHelpers.EndUiBusy("consume-form"); + console.error(xhr); + } + ); + }); + + $('#save-mark-as-open-button').on('click', function(e) + { + e.preventDefault(); + + if ($(".combobox-menu-visible").length) + { + return; + } + + var jsonForm = $('#consume-form').serializeJSON(); + Grocy.FrontendHelpers.BeginUiBusy("consume-form"); + + var apiUrl = 'stock/products/' + jsonForm.product_id + '/open'; + + var jsonData = {}; + jsonData.amount = jsonForm.amount; + jsonData.allow_subproduct_substitution = true; + + if ($("#use_specific_stock_entry").is(":checked")) + { + jsonData.stock_entry_id = jsonForm.specific_stock_entry; + } + + Grocy.Api.Get('stock/products/' + jsonForm.product_id, + function(productDetails) + { + Grocy.Api.Post(apiUrl, jsonData, + function(result) + { + $("#specific_stock_entry").find("option").remove().end().append(""); + if ($("#use_specific_stock_entry").is(":checked")) + { + $("#use_specific_stock_entry").click(); + } + + Grocy.FrontendHelpers.EndUiBusy("consume-form"); + toastr.success(__t('Marked %1$s of %2$s as opened', parseFloat(jsonForm.amount).toLocaleString({ minimumFractionDigits: 0, maximumFractionDigits: Grocy.UserSettings.stock_decimal_places_amounts }) + " " + __n(jsonForm.amount, productDetails.quantity_unit_stock.name, productDetails.quantity_unit_stock.name_plural), productDetails.product.name) + '
' + __t("Undo") + ''); + if (BoolVal(Grocy.UserSettings.stock_default_consume_amount_use_quick_consume_amount)) { $('#display_amount').val(productDetails.product.quick_consume_amount); @@ -118,75 +205,137 @@ $('#save-consume-button').on('click', function(e) } RefreshLocaleNumberInput(); $(".input-group-productamountpicker").trigger("change"); - $("#tare-weight-handling-info").addClass("d-none"); Grocy.Components.ProductPicker.Clear(); - if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_RECIPES) - { - Grocy.Components.RecipePicker.Clear(); - } - if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_LOCATION_TRACKING) - { - $("#location_id").find("option").remove().end().append(""); - } Grocy.Components.ProductPicker.GetInputElement().focus(); - Grocy.Components.ProductCard.Refresh(jsonForm.product_id); Grocy.FrontendHelpers.ValidateForm('consume-form'); - $("#consume-exact-amount-group").addClass("d-none"); + }, + function(xhr) + { + Grocy.FrontendHelpers.EndUiBusy("consume-form"); + console.error(xhr); + } + ); + }, + function(xhr) + { + Grocy.FrontendHelpers.EndUiBusy("consume-form"); + console.error(xhr); + } + ); + }); + var sumValue = 0; + $("#location_id").on('change', function(e) + { + var locationId = $(e.target).val(); + sumValue = 0; + var stockId = null; + + $("#specific_stock_entry").find("option").remove().end().append(""); + if ($("#use_specific_stock_entry").is(":checked")) + { + $("#use_specific_stock_entry").click(); + } + + if (GetUriParam("embedded") !== undefined) + { + stockId = GetUriParam('stockId'); + } + else + { + // try to get stock id from grocycode + if ($("#product_id").data("grocycode")) + { + var gc = $("#product_id").attr("barcode").split(":"); + if (gc.length == 4) + { + stockId = gc[3]; + } + } + } + + if (locationId) + { + Grocy.Api.Get("stock/products/" + Grocy.Components.ProductPicker.GetValue() + '/entries?include_sub_products=true', + function(stockEntries) + { + stockEntries.forEach(stockEntry => + { + var openTxt = __t("Not opened"); + if (stockEntry.open == 1) + { + openTxt = __t("Opened"); + } + + if (stockEntry.location_id == locationId) + { + $("#specific_stock_entry").append($(""); + if ($("#use_specific_stock_entry").is(":checked")) + { + $("#use_specific_stock_entry").click(); + } + $("#location_id").val(""); + + var productId = $(e.target).val(); + + if (productId) + { + Grocy.Components.ProductCard.Refresh(productId); + + Grocy.Api.Get('stock/products/' + productId, + function(productDetails) { - $("#specific_stock_entry").find("option").remove().end().append(""); - if ($("#use_specific_stock_entry").is(":checked")) - { - $("#use_specific_stock_entry").click(); - } - - Grocy.FrontendHelpers.EndUiBusy("consume-form"); - toastr.success(__t('Marked %1$s of %2$s as opened', parseFloat(jsonForm.amount).toLocaleString({ minimumFractionDigits: 0, maximumFractionDigits: Grocy.UserSettings.stock_decimal_places_amounts }) + " " + __n(jsonForm.amount, productDetails.quantity_unit_stock.name, productDetails.quantity_unit_stock.name_plural), productDetails.product.name) + '
' + __t("Undo") + ''); - + current_productDetails = productDetails; + + Grocy.Components.ProductAmountPicker.Reload(productDetails.product.id, productDetails.quantity_unit_stock.id); + Grocy.Components.ProductAmountPicker.SetQuantityUnit(productDetails.quantity_unit_stock.id); if (BoolVal(Grocy.UserSettings.stock_default_consume_amount_use_quick_consume_amount)) { $('#display_amount').val(productDetails.product.quick_consume_amount); @@ -197,447 +346,307 @@ $('#save-mark-as-open-button').on('click', function(e) } RefreshLocaleNumberInput(); $(".input-group-productamountpicker").trigger("change"); - Grocy.Components.ProductPicker.Clear(); - Grocy.Components.ProductPicker.GetInputElement().focus(); - Grocy.FrontendHelpers.ValidateForm('consume-form'); + + $("#location_id").find("option").remove().end().append(""); + Grocy.Api.Get("stock/products/" + productId + '/locations?include_sub_products=true', + function(stockLocations) + { + var setDefault = 0; + stockLocations.forEach(stockLocation => + { + if (productDetails.location.id == stockLocation.location_id) + { + $("#location_id").append($(""); - if ($("#use_specific_stock_entry").is(":checked")) + }); + + $('#display_amount').val(parseFloat(Grocy.UserSettings.stock_default_consume_amount)); + $(".input-group-productamountpicker").trigger("change"); + Grocy.FrontendHelpers.ValidateForm('consume-form'); + + $('#display_amount').on('focus', function(e) { - $("#use_specific_stock_entry").click(); - } - - if (GetUriParam("embedded") !== undefined) + $(this).select(); + }); + + $('#price').on('focus', function(e) { - stockId = GetUriParam('stockId'); - } - else + $(this).select(); + }); + + + $('#consume-form input').keyup(function(event) { - // try to get stock id from grocycode - if ($("#product_id").data("grocycode")) + Grocy.FrontendHelpers.ValidateForm('consume-form'); + }); + + $('#consume-form select').change(function(event) + { + Grocy.FrontendHelpers.ValidateForm('consume-form'); + }); + + $('#consume-form input').keydown(function(event) + { + if (event.keyCode === 13) //Enter { - var gc = $("#product_id").attr("barcode").split(":"); - if (gc.length == 4) + event.preventDefault(); + + if (document.getElementById('consume-form').checkValidity() === false) //There is at least one validation error { - stockId = gc[3]; + return false; + } + else + { + $('#save-consume-button').click(); } } - } - - if (locationId) + }); + + $("#specific_stock_entry").on("change", function(e) { - Grocy.Api.Get("stock/products/" + Grocy.Components.ProductPicker.GetValue() + '/entries?include_sub_products=true', - function(stockEntries) - { - stockEntries.forEach(stockEntry => - { - var openTxt = __t("Not opened"); - if (stockEntry.open == 1) - { - openTxt = __t("Opened"); - } - - if (stockEntry.location_id == locationId) - { - $("#specific_stock_entry").append($(""); - if ($("#use_specific_stock_entry").is(":checked")) - { - $("#use_specific_stock_entry").click(); - } - $("#location_id").val(""); - - var productId = $(e.target).val(); - - if (productId) - { - Grocy.Components.ProductCard.Refresh(productId); - - Grocy.Api.Get('stock/products/' + productId, - function(productDetails) - { - current_productDetails = productDetails; - - Grocy.Components.ProductAmountPicker.Reload(productDetails.product.id, productDetails.quantity_unit_stock.id); - Grocy.Components.ProductAmountPicker.SetQuantityUnit(productDetails.quantity_unit_stock.id); - if (BoolVal(Grocy.UserSettings.stock_default_consume_amount_use_quick_consume_amount)) - { - $('#display_amount').val(productDetails.product.quick_consume_amount); - } - else - { - $('#display_amount').val(parseFloat(Grocy.UserSettings.stock_default_consume_amount)); - } - RefreshLocaleNumberInput(); - $(".input-group-productamountpicker").trigger("change"); - - $("#location_id").find("option").remove().end().append(""); - Grocy.Api.Get("stock/products/" + productId + '/locations?include_sub_products=true', - function(stockLocations) - { - var setDefault = 0; - stockLocations.forEach(stockLocation => - { - if (productDetails.location.id == stockLocation.location_id) - { - $("#location_id").append($("