work in progress live unread / new counts

This commit is contained in:
Sam 2013-05-28 08:43:17 +10:00
parent 33ff87bf44
commit 33683715a9
8 changed files with 124 additions and 21 deletions

View File

@ -18,12 +18,12 @@ Discourse.ListController = Discourse.Controller.extend({
return Discourse.SiteSettings.top_menu.split("|").map(function(i) {
return Discourse.NavItem.fromText(i, {
loggedOn: loggedOn,
countSummary: summary
hasCategories: hasCategories
});
}).filter(function(i) {
return i !== null;
});
}.property('filterSummary'),
}.property(),
/**
Load a list based on a filter

View File

@ -10,6 +10,7 @@ var validNavNames = ['latest', 'hot', 'categories', 'category', 'favorited', 'un
var validAnon = ['latest', 'hot', 'categories', 'category'];
Discourse.NavItem = Discourse.Model.extend({
userTrackingStateBinding: Ember.Binding.oneWay('Discourse.currentUser.userTrackingState.messageCount'),
categoryName: function() {
var split = this.get('name').split('/');
return split[0] === 'category' ? split[1] : null;
@ -21,7 +22,14 @@ Discourse.NavItem = Discourse.Model.extend({
href = Discourse.getURL("/") + name.replace(' ', '-');
if (name === 'category') href += "/" + this.get('categoryName');
return href;
}.property('name')
}.property('name'),
count: function() {
var state = Discourse.get('currentUser.userTrackingState');
if (state) {
return state.lookupCount(this.get('name'));
}
}.property('userTrackingState')
});
Discourse.NavItem.reopenClass({
@ -43,7 +51,7 @@ Discourse.NavItem.reopenClass({
filters: split.splice(1)
};
if (countSummary && countSummary[name]) opts.count = countSummary[name];
// if (countSummary && countSummary[name]) opts.count = countSummary[name];
return Discourse.NavItem.create(opts);
}

View File

@ -118,7 +118,6 @@ Discourse.TopicList.reopenClass({
topics: Discourse.TopicList.topicsFrom(result),
can_create_topic: result.topic_list.can_create_topic,
more_topics_url: result.topic_list.more_topics_url,
filter_summary: result.topic_list.filter_summary,
draft_key: result.topic_list.draft_key,
draft_sequence: result.topic_list.draft_sequence,
draft: result.topic_list.draft,

View File

@ -314,6 +314,7 @@ Discourse.User.reopenClass({
});
},
/**
Checks if given username is valid for this email address

View File

@ -1,16 +1,80 @@
Discourse.UserTrackingState = Discourse.Model.extend({
unreadPosts: function(){
return 10;
}.property(),
init: function(){
this._super();
this.states = {};
newPosts: function() {
return 10;
}.property()
var _this = this;
setTimeout(function(){
console.log("YYYYYYYYYYY");
_this.loadStates([{
topic_id: 100,
last_read_post_number: null
}]);
_this.set('messageCount', 100);
}, 2000);
},
establishChannels: function() {
},
countNew: function(){
var count = 0;
$.each(this.states, function(){
count += this.last_read_post_number === null ? 1 : 0;
});
return count;
},
countUnread: function(){
var count = 0;
$.each(this.states, function(){
count += (this.last_read_post_number !== null &&
this.last_read_post_number < this.highest_post_number) ? 1 : 0;
});
return count;
},
countCategory: function(category) {
var count = 0;
$.each(this.states, function(){
if (this.category_name === category) {
count += (this.last_read_post_number === null ||
this.last_read_post_number < this.highest_post_number) ? 1 : 0;
}
});
return count;
},
lookupCount: function(name){
if(name==="new") {
return this.countNew();
} else if(name==="unread") {
return this.countUnread();
} else {
var category = name.split("/")[1];
if(category) {
return this.countCategory(category);
}
}
},
loadStates: function (data) {
// not exposed
var states = this.states;
data.each(function(row){
states["t" + row.topic_id] = row;
});
}
});
Discourse.UserTrackingState.reopenClass({
init: function(data){
createFromStates: function(data){
var instance = Discourse.UserTrackingState.create();
instance.loadStates(data);
instance.establishChannels();
return instance;
}
});

View File

@ -0,0 +1,24 @@
/**
The base Application route
@class ApplicationRoute
@extends Discourse.Route
@namespace Discourse
@module Discourse
**/
Discourse.ApplicationRoute = Discourse.Route.extend({
setupController: function(controller) {
Discourse.set('site', Discourse.Site.create(PreloadStore.get('site')));
var currentUser = PreloadStore.get('currentUser');
if (currentUser) {
var states = currentUser.user_tracking_states;
currentUser.user_tracking_states = null;
Discourse.set('currentUser', Discourse.User.create(currentUser));
Discourse.set('currentUser.userTrackingState', Discourse.UserTrackingState.createFromStates(states));
}
// make sure we delete preloaded data
PreloadStore.remove('site');
PreloadStore.remove('currentUser');
}
});

View File

@ -10,12 +10,12 @@ Discourse.NavItemView = Discourse.View.extend({
tagName: 'li',
classNameBindings: ['isActive', 'content.hasIcon:has-icon'],
attributeBindings: ['title'],
countBinding: Ember.Binding.oneWay('content.count'),
title: function() {
var name = this.get('content.name');
var categoryName = this.get('content.categoryName');
var extra;
var categoryName, extra, name;
name = this.get('content.name');
categoryName = this.get('content.categoryName');
if (categoryName) {
extra = { categoryName: categoryName };
name = "category";
@ -30,10 +30,17 @@ Discourse.NavItemView = Discourse.View.extend({
hidden: Em.computed.not('content.visible'),
countChanged: function(){
this.rerender();
}.observes('count'),
name: function() {
var name = this.get('content.name');
var categoryName = this.get('content.categoryName');
var extra = { count: this.get('content.count') || 0 };
var categoryName, extra, name;
name = this.get('content.name');
categoryName = this.get('content.categoryName');
extra = {
count: this.get('content.count') || 0
};
if (categoryName) {
name = 'category';
extra.categoryName = categoryName.titleize();

View File

@ -2,7 +2,7 @@ class TopicListSerializer < ApplicationSerializer
attributes :can_create_topic,
:more_topics_url,
:filter_summary,
# :filter_summary,
:draft,
:draft_key,
:draft_sequence,