Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#218 - Template - Composant libre #333

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/doc_user/param_data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ Il est maintenant nécessaire de peupler le bloc de gauche avec des composants e
* ``Chiffre clé`` : composant permettant d’afficher un chiffre clé à mettre en avant et nécessitant une valeur de type « nombre » en entrée.
* ``Liste`` : composant permettant d’afficher une liste et nécessitant un champ composé d’une liste comme indiqué dans la `documentation mviewer <https://mviewerdoc.readthedocs.io/fr/latest/doc_tech/config_tpl.html#iterer-sur-un-champ-de-type-json>`_.
* ``Texte`` : composant permettant d’afficher un texte et nécessitant une valeur de type texte en entrée.
* ``Libre`` : composant permettant de saisir librement le contenu du template comme dans un fichier Mustache (.mst).

Sélectionnez un composant et cliquez sur "Enregistrer" pour l’ajouter. Il n’est possible d’ajouter qu’un composant à la fois, veuillez réitérer l’opération pour ajouter des composants supplémentaires.

Expand Down
2 changes: 1 addition & 1 deletion js/mviewerstudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ var editLayer = function (item, themeid, layerid) {
mv.setCurrentLayerId(layerid);
var element = $(item).parent().parent();
var layerid = element.attr("data-layerid");

element.addClass("active");
if (layerid != "undefined") {
$("#mod-layerOptions").modal("show");
mv.showLayerOptions(element, themeid, layerid);
Expand Down
15 changes: 13 additions & 2 deletions lib/mv.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ var mv = (function () {
layerid = el.attr("data-layerid");
}
var themeid = mv.getCurrentThemeId();
if (!themeid) {
// search theme id from config object
themeid = Object.keys(config.themes)
.filter((f) => {
const them = config.themes[f];
return them.layers.filter((l) => l.id === layerid)?.length;
})
.filter((x) => x)[0];
}
return config.themes[themeid].layers.find((l) => l.id === layerid);
},

Expand Down Expand Up @@ -627,7 +636,7 @@ var mv = (function () {
$("#frm-layerid").val(layer.id);
$("#frm-url").val(layer.url);
$("#frm-queryable").prop("checked", layer.queryable);
$("#frm-featurecount").val(layer.featurecount);
$("#frm-featurecount").val(layer.featurecount || 5);
$("#frm-infopanel option[value='" + layer.infopanel + "']").prop("selected", true);
$("#frm-secure").val(layer.secure);
$("#frm-useproxy").prop("checked", layer.useproxy);
Expand Down Expand Up @@ -803,7 +812,9 @@ var mv = (function () {
layer.fusesearchkeys = $("#frm-fusesearchkeys").val();
layer.fusesearchresult = $("#frm-fusesearchresult").val();
layer.infoformat = $("#frm-infoformat").val();
layer.featurecount = $("#frm-featurecount").val();
layer.featurecount = document
.querySelector("#frm-featurecount")
.getAttribute("value");
layer.infopanel = $("#frm-infopanel").val();
layer.metadata = $("#frm-metadata").val();
layer["metadata-csw"] = $("#frm-metadata-csw").val();
Expand Down
2 changes: 2 additions & 0 deletions lib/template-generator/ComponentsSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { header as imageHeader } from "./components/library/ImageComponent.js";
import { header as buttonHeader } from "./components/library/ButtonComponent.js";
import { header as numberHeader } from "./components/library/NumbersComponent.js";
import { header as listHeader } from "./components/library/ListComponent.js";
import { header as freeHeader } from "./components/library/FreeComponent.js";

const headers = [
h1Header(),
Expand All @@ -16,6 +17,7 @@ const headers = [
numberHeader(),
listHeader(),
textHeader(),
freeHeader(),
];

/**
Expand Down
2 changes: 2 additions & 0 deletions lib/template-generator/TemplateGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ButtonComponent from "./components/library/ButtonComponent.js";
import ListComponent from "./components/library/ListComponent.js";
import NumbersComponent from "./components/library/NumbersComponent.js";
import IframeComponent from "./components/library/IframeComponent.js";
import FreeComponent from "./components/library/FreeComponent.js";
import { RightPanelLocation } from "./components/content/RightPanelLocation.js";
import { BottomPanelLocation } from "./components/content/BottomPanelLocation.js";
import { BottomPanelHtmlPreview } from "./components/content/BottomPanelLocation.js";
Expand All @@ -24,6 +25,7 @@ const components = [
new NumbersComponent(),
new IframeComponent(),
new ImageComponent(),
new FreeComponent(),
];

const maxItemsByCol = 3;
Expand Down
32 changes: 32 additions & 0 deletions lib/template-generator/components/forms/TextArea.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class TextArea {
uuid = mv.uuidv4();
value = "";
label = "Saisir un contenu";
constructor(value = "", label) {
this.value = value.value;
this.label = label;
}
render = () => {
return `
<div class="row g-3 align-items-center">
<label class="col-auto" for="head">${mviewer.tr(this.label)}</label>
<div class="col-12">
<textarea
class="form-control form-control-sm component-form"
style="height:100px"
id="${this.uuid}"
name="">${this.value ? decodeURI(this.value).trim() : ""}</textarea>
</div>
</div>
`;
};
set = ({ label, value }) => {
this.value = value || this.value;
this.label = label || this.label;
};
getValue = () => {
return document.getElementById(this.uuid).value;
};
}

export default TextArea;
49 changes: 49 additions & 0 deletions lib/template-generator/components/library/FreeComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import TemplateComponent from "./TemplateComponent.js";
import TextArea from "../forms/TextArea.js";

export const header = () => ({
icon: "bi bi-body-text",
id: mv.uuidv4(),
title: "template.free.title",
click: () => FreeComponent,
class: "FreeComponent",
});

const defaultTypeFields = [];

const attributes = ["data-value"];

export default class FreeComponent extends TemplateComponent {
constructor(customHeader, defaultAttrValues) {
let defaultValues = new Map();
if (defaultAttrValues) {
attributes.forEach((a) => {
defaultValues.set(a, defaultAttrValues[a]);
});
}
const textArea = new TextArea(defaultValues.get("data-value"), "template.free.label");
super(customHeader || header(), defaultTypeFields, [textArea], defaultValues);
this.textArea = textArea;
}

getAttributes = () => attributes;

render = () => {
let values = {
value: this.textArea.getValue(),
};
return this.template(values);
};

template = ({ value }) => {
return `
<div class="${header().class} template-component mb-2"
data-type-selector=""
data-value="${encodeURI(value)}"
data-component="${header().class}"
>
${value}
</div>
`;
};
}
33 changes: 21 additions & 12 deletions lib/template-generator/components/library/TemplateComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default class TemplateComponent {
customFields = [];
defaultType = "";
defaultValues = null;
typeFieldSelector = "";
constructor(
header = { icon: "", title: "" },
defaultTypeFields = ["field", "multi", "static"],
Expand All @@ -23,10 +24,12 @@ export default class TemplateComponent {
this.defaultValues = defaultValues;
this.defaultType = defaultValues.get("data-type-selector");
}
this.typeFieldSelector = new TypeFieldSelector({
fields: defaultTypeFields,
defaultType: this.defaultType,
});
if (defaultTypeFields && defaultTypeFields.length) {
this.typeFieldSelector = new TypeFieldSelector({
fields: defaultTypeFields,
defaultType: this.defaultType,
});
}
this.customFields = customFields;
}

Expand Down Expand Up @@ -138,7 +141,7 @@ export default class TemplateComponent {
return `
<div class="card cardComponent titleComponent" id=${this.uuid}>
${this.cardHeader()}
${this.typeFieldSelector.render()}
${this.typeFieldSelector ? this.typeFieldSelector.render() : ""}
<div class="subcomponents" id="${mv.uuidv4()}"></div>
${this.tooltipContent()}
</div>`;
Expand All @@ -149,14 +152,20 @@ export default class TemplateComponent {
this.componentsArea = document
.getElementById(this.uuid)
.querySelector(".subcomponents");
const typeFieldSelectorFromDOM = document.getElementById(this.typeFieldSelector.uuid);
typeFieldSelectorFromDOM.addEventListener("change", (el) => {
this.getForms(el.target.value);
if (this.typeFieldSelector) {
const typeFieldSelectorFromDOM = document.getElementById(
this.typeFieldSelector.uuid
);
typeFieldSelectorFromDOM.addEventListener("change", (el) => {
this.getForms(el.target.value);
this.getCustomFields();
_elementTranslate("body");
});
if (this.defaultType) {
typeFieldSelectorFromDOM.dispatchEvent(new Event("change"));
}
} else {
this.getCustomFields();
_elementTranslate("body");
});
if (this.defaultType) {
typeFieldSelectorFromDOM.dispatchEvent(new Event("change"));
}
_elementTranslate("body");
};
Expand Down
2 changes: 2 additions & 0 deletions mviewerstudio.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@
"template.typefield.static": "Saisie libre",
"template.typefield.liField": "A partir d'un champ JSON",
"template.typefield.select": "Choisir un type...",
"template.free.title": "Saisie libre",
"template.free.label": "Contenu du template :",
"version.comment": "Ajouter une description à cette version",
"version.comment.ph": "Exemple : Version de test sans données",
"publish.message": "Version publiée",
Expand Down