forked from Archives/Athou_commafeed
category formatting
This commit is contained in:
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<span> <input type="checkbox" checked="checked" />
|
<span> <input type="checkbox" checked="checked" />
|
||||||
<label ng-click="categoryClick({id: node.id})"
|
<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>
|
</label>
|
||||||
<ul>
|
<ul>
|
||||||
<li ng-repeat="feed in node.feeds">
|
<li ng-repeat="feed in node.feeds">
|
||||||
|
|||||||
@@ -12,14 +12,16 @@ module.controller('CategoryTreeCtrl',
|
|||||||
name : "News",
|
name : "News",
|
||||||
feeds : [ {
|
feeds : [ {
|
||||||
id : "2",
|
id : "2",
|
||||||
name : "Cyanide & Happiness"
|
name : "Cyanide & Happiness",
|
||||||
|
unread : 34
|
||||||
} ],
|
} ],
|
||||||
children : [ {
|
children : [ {
|
||||||
id : "2",
|
id : "2",
|
||||||
name : "Comics",
|
name : "Comics",
|
||||||
feeds : [ {
|
feeds : [ {
|
||||||
id : "1",
|
id : "1",
|
||||||
name : "Dilbert"
|
name : "Dilbert",
|
||||||
|
unread : 4
|
||||||
} ]
|
} ]
|
||||||
} ]
|
} ]
|
||||||
}, {
|
}, {
|
||||||
@@ -27,11 +29,35 @@ module.controller('CategoryTreeCtrl',
|
|||||||
name : "Blogs",
|
name : "Blogs",
|
||||||
feeds : [ {
|
feeds : [ {
|
||||||
id : "3",
|
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) {
|
$scope.feedClicked = function(id) {
|
||||||
$location.path('/feeds/view/feed/' + id);
|
$location.path('/feeds/view/feed/' + id);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -7,7 +7,8 @@ module.directive('category', function($compile) {
|
|||||||
selectedType: '=',
|
selectedType: '=',
|
||||||
selectedId: '=',
|
selectedId: '=',
|
||||||
feedClick: '&',
|
feedClick: '&',
|
||||||
categoryClick: '&'
|
categoryClick: '&',
|
||||||
|
formatCategoryName: '&'
|
||||||
},
|
},
|
||||||
restrict : 'E',
|
restrict : 'E',
|
||||||
replace: true,
|
replace: true,
|
||||||
@@ -15,7 +16,7 @@ module.directive('category', function($compile) {
|
|||||||
link: function(scope, element) {
|
link: function(scope, element) {
|
||||||
if (scope.node.children) {
|
if (scope.node.children) {
|
||||||
var ul = element.find('ul');
|
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);
|
$compile(ul.contents())(scope);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<div class="css-treeview">
|
<div class="css-treeview">
|
||||||
<ul>
|
<ul>
|
||||||
<li ng-repeat="node in root.children">
|
<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>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user