import { ResizeResponsiveEmbeds } from "../helpers/embeds"; import { IsTouchInputDevice } from "../helpers/input"; import { BoolVal, GetFileNameFromPath } from "../helpers/extensions"; // This function sets some global state and adds some global event listeners. function setInitialGlobalState(Grocy) { // __t isn't set in global context yet, make one locally. var __t = function(key, ...placeholder) { return Grocy.translate(key, ...placeholder) }; if (!Grocy.ActiveNav.isEmpty()) { var menuItem = $('#sidebarResponsive').find("[data-nav-for-page='" + Grocy.ActiveNav + "']"); menuItem.addClass('active-page'); if (menuItem.length) { var parentMenuSelector = menuItem.data("sub-menu-of"); if (typeof parentMenuSelector !== "undefined") { var pMenu = $(parentMenuSelector); pMenu.collapse("show"); pMenu.prev(".nav-link-collapse").addClass("active-page"); pMenu.on("shown.bs.collapse", function() { if (!menuItem.isVisibleInViewport(75)) { menuItem[0].scrollIntoView(); } }) } else { if (!menuItem.isVisibleInViewport(75)) { menuItem[0].scrollIntoView(); } } } } var observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { if (mutation.attributeName === "class") { var attributeValue = $(mutation.target).prop(mutation.attributeName); if (attributeValue.contains("sidenav-toggled")) { window.localStorage.setItem("sidebar_state", "collapsed"); } else { window.localStorage.setItem("sidebar_state", "expanded"); } } }); }); observer.observe(document.body, { attributes: true }); if (window.localStorage.getItem("sidebar_state") === "collapsed") { $("#sidenavToggler").click(); } window.toastr.options = { toastClass: 'alert', closeButton: true, timeOut: 20000, extendedTimeOut: 5000 }; window.FontAwesomeConfig = { searchPseudoElements: true } $(window).on('resize', function() { ResizeResponsiveEmbeds($("body").hasClass("fullscreen-card")); }); $("iframe").on("load", function() { ResizeResponsiveEmbeds($("body").hasClass("fullscreen-card")); }); // Don't show tooltips on touch input devices if (IsTouchInputDevice()) { var css = document.createElement("style"); css.innerHTML = ".tooltip { display: none; }"; document.body.appendChild(css); } $(document).on("keyup paste change", "input, textarea", function() { $(this).closest("form").addClass("is-dirty"); }); $(document).on("click", "select", function() { $(this).closest("form").addClass("is-dirty"); }); // Auto saving user setting controls $(document).on("change", ".user-setting-control", function() { var element = $(this); var settingKey = element.attr("data-setting-key"); if (!element[0].checkValidity()) { return; } var inputType = "unknown"; if (typeof element.attr("type") !== typeof undefined && element.attr("type") !== false) { inputType = element.attr("type").toLowerCase(); } if (inputType === "checkbox") { value = element.is(":checked"); } else { var value = element.val(); } Grocy.FrontendHelpers.SaveUserSetting(settingKey, value); }); // Show file name Bootstrap custom file input $('input.custom-file-input').on('change', function() { $(this).next('.custom-file-label').html(GetFileNameFromPath($(this).val())); }); // Translation of "Browse"-button of Bootstrap custom file input if ($(".custom-file-label").length > 0) { $("