Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 0d554903

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

modified Upload screen to dynamically display/hide user's private diagrams on log in/out

Zobrazit rozdíly:

sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/GetPrivateDiagrams.java
1
package cz.zcu.kiv.offscreen.servlets.api;
2

  
3
import cz.zcu.kiv.offscreen.servlets.BaseServlet;
4
import cz.zcu.kiv.offscreen.user.DB;
5
import cz.zcu.kiv.offscreen.user.Diagram;
6
import org.json.JSONArray;
7
import org.json.JSONObject;
8

  
9
import javax.servlet.http.HttpServletRequest;
10
import javax.servlet.http.HttpServletResponse;
11
import java.io.IOException;
12
import java.util.List;
13
import java.util.Map;
14

  
15
public class GetPrivateDiagrams extends BaseServlet {
16

  
17
    @Override
18
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
19
        if (!isLoggedIn(request)) {
20
            response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
21
            return;
22
        }
23

  
24
        DB db = new DB(getServletContext());
25
        Diagram diagram = new Diagram(db);
26

  
27
        int loggedUserId = getUserId(request);
28
        List<Map<String, String>> userDiagramList = diagram.getDiagramListByUserId(loggedUserId);
29

  
30
        JSONArray json = new JSONArray();
31
        for (Map<String, String> userDiagram : userDiagramList) {
32
            json.put(new JSONObject(userDiagram));
33
        }
34

  
35
        response.setContentType("application/json");
36
        response.setCharacterEncoding("UTF-8");
37
        response.setStatus(HttpServletResponse.SC_OK);
38
        response.getWriter().write(json.toString());
39
        response.getWriter().flush();
40
    }
41
}
sources/src/main/webapp/WEB-INF/web.xml
76 76
		<servlet-name>LoadGraphData</servlet-name>
77 77
		<servlet-class>cz.zcu.kiv.offscreen.servlets.api.LoadGraphData</servlet-class>
78 78
	</servlet>
79
	<servlet>
80
		<servlet-name>GetPrivateDiagrams</servlet-name>
81
		<servlet-class>cz.zcu.kiv.offscreen.servlets.api.GetPrivateDiagrams</servlet-class>
82
	</servlet>
79 83

  
80 84
	<!--
81 85
	URL-to-servlet mappings
......
116 120
		<servlet-name>LoadGraphData</servlet-name>
117 121
		<url-pattern>/api/load-graph-data</url-pattern>
118 122
	</servlet-mapping>
123
	<servlet-mapping>
124
		<servlet-name>GetPrivateDiagrams</servlet-name>
125
		<url-pattern>/api/get-private-diagrams</url-pattern>
126
	</servlet-mapping>
119 127

  
120 128
	<session-config>
121 129
		<session-timeout>120</session-timeout>
sources/src/main/webapp/uploadFiles.jsp
78 78
		</main>
79 79

  
80 80
		<script>
81
			$('.removeDiagramButton').click(function(e) {
82

  
83
			    if(confirm('Are you want to delete ' + $(this).data('name') + '?')) {
84
                    $.ajax({
85
                        'type': 'delete',
86
                        'url': 'api/remove-diagram?diagram_id=' + $(this).data('id'),
87
                        'success': function () {
88
                            location.reload(true);
89
                        },
90
                        'error': function (xhr) {
91
                            switch (xhr.status) {
92
                                case 401:
93
                                    alert('You are either not logged in or not an owner of this diagram.');
94
                                    break;
95
                                default:
96
                                    alert('Something went wrong.');
97
                            }
98
                        },
99
                    });
100
                }
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 = '';
101 132
			});
102 133
		</script>
103 134
	</body>

Také k dispozici: Unified diff