247 lines
8.3 KiB
JavaScript
247 lines
8.3 KiB
JavaScript
|
/* global Scrutari */
|
||
|
|
||
|
/**
|
||
|
* Surcharge et complément du client ScrutariJs pour le site librevous.org
|
||
|
*/
|
||
|
|
||
|
Scrutari.Lav = {};
|
||
|
|
||
|
/**
|
||
|
* Adresse du serveur Scrutari
|
||
|
*/
|
||
|
Scrutari.Lav.SCRUTARI_URL = "https://sct2.scrutari.net/sct/april_libreavous/";
|
||
|
/**
|
||
|
* Nombre de fiches dans une pagination
|
||
|
*/
|
||
|
Scrutari.Lav.PAGINATION_LENGTH = 20;
|
||
|
/**
|
||
|
* Tableau des différentes paginations, la première est la pagination sur la totalité des sites, les autres sont les paginations par site
|
||
|
*/
|
||
|
Scrutari.Lav.PAGINATION_ARRAY = ["", "libreavous", "librealire"];
|
||
|
/**
|
||
|
* Indication s'il s'agit de la version de développement
|
||
|
*/
|
||
|
Scrutari.Lav.DEV = false;
|
||
|
|
||
|
/**
|
||
|
* Fonction utilitaire de formatage de la séquence de recherche pour l'affichage
|
||
|
*/
|
||
|
Scrutari.Lav.formatSearchSequence = function (client, scrutariResult) {
|
||
|
var q = scrutariResult.getQ();
|
||
|
q = q.replace(/\&\&/g, '@%et%@');
|
||
|
q = q.replace(/\|\|/g, '@%ou%@');
|
||
|
q = q.replace(/\*/g, '@%*%@');
|
||
|
q = Scrutari.escape(q);
|
||
|
q = q.replace(/@%/g, '<span class="scrutari-lav-Operateur">');
|
||
|
q = q.replace(/%@/g, '</span>');
|
||
|
return q;
|
||
|
};
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Fonction principale d'initialisation du client. Remplace Scrutari.DefaultUi.init
|
||
|
*/
|
||
|
Scrutari.Lav.init = function (client) {
|
||
|
var scrutariMeta = client.scrutariMeta;
|
||
|
client.lav = {}; //propriété lav pour conserver des données propres à ce client
|
||
|
_initLav();
|
||
|
if (Scrutari.Lav.DEV) { //Code propre à la version de développement, en production la structure est en dur dans le squelette Spip
|
||
|
$("#" + client.clientId).html(client.compileStructure("main"));
|
||
|
$("#zone").html(client.compileStructure("main", {
|
||
|
noLoc: true,
|
||
|
withIndent: true,
|
||
|
withPath: true
|
||
|
}));
|
||
|
}
|
||
|
client.initForms();
|
||
|
client.initButtons(Scrutari.Lav.actionHandler);
|
||
|
client.initChangeListeners(Scrutari.Lav.changeHandler);
|
||
|
client.checkInitialQuery();
|
||
|
|
||
|
function _initLav() {
|
||
|
let paginationArray = new Array();
|
||
|
for(let name of Scrutari.Lav.PAGINATION_ARRAY) {
|
||
|
let active = false;
|
||
|
let suffix = "-" + name;
|
||
|
let radio = name;
|
||
|
if (!name) { // valeurs particulières pour « tous »
|
||
|
active = true;
|
||
|
suffix = "";
|
||
|
radio = "tous";
|
||
|
}
|
||
|
paginationArray.push({
|
||
|
name: name,
|
||
|
active: active,
|
||
|
suffix: suffix,
|
||
|
radio: radio
|
||
|
});
|
||
|
}
|
||
|
client.lav.paginationArray = paginationArray;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Fonction d'enrichissement du processus d'affichage des résultats.
|
||
|
* la méthode addPaginisations de la classe Scrutari.Process est remplacée pour que trois paginations
|
||
|
* soient créés : celle, normale, de toutes les fiches et une par site
|
||
|
* Remplace Scrutari.DefaultUi.newResultProcess
|
||
|
*/
|
||
|
Scrutari.Lav.newResultProcess = function (client, scrutariResult, searchOrigin) {
|
||
|
var resultProcess = new Scrutari.Process(client, scrutariResult, searchOrigin);
|
||
|
resultProcess.addPaginations = function ($paginationBlock) {
|
||
|
_addPaginations($paginationBlock);
|
||
|
};
|
||
|
var libreavousArray = new Array();
|
||
|
var librealireArray = new Array();
|
||
|
var length = scrutariResult.ficheGroupArray.length;
|
||
|
if (length > 0) {
|
||
|
for(let fiche of scrutariResult.ficheGroupArray[0].ficheArray) {
|
||
|
if (fiche.codecorpus === 1222) {
|
||
|
libreavousArray.push(fiche);
|
||
|
} else {
|
||
|
librealireArray.push(fiche);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
var arrays = {
|
||
|
libreavous: libreavousArray,
|
||
|
librealire: librealireArray
|
||
|
};
|
||
|
return resultProcess;
|
||
|
|
||
|
function _addPaginations($paginationBlock) {
|
||
|
$paginationBlock.html(client.render("pagination_lav", {array: client.lav.paginationArray}));
|
||
|
client.$count("total-tous", 'update', scrutariResult.getFicheCount());
|
||
|
for(let config of client.lav.paginationArray) {
|
||
|
let pagination = new Scrutari.Pagination(client, config.name);
|
||
|
if (config.name) {
|
||
|
client.$count("total-" + config.name, 'update', arrays[config.name].length);
|
||
|
pagination.getPaginationFicheArray = function (scrutariResult, paginationNumber) {
|
||
|
return _getPaginationFicheArray(config.name, pagination.paginationLength, paginationNumber);
|
||
|
};
|
||
|
}
|
||
|
client.putPagination(config.name, pagination);
|
||
|
pagination.change(1);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function _getPaginationFicheArray(name, paginationLength, paginationNumber) {
|
||
|
let array = arrays[name];
|
||
|
return Scrutari.Utils.extractFicheArray(array, paginationLength, paginationNumber);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Personnalisation des actions sur les boutons, passée à client.initButtons()
|
||
|
*/
|
||
|
Scrutari.Lav.actionHandler = function (client, button, action, target) {
|
||
|
switch(action) {
|
||
|
case 'copyPermalink':
|
||
|
navigator.clipboard.writeText($(button).attr("href"));
|
||
|
break;
|
||
|
default:
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Personnalisation des écouteurs actions sur les boutons, passée à client.initButtons()
|
||
|
*/
|
||
|
Scrutari.Lav.changeHandler = function (client, element, name) {
|
||
|
switch(name) {
|
||
|
case 'lav-site':
|
||
|
if (element.checked) {
|
||
|
_setSiteFilter(element.value);
|
||
|
}
|
||
|
return true;
|
||
|
default:
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
function _setSiteFilter (value) {
|
||
|
let suffix = "";
|
||
|
for(let pagination of client.lav.paginationArray) {
|
||
|
if (value === pagination.radio) {
|
||
|
pagination.active = true;
|
||
|
suffix = pagination.suffix;
|
||
|
} else {
|
||
|
pagination.active = false;
|
||
|
}
|
||
|
}
|
||
|
let activeBlockName = "ficheGroup" + suffix;
|
||
|
client.$block("ficheGroup", "*").each(function (index, element) {
|
||
|
if (element.dataset.scrutariBlock === activeBlockName) {
|
||
|
client.show(element);
|
||
|
} else {
|
||
|
client.hide(element);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Objet définissant les points d'entrée (hook) particuliers, transmis à la configuration
|
||
|
*/
|
||
|
Scrutari.Lav.HOOKS = {
|
||
|
completeFiche : function (scrutariResult, fiche, categoryName) { //Extrait le numéro du titre
|
||
|
let start = fiche.mtitre[0];
|
||
|
let numero = "";
|
||
|
if (start.startsWith("#")) {
|
||
|
let idx = start.indexOf("-");
|
||
|
if (idx > 0) {
|
||
|
numero = start.substring(0, idx).trim();
|
||
|
fiche.mtitre[0] = start.substring(idx + 1);
|
||
|
}
|
||
|
}
|
||
|
fiche._numero = numero;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Objet centralisant les fonctions surchargées du client
|
||
|
*/
|
||
|
Scrutari.Lav.FUNCTIONS = {
|
||
|
uiInit: function (client) {
|
||
|
Scrutari.Lav.init(client);
|
||
|
},
|
||
|
ignoreElement: null,
|
||
|
isFilterEnable: null,
|
||
|
mainsearchResultCallback: function (client, scrutariResult) {
|
||
|
$("#scrutari_lav_sequence").html(Scrutari.Lav.formatSearchSequence(client, scrutariResult));
|
||
|
},
|
||
|
mainsearchErrorCallback: null,
|
||
|
subsearchResultCallback: null,
|
||
|
subsearchErrorCallback: null,
|
||
|
initFilterByQuery: null,
|
||
|
newResultProcess: function (client, scrutariResult, searchOrigin) {
|
||
|
return Scrutari.Lav.newResultProcess(client, scrutariResult, searchOrigin);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Initialisation du client
|
||
|
*/
|
||
|
$(function () {
|
||
|
var scrutariConfig = new Scrutari.Config("libreavous", Scrutari.Lav.SCRUTARI_URL, "fr", "site", {
|
||
|
paginationLength: Scrutari.Lav.PAGINATION_LENGTH,
|
||
|
limit: -1
|
||
|
});
|
||
|
Scrutari.Client.init(scrutariConfig, "scrutari_client", {
|
||
|
withCorpus: false,
|
||
|
ficheTarget: "_blank",
|
||
|
ficheBodyList: "fiche_mtitre,fiche_msoustitre,libreavous_start_ul,fiche_primaryattributearray,fiche_mcomplementarray,fiche_secondaryattributearray,fiche_motclearray,fiche_bythesaurusarray,libreavous_end_ul", //Ajout de libreavous_start_ul et libreavous_end_ul, retrait de fiche_year
|
||
|
initialQuery: "",
|
||
|
initialQId: "",
|
||
|
ignoreList: "area-title,area-stats,modal-*", //Utilisé en mode développement
|
||
|
permalinkPattern: "https://www.libreavous.org/spip.php?page=scrutarijs&qid=$QID",
|
||
|
hooks: Scrutari.Lav.HOOKS,
|
||
|
functions: Scrutari.Lav.FUNCTIONS
|
||
|
|
||
|
}, function (client) {
|
||
|
|
||
|
});
|
||
|
});
|