1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
function newPassword(page, id = ''){
if(id != ''){
id = '&id=' + id;
}
alert('Le mot de passe a été modifié.');
window.setTimeout(function(){
location.href = "index.php?page=" + page + "&message=nouveau_mdp" + id;
}, 0);
}
function copyInClipBoard(link){
// une balise <input> avec des attributs
var element = document.createElement("input");
element.setAttribute("id", "copyMe");
element.setAttribute("value", link);
// placement dans la page (= le "document")
document.body.appendChild(element);
var cible = document.getElementById('copyMe');
// selection comme on le ferait à la souris
cible.select();
// copie (= Ctrl + C)
document.execCommand("copy");
// nettoyage
element.parentNode.removeChild(element);
alert('Cette adresse a été copiée dans le presse-papier:\n\n' + link);
}
// complète les fonctions dans tinymce.js
function switchPositions(article_id, direction)
{
const current_article = findParent(document.getElementById(article_id), 'article');
var other_article;
if(direction == 'down'){
other_article = current_article.nextElementSibling;
}
else if(direction == 'up'){
other_article = current_article.previousElementSibling;
}
var other_article_id;
try{
other_article_id = other_article.querySelector('div[id]').id;
other_article_id = 'i' + other_article_id.slice(1); // peut mieux faire
}
catch(error){
console.log('Inversion impossible');
return;
}
fetch('index.php?action=switch_positions', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ id1: article_id, id2: other_article_id })
})
.then(response => response.json())
.then(data => {
if(data.success)
{
if(direction == 'down'){
current_article.parentElement.insertBefore(other_article, current_article);
console.log('Inversion réussie');
}
else if(direction == 'up'){
other_article.parentElement.insertBefore(current_article, other_article);
console.log('Inversion réussie');
}
else{
console.error('Échec de l\'inversion');
}
}
else {
console.error('Échec de l\'inversion');
}
})
.catch(error => {
console.error('Erreur:', error);
});
}
function changeDate(id_date)
{
const real_id = 'i' + id_date.slice(1);
const date_span = document.getElementById(id_date); // = <span>
var old_date = date_span.innerHTML;
// changer "le 28-12-2024 à 23h14" en "2024-12-28T23:14"
let values = old_date.split(" à "); // 2 parties: date et heure
values[1] = values[1].replace('h', ':');
values[0] = values[0].replace("le ", "");
let date = values[0].split("-"); // tableau jj-mm-aaaa
old_date = date[2] + '-' + date[1] + "-" + date[0] + "T" + values[1];
var label = document.createElement('label');
label.textContent = 'Choisir une date: ';
label.id = 'label-' + id_date;
var input = document.createElement('input');
input.type = 'datetime-local';
input.value = old_date;
input.id = 'input-' + id_date;
var parent = date_span.parentElement;
parent.appendChild(label)
parent.appendChild(input);
date_span.classList.add('hidden');
document.querySelector(`#edit-${id_date}`).classList.add('hidden');
document.querySelector(`#cancel-${id_date}`).classList.remove('hidden');
document.querySelector(`#submit-${id_date}`).classList.remove('hidden');
}
function closeInput(id)
{
const date_span = document.getElementById(id);
const date_input = document.getElementById('input-' + id);
const date_label = document.getElementById('label-' + id);
date_span.classList.remove('hidden');
date_input.remove();
date_label.remove();
document.querySelector(`#edit-${id}`).classList.remove('hidden');
document.querySelector(`#cancel-${id}`).classList.add('hidden');
document.querySelector(`#submit-${id}`).classList.add('hidden');
}
function submitDate(id_date)
{
const date_input = document.getElementById('input-' + id_date);
fetch('index.php?action=date_submit', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({id: id_date, date: date_input.value})
})
.then(response => response.json())
.then(data => {
if (data.success) {
// modifier la date dans le <span> caché
const date_span = document.getElementById(id_date);
let date = new Date(date_input.value);
date_span.innerHTML =
'le ' + String(date.getDate()).padStart(2, '0') + '-' +
String(date.getMonth() + 1).padStart(2, '0') + '-' +
String(date.getFullYear()).padStart(4, '0') + ' à ' +
String(date.getHours()).padStart(2, '0') + 'h' +
String(date.getMinutes()).padStart(2, '0');
closeInput(id_date);
}
else {
console.error('Erreur lors de la sauvegarde de la date.');
}
})
.catch(error => {
console.error('Erreur:', error);
});
}
function findParent(element, tag_name) {
while (element !== null) {
if (element.tagName === tag_name.toUpperCase()) // tagName est en majuscules
{
return element;
}
element = element.parentElement;
}
return null;
}
|