Projekt

Obecné

Profil

« Předchozí | Další » 

Revize f8464192

Přidáno uživatelem Pavel Fidranský před asi 6 roky(ů)

reworked LoginPopup and RegisterPopup to extend newly created Popup JS class

Zobrazit rozdíly:

sources/src/main/webapp/css/main.css
17 17
}
18 18

  
19 19
form input[type="text"],
20
form input[type="email"],
20 21
form input[type="password"] {
21 22
	width: 150px;
22 23
}
sources/src/main/webapp/js/components/loginPopup.js
1
class LoginPopup extends Popup {
2
	/**
3
	 * @inheritdoc
4
	 */
5
	render() {
6
		super.render();
7

  
8
		this._rootElement.classList.add('login_popup');
9

  
10
		this._usernameInput = DOM.h('input', {
11
			type: 'text',
12
			name: 'username',
13
			id: 'loginUsername',
14
		});
15

  
16
		this._rootElement.appendChild(DOM.h('form', {
17
			action: Constants.API.logIn,
18
			method: 'post',
19
			onSubmit: this._onFormSubmit.bind(this),
20
		}, [
21
			DOM.h('table', {}, [
22
				DOM.h('tr', {}, [
23
					DOM.h('td', {}, [
24
						DOM.h('label', {
25
							for: 'loginUsername',
26
							innerText: 'Login name:',
27
						}),
28
					]),
29
					DOM.h('td', {}, [
30
						this._usernameInput,
31
					]),
32
				]),
33
				DOM.h('tr', {}, [
34
					DOM.h('td', {}, [
35
						DOM.h('label', {
36
							for: 'loginPassword',
37
							innerText: 'Password:',
38
						}),
39
					]),
40
					DOM.h('td', {}, [
41
						DOM.h('input', {
42
							type: 'password',
43
							name: 'password',
44
							id: 'loginPassword',
45
						}),
46
					]),
47
				]),
48
				DOM.h('tr', {}, [
49
					DOM.h('td'),
50
					DOM.h('td', {}, [
51
						DOM.h('button', {
52
							type: 'submit',
53
							class: 'button',
54
							innerText: 'Log in',
55
						})
56
					]),
57
				]),
58
			]),
59
		]));
60

  
61
		return this._rootElement;
62
	}
63

  
64
	/**
65
	 * Opens the popup and sets focus on username field.
66
	 */
67
	open() {
68
		super.open();
69

  
70
		this._usernameInput.focus();
71
	}
72

  
73
	/**
74
	 * Logs the user in.
75
	 * @param {Event} e Form submit event.
76
	 */
77
	async _onFormSubmit(e) {
78
		e.preventDefault();
79

  
80
		const body = new URLSearchParams;
81
		body.set('username', e.target.username.value);
82
		body.set('password', e.target.password.value);
83

  
84
		try {
85
			const response = await AJAX.do(e.target.action, {
86
				method: e.target.method,
87
				credentials: 'same-origin',
88
				body,
89
			});
90
			const data = await response.json();
91

  
92
			document.dispatchEvent(new CustomEvent('imiger.userLoggedIn', {
93
				detail: data,
94
			}));
95

  
96
			document.body.classList.remove('loggedOut');
97
			document.body.classList.add('loggedIn');
98

  
99
			this.close();
100

  
101
		} catch (error) {
102
			if (error instanceof HttpError) {
103
				switch (error.response.status) {
104
					case 400:
105
						this._printErrors(error.response);
106
						break;
107
					case 401:
108
						alert('Invalid credentials.');
109
						break;
110
					default:
111
						alert('Something went wrong.');
112
				}
113
			} else {
114
				alert('Server has probably gone away.');
115
			}
116
		}
117
	}
118

  
119
	async _printErrors(response) {
120
		const data = await response.json();
121
		for (let key in data.error) {
122
			if (data.error.hasOwnProperty(key) === false) continue;
123

  
124
			alert(data.error[key]);
125
		}
126
	}
127
}
sources/src/main/webapp/js/components/registerPopup.js
1
class RegisterPopup extends Popup {
2
	/**
3
	 * @inheritdoc
4
	 */
5
	render() {
6
		super.render();
7

  
8
		this._rootElement.classList.add('register_popup');
9

  
10
		this._nameInput = DOM.h('input', {
11
			'type': 'text',
12
			'name': 'name',
13
			'id': 'registerName',
14
		});
15

  
16
		this._rootElement.appendChild(DOM.h('form', {
17
			'action': Constants.API.register,
18
			'method': 'post',
19
			'onSubmit': this._onFormSubmit.bind(this),
20
		}, [
21
			DOM.h('table', {}, [
22
				DOM.h('tr', {}, [
23
					DOM.h('td', {}, [
24
						DOM.h('label', {
25
							for: 'registerName',
26
							innerText: 'Name:',
27
						}),
28
					]),
29
					DOM.h('td', {}, [
30
						this._nameInput,
31
					]),
32
				]),
33
				DOM.h('tr', {}, [
34
					DOM.h('td', {}, [
35
						DOM.h('label', {
36
							for: 'registerEmail',
37
							innerText: 'Email:',
38
						}),
39
					]),
40
					DOM.h('td', {}, [
41
						DOM.h('input', {
42
							type: 'email',
43
							name: 'email',
44
							id: 'registerEmail',
45
						}),
46
					]),
47
				]),
48
				DOM.h('tr', {}, [
49
					DOM.h('td', {}, [
50
						DOM.h('label', {
51
							for: 'registerUsername',
52
							innerText: 'Username:',
53
						}),
54
					]),
55
					DOM.h('td', {}, [
56
						DOM.h('input', {
57
							type: 'text',
58
							name: 'username',
59
							id: 'registerUsername',
60
						}),
61
					]),
62
				]),
63
				DOM.h('tr', {}, [
64
					DOM.h('td', {}, [
65
						DOM.h('label', {
66
							for: 'registerPassword',
67
							innerText: 'Password:',
68
						}),
69
					]),
70
					DOM.h('td', {}, [
71
						DOM.h('input', {
72
							type: 'password',
73
							name: 'password',
74
							id: 'registerPassword',
75
						}),
76
					]),
77
				]),
78
				DOM.h('tr', {}, [
79
					DOM.h('td', {}, [
80
						DOM.h('label', {
81
							for: 'registerPasswordCheck',
82
							innerText: 'Password again:',
83
						}),
84
					]),
85
					DOM.h('td', {}, [
86
						DOM.h('input', {
87
							type: 'password',
88
							name: 'passwordCheck',
89
							id: 'registerPasswordCheck',
90
						}),
91
					]),
92
				]),
93
				DOM.h('tr', {}, [
94
					DOM.h('td'),
95
					DOM.h('td', {}, [
96
						DOM.h('button', {
97
							type: 'submit',
98
							class: 'button',
99
							innerText: 'Register',
100
						})
101
					]),
102
				]),
103
			]),
104
		]));
105

  
106
		return this._rootElement;
107
	}
108

  
109
	/**
110
	 * Opens the popup and sets focus on name field.
111
	 */
112
	open() {
113
		super.open();
114

  
115
		this._nameInput.focus();
116
	}
117

  
118
	/**
119
	 * Signs the user up.
120
	 * @param {Event} e Form submit event.
121
	 */
122
	async _onFormSubmit(e) {
123
		e.preventDefault();
124

  
125
		const body = new URLSearchParams;
126
		body.set('name', e.target.name.value);
127
		body.set('email', e.target.email.value);
128
		body.set('username', e.target.username.value);
129
		body.set('password', e.target.password.value);
130
		body.set('passwordCheck', e.target.passwordCheck.value);
131

  
132
		try {
133
			await AJAX.do(e.target.action, {
134
				method: e.target.method,
135
				credentials: 'same-origin',
136
				body,
137
			});
138

  
139
			document.dispatchEvent(new CustomEvent('imiger.userRegistered'));
140

  
141
			this.close();
142
			alert('You were successfully registered.');
143

  
144
		} catch (error) {
145
			if (error instanceof HttpError) {
146
				switch (error.response.status) {
147
					case 400:
148
						this._printErrors(error.response);
149
						break;
150
					default:
151
						alert('Something went wrong.');
152
				}
153
			} else {
154
				alert('Server has probably gone away.');
155
			}
156
		}
157
	}
158

  
159
	async _printErrors(response) {
160
		const data = await response.json();
161
		for (let key in data.error) {
162
			if (data.error.hasOwnProperty(key) === false) continue;
163

  
164
			alert(data.error[key]);
165
		}
166
	}
167

  
168
}
sources/src/main/webapp/js/constants.js
10 10
/** @static @prop {object} API Map of application programming interface paths. */
11 11
Constants.API = {
12 12
	logIn: 'api/log-in',
13
	logOut: 'api/log-out',
13 14
	register: 'api/register',
14 15
	loadGraphData: 'api/load-graph-data',
15 16
	getDiagram: 'api/get-diagram',
sources/src/main/webapp/js/showGraphApp.js
139 139
		self.modalWindowComponent = new ModalWindow;
140 140
		content.appendChild(self.modalWindowComponent.render());
141 141

  
142
		// auth events
143
		const usernameLabel = document.getElementById('usernameLabel');
144

  
145
		document.addEventListener('imiger.userLoggedIn', e => {
146
			usernameLabel.innerText = e.detail.user.username;
147
		});
148
		document.addEventListener('imiger.userLoggedOut', () => {
149
			usernameLabel.innerText = '';
150
		});
151

  
142 152
		// context menu
143 153
		document.body.addEventListener('mousedown', function() {
144 154
			self.closeFloatingComponents();
sources/src/main/webapp/js/uploadFilesApp.js
14 14
			button.addEventListener('click', this._removeDiagram);
15 15
		});
16 16

  
17
		document.addEventListener('imiger.userLoggedIn', () => this._loadPrivateDiagrams());
18
		document.addEventListener('imiger.userLoggedOut', () => privateDiagramList.innerHTML = '');
17
		// auth events
18
		const usernameLabel = document.getElementById('usernameLabel');
19

  
20
		document.addEventListener('imiger.userLoggedIn', e => {
21
			this._loadPrivateDiagrams();
22
			usernameLabel.innerText = e.detail.user.username;
23
		});
24
		document.addEventListener('imiger.userLoggedOut', () => {
25
			privateDiagramList.innerHTML = '';
26
			usernameLabel.innerText = '';
27
		});
19 28
	}
