summaryrefslogtreecommitdiff
path: root/public/js/modif_page.js
blob: 8ff26c13fcaa873062fbfea22e7fb461d18422f8 (plain)
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
179
180
181
182
183
184
185
186
187
188
/* -- mode modification d'une page -- */

// même fonction que dans new_page.js
function makePageNamePath(){
	const page_name = document.getElementById("page_name");
	const page_name_path = document.getElementById("page_name_path");
	page_name_path.value = page_name.value.replace(/\W+/g, " ").trim().toLowerCase().split(' ').join('_');

	/* explication de l'expression régulière
    / = début et fin, \W+ = lettres et chiffres, g = global */
}


// partie "page"
function changePageTitle(page_id){
	const page_name = document.getElementById("page_name");

	fetch('index.php?page_edit=page_title', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({title: page_name.value, page_id: page_id})
    })
    .then(response => response.json())
    .then(data => {
        if(data.success){
        	document.querySelector("title").innerHTML = data.title; // metadonnées
			document.getElementById("m_" + page_id).innerHTML = data.title; // menu
            const thesee = document.getElementById("thesee"); // fil d'Ariane
            if(thesee != null){
                thesee.innerHTML = data.title;
            }
			console.log("la page a été renommée: " + data.title);
			toastNotify("la page a été renommée: " + data.title);
        }
        else{
            console.error('Erreur au renommage de la page.');
        }
    })
    .catch(error => {
        console.error('Erreur:', error);
    });
}
/*function changePageMenuPath(page_id){
	const page_name_path = document.getElementById("page_name_path");
	
	fetch('index.php?page_edit=page_menu_path', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({page_menu_path: page_name_path.value, page_id: page_id})
    })
    .then(response => response.json())
    .then(data => {
        if(data.success){
        	// oh putaing...
			let url = new URL(document.getElementById("m_" + page_id).parentElement.href); // url attrapée dans une balise <a>
			let params = new URLSearchParams(url.search); // params à droite du ?
			let path_array = params.get('page').split('/'); // chemin 'page' découpé dans un tableau
            console.log(data.page_name_path);
			path_array[path_array.length - 1] = data.page_name_path; // modif de la dernière case
			params.set('page', path_array.join('/')); // réassemblage du chemin et MAJ de params
			url.search = params.toString(); // MAJ de url
			document.getElementById("m_" + page_id).parentElement.href = url.toString(); // MAJ de la balise <a>

            //  modifier l'URL sans rafraichir en touchant à l'historique
            params.set('action', 'modif_page'); // on veut rester en mode "modif"
            url.search = params.toString();
            history.pushState({}, '', url.toString())
            
            console.log("la nouveau chemin est: " + data.page_name_path);
            toastNotify("la nouveau chemin est: " + data.page_name_path);
        }
        else{
            console.error("Erreur à la modification du chemin de la page dans l'URL.");
        }
    })
    .catch(error => {
        console.error('Erreur:', error);
    });
}*/
function changeDescription(node_data_id){
	const textarea = document.getElementById("description_textarea");

	fetch('index.php?page_edit=page_description', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({description: textarea.value, node_data_id: node_data_id})
    })
    .then(response => response.json())
    .then(data => {
        if(data.success){
        	document.querySelector('meta[name="description"]').setAttribute('content', data.description); // c'était vraiment nécéssaire?
			console.log("la nouvelle description de la page est: " + data.description);
			toastNotify("la nouvelle description de la page est: " + data.description);
        }
        else{
            console.error('Erreur à la modification de la description de la page.');
        }
    })
    .catch(error => {
        console.error('Erreur:', error);
    });
}


// partie "blocs"
function renamePageBloc(bloc_id){
	const input = document.getElementById("bloc_rename_" + bloc_id);
	const title = document.getElementById(bloc_id).querySelector("h3");

	fetch('index.php?bloc_edit=rename_page_bloc', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({bloc_title: input.value, bloc_id: bloc_id})
    })
    .then(response => response.json())
    .then(data => {
        if(data.success){
        	title.innerHTML = data.title;
        	console.log(data.title);
        	toastNotify('Le bloc a été renommé: ' + data.title);
        }
        else{
            console.error('Erreur au renommage du titre.');
        }
    })
    .catch(error => {
        console.error('Erreur:', error);
    });
}

function switchBlocsPositions(bloc_id, direction, current_page) {
	const current_bloc = document.getElementById(bloc_id);
	const current_bloc_edit_zone = document.getElementById("bloc_edit_" + bloc_id);
	var other_bloc;

	if(direction == 'down'){
		other_bloc = current_bloc.nextElementSibling;
	}
	else if(direction == 'up'){
		other_bloc = current_bloc.previousElementSibling;
	}

	if(other_bloc == null || other_bloc.tagName !== 'SECTION')
	{
		console.log('Inversion impossible');
		return;
	}
	const other_bloc_edit_zone = document.getElementById("bloc_edit_" + other_bloc.id);
	
    fetch('index.php?page=' + current_page + '&bloc_edit=switch_blocs_positions', {
        method: 'POST',
        headers: {
        'Content-Type': 'application/json'
        },
        body: JSON.stringify({ id1: bloc_id, id2: parseInt(other_bloc.id) })
    })
    .then(response => response.json())
    .then(data => {
        if(data.success)
        {
        	if(direction == 'down'){
				current_bloc.parentElement.insertBefore(other_bloc, current_bloc);
				current_bloc_edit_zone.parentElement.insertBefore(other_bloc_edit_zone, current_bloc_edit_zone);
				console.log('Inversion réussie');
			}
			else if(direction == 'up'){
				other_bloc.parentElement.insertBefore(current_bloc, other_bloc);
				other_bloc_edit_zone.parentElement.insertBefore(current_bloc_edit_zone, other_bloc_edit_zone);
				console.log('Inversion réussie');
			}
        }
        else {

            console.error('Échec de l\'inversion');
        }
    })
    .catch(error => {
        console.error('Erreur:', error);
    });
}