Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 1582 | Rev 1593 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

//+----------------------------------------------------------------------------------------------------------+
// Onglets et Portlets
/**
 * Les variables suivantes ont été ajoutée par php
 * 
 * var urlTexteBrutSectionWikiTpl : url pour obtenir le texte brut d'une section de wiki
 * var urlEditionSectionWikiTpl : url pour éditer une section du wiki 
 * var urlTexteFormateSectionWikiTpl : url pour obtenir le texte formaté en html d'une section de wiki
 * var pageWikiTaxon : url pour obtenir le texte formaté en html d'une section de wiki
 * var urlPopup = url de base pour les popup contenant du code un peu complexe
 * 
*/

/**
 * Variables globales du script
 */
var modules = Array();
var REFERENTIEL = "<?php echo Registre::get('parametres.referentiel'); ?>";

/** Classe Module
* Définit un module de type portlet
**/
function Module(id, module, onglet, statique) {

        // Variables de classe : identifiant, titre, et le HTML du résumé à afficher dans le contenu
        this.id = id;
        this.titre;
        this.htmlResume;
        this.statique = statique;
}

function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}


// remplace des liens directs vers les onglets par des liens permettant de ne charger
// que le contenu de l'onglet (pour le plugin tab)
function remplacerUrlsOnglets() {
                $(".lien-onglet").each(function() {
                var urlOnglet = $(this).attr('href');
                var urlVars = urlOnglet.slice(urlOnglet.indexOf('?') + 1);
                urlBase = base_url_application_onglets+"?"+urlVars;
                urlOnglet = urlBase.replace('action=fiche','action=onglet');
                $(this).attr('href', urlOnglet);
        });
}



//+----------------------------------------------------------------------------------------------------------+
// Wikini Flora
var htmlWikiOriginal = '';
var objetContenuWiki = null;

function getTemplateFormulaireEditionWiki(page, section, texte) {       
        var urlWebServiceWiki = getUrlEditionPourPageEtSection(page, section);
        var formulaire = 
                '<form method="post" id="formulaire_edition_wiki" name="formulaire_edition_wiki" action="'+urlWebServiceWiki+'">'+
                  '<div>'+
                        '<textarea id="pageContenu" name="pageContenu" class="champ_edition_wiki">'+texte+'</textarea>'+
                  '</div>'+
                  '<input type="hidden" name="pageSectionTitre" id="pageSectionTitre" value="'+section+'" />'+
                  '<input type="submit" text="sauver" />'+
                  '<input class="bouton_annuler_edition" type="button" value="annuler" />';
                '</form>';              
        return formulaire;
}

function getSection() {
        return objetContenuWiki.attr("title");
}

function getPageWikiTaxon() {
        return pageWikiTaxon;
}

function getUrlEditionPourPageEtSection(page, section) {
        var url = urlEditionSectionWikiTpl.replace('{pageTag}', page).replace('{sectionTitre}', section);
        return url;
}

function getUrlTextePourPageSectionBrute(page, section) {
        return urlTexteBrutSectionWikiTpl.replace('{pageTag}', page).replace('{sectionTitre}', section);
}

function getUrlTextePourPageSectionFormatee(page, section) {
        return urlTexteFormateSectionWikiTpl.replace('{pageTag}', page).replace('{sectionTitre}', section);
}

function remplacerContenuWikiParFormulaireEdition(objet_contenu) {
        objetContenuWiki = objet_contenu;
        var adresse = getUrlEditionPourPageEtSection(getPageWikiTaxon(), getSection());
        htmlWikiOriginal = objet_contenu.html();
        $.getJSON(adresse, function(data) {
                $(objetContenuWiki).removeClass('editable_sur_clic').html(getTemplateFormulaireEditionWiki(getPageWikiTaxon(), getSection(), data.texte));
        });
}

function gererEvenementsWiki() {
        //TODO: ajout automatique d'un lien ouvrant une page d'aide (un popup) au formatage des pages wikini
        $('.editable_sur_clic').live('dblclick',function(event) {
                remplacerContenuWikiParFormulaireEdition($(this));
        });
        
        $('#formulaire_edition_wiki').live('submit',function(event) {
            event.preventDefault(); 
                var valeurs = $(this).serialize();
                $.post($(this).attr('action'), valeurs, function(data) {
                        var adresse = getUrlTextePourPageSectionFormatee(getPageWikiTaxon(), getSection());
                        $.getJSON(adresse, function(data) {
                                $(objetContenuWiki).addClass('editable_sur_clic').html(data.texte);
                        });
                });
        });
        
        $('.bouton_annuler_edition').live('click', function(event) {
                event.preventDefault();
                $(objetContenuWiki).addClass('editable_sur_clic').html(htmlWikiOriginal);
        });
        
        rendreLienAffichableDansNouvelleFenetre('.contenu_editable');
}

