Save Search

Use this forum to discuss possible implementation of a new feature before opening a ticket.
A developer shall edit the topic title with "[xxx]" where xxx is the id of the accompanying tracker id.
Duplicate posts about the same id. +1 posts are not allowed.

Moderators: leecollings, remb0

Post Reply
Jemand
Posts: 15
Joined: Saturday 11 May 2019 12:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Save Search

Post by Jemand »

Hi,

I have over 100 devices and when I search for a device at the top of the page and click on "Log" or "Edit" and come back to the switches overview the search is empty and I have to search for the device again.

I've built a small javascript which I included in the page to fix that. The script saves the last search input and restores the search when I enter the page again. This works perfect but it would be great to have this within domoticz natively like in the devices-search.

Here is my Javascript for this:

Code: Select all

// Newer version below...
Last edited by Jemand on Sunday 01 September 2024 16:48, edited 1 time in total.
Kedi
Posts: 536
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: Save Search

Post by Kedi »

Nice. :D +1
Logic will get you from A to B. Imagination will take you everywhere.
User avatar
gizmocuz
Posts: 2350
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Save Search

Post by gizmocuz »

Very nice indeed!
Does this also work when you switch for instance from the temperature tab to the utility tab?

If you want, please make a github PR with your changes
Quality outlives Quantity!
Jemand
Posts: 15
Joined: Saturday 11 May 2019 12:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Save Search

Post by Jemand »

My Script was just for LightSwitches as I don't have too many other devices where I need to search.

But here is an updated version for every domoticz-page. You can include this script in your domoticz directory under www/styles/default/custom.js:

Code: Select all

// Function to extract the page type from the URL
function getCurrentPageType() {
    const hash = window.location.hash;
    const match = hash.match(/#\/([^/?]+)/);
    return match ? match[1].toLowerCase() : 'unknown';
}

// Function to save the search term
function saveSearch(searchTerm) {
    const pageType = getCurrentPageType();
    if (pageType !== 'unknown') {
        if (searchTerm) {
            localStorage.setItem(`savedSearch_${pageType}`, searchTerm);
        } else {
            localStorage.removeItem(`savedSearch_${pageType}`);
        }
    }
}

// Function to load the saved search term
function loadSearch() {
    const pageType = getCurrentPageType();
    return pageType !== 'unknown' ? (localStorage.getItem(`savedSearch_${pageType}`) || '') : '';
}

// Function to trigger the search
function triggerSearch(searchInput) {
    if (getCurrentPageType() === 'unknown') return;
    
    // Trigger the 'input' event
    const inputEvent = new Event('input', { bubbles: true, cancelable: true });
    searchInput.dispatchEvent(inputEvent);
    
    // Trigger the 'change' event
    const changeEvent = new Event('change', { bubbles: true, cancelable: true });
    searchInput.dispatchEvent(changeEvent);
    
    // Trigger the 'keyup' event
    const keyupEvent = new KeyboardEvent('keyup', { bubbles: true, cancelable: true, key: 'Enter', keyCode: 13 });
    searchInput.dispatchEvent(keyupEvent);
}

// Function to clear the search
function clearSearch(searchInput) {
    searchInput.value = '';
    saveSearch('');
    triggerSearch(searchInput);
}

// Function to initialize the script
function initSearchPersistence() {
    function setupSearchInput() {
        const searchInput = document.querySelector('.livesearch.jsLiveSearch');
        
        if (searchInput) {
            // Load the saved search term
            const savedValue = loadSearch();
            if (savedValue && searchInput.value !== savedValue) {
                searchInput.value = savedValue;
                
                // Delayed triggering of the search
                setTimeout(() => triggerSearch(searchInput), 500);
            }
            
            // Event listener for changes in the search field
            searchInput.addEventListener('input', function() {
                saveSearch(this.value);
            });
            
            // Find and set up the clear button
            const clearButton = searchInput.parentElement.querySelector('.fa-circle-xmark');
            if (clearButton) {
                clearButton.addEventListener('click', function() {
                    clearSearch(searchInput);
                });
            }
            
            // Remove the interval as the search field has been found
            clearInterval(searchCheckInterval);
        }
    }
    // Regularly check if the search field is available
    const searchCheckInterval = setInterval(setupSearchInput, 100);
}

// Function to handle URL changes
function handleUrlChange() {
    // Only initialize if a search input exists on the page
    if (document.querySelector('.livesearch.jsLiveSearch')) {
        initSearchPersistence();
    }
}

// Initialization when loading the page
handleUrlChange();

// Listener for hash changes
window.addEventListener('hashchange', handleUrlChange);
User avatar
gizmocuz
Posts: 2350
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Save Search

Post by gizmocuz »

Thank you for this! Could you make a git pull request with the changes?
Quality outlives Quantity!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest