category formatting

This commit is contained in:
Athou
2013-03-21 23:10:05 +01:00
parent 90289a8cf7
commit 42f9b38f68
5 changed files with 173 additions and 7 deletions

View File

@@ -0,0 +1,139 @@
package com.commafeed.frontend.rest;
import java.util.List;
import javax.inject.Inject;
import org.apache.commons.lang.ObjectUtils;
import com.commafeed.backend.dao.FeedCategoryService;
import com.commafeed.backend.dao.FeedSubscriptionService;
import com.commafeed.frontend.CommaFeedSession;
import com.commafeed.frontend.pages.JSONPage;
import com.commafeed.frontend.utils.ModelFactory.MF;
import com.commafeed.model.FeedCategory;
import com.commafeed.model.FeedSubscription;
import com.commafeed.model.User;
import com.google.common.collect.Lists;
@SuppressWarnings("serial")
public class FeedSubscriptionsREST extends JSONPage {
@Inject
FeedSubscriptionService feedSubscriptionService;
@Inject
FeedCategoryService FeedCategoryService;
@Override
protected Object getObject() {
User user = CommaFeedSession.get().getUser();
List<FeedCategory> categories = FeedCategoryService.findAll(user);
Category root = new Category();
addChildren(categories, root);
for (FeedSubscription subscription : feedSubscriptionService
.findByField(MF.i(MF.p(FeedSubscription.class).getCategory()),
null)) {
Subscription sub = new Subscription();
sub.setId(subscription.getId());
sub.setName(subscription.getTitle());
sub.setUnread(77);
}
return root;
}
private void addChildren(List<FeedCategory> categories, Category current) {
for (FeedCategory category : categories) {
if ((category.getParent() == null && current.getId() == null)
|| (category.getParent() != null && (ObjectUtils.equals(
category.getParent().getId(), current.getId())))) {
Category child = new Category();
child.setId(category.getId());
child.setName(category.getName());
addChildren(categories, child);
for (FeedSubscription subscription : category
.getSubscriptions()) {
Subscription sub = new Subscription();
sub.setId(subscription.getId());
sub.setName(subscription.getTitle());
sub.setUnread(77);
}
current.getChildren().add(child);
}
}
}
public static class Category {
private Long id;
private String name;
private List<Category> children = Lists.newArrayList();
private List<Subscription> feeds = Lists.newArrayList();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Category> getChildren() {
return children;
}
public void setChildren(List<Category> children) {
this.children = children;
}
public List<Subscription> getFeeds() {
return feeds;
}
public void setFeeds(List<Subscription> feeds) {
this.feeds = feeds;
}
}
public static class Subscription {
private Long id;
private String name;
private int unread;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getUnread() {
return unread;
}
public void setUnread(int unread) {
this.unread = unread;
}
}
}

View File

@@ -1,6 +1,6 @@
<span> <input type="checkbox" checked="checked" />
<label ng-click="categoryClick({id: node.id})"
ng-class="{selected: (node.id == selectedId && selectedType == 'category')}">{{node.name}}
ng-class="{selected: (node.id == selectedId && selectedType == 'category')}">{{formatCategoryName({category:node})}}
</label>
<ul>
<li ng-repeat="feed in node.feeds">

View File

@@ -12,14 +12,16 @@ module.controller('CategoryTreeCtrl',
name : "News",
feeds : [ {
id : "2",
name : "Cyanide & Happiness"
name : "Cyanide & Happiness",
unread : 34
} ],
children : [ {
id : "2",
name : "Comics",
feeds : [ {
id : "1",
name : "Dilbert"
name : "Dilbert",
unread : 4
} ]
} ]
}, {
@@ -27,11 +29,35 @@ module.controller('CategoryTreeCtrl',
name : "Blogs",
feeds : [ {
id : "3",
name : "Engadget"
name : "Engadget",
unread : 0
} ]
} ]
};
var unreadCount = function(category) {
var count = 0;
console.log(category)
for ( var child in category.children) {
count = count + unreadCount(child);
}
for ( var feed in category.feeds) {
if (feed.unread) {
count = count + feed.unread;
}
}
return count;
}
$scope.formatCategoryName = function(category) {
var count = unreadCount(category);
var label = category.name;
if (count > 0) {
label = label + " (" + count + ")";
}
return label;
}
$scope.feedClicked = function(id) {
$location.path('/feeds/view/feed/' + id);
};

View File

@@ -7,7 +7,8 @@ module.directive('category', function($compile) {
selectedType: '=',
selectedId: '=',
feedClick: '&',
categoryClick: '&'
categoryClick: '&',
formatCategoryName: '&'
},
restrict : 'E',
replace: true,
@@ -15,7 +16,7 @@ module.directive('category', function($compile) {
link: function(scope, element) {
if (scope.node.children) {
var ul = element.find('ul');
ul.prepend('<category ng-repeat="child in node.children" node="child" feed-click="feedClick({id:id})" category-click="categoryClick({id:id})" selected-type="selectedType" selected-id="selectedId"></category>');
ul.prepend('<category ng-repeat="child in node.children" node="child" feed-click="feedClick({id:id})" category-click="categoryClick({id:id})" selected-type="selectedType" selected-id="selectedId" format-category-name="formatCategoryName({category:category})"></category>');
$compile(ul.contents())(scope);
}
}

View File

@@ -4,7 +4,7 @@
<div class="css-treeview">
<ul>
<li ng-repeat="node in root.children">
<category node="node" feed-click="feedClicked(id)" category-click="categoryClicked(id)" selected-type="selectedType" selected-id="selectedId"></category>
<category node="node" feed-click="feedClicked(id)" category-click="categoryClicked(id)" selected-type="selectedType" selected-id="selectedId" format-category-name="formatCategoryName(category)"></category>
</li>
</ul>
</div>