Remove duplicated `NotificationType` globals

This commit is contained in:
Robin Ward 2015-02-26 12:52:30 -05:00
parent be9feeb918
commit 3d3b70f4bb
9 changed files with 82 additions and 80 deletions

View File

@ -3,12 +3,9 @@ import NotificationsButton from 'discourse/components/notifications-button';
export default NotificationsButton.extend({ export default NotificationsButton.extend({
classNames: ['notification-options', 'category-notification-menu'], classNames: ['notification-options', 'category-notification-menu'],
buttonIncludesText: false, buttonIncludesText: false,
longDescriptionBinding: null,
hidden: Em.computed.alias('category.deleted'), hidden: Em.computed.alias('category.deleted'),
notificationLevels: Discourse.Category.NotificationLevel,
notificationLevel: Em.computed.alias('category.notification_level'), notificationLevel: Em.computed.alias('category.notification_level'),
i18nPrefix: 'category.notifications', i18nPrefix: 'category.notifications',
i18nPostfix: '',
clicked(id) { clicked(id) {
this.get('category').setNotification(id); this.get('category').setNotification(id);

View File

@ -1,11 +1,11 @@
import DropdownButton from 'discourse/components/dropdown-button'; import DropdownButton from 'discourse/components/dropdown-button';
import NotificationLevels from 'discourse/lib/notification-levels';
export default DropdownButton.extend({ const NotificationsButton = DropdownButton.extend({
classNames: ['notification-options'], classNames: ['notification-options'],
title: '', title: '',
buttonIncludesText: true, buttonIncludesText: true,
activeItem: Em.computed.alias('notificationLevel'), activeItem: Em.computed.alias('notificationLevel'),
notificationLevels: [],
i18nPrefix: '', i18nPrefix: '',
i18nPostfix: '', i18nPostfix: '',
watchingClasses: 'fa fa-exclamation-circle watching', watchingClasses: 'fa fa-exclamation-circle watching',
@ -23,13 +23,12 @@ export default DropdownButton.extend({
dropDownContent: function() { dropDownContent: function() {
const contents = [], const contents = [],
prefix = this.get('i18nPrefix'), prefix = this.get('i18nPrefix'),
postfix = this.get('i18nPostfix'), postfix = this.get('i18nPostfix');
levels = this.get('notificationLevels');
_.each(this.get('options'), function(pair) { _.each(this.get('options'), function(pair) {
if (postfix === '_pm' && pair[1] === 'regular') { return; } if (postfix === '_pm' && pair[1] === 'regular') { return; }
contents.push({ contents.push({
id: levels[pair[0]], id: NotificationLevels[pair[0]],
title: I18n.t(prefix + '.' + pair[1] + postfix + '.title'), title: I18n.t(prefix + '.' + pair[1] + postfix + '.title'),
description: I18n.t(prefix + '.' + pair[1] + postfix + '.description'), description: I18n.t(prefix + '.' + pair[1] + postfix + '.description'),
styleClasses: pair[2] styleClasses: pair[2]
@ -42,14 +41,13 @@ export default DropdownButton.extend({
text: function() { text: function() {
const self = this, const self = this,
prefix = this.get('i18nPrefix'), prefix = this.get('i18nPrefix'),
postfix = this.get('i18nPostfix'), postfix = this.get('i18nPostfix');
levels = this.get('notificationLevels');
const key = (function() { const key = (function() {
switch (this.get('notificationLevel')) { switch (this.get('notificationLevel')) {
case levels.WATCHING: return 'watching'; case NotificationLevels.WATCHING: return 'watching';
case levels.TRACKING: return 'tracking'; case NotificationLevels.TRACKING: return 'tracking';
case levels.MUTED: return 'muted'; case NotificationLevels.MUTED: return 'muted';
default: return 'regular'; default: return 'regular';
} }
}).call(this); }).call(this);
@ -70,3 +68,6 @@ export default DropdownButton.extend({
} }
}); });
export default NotificationsButton;
export { NotificationLevels };

View File

@ -1,17 +1,14 @@
import NotificationsButton from 'discourse/components/notifications-button'; import NotificationsButton from 'discourse/components/notifications-button';
export default NotificationsButton.extend({ export default NotificationsButton.extend({
longDescriptionBinding: 'topic.details.notificationReasonText', longDescription: Em.computed.alias('topic.details.notificationReasonText'),
target: Em.computed.alias('topic'),
hidden: Em.computed.alias('topic.deleted'), hidden: Em.computed.alias('topic.deleted'),
notificationLevels: Discourse.Topic.NotificationLevel,
notificationLevel: Em.computed.alias('topic.details.notification_level'), notificationLevel: Em.computed.alias('topic.details.notification_level'),
isPrivateMessage: Em.computed.alias('topic.isPrivateMessage'),
i18nPrefix: 'topic.notifications', i18nPrefix: 'topic.notifications',
i18nPostfix: function() { i18nPostfix: function() {
return this.get('isPrivateMessage') ? '_pm' : ''; return this.get('topic.isPrivateMessage') ? '_pm' : '';
}.property('isPrivateMessage'), }.property('topic.isPrivateMessage'),
clicked(id) { clicked(id) {
this.get('topic.details').updateNotifications(id); this.get('topic.details').updateNotifications(id);

View File

@ -1,3 +1,5 @@
import NotificationLevels from 'discourse/lib/notification-levels';
// Support for changing the notification level of various topics // Support for changing the notification level of various topics
export default Em.Controller.extend({ export default Em.Controller.extend({
needs: ['topic-bulk-actions'], needs: ['topic-bulk-actions'],
@ -5,9 +7,9 @@ export default Em.Controller.extend({
notificationLevels: function() { notificationLevels: function() {
var result = []; var result = [];
Object.keys(Discourse.Topic.NotificationLevel).forEach(function(k) { Object.keys(NotificationLevels).forEach(function(k) {
result.push({ result.push({
id: Discourse.Topic.NotificationLevel[k].toString(), id: NotificationLevels[k].toString(),
name: I18n.t('topic.notifications.' + k.toLowerCase() + ".title"), name: I18n.t('topic.notifications.' + k.toLowerCase() + ".title"),
description: I18n.t('topic.notifications.' + k.toLowerCase() + ".description") description: I18n.t('topic.notifications.' + k.toLowerCase() + ".description")
}); });

View File

@ -1,5 +1,6 @@
import DiscoveryController from 'discourse/controllers/discovery'; import DiscoveryController from 'discourse/controllers/discovery';
import { queryParams } from 'discourse/controllers/discovery-sortable'; import { queryParams } from 'discourse/controllers/discovery-sortable';
import NotificationLevels from 'discourse/lib/notification-levels';
var controllerOpts = { var controllerOpts = {
needs: ['discovery'], needs: ['discovery'],
@ -86,7 +87,7 @@ var controllerOpts = {
operation = { type: 'dismiss_posts' }; operation = { type: 'dismiss_posts' };
} else { } else {
operation = { type: 'change_notification_level', operation = { type: 'change_notification_level',
notification_level_id: Discourse.Topic.NotificationLevel.REGULAR }; notification_level_id: NotificationLevels.REGULAR };
} }
var promise; var promise;

View File

@ -0,0 +1,6 @@
export default {
WATCHING: 3,
TRACKING: 2,
REGULAR: 1,
MUTED: 0
};

View File

@ -192,13 +192,6 @@ var _uncategorized;
Discourse.Category.reopenClass({ Discourse.Category.reopenClass({
NotificationLevel: {
WATCHING: 3,
TRACKING: 2,
REGULAR: 1,
MUTED: 0
},
findUncategorized: function() { findUncategorized: function() {
_uncategorized = _uncategorized || Discourse.Category.list().findBy('id', Discourse.Site.currentProp('uncategorized_category_id')); _uncategorized = _uncategorized || Discourse.Category.list().findBy('id', Discourse.Site.currentProp('uncategorized_category_id'));
return _uncategorized; return _uncategorized;

View File

@ -1,16 +1,18 @@
function isNew(topic){ import NotificationLevels from 'discourse/lib/notification-levels';
function isNew(topic) {
return topic.last_read_post_number === null && return topic.last_read_post_number === null &&
((topic.notification_level !== 0 && !topic.notification_level) || ((topic.notification_level !== 0 && !topic.notification_level) ||
topic.notification_level >= Discourse.Topic.NotificationLevel.TRACKING); topic.notification_level >= NotificationLevels.TRACKING);
} }
function isUnread(topic){ function isUnread(topic) {
return topic.last_read_post_number !== null && return topic.last_read_post_number !== null &&
topic.last_read_post_number < topic.highest_post_number && topic.last_read_post_number < topic.highest_post_number &&
topic.notification_level >= Discourse.Topic.NotificationLevel.TRACKING; topic.notification_level >= NotificationLevels.TRACKING;
} }
Discourse.TopicTrackingState = Discourse.Model.extend({ const TopicTrackingState = Discourse.Model.extend({
messageCount: 0, messageCount: 0,
_setup: function() { _setup: function() {
@ -19,17 +21,17 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
this.states = {}; this.states = {};
}.on('init'), }.on('init'),
establishChannels: function() { establishChannels() {
var tracker = this; const tracker = this;
var process = function(data){ const process = function(data){
if (data.message_type === "delete") { if (data.message_type === "delete") {
tracker.removeTopic(data.topic_id); tracker.removeTopic(data.topic_id);
tracker.incrementMessageCount(); tracker.incrementMessageCount();
} }
if (data.message_type === "new_topic" || data.message_type === "latest") { if (data.message_type === "new_topic" || data.message_type === "latest") {
var ignored_categories = Discourse.User.currentProp("muted_category_ids"); const ignored_categories = Discourse.User.currentProp("muted_category_ids");
if(_.include(ignored_categories, data.payload.category_id)){ if(_.include(ignored_categories, data.payload.category_id)){
return; return;
} }
@ -41,7 +43,7 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
if (data.message_type === "new_topic" || data.message_type === "unread" || data.message_type === "read") { if (data.message_type === "new_topic" || data.message_type === "unread" || data.message_type === "read") {
tracker.notify(data); tracker.notify(data);
var old = tracker.states["t" + data.topic_id]; const old = tracker.states["t" + data.topic_id];
if(!_.isEqual(old, data.payload)){ if(!_.isEqual(old, data.payload)){
tracker.states["t" + data.topic_id] = data.payload; tracker.states["t" + data.topic_id] = data.payload;
@ -52,32 +54,32 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
Discourse.MessageBus.subscribe("/new", process); Discourse.MessageBus.subscribe("/new", process);
Discourse.MessageBus.subscribe("/latest", process); Discourse.MessageBus.subscribe("/latest", process);
var currentUser = Discourse.User.current(); const currentUser = Discourse.User.current();
if(currentUser) { if(currentUser) {
Discourse.MessageBus.subscribe("/unread/" + currentUser.id, process); Discourse.MessageBus.subscribe("/unread/" + currentUser.id, process);
} }
}, },
updateSeen: function(topicId, highestSeen) { updateSeen(topicId, highestSeen) {
if(!topicId || !highestSeen) { return; } if(!topicId || !highestSeen) { return; }
var state = this.states["t" + topicId]; const state = this.states["t" + topicId];
if(state && (!state.last_read_post_number || state.last_read_post_number < highestSeen)) { if(state && (!state.last_read_post_number || state.last_read_post_number < highestSeen)) {
state.last_read_post_number = highestSeen; state.last_read_post_number = highestSeen;
this.incrementMessageCount(); this.incrementMessageCount();
} }
}, },
notify: function(data){ notify(data){
if (!this.newIncoming) { return; } if (!this.newIncoming) { return; }
var filter = this.get("filter"); const filter = this.get("filter");
if ((filter === "all" || filter === "latest" || filter === "new") && data.message_type === "new_topic" ) { if ((filter === "all" || filter === "latest" || filter === "new") && data.message_type === "new_topic" ) {
this.addIncoming(data.topic_id); this.addIncoming(data.topic_id);
} }
if ((filter === "all" || filter === "unread") && data.message_type === "unread") { if ((filter === "all" || filter === "unread") && data.message_type === "unread") {
var old = this.states["t" + data.topic_id]; const old = this.states["t" + data.topic_id];
if(!old || old.highest_post_number === old.last_read_post_number) { if(!old || old.highest_post_number === old.last_read_post_number) {
this.addIncoming(data.topic_id); this.addIncoming(data.topic_id);
} }
@ -90,47 +92,47 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
this.set("incomingCount", this.newIncoming.length); this.set("incomingCount", this.newIncoming.length);
}, },
addIncoming: function(topicId) { addIncoming(topicId) {
if(this.newIncoming.indexOf(topicId) === -1){ if(this.newIncoming.indexOf(topicId) === -1){
this.newIncoming.push(topicId); this.newIncoming.push(topicId);
} }
}, },
resetTracking: function(){ resetTracking(){
this.newIncoming = []; this.newIncoming = [];
this.set("incomingCount", 0); this.set("incomingCount", 0);
}, },
// track how many new topics came for this filter // track how many new topics came for this filter
trackIncoming: function(filter) { trackIncoming(filter) {
this.newIncoming = []; this.newIncoming = [];
this.set("filter", filter); this.set("filter", filter);
this.set("incomingCount", 0); this.set("incomingCount", 0);
}, },
hasIncoming: function(){ hasIncoming: function(){
var count = this.get('incomingCount'); const count = this.get('incomingCount');
return count && count > 0; return count && count > 0;
}.property('incomingCount'), }.property('incomingCount'),
removeTopic: function(topic_id) { removeTopic(topic_id) {
delete this.states["t" + topic_id]; delete this.states["t" + topic_id];
}, },
// If we have a cached topic list, we can update it from our tracking // If we have a cached topic list, we can update it from our tracking
// information. // information.
updateTopics: function(topics) { updateTopics(topics) {
if (Em.isEmpty(topics)) { return; } if (Em.isEmpty(topics)) { return; }
var states = this.states; const states = this.states;
topics.forEach(function(t) { topics.forEach(function(t) {
var state = states['t' + t.get('id')]; const state = states['t' + t.get('id')];
if (state) { if (state) {
var lastRead = t.get('last_read_post_number'); const lastRead = t.get('last_read_post_number');
if (lastRead !== state.last_read_post_number) { if (lastRead !== state.last_read_post_number) {
var postsCount = t.get('posts_count'), const postsCount = t.get('posts_count');
newPosts = postsCount - state.highest_post_number, let newPosts = postsCount - state.highest_post_number,
unread = postsCount - state.last_read_post_number; unread = postsCount - state.last_read_post_number;
if (newPosts < 0) { newPosts = 0; } if (newPosts < 0) { newPosts = 0; }
@ -151,16 +153,16 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
}); });
}, },
sync: function(list, filter) { sync(list, filter) {
var tracker = this, const tracker = this,
states = tracker.states; states = tracker.states;
if (!list || !list.topics) { return; } if (!list || !list.topics) { return; }
// compensate for delayed "new" topics // compensate for delayed "new" topics
// client side we know they are not new, server side we think they are // client side we know they are not new, server side we think they are
for (var i=list.topics.length-1; i>=0; i--) { for (let i=list.topics.length-1; i>=0; i--) {
var state = states["t"+ list.topics[i].id]; const state = states["t"+ list.topics[i].id];
if (state && state.last_read_post_number > 0) { if (state && state.last_read_post_number > 0) {
if (filter === "new") { if (filter === "new") {
list.topics.splice(i, 1); list.topics.splice(i, 1);
@ -172,7 +174,7 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
} }
list.topics.forEach(function(topic){ list.topics.forEach(function(topic){
var row = tracker.states["t" + topic.id] || {}; const row = tracker.states["t" + topic.id] || {};
row.topic_id = topic.id; row.topic_id = topic.id;
row.notification_level = topic.notification_level; row.notification_level = topic.notification_level;
@ -199,7 +201,7 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
// Correct missing states, safeguard in case message bus is corrupt // Correct missing states, safeguard in case message bus is corrupt
if((filter === "new" || filter === "unread") && !list.more_topics_url){ if((filter === "new" || filter === "unread") && !list.more_topics_url){
var ids = {}; const ids = {};
list.topics.forEach(function(r){ list.topics.forEach(function(r){
ids["t" + r.id] = true; ids["t" + r.id] = true;
}); });
@ -224,11 +226,11 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
this.incrementMessageCount(); this.incrementMessageCount();
}, },
incrementMessageCount: function() { incrementMessageCount() {
this.set("messageCount", this.get("messageCount") + 1); this.set("messageCount", this.get("messageCount") + 1);
}, },
countNew: function(category_id){ countNew(category_id){
return _.chain(this.states) return _.chain(this.states)
.where(isNew) .where(isNew)
.where(function(topic){ return topic.category_id === category_id || !category_id;}) .where(function(topic){ return topic.category_id === category_id || !category_id;})
@ -236,8 +238,8 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
.length; .length;
}, },
resetNew: function() { resetNew() {
var self = this; const self = this;
Object.keys(this.states).forEach(function (id) { Object.keys(this.states).forEach(function (id) {
if (self.states[id].last_read_post_number === null) { if (self.states[id].last_read_post_number === null) {
delete self.states[id]; delete self.states[id];
@ -245,7 +247,7 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
}); });
}, },
countUnread: function(category_id){ countUnread(category_id){
return _.chain(this.states) return _.chain(this.states)
.where(isUnread) .where(isUnread)
.where(function(topic){ return topic.category_id === category_id || !category_id;}) .where(function(topic){ return topic.category_id === category_id || !category_id;})
@ -253,19 +255,19 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
.length; .length;
}, },
countCategory: function(category_id) { countCategory(category_id) {
var count = 0; let sum = 0;
_.each(this.states, function(topic){ _.each(this.states, function(topic){
if (topic.category_id === category_id) { if (topic.category_id === category_id) {
count += (topic.last_read_post_number === null || sum += (topic.last_read_post_number === null ||
topic.last_read_post_number < topic.highest_post_number) ? 1 : 0; topic.last_read_post_number < topic.highest_post_number) ? 1 : 0;
} }
}); });
return count; return sum;
}, },
lookupCount: function(name, category){ lookupCount(name, category){
var categoryName = category ? Em.get(category, "name") : null; let categoryName = category ? Em.get(category, "name") : null;
if(name === "new") { if(name === "new") {
return this.countNew(categoryName); return this.countNew(categoryName);
} else if(name === "unread") { } else if(name === "unread") {
@ -277,9 +279,9 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
} }
} }
}, },
loadStates: function (data) { loadStates(data) {
// not exposed // not exposed
var states = this.states; const states = this.states;
if(data) { if(data) {
_.each(data,function(topic){ _.each(data,function(topic){
@ -290,19 +292,21 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
}); });
Discourse.TopicTrackingState.reopenClass({ TopicTrackingState.reopenClass({
createFromStates: function(data){ createFromStates(data){
var instance = Discourse.TopicTrackingState.create(); const instance = Discourse.TopicTrackingState.create();
instance.loadStates(data); instance.loadStates(data);
instance.establishChannels(); instance.establishChannels();
return instance; return instance;
}, },
current: function(){ current(){
if (!this.tracker) { if (!this.tracker) {
var data = PreloadStore.get('topicTrackingStates'); const data = PreloadStore.get('topicTrackingStates');
this.tracker = this.createFromStates(data); this.tracker = this.createFromStates(data);
PreloadStore.remove('topicTrackingStates'); PreloadStore.remove('topicTrackingStates');
} }
return this.tracker; return this.tracker;
} }
}); });
export default TopicTrackingState;

View File

@ -10,6 +10,7 @@
// //
// Stuff we need to load first // Stuff we need to load first
//= require ./discourse/lib/notification-levels
//= require ./discourse/lib/app-events //= require ./discourse/lib/app-events
//= require ./discourse/helpers/i18n //= require ./discourse/helpers/i18n
//= require ./discourse/helpers/fa-icon //= require ./discourse/helpers/fa-icon