* refactoring

* bugfix: Post to active channel/DM with mention when window isn't focused, I expect count badge appear but unread badge appear.
This commit is contained in:
Tatsuya Niwa
2016-01-26 00:35:59 +09:00
parent 95f393dc63
commit f69f1f6016
2 changed files with 98 additions and 111 deletions

View File

@@ -19,18 +19,71 @@ var unreadCountTimer = setInterval(function() {
var elem = document.getElementsByClassName('badge')
var mentionCount = 0;
for (var i = 0; i < elem.length; i++) {
if (elem[i].offsetHeight != 0) {
if (isElementVisible(elem[i])) {
mentionCount++;
}
}
if (this.unreadCount != unreadCount || this.mentionCount != mentionCount) {
ipc.sendToHost('onUnreadCountChange', unreadCount, mentionCount);
var postAttrName = 'data-reactid';
var lastPostElem = document.querySelector('div[' + postAttrName + '="' + lastCheckedPost.reactId + '"]');
var isUnread = false;
var isMentioned = false;
if (lastPostElem === null || !isElementVisible(lastPostElem)) {
// When load channel or change channel, lastCheckedPost.reactId is invalid.
// So we get latest post and save lastCheckedPost.
// find active post-list.
var postLists = document.querySelectorAll('div.post-list__content');
var post;
for (var i = 0; i < postLists.length; i++) {
if (isElementVisible(postLists[i])) {
post = postLists[i].children[0];
}
}
// find latest post and save.
while (post = post.nextSibling) {
if (post.nextSibling === null) {
if (post.getAttribute(postAttrName) !== null) {
lastCheckedPost.reactId = post.getAttribute(postAttrName);
}
}
}
}
else if (lastPostElem !== null) {
var newPostElem = lastPostElem;
while (newPostElem = newPostElem.nextSibling) {
lastCheckedPost.reactId = newPostElem.getAttribute(postAttrName);
isUnread = true;
var activeChannel = document.querySelector('.active .sidebar-channel');
var closeButton = activeChannel.getElementsByClassName('btn-close');
if (closeButton.length === 1 && closeButton[0].getAttribute('aria-describedby') === 'remove-dm-tooltip') {
// If active channel is DM, all posts is treated as menion.
isMentioned = true;
break;
}
else {
// If active channel is public/private channel, only mentioned post is treated as mention.
var highlight = newPostElem.getElementsByClassName('mention-highlight');
if (highlight.length != 0 && isElementVisible(highlight[0])) {
isMentioned = true;
break;
}
}
}
}
if (this.unreadCount != unreadCount || this.mentionCount != mentionCount || isUnread || isMentioned) {
ipc.sendToHost('onUnreadCountChange', unreadCount, mentionCount, isUnread, isMentioned);
}
this.unreadCount = unreadCount;
this.mentionCount = mentionCount;
}, 1000);
function isElementVisible(elem) {
return elem.offsetHeight !== 0;
}
// On Windows 8.1 and Windows 8, a shortcut with a Application User Model ID must be installed to the Start screen.
// In current version, use tray balloon for notification
function isLowerThanOrEqualWindows8_1() {
@@ -48,12 +101,6 @@ function overrideNotificationWithBalloon() {
title: title,
options: options
});
// Send notification event at active channel.
var activeChannel = document.querySelector('.active .sidebar-channel').text;
if (activeChannel === title) {
ipc.sendToHost('onActiveChannelNotify');
}
};
Notification.requestPermission = function(callback) {
callback('granted');
@@ -61,69 +108,14 @@ function overrideNotificationWithBalloon() {
Notification.prototype.close = function() {};
};
var lastUnread = {};
var lastCheckedPost = {
reactId: null
};
// Show window even if it is hidden/minimized when notification is clicked.
function overrideNotification() {
Notification = function(title, options) {
this.notification = new NativeNotification(title, options);
// Send notification event at active channel.
var activeChannel = document.querySelector('.active .sidebar-channel').text;
console.log(activeChannel);
console.log(title);
// mentionCount for active channel
var newSeparators = document.getElementsByClassName('new-separator');
var post;
var isMentioned = false;
// Skip until real new-separator appear.
for (var i = 0; i < newSeparators.length; i++) {
if (newSeparators[i].offsetParent !== null) {
post = newSeparators[i];
}
}
// If active channel is DM, all posts is treated as menion.
if (activeChannel === title + "×") {
isMentioned = true;
}
else {
// If active channel is CHANNEL, only .mention-highlight post is treated as mention.
if (post != null) {
// Skip posts until last unread.
if (activeChannel === title && lastUnread.channel === title && lastUnread.post !== null) {
var firstPost = post;
while (post = post.nextSibling) {
if (lastUnread.post === post.getAttribute('data-reactid')) {
break;
}
}
// Because last unread post not found, set first post.
if (post === null) {
post = firstPost;
}
}
while (post = post.nextSibling) {
var highlight = post.getElementsByClassName('mention-highlight');
if (highlight.length != 0 && highlight[0].offsetHeight != null) {
isMentioned = true;
}
// Remember last unread post.
if (post.nextSibling === null) {
lastUnread.post = post.getAttribute('data-reactid');
lastUnread.channel = title;
}
}
}
}
// Note: DM title is "{username}×". CHANNEL title is "{channel_title}".
if (activeChannel === title || activeChannel === title + "×") {
ipc.sendToHost('onActiveChannelNotify', isMentioned);
}
};
Notification.requestPermission = function(callback) {
callback('granted');