Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
online_help.js106 linesDownload Raw Back to QuickSearch
1/////////////////////////////////////////////////////////////2//3// pgAdmin 4 - PostgreSQL Tools4//5// Copyright (C) 2013 - 2024, The pgAdmin Development Team6// This software is released under the PostgreSQL Licence7//8//////////////////////////////////////////////////////////////9import React from 'react';10import ReactDOM from 'react-dom';11import HiddenIframe from './HiddenIframe';12import url_for from 'sources/url_for';13 14const extractSearchResult = (list) => {15  let result = {};16  for (let list_val of list) {17    let link = list_val.getElementsByTagName('A');18    // we are not going to display more than first 10 result as per design19    if (link.length == 0) {20      break;21    }22    let topicName  = link[0].text;23    let topicLink  = url_for('help.static', {24      'filename': link[0].getAttribute('href'),25    });26    result[topicName] = topicLink;27  }28  return result;29};30 31export function onlineHelpSearch(param, props) {32  param = param.split(' ').join('+');33  const setState = props.setState;34  const helpURL = url_for('help.static', {35    'filename': 'search.html',36  });37  const srcURL = `${helpURL}?q=${param}`;38  let isIFrameLoaded = false;39  if(document.getElementById('hidden-quick-search-iframe')){40    document.getElementById('hidden-quick-search-iframe').contentDocument.location.reload(true);41  }42 43  // Below function will be called when the page will be loaded in Iframe44  const _iframeLoaded = () => {45    if (isIFrameLoaded) {46      return false;47    }48    isIFrameLoaded = true;49    let iframe = document.getElementById('hidden-quick-search-iframe');50    let content = (iframe.contentWindow || iframe.contentDocument);51    let iframeHTML = content.document;52    window.pooling = setInterval(() => {53      let resultEl = iframeHTML.getElementById('search-results');54      let pooling = window.pooling;55      if(resultEl) {56        let searchResultsH2Tags = resultEl.getElementsByTagName('h2');57        let list = resultEl?.getElementsByTagName('LI');58        if ((list && list.length > 0 )) {59          let res = extractSearchResult(list);60          // After getting the data, we need to call the Parent component function61          // which will render the data on the screen62          if (searchResultsH2Tags[0]['childNodes'][0]['textContent'] != 'Searching') {63            window.clearInterval(pooling);64            setState(state => ({65              ...state,66              fetched: true,67              clearedPooling: true,68              url: srcURL,69              data: res,70            }));71            isIFrameLoaded = false;72            ReactDOM.unmountComponentAtNode(document.getElementById('quick-search-iframe-container'));73          } else {74            setState(state => ({75              ...state,76              fetched: true,77              clearedPooling: false,78              url: srcURL,79              data: res,80            }));81          }82        } else if(searchResultsH2Tags[0]['childNodes'][0]['textContent'] == 'Search Results') {83          setState(state => ({84            ...state,85            fetched: true,86            clearedPooling: true,87            url: srcURL,88            data: {},89          }));90          ReactDOM.unmountComponentAtNode(document.getElementById('quick-search-iframe-container'));91          isIFrameLoaded = false;92          window.clearInterval(pooling);93        }94      } else {95        window.clearInterval(pooling);96      }97    }, 500);98  };99 100  // Render IFrame101  ReactDOM.render(102    <HiddenIframe id='hidden-quick-search-iframe' srcURL={srcURL} onLoad={_iframeLoaded}/>,103    document.getElementById('quick-search-iframe-container'),104  );105}106 
codekingpro/portable-devtools · Team Ai