diff --git a/js/viewjs/about.js b/js/viewjs/about.js index 2f1dcb7a..a5ba7792 100644 --- a/js/viewjs/about.js +++ b/js/viewjs/about.js @@ -6,15 +6,15 @@ $scope = $(scope).find; } - $('[data-toggle="collapse-next"]').on("click", function(e) + $scope('[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"); + $scope(".nav-tabs a[href='#changelog']").tab("show"); } - + } diff --git a/js/viewjs/barcodescannertesting.js b/js/viewjs/barcodescannertesting.js index 52104d67..ad96a721 100644 --- a/js/viewjs/barcodescannertesting.js +++ b/js/viewjs/barcodescannertesting.js @@ -9,99 +9,99 @@ Grocy.Use("barcodescanner"); Grocy.BarCodeScannerTestingHitCount = 0; Grocy.BarCodeScannerTestingMissCount = 0; - - $("#scanned_barcode").on("blur", function(e) + + $scope("#scanned_barcode").on("blur", function(e) { - OnBarcodeScanned($("#scanned_barcode").val()); + OnBarcodeScanned($scope("#scanned_barcode").val()); }); - - $("#scanned_barcode").keydown(function(event) + + $scope("#scanned_barcode").keydown(function(event) { if (event.keyCode === 13) //Enter { event.preventDefault(); - OnBarcodeScanned($("#scanned_barcode").val()); + OnBarcodeScanned($scope("#scanned_barcode").val()); } }); - - $("#expected_barcode").on("keyup", function(e) + + $scope("#expected_barcode").on("keyup", function(e) { - if ($("#expected_barcode").val().length > 1) + if ($scope("#expected_barcode").val().length > 1) { - $("#scanned_barcode").removeAttr("disabled"); - $("#barcodescanner-start-button").removeAttr("disabled"); - $("#barcodescanner-start-button").removeClass("disabled"); + $scope("#scanned_barcode").removeAttr("disabled"); + $scope("#barcodescanner-start-button").removeAttr("disabled"); + $scope("#barcodescanner-start-button").removeClass("disabled"); } else { - $("#scanned_barcode").attr("disabled", ""); - $("#barcodescanner-start-button").attr("disabled", ""); - $("#barcodescanner-start-button").addClass("disabled"); + $scope("#scanned_barcode").attr("disabled", ""); + $scope("#barcodescanner-start-button").attr("disabled", ""); + $scope("#barcodescanner-start-button").addClass("disabled"); } }); - - $("#expected_barcode").focus(); + + $scope("#expected_barcode").focus(); setTimeout(function() { - $("#barcodescanner-start-button").attr("disabled", ""); - $("#barcodescanner-start-button").addClass("disabled"); + $scope("#barcodescanner-start-button").attr("disabled", ""); + $scope("#barcodescanner-start-button").addClass("disabled"); }, 200); - + if (GetUriParam("barcode") !== undefined) { - $("#expected_barcode").val(GetUriParam("barcode")); + $scope("#expected_barcode").val(GetUriParam("barcode")); setTimeout(function() { - $("#expected_barcode").keyup(); - $("#scanned_barcode").focus(); + $scope("#expected_barcode").keyup(); + $scope("#scanned_barcode").focus(); }, 200); } - + function OnBarcodeScanned(barcode) { if (barcode.length === 0) { return; } - + var bgClass = ""; - if (barcode != $("#expected_barcode").val()) + if (barcode != $scope("#expected_barcode").val()) { Grocy.BarCodeScannerTestingMissCount++; bgClass = "bg-danger"; - - $("#miss-count").text(Grocy.BarCodeScannerTestingMissCount); + + $scope("#miss-count").text(Grocy.BarCodeScannerTestingMissCount); animateCSS("#miss-count", "pulse"); } else { Grocy.BarCodeScannerTestingHitCount++; bgClass = "bg-success"; - - $("#hit-count").text(Grocy.BarCodeScannerTestingHitCount); + + $scope("#hit-count").text(Grocy.BarCodeScannerTestingHitCount); animateCSS("#hit-count", "pulse"); } - - $("#scanned_codes").prepend(""); + + $scope("#scanned_codes").prepend(""); setTimeout(function() { $("#scanned_barcode").val(""); - - if (!$(":focus").is($("#expected_barcode"))) + + if (!$scope(":focus").is($scope("#expected_barcode"))) { - $("#scanned_barcode").focus(); + $scope("#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 334c782c..1102fddc 100644 --- a/js/viewjs/batteries.js +++ b/js/viewjs/batteries.js @@ -6,7 +6,7 @@ $scope = $(scope).find; } - var batteriesTable = $('#batteries-table').DataTable({ + var batteriesTable = $scope('#batteries-table').DataTable({ 'order': [[1, 'asc']], 'columnDefs': [ { 'orderable': false, 'targets': 0 }, @@ -14,14 +14,14 @@ { "type": "num", "targets": 4 } ].concat($.fn.dataTable.defaults.columnDefs) }); - $('#batteries-table tbody').removeClass("d-none"); + $scope('#batteries-table tbody').removeClass("d-none"); Grocy.FrontendHelpers.InitDataTable(batteriesTable, null, function() { - $("#search").val(""); + $scope("#search").val(""); batteriesTable.search("").draw(); - $("#show-disabled").prop('checked', false); + $scope("#show-disabled").prop('checked', false); }); - + Grocy.FrontendHelpers.MakeDeleteConfirmBox( 'Are you sure to delete battery "%s"?', '.battery-delete-button', @@ -30,8 +30,8 @@ 'objects/batteries/', '/batteries' ); - - $("#show-disabled").change(function() + + $scope("#show-disabled").change(function() { if (this.checked) { @@ -42,10 +42,10 @@ window.location.href = U('/batteries'); } }); - + if (GetUriParam('include_disabled')) { - $("#show-disabled").prop('checked', true); + $scope("#show-disabled").prop('checked', true); } - + } diff --git a/js/viewjs/batteriesjournal.js b/js/viewjs/batteriesjournal.js index 9e64f6f2..0243959e 100644 --- a/js/viewjs/batteriesjournal.js +++ b/js/viewjs/batteriesjournal.js @@ -6,7 +6,7 @@ $scope = $(scope).find; } - var batteriesJournalTable = $('#batteries-journal-table').DataTable({ + var batteriesJournalTable = $scope('#batteries-journal-table').DataTable({ 'paginate': true, 'order': [[2, 'desc']], 'columnDefs': [ @@ -14,23 +14,25 @@ { 'searchable': false, "targets": 0 } ].concat($.fn.dataTable.defaults.columnDefs) }); - $('#batteries-journal-table tbody').removeClass("d-none"); + $scope('#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"); + $scope("#battery-filter").val(GetUriParam("battery")); + $scope("#battery-filter").trigger("change"); } - - $(document).on('click', '.undo-battery-execution-button', function(e) + + var top = scope != null ? $(scope) : $(document); + + top.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) { @@ -46,5 +48,5 @@ } ); }); - + } diff --git a/js/viewjs/batteriesoverview.js b/js/viewjs/batteriesoverview.js index be0bbd53..bd537767 100644 --- a/js/viewjs/batteriesoverview.js +++ b/js/viewjs/batteriesoverview.js @@ -6,9 +6,9 @@ $scope = $(scope).find; } - Grocy.Use("batterycard"); - - var batteriesOverviewTable = $('#batteries-overview-table').DataTable({ + Grocy.Use("batterycard", scope); + + var batteriesOverviewTable = $scope('#batteries-overview-table').DataTable({ 'order': [[4, 'asc']], 'columnDefs': [ { 'orderable': false, 'targets': 0 }, @@ -17,36 +17,38 @@ { "type": "html", "targets": 4 } ].concat($.fn.dataTable.defaults.columnDefs) }); - $('#batteries-overview-table tbody').removeClass("d-none"); - + $scope('#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) + + var top = scope != null ? $(scope) : $(document); + + top.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 batteryId = $scope(e.currentTarget).attr('data-battery-id'); + var batteryName = $scope(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 batteryRow = $scope('#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)) @@ -57,17 +59,17 @@ { 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); + + $scope('#battery-' + batteryId + '-last-tracked-time').text(trackedTime); + $scope('#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); + $scope('#battery-' + batteryId + '-next-charge-time').text(result.next_estimated_charge_time); + $scope('#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"); @@ -87,16 +89,18 @@ } ); }); - - $(document).on("click", ".battery-name-cell", function(e) + + + + top.on("click", ".battery-name-cell", function(e) { - Grocy.Components.BatteryCard.Refresh($(e.currentTarget).attr("data-battery-id")); - $("#batteriesoverview-batterycard-modal").modal("show"); + Grocy.Components.BatteryCard.Refresh($scope(e.currentTarget).attr("data-battery-id")); + $scope("#batteriesoverview-batterycard-modal").modal("show"); }); - + function RefreshStatistics() { - var nextXDays = $("#info-due-batteries").data("next-x-days"); + var nextXDays = $scope("#info-due-batteries").data("next-x-days"); Grocy.Api.Get('batteries', function(result) { @@ -116,9 +120,9 @@ 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')); + + $scope("#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')); + $scope("#info-overdue-batteries").html('' + overdueCount + ' ' + __n(overdueCount, '%s battery is overdue to be charged', '%s batteries are overdue to be charged')); }, function(xhr) { @@ -126,7 +130,7 @@ } ); } - + RefreshStatistics(); - + } diff --git a/js/viewjs/batteriessettings.js b/js/viewjs/batteriessettings.js index e2651692..0b652f15 100644 --- a/js/viewjs/batteriessettings.js +++ b/js/viewjs/batteriessettings.js @@ -7,9 +7,9 @@ } Grocy.Use("numberpicker"); - - $("#batteries_due_soon_days").val(Grocy.UserSettings.batteries_due_soon_days); - + + $scope("#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 116e5d97..856910b3 100644 --- a/js/viewjs/batteryform.js +++ b/js/viewjs/batteryform.js @@ -1,4 +1,6 @@ -function batteryformView(Grocy, scope = null) +import { WindowMessageBag } from '../helpers/messagebag'; + +function batteryformView(Grocy, scope = null) { var $scope = $; if (scope != null) @@ -6,23 +8,21 @@ $scope = $(scope).find; } - import { WindowMessageBag } from '../helpers/messagebag'; - Grocy.Use("numberpicker"); Grocy.Use("userfieldsform"); - - $('#save-battery-button').on('click', function(e) + + $scope('#save-battery-button').on('click', function(e) { e.preventDefault(); - + if ($(".combobox-menu-visible").length) { return; } - - var jsonData = $('#battery-form').serializeJSON(); + + var jsonData = $scope('#battery-form').serializeJSON(); Grocy.FrontendHelpers.BeginUiBusy("battery-form"); - + if (Grocy.EditMode === 'create') { Grocy.Api.Post('objects/batteries', jsonData, @@ -73,18 +73,18 @@ ); } }); - - $('#battery-form input').keyup(function(event) + + $scope('#battery-form input').keyup(function(event) { Grocy.FrontendHelpers.ValidateForm('battery-form'); }); - - $('#battery-form input').keydown(function(event) + + $scope('#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; @@ -95,9 +95,9 @@ } } }); - + Grocy.Components.UserfieldsForm.Load(); - $('#name').focus(); + $scope('#name').focus(); Grocy.FrontendHelpers.ValidateForm('battery-form'); - + } diff --git a/js/viewjs/batterytracking.js b/js/viewjs/batterytracking.js index 0410b6b7..921e9d0d 100644 --- a/js/viewjs/batterytracking.js +++ b/js/viewjs/batterytracking.js @@ -8,35 +8,35 @@ Grocy.Use("batterycard"); Grocy.Use("datetimepicker"); - - $('#save-batterytracking-button').on('click', function(e) + + $scope('#save-batterytracking-button').on('click', function(e) { e.preventDefault(); - - if ($(".combobox-menu-visible").length) + + if ($scope(".combobox-menu-visible").length) { return; } - - var jsonForm = $('#batterytracking-form').serializeJSON(); + + var jsonForm = $scope('#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() }, + Grocy.Api.Post('batteries/' + jsonForm.battery_id + '/charge', { 'tracked_time': $scope('#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") + ''); + toastr.success(__t('Tracked charge cycle of battery %1$s on %2$s', batteryDetails.battery.name, $scope('#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'); + + $scope('#battery_id').val(''); + $scope('#battery_id_text_input').focus(); + $scope('#battery_id_text_input').val(''); + $scope('#tracked_time').find('input').val(moment().format('YYYY-MM-DD HH:mm:ss')); + $scope('#tracked_time').find('input').trigger('change'); + $scope('#battery_id_text_input').trigger('change'); Grocy.FrontendHelpers.ValidateForm('batterytracking-form'); }, function(xhr) @@ -53,61 +53,58 @@ } ); }); - - $('#battery_id').on('change', function(e) + + $scope('#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(); + var input = $scope('#battery_id_text_input').val().toString(); + $scope('#battery_id_text_input').val(input); + $scope('#battery_id').data('combobox').refresh(); + + var batteryId = $scope(e.target).val(); if (batteryId) { Grocy.Components.BatteryCard.Refresh(batteryId); - $('#tracked_time').find('input').focus(); + $scope('#tracked_time').find('input').focus(); Grocy.FrontendHelpers.ValidateForm('batterytracking-form'); } }); - - $('.combobox').combobox({ + + $scope('.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'); + + $scope('#battery_id').val(''); + $scope('#battery_id_text_input').focus(); + $scope('#battery_id_text_input').val(''); + $scope('#battery_id_text_input').trigger('change'); Grocy.Components.DateTimePicker.GetInputElement().trigger('input'); Grocy.FrontendHelpers.ValidateForm('batterytracking-form'); - - $('#batterytracking-form input').keyup(function(event) + + $scope('#batterytracking-form input').keyup(function(event) { Grocy.FrontendHelpers.ValidateForm('batterytracking-form'); }); - - $('#batterytracking-form input').keydown(function(event) + + $scope('#batterytracking-form input').keydown(function(event) { if (event.keyCode === 13) //Enter { event.preventDefault(); - + if (document.getElementById('batterytracking-form').checkValidity() === false) //There is at least one validation error { return false; } else { - $('#save-batterytracking-button').click(); + $scope('#save-batterytracking-button').click(); } } }); - - $('#tracked_time').find('input').on('keypress', function(e) + + $scope('#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 ea0e2a1a..965969e0 100644 --- a/js/viewjs/calendar.js +++ b/js/viewjs/calendar.js @@ -1,26 +1,30 @@ -function calendarView(Grocy, scope = null) + +/* 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'; + +function calendarView(Grocy, scope = null) { var $scope = $; + var $viewport = $(window); + if (scope != null) { $scope = $(scope).find; + $viewport = $(scope); } - /* 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", @@ -30,7 +34,7 @@ right: "prev,next" }, weekNumbers: Grocy.CalendarShowWeekNumbers, - defaultView: ($(window).width() < 768) ? "timeGridDay" : "dayGridMonth", + defaultView: ($viewport.width() < 768) ? "timeGridDay" : "dayGridMonth", firstDay: firstDay, eventLimit: false, height: "auto", @@ -48,26 +52,26 @@ window.location.href = info.link; } }; - + 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) + + $scope("#ical-button").on("click", function(e) { e.preventDefault(); - + Grocy.Api.Get('calendar/ical/sharing-link', function(result) { @@ -84,12 +88,12 @@ } ); }); - + $(window).one("resize", function() { // Automatically switch the calendar to "basicDay" view on small screens // and to "month" otherwise - if ($(window).width() < 768) + if ($viewport.width() < 768) { calendar.changeView("timeGridDay"); } @@ -98,5 +102,7 @@ calendar.changeView("dayGridMonth"); } }); - + } + +window.calendarView = calendarView; \ No newline at end of file diff --git a/js/viewjs/choreform.js b/js/viewjs/choreform.js index 7fec5809..21b901e0 100644 --- a/js/viewjs/choreform.js +++ b/js/viewjs/choreform.js @@ -8,24 +8,24 @@ Grocy.Use("numberpicker"); Grocy.Use("userfieldsform"); - - $('#save-chore-button').on('click', function(e) + + $scope('#save-chore-button').on('click', function(e) { e.preventDefault(); - - if ($(".combobox-menu-visible").length) + + if ($scope(".combobox-menu-visible").length) { return; } - - var jsonData = $('#chore-form').serializeJSON(); + + var jsonData = $scope('#chore-form').serializeJSON(); if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_CHORES_ASSIGNMENTS) { - jsonData.assignment_config = $("#assignment_config").val().join(","); + jsonData.assignment_config = $scope("#assignment_config").val().join(","); } - + Grocy.FrontendHelpers.BeginUiBusy("chore-form"); - + if (Grocy.EditMode === 'create') { Grocy.Api.Post('objects/chores', jsonData, @@ -82,164 +82,164 @@ ); } }); - + $('#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(); + $scope('#save-chore-button').click(); } } }); - - var checkboxValues = $("#period_config").val().split(","); + + var checkboxValues = $scope("#period_config").val().split(","); for (var i = 0; i < checkboxValues.length; i++) { if (!checkboxValues[i].isEmpty()) { - $("#" + checkboxValues[i]).prop('checked', true); + $scope("#" + checkboxValues[i]).prop('checked', true); } } - + Grocy.Components.UserfieldsForm.Load(); - $('#name').focus(); + $scope('#name').focus(); Grocy.FrontendHelpers.ValidateForm('chore-form'); - + setTimeout(function() { - $(".input-group-chore-period-type").trigger("change"); - $(".input-group-chore-assignment-type").trigger("change"); - + $scope(".input-group-chore-period-type").trigger("change"); + $scope(".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(); - + $scope("#consume_product_on_execution").click(); + $scope("#consume_product_on_execution").click(); + Grocy.Components.ProductPicker.GetPicker().trigger('change'); }, 100); - - $('.input-group-chore-period-type').on('change', function(e) + + $scope('.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(""); - + var periodType = $scope('#period_type').val(); + var periodDays = $scope('#period_days').val(); + var periodInterval = $scope('#period_interval').val(); + + $scope(".period-type-input").addClass("d-none"); + $scope(".period-type-" + periodType).removeClass("d-none"); + $scope('#chore-period-type-info').attr("data-original-title", ""); + $scope("#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')); + $scope('#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())); + $scope("label[for='period_days']").text(__t("Period days")); + $scope("#period_days").attr("min", "0"); + $scope("#period_days").removeAttr("max"); + $scope('#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())); + $scope('#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')); + $scope('#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())); + $scope('#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')); + $scope("#period_config").val($(".period-type-weekly input:checkbox:checked").map(function() { return this.value; }).get().join(",")); + $scope('#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())); + $scope('#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')); + $scope("label[for='period_days']").text(__t("Day of month")); + $scope("#period_days").attr("min", "1"); + $scope("#period_days").attr("max", "31"); + $scope('#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())); + $scope('#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')); + $scope('#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) + + $scope('.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", ""); - + var assignmentType = $scope('#assignment_type').val(); + + $scope('#chore-period-assignment-info').text(""); + $scope("#assignment_config").removeAttr("required"); + $scope("#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')); + $scope('#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"); + $scope('#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')); + $scope("#assignment_config").attr("required", ""); + $scope("#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"); + $scope('#chore-assignment-type-info').attr("data-original-title", __t('This means the next execution of this chore will be assigned randomly')); + $scope("#assignment_config").attr("required", ""); + $scope("#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"); + $scope('#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')); + $scope("#assignment_config").attr("required", ""); + $scope("#assignment_config").removeAttr("disabled"); } - - Grocy.FrontendHelpers.ValidateForm('chore-form'); + + Grocy.FrontendHelpers.ValidateForm('chore-form', $scope); }); - - $("#consume_product_on_execution").on("click", function() + + $scope("#consume_product_on_execution").on("click", function() { if (this.checked) { Grocy.Components.ProductPicker.Enable(); - $("#product_amount").removeAttr("disabled"); + $scope("#product_amount").removeAttr("disabled"); } else { Grocy.Components.ProductPicker.Disable(); - $("#product_amount").attr("disabled", ""); + $scope("#product_amount").attr("disabled", ""); } - + Grocy.FrontendHelpers.ValidateForm("chore-form"); }); - + Grocy.Components.ProductPicker.GetPicker().on('change', function(e) { - var productId = $(e.target).val(); - + var productId = $scope(e.target).val(); + if (productId) { Grocy.Api.Get('stock/products/' + productId, function(productDetails) { - $('#amount_qu_unit').text(productDetails.quantity_unit_stock.name); + $scope('#amount_qu_unit').text(productDetails.quantity_unit_stock.name); }, function(xhr) { @@ -248,5 +248,5 @@ ); } }); - + } diff --git a/js/viewjs/chores.js b/js/viewjs/chores.js index e1d39024..6093fef0 100644 --- a/js/viewjs/chores.js +++ b/js/viewjs/chores.js @@ -6,21 +6,21 @@ $scope = $(scope).find; } - var choresTable = $('#chores-table').DataTable({ + var choresTable = $scope('#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"); + $scope('#chores-table tbody').removeClass("d-none"); Grocy.FrontendHelpers.InitDataTable(choresTable, null, function() { - $("#search").val(""); + $scope("#search").val(""); choresTable.search("").draw(); - $("#show-disabled").prop('checked', false); + $scope("#show-disabled").prop('checked', false); }); - + Grocy.FrontendHelpers.MakeDeleteConfirmBox( 'Are you sure to delete chore "%s"?', '.core-delete-button', @@ -29,7 +29,7 @@ 'objects/chores/', '/chroes' ); - + $("#show-disabled").change(function() { if (this.checked) @@ -41,10 +41,10 @@ window.location.href = U('/chores'); } }); - + if (GetUriParam('include_disabled')) { - $("#show-disabled").prop('checked', true); + $scope("#show-disabled").prop('checked', true); } - + } diff --git a/js/viewjs/choresjournal.js b/js/viewjs/choresjournal.js index 62b59a7c..07610509 100644 --- a/js/viewjs/choresjournal.js +++ b/js/viewjs/choresjournal.js @@ -6,7 +6,7 @@ $scope = $(scope).find; } - var choresJournalTable = $('#chores-journal-table').DataTable({ + var choresJournalTable = $scope('#chores-journal-table').DataTable({ 'paginate': true, 'order': [[2, 'desc']], 'columnDefs': [ @@ -14,23 +14,25 @@ { 'searchable': false, "targets": 0 } ].concat($.fn.dataTable.defaults.columnDefs) }); - $('#chores-journal-table tbody').removeClass("d-none"); + $scope('#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"); + $scope("#chore-filter").val(GetUriParam("chore")); + $scope("#chore-filter").trigger("change"); } - - $(document).on('click', '.undo-chore-execution-button', function(e) + + var top = scope != null ? $(scope) : $(document); + + top.on('click', '.undo-chore-execution-button', function(e) { e.preventDefault(); - - var element = $(e.currentTarget); + + var element = $scope(e.currentTarget); var executionId = element.attr('data-execution-id'); - + Grocy.Api.Post('chores/executions/' + executionId.toString() + '/undo', {}, function(result) { @@ -46,5 +48,5 @@ } ); }); - + } diff --git a/js/viewjs/choresoverview.js b/js/viewjs/choresoverview.js index 767743b8..706d68b3 100644 --- a/js/viewjs/choresoverview.js +++ b/js/viewjs/choresoverview.js @@ -1,14 +1,16 @@ function choresoverviewView(Grocy, scope = null) { var $scope = $; + var top = scope != null ? $(scope) : $(document); + if (scope != null) { $scope = $(scope).find; } Grocy.Use("chorecard"); - - var choresOverviewTable = $('#chores-overview-table').DataTable({ + + var choresOverviewTable = $scope('#chores-overview-table').DataTable({ 'order': [[2, 'asc']], 'columnDefs': [ { 'orderable': false, 'targets': 0 }, @@ -18,37 +20,37 @@ { "type": "html", "targets": 3 } ].concat($.fn.dataTable.defaults.columnDefs) }); - $('#chores-overview-table tbody').removeClass("d-none"); + $scope('#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() + + $scope("#user-filter").on("change", function() { var user = $(this).val(); if (user !== null && !user.isEmpty()) { - UpdateUriParam("user", $("#user-filter option:selected").data("user-id")); + UpdateUriParam("user", $scope("#user-filter option:selected").data("user-id")); } else { RemoveUriParam("user") } }); - - $(document).on('click', '.track-chore-button', function(e) + + top.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'); - + + var choreId = $scope(e.currentTarget).attr('data-chore-id'); + var choreName = $scope(e.currentTarget).attr('data-chore-name'); + Grocy.Api.Get('objects/chores/' + choreId, function(chore) { @@ -57,60 +59,60 @@ { 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 choreRow = $scope('#chore-' + choreId + '-row'); + var nextXDaysThreshold = moment().add($scope("#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(""); + $scope('#chore-' + choreId + '-due-filter-column').html(""); if (nextExecutionTime.isBefore(now)) { choreRow.addClass("table-danger"); - $('#chore-' + choreId + '-due-filter-column').html("overdue"); + $scope('#chore-' + choreId + '-due-filter-column').html("overdue"); } else if (nextExecutionTime.isBefore(nextXDaysThreshold)) { choreRow.addClass("table-warning"); - $('#chore-' + choreId + '-due-filter-column').html("duesoon"); + $scope('#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); - + + $scope('#chore-' + choreId + '-last-tracked-time').text(trackedTime); + $scope('#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); + $scope('#chore-' + choreId + '-next-execution-time').text(result.next_estimated_execution_time); + $scope('#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); + $scope('#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"); + $scope(".input-group-filter").trigger("change"); }, 550); }, function(xhr) @@ -134,16 +136,16 @@ } ); }); - - $(document).on("click", ".chore-name-cell", function(e) + + top.on("click", ".chore-name-cell", function(e) { - Grocy.Components.ChoreCard.Refresh($(e.currentTarget).attr("data-chore-id")); - $("#choresoverview-chorecard-modal").modal("show"); + Grocy.Components.ChoreCard.Refresh($scope(e.currentTarget).attr("data-chore-id")); + $scope("#choresoverview-chorecard-modal").modal("show"); }); - + function RefreshStatistics() { - var nextXDays = $("#info-due-chores").data("next-x-days"); + var nextXDays = $scope("#info-due-chores").data("next-x-days"); Grocy.Api.Get('chores', function(result) { @@ -163,16 +165,16 @@ { 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')); + + $scope("#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')); + $scope("#info-overdue-chores").html('' + overdueCount + ' ' + __n(overdueCount, '%s chore is overdue to be done', '%s chores are overdue to be done')); + $scope("#info-assigned-to-me-chores").html('' + assignedToMeCount + ' ' + __n(assignedToMeCount, '%s chore is assigned to me', '%s chores are assigned to me')); }, function(xhr) { @@ -180,13 +182,13 @@ } ); } - + if (GetUriParam("user") !== undefined) { - $("#user-filter").val("xx" + GetUriParam("user") + "xx"); - $("#user-filter").trigger("change"); + $scope("#user-filter").val("xx" + GetUriParam("user") + "xx"); + $scope("#user-filter").trigger("change"); } - + RefreshStatistics(); - + } diff --git a/js/viewjs/choressettings.js b/js/viewjs/choressettings.js index 49c0a9a5..4c96e8ce 100644 --- a/js/viewjs/choressettings.js +++ b/js/viewjs/choressettings.js @@ -7,9 +7,9 @@ } Grocy.Use("numberpicker"); - - $("#chores_due_soon_days").val(Grocy.UserSettings.chores_due_soon_days); - + + $scope("#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 a8937ee4..01b4ee99 100644 --- a/js/viewjs/choretracking.js +++ b/js/viewjs/choretracking.js @@ -9,34 +9,34 @@ Grocy.Use("chorecard"); Grocy.Use("datetimepicker"); Grocy.Use("userpicker"); - - $('#save-choretracking-button').on('click', function(e) + + $scope('#save-choretracking-button').on('click', function(e) { e.preventDefault(); - - if ($(".combobox-menu-visible").length) + + if ($scope(".combobox-menu-visible").length) { return; } - - var jsonForm = $('#choretracking-form').serializeJSON(); + + var jsonForm = $scope('#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() }, + Grocy.Api.Post('chores/' + jsonForm.chore_id + '/execute', { 'tracked_time': Grocy.Components.DateTimePicker.GetValue(), 'done_by': $scope("#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.ChoreCard.Refresh($scope('#chore_id').val()); + + $scope('#chore_id').val(''); + $scope('#chore_id_text_input').focus(); + $scope('#chore_id_text_input').val(''); Grocy.Components.DateTimePicker.SetValue(moment().format('YYYY-MM-DD HH:mm:ss')); - $('#chore_id_text_input').trigger('change'); + $scope('#chore_id_text_input').trigger('change'); Grocy.FrontendHelpers.ValidateForm('choretracking-form'); }, function(xhr) @@ -53,14 +53,14 @@ } ); }); - - $('#chore_id').on('change', function(e) + + $scope('#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(); + var input = $scope('#chore_id_text_input').val().toString(); + $scope('#chore_id_text_input').val(input); + $scope('#chore_id').data('combobox').refresh(); + + var choreId = $scope(e.target).val(); if (choreId) { Grocy.Api.Get('objects/chores/' + choreId, @@ -82,48 +82,48 @@ console.error(xhr); } ); - + Grocy.Components.ChoreCard.Refresh(choreId); Grocy.Components.DateTimePicker.GetInputElement().focus(); Grocy.FrontendHelpers.ValidateForm('choretracking-form'); } }); - - $('.combobox').combobox({ + + $scope('.combobox').combobox({ appendId: '_text_input', bsVersion: '4' }); - - $('#chore_id_text_input').focus(); - $('#chore_id_text_input').trigger('change'); + + $scope('#chore_id_text_input').focus(); + $scope('#chore_id_text_input').trigger('change'); Grocy.Components.DateTimePicker.GetInputElement().trigger('input'); Grocy.FrontendHelpers.ValidateForm('choretracking-form'); - - $('#choretracking-form input').keyup(function(event) + + $scope('#choretracking-form input').keyup(function(event) { Grocy.FrontendHelpers.ValidateForm('choretracking-form'); }); - - $('#choretracking-form input').keydown(function(event) + + $scope('#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(); + $scope('#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 8188e2e9..77136317 100644 --- a/js/viewjs/consume.js +++ b/js/viewjs/consume.js @@ -1,54 +1,56 @@ -function consumeView(Grocy, scope = null) +import { BoolVal } from '../helpers/extensions'; +import { WindowMessageBag } from '../helpers/messagebag'; + +function consumeView(Grocy, scope = null) { var $scope = $; + var top = scope != null ? $(scope) : $(document); + if (scope != null) { $scope = $(scope).find; } - 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) + + $scope('#save-consume-button').on('click', function(e) { e.preventDefault(); - - if ($(".combobox-menu-visible").length) + + if ($scope(".combobox-menu-visible").length) { return; } - - var jsonForm = $('#consume-form').serializeJSON(); + + var jsonForm = $scope('#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.exact_amount = $scope('#consume-exact-amount').is(':checked'); + jsonData.spoiled = $scope('#spoiled').is(':checked'); jsonData.allow_subproduct_substitution = true; - - if ($("#use_specific_stock_entry").is(":checked")) + + if ($scope("#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(); + jsonData.location_id = $scope("#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) @@ -60,21 +62,21 @@ { 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'); + $scope("#flow-info-InplaceAddBarcodeToExistingProduct").addClass("d-none"); + $scope('#barcode-lookup-disabled-hint').addClass('d-none'); + $scope('#barcode-lookup-hint').removeClass('d-none'); window.history.replaceState({}, document.title, U("/consume")); }, function(xhr) @@ -84,13 +86,13 @@ } ); } - - $("#specific_stock_entry").find("option").remove().end().append(""); - if ($("#use_specific_stock_entry").is(":checked")) + + $scope("#specific_stock_entry").find("option").remove().end().append(""); + if ($scope("#use_specific_stock_entry").is(":checked")) { - $("#use_specific_stock_entry").click(); + $scope("#use_specific_stock_entry").click(); } - + var successMessage = null; if (productDetails.product.enable_tare_weight_handling == 1 && !jsonData.exact_amount) { @@ -100,7 +102,7 @@ { 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); @@ -112,21 +114,21 @@ 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"); + $scope("#display_amount").attr("min", Grocy.DefaultMinAmount); + $scope("#display_amount").removeAttr("max"); if (BoolVal(Grocy.UserSettings.stock_default_consume_amount_use_quick_consume_amount)) { - $('#display_amount').val(productDetails.product.quick_consume_amount); + $scope('#display_amount').val(productDetails.product.quick_consume_amount); } else { - $('#display_amount').val(parseFloat(Grocy.UserSettings.stock_default_consume_amount)); + $scope('#display_amount').val(parseFloat(Grocy.UserSettings.stock_default_consume_amount)); } RefreshLocaleNumberInput(); - $(".input-group-productamountpicker").trigger("change"); - $("#tare-weight-handling-info").addClass("d-none"); + $scope(".input-group-productamountpicker").trigger("change"); + $scope("#tare-weight-handling-info").addClass("d-none"); Grocy.Components.ProductPicker.Clear(); if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_RECIPES) { @@ -134,12 +136,12 @@ } if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_LOCATION_TRACKING) { - $("#location_id").find("option").remove().end().append(""); + $scope("#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"); + $scope("#consume-exact-amount-group").addClass("d-none"); } }, function(xhr) @@ -156,55 +158,55 @@ } ); }); - - $('#save-mark-as-open-button').on('click', function(e) + + $scope('#save-mark-as-open-button').on('click', function(e) { e.preventDefault(); - - if ($(".combobox-menu-visible").length) + + if ($scope(".combobox-menu-visible").length) { return; } - - var jsonForm = $('#consume-form').serializeJSON(); + + var jsonForm = $scope('#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")) + + if ($scope("#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")) + $scope("#specific_stock_entry").find("option").remove().end().append(""); + if ($scope("#use_specific_stock_entry").is(":checked")) { - $("#use_specific_stock_entry").click(); + $scope("#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); + $scope('#display_amount').val(productDetails.product.quick_consume_amount); } else { - $('#display_amount').val(parseFloat(Grocy.UserSettings.stock_default_consume_amount)); + $scope('#display_amount').val(parseFloat(Grocy.UserSettings.stock_default_consume_amount)); } RefreshLocaleNumberInput(); - $(".input-group-productamountpicker").trigger("change"); + $scope(".input-group-productamountpicker").trigger("change"); Grocy.Components.ProductPicker.Clear(); Grocy.Components.ProductPicker.GetInputElement().focus(); Grocy.FrontendHelpers.ValidateForm('consume-form'); @@ -224,18 +226,18 @@ ); }); var sumValue = 0; - $("#location_id").on('change', function(e) + $scope("#location_id").on('change', function(e) { - var locationId = $(e.target).val(); + var locationId = $scope(e.target).val(); sumValue = 0; var stockId = null; - - $("#specific_stock_entry").find("option").remove().end().append(""); - if ($("#use_specific_stock_entry").is(":checked")) + + $scope("#specific_stock_entry").find("option").remove().end().append(""); + if ($scope("#use_specific_stock_entry").is(":checked")) { - $("#use_specific_stock_entry").click(); + $scope("#use_specific_stock_entry").click(); } - + if (GetUriParam("embedded") !== undefined) { stockId = GetUriParam('stockId'); @@ -243,16 +245,16 @@ else { // try to get stock id from grocycode - if ($("#product_id").data("grocycode")) + if ($scope("#product_id").data("grocycode")) { - var gc = $("#product_id").attr("barcode").split(":"); + var gc = $scope("#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', @@ -265,25 +267,25 @@ { openTxt = __t("Opened"); } - + if (stockEntry.location_id == locationId) { - $("#specific_stock_entry").append($(""); - if ($("#use_specific_stock_entry").is(":checked")) + + $scope("#specific_stock_entry").find("option").remove().end().append(""); + if ($scope("#use_specific_stock_entry").is(":checked")) { - $("#use_specific_stock_entry").click(); + $scope("#use_specific_stock_entry").click(); } - $("#location_id").val(""); - - var productId = $(e.target).val(); - + $scope("#location_id").val(""); + + var productId = $scope(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); + $scope('#display_amount').val(productDetails.product.quick_consume_amount); } else { - $('#display_amount').val(parseFloat(Grocy.UserSettings.stock_default_consume_amount)); + $scope('#display_amount').val(parseFloat(Grocy.UserSettings.stock_default_consume_amount)); } RefreshLocaleNumberInput(); - $(".input-group-productamountpicker").trigger("change"); - - $("#location_id").find("option").remove().end().append(""); + $scope(".input-group-productamountpicker").trigger("change"); + + $scope("#location_id").find("option").remove().end().append(""); Grocy.Api.Get("stock/products/" + productId + '/locations?include_sub_products=true', function(stockLocations) { @@ -356,29 +358,29 @@ { if (productDetails.location.id == stockLocation.location_id) { - $("#location_id").append($("