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

Merge branch 'jc/ead-search' into dev

parents 4b006a26 7bb075dd
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
<?xml version="1.0"?>

<search>
    <tags>
        <tag name="corpname" label="corpname"/>
        <tag name="famname" label="famname"/>
        <tag name="function" label="Fonction"/>
        <tag name="genreform" label="genreform"/>
        <tag name="geogname" label="geogname"/>
        <tag name="occupation" label="Occupation"/>
        <tag name="name" label="Nom"/>
        <tag name="persname" label="Nom de personne"/>
        <tag name="subject" label="Sujet"/>
        <tag name="title" label="Titre"/>
    </tags>
    <dates format="YYYY"></dates>
</search>
 No newline at end of file
+128 −0
Original line number Diff line number Diff line
.ead-search{
    margin-top: 100px;
}

.ead-search button{
    cursor: pointer;
}

.ead-search-ul {
    list-style-type: none;
    display: inline-block;
    margin:0;
    padding:0
}

.ead-search-ul li {
    display: inline-block;
    margin-left:5px;
    border: 1px solid #ccc;
    border-radius: 2px;
    padding: 2px 5px;
    font-size:0.8rem;
}

.ead-search-ul li button {
    display :inline-block;
    border: none;
    background-color: none;
    padding: 5px;
    margin-left: 5px;
    color: red;
    cursor : pointer;
}

.ead-search label{
    display :inline-block;
    min-width: 200px;
}

#ead-search-date-wrap{
    border-top: 1px solid #ccc;
    margin-top:10px;
    padding-top:10px;
}
#ead-search-date-wrap div span{
    display :inline-block;
    min-width: 50px;
}

#ead-search-results {
    border-top: 1px solid #888;
}

.ead-search-popup-wrap{
    display: inline;
    position: relative;
}

.ead-search-popup{
    display: none;
    position: absolute;
    top:0;
    left: 10px;
    border:1px solid #999;
    min-width:500px;
    background-color: white;
    z-index:10;
    font-size:0.8rem;
}

.ead-search-close-popup {
    position: absolute;
    top :5px;
    right: 5px;
}
.ead-search-popup button {
    display:flex;
    justify-content: center;
    align-items: center;
    max-width: 32px;
    max-height: 32px;
    min-width: 32px;
    min-height: 32px;
}

.ead-search-popup ul {
    list-style-type: none;
    margin-left: 0;
    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;
}
.ead-search-index-pager{
    display:flex;
    justify-content: center;
    align-items: center;
}
.ead-search-index-pager button{
    margin: 5px;
}
.ead-search-index-pager input{
    max-width: 80px;
}

.ead-search-index-n{
    display: inline-block;
    margin-right: 15px;
    min-width: 25px;
    font-weight: bold;
    text-align:right;
}
.ead-search-alpha {
    cursor : pointer;
}
.ead-search-alpha.selected{
    color: black;
    font-weight: bold;
}
 No newline at end of file
+277 −0
Original line number Diff line number Diff line
import {Plugin} from '../../core/ui/js/Plugin.js';

const PAGE_LENGTH = 10;

class EadSearchPlugin extends Plugin{
  constructor(name) {
     super(name);
     this.currentFirstLetterFilter = null;
     this.withDatesFilter = document.getElementById('ead-search-date-cb').checked;
     this.filters = {}
     if(document.getElementById('ead-search-section')){
        document.getElementById('ead-search-date-cb').addEventListener('click', (evt) => {
            this.withDatesFilter = evt.target.checked;
            if(!this.withDatesFilter){
                document.getElementById('ead-search-date-from').setAttribute('disabled', true);
                document.getElementById('ead-search-date-to').setAttribute('disabled', true);
            }
            else{
                document.getElementById('ead-search-date-from').removeAttribute('disabled');
                document.getElementById('ead-search-date-to').removeAttribute('disabled')
            }

        })
        this.bindIndexAutocompletes();
     }
//        this.fetchIndexLists();

  }

