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 26.30 by bougault on 2022/11/16 15:24


  window.addEventListener('DOMContentLoaded', function() {
    const filters = {
      search: '',
      offset: 0,
      orderField: 'title',
      order: 'asc',
      favorite: false,
      roles: [] // to be joined as administrator+editor+viewer
    };

    function addRole(role) {
      if(filters.roles.indexOf(role) == -1) {
        filters.roles.push(role);
      }
    }

    function removeRole(role) {
      filters.roles.filter(r => r !== 'administrator')
    }

    document.querySelector('checkbox[data-role-filter]').forEach(function(filter) {
      filter.addEventListener('click', {
        const role = this.getAttribute('data-role-filter');
        if(this.checked) {
          addRole(role);
        } else {
          removeRole(role);
        }
        console.log(filters);
      })
    });

    // https://wiki-dev.ebrains.eu/rest/v1/collabs?search=&offset=0&orderField=title&order=asc&favorite=false&roles=administrator
    const contextSearchType = document.getElementById("context-search-type");
    const contextSearchText = document.getElementById("context-search-text");
    const contextSearchResponse = document.getElementById("context-search-response");
    const contextSearchButton = document.getElementById("context-search-button");
    contextSearchButton.addEventListener('click', function() {
      const context = contextSearchType.options[contextSearchType.selectedIndex].value;
      const term = contextSearchText.value;
      switch(context) {
          case 'collabs':
            document.location.href = `/bin/view/Collabs/#search=${term}`;
            break;
          case 'current-collab':
            handleXWikiSearch('bougaultx');
            break
          default:
            handleXWikiSearch('', applyAdministratorFilter)
      }

    });
    function handleXWikiSearch(space, callback) {
      require(['jquery'], function($) {
        var solrServiceURL = new XWiki.Document('SuggestSolrService', 'XWiki').getURL('get');
        const contextSearchButton = document.getElementById("context-search-button");
        const xwikiSpace = space ? `.${space}` : '';
        contextSearchButton.addEventListener('click', function() {
          $.post(solrServiceURL, {
              outputSyntax: 'plain',
              media: 'json',
              query: [
                'q="__INPUT__"~100',
                'fq=type:DOCUMENT',
                `fq=space:Collabs${xwikiSpace}.*`
              ].join('\n'),
              input: $('#context-search-text').val()
            }).then(res => {
            if(callback) {
              callback(res).then(results => {
                contextSearchResponse.innerText = JSON.stringify(results, null, 2);
              })
            } else {
              contextSearchResponse.innerText = JSON.stringify(res, null, 2);
             }
          });
        });
    });
  }
  function applyAdministratorFilter(results) {
      return new Promise((resolve, reject) => {
        //fetch("/rest/v1/collabs?roles=administrator")
        fetch("/rest/v1/collabs?search=&offset=0&orderField=title&order=asc&favorite=false&roles=administrator")
          .then(res => res.json())
          .then(collabs => {
            const spaces = collabs.map(collab => `Collabs.${collab.name}`);
            const filtered = results.filter(result => spaces.some(space => result.space.startsWith(space)));
            resolve(filtered);
          })

      })
  }
});