Commit 15533569 authored by Jerome Chauveau's avatar Jerome Chauveau
Browse files

prise en charge de recherche/formulaire sur même tag avec attr et value différentes

parent 1c01038b
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -9,6 +9,8 @@
.ead-search-ul {
    list-style-type: none;
    display: inline-block;
    margin:0;
    padding:0
}

.ead-search-ul li {
@@ -89,6 +91,14 @@
    padding-left:0;
}

.ead-search-popup ul li{
    cursor: pointer;
}

.ead-search-popup ul li:hover{
    color: #888;
}

.ead-search-popup-alphabet{
    text-align :center;
}
+37 −32
Original line number Diff line number Diff line
@@ -31,13 +31,15 @@ class EadSearchPlugin extends Plugin{

    indexList.forEach((e) => {
        e.addEventListener('input', (evt) => {

            let indexId = e.dataset.indexid
            let indexName = e.dataset.index;
            let q = e.value;
            if(q.trim() !==''){
                let url = baseURI + projectId + '/search-index/'+indexName+'.json?q='+ q ;
                let datalist = document.getElementById(indexName);
                fetch(baseURI + projectId + '/search-index/'+indexName+'.json').then((res) => res.json()).then( (json) => {
                url += e.dataset.attr ? ('&attr=' +e.dataset.attr + '&value='+e.dataset.value) : '';

                let datalist = document.getElementById(indexId);
                fetch(url).then((res) => res.json()).then( (json) => {
                    datalist.innerHTML = '';
                    let entries = json.entries
                    entries.forEach((entry) => {
@@ -45,9 +47,9 @@ class EadSearchPlugin extends Plugin{
                        option.value = decodeURIComponent(entry.normal);
                        datalist.appendChild(option);
                    })
                    document.getElementById(indexName+'-input').addEventListener('input', (event) => {
                    document.getElementById(indexId+'-input').addEventListener('input', (event) => {
                            if(event.inputType === 'insertReplacementText'){
                                this.addIndexFilter(indexName, event.target.value)
                                this.addIndexFilter(indexName, event.target.value, indexId)
                            }
                    });
                })
@@ -57,36 +59,41 @@ class EadSearchPlugin extends Plugin{

  }

  togglePopup(tagName){
    let popupElt = document.getElementById('ead-search-popup-'+tagName);
  togglePopup(tagId){
    let popupElt = document.getElementById('ead-search-popup-'+tagId);
    if(popupElt.style.display=='block'){
        popupElt.style.display = 'none';
    }
    else
        this.popupIndex(tagName);
        this.popupIndex(tagId);
  }

  popupIndex(tagName){
    if(!document.getElementById('ul-'+tagName)){//needs first fetch
        this.fetchPage(tagName, 1);
  popupIndex(tagId){
    document.querySelectorAll('.ead-search-popup').forEach((p) => p.style.display='none')
    if(!document.getElementById('ul-'+tagId)){//needs first fetch
        this.fetchPage(tagId, 1);
    }
    let popupElt = document.getElementById('ead-search-popup-'+tagName);
    let popupElt = document.getElementById('ead-search-popup-'+tagId);
    popupElt.style.display='block';
  }

  fetchPage(tagName, pageNumber){
        let popupElt = document.getElementById('ead-search-popup-'+tagName);
        let popupContent = document.getElementById('ead-search-popup-'+tagName+"-contents");
  fetchPage(tagId, pageNumber){
        let popupElt = document.getElementById('ead-search-popup-'+tagId);
        let popupContent = document.getElementById('ead-search-popup-'+tagId+"-contents");
        //popupContent.innerHTML = '';
        //let url = baseURI + projectId + '/search-index/'+tagName+'.json?p='+pageNumber;
        let listTarget = document.getElementById('ul-'+tagName);
        let listTarget = document.getElementById('ul-'+tagId);
        if(!listTarget){
            listTarget = document.createElement('ul');
            listTarget.setAttribute('id', 'ul-'+tagName);
            listTarget.setAttribute('id', 'ul-'+tagId);
            popupContent.appendChild(listTarget);
        }

        fetch(baseURI + projectId + '/search-index/'+tagName+'.json?page='+pageNumber).then((res) => res.json()).then( (json) => {
        let inputTarget = document.getElementById(tagId+'-input');
        let tagName = inputTarget.dataset.index;
        let url = baseURI + projectId + '/search-index/'+tagName+'.json?page='+pageNumber;
        url += inputTarget.dataset.attr ? ('&attr=' +inputTarget.dataset.attr + '&value='+inputTarget.dataset.value) : '';
        fetch(url)
            .then((res) => res.json()).then( (json) => {
            let entries = json.entries
            let total = json.total;
            if(pageNumber === 1) {
@@ -103,11 +110,14 @@ class EadSearchPlugin extends Plugin{
                    let entryText = document.createTextNode(decodeURIComponent(entry.normal));
                    li.appendChild(spanNumber);
                    li.appendChild(entryText);
                    li.addEventListener('click', (evt) => {
                       this.addIndexFilter(tagName, decodeURIComponent(entry.normal), tagId);
                    })
                    listTarget.appendChild(li);
            });

            if(!document.getElementById('ead-search-index-pager-'+tagName))
                popupContent.appendChild(this.buildPager(tagName, total));
            if(!document.getElementById('ead-search-index-pager-'+tagId))
                popupContent.appendChild(this.buildPager(tagId, total));
        })
  }
//
@@ -139,7 +149,7 @@ class EadSearchPlugin extends Plugin{
//    })
//  }

  addIndexFilter(filterType, filterValue){
  addIndexFilter(filterType, filterValue, filterId){
    if(filterValue.trim() === '')
    return;

@@ -151,8 +161,8 @@ class EadSearchPlugin extends Plugin{
    rmBtn.innerHTML = "×"
    li.innerHTML = filterValue;
    li.appendChild(rmBtn);
    document.getElementById(filterType+'-ul').appendChild(li);
    document.getElementById(filterType+'-input').value = '';
    document.getElementById(filterId+'-ul').appendChild(li);
    document.getElementById(filterId+'-input').value = '';
    rmBtn.addEventListener('click', (evt) => {
        li.remove();
        this.filters[filterType].splice(this.filters[filterType].indexOf(filterValue), 1);
@@ -162,7 +172,7 @@ class EadSearchPlugin extends Plugin{
    })
  }

   buildPager(tagName, total, url){
   buildPager(tagId, total, url){
        let nbPage = Math.floor(total / PAGE_LENGTH);
        console.log('nbPager = ' + nbPage)
        let pagerElt = document.createElement('div');
@@ -199,16 +209,11 @@ class EadSearchPlugin extends Plugin{

        inputPageElt.addEventListener('change', (evt) => {
            let pageTarget = inputPageElt.value;
            this.fetchPage(tagName, pageTarget);
            this.fetchPage(tagId, pageTarget);
        })

//        for(let i = 1; i <= nbPage; i++){
//            let pageElt = document.createElement('span');
//            pageElt.innerHTML = i;
//            pagerElt.appendChild(pageElt);
//        }
        pagerElt.classList.add('ead-search-index-pager');
        pagerElt.setAttribute('id','ead-search-index-pager-'+tagName)
        pagerElt.setAttribute('id','ead-search-index-pager-'+tagId)
        return pagerElt;
   }

+32 −16
Original line number Diff line number Diff line
@@ -20,19 +20,31 @@ function max.plugin.ead_search:searchPage($project){
    let $dbPath := max.config:getProjectDBPath($project)
    let $datalists := <section id="ead-search-section" class="ead-search">
        {
        for $indexTag in max.plugin.ead_search:getSearchConfiguration($project)//tag
        for $indexTag at $tagNumber in  max.plugin.ead_search:getSearchConfiguration($project)//tag
        let $tagName := string($indexTag/@name)
        let $attr := string($indexTag/@attribute)
        let $value := string($indexTag/@value)
(:        let $queryParameters := 'if($attr) then "&amp;attr="||$attr||"&amp;value="||$value else '':)
        let $tagId := $tagName ||$tagNumber
        return
            if(count(collection($dbPath)//*[fn:local-name()=$tagName and @normal]) > 0)
            then
            <div>
                <label>{string($indexTag/@label)}</label>
                <input type="text" class="ead-search-input" data-index="{$tagName}" id="{$tagName}-input" list="{$tagName}" multiple="multiple"></input>
                <input type="text"
                       class="ead-search-input"
                       data-indexid="{$tagId}"
                       data-index="{$tagName}"
                       data-attr="{$attr}"
                       data-value="{$value}"
                       id="{$tagId}-input"
                       list="{$tagId}"
                       multiple="multiple"></input>
                <div class="ead-search-popup-wrap">
                    <button onclick="window.eadSearch.togglePopup('{$tagName}')">&#x1F4D6;</button>
                    <div class="ead-search-popup" id="ead-search-popup-{$tagName}">
                            <button class="ead-search-close-popup" onclick="window.eadSearch.togglePopup('{$tagName}')">&#215;</button>
                            <div id="ead-search-popup-{$tagName}-alphabet" class="ead-search-popup-alphabet">
                    <button onclick="window.eadSearch.togglePopup('{$tagId}')">&#x1F4D6;</button>
                    <div class="ead-search-popup" id="ead-search-popup-{$tagId}">
                            <button class="ead-search-close-popup" onclick="window.eadSearch.togglePopup('{$tagId}')">&#215;</button>
                            <div id="ead-search-popup-{$tagId}-alphabet" class="ead-search-popup-alphabet">
                                {for $l in 1 to 26 return
                                    let $letter:= codepoints-to-string($l+64)
                                    return <a>{codepoints-to-string($l+64)}</a>
@@ -41,12 +53,12 @@ function max.plugin.ead_search:searchPage($project){
(:                                        else():)
                                }
                            </div>
                            <div id="ead-search-popup-{$tagName}-contents"></div>
                            <div id="ead-search-popup-{$tagId}-contents"></div>
                    </div>
                </div>
                <ul id="{$tagName}-ul" class="ead-search-ul">
                <ul id="{$tagId}-ul" class="ead-search-ul">
                </ul>
                <datalist id="{$tagName}" class="ead-index-list"></datalist>
                <datalist id="{$tagId}" class="ead-index-list"></datalist>
            </div>
            else ()
        }
@@ -156,7 +168,7 @@ declare %private function max.plugin.ead_search:dateFilter(

};

declare %private function max.plugin.ead_search:dateIntervalFilter($from as xs:string, $to as xs:string, $dateFormat as xs:string, $matches as item()+){
declare %private function max.plugin.ead_search:dateIntervalFilter($from as xs:string, $to as xs:string, $dateFormat as xs:string, $matches as item()*){

    let $fromDateTime := max.plugin.ead_search:parseDateParameter($from,$dateFormat)
    let $toDateTime := max.plugin.ead_search:parseDateParameter($to,$dateFormat)
@@ -190,11 +202,13 @@ declare function max.plugin.ead_search:buildSimpleDateQuery($project as xs:strin
declare
%rest:GET
%output:method("json")
%rest:query-param("attr", "{$attr}")
%rest:query-param("value", "{$value}")
%rest:query-param("q", "{$query}")
%rest:query-param("page", "{$page}",1)
%rest:path("/{$project}/search-index/{$tag}.json")
function max.plugin.ead_search:searchIndexesListAsJSON($project, $tag, $page as xs:integer, $query as xs:string?){
    let $entries := max.plugin.ead_search:searchIndexesList($project, $tag, $page, $query)
function max.plugin.ead_search:searchIndexesListAsJSON($project, $tag, $page as xs:integer,$attr, $value, $query as xs:string?){
    let $entries := max.plugin.ead_search:searchIndexesList($project, $tag, $page, $attr, $value, $query)
    let $jsonStr :=
            for $entry in $entries/*
            order by $entry/@normal
@@ -213,20 +227,22 @@ function max.plugin.ead_search:searchIndexesListAsJSON($project, $tag, $page as

declare
%rest:GET
%rest:query-param("attr", "{$attr}")
%rest:query-param("value", "{$value}")
%rest:query-param("q", "{$query}")
%rest:query-param("page", "{$page}",1)
%rest:path("/{$project}/search-index/{$tag}.xml")
function max.plugin.ead_search:searchIndexesList($project, $tag, $page as xs:integer, $query as xs:string?){
function max.plugin.ead_search:searchIndexesList($project, $tag, $page as xs:integer, $attr, $value, $query as xs:string?){
    let $pageLength := 10
    let $dbPath := max.config:getProjectDBPath($project)
    let $tagParameters := max.plugin.ead_search:getSearchConfiguration($project)//tag[@name=$tag]
(:    let $tagParameters := max.plugin.ead_search:getSearchConfiguration($project)//tag[@name=$tag]:)
    (:select all nodes with required tag and attributes :)
    let $allEntries :=
        for $elt in collection($dbPath)//*[fn:local-name()=$tag and @normal]
        order by $elt/@normal
        where(
            if($tagParameters/@attribute) then
                $elt/@*[local-name(.)=string($tagParameters/@attribute) and string(.)=string($tagParameters/@value)]
            if($attr) then
                $elt/@*[local-name(.)=$attr and string(.)=$value]
            else true()
        )
        and (