New file |
0,0 → 1,390 |
/** |
* Constructeur WidgetSaisie par défaut |
*/ |
function WidgetSaisie() { |
this.langue = 'fr'; |
this.obsNbre = 0; |
this.nbObsEnCours = 1; |
this.totalObsATransmettre = 0; |
this.nbObsTransmises = 0; |
this.debug = null; |
this.html5 = null; |
this.tagProjet = null; |
this.tagImg = null; |
this.tagObs = null; |
this.separationTagImg = null; |
this.separationTagObs = null; |
this.obsId = null; |
this.serviceSaisieUrl = null; |
this.serviceObsUrl = null; |
this.nomSciReferentiel = null; |
this.especeImposee = false; |
this.infosEspeceImposee = null; |
this.autocompletionElementsNbre = null; |
this.referentielImpose = null; |
this.serviceAutocompletionNomSciUrl = null; |
this.serviceAutocompletionNomSciUrlTpl = null; |
this.obsMaxNbre = null; |
this.dureeMessage = null; |
this.serviceAnnuaireIdUrl = null; |
this.serviceNomCommuneUrl = null; |
this.serviceNomCommuneUrlAlt = null; |
this.chargementIconeUrl = null; |
this.chargementImageIconeUrl = null; |
this.calendrierIconeUrl = null; |
this.pasDePhotoIconeUrl = null; |
} |
|
/** |
* Initialisation du widget |
*/ |
WidgetSaisie.prototype.init = function() { |
this.initForm(); |
this.initEvts(); |
this.requeterIdentite(); |
}; |
|
/** |
* Initialise le formulaire, les validateurs, les listes de complétion... |
*/ |
WidgetSaisie.prototype.initForm = function() { |
if (this.obsId != '') { |
//this.chargerInfoObs(); |
} |
|
this.configurerDatePicker('#date'); |
this.ajouterAutocompletionNoms(); |
//this.configurerFormValidator(); |
//this.definirReglesFormValidator(); |
|
if(this.especeImposee) { |
$("#taxon").attr("disabled", "disabled"); |
$("#taxon-input-groupe").attr("title",""); |
// Bricolage cracra pour avoir le nom retenu avec auteur (nom_retenu.libelle ne le mentionne pas) |
var nomRetenuComplet = this.infosEspeceImposee["nom_retenu_complet"], |
debutAnneRefBiblio = nomRetenuComplet.indexOf(" ["); |
if (debutAnneRefBiblio != -1) { |
nomRetenuComplet = nomRetenuComplet.substr(0, debutAnneRefBiblio); |
} |
// fin bricolage cracra |
var infosAssociee = { |
label : this.infosEspeceImposee.nom_sci_complet, |
value : this.infosEspeceImposee.nom_sci_complet, |
nt : this.infosEspeceImposee.num_taxonomique, |
nomSel : this.infosEspeceImposee.nom_sci, |
nomSelComplet : this.infosEspeceImposee.nom_sci_complet, |
numNomSel : this.infosEspeceImposee.id, |
nomRet : nomRetenuComplet, |
numNomRet : this.infosEspeceImposee["nom_retenu.id"], |
famille : this.infosEspeceImposee.famille, |
retenu : (this.infosEspeceImposee.retenu == 'false') ? false : true |
}; |
$("#taxon").data(infosAssociee); |
} |
}; |
|
/** |
* Initialise les écouteurs d'événements |
*/ |
WidgetSaisie.prototype.initEvts = function() { |
var lthis = this; |
$('body').on('click', '.effacer-miniature', function() { |
$(this).parent().remove(); |
}); |
$("#fichier").bind('change', function (e) { |
arreter(e); |
var options = { |
success: lthis.afficherMiniature.bind(lthis), // post-submit callback |
dataType: 'xml', // 'xml', 'script', or 'json' (expected server response type) |
resetForm: true // reset the form after successful submit |
}; |
$("#miniature").append('<img id="miniature-chargement" class="miniature" alt="chargement" src="'+this.chargementImageIconeUrl+'"/>'); |
$("#ajouter-obs").attr('disabled', 'disabled'); |
if(lthis.verifierFormat($("#fichier").val())) { |
$("#form-upload").ajaxSubmit(options); |
} else { |
$('#form-upload')[0].reset(); |
window.alert("Le format de fichier n'est pas supporté, les formats acceptés sont "+ $("#fichier").attr("accept")); |
} |
return false; |
}); |
// identité |
$("#courriel").on('blur', this.requeterIdentite.bind(this)); |
$("#courriel").on('keypress', this.testerLancementRequeteIdentite.bind(this)); |
$(".alert .close").on('click', this.fermerPanneauAlert); |
$(".has-tooltip").tooltip('enable'); |
$("#btn-aide").on('click', this.basculerAffichageAide); |
$("#prenom").on("change", this.formaterPrenom.bind(this)); |
$("#nom").on("change", this.formaterNom.bind(this)); |
|
$("#courriel_confirmation").on('paste', this.bloquerCopierCollerCourriel.bind(this)); |
/*$("#ajouter-obs").on('click', this.ajouterObs.bind(this)); |
$(".obs-nbre").on('changement', this.surChangementNbreObs.bind(this)); |
$("body").on('click', ".supprimer-obs", function() { |
var that = this, |
suppObs = lthis.supprimerObs.bind(lthis); |
// bricolage pour avoir les deux contextes en même temps (objet et elt. du DOM) |
suppObs(that); |
}); |
$("#transmettre-obs").on('click', this.transmettreObs.bind(this)); |
$("#referentiel").on('change', this.surChangementReferentiel.bind(this)); |
|
$("body").on('click', ".defilement-miniatures-gauche", function(event) { |
event.preventDefault(); |
lthis.defilerMiniatures($(this)); |
}); |
$("body").on('click', ".defilement-miniatures-droite", function(event) { |
event.preventDefault(); |
lthis.defilerMiniatures($(this)); |
}); |
*/ |
// fermeture fenêtre |
if (this.debug == false) { |
$(window).on('beforeunload', function(event) { |
return 'Êtes vous sûr de vouloir quiter la page?\nLes observations saisies mais non transmises seront perdues.'; |
}); |
} |
|
}; |
|
/** |
* Retourne true si l'extension de l'image "nom" est .jpg ou .jpeg |
*/ |
WidgetSaisie.prototype.verifierFormat = function(nom) { |
var parts = nom.split('.'); |
extension = parts[parts.length - 1]; |
return (extension.toLowerCase() == 'jpeg' || extension.toLowerCase() == 'jpg'); |
}; |
|
/** |
* Affiche la miniature d'une image temporaire (formulaire) qu'on a ajoutée à l'obs |
*/ |
WidgetSaisie.prototype.afficherMiniature = function(reponse) { |
if (this.debug) { |
var debogage = $("debogage", reponse).text(); |
//console.log("Débogage upload : "+debogage); |
} |
var message = $("message", reponse).text(); |
if (message != '') { |
$("#miniature-msg").append(message); |
} else { |
$("#miniatures").append(this.creerWidgetMiniature(reponse)); |
} |
$('#ajouter-obs').removeAttr('disabled'); |
}; |
|
/** |
* Crée la miniature d'une image temporaire (formulaire), avec le bouton pour l'effacer |
*/ |
WidgetSaisie.prototype.creerWidgetMiniature = function(reponse) { |
var miniatureUrl = $("miniature-url", reponse).text(); |
var imgNom = $("image-nom", reponse).text(); |
var html = |
'<div class="miniature">'+ |
'<img class="miniature-img" class="miniature" alt="'+imgNom+'" src="'+miniatureUrl+'"/>'+ |
'<button class="effacer-miniature" type="button">Effacer</button>'+ |
'</div>' |
return html; |
}; |
|
/** |
* Efface une miniature (formulaire) |
*/ |
WidgetSaisie.prototype.supprimerMiniature = function(miniature) { |
miniature.parents('.miniature').remove(); |
}; |
|
/** |
* Efface toutes les miniatures (formulaire) |
*/ |
WidgetSaisie.prototype.supprimerMiniatures = function() { |
$("#miniatures").empty(); |
$("#miniature-msg").empty(); |
}; |
|
/* Observateur */ |
WidgetSaisie.prototype.testerLancementRequeteIdentite = function(event) { |
if (event.which == 13) { |
this.requeterIdentite(); |
this.event.preventDefault(); |
this.event.stopPropagation(); |
} |
}; |
|
WidgetSaisie.prototype.requeterIdentite = function() { |
var lthis = this; |
var courriel = $("#courriel").val(); |
var urlAnnuaire = this.serviceAnnuaireIdUrl + courriel; |
if (courriel != '') { |
$.ajax({ |
url : urlAnnuaire, |
type : "GET", |
success : function(data, textStatus, jqXHR) { |
if (lthis.debug) { |
console.log('SUCCESS: '+textStatus); |
} |
if (data != undefined && data[courriel] != undefined) { |
var infos = data[courriel]; |
lthis.surSuccesCompletionCourriel(infos, courriel); |
} else { |
lthis.surErreurCompletionCourriel(); |
} |
}, |
error : function(jqXHR, textStatus, errorThrown) { |
if (lthis.debug) { |
console.log('ERREUR: '+textStatus); |
} |
lthis.surErreurCompletionCourriel(); |
}, |
complete : function(jqXHR, textStatus) { |
if (lthis.debug) { |
console.log('COMPLETE: '+textStatus); |
} |
// @TODO harmoniser class="hidden" VS style="display:none;" |
$("#zone-prenom-nom").removeClass("hidden").show(); |
$("#zone-courriel-confirmation").removeClass("hidden").show(); |
} |
}); |
} |
}; |
|
WidgetSaisie.prototype.surSuccesCompletionCourriel = function(infos, courriel) { |
$("#id_utilisateur").val(infos.id); |
$("#prenom").val(infos.prenom); |
$("#nom").val(infos.nom); |
$("#courriel_confirmation").val(courriel); |
$("#prenom, #nom, #courriel_confirmation").attr('disabled', 'disabled'); |
this.focusChampFormulaire(); |
this.masquerPanneau("#dialogue-courriel-introuvable"); |
}; |
|
WidgetSaisie.prototype.surErreurCompletionCourriel = function() { |
$("#prenom, #nom, #courriel_confirmation").val(''); |
$("#prenom, #nom, #courriel_confirmation").removeAttr('disabled'); |
this.afficherPanneau("#dialogue-courriel-introuvable"); |
}; |
|
WidgetSaisie.prototype.formaterNom = function() { |
$(this).val($(this).val().toUpperCase()); |
}; |
|
WidgetSaisie.prototype.formaterPrenom = function() { |
var prenom = new Array(); |
var mots = $(this).val().split(' '); |
for (var i = 0; i < mots.length; i++) { |
var mot = mots[i]; |
if (mot.indexOf('-') >= 0) { |
var prenomCompose = new Array(); |
var motsComposes = mot.split('-'); |
for (var j = 0; j < motsComposes.length; j++) { |
var motSimple = motsComposes[j]; |
var motMajuscule = motSimple.charAt(0).toUpperCase() + motSimple.slice(1); |
prenomCompose.push(motMajuscule); |
} |
prenom.push(prenomCompose.join('-')); |
} else { |
var motMajuscule = mot.charAt(0).toUpperCase() + mot.slice(1); |
prenom.push(motMajuscule); |
} |
} |
$(this).val(prenom.join(' ')); |
}; |
|
WidgetSaisie.prototype.bloquerCopierCollerCourriel = function() { |
this.afficherPanneau("#dialogue-bloquer-copier-coller"); |
return false; |
}; |
|
WidgetSaisie.prototype.focusChampFormulaire = function() { |
$("#date").focus(); |
}; |
|
/* calendrier */ |
WidgetSaisie.prototype.configurerDatePicker = function(selector) { |
$.datepicker.setDefaults($.datepicker.regional[this.langue]); |
$(selector).datepicker({ |
dateFormat: "dd/mm/yy", |
maxDate: new Date, |
onSelect: function(date) { |
$(this).valid(); |
} |
}); |
$(selector + ' + img.ui-datepicker-trigger').appendTo(selector + '-icone.add-on'); |
}; |
|
/* auto completion nom sci */ |
WidgetSaisie.prototype.ajouterAutocompletionNoms = function() { |
var lthis = this; |
$('#taxon').autocomplete({ |
source: function(requete, add){ |
// la variable de requête doit être vidée car sinon le parametre "term" est ajouté |
requete = ""; |
if($("#referentiel").val() != "autre") { |
var url = lthis.getUrlAutocompletionNomsSci();console.log(url); |
$.getJSON(url, requete, function(data) { |
var suggestions = lthis.traiterRetourNomsSci(data); |
add(suggestions); |
}); |
} |
}, |
html: true |
}); |
|
$("#taxon").bind("autocompleteselect", this.surAutocompletionTaxon); |
}; |
|
WidgetSaisie.prototype.surAutocompletionTaxon = function(event, ui) { |
$("#taxon").data(ui.item); |
if (ui.item.retenu == true) { |
$("#taxon").addClass('ns-retenu'); |
} else { |
$("#taxon").removeClass('ns-retenu'); |
} |
}; |
|
WidgetSaisie.prototype.getUrlAutocompletionNomsSci = function() { |
var mots = $('#taxon').val(); |
var url = this.serviceAutocompletionNomSciUrlTpl.replace('{referentiel}', this.nomSciReferentiel); |
url = url.replace('{masque}', mots); |
return url; |
}; |
|
WidgetSaisie.prototype.traiterRetourNomsSci = function(data) { |
var suggestions = []; |
if (data.resultat != undefined) { |
$.each(data.resultat, function(i, val) { |
val.nn = i; |
var nom = {label : '', value : '', nt : '', nomSel : '', nomSelComplet : '', numNomSel : '', |
nomRet : '', numNomRet : '', famille : '', retenu : false |
}; |
if (suggestions.length >= this.autocompletionElementsNbre) { |
nom.label = "..."; |
nom.value = $('#taxon').val(); |
suggestions.push(nom); |
return false; |
} else { |
nom.label = val.nom_sci_complet; |
nom.value = val.nom_sci_complet; |
nom.nt = val.num_taxonomique; |
nom.nomSel = val.nom_sci; |
nom.nomSelComplet = val.nom_sci_complet; |
nom.numNomSel = val.nn; |
nom.nomRet = val.nom_retenu_complet; |
nom.numNomRet = val["nom_retenu.id"]; |
nom.famille = val.famille; |
// Tester dans ce sens, permet de considérer "absent" comme "false" => est-ce opportun ? |
// en tout cas c'est harmonisé avec le CeL |
nom.retenu = (val.retenu == 'true') ? true : false; |
|
suggestions.push(nom); |
} |
}); |
} |
|
return suggestions; |
}; |
|
WidgetSaisie.prototype.afficherPanneau = function(selecteur) { |
$(selecteur).fadeIn("slow").delay(this.dureeMessage).fadeOut("slow"); |
}; |
|