feat: on deplace dans un fichier séparé des fonctions helpers

This commit is contained in:
Christophe 2023-01-12 11:26:19 +01:00
parent 4608e25fdf
commit a28dcfa118
2 changed files with 109 additions and 86 deletions

View file

@ -344,6 +344,13 @@
<script setup> <script setup>
import { watch, ref, onMounted, computed, nextTick, inject } from "vue"; import { watch, ref, onMounted, computed, nextTick, inject } from "vue";
import VueSelect from "vue-next-select"; import VueSelect from "vue-next-select";
import {
orderBy,
ordonnerSelect,
navigate,
recupJson,
trouver_index,
} from "./helpers";
const $papa = inject("$papa"); const $papa = inject("$papa");
const props = defineProps({ const props = defineProps({
tparpage: { tparpage: {
@ -452,6 +459,7 @@ let searchInputHead = ref("");
let searchInputVal = ref(""); let searchInputVal = ref("");
let loadingVueSelect = ref(true); let loadingVueSelect = ref(true);
let ajaxCrayons = ref(false); let ajaxCrayons = ref(false);
let maj = ref(0);
onMounted(() => { onMounted(() => {
localforage.setDriver(localforage[props.stockage.toUpperCase()]); localforage.setDriver(localforage[props.stockage.toUpperCase()]);
@ -616,12 +624,17 @@ watch(filtreColSelected, () => {
} }
}); });
//~~~~~~~~~~~~~~~~~~~~~~~~~
// les methodes
//~~~~~~~~~~~~~~~~~~~~~~~~~
function chargerJson(id) { function chargerJson(id) {
console.log("chargerJson pour ", id); console.log("chargerJson pour ", id);
chargement.value = true; chargement.value = true;
let url = props.apiuri; let url = props.apiuri;
if (parseInt(id) > 0) { if (parseInt(id) > 0) {
url += "&id=" + id; url += "&id=" + id;
} else if (id === "maj") {
url += "&maj=" + maj.value;
} else { } else {
let config = localStorage.getItem("header_" + nameLocalStorage.value); let config = localStorage.getItem("header_" + nameLocalStorage.value);
config = recupJson(config); config = recupJson(config);
@ -630,6 +643,9 @@ function chargerJson(id) {
if (config.crayons !== undefined) { if (config.crayons !== undefined) {
crayons.value = config.crayons; crayons.value = config.crayons;
} }
if (config.maj !== undefined) {
maj.value = config.maj?.lastMAJ;
}
if (config.classes !== undefined) { if (config.classes !== undefined) {
classes.value = config.classes; classes.value = config.classes;
} }
@ -685,6 +701,9 @@ function chargerJson(id) {
.then((data) => { .then((data) => {
let config = data.shift(); let config = data.shift();
header.value = config.header; header.value = config.header;
if (config.maj !== undefined) {
maj.value = config.maj?.lastMAJ;
}
if (config.crayons !== undefined) { if (config.crayons !== undefined) {
crayons.value = config.crayons; crayons.value = config.crayons;
} else { } else {
@ -925,14 +944,6 @@ function genererPDF(quoi = "tableau") {
} }
}); });
} }
function navigate(href, newTab) {
let a = document.createElement("a");
a.href = href;
if (newTab) {
a.setAttribute("target", "_blank");
}
a.click();
}
function exportCSV(quoi = "tableau") { function exportCSV(quoi = "tableau") {
let $csv = []; let $csv = [];
let $header = []; let $header = [];
@ -947,18 +958,6 @@ function exportCSV(quoi = "tableau") {
exporterCSV($csv, props.delimitercsv, props.namecsv); exporterCSV($csv, props.delimitercsv, props.namecsv);
} }
function exporterCSV(json, delimitercsv, name) {
if (name.includes(".csv")) {
name = name.split(".")[0];
}
let csv = "";
if (delimitercsv) {
csv = $papa.unparse(json, { delimiter: delimitercsv });
} else {
csv = $papa.unparse(json);
}
$papa.download(csv, name);
}
function replaceBloc(ligne) { function replaceBloc(ligne) {
let html = vuebloc.value; let html = vuebloc.value;
Object.keys(ligne).forEach((key) => { Object.keys(ligne).forEach((key) => {
@ -969,25 +968,6 @@ function replaceBloc(ligne) {
function changerVue(vue) { function changerVue(vue) {
quelleVue.value = vue; quelleVue.value = vue;
} }
function ordonnerSelect(a, b) {
// attention ! le test d'égalité en == et non pas === est voulu tel quel...
if (
(Number.isInteger(a) || parseInt(a) == a) &&
(Number.isInteger(b) || parseInt(b) == b)
) {
return parseInt(a) - parseInt(b);
} else {
let x = toString(a).toLowerCase();
let y = toString(b).toLowerCase();
if (x < y) {
return -1;
}
if (x > y) {
return 1;
}
return 0;
}
}
function validerCheckboxCol(head) { function validerCheckboxCol(head) {
let tableau = tableau.value; let tableau = tableau.value;
if (Tcheckbox.value[head] !== undefined && Tcheckbox.value[head].length) { if (Tcheckbox.value[head] !== undefined && Tcheckbox.value[head].length) {
@ -1040,56 +1020,19 @@ function setPages() {
} }
} }
function recupJson(d) { function exporterCSV(json, delimitercsv, name) {
try { if (name.includes(".csv")) {
return JSON.parse(d); name = name.split(".")[0];
} catch (e) {
console.log("erreur recupJson ", e);
return false;
} }
let csv = "";
if (delimitercsv) {
csv = $papa.unparse(json, { delimiter: delimitercsv });
} else {
csv = $papa.unparse(json);
} }
const orderBy = (arr, props, orders, champ) => $papa.download(csv, name);
arr.sort((a, b) =>
props.reduce((acc, prop, i) => {
if (acc === 0) {
let [p1, p2] =
orders && orders[i] === "desc"
? [b[champ][prop], a[champ][prop]]
: [a[champ][prop], b[champ][prop]];
// passe en lowercase les String
p1 = typeof p1 === "string" ? p1.toLowerCase() : p1;
p2 = typeof p2 === "string" ? p2.toLowerCase() : p2;
// Gestion du format de date
// transforme 03/11/2000 en 20001103
let re = /^(\d{2})\/(\d{2})\/(\d{2,4})$/;
if (typeof p1 !== "number") {
let r1 = p1.match(re);
if (Array.isArray(r1)) {
p1 = r1[3] + r1[2] + r1[1];
} }
let r2 = p2.match(re);
if (Array.isArray(r2)) {
p2 = r2[3] + r2[2] + r2[1];
}
}
acc = p1 > p2 ? 1 : p1 < p2 ? -1 : 0;
}
return acc;
}, 0)
);
function trouver_index(table, id) {
let i = -1;
table.forEach((ligne, index) => {
if (ligne.html.id === parseInt(id)) {
i = index;
}
});
return i;
}
function rechargerJson(id, ajax_Crayons = true) { function rechargerJson(id, ajax_Crayons = true) {
console.log("rechargerJson"); console.log("rechargerJson");
ajaxCrayons = ajax_Crayons; ajaxCrayons = ajax_Crayons;

80
src/components/helpers.js Normal file
View file

@ -0,0 +1,80 @@
function trouver_index(table, id) {
let i = -1;
table.forEach((ligne, index) => {
if (ligne.html.id === parseInt(id)) {
i = index;
}
});
return i;
}
function recupJson(d) {
try {
return JSON.parse(d);
} catch (e) {
console.log('erreur recupJson ', e);
return false;
}
}
function navigate(href, newTab) {
let a = document.createElement('a');
a.href = href;
if (newTab) {
a.setAttribute('target', '_blank');
}
a.click();
}
function ordonnerSelect(a, b) {
// attention ! le test d'égalité en == et non pas === est voulu tel quel...
if ((Number.isInteger(a) || parseInt(a) == a) && (Number.isInteger(b) || parseInt(b) == b)) {
return parseInt(a) - parseInt(b);
} else {
let x = toString(a).toLowerCase();
let y = toString(b).toLowerCase();
if (x < y) {
return -1;
}
if (x > y) {
return 1;
}
return 0;
}
}
const orderBy = (arr, props, orders, champ) =>
arr.sort((a, b) =>
props.reduce((acc, prop, i) => {
if (acc === 0) {
let [p1, p2] =
orders && orders[i] === 'desc'
? [b[champ][prop], a[champ][prop]]
: [a[champ][prop], b[champ][prop]];
// passe en lowercase les String
p1 = typeof p1 === 'string' ? p1.toLowerCase() : p1;
p2 = typeof p2 === 'string' ? p2.toLowerCase() : p2;
// Gestion du format de date
// transforme 03/11/2000 en 20001103
let re = /^(\d{2})\/(\d{2})\/(\d{2,4})$/;
if (typeof p1 !== 'number') {
let r1 = p1.match(re);
if (Array.isArray(r1)) {
p1 = r1[3] + r1[2] + r1[1];
}
let r2 = p2.match(re);
if (Array.isArray(r2)) {
p2 = r2[3] + r2[2] + r2[1];
}
}
acc = p1 > p2 ? 1 : p1 < p2 ? -1 : 0;
}
return acc;
}, 0)
);
export { orderBy, ordonnerSelect, navigate, recupJson, trouver_index };