@osfarm/itineraire-technique 1.0.7 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/editor.html CHANGED
@@ -20,12 +20,13 @@
20
20
  <script src="./js/editor-interventions.js"></script>
21
21
  <script src="./js/editor-crops.js"></script>
22
22
  <script src="./js/editor-export.js"></script>
23
+ <script src="./js/editor-wiki-editor.js"></script>
23
24
 
24
25
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
25
26
  integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
26
27
  <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
27
28
  <link href="./css/styles-editor.css" rel="stylesheet">
28
- <link href="./css/styles-rendering.css" rel="stylesheet">
29
+ <link href="./css/styles-rendering.css" rel="stylesheet">
29
30
  </head>
30
31
 
31
32
  <body>
@@ -35,7 +36,7 @@
35
36
  <button type="button" onclick="importFromJsonFile()" class="btn btn-outline-primary primary-button"
36
37
  id="importFromJsonButton"><i class="fa fa-upload" aria-hidden="true"></i> Charger (JSON)</button>
37
38
  <button type="button" onclick="importFromTestJson()" class="btn btn-outline-primary primary-button"
38
- id="importFromJsonButton"><i class="fa fa-upload" aria-hidden="true"></i> Charger un
39
+ id="importFromExampleJsonButton"><i class="fa fa-upload" aria-hidden="true"></i> Charger un
39
40
  exemple</button>
40
41
  <button type="button" onclick="exportToJsonFile(crops)"
41
42
  class="btn btn-outline-primary primary-button" id="exportToJsonButton"><i
@@ -379,6 +380,8 @@
379
380
 
380
381
  refreshAllTables();
381
382
  });
383
+
384
+ let we = new WikiEditor();
382
385
  }
383
386
 
384
387
  class StepModel {
@@ -29,12 +29,20 @@ class RotationRenderer {
29
29
 
30
30
  this.itk_container = $("#" + divID).css({ 'width': '100%' });
31
31
 
32
- if (this.itk_container.find('.mainITKContainer').length == 0) {
33
- this.itk_container.append(`<div class="row mainITKContainer">
34
- <div class="col-auto left-transcript"><div class="transcript"></div></div>
35
- <div class="col col-auto chart-div"><div class="charts"></div></div>
36
- <div class="col col-12 bottom-transcript"><div class="transcript"></div></div>
37
- </div>`);
32
+ if (this.data.options.show_transcript) {
33
+ if (this.itk_container.find('.mainITKContainer').length == 0) {
34
+ this.itk_container.append(`<div class="row mainITKContainer">
35
+ <div class="col-auto left-transcript"><div class="transcript"></div></div>
36
+ <div class="col col-auto chart-div"><div class="charts"></div></div>
37
+ <div class="col col-12 bottom-transcript"><div class="transcript"></div></div>
38
+ </div>`);
39
+ }
40
+ } else {
41
+ if (this.itk_container.find('.mainITKContainer').length == 0) {
42
+ this.itk_container.append(`<div class="row mainITKContainer">
43
+ <div class="col col-12 chart-div"><div class="charts"></div></div>
44
+ </div>`);
45
+ }
38
46
  }
39
47
  }
40
48
 
@@ -0,0 +1,103 @@
1
+ // Encapsulate the wiki editor functionality
2
+ class WikiEditor {
3
+ constructor() {
4
+ // Initialize any properties if needed
5
+
6
+ const self = this;
7
+
8
+ // When the page loads, get the URL paremeter with the target page title we want to edit:
9
+ window.onload = function () {
10
+ const urlParams = new URLSearchParams(window.location.search);
11
+ const pageTitle = urlParams.get('wiki');
12
+
13
+ if (!pageTitle)
14
+ return; // No page title provided, we are not in wiki edit mode
15
+
16
+ // If a page title is provided, load its content
17
+ fetch(`/api.php?action=parse&page=${encodeURIComponent(pageTitle)}&format=json&prop=wikitext`, {
18
+ credentials: 'same-origin'
19
+ })
20
+ .then(response => {
21
+ let res = response.json();
22
+ return res;
23
+ })
24
+ .then(data => {
25
+ if (data.parse && data.parse.wikitext) {
26
+ try {
27
+ const content = JSON.parse(data.parse.wikitext['*']);
28
+ reloadCropsFromJson(content);
29
+ } catch (e) {
30
+ console.error("Erreur lors de l'analyse du JSON de la page :", e);
31
+ $('#jsonErrorMessage').text("Le contenu de la page n'est pas un JSON valide.");
32
+ const jsonErrorModal = new bootstrap.Modal(document.getElementById('jsonErrorModal'));
33
+ jsonErrorModal.show();
34
+ }
35
+ } else {
36
+
37
+ if (data.error.code == "missingtitle") {
38
+ // The page doesn't exist yet, we can start with a blank rotation
39
+ wipe();
40
+ } else {
41
+ console.error("Erreur lors du chargement de la page :", data);
42
+ $('#jsonErrorMessage').text("Impossible de charger le contenu de la page.");
43
+ const jsonErrorModal = new bootstrap.Modal(document.getElementById('jsonErrorModal'));
44
+ jsonErrorModal.show();
45
+ }
46
+
47
+ }
48
+ });
49
+
50
+ // Add a button to save the page back the wiki
51
+ const saveButton = $(`<button type="button" class="btn btn-outline-primary primary-button" id="exportToJsonButton">
52
+ <i class="fa fa-download" aria-hidden="true"></i> Enregistrer dans le wiki
53
+ </button>`).on('click', function () {
54
+ if (pageTitle) {
55
+ // If a page title is provided, save to that page
56
+ self.savePageToWiki(pageTitle, JSON.stringify(crops, null, 2))
57
+ .then(() => {
58
+ alert("Itinéraire technique enregistré avec succès !");
59
+ })
60
+ .catch(err => {
61
+ console.error("Erreur lors de l'enregistrement de la page :", err);
62
+ alert("Une erreur s'est produite lors de l'enregistrement.");
63
+ });
64
+ } else {
65
+ // Otherwise, export to JSON file
66
+ exportToJsonFile(crops);
67
+ }
68
+ });
69
+
70
+ $(".file-icons").first().prepend(saveButton);
71
+
72
+ $('#importFromExampleJsonButton').remove();
73
+ $('#importFromJsonButton').remove();
74
+ };
75
+ }
76
+
77
+ async savePageToWiki(pageTitle, newContent) {
78
+ // 1. Obtenir un token
79
+ const tokenResp = await fetch('/api.php?action=query&meta=tokens&type=csrf&format=json', {
80
+ credentials: 'same-origin' // important pour envoyer les cookies de session
81
+ });
82
+ const tokenData = await tokenResp.json();
83
+ const token = tokenData.query.tokens.csrftoken;
84
+
85
+ // 2. Faire l’édition
86
+ const params = new URLSearchParams();
87
+ params.append('action', 'edit');
88
+ params.append('format', 'json');
89
+ params.append('title', pageTitle);
90
+ params.append('text', newContent);
91
+ params.append('token', token);
92
+ params.append('contentmodel', 'json');
93
+
94
+ const editResp = await fetch('/api.php', {
95
+ method: 'POST',
96
+ body: params,
97
+ credentials: 'same-origin'
98
+ });
99
+
100
+ const editData = await editResp.json();
101
+ console.log(editData);
102
+ }
103
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osfarm/itineraire-technique",
3
- "version": "1.0.7",
3
+ "version": "1.1.0",
4
4
  "description": "A visualisation tool to show agricultural technical itineraries based on Echarts",
5
5
  "main": "editor.html",
6
6
  "scripts": {