Skip to content

Commit 6e01007

Browse files
committed
Tutorial - Todo List App
1 parent 313a2d5 commit 6e01007

File tree

17 files changed

+1000
-0
lines changed

17 files changed

+1000
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
*.class
2+
/build/
3+
target/
24

35
# Mobile Tools for Java (J2ME)
46
.mtj.tmp/
@@ -11,3 +13,5 @@
1113
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
1214
hs_err_pid*
1315

16+
# Eclipse
17+
.settings/

tutorial/todo/.classpath

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" path="tst"/>
10+
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v7.0">
11+
<attributes>
12+
<attribute name="owner.project.facets" value="jst.web"/>
13+
</attributes>
14+
</classpathentry>
15+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
16+
<attributes>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
24+
</attributes>
25+
</classpathentry>
26+
<classpathentry kind="output" path="target/classes"/>
27+
</classpath>

tutorial/todo/.project

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>azure-documentdb-java-sample</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.jdt.core.javabuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.wst.common.project.facet.core.builder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
<buildCommand>
24+
<name>org.eclipse.wst.validation.validationbuilder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
<buildCommand>
29+
<name>org.eclipse.m2e.core.maven2Builder</name>
30+
<arguments>
31+
</arguments>
32+
</buildCommand>
33+
</buildSpec>
34+
<natures>
35+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
36+
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
37+
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
38+
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
39+
<nature>org.eclipse.jdt.core.javanature</nature>
40+
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
41+
</natures>
42+
</projectDescription>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Class-Path:
3+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
3+
<display-name>azure-documentdb-java-sample</display-name>
4+
<welcome-file-list>
5+
<welcome-file>index.html</welcome-file>
6+
<welcome-file>index.htm</welcome-file>
7+
<welcome-file>index.jsp</welcome-file>
8+
<welcome-file>default.html</welcome-file>
9+
<welcome-file>default.htm</welcome-file>
10+
<welcome-file>default.jsp</welcome-file>
11+
</welcome-file-list>
12+
</web-app>
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/**
2+
* ToDo App
3+
*/
4+
5+
var todoApp = {
6+
/*
7+
* API methods to call Java backend.
8+
*/
9+
apiEndpoint: "api",
10+
11+
createTodoItem: function(name, category, isComplete) {
12+
$.post(todoApp.apiEndpoint, {
13+
"method": "createTodoItem",
14+
"todoItemName": name,
15+
"todoItemCategory": category,
16+
"todoItemComplete": isComplete
17+
},
18+
function(data) {
19+
var todoItem = data;
20+
todoApp.addTodoItemToTable(todoItem.id, todoItem.name, todoItem.category, todoItem.complete);
21+
},
22+
"json");
23+
},
24+
25+
getTodoItems: function() {
26+
$.post(todoApp.apiEndpoint, {
27+
"method": "getTodoItems"
28+
},
29+
function(data) {
30+
var todoItemArr = data;
31+
$.each(todoItemArr, function(index, value) {
32+
todoApp.addTodoItemToTable(value.id, value.name, value.category, value.complete);
33+
});
34+
},
35+
"json");
36+
},
37+
38+
updateTodoItem: function(id, isComplete) {
39+
$.post(todoApp.apiEndpoint, {
40+
"method": "updateTodoItem",
41+
"todoItemId": id,
42+
"todoItemComplete": isComplete
43+
},
44+
function(data) {},
45+
"json");
46+
},
47+
48+
/*
49+
* UI Methods
50+
*/
51+
addTodoItemToTable: function(id, name, category, isComplete) {
52+
var rowColor = isComplete ? "active" : "warning";
53+
54+
todoApp.ui_table().append($("<tr>")
55+
.append($("<td>").text(name))
56+
.append($("<td>").text(category))
57+
.append($("<td>")
58+
.append($("<input>")
59+
.attr("type", "checkbox")
60+
.attr("id", id)
61+
.attr("checked", isComplete)
62+
.attr("class", "isComplete")
63+
))
64+
.addClass(rowColor)
65+
);
66+
},
67+
68+
/*
69+
* UI Bindings
70+
*/
71+
bindCreateButton: function() {
72+
todoApp.ui_createButton().click(function() {
73+
todoApp.createTodoItem(todoApp.ui_createNameInput().val(), todoApp.ui_createCategoryInput().val(), false);
74+
todoApp.ui_createNameInput().val("");
75+
todoApp.ui_createCategoryInput().val("");
76+
});
77+
},
78+
79+
bindUpdateButton: function() {
80+
todoApp.ui_updateButton().click(function() {
81+
// Disable button temporarily.
82+
var myButton = $(this);
83+
var originalText = myButton.text();
84+
$(this).text("Updating...");
85+
$(this).prop("disabled", true);
86+
87+
// Call api to update todo items.
88+
$.each(todoApp.ui_updateId(), function(index, value) {
89+
todoApp.updateTodoItem(value.name, value.value);
90+
value.remove();
91+
});
92+
93+
// Re-enable button.
94+
setTimeout(function() {
95+
myButton.prop("disabled", false);
96+
myButton.text(originalText);
97+
}, 500);
98+
});
99+
},
100+
101+
bindUpdateCheckboxes: function() {
102+
todoApp.ui_table().on("click", ".isComplete", function(event) {
103+
var checkboxElement = $(event.toElement);
104+
var rowElement = $(event.toElement).parents('tr');
105+
var id = checkboxElement.attr('id');
106+
var isComplete = checkboxElement.is(':checked');
107+
108+
// Togle table row color
109+
if (isComplete) {
110+
rowElement.addClass("active");
111+
rowElement.removeClass("warning");
112+
} else {
113+
rowElement.removeClass("active");
114+
rowElement.addClass("warning");
115+
}
116+
117+
// Update hidden inputs for update panel.
118+
todoApp.ui_updateForm().children("input[name='" + id + "']").remove();
119+
120+
todoApp.ui_updateForm().append($("<input>")
121+
.attr("type", "hidden")
122+
.attr("class", "updateComplete")
123+
.attr("name", id)
124+
.attr("value", isComplete));
125+
126+
});
127+
},
128+
129+
/*
130+
* UI Elements
131+
*/
132+
ui_createNameInput: function() {
133+
return $(".todoForm #inputItemName");
134+
},
135+
136+
ui_createCategoryInput: function() {
137+
return $(".todoForm #inputItemCategory");
138+
},
139+
140+
ui_createButton: function() {
141+
return $(".todoForm button");
142+
},
143+
144+
ui_table: function() {
145+
return $(".todoList table tbody");
146+
},
147+
148+
ui_updateButton: function() {
149+
return $(".todoUpdatePanel button");
150+
},
151+
152+
ui_updateForm: function() {
153+
return $(".todoUpdatePanel form");
154+
},
155+
156+
ui_updateId: function() {
157+
return $(".todoUpdatePanel .updateComplete");
158+
},
159+
160+
/*
161+
* Install the TodoApp
162+
*/
163+
install: function() {
164+
todoApp.bindCreateButton();
165+
todoApp.bindUpdateButton();
166+
todoApp.bindUpdateCheckboxes();
167+
168+
todoApp.getTodoItems();
169+
}
170+
};
171+
172+
$(document).ready(function() {
173+
todoApp.install();
174+
});

