[Notice] Announcing the End of Demo Server [Read me]

project: Add Tagging feature.
While getting tags: * Return 406 if the client cannot accept appcation/json. While tagging: * Return the tag if that is tagged newly. * Create new tag if no tag matches the given name. * Do nothing if that has been tagged already. While untagging: * Respond 404 if no tag matches the given id. * Delete the tag if there is no project tagged by that.
@5be44e381b9e24774fe22ecff92007dc0420efbd
--- app/controllers/ProjectApp.java
+++ app/controllers/ProjectApp.java
... | ... | @@ -1,5 +1,6 @@ |
1 | 1 |
package controllers; |
2 | 2 |
|
3 |
+import com.avaje.ebean.ExpressionList; |
|
3 | 4 |
import com.avaje.ebean.Page; |
4 | 5 |
import models.*; |
5 | 6 |
import models.enumeration.Operation; |
... | ... | @@ -16,13 +17,17 @@ |
16 | 17 |
import playRepository.RepositoryService; |
17 | 18 |
import utils.AccessControl; |
18 | 19 |
import utils.Constants; |
20 |
+import utils.HttpUtil; |
|
19 | 21 |
import views.html.project.*; |
20 | 22 |
|
21 | 23 |
import java.io.File; |
22 | 24 |
import java.io.IOException; |
23 | 25 |
import java.security.NoSuchAlgorithmException; |
26 |
+import java.util.HashMap; |
|
27 |
+import java.util.Map; |
|
24 | 28 |
|
25 | 29 |
import static play.data.Form.form; |
30 |
+import static play.libs.Json.toJson; |
|
26 | 31 |
|
27 | 32 |
/** |
28 | 33 |
* @author "Hwi Ahn" |
... | ... | @@ -245,4 +250,72 @@ |
245 | 250 |
|
246 | 251 |
return ok(projectList.render("title.projectList", projects, filter, state)); |
247 | 252 |
} |
253 |
+ |
|
254 |
+ public static Result tags(String ownerName, String projectName) { |
|
255 |
+ Project project = Project.findByNameAndOwner(ownerName, projectName); |
|
256 |
+ if (!AccessControl.isAllowed(UserApp.currentUser(), project.asResource(), Operation.READ)) { |
|
257 |
+ return forbidden(); |
|
258 |
+ } |
|
259 |
+ |
|
260 |
+ if (!request().accepts("application/json")) { |
|
261 |
+ return status(406); |
|
262 |
+ } |
|
263 |
+ |
|
264 |
+ Map<Long, String> tags = new HashMap<Long, String>(); |
|
265 |
+ for (Tag tag: project.tags) { |
|
266 |
+ tags.put(tag.id, tag.name); |
|
267 |
+ } |
|
268 |
+ |
|
269 |
+ return ok(toJson(tags)); |
|
270 |
+ } |
|
271 |
+ |
|
272 |
+ public static Result tag(String ownerName, String projectName) { |
|
273 |
+ Project project = Project.findByNameAndOwner(ownerName, projectName); |
|
274 |
+ if (!AccessControl.isAllowed(UserApp.currentUser(), project.asResource(), Operation.UPDATE)) { |
|
275 |
+ return forbidden(); |
|
276 |
+ } |
|
277 |
+ |
|
278 |
+ // Get tag name from the request. Return empty map if the name is not given. |
|
279 |
+ Map<String, String[]> data = request().body().asFormUrlEncoded(); |
|
280 |
+ String name = HttpUtil.getFirstValueFromQuery(data, "name"); |
|
281 |
+ if (name == null || name.length() == 0) { |
|
282 |
+ return ok(toJson(new HashMap<Long, String>())); |
|
283 |
+ } |
|
284 |
+ |
|
285 |
+ Tag tag = project.tag(name); |
|
286 |
+ |
|
287 |
+ if (tag == null) { |
|
288 |
+ // Return empty map if the tag has been already attached. |
|
289 |
+ return ok(toJson(new HashMap<Long, String>())); |
|
290 |
+ } else { |
|
291 |
+ // Return the tag. |
|
292 |
+ Map<Long, String> tags = new HashMap<Long, String>(); |
|
293 |
+ tags.put(tag.id, tag.name); |
|
294 |
+ return ok(toJson(tags)); |
|
295 |
+ } |
|
296 |
+ } |
|
297 |
+ |
|
298 |
+ public static Result untag(String ownerName, String projectName, Long id) { |
|
299 |
+ Project project = Project.findByNameAndOwner(ownerName, projectName); |
|
300 |
+ if (!AccessControl.isAllowed(UserApp.currentUser(), project.asResource(), Operation.UPDATE)) { |
|
301 |
+ return forbidden(); |
|
302 |
+ } |
|
303 |
+ |
|
304 |
+ // _method must be 'delete' |
|
305 |
+ Map<String, String[]> data = request().body().asFormUrlEncoded(); |
|
306 |
+ if (!HttpUtil.getFirstValueFromQuery(data, "_method").toLowerCase() |
|
307 |
+ .equals("delete")) { |
|
308 |
+ return badRequest("_method must be 'delete'."); |
|
309 |
+ } |
|
310 |
+ |
|
311 |
+ Tag tag = Tag.find.byId(id); |
|
312 |
+ |
|
313 |
+ if (tag == null) { |
|
314 |
+ return notFound(); |
|
315 |
+ } |
|
316 |
+ |
|
317 |
+ project.untag(tag); |
|
318 |
+ |
|
319 |
+ return status(204); |
|
320 |
+ } |
|
248 | 321 |
} |
+++ app/controllers/TagApp.java
... | ... | @@ -0,0 +1,48 @@ |
1 | +package controllers; | |
2 | + | |
3 | +import com.avaje.ebean.ExpressionList; | |
4 | +import models.Project; | |
5 | +import models.Tag; | |
6 | +import models.enumeration.Operation; | |
7 | +import play.mvc.Controller; | |
8 | +import play.mvc.Result; | |
9 | +import utils.AccessControl; | |
10 | + | |
11 | +import java.util.ArrayList; | |
12 | +import java.util.HashMap; | |
13 | +import java.util.List; | |
14 | +import java.util.Map; | |
15 | + | |
16 | +import static play.libs.Json.toJson; | |
17 | + | |
18 | +/** | |
19 | + * Created with IntelliJ IDEA. | |
20 | + * User: nori | |
21 | + * Date: 13. 4. 12 | |
22 | + * Time: 오후 3:37 | |
23 | + * To change this template use File | Settings | File Templates. | |
24 | + */ | |
25 | +public class TagApp extends Controller { | |
26 | + private static final int MAX_FETCH_TAGS = 1000; | |
27 | + | |
28 | + public static Result tags(String query) { | |
29 | + if (!request().accepts("application/json")) { | |
30 | + return status(406); | |
31 | + } | |
32 | + | |
33 | + ExpressionList<Tag> el = Tag.find.where().contains("name", query); | |
34 | + int total = el.findRowCount(); | |
35 | + if (total > MAX_FETCH_TAGS) { | |
36 | + el.setMaxRows(MAX_FETCH_TAGS); | |
37 | + response().setHeader("Content-Range", "items " + MAX_FETCH_TAGS + "/" + total); | |
38 | + } | |
39 | + | |
40 | + Map<Long, String> tags = new HashMap<Long, String>(); | |
41 | + for (Tag tag: el.findList()) { | |
42 | + tags.put(tag.id, tag.name); | |
43 | + } | |
44 | + | |
45 | + return ok(toJson(tags)); | |
46 | + } | |
47 | + | |
48 | +} |
--- app/models/Project.java
+++ app/models/Project.java
... | ... | @@ -3,11 +3,7 @@ |
3 | 3 |
import java.io.IOException; |
4 | 4 |
import java.util.*; |
5 | 5 |
|
6 |
-import javax.persistence.CascadeType; |
|
7 |
-import javax.persistence.Entity; |
|
8 |
-import javax.persistence.Id; |
|
9 |
-import javax.persistence.OneToMany; |
|
10 |
-import javax.persistence.OneToOne; |
|
6 |
+import javax.persistence.*; |
|
11 | 7 |
import javax.servlet.ServletException; |
12 | 8 |
import javax.validation.constraints.NotNull; |
13 | 9 |
|
... | ... | @@ -80,6 +76,9 @@ |
80 | 76 |
|
81 | 77 |
private long lastIssueNumber; |
82 | 78 |
private long lastPostingNumber; |
79 |
+ |
|
80 |
+ @ManyToMany |
|
81 |
+ public Set<Tag> tags; |
|
83 | 82 |
|
84 | 83 |
public static Long create(Project newProject) { |
85 | 84 |
newProject.siteurl = "http://localhost:9000/" + newProject.name; |
... | ... | @@ -315,4 +314,33 @@ |
315 | 314 |
return User.findByLoginId(userId); |
316 | 315 |
} |
317 | 316 |
|
317 |
+ public Tag tag(String tagName) { |
|
318 |
+ // Find a tag by the given name. |
|
319 |
+ Tag tag = Tag.find.where().eq("name", tagName).findUnique(); |
|
320 |
+ |
|
321 |
+ if (tag == null) { |
|
322 |
+ // Create new tag if there is no tag which has the given name. |
|
323 |
+ tag = new Tag(); |
|
324 |
+ tag.name = tagName; |
|
325 |
+ tag.save(); |
|
326 |
+ } else if (tag.projects.contains(this)) { |
|
327 |
+ // Return empty map if the tag has been already attached. |
|
328 |
+ return null; |
|
329 |
+ } |
|
330 |
+ |
|
331 |
+ // Attach new tag. |
|
332 |
+ tag.projects.add(this); |
|
333 |
+ tag.update(); |
|
334 |
+ |
|
335 |
+ return tag; |
|
336 |
+ } |
|
337 |
+ |
|
338 |
+ public void untag(Tag tag) { |
|
339 |
+ tag.projects.remove(this); |
|
340 |
+ if (tag.projects.size() == 0) { |
|
341 |
+ tag.delete(); |
|
342 |
+ } else { |
|
343 |
+ tag.update(); |
|
344 |
+ } |
|
345 |
+ } |
|
318 | 346 |
} |
+++ app/models/Tag.java
... | ... | @@ -0,0 +1,71 @@ |
1 | +package models; | |
2 | + | |
3 | +import models.enumeration.ResourceType; | |
4 | +import models.resource.Resource; | |
5 | +import play.data.validation.Constraints.Required; | |
6 | +import play.db.ebean.Model; | |
7 | + | |
8 | +import javax.persistence.*; | |
9 | +import java.util.List; | |
10 | +import java.util.Set; | |
11 | + | |
12 | +@Entity | |
13 | +public class Tag extends Model { | |
14 | + | |
15 | + /** | |
16 | + * | |
17 | + */ | |
18 | + private static final long serialVersionUID = -35487506476718498L; | |
19 | + public static Finder<Long, Tag> find = new Finder<Long, Tag>(Long.class, Tag.class); | |
20 | + | |
21 | + @Id | |
22 | + public Long id; | |
23 | + | |
24 | + @Required | |
25 | + @Column(unique=true) | |
26 | + public String name; | |
27 | + | |
28 | + @ManyToMany(mappedBy="tags") | |
29 | + public Set<Project> projects; | |
30 | + | |
31 | + public static List<Tag> findByProjectId(Long projectId) { | |
32 | + return find.where().eq("project.id", projectId).findList(); | |
33 | + } | |
34 | + | |
35 | + public static Tag findById(Long id) { | |
36 | + return find.byId(id); | |
37 | + } | |
38 | + | |
39 | + @Transient | |
40 | + public boolean exists() { | |
41 | + return find.where().eq("name", name).findRowCount() > 0; | |
42 | + } | |
43 | + | |
44 | + @Override | |
45 | + public void delete() { | |
46 | + for(Project project: projects) { | |
47 | + project.tags.remove(this); | |
48 | + project.save(); | |
49 | + } | |
50 | + super.delete(); | |
51 | + } | |
52 | + | |
53 | + public Resource asResource() { | |
54 | + return new Resource() { | |
55 | + @Override | |
56 | + public Long getId() { | |
57 | + return id; | |
58 | + } | |
59 | + | |
60 | + @Override | |
61 | + public Project getProject() { | |
62 | + return null; | |
63 | + } | |
64 | + | |
65 | + @Override | |
66 | + public ResourceType getType() { | |
67 | + return ResourceType.TAG; | |
68 | + } | |
69 | + }; | |
70 | + } | |
71 | +}(No newline at end of file) |
--- app/models/enumeration/ResourceType.java
+++ app/models/enumeration/ResourceType.java
... | ... | @@ -21,7 +21,8 @@ |
21 | 21 |
PROJECT("project"), |
22 | 22 |
ATTACHMENT("attachment"), |
23 | 23 |
ISSUE_COMMENT("issue_comment"), |
24 |
- NONISSUE_COMMENT("nonissue_comment"); |
|
24 |
+ NONISSUE_COMMENT("nonissue_comment"), |
|
25 |
+ TAG("tag"); |
|
25 | 26 |
|
26 | 27 |
private String resource; |
27 | 28 |
|
--- app/views/project/projectHome.scala.html
+++ app/views/project/projectHome.scala.html
... | ... | @@ -27,7 +27,10 @@ |
27 | 27 |
<strong>라이센스 :</strong> GPL v2 |
28 | 28 |
</li> |
29 | 29 |
<li class="info"> |
30 |
- <strong>운영체제 :</strong> 리눅스 |
|
30 |
+ <strong>@Messages("project.tags") :</strong> |
|
31 |
+ @for(tag <- project.tags) { |
|
32 |
+ <span class='label label-info'>@tag.name</span> |
|
33 |
+ } |
|
31 | 34 |
</li> |
32 | 35 |
<li class="info"> |
33 | 36 |
<strong>프로그래밍 언어 :</strong> PHP, Python, Java |
--- app/views/project/projectList.scala.html
+++ app/views/project/projectList.scala.html
... | ... | @@ -37,6 +37,9 @@ |
37 | 37 |
<div class="header"> |
38 | 38 |
<a href="@routes.UserApp.userInfo(project.owner)">@project.owner</a> / <a href="@routes.ProjectApp.project(project.owner, project.name)" class="project-name">@project.name</a> |
39 | 39 |
@if(!project.share_option){ <i class="ico ico-lock"></i> } |
40 |
+ @for(tag <- project.tags) { |
|
41 |
+ <span class='label label-info'>@tag.name</span> |
|
42 |
+ } |
|
40 | 43 |
</div> |
41 | 44 |
<div class="desc"> |
42 | 45 |
@project.overview |
--- app/views/project/setting.scala.html
+++ app/views/project/setting.scala.html
... | ... | @@ -54,6 +54,17 @@ |
54 | 54 |
</dl> |
55 | 55 |
</div> |
56 | 56 |
<div class="box-wrap middle"> |
57 |
+ <div class="cu-label">@Messages("project.tags")</div> |
|
58 |
+ <div class="cu-desc"> |
|
59 |
+ <div id="tags"> |
|
60 |
+ <!-- tags will be added here by hive.project.Settings.js --> |
|
61 |
+ </div> |
|
62 |
+ <input name="newTag" type="text" class="text" style="margin-bottom: 0px" |
|
63 |
+ data-provider="typeahead" autocomplete="off"/> |
|
64 |
+ <a href="javascript:void(0)" class="n-btn small orange" id="addTag">@Messages("button.add")</a> |
|
65 |
+ </div> |
|
66 |
+ </div> |
|
67 |
+ <div class="box-wrap middle"> |
|
57 | 68 |
<div class="cu-label">@Messages("project.shareOption")</div> |
58 | 69 |
<div class="cu-desc"> |
59 | 70 |
<input name="share_option" type="radio" @if(project.share_option == true){checked="checked"} id="public" value="true" class="radio-btn"><label for="public" class="bg-radiobtn label-public">@Messages("project.public")</label> |
... | ... | @@ -103,8 +114,12 @@ |
103 | 114 |
|
104 | 115 |
<script type="text/javascript"> |
105 | 116 |
$(document).ready(function(){ |
106 |
- $hive.loadModule("project.Setting"); |
|
117 |
+ $hive.loadModule("project.Setting", { |
|
118 |
+ "sURLProjectTags": "@routes.ProjectApp.tags(project.owner, project.name)", |
|
119 |
+ "sURLTags" : "@routes.TagApp.tags()" |
|
120 |
+ }); |
|
107 | 121 |
}); |
122 |
+ |
|
108 | 123 |
</script> |
109 | 124 |
|
110 | 125 |
} |
--- app/views/user/info.scala.html
+++ app/views/user/info.scala.html
... | ... | @@ -100,6 +100,11 @@ |
100 | 100 |
</h3> |
101 | 101 |
<div class="stream-desc-wrap"> |
102 | 102 |
<div class="stream-desc"> |
103 |
+ <p class="tags"> |
|
104 |
+ @for(tag <- project.tags) { |
|
105 |
+ <span class='label label-info'>@tag.name</span> |
|
106 |
+ } |
|
107 |
+ </p> |
|
103 | 108 |
<p class="nm">@project.overview</p> |
104 | 109 |
<p class="date">Last updated @agoString(project.ago)</p> |
105 | 110 |
</div> |
+++ conf/evolutions/default/8.sql
... | ... | @@ -0,0 +1,20 @@ |
1 | +# --- !Ups | |
2 | + | |
3 | +CREATE TABLE project_tag ( | |
4 | + project_id BIGINT NOT NULL, | |
5 | + tag_id BIGINT NOT NULL, | |
6 | + CONSTRAINT pk_project_tag PRIMARY KEY (project_id, tag_id)); | |
7 | + | |
8 | +CREATE TABLE tag ( | |
9 | + id BIGINT NOT NULL, | |
10 | + name VARCHAR(255), | |
11 | + CONSTRAINT uq_tag_name UNIQUE (NAME), | |
12 | + CONSTRAINT pk_tag PRIMARY KEY (ID)); | |
13 | + | |
14 | +CREATE SEQUENCE tag_seq; | |
15 | + | |
16 | +# --- !Downs | |
17 | + | |
18 | +DROP SEQUENCE IF EXISTS tag_seq; | |
19 | +DROP TABLE IF EXISTS project_tag; | |
20 | +DROP TABLE IF EXISTS tag; |
--- conf/messages.en
+++ conf/messages.en
... | ... | @@ -249,6 +249,7 @@ |
249 | 249 |
project.readme = You can see README.md here if you add it into the code repository. |
250 | 250 |
project.searchPlaceholder = search at current project |
251 | 251 |
project.wrongName = Project name is wrong |
252 |
+project.tags = Tags |
|
252 | 253 |
|
253 | 254 |
#Site |
254 | 255 |
site.sidebar = Site Management |
... | ... | @@ -367,4 +368,4 @@ |
367 | 368 |
|
368 | 369 |
#userinfo |
369 | 370 |
userinfo.myProjects = My Projects |
370 |
-userinfo.starredProjects = Starred(No newline at end of file) |
|
371 |
+userinfo.starredProjects = Starred |
--- conf/messages.ko
+++ conf/messages.ko
... | ... | @@ -248,6 +248,7 @@ |
248 | 248 |
project.readme = 프로젝트에 대한 설명을 README.md 파일로 작성해서 코드저장소에 추가하면 이 곳에 나타납니다. |
249 | 249 |
project.searchPlaceholder = 현재 프로젝트에서 검색 |
250 | 250 |
project.wrongName = 프로젝트 이름이 올바르지 않습니다. |
251 |
+project.tags = 태그 |
|
251 | 252 |
|
252 | 253 |
#Site |
253 | 254 |
site.sidebar = 사이트 관리 |
... | ... | @@ -367,4 +368,4 @@ |
367 | 368 |
|
368 | 369 |
#userinfo |
369 | 370 |
userinfo.myProjects = 내 프로젝트 |
370 |
-userinfo.starredProjects = 관심 프로젝트(No newline at end of file) |
|
371 |
+userinfo.starredProjects = 관심 프로젝트 |
--- conf/routes
+++ conf/routes
... | ... | @@ -59,6 +59,12 @@ |
59 | 59 |
POST /:user/:project/post/:id/edit controllers.BoardApp.editPost(user, project, id:Long) |
60 | 60 |
GET /:user/:project/post/:id/comment/:commentId/delete controllers.BoardApp.deleteComment(user, project, id:Long, commentId:Long) |
61 | 61 |
|
62 |
+# Tags |
|
63 |
+GET /tags controllers.TagApp.tags(query: String ?= "") |
|
64 |
+GET /:user/:project/tags controllers.ProjectApp.tags(user, project) |
|
65 |
+POST /:user/:project/tags controllers.ProjectApp.tag(user, project) |
|
66 |
+POST /:user/:project/tags/:id controllers.ProjectApp.untag(user, project, id: Long) |
|
67 |
+ |
|
62 | 68 |
# Projects |
63 | 69 |
GET /projectform controllers.ProjectApp.newProjectForm() |
64 | 70 |
POST /projects controllers.ProjectApp.newProject() |
--- public/javascripts/common/hive.Common.js
+++ public/javascripts/common/hive.Common.js
... | ... | @@ -239,6 +239,30 @@ |
239 | 239 |
function getTrim(sValue){ |
240 | 240 |
return sValue.trim().replace(htVar.rxTrim, ''); |
241 | 241 |
} |
242 |
+ |
|
243 |
+ /** |
|
244 |
+ * Return whether the given content range is an entire range for items. |
|
245 |
+ * e.g) "items 10/10" |
|
246 |
+ * |
|
247 |
+ * @param {String} contentRange the vaule of Content-Range header from response |
|
248 |
+ * @return {Boolean} |
|
249 |
+ */ |
|
250 |
+ function isEntireRange(contentRange) { |
|
251 |
+ var result, items, total; |
|
252 |
+ |
|
253 |
+ if (contentRange) { |
|
254 |
+ result = /items\s+([0-9]+)\/([0-9]+)/.exec(contentRange); |
|
255 |
+ if (result) { |
|
256 |
+ items = parseInt(result[1]); |
|
257 |
+ total = parseInt(result[2]); |
|
258 |
+ if (items < total) { |
|
259 |
+ return false; |
|
260 |
+ } |
|
261 |
+ } |
|
262 |
+ } |
|
263 |
+ |
|
264 |
+ return true; |
|
265 |
+ } |
|
242 | 266 |
|
243 | 267 |
/* public Interface */ |
244 | 268 |
return { |
... | ... | @@ -249,7 +273,8 @@ |
249 | 273 |
"stopEvent": stopEvent, |
250 | 274 |
"getContrastColor": getContrastColor, |
251 | 275 |
"sendForm" : sendForm, |
252 |
- "getTrim" : getTrim |
|
276 |
+ "getTrim" : getTrim, |
|
277 |
+ "isEntireRange": isEntireRange |
|
253 | 278 |
}; |
254 | 279 |
})(); |
255 | 280 |
|
--- public/javascripts/service/hive.project.Member.js
+++ public/javascripts/service/hive.project.Member.js
... | ... | @@ -110,8 +110,35 @@ |
110 | 110 |
} |
111 | 111 |
}); |
112 | 112 |
} |
113 |
- |
|
113 |
+ |
|
114 |
+ /** |
|
115 |
+ * Data source for loginId typeahead while adding new member. |
|
116 |
+ * |
|
117 |
+ * For more information, See "source" option at |
|
118 |
+ * http://twitter.github.io/bootstrap/javascript.html#typeahead |
|
119 |
+ * |
|
120 |
+ * @param {String} query |
|
121 |
+ * @param {Function} process |
|
122 |
+ */ |
|
123 |
+ function _userTypeaheadSource(query, process) { |
|
124 |
+ if (query.match(htVar.lastQuery) && htVar.isLastRangeEntire) { |
|
125 |
+ process(htVar.cachedUsers); |
|
126 |
+ } else { |
|
127 |
+ $('<form action="/users" method="GET">') |
|
128 |
+ .append($('<input type="hidden" name="query">').val(query)) |
|
129 |
+ .ajaxForm({ |
|
130 |
+ "dataType": "json", |
|
131 |
+ "success": function(data, status, xhr) { |
|
132 |
+ htVar.isLastRangeEntire = $hive.isEntireRange(xhr.getResponseHeader('Content-Range')); |
|
133 |
+ htVar.lastQuery = query; |
|
134 |
+ htVar.cachedUsers = data; |
|
135 |
+ process(data); |
|
136 |
+ } |
|
137 |
+ }).submit(); |
|
138 |
+ } |
|
139 |
+ } |
|
140 |
+ |
|
114 | 141 |
_init(htOptions); |
115 | 142 |
}; |
116 | 143 |
|
117 |
-})("hive.project.Member");(No newline at end of file) |
|
144 |
+})("hive.project.Member"); |
--- public/javascripts/service/hive.project.Setting.js
+++ public/javascripts/service/hive.project.Setting.js
... | ... | @@ -23,6 +23,7 @@ |
23 | 23 |
_initVar(htOpt); |
24 | 24 |
_initElement(htOpt); |
25 | 25 |
_attachEvent(); |
26 |
+ _updateTags(); |
|
26 | 27 |
|
27 | 28 |
htVar.waPopOvers.popover(); |
28 | 29 |
} |
... | ... | @@ -34,8 +35,10 @@ |
34 | 35 |
function _initVar(htOptions){ |
35 | 36 |
htVar.rxLogoExt = /\.(gif|bmp|jpg|jpeg|png)$/i; |
36 | 37 |
htVar.rxPrjName = /^[a-zA-Z0-9_][-a-zA-Z0-9_]+[^-]$/; |
38 |
+ htVar.sURLProjectTags = htOptions.sURLProjectTags; |
|
39 |
+ htVar.sURLTags = htOptions.sURLTags; |
|
37 | 40 |
} |
38 |
- |
|
41 |
+ |
|
39 | 42 |
/** |
40 | 43 |
* initialize element variables |
41 | 44 |
*/ |
... | ... | @@ -57,15 +60,24 @@ |
57 | 60 |
|
58 | 61 |
// popovers |
59 | 62 |
htVar.waPopOvers = $([$("#project_name"), $("#share_option_explanation"), $("#terms")]); |
63 |
+ |
|
64 |
+ // tags |
|
65 |
+ htElement.welInputAddTag = $('input[name="newTag"]'); |
|
66 |
+ htElement.welTags = $('#tags'); |
|
67 |
+ htElement.welBtnAddTag = $('#addTag'); |
|
60 | 68 |
} |
61 |
- |
|
62 |
- /** |
|
69 |
+ |
|
70 |
+ /** |
|
63 | 71 |
* attach event handlers |
64 | 72 |
*/ |
65 | 73 |
function _attachEvent(){ |
66 | 74 |
htElement.welInputLogo.change(_onChangeLogoPath); |
67 | 75 |
htElement.welBtnDeletePrj.click(_onClickBtnDeletePrj); |
68 | 76 |
htElement.welBtnSave.click(_onClickBtnSave); |
77 |
+ htElement.welInputAddTag |
|
78 |
+ .keypress(_onKeyPressNewTag) |
|
79 |
+ .typeahead().data('typeahead').source = _tagTypeaheadSource; |
|
80 |
+ htElement.welBtnAddTag.click(_submitTag); |
|
69 | 81 |
} |
70 | 82 |
|
71 | 83 |
/** |
... | ... | @@ -110,6 +122,113 @@ |
110 | 122 |
return true; |
111 | 123 |
} |
112 | 124 |
|
125 |
+ /** |
|
126 |
+ * Data source for tag typeahead while adding new tag. |
|
127 |
+ * |
|
128 |
+ * For more information, See "source" option at |
|
129 |
+ * http://twitter.github.io/bootstrap/javascript.html#typeahead |
|
130 |
+ * |
|
131 |
+ * @param {String} query |
|
132 |
+ * @param {Function} process |
|
133 |
+ */ |
|
134 |
+ function _tagTypeaheadSource(query, process) { |
|
135 |
+ if (query.match(htVar.lastQuery) && htVar.isLastRangeEntire) { |
|
136 |
+ process(htVar.cachedTags); |
|
137 |
+ } else { |
|
138 |
+ $('<form method="GET">') |
|
139 |
+ .attr('action', htVar.sURLTags) |
|
140 |
+ .append($('<input type="hidden" name="query">').val(query)) |
|
141 |
+ .ajaxForm({ |
|
142 |
+ "dataType": "json", |
|
143 |
+ "success": function(tags, status, xhr) { |
|
144 |
+ var tagNames = []; |
|
145 |
+ for(var id in tags) { |
|
146 |
+ tagNames.push(tags[id]); |
|
147 |
+ } |
|
148 |
+ htVar.isLastRangeEntire = $hive.isEntireRange( |
|
149 |
+ xhr.getResponseHeader('Content-Range')); |
|
150 |
+ htVar.lastQuery = query; |
|
151 |
+ htVar.cachedTags = tagNames; |
|
152 |
+ process(tagNames); |
|
153 |
+ } |
|
154 |
+ }).submit(); |
|
155 |
+ } |
|
156 |
+ }; |
|
157 |
+ |
|
158 |
+ /** |
|
159 |
+ * Submit new tag to add that. |
|
160 |
+ */ |
|
161 |
+ function _submitTag () { |
|
162 |
+ $('<form method="POST">') |
|
163 |
+ .attr('action', htVar.sURLProjectTags) |
|
164 |
+ .append($('<input type="hidden" name="name">') |
|
165 |
+ .val(htElement.welInputAddTag.val())) |
|
166 |
+ .ajaxForm({ "success": _appendTags }) |
|
167 |
+ .submit(); |
|
168 |
+ } |
|
169 |
+ |
|
170 |
+ /** |
|
171 |
+ * If user presses enter at newtag element, get list of tags from the |
|
172 |
+ * server and show them in #tags div. |
|
173 |
+ * |
|
174 |
+ * @param {Object} oEvent |
|
175 |
+ */ |
|
176 |
+ function _onKeyPressNewTag(oEvent) { |
|
177 |
+ if (oEvent.keyCode == 13) { |
|
178 |
+ _submitTag(); |
|
179 |
+ htElement.welInputAddTag.val(""); |
|
180 |
+ return false; |
|
181 |
+ } |
|
182 |
+ } |
|
183 |
+ |
|
184 |
+ /** |
|
185 |
+ * Get list of tags from the server and show them in #tags div. |
|
186 |
+ */ |
|
187 |
+ function _updateTags() { |
|
188 |
+ $('<form method="GET">') |
|
189 |
+ .attr('action', htVar.sURLProjectTags) |
|
190 |
+ .ajaxForm({ |
|
191 |
+ "dataType": "json", |
|
192 |
+ "success": _appendTags |
|
193 |
+ }).submit(); |
|
194 |
+ } |
|
195 |
+ |
|
196 |
+ /** |
|
197 |
+ * Make a tag element by given id and name. |
|
198 |
+ |
|
199 |
+ * @param {String} sId |
|
200 |
+ * @param {String} sName |
|
201 |
+ */ |
|
202 |
+ function _createTag(sId, sName) { |
|
203 |
+ var fDelTag = function(ev) { |
|
204 |
+ $('<form method="POST">') |
|
205 |
+ .attr('action', htVar.sURLProjectTags + '/' + sId) |
|
206 |
+ .append($('<input type="hidden" name="_method" value="DELETE">')) |
|
207 |
+ .ajaxForm({ |
|
208 |
+ "success": function(data, status, xhr) { |
|
209 |
+ welTag.remove(); |
|
210 |
+ } |
|
211 |
+ }).submit(); |
|
212 |
+ }; |
|
213 |
+ |
|
214 |
+ var welTag = $("<span class='label label-info'>") |
|
215 |
+ .text(sName + " ") |
|
216 |
+ .append($("<a href='javascript:void(0)'>").text("x").click(fDelTag)); |
|
217 |
+ |
|
218 |
+ return welTag; |
|
219 |
+ }; |
|
220 |
+ |
|
221 |
+ /** |
|
222 |
+ * Append the given tags on #tags div to show them. |
|
223 |
+ * |
|
224 |
+ * @param {Object} htTags |
|
225 |
+ */ |
|
226 |
+ function _appendTags(htTags) { |
|
227 |
+ for(var sId in htTags) { |
|
228 |
+ htElement.welTags.append(_createTag(sId, htTags[sId])); |
|
229 |
+ } |
|
230 |
+ }; |
|
231 |
+ |
|
113 | 232 |
_init(htOptions); |
114 | 233 |
}; |
115 | 234 |
|
+++ test/controllers/ProjectAppTest.java
... | ... | @@ -0,0 +1,131 @@ |
1 | +package controllers; | |
2 | + | |
3 | +import models.Project; | |
4 | +import models.Tag; | |
5 | +import models.User; | |
6 | +import org.codehaus.jackson.JsonNode; | |
7 | +import org.junit.BeforeClass; | |
8 | +import org.junit.Test; | |
9 | +import play.libs.Json; | |
10 | +import play.mvc.Result; | |
11 | +import play.test.Helpers; | |
12 | + | |
13 | +import java.util.HashMap; | |
14 | +import java.util.Iterator; | |
15 | +import java.util.Map; | |
16 | + | |
17 | +import static org.fest.assertions.Assertions.assertThat; | |
18 | +import static play.test.Helpers.*; | |
19 | + | |
20 | +public class ProjectAppTest { | |
21 | + @BeforeClass | |
22 | + public static void beforeClass() { | |
23 | + callAction( | |
24 | + routes.ref.Application.init() | |
25 | + ); | |
26 | + } | |
27 | + | |
28 | + @Test | |
29 | + public void tag() { | |
30 | + running(fakeApplication(Helpers.inMemoryDatabase()), new Runnable() { | |
31 | + public void run() { | |
32 | + //Given | |
33 | + Map<String,String> data = new HashMap<String,String>(); | |
34 | + data.put("name", "foo"); | |
35 | + User admin = User.findByLoginId("admin"); | |
36 | + | |
37 | + //When | |
38 | + Result result = callAction( | |
39 | + controllers.routes.ref.ProjectApp.tag("hobi", "nForge4java"), | |
40 | + fakeRequest() | |
41 | + .withFormUrlEncodedBody(data) | |
42 | + .withHeader("Accept", "application/json") | |
43 | + .withSession(UserApp.SESSION_USERID, admin.id.toString()) | |
44 | + ); | |
45 | + | |
46 | + //Then | |
47 | + assertThat(status(result)).isEqualTo(OK); | |
48 | + Iterator<Map.Entry<String, JsonNode>> fields = Json.parse(contentAsString(result)).getFields(); | |
49 | + Map.Entry<String, JsonNode> field = fields.next(); | |
50 | + Tag expected = new Tag(); | |
51 | + expected.id = Long.valueOf(field.getKey()); | |
52 | + expected.name = field.getValue().asText(); | |
53 | + assertThat(expected.name).isEqualTo("foo"); | |
54 | + assertThat(Project.findByNameAndOwner("hobi", "nForge4java").tags.contains(expected)).isTrue(); | |
55 | + } | |
56 | + }); | |
57 | + } | |
58 | + | |
59 | + @Test | |
60 | + public void tags() { | |
61 | + running(fakeApplication(Helpers.inMemoryDatabase()), new Runnable() { | |
62 | + public void run() { | |
63 | + //Given | |
64 | + Project project = Project.findByNameAndOwner("hobi", "nForge4java"); | |
65 | + | |
66 | + Tag tag1 = new Tag(); | |
67 | + tag1.name = "foo"; | |
68 | + tag1.save(); | |
69 | + project.tags.add(tag1); | |
70 | + project.update(); | |
71 | + | |
72 | + Tag tag2 = new Tag(); | |
73 | + tag2.name = "bar"; | |
74 | + tag2.save(); | |
75 | + project.tags.add(tag2); | |
76 | + project.update(); | |
77 | + | |
78 | + //When | |
79 | + | |
80 | + Result result = callAction( | |
81 | + controllers.routes.ref.ProjectApp.tags("hobi", "nForge4java"), | |
82 | + fakeRequest().withHeader("Accept", "application/json") | |
83 | + ); | |
84 | + | |
85 | + //Then | |
86 | + assertThat(status(result)).isEqualTo(OK); | |
87 | + JsonNode json = Json.parse(contentAsString(result)); | |
88 | + assertThat(json.has(tag1.id.toString())).isTrue(); | |
89 | + assertThat(json.has(tag2.id.toString())).isTrue(); | |
90 | + assertThat(json.get(tag1.id.toString()).asText()).isEqualTo("foo"); | |
91 | + assertThat(json.get(tag2.id.toString()).asText()).isEqualTo("bar"); | |
92 | + } | |
93 | + }); | |
94 | + } | |
95 | + | |
96 | + @Test | |
97 | + public void untag() { | |
98 | + running(fakeApplication(Helpers.inMemoryDatabase()), new Runnable() { | |
99 | + public void run() { | |
100 | + //Given | |
101 | + Project project = Project.findByNameAndOwner("hobi", "nForge4java"); | |
102 | + | |
103 | + Tag tag1 = new Tag(); | |
104 | + tag1.name = "foo"; | |
105 | + tag1.save(); | |
106 | + project.tags.add(tag1); | |
107 | + project.update(); | |
108 | + Long tagId = tag1.id; | |
109 | + | |
110 | + assertThat(project.tags.contains(tag1)).isTrue(); | |
111 | + | |
112 | + Map<String,String> data = new HashMap<String,String>(); | |
113 | + data.put("_method", "DELETE"); | |
114 | + User admin = User.findByLoginId("admin"); | |
115 | + | |
116 | + //When | |
117 | + Result result = callAction( | |
118 | + controllers.routes.ref.ProjectApp.untag("hobi", "nForge4java", tagId), | |
119 | + fakeRequest() | |
120 | + .withFormUrlEncodedBody(data) | |
121 | + .withHeader("Accept", "application/json") | |
122 | + .withSession(UserApp.SESSION_USERID, admin.id.toString()) | |
123 | + ); | |
124 | + | |
125 | + //Then | |
126 | + assertThat(status(result)).isEqualTo(204); | |
127 | + assertThat(Project.findByNameAndOwner("hobi", "nForge4java").tags.contains(tag1)).isFalse(); | |
128 | + } | |
129 | + }); | |
130 | + } | |
131 | +} |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?