//+----------------------------------------------------------------------------------------------------------+
//Affichage dans une nouvelle fenetre
function rendreLienAffichableDansNouvelleFenetre(selecteur) {
        $(selecteur).find('a').live('click', function(event) {
                window.open($(this).attr('href'));
                event.preventDefault();
                return false;
        });
}

//+----------------------------------------------------------------------------------------------------------+
// Plier / déplier
function plierTout() {
        $('.nom').children('.imagetteMoins').removeClass('imagetteMoins').addClass('imagettePlus');
        $('.plus').css('display', 'none');
}

function deplierTout() {
        $('.nom').children('.imagettePlus').removeClass('imagettePlus').addClass('imagetteMoins');
        $('.plus').css('display', 'inline');
}

function gestionBiblio(parent) {
        $('.nom').each(function () {
                if ($(this).children('.plus').length == 0) {
                        html = $(this).html();
                        
                        // La partie biblio commence au premier crochet ouvrant
                        // ou bien au span de la biblio à exclure
                        posCrochetGauche = html.indexOf('[');
                        posbiblioAExclure = html.indexOf('<span class="bib_excl">');
                        
                        if(posCrochetGauche < 0 || (posbiblioAExclure > 0 && posbiblioAExclure < posCrochetGauche)) {
                                posCrochetGauche = posbiblioAExclure;
                        }
                        
                        // console.log(posCrochetGauche+" "+posbiblioAExclure);
                        
                        
                        if (posCrochetGauche > 0) {
                                nom = html.substr(0, posCrochetGauche);
                                biblio = html.substr(posCrochetGauche, html.length);
                                $(this).html(nom);
                                
                                imagettePlus = document.createElement('div');
                                $(imagettePlus).addClass('imagettePlus');
                                $(this).prepend($(imagettePlus));
                                
                                plus = document.createElement('span');
                                $(plus).addClass('plus');
                                $(plus).html(biblio);
                                $(plus).hide();
                                
                                $(this).click(function(e) {
                                        // test pour ne réagir que sur le clic sur le + ou -
                                        // TODO: fusionner ce bout de code en doublon avec synthese.js
                                        if($(e.target).hasClass("imagettePlus") || $(e.target).hasClass("imagetteMoins")) {
                                                if (!$(this).children('.plus').is(':visible')) {
                                                        $(this).children('.imagettePlus').removeClass('imagettePlus').addClass('imagetteMoins');
                                                        $(this).children('.plus').css('display', 'inline');
                                                } else {
                                                        $(this).children('.imagetteMoins').removeClass('imagetteMoins').addClass('imagettePlus');
                                                        $(this).children('.plus').css('display', 'none');
                                                }
                                        }
                                        
                                });
                                        
                                $(this).append($(plus));
                                
                        }
                }
        });
}

function gererEvenementsPliage() {
        $('.lien_tout_deplier').live('click', function() {
                deplierTout();
        });
        $('.lien_tout_plier').live('click', function() {
                plierTout();
        });
        gestionBiblio(document);
}

//+----------------------------------------------------------------------------------------------------------+
//Pop Up images
function ouvrirPopUpImg(event) {
        event.preventDefault();
        window.open($(this).attr('href'),"Photo_"+$(this).children("img").attr("data-num-image"),
                        '"'+'height='+event.data.h+',width='+event.data.w+',top='+event.data.t+',left='+event.data.l 
                        +',toolbar='+event.data.toolbar+',menubar='+event.data.menubar +',location='
                        +event.data.location+',resizable='+event.data.resizable+',scrollbars='+event.data.scrollbars 
                        +',status='+event.data.status+'"');
}

//+----------------------------------------------------------------------------------------------------------+
// Message de chargement pour les graphique écologie
function gererChargementGraphiqueEcologie() {
        $('.ecologie_svg').addClass('chargement');
}

function gererAffichageLegendeEcologie() {
        $('.voir').live('click', function() {
                $(this).siblings('.legende_graphique').show();
                $(this).siblings('.cacher').show();
                $(this).hide();
        });
        
        $('.cacher').live('click', function() {
                $(this).siblings('.legende_graphique').hide();
                $(this).siblings('.voir').show();
                $(this).hide();
        });
}


