Failed to execute template. Cause: [Access denied when checking [script] access to [xwiki:Collaboratory.UX.HbpSkin.WebHome] for user [xwiki:XWiki.Admin]]. Click on this message for details.

Context Search - HBP Wiki

IAM21 instance, do not create collab nor modify a team, your changes will be lost


Context Search

Version 27.83 by bougault on 2022/11/17 15:10

import {LitElement, html, css} from 'https://cdn.jsdelivr.net/gh/lit/dist@2/core/lit-core.min.js';
window.addEventListener('DOMContentLoaded', function() {

  class UnifiedSearch extends LitElement {
    static properties = {
      results: { type: Array },
      resultsCache: { type: Array },
      collabFiltersResults: { type: Array}
    }
    static styles = css`
      :host > div {
        padding-bottom: 4em;
      }
      .result {
        margin-top: 1em;
      }
      .light {
        color: var(--color-gray-500, #CCC);
        font-size: 14px;
      }
      a {
        color: var(--color-brand-primary, blue);
      }
    `;
    constructor() {
      super();
      this.searchText = "";
      this.prevSearchText = "";
      this.administrator = false;
      this.editor = false;
      this.viewer = false;
      this.rolesFilter = [];
      this.filterUrl = new URL(`${document.location.origin}/rest/v1/collabs?search=&offset=0&orderField=title&order=asc&limit=10000&favorite=false&roles=`);
      this.results = [];
      this.resultsCache = [];
    }
    firstUpdated() {
      this.renderRoot.getElementById('searchInput').focus();
    }
    toggleRoleFilter(roleName) {
      if(this.rolesFilter.indexOf(roleName) === -1) {
        this.rolesFilter.push(roleName);
      } else {
        this.rolesFilter = this.rolesFilter.filter(role => role !== roleName);
      }
      this[roleName] = !this[roleName];
      this.filterUrl.searchParams.set('roles', this.rolesFilter.join("+"));
      this.handleSearch();
    }
    setSearchText(e) {
      this.searchText = e.target.value;
    }
    handleSearch(textSearch) {
      if(this.searchText !== "" || textSearch) {
        if(this.searchText !== this.prevSearchText) {
          this.clearResults();
          this.prevSearchText = this.searchText;
          this.fetchWikiSearch().then(results => {
            this.resultsCache = results;
            if(this.hasActiveFilters()) {
              this.fetchFilterSearch().then(this.filterResults);
            } else {
              this.results = results;
            }
          })
        } else if (this.searchText === this.prevSearchText && this.hasActiveFilters()) {
          this.fetchFilterSearch().then(this.filterResults)
        } else {
          this.results = this.resultsCache;
        }
      } else {
        this.clearResults();
        console.log("no search text, should return filter search results if any");
        this.fetchFilterSearch().then(collabs => {
          console.log("Format and display collabs", collabs);
        })
      }
    }
    hasActiveFilters() {
      // we only look at role filters for POC.
      return this.rolesFilter.length > 0;
    }
    fetchWikiSearch() {
      // request should be built from component, not from "external" dependency. Leave it here like this for POC.
      return handleXWikiSearch(this.searchText);
    }
    fetchFilterSearch() {
      return new Promise((resolve) => {
        if(this.hasActiveFilters()) {
          fetch(this.filterUrl).then(res => res.json()).then((res) => resolve(res));
        } else {
          resolve([]);
        }
      });
    }
    filterResults = (filteredCollabs) => {
      const collabSpaces = filteredCollabs.map(collab => `Collabs.${collab.name}`);
      this.results = this.resultsCache.filter(result => collabSpaces.some(space => result.space.startsWith(space)));
    }
    clearResults() {
      this.results = [];
      this.resultsCache = [];
    }
    getLink(xwikiDocSpace) {
      return `/bin/view/${xwikiDocSpace.replaceAll('.', '/')}`;
    }
    handleKeyPress(e) {
      if (e.keyCode === 13) {
        this.handleSearch(true);
      }
    }
    render() {
      return html`
${this.results.map(result => html`
${result.title_}
${result.doccontent_.substring(0, 150)}
${result.space.replaceAll('.', ' / ')}
`)}
` } } customElements.define('clb-unified-search', UnifiedSearch); function handleXWikiSearch(searchString) { return new Promise((resolve) => { require(['jquery'], function($) { const solrServiceURL = new XWiki.Document('SuggestSolrService', 'XWiki').getURL('get'); //contextSearchButton.addEventListener('click', function() { $.post(solrServiceURL, { outputSyntax: 'plain', media: 'json', query: [ 'q="__INPUT__"~100', 'fq=type:DOCUMENT', `fq=space:Collabs.*` ].join('\n'), input: searchString }).then(resolve); //}); }); }); } });