20 29

  
21 30
	/**
sources/src/main/webapp/js/userMenu.js
1 1
document.addEventListener('DOMContentLoaded', function() {
2
	var toggleLoginPopupButton = document.getElementById('toggleLoginPopupButton');
3
	var toggleRegisterPopupButton = document.getElementById('toggleRegisterPopupButton');
4
	var usernameLabel = document.getElementById('usernameLabel');
5
	var logoutButton = document.getElementById('logoutButton');
2
	const loginPopup = new LoginPopup;
3
	const registerPopup = new RegisterPopup;
6 4

  
7
	var loginPopup = document.getElementById('loginPopup');
8
	var registerPopup = document.getElementById('registerPopup');
5
	const header = document.getElementById('header');
6
	header.appendChild(loginPopup.render());
7
	header.appendChild(registerPopup.render());
9 8

  
10
	var loginForm = document.forms['loginForm'];
11
	var registerForm = document.forms['registerForm'];
12

  
13
	toggleLoginPopupButton.addEventListener('click', function() {
14
		registerPopup.classList.add('hidden');
15
		loginPopup.classList.toggle('hidden');
16

  
17
		loginForm.username.focus();
9
	document.getElementById('toggleLoginPopupButton').addEventListener('click', function() {
10
		registerPopup.close();
11
		loginPopup.toggle();
18 12
	});
19 13

  
20
	toggleRegisterPopupButton.addEventListener('click', function() {
21
		loginPopup.classList.add('hidden');
22
		registerPopup.classList.toggle('hidden');
23

  
24
		registerForm.name.focus();
14
	document.getElementById('toggleRegisterPopupButton').addEventListener('click', function() {
15
		loginPopup.close();
16
		registerPopup.toggle();
25 17
	});
26 18

  
27
	logoutButton.addEventListener('click', async function(e) {
19
	document.getElementById('logoutButton').addEventListener('click', async function(e) {
28 20
		e.preventDefault();
29 21

  
30 22
		try {
31
			await AJAX.get(logoutButton.href);
23
			await AJAX.get(Constants.API.logOut);
32 24

  
33 25
			document.dispatchEvent(new CustomEvent('imiger.userLoggedOut'));
34 26

  
35
			usernameLabel.innerText = '';
36

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

  
......
45 35
			}
46 36
		}
47 37
	});
48

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

  
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) {
76
					case 400:
77
						printErrors(error.response);
78
						break;
79
					case 401:
80
						alert('Invalid credentials.');
81
						break;
82
					default:
83
						alert('Something went wrong.');
84
				}
85
			} else {
86
				alert('Server has probably gone away.');
87
			}
88
		}
89
	});
90

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

  
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) {
116
					case 400:
117
						printErrors(error.response);
118
						break;
119
					default:
120
						alert('Something went wrong.');
121
				}
122
			} else {
123
				alert('Server has probably gone away.');
124
			}
125
		}
126
	});
127 38
});
128

  
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;
133

  
134
		alert(data.error[key]);
135
	}
136
}
sources/src/main/webapp/showGraph.jsp
21 21
		<script src="js/libs/saveSvgAsPng.js"></script>
22 22
		<script src="js/libs/jstree.min.js"></script>
23 23

  
24
		<script src="js/components/generic/modalWindow.js"></script>
25
		<script src="js/components/generic/popover.js"></script>
26
		<script src="js/components/generic/popup.js"></script>
24 27
		<script src="js/components/attribute.js"></script>
25 28
		<script src="js/components/edge.js"></script>
26 29
		<script src="js/components/edgePopover.js"></script>
27 30
		<script src="js/components/floatingPoint.js"></script>
28 31
		<script src="js/components/group.js"></script>
29 32
		<script src="js/components/groupVertexList.js"></script>
33
		<script src="js/components/loginPopup.js"></script>
30 34
		<script src="js/components/minimap.js"></script>
31 35
		<script src="js/components/modalWindow.js"></script>
36
		<script src="js/components/registerPopup.js"></script>
32 37
		<script src="js/components/sidebar.js"></script>
33 38
		<script src="js/components/sidebarExcludedNodeList.js"></script>
34 39
		<script src="js/components/sidebarUnconnectedNodeList.js"></script>
sources/src/main/webapp/uploadFiles.jsp
13 13

  
14 14
		<link rel="stylesheet" href="css/main.css">
15 15

  
16
		<script id="htmlTags" type="application/json"><%@ include file="node_modules/html-tags/html-tags.json" %></script>
17
		<script id="svgTags" type="application/json"><%@ include file="node_modules/svg-tags/lib/svg-tags.json" %></script>
18

  
19
		<script src="js/components/generic/popup.js"></script>
20
		<script src="js/components/loginPopup.js"></script>
21
		<script src="js/components/registerPopup.js"></script>
22

  
16 23
		<script src="js/errors/abstractMethodError.js"></script>
17 24
		<script src="js/errors/httpError.js"></script>
18 25

  
19 26
		<script src="js/utils/ajax.js"></script>
27
		<script src="js/utils/dom.js"></script>
20 28

  
21 29
		<script src="js/constants.js"></script>
22 30

  
sources/src/main/webapp/userMenu.jsp
1 1
<div class="user-menu loggedInOnly">
2
	<span id="usernameLabel">${user.username}</span> <a href="api/log-out" class="button" id="logoutButton">Log out</a>
2
	<span id="usernameLabel">${user.username}</span>
3
	<button class="button" id="logoutButton">Log out</button>
3 4
</div>
4 5

  
5 6
<div class="user-menu loggedOutOnly">
6
	<button class="button" id="toggleLoginPopupButton">Log in</a>
7
	<button class="button" id="toggleRegisterPopupButton">Register</a>
8
</div>
9

  
10
<div class="login_popup hidden" id="loginPopup">
11
	<form action="api/log-in" method="post" name="loginForm">
12
		<div class="err_msg"></div>
13
		<div class="not_msg"></div>
14
		<table>
15
			<tr>
16
				<td>
17
					<label for="loginUsername">Login name:</label>
18
				</td>
19
				<td>
20
					<input type="text" name="username" id="loginUsername">
21
				</td>
22
			</tr>
23
			<tr>
24
				<td>
25
					<label for="loginPassword">Password:</label>
26
				</td>
27
				<td>
28
					<input type="password" name="password" id="loginPassword">
29
				</td>
30
			</tr>
31
			<tr>
32
				<td></td>
33
				<td>
34
					<button type="submit" class="button">Login</button>
35
				</td>
36
			</tr>
37
		</table>
38
	</form>
39
</div>
40

  
41
<div class="register_popup hidden" id="registerPopup">
42
	<form action="api/register" method="post" name="registerForm">
43
		<table>	  		
44
			<tr>
45
				<td>
46
					<label for="registerName">Name:</label>
47
				</td>
48
				<td>
49
					<input type="text" name="name" id="registerName">
50
				</td>
51
			</tr>
52
			<tr>
53
				<td>
54
					<label for="registerEmail">E-mail:</label>
55
				</td>
56
				<td>
57
					<input type="text" name="email" id="registerEmail">
58
				</td>
59
			</tr>
60
			<tr>
61
				<td>
62
					<label for="registerUsername">Login name:</label>
63
				</td>
64
				<td>
65
					<input type="text" name="username" id="registerUsername">
66
				</td>
67
			</tr>
68
			<tr>
69
				<td>
70
					<label for="registerPassword">Password:</label>
71
				</td>
72
				<td>
73
					<input type="password" name="password" id="registerPassword">
74
				</td>
75
			</tr>
76
			<tr>
77
				<td>
78
					<label for="registerPasswordCheck">Password again:</label>
79
				</td>
80
				<td>
81
					<input type="password" name="passwordCheck" id="registerPasswordCheck">
82
				</td>
83
			</tr>
84
			<tr>
85
				<td></td>
86
				<td>
87
					<button type="submit" class="button">Register</button>
88
				</td>
89
			</tr>
90
		</table>
91
	</form>
7
	<button class="button" id="toggleLoginPopupButton">Log in</button>
8
	<button class="button" id="toggleRegisterPopupButton">Register</button>
92 9
</div>

Také k dispozici: Unified diff