//+----------------------------------------------------------------------------------------------------------+
// Initialisation
var fancyboxinitialise = false;
var param_popup_Coste = {h: 650, w: 550, t: 100, l: 100, 
                toolbar: 'no', menubar: 'no', location: 'no', resizable: 'yes', scrollbars: 'yes', status: 'no'};

var param_popup_Cel = {h: 750, w: 630, t: 100, l: 100, 
                toolbar: 'no', menubar: 'no', location: 'no', resizable: 'yes', scrollbars: 'yes', status: 'no'};

//Initialisation (uniquement sur la présence de la div bloc-fiche, qui contient la fiche eflore)
$(document).one('ficheSyntheseChargee', function() {
        initialiserOnglets();

        // TODO : devrait être .on() à la place de live()
        $('.lien_popup').live('click',function(event) {
                event.preventDefault();
                $.fancybox(this,{
                        autoDimensions:false,
                        width:580
                });
        });
        
        // TODO : devrait être .on() à la place de live()
        $('.lien_popup.lien_metadonnees').live('click',function(event) {
                event.preventDefault();
                $.fancybox(this,{
                        autoDimensions:true
                });
        });
                
        $('.lien-image-cel').live('click', param_popup_Cel , ouvrirPopUpImg);
        $('.lien-image-coste').live('click', param_popup_Coste , ouvrirPopUpImg);
        
        afficherOngletsPourNiveau();

        gererEvenementsWiki();
        gererEvenementsPliage();
        
        $('a.lien_retour_synthese').click(function(event) {
                $('#onglet_synthese a').click();
                event.preventDefault();
        });
        
        $(window).resize(function() {
                redimensionnerOnglets();
        });
        redimensionnerOnglets();
        
        if($("#bloc-fiche").length > 0) {
                // initialisation de l'url avec l'onglet par défaut ou bien 
                // celui qui a été explicitement demandé
                mettreAJourUrl(permalienNumNom+"-"+onglet, onglet);
        }
        gererInteractionsPermaliens();
});

function ajouterEvenement(elementHtml, nomEvenement, functionCallBack) {  
        if(!!elementHtml && elementHtml != null) {      
                if(elementHtml.attachEvent) {// Internet Explorer
                        elementHtml.attachEvent("on" + nomEvenement, function() {functionCallBack.call(elementHtml);}); 
                } else if(elementHtml.addEventListener) { // Firefox & autres
                        elementHtml.addEventListener(nomEvenement, functionCallBack, false);
                }
        }
}

$(document).one('ongletRepartitionCharge', function() {
        var contRepObs = $(".conteneur_repartition_observations");
        contRepObs.addClass("chargement_repartition");
        // utilisation d'une fonction native car jQuery refuse d'ajouter un évènement load
        // sur autre chose qu'une image
        $('#repartition_observations').ready(function() {
                ajouterEvenement(document.getElementById("repartition_observations"), "load", function() {
                        contRepObs.removeClass("chargement_repartition");
                });
        });
        // lien "recalculer la carte de moissonnage"
        $('#lien_recalculer_carte').click(function() {
                var carteMoissonnageSvg = $('#repartition_observations'); // c'est plutôt "moissonnage" que "répartition" => squelette pourave power
                var urlCarte = carteMoissonnageSvg.attr("data");
                urlCarte += '&recalculer=1';
                contRepObs.addClass("chargement_repartition");
                carteMoissonnageSvg.attr("data", urlCarte);ajouterEvenement(document.getElementById("repartition_observations"), "load", function() {
                        contRepObs.removeClass("chargement_repartition");
                });
                return false;
        });
});


// À la fin du chargement de l'onglet Illustrations
$(document).one('ongletIllustrationsCharge', function() {
        $('a.lien-images-organes.fourni').live('click', afficherOngletOrgane);
        $('a.lien-grande-image-organe').live('click', afficherGrandeImageOrgane);
        // pour que la galerie soit ouverte par défaut sur le premier organe non vide
        var premier = $('a.lien-images-organes.fourni').first().data('tag');
        afficherOngletOrgane(null, premier);
        
        $('a.lien_telechargement_image').live('click',function(e) {
                if($(e.target).data('oneclicked')!='yes')
                {
                        $(e.target).data('oneclicked','yes');
                        $(this).fancybox({
                                "type" : "iframe",
                                "autoDimensions" : false,
                                "width" : 800,
                                "height" : 405,
                                "titleShow" : false
                        }).click();
                }

                e.preventDefault();
                return false;
        });
});

