/** This is a collection of javascript functions and whatnot under the spree namespace that do stuff we find helpful. Hopefully, this will evolve into a propper class. **/ /* global AUTH_TOKEN, order_number, Sortable, flatpickr, DOMPurify */ //= require spree/backend/flatpickr_locals jQuery(function ($) { // Add some tips $('.with-tip').each(function() { $(this).tooltip({ container: $(this) }) }) $('.with-tip').on('show.bs.tooltip', function(event) { if (('ontouchstart' in window)) { event.preventDefault() } }) $('.js-show-index-filters').click(function () { $('.filter-well').slideToggle() $(this).parents('.filter-wrap').toggleClass('collapsed') }) // Responsive Menus var body = $('body') var modalBackdrop = $('#multi-backdrop') // Fail safe on resize var resizeTimer; window.addEventListener('resize', function () { document.body.classList.remove('modal-open', 'sidebar-open', 'contextualSideMenu-open'); document.body.classList.add('resize-animation-stopper'); clearTimeout(resizeTimer); resizeTimer = setTimeout(function () { document.body.classList.remove('resize-animation-stopper'); }, 400); }); function closeAllMenus() { body.removeClass() body.addClass('admin') modalBackdrop.removeClass('show') } modalBackdrop.click(closeAllMenus) // Main Menu Functionality var sidebarOpen = $('#sidebar-open') var sidebarClose = $('#sidebar-close') var activeItem = $('#main-sidebar').find('.selected') activeItem.closest('.nav-sidebar').addClass('active-option') activeItem.closest('.nav-pills').addClass('in show') function openMenu() { closeAllMenus() body.addClass('sidebar-open modal-open') modalBackdrop.addClass('show') } sidebarOpen.click(openMenu) sidebarClose.click(closeAllMenus) // Contextual Sidebar Menu var contextualSidebarMenuToggle = $('#contextual-menu-toggle') var contextualSidebarMenuClose = $('#contextual-menu-close') function toggleContextualMenu() { if (document.body.classList.contains('contextualSideMenu-open')) { closeAllMenus() } else { closeAllMenus() body.addClass('contextualSideMenu-open modal-open') modalBackdrop.addClass('show') } } contextualSidebarMenuToggle.click(toggleContextualMenu) contextualSidebarMenuClose.click(toggleContextualMenu) // TODO: remove this js temp behaviour and fix this decent // Temp quick search // When there was a search term, copy it $('.js-quick-search').val($('.js-quick-search-target').val()) // Catch the quick search form submit and submit the real form $('#quick-search').submit(function () { $('.js-quick-search-target').val($('.js-quick-search').val()) $('#table-filter form').submit() return false }) // Clickable ransack filters $('.js-add-filter').click(function () { var ransackField = $(this).data('ransack-field') var ransackValue = $(this).data('ransack-value') $('#' + ransackField).val(ransackValue) $('#table-filter form').submit() }) $(document).on('click', '.js-delete-filter', function () { var ransackField = $(this).parents('.js-filter').data('ransack-field') $('#' + ransackField).val('') $('#table-filter form').submit() }) function ransackField (value) { switch (value) { case 'Date Range': return 'Start' case '': return 'Stop' default: return value.trim() } } $('.js-filterable').each(function () { var $this = $(this) if ($this.val() !== null && $this.val() !== '' && $this.val().length !== 0) { var ransackValue, filter var ransackFieldId = $this.attr('id') var label = $('label[for="' + ransackFieldId + '"]') if ($this.is('select')) { ransackValue = $this.find('option:selected').toArray().map(function (option) { return option.text; }).join(', ') } else { ransackValue = $this.val() } label = ransackField(label.text()) + ': ' + ransackValue var cleanLabel = DOMPurify.sanitize(label) filter = '' + cleanLabel + '' $(".js-filters").append(filter).show() } }) // per page dropdown // preserves all selected filters / queries supplied by user // changes only per_page value $('.js-per-page-select').change(function () { var form = $(this).closest('.js-per-page-form') var url = form.attr('action') var value = $(this).val().toString() if (url.match(/\?/)) { url += '&per_page=' + value } else { url += '?per_page=' + value } window.location = url }) // injects per_page settings to all available search forms // so when user changes some filters / queries per_page is preserved $(document).ready(function () { var perPageDropdown = $('.js-per-page-select:first') if (perPageDropdown.length) { var perPageValue = perPageDropdown.val().toString() var perPageInput = '' $('#table-filter form').append(perPageInput) } }) }) function handleAlert (element) { element.classList.add('animate__animated', 'animate__bounceInUp') element.addEventListener('animationend', function () { element.classList.remove('animate__bounceInUp') element.classList.add('animate__fadeOutDownBig', 'animate__delay-3s') }) } // Triggers alert if required on document ready. $(document).ready(function () { var element = document.querySelector('.flash-alert') if (element) { handleAlert(element) } }) $.fn.visible = function (cond) { this[ cond ? 'show' : 'hide' ]() } // Triggers alerts when requested by javascript. // eslint-disable-next-line camelcase function show_flash (type, message) { var cleanMessage = DOMPurify.sanitize(message) var existingAlert = document.querySelector('.flash-alert') if (existingAlert) { existingAlert.remove() } var flashDiv = $('.alert-' + type) if (flashDiv.length === 0) { flashDiv = $('