feat: on deplace dans un fichier séparé des fonctions helpers
This commit is contained in:
parent
4608e25fdf
commit
a28dcfa118
2 changed files with 109 additions and 86 deletions
|
@ -344,6 +344,13 @@
|
|||
<script setup>
|
||||
import { watch, ref, onMounted, computed, nextTick, inject } from "vue";
|
||||
import VueSelect from "vue-next-select";
|
||||
import {
|
||||
orderBy,
|
||||
ordonnerSelect,
|
||||
navigate,
|
||||
recupJson,
|
||||
trouver_index,
|
||||
} from "./helpers";
|
||||
const $papa = inject("$papa");
|
||||
const props = defineProps({
|
||||
tparpage: {
|
||||
|
@ -452,6 +459,7 @@ let searchInputHead = ref("");
|
|||
let searchInputVal = ref("");
|
||||
let loadingVueSelect = ref(true);
|
||||
let ajaxCrayons = ref(false);
|
||||
let maj = ref(0);
|
||||
|
||||
onMounted(() => {
|
||||
localforage.setDriver(localforage[props.stockage.toUpperCase()]);
|
||||
|
@ -616,12 +624,17 @@ watch(filtreColSelected, () => {
|
|||
}
|
||||
});
|
||||
|
||||
//~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
// les methodes
|
||||
//~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
function chargerJson(id) {
|
||||
console.log("chargerJson pour ", id);
|
||||
chargement.value = true;
|
||||
let url = props.apiuri;
|
||||
if (parseInt(id) > 0) {
|
||||
url += "&id=" + id;
|
||||
} else if (id === "maj") {
|
||||
url += "&maj=" + maj.value;
|
||||
} else {
|
||||
let config = localStorage.getItem("header_" + nameLocalStorage.value);
|
||||
config = recupJson(config);
|
||||
|
@ -630,6 +643,9 @@ function chargerJson(id) {
|
|||
if (config.crayons !== undefined) {
|
||||
crayons.value = config.crayons;
|
||||
}
|
||||
if (config.maj !== undefined) {
|
||||
maj.value = config.maj?.lastMAJ;
|
||||
}
|
||||
if (config.classes !== undefined) {
|
||||
classes.value = config.classes;
|
||||
}
|
||||
|
@ -685,6 +701,9 @@ function chargerJson(id) {
|
|||
.then((data) => {
|
||||
let config = data.shift();
|
||||
header.value = config.header;
|
||||
if (config.maj !== undefined) {
|
||||
maj.value = config.maj?.lastMAJ;
|
||||
}
|
||||
if (config.crayons !== undefined) {
|
||||
crayons.value = config.crayons;
|
||||
} 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") {
|
||||
let $csv = [];
|
||||
let $header = [];
|
||||
|
@ -947,18 +958,6 @@ function exportCSV(quoi = "tableau") {
|
|||
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) {
|
||||
let html = vuebloc.value;
|
||||
Object.keys(ligne).forEach((key) => {
|
||||
|
@ -969,25 +968,6 @@ function replaceBloc(ligne) {
|
|||
function changerVue(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) {
|
||||
let tableau = tableau.value;
|
||||
if (Tcheckbox.value[head] !== undefined && Tcheckbox.value[head].length) {
|
||||
|
@ -1040,56 +1020,19 @@ function setPages() {
|
|||
}
|
||||
}
|
||||
|
||||
function recupJson(d) {
|
||||
try {
|
||||
return JSON.parse(d);
|
||||
} catch (e) {
|
||||
console.log("erreur recupJson ", e);
|
||||
return false;
|
||||
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);
|
||||
}
|
||||
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)
|
||||
);
|
||||
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) {
|
||||
console.log("rechargerJson");
|
||||
ajaxCrayons = ajax_Crayons;
|
||||
|
|
80
src/components/helpers.js
Normal file
80
src/components/helpers.js
Normal 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 };
|
Loading…
Add table
Reference in a new issue