// Affiche les n images ayant le plus de votes, pour l'organe $tag
function afficherOngletOrgane(e, tag) {
        var onglets = $('div.onglet-organe'),
                contenu = $('#galerie-organes-contenu'),
                organeOnglet;

        if (tag === undefined) {
                tag = $(this).data('tag')
        }
        if ($(this).parent().hasClass('active')) { // replier
                $(this).parent().removeClass('active')
                contenu.hide();
                return false;
        }

        onglets.each(function() {
                organeOnglet = $(this).data('tag');
                var li = $('a.lien-images-organes[data-tag="' + organeOnglet + '"]').parent();
                if (organeOnglet == tag) {
                        $(this).show();
                        li.addClass('active');
                } else {
                        $(this).hide();
                        li.removeClass('active');
                }
        });

        contenu.show();

        var premierLienGrandeImage = $('div.onglet-organe:visible').find('a.lien-grande-image-organe').first(),
                grandeImage = $('#grande-image-organe').find('img');
        premierLienGrandeImage.trigger('click');
        grandeImage.show('fast');

        return false;
}

// Affiche en grand sous les miniatures l'image d'organe sur laquelle on a cliqué
function afficherGrandeImageOrgane() {
        if ($(this).data('id-image') === "") {
                return false;
        }
        var grandeImage = $('#grande-image-organe').find('img'),
                source = $(this).find('img').attr('src');
        source = obtenirUrlMoyenFormatIdImage($(this).data('id-image'));
        // chargement image et lien popup
        grandeImage.attr('src', source);
        // important pour Pinterest et cie.
        grandeImage.attr('alt', $(this).data('auteur') + ' - ' + $(this).data('titre'));
        grandeImage.parent().attr('href',obtenirUrlGrandFormatIdImage($(this).data('id-image')));
        // métadonnées
        var metadonnees = ['id-image', 'titre', 'description', 'localisation', 'auteur'],
                clef;
        for (var i = 0; i < metadonnees.length; i++) {
                clef = metadonnees[i];
                var contenu = $(this).data(clef);
                if(clef == 'id-image') {
                        contenu += 
                                '    <a title="T\xE9l\xE9charger cette image" class="lien_telechargement_image" href="'+urlBaseWidget+'telechargement?id_image='+$(this).data(clef)+'">'+
                                        '<img class="icone_action_ill" src="'+urlCssImages+'sauver.png" />'+
                                '</a>    '+
                                '<a title="Voir plus d\'informations sur cette image (s\'ouvre dans un popup)" class="lien-image-cel" href="'+urlPopup+'?module=popup-illustrations&action=fiche&referentiel=bdtfx&id=' + $(this).data('id-image')+'">'+
                                        '<img class="icone_action_ill" src="'+urlCssImages+'infos_image.png" />'+
                                '</a>'+
                                '<a style="float:right" title="Signaler une mauvaise identification ou en proposer une autre via l\'outil identiplante" class="signaler-mauvaise-identification" href="' + $(this).data('url-mauvaise-ident') + '" target="_blank">' +
                                        '<img class="icone_action_ill" src="'+urlCssImages+'mauvaise_id.png">Signaler une mauvaise identification' +
                                '</a>';
                }
                $('#grande-image-legende-' + clef).html(contenu);

        }
        // valeurs génériques
        $('#grande-image-legende-' + clef).html($(this).data(clef));
        $('#grande-image-legende-date').html(formaterDateVersFrancais($(this).data('date')));
        $('#grande-image-legende-id-destinataire').attr('href', urlPopup+'?module=popup-contact&action=form&referentiel='+REFERENTIEL+'&id_destinataire=' +
                        $(this).data('id-destinataire') + '&id_img=' + $(this).data('id-image'));
        $('#grande-image-legende-id-destinataire').attr('title', "Envoyer un message \xE0 l'auteur \xE0 propos de cette image (n\xE9cessite d'\xEAtre identifi\xE9)");
        $('#grande-image-legende-auteur').attr('href', 'http://tela-botanica.org/profil:' + $(this).data('id-destinataire'));
        $('#grande-image-legende-auteur').attr('target', '_blank');
        $('#grande-image-legende-auteur').attr('title', "Voir le profil de cet utilisateur (n\xE9cessite d'\xEAtre identifi\xE9)");


        $('#grande-image-legende-id-image a.lien_telechargement_image').unbind('click.fb').fancybox({
                "type" : "iframe",
                "autoDimensions" : false,
                "width" : 800,
                "height" : 405,
                "titleShow" : false
        });
}

function obtenirUrlMoyenFormatIdImage(idImage) {
        idImage = remplirChaineDebut(idImage, "0", 9);
        return urlImagesOrganesMoyenFormatTpl.replace('{id_image}', idImage);
}

