No edit summary
Tag: 2017 source edit
No edit summary
Tag: 2017 source edit
Line 137: Line 137:
<html>
<html>
<script>
<script>
   (function() {
   (function () {
     const input = document.getElementById('customsearch-input');
     var input = document.getElementById('customsearch-input');
     const suggestionsBox = document.getElementById('suggestion-container');
     var suggestionsBox = document.getElementById('suggestion-container');
     const suggestionList = document.getElementById('suggestion-list');
     var suggestionList = document.getElementById('suggestion-list');


     let lastFetchController = null;
     var lastFetchController = null;


     // Hide suggestions
     // Hide suggestions
Line 163: Line 163:
       }
       }


       suggestions.forEach(sugg => {
       for (var i = 0; i < suggestions.length; i++) {
         const li = document.createElement('li');
        var sugg = suggestions[i];
         var li = document.createElement('li');
         li.textContent = sugg;
         li.textContent = sugg;
         li.style.padding = '8px 12px';
         li.style.padding = '8px 12px';
         li.style.cursor = 'pointer';
         li.style.cursor = 'pointer';


         li.addEventListener('click', () => {
         li.addEventListener('click', function () {
           input.value = sugg;
           input.value = this.textContent;
           hideSuggestions();
           hideSuggestions();
         });
         });


         suggestionList.appendChild(li);
         suggestionList.appendChild(li);
       });
       }


       showSuggestions();
       showSuggestions();
     }
     }


     // Fetch suggestions on user input
     // Fetch suggestions using XMLHttpRequest
     input.addEventListener('input', async function() {
    function fetchSuggestions(query) {
       const query = this.value.trim();
      var xhr = new XMLHttpRequest();
      var url = 'https://search-api.mdriven.net/wiki_suggestions?q=' + encodeURIComponent(query);
 
      xhr.open('GET', url, true);
 
      xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
          try {
            var data = JSON.parse(xhr.responseText);
            updateSuggestions(data);
          } catch (err) {
            console.error('Error parsing suggestions:', err);
          }
        }
      };
 
      xhr.onerror = function () {
        console.error('Error fetching suggestions');
      };
 
      xhr.send();
    }
 
    // Listen for user input
     input.addEventListener('input', function () {
       var query = this.value.trim();
       if (!query) {
       if (!query) {
         hideSuggestions();
         hideSuggestions();
Line 192: Line 218:
         lastFetchController.abort();
         lastFetchController.abort();
       }
       }
      lastFetchController = new AbortController();
      const signal = lastFetchController.signal;
      try {
        const response = await fetch(
          `https://search-api.mdriven.net/wiki_suggestions?q=${encodeURIComponent(query)}`,
          { signal }
        );


        if (!response.ok) {
      // Create a new AbortController (Polyfill for ES5 compatibility)
          console.error('Suggestion request failed:', response.statusText);
      lastFetchController = new AbortControllerPolyfill();
          return;
        }


        const data = await response.json();
      // Fetch suggestions using XMLHttpRequest
        updateSuggestions(data);
       fetchSuggestions(query);
       } catch (err) {
        if (err.name === 'AbortError') {
          return;
        }
        console.error('Error fetching suggestions:', err);
      }
     });
     });


     // Hide suggestions on clicking outside
     // Hide suggestions on clicking outside
     document.addEventListener('click', (e) => {
     document.addEventListener('click', function (e) {
       if (!suggestionsBox.contains(e.target) && e.target !== input) {
       if (!suggestionsBox.contains(e.target) && e.target !== input) {
         hideSuggestions();
         hideSuggestions();
       }
       }
     });
     });
    // Polyfill for AbortController for older browsers
    function AbortControllerPolyfill() {
      this.signal = {};
      this.abort = function () {
        // Aborting is not fully supported in ES5, but this is a placeholder to mimic behavior
      };
    }
   })();
   })();
</script>
</script>

Revision as of 12:56, 31 March 2025

This page was created by MediaWiki default on 2016-11-24. Last edited by Edgar on 2025-03-31.

MDriven Learn
Learn. Model. Build.
Visualize and design your ideas. Discover more with MDriven documentation, hands-on training, and ready-made models to get the most from MDriven's tools and services.
Image
Icon
Documentation
Find technical documentation
Gain understanding through in-depth explanations of the concepts behind the MDriven tools.
Icon
Training
Learn by doing
Practical training and tutorials on MDriven designed to educate and inspire you to model your ideas.
Icon
Q&A
Ask questions and get help
Write to us your MDriven-related questions and receive quality answers and feedback.
Icon
Model Examples
Try model samples
Use our unique, pre-built models to build your ideas and achieve your goals.
Icon
Best Practices
Explore effective solutions
Our suggestions on how you can maximize the benefits of the MDriven tools to realize business solutions.