mirror of
https://github.com/grocy/grocy.git
synced 2026-04-05 20:36:15 +02:00
This *absolute commit monster* does the following things: - Introduce gulp to build javascript and css files. This includes moving node_modules out of the public/ folder. Use `gulp --tasks` to get a list of all tasks; however some of them are automatically generated. Use `gulp live` to watch for changes and automatically recompile what's needed. - Upgrade to yarn2 - Upgrade FullCalendar to 4.4.2 I know that 5.x is the current version, but two major version upgrades are too much right now. Also v5 would break any custom css as they renamed a bunch of classes. - Move Styles to sass (Most) global styles are now included in one sass file. This also means that we now compile our own bootstrap. - Javascript is now in strict mode Because everything is a module now, `use strict` is now in effect for all javascript files. There are probably still some parts left where implicit variable declarations were used. - grocy*.js were split up in modules. `window.Grocy` is now an instance of GrocyClass. API-wise nothing has changed (albeit some functions were added regarding Undo actions) At the Moment, this leaks a whole bunch of functions into window (that was easier than tracking those down). - FindObjectIn... style functions were removed. Array.prototype.find and Array.prototype.filter suffice. - Use babel to preprocess javascript. - Use rollup to bundle javascript. rollup bundles and tree-shakes es6 javascript bundles. It also allows to "import" css files and generate css files specific to this javascript file. This is used in viewjs scripts, for example when importing FullCalendar, to generate an associated viewcss file. - Use postcss to post-process css files. postcss uses autoprefixer to handle browser compatiblity. Internally this uses the package `browserslist`; and is currently configured to the default setting. - Minify assets when building in production `gulp publish` builds all assets in production mode, that is, the assets get minified. This includes javascript as well as css files. - css bundling concatCss is used to pull @imports of non-sass-files into one grocy.css - animate.css is now in the main bundle animate.css was used in so many places that it is now located in the main style bundle.
276 lines
8.5 KiB
JavaScript
276 lines
8.5 KiB
JavaScript
import { WindowMessageBag } from '../helpers/messagebag';
|
|
|
|
var stockEntriesTable = $('#stockentries-table').DataTable({
|
|
'order': [[2, 'asc']],
|
|
'columnDefs': [
|
|
{ 'orderable': false, 'targets': 0 },
|
|
{ 'searchable': false, "targets": 0 }
|
|
].concat($.fn.dataTable.defaults.columnDefs)
|
|
});
|
|
$('#stockentries-table tbody').removeClass("d-none");
|
|
stockEntriesTable.columns.adjust().draw();
|
|
|
|
$.fn.dataTable.ext.search.push(function(settings, data, dataIndex)
|
|
{
|
|
var productId = Grocy.Components.ProductPicker.GetValue();
|
|
|
|
if ((isNaN(productId) || productId == "" || productId == data[1]))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
|
|
$("#clear-filter-button").on("click", function()
|
|
{
|
|
Grocy.Components.ProductPicker.Clear();
|
|
stockEntriesTable.draw();
|
|
});
|
|
|
|
Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
|
|
{
|
|
stockEntriesTable.draw();
|
|
});
|
|
|
|
Grocy.Components.ProductPicker.GetInputElement().on('keyup', function(e)
|
|
{
|
|
stockEntriesTable.draw();
|
|
});
|
|
|
|
$(document).on('click', '.stock-consume-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 productId = $(e.currentTarget).attr('data-product-id');
|
|
var locationId = $(e.currentTarget).attr('data-location-id');
|
|
var specificStockEntryId = $(e.currentTarget).attr('data-stock-id');
|
|
var stockRowId = $(e.currentTarget).attr('data-stockrow-id');
|
|
var consumeAmount = $(e.currentTarget).attr('data-consume-amount');
|
|
|
|
var wasSpoiled = $(e.currentTarget).hasClass("stock-consume-button-spoiled");
|
|
|
|
Grocy.Api.Post('stock/products/' + productId + '/consume', { 'amount': consumeAmount, 'spoiled': wasSpoiled, 'location_id': locationId, 'stock_entry_id': specificStockEntryId, 'exact_amount': true },
|
|
function(bookingResponse)
|
|
{
|
|
Grocy.Api.Get('stock/products/' + productId,
|
|
function(result)
|
|
{
|
|
var toastMessage = __t('Removed %1$s of %2$s from stock', parseFloat(consumeAmount).toLocaleString({ minimumFractionDigits: 0, maximumFractionDigits: Grocy.UserSettings.stock_decimal_places_amounts }) + " " + __n(consumeAmount, result.quantity_unit_stock.name, result.quantity_unit_stock.name_plural), result.product.name) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="Grocy.UndoStockBookingEntry(' + bookingResponse.id + ',' + stockRowId + ')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>';
|
|
if (wasSpoiled)
|
|
{
|
|
toastMessage += " (" + __t("Spoiled") + ")";
|
|
}
|
|
|
|
Grocy.FrontendHelpers.EndUiBusy();
|
|
RefreshStockEntryRow(stockRowId);
|
|
toastr.success(toastMessage);
|
|
},
|
|
function(xhr)
|
|
{
|
|
Grocy.FrontendHelpers.EndUiBusy();
|
|
console.error(xhr);
|
|
}
|
|
);
|
|
},
|
|
function(xhr)
|
|
{
|
|
Grocy.FrontendHelpers.EndUiBusy();
|
|
console.error(xhr);
|
|
}
|
|
);
|
|
});
|
|
|
|
$(document).on('click', '.product-open-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 productId = $(e.currentTarget).attr('data-product-id');
|
|
var productName = $(e.currentTarget).attr('data-product-name');
|
|
var productQuName = $(e.currentTarget).attr('data-product-qu-name');
|
|
var specificStockEntryId = $(e.currentTarget).attr('data-stock-id');
|
|
var stockRowId = $(e.currentTarget).attr('data-stockrow-id');
|
|
var button = $(e.currentTarget);
|
|
|
|
Grocy.Api.Post('stock/products/' + productId + '/open', { 'amount': 1, 'stock_entry_id': specificStockEntryId },
|
|
function(bookingResponse)
|
|
{
|
|
button.addClass("disabled");
|
|
Grocy.FrontendHelpers.EndUiBusy();
|
|
toastr.success(__t('Marked %1$s of %2$s as opened', 1 + " " + productQuName, productName) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="Grocy.UndoStockBookingEntry(' + bookingResponse.id + ',' + stockRowId + ')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>');
|
|
RefreshStockEntryRow(stockRowId);
|
|
},
|
|
function(xhr)
|
|
{
|
|
Grocy.FrontendHelpers.EndUiBusy();
|
|
console.error(xhr);
|
|
}
|
|
);
|
|
});
|
|
|
|
$(document).on("click", ".stock-name-cell", function(e)
|
|
{
|
|
Grocy.Components.ProductCard.Refresh($(e.currentTarget).attr("data-stock-id"));
|
|
$("#stockentry-productcard-modal").modal("show");
|
|
});
|
|
|
|
$(document).on('click', '.stockentry-grocycode-stockentry-label-print', function(e)
|
|
{
|
|
e.preventDefault();
|
|
document.activeElement.blur();
|
|
|
|
var stockId = $(e.currentTarget).attr('data-stock-id');
|
|
Grocy.Api.Get('stock/entry/' + stockId + '/printlabel', function(labelData)
|
|
{
|
|
if (Grocy.Webhooks.labelprinter !== undefined)
|
|
{
|
|
Grocy.FrontendHelpers.RunWebhook(Grocy.Webhooks.labelprinter, labelData);
|
|
}
|
|
});
|
|
});
|
|
|
|
function RefreshStockEntryRow(stockRowId)
|
|
{
|
|
Grocy.Api.Get("stock/entry/" + stockRowId,
|
|
function(result)
|
|
{
|
|
var stockRow = $('#stock-' + stockRowId + '-row');
|
|
|
|
// If the stock row not exists / is invisible (happens after consume/undo because the undone new stock row has different id), just reload the page for now
|
|
if (!stockRow.length || stockRow.hasClass("d-none"))
|
|
{
|
|
window.location.reload();
|
|
}
|
|
|
|
if (result == null || result.amount == 0)
|
|
{
|
|
animateCSS("#stock-" + stockRowId + "-row", "fadeOut", function()
|
|
{
|
|
$("#stock-" + stockRowId + "-row").addClass("d-none");
|
|
});
|
|
}
|
|
else
|
|
{
|
|
var dueThreshold = moment().add(Grocy.UserSettings.stock_due_soon_days, "days");
|
|
var now = moment();
|
|
var bestBeforeDate = moment(result.best_before_date);
|
|
|
|
stockRow.removeClass("table-warning");
|
|
stockRow.removeClass("table-danger");
|
|
stockRow.removeClass("table-info");
|
|
stockRow.removeClass("d-none");
|
|
stockRow.removeAttr("style");
|
|
if (now.isAfter(bestBeforeDate))
|
|
{
|
|
if (stockRow.attr("data-due-type") == 1)
|
|
{
|
|
stockRow.addClass("table-secondary");
|
|
}
|
|
else
|
|
{
|
|
stockRow.addClass("table-danger");
|
|
}
|
|
}
|
|
else if (bestBeforeDate.isBefore(dueThreshold))
|
|
{
|
|
stockRow.addClass("table-warning");
|
|
}
|
|
|
|
animateCSS("#stock-" + stockRowId + "-row td:not(:first)", "shake");
|
|
|
|
$('#stock-' + stockRowId + '-amount').text(result.amount);
|
|
$('#stock-' + stockRowId + '-due-date').text(result.best_before_date);
|
|
$('#stock-' + stockRowId + '-due-date-timeago').attr('datetime', result.best_before_date + ' 23:59:59');
|
|
|
|
$(".stock-consume-button").attr('data-location-id', result.location_id);
|
|
|
|
var locationName = "";
|
|
Grocy.Api.Get("objects/locations/" + result.location_id,
|
|
function(locationResult)
|
|
{
|
|
locationName = locationResult.name;
|
|
|
|
$('#stock-' + stockRowId + '-location').attr('data-location-id', result.location_id);
|
|
$('#stock-' + stockRowId + '-location').text(locationName);
|
|
},
|
|
function(xhr)
|
|
{
|
|
console.error(xhr);
|
|
}
|
|
);
|
|
|
|
$('#stock-' + stockRowId + '-price').text(result.price);
|
|
$('#stock-' + stockRowId + '-purchased-date').text(result.purchased_date);
|
|
$('#stock-' + stockRowId + '-purchased-date-timeago').attr('datetime', result.purchased_date + ' 23:59:59');
|
|
|
|
var shoppingLocationName = "";
|
|
Grocy.Api.Get("objects/shopping_locations/" + result.shopping_location_id,
|
|
function(shoppingLocationResult)
|
|
{
|
|
shoppingLocationName = shoppingLocationResult.name;
|
|
|
|
$('#stock-' + stockRowId + '-shopping-location').attr('data-shopping-location-id', result.location_id);
|
|
$('#stock-' + stockRowId + '-shopping-location').text(shoppingLocationName);
|
|
},
|
|
function(xhr)
|
|
{
|
|
console.error(xhr);
|
|
}
|
|
);
|
|
|
|
if (result.open == 1)
|
|
{
|
|
$('#stock-' + stockRowId + '-opened-amount').text(__t('Opened'));
|
|
}
|
|
else
|
|
{
|
|
$('#stock-' + stockRowId + '-opened-amount').text("");
|
|
$(".product-open-button[data-stockrow-id='" + stockRowId + "']").removeClass("disabled");
|
|
}
|
|
}
|
|
|
|
// Needs to be delayed because of the animation above the date-text would be wrong if fired immediately...
|
|
setTimeout(function()
|
|
{
|
|
RefreshContextualTimeago("#stock-" + stockRowId + "-row");
|
|
RefreshLocaleNumberDisplay("#stock-" + stockRowId + "-row");
|
|
}, 600);
|
|
},
|
|
function(xhr)
|
|
{
|
|
Grocy.FrontendHelpers.EndUiBusy();
|
|
console.error(xhr);
|
|
}
|
|
);
|
|
}
|
|
|
|
$(window).on("message", function(e)
|
|
{
|
|
var data = e.originalEvent.data;
|
|
|
|
if (data.Message === "StockEntryChanged")
|
|
{
|
|
RefreshStockEntryRow(data.Payload);
|
|
}
|
|
});
|
|
|
|
Grocy.Components.ProductPicker.GetPicker().trigger('change');
|
|
|
|
$(document).on("click", ".product-name-cell", function(e)
|
|
{
|
|
Grocy.Components.ProductCard.Refresh($(e.currentTarget).attr("data-product-id"));
|
|
$("#productcard-modal").modal("show");
|
|
});
|