function obtenirUrlGrandFormatIdImage(idImage) {
        idImage = remplirChaineDebut(idImage, "0", 9);
        return urlImagesOrganesGrandFormatTpl.replace('{id_image}', idImage);
}

function remplirChaineDebut(chaine, remplissage, longueur) {
        while(chaine.toString().length < longueur) {
                chaine = remplissage.toString()+chaine.toString();
        }
        return chaine;
}

// transforme une date du type "2012-04-06" en "06 avril 2012"
function formaterDateVersFrancais(date) {
        var mois = ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'],
                amj_hms = date.split(' '),
                amj = amj_hms[0].split('-');
        return amj[2] + ' ' + mois[parseInt(amj[1]) - 1] + ' ' + amj[0];
}


// ouvre le popup de galerie lors d'un clic sur l'illustration dans la fiche synthèse
function gererClicIllustrationsFiche() {
        $('.illustration_cel').live('click', function() {
                var url_image = $(this).attr('src');
                var titre = $('.nomenclature').first().text();
                var url = urlPopup+"?module=popup-galerie&action=fiche&num_nom="+$(this).attr('data-num-nom')+"&titre="+encodeURIComponent(titre)+"&url_image="+encodeURIComponent(url_image)+"&referentiel="+REFERENTIEL;
                //var url = urlPopup+"?module=popup-galerie-organes&action=fiche&num_nom="+$(this).attr('data-num-nom')+"&titre="+encodeURIComponent(titre)+"&url_image="+encodeURIComponent(url_image)+"&referentiel="+REFERENTIEL;
                //alert(url);
                //return false;
                window.open(url, $(this).attr('data-num-nom'), 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no, width='+(400)+', height='+(375));
        }); 
}

function ouvrirFenetreIllustrationFiche(url, titre, hauteur, largeur) {
        var fenetre = window.open('_blank', '','directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no, width='+(largeur+17)+', height='+(hauteur+17));
        var tmp = fenetre.document;
        tmp.write('<html><head><title>'+titre+'</title>');
        tmp.write('</head><body>');
        tmp.write('<p style="height='+hauteur+'px;text-align:center;line-height='+hauteur+'px;"><img id="image_agrandie" height="'+hauteur+'" width="'+largeur+'" style="vertical-align:middle;" src="'+url+'" /></p>');
        tmp.write('</body></html>');
        tmp.close();
}


// met à jour l'url dans la barre du navigateur ainsi que dans le permalien en bas
// de la fiche
function mettreAJourUrl(nouvelleUrl, etat) {
        $('#permalien_num_nom').val(nouvelleUrl);
        if (!!(window.history && window.history.pushState)) {
                var currentState = window.history.state;
                if (currentState != nouvelleUrl && !!etat) {
                        window.history.pushState(etat, "", nouvelleUrl);
                }
        }
}

function gererInteractionsPermaliens() {        
        $(".conteneur_permalien > a").each(function() {
                var txt = '<input type="text" id="'+$(this).attr('id')+'" readonly="readonly" class="lien_externe" value="'+$(this).attr('href')+'">';
                $(this).replaceWith(txt);
        });
        $(".conteneur_permalien > input").each(function() {
                $(this).attr('size', $(this).val().length - 7);
        });
        $(".conteneur_permalien > input").hover(
                function() {
                         $(this).select();
                }, function() {
                        // rien à faire sur le mouseout
                }
        );
}

function gererTemplatesChargementALaDemande() {
        $('.lien-ajax-tpl a.lien-ajax-tpl-chargement').on('click', function(e) {
                if($(this).attr('data-loading') != "true") {
                        $(this).attr('data-loading', "true");
                        e.preventDefault();
                        urlTpl = $(this).attr('href');
                        $(this).hide();
                        thisElementParent = $(this).parent();
                        thisElementParent.append("<div class='tab-chargement'>Chargement des informations...</div>");
                        $.get(urlTpl, function( data ) {
                                thisElementParent.replaceWith( data );
                        });
                        return false;
                }
        });
}

$(document).ready(function() {
        gererChargementGraphiqueEcologie();
        gererAffichageLegendeEcologie();
        gererTemplatesChargementALaDemande();
        if (document.implementation.hasFeature('http://www.w3.org/TR/SVG11/feature#BasicStructure', '1.1') === false) {
                $('.svg').each(function() {
                        var img = document.createElement('img');
                        img.src = $(this).attr('alt');
                        $(this).replaceWith(img);
                });
        }
});