Projekt

Obecné

Profil

« Předchozí | Další » 

Revize edd0c51e

Přidáno uživatelem Pavel Fidranský před více než 6 roky(ů)

reworked all jQuery-backed AJAX calls to use AJAX JS class

Zobrazit rozdíly:

sources/src/main/webapp/js/app.js
293 293
	 * Loads graph data of a diagram.
294 294
	 * @param {string} diagramId Identifier of the diagram to be loaded.
295 295
	 */
296
	function loadGraphData(diagramId) {
297
		var self = this;
298

  
299
		self.loader.enable();
296
	async function loadGraphData(diagramId) {
297
		this.loader.enable();
300 298

  
301
		var loadGraphDataPromise;
299
		let loadGraphDataPromise;
302 300

  
303 301
		if (diagramId === '') {
304
			loadGraphDataPromise = $.getJSON(self.API.loadGraphData);
302
			loadGraphDataPromise = AJAX.getJSON(this.API.loadGraphData);
305 303

  
306 304
		} else {
307
			loadGraphDataPromise = $.getJSON(self.API.getDiagram + '?id=' + diagramId).then(function(data) {
308
				self.diagram = new Diagram(data);
305
			const diagramData = await AJAX.getJSON(this.API.getDiagram + '?id=' + diagramId);
309 306

  
310
				document.title = self.NAME + ' - ' + self.diagram.name;
307
			this.diagram = new Diagram(diagramData);
311 308

  
312
				// return graph data as a new promise
313
				var deferred = new $.Deferred();
314
				deferred.resolve(JSON.parse(data.graph_json));
309
			document.title = this.NAME + ' - ' + this.diagram.name;
315 310

  
316
				return deferred.promise();
317
			});
311
			loadGraphDataPromise = Promise.resolve(JSON.parse(diagramData.graph_json));
318 312
		}
319 313

  
320
		// get vertex position data
321
		loadGraphDataPromise.then(function(data) {
322
			// construct graph
323
			self.graphLoader.run(data);
324

  
325
			self.loader.disable();
314
		try {
315
			// get vertex position data
316
			const graphData = await loadGraphDataPromise;
326 317

  
327
		}, function(xhr) {
328
			switch (xhr.status) {
329
				case 401:
330
					alert('You are not allowed to view the diagram.');
331
					break;
332
				default:
333
					alert('Something went wrong.');
318
			// construct graph
319
			this.graphLoader.run(graphData);
320

  
321
			this.loader.disable();
322

  
323
		} catch (error) {
324
			if (error instanceof HttpError) {
325
				switch (error.response.status) {
326
					case 401:
327
						alert('You are not allowed to view the diagram.');
328
						break;
329
					default:
330
						alert('Something went wrong.');
331
				}
332
			} else {
333
				alert('Server has probably gone away.');
334 334
			}
335 335

  
336 336
			// go to the upload page
337 337
			window.location.replace('./');
338
		});
338
		}
339 339
	}
340 340

  
341 341
	/**
sources/src/main/webapp/js/components/modalWindow.js
69 69
	 * Saves diagram.
70 70
	 * @param {Event} e Submit event.
71 71
	 */
72
	function saveDiagram(e) {
72
	async function saveDiagram(e) {
73 73
		e.preventDefault();
74 74

  
75
		var self = this;
76

  
77
		$.ajax({
78
			'type': 'POST',
79
			'url': app.API.saveDiagram,
80
			'data': {
81
				'id': app.diagram === null ? '' : app.diagram.id,
82
				'name': e.target.diagramName.value,
83
				'graphJson': JSON.stringify(app.graphExporter.run()),
84
				'public': (e.target.diagramPublic.checked | 0).toString(),
85
			},
86
			'success': function(data) {
87
				app.diagram = new Diagram(data);
88

  
89
				document.title = app.NAME + ' - ' + app.diagram.name;
90
				history.replaceState({} , document.title, app.HOME_URL + 'graph?diagramId=' + app.diagram.id);
91

  
92
				self.close();
93
				alert('Diagram was successfully saved.');
94
			},
95
			'error': function(xhr) {
96
				switch (xhr.status) {
75
		const body = new URLSearchParams;
76
		body.set('id', app.diagram === null ? '' : app.diagram.id);
77
		body.set('name', e.target.diagramName.value);
78
		body.set('graphJson', JSON.stringify(app.graphExporter.run()));
79
		body.set('public', (e.target.diagramPublic.checked | 0).toString());
80

  
81
		try {
82
			const response = await AJAX.post(app.API.saveDiagram, body);
83
			const data = await response.json();
84

  
85
			app.diagram = new Diagram(data);
86

  
87
			document.title = app.NAME + ' - ' + app.diagram.name;
88
			history.replaceState({} , document.title, app.HOME_URL + 'graph?diagramId=' + app.diagram.id);
89

  
90
			this.close();
91
			alert('Diagram was successfully saved.');
92

  
93
		} catch (error) {
94
			if (error instanceof HttpError) {
95
				switch (error.response.status) {
97 96
					case 401:
98 97
						alert('You are either not logged in or not an owner of this diagram.');
99 98
						break;
100 99
					default:
101 100
						alert('Something went wrong.');
102 101
				}
103
			},
104
		});
102
			} else {
103
				alert('Server has probably gone away.');
104
			}
105
		}
105 106
	}
106 107
}
sources/src/main/webapp/js/uploadFiles.js
1
document.addEventListener('DOMContentLoaded', function() {
2
	var privateDiagramList = document.getElementById('privateDiagramList');
3

  
4
	privateDiagramList.querySelectorAll('.remove-diagram-button').forEach(function(button) {
5
		button.addEventListener('click', removeDiagram);
6
	});
7

  
8
	document.addEventListener('imiger.userLoggedIn', function() {
9
		loadPrivateDiagrams();
10
	});
11

  
12
	document.addEventListener('imiger.userLoggedOut', function() {
13
		privateDiagramList.innerHTML = '';
14
	});
15

  
16
	async function loadPrivateDiagrams() {
17
		try {
18
			const data = await AJAX.getJSON(Constants.API.getPrivateDiagrams);
19

  
20
			data.forEach(function(diagram) {
21
				var openDiagramLink = document.createElement('a');
22
				openDiagramLink.setAttribute('href', './graph?diagramId=' + diagram.id);
23
				openDiagramLink.innerText = diagram.name;
24

  
25
				var removeDiagramIcon = document.createElement('img');
26
				removeDiagramIcon.setAttribute('src', 'images/button_cancel.png');
27
				removeDiagramIcon.setAttribute('alt', 'odstranit');
28

  
29
				var removeDiagramButton = document.createElement('button');
30
				removeDiagramButton.setAttribute('class', 'remove-diagram-button');
31
				removeDiagramButton.setAttribute('data-id', diagram.id);
32
				removeDiagramButton.setAttribute('data-name', diagram.name);
33
				removeDiagramButton.addEventListener('click', removeDiagram);
34
				removeDiagramButton.appendChild(removeDiagramIcon);
35

  
36
				var diagramListItem = document.createElement('li');
37
				diagramListItem.appendChild(openDiagramLink);
38
				diagramListItem.appendChild(removeDiagramButton);
39

  
40
				privateDiagramList.appendChild(diagramListItem);
41
			});
42
		} catch (error) {
43
			if (error instanceof HttpError) {
44
				alert('Something went wrong.');
45
			} else {
46
				alert('Server has probably gone away.');
47
			}
48
		}
49
	}
50

  
51
	async function removeDiagram() {
52
		let diagramId = this.getAttribute('data-id');
53
		let diagramName = this.getAttribute('data-name');
54

  
55
		if (confirm('Do you really want to delete ' + diagramName + '?')) {
56
			try {
57
				await AJAX.delete(Constants.API.removeDiagram + '?diagramId=' + diagramId);
58

  
59
				location.reload(true);
60

  
61
			} catch (error) {
62
				if (error instanceof HttpError) {
63
					switch (error.response.status) {
64
						case 401:
65
							alert('You are either not logged in or not an owner of this diagram.');
66
							break;
67
						default:
68
							alert('Something went wrong.');
69
					}
70
				} else {
71
					alert('Server has probably gone away.');
72
				}
73
			}
74
		}
75
	}
76
});
sources/src/main/webapp/js/userMenu.js
24 24
		registerForm.name.focus();
25 25
	});
26 26

  
27
	logoutButton.addEventListener('click', function(e) {
27
	logoutButton.addEventListener('click', async function(e) {
28 28
		e.preventDefault();
29 29

  
30
		$.ajax({
31
			'type': 'GET',
32
			'url': logoutButton.href,
33
			'success': function() {
34
				document.dispatchEvent(new CustomEvent('imiger.userLoggedOut'));
30
		try {
31
			await AJAX.get(logoutButton.href);
35 32

  
36
				usernameLabel.innerText = '';
33
			document.dispatchEvent(new CustomEvent('imiger.userLoggedOut'));
37 34

  
38
				document.body.classList.remove('loggedIn');
39
				document.body.classList.add('loggedOut');
40
			},
41
			'error': function() {
35
			usernameLabel.innerText = '';
36

  
37
			document.body.classList.remove('loggedIn');
38
			document.body.classList.add('loggedOut');
39

  
40
		} catch (error) {
41
			if (error instanceof HttpError) {
42 42
				alert('Something went wrong.');
43
			},
44
		});
43
			} else {
44
				alert('Server has probably gone away.');
45
			}
46
		}
45 47
	});
46 48

  
47
	loginForm.addEventListener('submit', function(e) {
49
	loginForm.addEventListener('submit', async function(e) {
48 50
		e.preventDefault();
49 51

  
50
		$.ajax({
51
			'type': loginForm.method,
52
			'url': loginForm.action,
53
			'data': {
54
				'username': loginForm.username.value,
55
				'password': loginForm.password.value,
56
			},
57
			'success': function(data) {
58
				document.dispatchEvent(new CustomEvent('imiger.userLoggedIn'));
59

  
60
				usernameLabel.innerText = data.user.username;
61

  
62
				document.body.classList.remove('loggedOut');
63
				document.body.classList.add('loggedIn');
64

  
65
				loginPopup.classList.add('hidden');
66
			},
67
			'error': function(xhr) {
68
				switch (xhr.status) {
52
		const body = new URLSearchParams;
53
		body.set('username', loginForm.username.value);
54
		body.set('password', loginForm.password.value);
55

  
56
		try {
57
			const response = await AJAX.do(loginForm.action, {
58
				method: loginForm.method,
59
				credentials: 'same-origin',
60
				body,
61
			});
62
			const data = await response.json();
63

  
64
			document.dispatchEvent(new CustomEvent('imiger.userLoggedIn'));
65

  
66
			usernameLabel.innerText = data.user.username;
67

  
68
			document.body.classList.remove('loggedOut');
69
			document.body.classList.add('loggedIn');
70

  
71
			loginPopup.classList.add('hidden');
72

  
73
		} catch (error) {
74
			if (error instanceof HttpError) {
75
				switch (error.response.status) {
69 76
					case 400:
70
						printErrors(xhr);
77
						printErrors(error.response);
71 78
						break;
72 79
					case 401:
73 80
						alert('Invalid credentials.');
......
75 82
					default:
76 83
						alert('Something went wrong.');
77 84
				}
78
			},
79
		});
85
			} else {
86
				alert('Server has probably gone away.');
87
			}
88
		}
80 89
	});
81 90

  
82
	registerForm.addEventListener('submit', function(e) {
91
	registerForm.addEventListener('submit', async function(e) {
83 92
		e.preventDefault();
84 93

  
85
		$.ajax({
86
			'type': registerForm.method,
87
			'url': registerForm.action,
88
			'data': {
89
				'name': registerForm.name.value,
90
				'email': registerForm.email.value,
91
				'username': registerForm.username.value,
92
				'password': registerForm.password.value,
93
				'passwordCheck': registerForm.passwordCheck.value,
94
			},
95
			'success': function() {
96
				document.dispatchEvent(new CustomEvent('imiger.userRegistered'));
97

  
98
				registerPopup.classList.add('hidden');
99
				alert('You were successfully registered.');
100
			},
101
			'error': function(xhr) {
102
				switch (xhr.status) {
94
		const body = new URLSearchParams;
95
		body.set('name', registerForm.name.value);
96
		body.set('email', registerForm.email.value);
97
		body.set('username', registerForm.username.value);
98
		body.set('password', registerForm.password.value);
99
		body.set('passwordCheck', registerForm.passwordCheck.value);
100

  
101
		try {
102
			await AJAX.do(registerForm.action, {
103
				method: registerForm.method,
104
				credentials: 'same-origin',
105
				body,
106
			});
107

  
108
			document.dispatchEvent(new CustomEvent('imiger.userRegistered'));
109

  
110
			registerPopup.classList.add('hidden');
111
			alert('You were successfully registered.');
112

  
113
		} catch (error) {
114
			if (error instanceof HttpError) {
115
				switch (error.response.status) {
103 116
					case 400:
104
						printErrors(xhr);
117
						printErrors(error.response);
105 118
						break;
106 119
					default:
107 120
						alert('Something went wrong.');
108 121
				}
109
			},
110
		});
122
			} else {
123
				alert('Server has probably gone away.');
124
			}
125
		}
111 126
	});
112 127
});
113 128

  
114
function printErrors(xhr) {
115
	var data = JSON.parse(xhr.responseText);
116

  
117
	for (var key in data.error) {
118
		if (!data.error.hasOwnProperty(key)) continue;
129
async function printErrors(response) {
130
	const data = await response.json();
131
	for (let key in data.error) {
132
		if (data.error.hasOwnProperty(key) === false) continue;
119 133

  
120 134
		alert(data.error[key]);
121 135
	}
sources/src/main/webapp/uploadFiles.jsp
13 13
		<link rel="stylesheet" href="css/main.css">
14 14

  
15 15
		<script src="js/libs/jquery-3.3.1.min.js"></script>
16
		<script src="js/errors/httpError.js"></script>
17
		<script src="js/utils/ajax.js"></script>
18
		<script src="js/constants.js"></script>
19
		<script src="js/uploadFiles.js"></script>
16 20
		<script src="js/userMenu.js"></script>
17 21

  
18 22
		<title>IMiGEr</title>
......
58 62
					<c:forEach items="${diagramsPrivate}" var="diagram">
59 63
						<li>
60 64
							<a href="${HOME_URL}graph?diagramId=${diagram.id}">${diagram.name}</a>
61
							<button class="removeDiagramButton" data-name="${diagram.name}" data-id="${diagram.id}"><img src="images/button_cancel.png" alt="odstranit"></button>
65
							<button class="remove-diagram-button" data-id="${diagram.id}" data-name="${diagram.name}"><img src="images/button_cancel.png" alt="odstranit"></button>
62 66
						</li>
63 67
					</c:forEach>
64 68
				</ul>
......
76 80
				</ul>
77 81
			</div>
78 82
		</main>
79

  
80
		<script>
81
			var privateDiagramList = document.getElementById('privateDiagramList');
82

  
83
			$(privateDiagramList).on('click', '.removeDiagramButton', function(e) {
84
				if (confirm('Do you really want to delete ' + $(this).data('name') + '?')) {
85
					$.ajax({
86
						'type': 'delete',
87
						'url': 'api/remove-diagram?diagramId=' + $(this).data('id'),
88
						'success': function () {
89
							location.reload(true);
90
						},
91
						'error': function (xhr) {
92
							switch (xhr.status) {
93
								case 401:
94
									alert('You are either not logged in or not an owner of this diagram.');
95
									break;
96
								default:
97
									alert('Something went wrong.');
98
							}
99
						},
100
					});
101
				}
102
			});
103

  
104
			document.addEventListener('imiger.userLoggedIn', function() {
105
				$.getJSON('api/get-private-diagrams').then(function(data) {
106
					data.forEach(function(diagram) {
107
						var openDiagramLink = document.createElement('a');
108
						openDiagramLink.setAttribute('href', './graph?diagramId=' + diagram.id);
109
						openDiagramLink.innerText = diagram.name;
110

  
111
						var removeDiagramIcon = document.createElement('img');
112
						removeDiagramIcon.setAttribute('src', 'images/button_cancel.png');
113
						removeDiagramIcon.setAttribute('alt', 'odstranit');
114

  
115
						var removeDiagramButton = document.createElement('button');
116
						removeDiagramButton.setAttribute('class', 'removeDiagramButton');
117
						removeDiagramButton.setAttribute('data-id', diagram.id);
118
						removeDiagramButton.setAttribute('data-name', diagram.name);
119
						removeDiagramButton.appendChild(removeDiagramIcon);
120

  
121
						var diagramListItem = document.createElement('li');
122
						diagramListItem.appendChild(openDiagramLink);
123
						diagramListItem.appendChild(removeDiagramButton);
124

  
125
						privateDiagramList.appendChild(diagramListItem);
126
					});
127
				});
128
			});
129

  
130
			document.addEventListener('imiger.userLoggedOut', function() {
131
				privateDiagramList.innerHTML = '';
132
			});
133
		</script>
134 83
	</body>
135 84
</html>

Také k dispozici: Unified diff