  bindIndexAutocompletes(){
    let indexList = document.querySelectorAll('.ead-search-input');

    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 +'.*' ;
                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.querySelectorAll('*').forEach(n => n.remove());
                    let entries = json.entries;
                    let options =''
                    entries.forEach((entry) => {
                            options += '<option value="'+decodeURIComponent(entry.normal)+'"/>';
                    });
                    datalist.innerHTML=options;
                });

                document.getElementById(indexId+'-input').addEventListener('input', (event) => {
                        if(event.inputType === 'insertReplacementText'){
                            this.addIndexFilter(indexName, event.target.value, indexId)
                        }
                });

            }
        })
    })

  }

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

  popupIndex(tagId){
    this.currentFirstLetterFilter = null;
    document.getElementById('alpha-'+tagId).classList.toggle('selected')
    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-'+tagId);
    popupElt.style.display='block';
  }

  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-'+tagId);
        if(!listTarget){
            listTarget = document.createElement('ul');
            listTarget.setAttribute('id', 'ul-'+tagId);
            popupContent.appendChild(listTarget);
        }
        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) : '';
        if(this.currentFirstLetterFilter)
            url += "&q=^"+this.currentFirstLetterFilter
        fetch(url)
            .then((res) => res.json()).then( (json) => {
            let entries = json.entries
            let total = json.total;
            let totalElt = document.getElementById('ead-search-nb-entries-'+tagId)
            if(!totalElt) {
                totalElt = document.createElement('h5');
                totalElt.setAttribute('id','ead-search-nb-entries-'+tagId)
                popupContent.appendChild(totalElt);
            }
            totalElt.innerHTML = total + (entries > 1 ? ' entrée' : ' entrées');
            listTarget.innerHTML = '';
            entries.forEach((entry, n) => {
                    let li = document.createElement('li');
                    let spanNumber = document.createElement('span');
                    spanNumber.classList.add('ead-search-index-n');
                    spanNumber.innerHTML = (PAGE_LENGTH * (pageNumber-1) + n + 1);
                    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-'+tagId))
                popupContent.appendChild(this.buildPager(tagId, total));
        })
  }
//
//  fetchIndexLists(){
//    let indexList = document.querySelectorAll('.ead-search-input')
//
//    indexList.forEach((e) => {
//        let indexName = e.dataset.index;
//        fetch(baseURI + projectId + '/search-index/'+indexName+'.json').then((res) => res.json()).then( (json) => {
//                let datalist = document.getElementById(indexName);
//                let entries = json.entries
//                entries.forEach((entry) => {
//                    let option = document.createElement('option');
//                    option.value = decodeURIComponent(entry.normal);
//                    datalist.appendChild(option);
//
//                })
//
//                document.getElementById(indexName+'-input').addEventListener('input', (event) => {
//                        //alert(event.target.value)
//                        if(event.inputType === 'insertReplacementText'){
//                            this.addIndexFilter(indexName, event.target.value)
//                        }
//
//                });
//            })
//
//
//    })
//  }

  fetchIndexEntriesByLetter(letter, tagId)  {
    this.currentFirstLetterFilter = letter.trim() === '' ? null : letter;
    document.querySelectorAll('.ead-search-alpha.selected').forEach(e => {e.classList.toggle('selected')})
    document.getElementById("alpha-"+tagId+(this.currentFirstLetterFilter ? '-'+ this.currentFirstLetterFilter : '')).classList.toggle('selected')
    this.fetchPage(tagId, 1)
  }

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

    if(!this.filters[filterType])
        this.filters[filterType] = []
    this.filters[filterType].push(filterValue);
    let li = document.createElement('li');
    let rmBtn = document.createElement('button');
    rmBtn.innerHTML = "&times;"
    li.innerHTML = filterValue;
    li.appendChild(rmBtn);
    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);
        if(this.filters[filterType].length ===0 )
            delete this.filters[filterType];
        console.log(this.filters);
    })
  }

   buildPager(tagId, total, url){
        let nbPage = Math.floor(total / PAGE_LENGTH);
        console.log('nbPager = ' + nbPage)
        let pagerElt = document.createElement('div');

        let firstArrowElt = document.createElement('button');
        firstArrowElt.innerHTML = '<<';
        let prevArrowElt = document.createElement('button');
        prevArrowElt.innerHTML = '<';

        let lastArrowElt = document.createElement('button');
        lastArrowElt.innerHTML = '>>';
        let nextArrowElt = document.createElement('button');
        nextArrowElt.innerHTML = '>';

        let inputPageElt = document.createElement('input');
        inputPageElt.type='number';
        inputPageElt.value=1;

        pagerElt.appendChild(firstArrowElt);
        pagerElt.appendChild(prevArrowElt);
        pagerElt.appendChild(inputPageElt);
        pagerElt.appendChild(nextArrowElt);
        pagerElt.appendChild(lastArrowElt);

        let gotoPage = function(n){
             inputPageElt.value = n;
             inputPageElt.dispatchEvent(new Event('change'));
        }

        firstArrowElt.addEventListener('click', () => gotoPage(1));
        prevArrowElt.addEventListener('click', () => gotoPage(Number(inputPageElt.value)-1));
        lastArrowElt.addEventListener('click', () => gotoPage(nbPage));
        nextArrowElt.addEventListener('click', () => gotoPage(Number(inputPageElt.value)+1));

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

        pagerElt.classList.add('ead-search-index-pager');
        pagerElt.setAttribute('id','ead-search-index-pager-'+tagId)
        return pagerElt;
   }

  runEadSearch(){
        let query = 'resultats.html';
        let filtered = false;
        Object.keys(this.filters).forEach((k,i) => {
            query+= (i===0 ? '?' : '&') + 'indexes[]=' + k;
            console.log(this.filters[k])
            for(let j = 0 ; j < this.filters[k].length; j++) {
                query+='&'+k+"[]=" + encodeURIComponent(this.filters[k][j])
            }
            filtered = true;
        })

       query = baseURI + projectId + '/' +query;

        if(this.withDatesFilter){
            //date
            let dateMode = document.querySelector('input[name=ead-search-radio-date]:checked').value
            if(dateMode === 'interval'){
                let from = document.getElementById('ead-search-date-from').value;
                let to = document.getElementById('ead-search-date-to').value;
                if(from && to)
                    query+= (!filtered ? "?" : "&") +"from="+from+"&to="+to
            }
            else if (dateMode === 'in'){
                let dateIn = document.getElementById('ead-search-date-in').value;
                query+=(!filtered ? "?" : "&") + "in="+dateIn
            }
        }

       fetch(query).then(res => res.text()).then(text => {
        document.getElementById('ead-search-results').innerHTML = text
       })
  }

  buildFtIndex(){
    //fetch(this.baseURL + )
  }
 }

let eadSearch = new EadSearchPlugin('EadSearchPlugin')
window.eadSearch = eadSearch;
MAX.addPlugin(eadSearch);
+517 −0

File added.

Preview size limit exceeded, changes collapsed.

+8 −2
Original line number Diff line number Diff line
@@ -57,8 +57,14 @@ declare function max.config:getPluginByName($projectId, $pluginName){
};

declare function max.config:getPluginParameterValue($projectId, $pluginName, $parameterKey){
  doc($max.config:CONFIGURATION_FILE)//edition[@xml:id=$projectId]//plugin[@name=$pluginName]
    //parameter[@key=$parameterKey]/@value/string()
    let $param := doc($max.config:CONFIGURATION_FILE)//edition[@xml:id=$projectId]//plugin[@name=$pluginName]
            //parameter[@key=$parameterKey]
    return
        if($param/@value)
        then  $param/@value/string()
        else $param
(:  doc($max.config:CONFIGURATION_FILE)//edition[@xml:id=$projectId]//plugin[@name=$pluginName]:)
(:    //parameter[@key=$parameterKey]/@value/string():)
  
};