diff --git a/src/main/java/com/commafeed/backend/dao/FeedCategoryDAO.java b/src/main/java/com/commafeed/backend/dao/FeedCategoryDAO.java index 6161b349..36817aa0 100644 --- a/src/main/java/com/commafeed/backend/dao/FeedCategoryDAO.java +++ b/src/main/java/com/commafeed/backend/dao/FeedCategoryDAO.java @@ -51,6 +51,13 @@ public class FeedCategoryDAO extends GenericDAO { return category; } + public List findByParent(User user, FeedCategory parent) { + EasyCriteria criteria = createCriteria(); + criteria.andEquals(FeedCategory_.user.getName(), user); + criteria.andEquals(FeedCategory_.parent.getName(), parent); + return criteria.getResultList(); + } + public List findAllChildrenCategories(User user, FeedCategory parent) { List list = Lists.newArrayList(); @@ -77,4 +84,5 @@ public class FeedCategoryDAO extends GenericDAO { } return isChild; } + } diff --git a/src/main/java/com/commafeed/frontend/model/request/CategoryModificationRequest.java b/src/main/java/com/commafeed/frontend/model/request/CategoryModificationRequest.java index d8e1c15b..7768f9dd 100644 --- a/src/main/java/com/commafeed/frontend/model/request/CategoryModificationRequest.java +++ b/src/main/java/com/commafeed/frontend/model/request/CategoryModificationRequest.java @@ -24,6 +24,9 @@ public class CategoryModificationRequest implements Serializable { @ApiProperty(value = "new parent category id") private String parentId; + @ApiProperty(value = "new display position, null if not changed") + private Integer position; + public Long getId() { return id; } @@ -48,4 +51,12 @@ public class CategoryModificationRequest implements Serializable { this.parentId = parentId; } + public Integer getPosition() { + return position; + } + + public void setPosition(Integer position) { + this.position = position; + } + } diff --git a/src/main/java/com/commafeed/frontend/model/request/FeedModificationRequest.java b/src/main/java/com/commafeed/frontend/model/request/FeedModificationRequest.java index 5a3c7825..b3025db7 100644 --- a/src/main/java/com/commafeed/frontend/model/request/FeedModificationRequest.java +++ b/src/main/java/com/commafeed/frontend/model/request/FeedModificationRequest.java @@ -24,6 +24,9 @@ public class FeedModificationRequest implements Serializable { @ApiProperty(value = "new parent category id") private String categoryId; + @ApiProperty(value = "new display position, null if not changed") + private Integer position; + public Long getId() { return id; } @@ -48,4 +51,12 @@ public class FeedModificationRequest implements Serializable { this.categoryId = categoryId; } + public Integer getPosition() { + return position; + } + + public void setPosition(Integer position) { + this.position = position; + } + } diff --git a/src/main/java/com/commafeed/frontend/rest/resources/CategoryREST.java b/src/main/java/com/commafeed/frontend/rest/resources/CategoryREST.java index 6fd24494..982180fc 100644 --- a/src/main/java/com/commafeed/frontend/rest/resources/CategoryREST.java +++ b/src/main/java/com/commafeed/frontend/rest/resources/CategoryREST.java @@ -266,6 +266,31 @@ public class CategoryREST extends AbstractResourceREST { Long.valueOf(req.getParentId())); } category.setParent(parent); + + if (req.getPosition() != null) { + List categories = feedCategoryDAO.findByParent( + getUser(), parent); + int existingIndex = -1; + for (int i = 0; i < categories.size(); i++) { + if (ObjectUtils.equals(categories.get(i).getId(), + category.getId())) { + existingIndex = i; + } + } + if (existingIndex != -1) { + categories.remove(existingIndex); + } + + categories.add(Math.min(req.getPosition(), categories.size()), + category); + for (int i = 0; i < categories.size(); i++) { + categories.get(i).setPosition(i); + } + feedCategoryDAO.update(categories); + } else { + feedCategoryDAO.update(category); + } + feedCategoryDAO.update(category); return Response.ok(Status.OK).build(); diff --git a/src/main/java/com/commafeed/frontend/rest/resources/FeedREST.java b/src/main/java/com/commafeed/frontend/rest/resources/FeedREST.java index 64d9d1f7..1a76dd2e 100644 --- a/src/main/java/com/commafeed/frontend/rest/resources/FeedREST.java +++ b/src/main/java/com/commafeed/frontend/rest/resources/FeedREST.java @@ -24,6 +24,7 @@ import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -327,7 +328,29 @@ public class FeedREST extends AbstractResourceREST { Long.valueOf(req.getCategoryId())); } subscription.setCategory(parent); - feedSubscriptionDAO.update(subscription); + + if (req.getPosition() != null) { + List subs = feedSubscriptionDAO.findByCategory( + getUser(), parent); + int existingIndex = -1; + for (int i = 0; i < subs.size(); i++) { + if (ObjectUtils.equals(subs.get(i).getId(), + subscription.getId())) { + existingIndex = i; + } + } + if (existingIndex != -1) { + subs.remove(existingIndex); + } + + subs.add(Math.min(req.getPosition(), subs.size()), subscription); + for (int i = 0; i < subs.size(); i++) { + subs.get(i).setPosition(i); + } + feedSubscriptionDAO.update(subs); + } else { + feedSubscriptionDAO.update(subscription); + } return Response.ok(Status.OK).build(); } diff --git a/src/main/webapp/js/directives.js b/src/main/webapp/js/directives.js index b9caf140..3553349b 100644 --- a/src/main/webapp/js/directives.js +++ b/src/main/webapp/js/directives.js @@ -364,13 +364,13 @@ module.directive('draggable', function() { restrict : 'A', link : function(scope, element, attrs) { element.draggable({ - revert: true + axis: 'y' }).data('source', scope.$eval(attrs.draggable)); } }; }); -module.directive('droppable', function($compile) { +module.directive('droppable', [ 'CategoryService', 'FeedService', function(CategoryService, FeedService) { return { restrict : 'A', link : function(scope, element, attrs) { @@ -382,15 +382,50 @@ module.directive('droppable', function($compile) { drop : function(event, ui) { var draggable = angular.element(ui.draggable); - - var index = draggable.data('index'); var source = draggable.data('source'); + var target = scope.$eval(attrs.droppable); - console.log(source) + var data = { + id: source.id, + name: source.name + }; + + if (source.children) { + // source is a category + if (target.children) { + // target is a category + data.parentId = target.id; + data.position = 0; + } else { + // target is a feed + data.parentId = target.categoryId; + } + + CategoryService.modify(data, function() { + CategoryService.init(); + }); + } else { + // source is a feed + + if (target.children) { + // target is a category + data.categoryId = target.id; + data.position = 0; + } else { + // target is a feed + data.categoryId = target.categoryId; + data.position = target.position; + } + + FeedService.modify(data, function() { + CategoryService.init(); + }); + } + scope.$apply(); } }); } }; -}); \ No newline at end of file +}]); \ No newline at end of file