gamutable/src/gamutable.js
tofulm fe653417c0 feat: test saturation localstorage
on test en amont du chargement des gamutables si le localstorage n'est
pas saturé, pour ca, on essaye d'ecrire une string de 100 * "gamutable"
Si c'est Ok, on supprime cette entrée Sinon, on vidange tout le
localstorage
2025-02-11 18:41:27 +01:00

132 lines
3.4 KiB
JavaScript

// https://vitejs.dev/config/#build-polyfillmodulepreload
import 'vite/modulepreload-polyfill';
// Vue
import { createApp } from 'vue';
// if importing all is too much you can always do it manually
import GamuTable from './components/gamuTable.vue';
import VuePapaParse from 'vue-papa-parse';
const components = {
GamuTable,
};
//
// test si le localstorage n'est pas saturé, sinon, on le vidange entierement
//
try {
const repeat = function (str, x) { return new Array(x + 1).join(str); };
const too_big = repeat("gamutable ", 100); // each JS character is 2 bytes
localStorage.setItem("test_gamutable", too_big);
localStorage.removeItem("test_gamutable")
} catch (e) {
console.log(e.code);
if (e.code === 22 || e.code === 1024) {
console.error('On fait le ménage de tout le localstorage');
localStorage.clear();
}
}
gamutable();
function gamutable() {
let i = 1;
for (const el of document.getElementsByClassName('vue-gamutable')) {
if (i === 1) {
window.app = createApp({
components,
mounted() {
app.rechargerJson = this.$refs.montableau.rechargerJson;
app.delLigne = this.$refs.montableau.delLigne;
},
});
app.use(VuePapaParse);
app.provide('$papa', app.config.globalProperties.$papa);
app.mount(el);
} else if (i === 2) {
window.app2 = createApp({
components,
mounted() {
app2.rechargerJson = this.$refs.montableau_deux.rechargerJson;
app2.delLigne = this.$refs.montableau_deux.delLigne;
},
});
app2.use(VuePapaParse);
app2.provide('$papa', app2.config.globalProperties.$papa);
app2.mount(el);
}
i++;
}
}
// le chargement de gamutable passe dans une variable globale
// permettant de forcer son rechargement à l'ouverture du modalbox
// via la classe : modalgamutable
window.gamutable = gamutable
$('#vueGamutable').on('click', '.url_action, .url_action--ss_css', function (e) {
e.preventDefault();
e.stopPropagation();
let confirmation = $(this).data('confirm');
if (confirmation !== undefined) {
if (!confirm(confirmation)) {
return;
}
}
let url = $(this).attr('href');
let id = $(this).data('id');
// gamutable(s) à mettre à jour ? 1 | 2 | 12 (par défaut 1)
let Treload = '1';
if ($(this).parents('.gamutableDeux').length) {
Treload = '2';
}
if ($(this).data("treload") !== undefined) {
Treload = $(this).data("treload").toString();
}
let Treload1 = Treload.indexOf('1') !== -1;
let Treload2 = Treload.indexOf('2') !== -1;
// passer en refresh animé une éventuelle icone
if ($(this).has('i.icon, i.fa')) {
$(this).find('i.icon, i.fa').eq(0).replaceWith('<i class="fa fa-refresh fa-spin"></i>');
}
let nomBlocAjaxReload = $(this).data('ajaxreload');
$.ajax({
url: url,
dataType: 'json',
async: true,
}).done(function (retour) {
if (!$.isEmptyObject(retour) && !$.isEmptyObject(retour.message_erreur)) {
alert(retour.message_erreur);
} else {
if (parseInt(id) > 0) {
if (Treload1) {
app.rechargerJson(id);
}
if (Treload2) {
app2.rechargerJson(id);
}
} else if (parseInt(id) < 0) {
if (Treload1) {
app.delLigne(id);
}
if (Treload2) {
app2.delLigne(id);
}
} else {
if (Treload1) {
app.rechargerJson('maj 1');
}
if (Treload2) {
app2.rechargerJson('maj 2');
}
}
if (nomBlocAjaxReload !== undefined) {
ajaxReload(nomBlocAjaxReload, {
args: { id },
callback: function () { },
});
}
}
});
});