FunctionProject/scripts/main.js

253 lines
9.7 KiB
JavaScript
Raw Normal View History

2019-08-16 12:05:56 +02:00
$(function () {
/* ÉXECUTION DES FONCTONS */
2019-10-24 18:57:39 +02:00
// Touche entrer génère un clique sur les classes .btn
2019-10-23 12:21:21 +02:00
$("body").keydown(function(e){
if(e.which === 13){
$(".btn").click();
}
});
$("#submitWeatherRequest").click(() => {
2019-10-18 09:55:35 +02:00
const city = $('#cityName').val();
const cityName = city.split(' ').join('+');
2019-10-23 09:33:36 +02:00
if(isEmptyValue(cityName)) {
$('.results').html(emptyMessageError);
}
2019-10-23 09:33:36 +02:00
else {
createSessionCookie("city", cityName);
weatherRequest();
}
});
2019-10-23 09:33:36 +02:00
$("#submitRandomNumber").click(() => {
2019-10-18 09:55:35 +02:00
const minEntered = $('#minValue').val();
const maxEntered = $('#maxValue').val();
2019-10-23 09:33:36 +02:00
if(isEmptyValue(minEntered) || isEmptyValue(maxEntered)) {
2019-08-18 20:06:06 +02:00
$('.results').html(emptyMessageError);
2019-08-16 12:05:56 +02:00
}
2019-10-23 09:33:36 +02:00
else {
2019-10-18 09:55:35 +02:00
const result = randomNumber(minEntered, maxEntered);
2019-10-04 20:10:38 +02:00
if(result === messageError) {
2019-09-17 19:17:30 +02:00
$('.results').html(messageError);
2019-10-04 20:10:38 +02:00
} else {
$('.results').html("Nombre aléatoire compris entre " + minEntered + " inclus et " + maxEntered + " inclus : " + formatNumberResult(result));
}
2019-08-16 12:05:56 +02:00
}
});
2019-10-23 09:33:36 +02:00
$("#birthDateValue").bind("keyup change", () =>
2019-08-16 12:05:56 +02:00
{
2019-10-24 18:57:39 +02:00
$('.results').html(calculateAge($('#birthDateValue').val()));
2019-08-16 12:05:56 +02:00
});
2019-10-23 09:33:36 +02:00
$("#submitConvertTemperature").click(() =>
2019-08-16 12:05:56 +02:00
{
2019-10-18 09:55:35 +02:00
const temperatureValue = $('#temperatureValue').val();
const degree = parseFloat(temperatureValue.slice(0, temperatureValue.length - 2));
const unit = temperatureValue.slice(temperatureValue.length - 2);
2019-10-18 20:35:03 +02:00
if(isEmptyValue(temperatureValue)) {
2019-08-18 20:06:06 +02:00
$('.results').html(emptyMessageError);
2019-08-16 12:05:56 +02:00
}
2019-10-18 20:35:03 +02:00
else {
$('.results').html(convertTemperature(degree, unit));
2019-08-16 12:05:56 +02:00
}
});
2019-10-23 09:33:36 +02:00
$("#submitConvertDistance").click(() =>
2019-08-16 12:05:56 +02:00
{
2019-08-29 23:35:40 +02:00
let firstValue = $('#firstValue').val();
2019-10-18 09:55:35 +02:00
const unitFirstValue = $("#firstValueUnit option:selected").text();
const secondValue = $("#secondValue option:selected").text();
2019-10-18 20:35:03 +02:00
if(isEmptyValue(firstValue) || isEmptyValue(secondValue)) {
2019-08-18 20:06:06 +02:00
$('.results').html(emptyMessageError);
2019-08-16 12:05:56 +02:00
}
2019-10-18 20:35:03 +02:00
else {
2019-09-15 09:45:20 +02:00
firstValue = parseFloat(firstValue.replace(/\s/g,''));
2019-10-18 20:35:03 +02:00
$('.results').html(convertDistance(firstValue, unitFirstValue, secondValue));
2019-08-16 12:05:56 +02:00
}
});
2019-10-23 09:33:36 +02:00
$("#submitFilterStudents").click(() =>
2019-08-29 14:53:15 +02:00
{
2019-10-18 09:55:35 +02:00
const nameEntered = $('#nameEntered').val();
2019-08-29 14:53:15 +02:00
let filteredLetter = $("#filteredLetter").val();
2019-10-18 20:35:03 +02:00
if(isEmptyValue(nameEntered) || isEmptyValue(filteredLetter)) {
2019-08-29 14:53:15 +02:00
$('.results').html(emptyMessageError);
}
2019-10-18 20:35:03 +02:00
else if(filteredLetter.length === 1) {
2019-10-18 09:55:35 +02:00
const students = nameEntered.split(', ');
2019-08-29 14:53:15 +02:00
filteredLetter = capitalize(filteredLetter);
2019-10-18 20:35:03 +02:00
$('.results').html(filterStudents(filteredLetter, students));
2019-08-29 14:53:15 +02:00
}
else {
$('.results').html(messageError);
}
});
2019-09-13 14:39:29 +02:00
let randomQuoteClicked;
2019-10-23 09:33:36 +02:00
$("#submitRandomQuote").click(() => {
2019-09-13 14:39:29 +02:00
randomQuoteClicked = true;
$('.resultsRandomQuote').html(getRandomQuote());
});
// Affichage d'une citation au chargement de la page
2019-10-23 09:33:36 +02:00
if (randomQuoteClicked != true && window.location.href.includes("randomQuote")) {
2019-09-13 14:39:29 +02:00
$('.resultsRandomQuote').html(getRandomQuote());
}
/* Permet d'afficher la liste des citations/proverbes */
if(chemin === "/views/quote-list.php") {
window.onload = $('.totalLengthQuote').html('Total de ' + quotes.length + ' citations.');
2019-10-04 23:53:26 +02:00
let resultat = "";
for (index in quotes) {
2019-10-04 23:53:26 +02:00
resultat = resultat + `<tr> <td class="quote-element-list important">${quotes[index]["source"]}</td> <td class="quote-element-list">${quotes[index]["quote"]}</td> </tr>`;
}
2019-10-24 18:57:39 +02:00
$(".quote-list").append(resultat);
}
2019-10-23 09:33:36 +02:00
$("#submitConvertCurrency").click(() => {
2019-09-16 20:39:30 +02:00
let value = $('#value').val();
2019-10-18 09:55:35 +02:00
const currencyOfTheValue = $("#currencyOfTheValue option:selected").val();
const currencyAfter = $("#currencyAfter option:selected").val();
2019-10-18 20:35:03 +02:00
if(isEmptyValue(value) || isNaN(parseFloat(value))) {
2019-09-13 21:56:36 +02:00
$('.results').html(emptyMessageError);
}
2019-10-18 20:35:03 +02:00
else {
const url = 'https://api.exchangeratesapi.io/latest?base=' + currencyOfTheValue;
2019-09-16 20:39:30 +02:00
value = parseFloat(value);
convertCurrency(value, currencyAfter, url);
2019-09-13 21:56:36 +02:00
}
});
2019-10-23 09:33:36 +02:00
$("#submitConvertEncoding").click(() => {
2019-10-18 09:55:35 +02:00
const value = $('#value').val();
const option = $("#option option:selected").val();
2019-10-18 20:35:03 +02:00
if(isEmptyValue(value)) {
$('.results').html(messageError);
2019-09-14 17:38:30 +02:00
}
2019-10-23 09:33:36 +02:00
else {
2019-10-24 18:57:39 +02:00
// Objet qui recense toutes les fonctions de convertEncoding
const convertEncoding = { decimalToBinary, binaryToDecimal, decimalToHexadecimal, hexadecimalToDecimal, binaryToHexadecimal, hexadecimalToBinary, textToNumberUnicode, numberUnicodeToText, textToBinary, binaryToText, textToHexadecimal, hexadecimalToText };
try {
function executionFunction(option, value) {
if (convertEncoding[option]) {
return convertEncoding[option](value)
} else {
console.log(convertEncoding[option]);
return messageError;
}
}
$('.results').html(executionFunction(option, value));
} catch (error) {
$('.results').html(messageError);
console.log(error);
}
2019-09-14 17:38:30 +02:00
}
});
2019-10-23 09:33:36 +02:00
$("#submitConvertRomanArabicNumbers").click(() => {
let numbersValue = $('#numbersArabic').val();
2019-10-18 20:35:03 +02:00
numbersValue = numbersValue.replace(/\s/g,'');
2019-10-18 09:55:35 +02:00
const convertNumberType = $("#convertNumberType option:selected").text();
if(isEmptyValue(numbersValue)) {
$('.results').html(emptyMessageError);
}
2019-10-18 20:35:03 +02:00
else if (!isNaN(Number(numbersValue)) && convertNumberType === "Nombre Romain") {
const result = convertArabicToRoman(parseInt(numbersValue));
let numbersValueFormat = formatNumberResult(numbersValue);
if (result === messageError || isFloat(numbersValue)) {
$('.results').html(messageError);
} else {
$('.results').html(`<b>${numbersValueFormat}</b> s'écrit <b>${result}</b> en chiffres romains.`);
}
}
else if (convertNumberType === "Nombre Arabe") {
if (!isNaN(Number(numbersValue))) {
$('.results').html(`<b>${numbersValue}</b> est déjà en chiffres arabes.`);
} else {
2019-09-22 10:48:03 +02:00
numbersValue = numbersValue.toUpperCase();
2019-10-18 09:55:35 +02:00
const result = convertRomanToArabic(numbersValue);
if (result === 0) {
$('.results').html(messageError);
} else {
2019-10-08 11:18:34 +02:00
$('.results').html(`<b>${numbersValue}</b> s'écrit <b>${formatNumberResult(result)}</b> en chiffres arabes.`);
}
}
}
else {
$('.results').html(messageError);
}
});
2019-10-23 09:33:36 +02:00
$("#numberToTest").bind("keyup change", () => {
2019-09-21 22:40:16 +02:00
let numbersValue = $('#numberToTest').val();
2019-10-13 20:58:13 +02:00
numbersValue = parseInt(numbersValue.replace(/\s/g,''));
2019-10-18 20:35:03 +02:00
if (!isNaN(numbersValue) && numbersValue >= 0) {
$('.results').html(armstrongNumber(numbersValue));
2019-09-21 22:40:16 +02:00
}
else {
$('.results').html(messageError);
}
});
2019-10-23 09:33:36 +02:00
$("#submitHeapAlgorithm").click(() => {
2019-10-18 09:55:35 +02:00
const value = $('#value').val();
2019-10-18 20:35:03 +02:00
if(isEmptyValue(value)) {
2019-10-11 21:30:05 +02:00
$('.results').html(emptyMessageError);
}
2019-10-18 20:35:03 +02:00
else {
2019-10-18 09:55:35 +02:00
const stringPermutationsResult = stringPermutations(value);
2019-10-11 21:30:05 +02:00
let result = "";
for (element in stringPermutationsResult) {
result = result + stringPermutationsResult[element] + "<br>";
}
$('.results').html(`Il y a ${formatNumberResult(stringPermutationsResult.length)} possibilités d'anagramme pour le mot "${value}" qui contient ${value.length} caractères, la liste : <br><br> ${result}`);
}
});
2019-10-24 18:57:39 +02:00
/* Changement du texte accueil (exemples de fonction) */
if(chemin === "/" || chemin === '/index.php') {
let index=-1;
function change() {
if(index === texteFonctionChange.length-1) {
index = 0;
}
else {
index++;
}
document.getElementById("change").innerHTML = texteFonctionChange[index];
}
setInterval(change,10000);
}
2019-10-11 21:30:05 +02:00
2019-08-18 20:06:06 +02:00
/* Permet d'afficher l'heure en temps réel sur le footer */
window.onload = realDateTime('realDateTime');
2019-08-23 20:23:57 +02:00
/* Window Scroll Top Button */
2019-10-18 09:55:35 +02:00
const $btnScrollTop = $('.scroll-top-arrow');
2019-08-23 20:23:57 +02:00
$btnScrollTop.on('click', function () {
$('html, body').animate({scrollTop: 0}, 800);
return false;
});
2019-08-29 22:33:07 +02:00
/* Date Picker */
$.fn.datepicker.dates['fr'] = {
2019-10-18 09:55:35 +02:00
days: ["dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"],
daysShort: ["dim.", "lun.", "mar.", "mer.", "jeu.", "ven.", "sam."],
daysMin: ["d", "l", "ma", "me", "j", "v", "s"],
months: ["janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"],
monthsShort: ["janv.", "févr.", "mars", "avril", "mai", "juin", "juil.", "août", "sept.", "oct.", "nov.", "déc."],
today: "Aujourd'hui",
monthsTitle: "Mois",
clear: "Effacer",
weekStart: 1,
format: "dd/mm/yyyy"
2019-08-29 22:33:07 +02:00
};
$('.datepicker').datepicker({
2019-10-18 09:55:35 +02:00
language: 'fr',
2019-10-18 20:35:03 +02:00
autoclose: false,
2019-10-18 09:55:35 +02:00
todayHighlight: true
2019-10-24 18:57:39 +02:00
});
2019-10-24 18:57:39 +02:00
});