tutorial/todo/WebContent/index.jsp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
2+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
3+
<html>
4+
<head>
5+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6+
<title>Azure DocumentDB Java Sample</title>
7+
8+
<!-- Bootstrap -->
9+
<link href="//ajax.aspnetcdn.com/ajax/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
10+
11+
<style>
12+
/* Add padding to body for fixed nav bar */
13+
body {
14+
padding-top: 50px;
15+
}
16+
</style>
17+
</head>
18+
<body>
19+
<!-- Nav Bar -->
20+
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
21+
<div class="container">
22+
<div class="navbar-header">
23+
<a class="navbar-brand" href="#">My Tasks</a>
24+
</div>
25+
</div>
26+
</div>
27+
28+
<!-- Body -->
29+
<div class="container">
30+
<h1>My ToDo List</h1>
31+
32+
<hr/>
33+
34+
<!-- The ToDo List -->
35+
<div class = "todoList">
36+
<table class="table table-bordered table-striped" id="todoItems">
37+
<thead>
38+
<tr>
39+
<th>Name</th>
40+
<th>Category</th>
41+
<th>Complete</th>
42+
</tr>
43+
</thead>
44+
<tbody>
45+
</tbody>
46+
</table>
47+
48+
<!-- Update Button -->
49+
<div class="todoUpdatePanel">
50+
<form class="form-horizontal" role="form">
51+
<button type="button" class="btn btn-primary">Update Tasks</button>
52+
</form>
53+
</div>
54+
55+
</div>
56+
57+
<hr/>
58+
59+
<!-- Item Input Form -->
60+
<div class="todoForm">
61+
<form class="form-horizontal" role="form">
62+
<div class="form-group">
63+
<label for="inputItemName" class="col-sm-2">Task Name</label>
64+
<div class="col-sm-10">
65+
<input type="text" class="form-control" id="inputItemName" placeholder="Enter name">
66+
</div>
67+
</div>
68+
69+
<div class="form-group">
70+
<label for="inputItemCategory" class="col-sm-2">Task Category</label>
71+
<div class="col-sm-10">
72+
<input type="text" class="form-control" id="inputItemCategory" placeholder="Enter category">
73+
</div>
74+
</div>
75+
76+
<button type="button" class="btn btn-primary">Add Task</button>
77+
</form>
78+
</div>
79+
80+
</div>
81+
82+
<!-- Placed at the end of the document so the pages load faster -->
83+
<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.1.min.js"></script>
84+
<script src="//ajax.aspnetcdn.com/ajax/bootstrap/3.2.0/bootstrap.min.js"></script>
85+
<script src="assets/todo.js"></script>
86+
</body>
87+
</html>

0 commit comments

Comments
 (0)