Skip to content

Commit

Permalink
Merge pull request thelounge#671 from stepie22/date-seperator
Browse files Browse the repository at this point in the history
Add a date separator to channels/PMs
  • Loading branch information
xPaw authored Nov 24, 2016
2 parents a6ad13a + a0afa48 commit 89a2f46
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 5 deletions.
27 changes: 27 additions & 0 deletions client/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,33 @@ button {
display: none;
}

#chat .date-marker {
position: relative;
text-align: center;
opacity: .5;
margin: 0 10px;
z-index: 0;
font-weight: bold;
font-size: 12px;
}

#chat .date-marker:before {
position: absolute;
z-index: -1;
content: "";
left: 0;
right: 0;
top: 50%;
border-top: 1px solid #006b3b;
}

#chat .date-marker-text:before {
content: attr(data-date);
background-color: white;
color: #006b3b;
padding: 0 10px;
}

.inline-channel {
cursor: pointer;
}
Expand Down
5 changes: 5 additions & 0 deletions client/js/libs/handlebars/localedate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";

Handlebars.registerHelper("localedate", function(time) {
return new Date(time).toLocaleDateString();
});
86 changes: 86 additions & 0 deletions client/js/lounge.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,27 @@ $(function() {
} else {
channel.append(render("unread_marker"));
}

if (data.type !== "lobby") {
var lastDate;
$(chat.find("#chan-" + data.id + " .messages .msg[data-time]")).each(function() {
var msg = $(this);
var msgDate = new Date(msg.attr("data-time"));

// Top-most message in a channel
if (!lastDate) {
lastDate = msgDate;
msg.before(render("date-marker", {msgDate: msgDate}));
}

if (lastDate.toDateString() !== msgDate.toDateString()) {
msg.before(render("date-marker", {msgDate: msgDate}));
}

lastDate = msgDate;
});
}

}

function renderChannelUsers(data) {
Expand Down Expand Up @@ -362,6 +383,21 @@ $(function() {
var target = "#chan-" + data.chan;
var container = chat.find(target + " .messages");

// Check if date changed
var prevMsg = $(container.find(".msg")).last();
var prevMsgTime = new Date(prevMsg.attr("data-time"));
var msgTime = new Date(msg.attr("data-time"));

// It's the first message in a channel/query
if (prevMsg.length === 0) {
container.append(render("date-marker", {msgDate: msgTime}));
}

if (prevMsgTime.toDateString() !== msgTime.toDateString()) {
prevMsg.append(render("date-marker", {msgDate: msgTime}));
}

// Add message to the container
container
.append(msg)
.trigger("msg", [
Expand All @@ -382,6 +418,13 @@ $(function() {
.find("#chan-" + data.chan)
.find(".messages");

// Remove the date-change marker we put at the top, because it may
// not actually be a date change now
var firstChild = $(chan).children().eq(0);
if (firstChild.attr("class") === "date-marker") {
firstChild.remove();
}

// get the scrollable wrapper around messages
var scrollable = chan.closest(".chat");
var heightOld = chan.height();
Expand All @@ -394,6 +437,28 @@ $(function() {
if (data.messages.length !== 100) {
scrollable.find(".show-more").removeClass("show");
}

// Date change detect
// Have to use data instaid of the documentFragment because it's being weird
var lastDate;
$(data.messages).each(function() {
var msgData = this;
var msgDate = new Date(msgData.time);
var msg = $(chat.find("#chan-" + data.chan + " .messages #msg-" + msgData.id));

// Top-most message in a channel
if (!lastDate) {
lastDate = msgDate;
msg.before(render("date-marker", {msgDate: msgDate}));
}

if (lastDate.toDateString() !== msgDate.toDateString()) {
msg.before(render("date-marker", {msgDate: msgDate}));
}

lastDate = msgDate;
});

});

socket.on("network", function(data) {
Expand Down Expand Up @@ -1210,13 +1275,34 @@ $(function() {
var chan = $(this);
if (chan.find(".messages .msg:not(.unread-marker)").slice(0, -100).remove().length) {
chan.find(".show-more").addClass("show");

// Remove date-seperators that would otherwise be "stuck" at the top
// of the channel
var prev;
$(chan.find(".messages").children()).each(function() {
if (!prev) {
// Should always be a date-seperator, because it's always added
prev = $(this);
} else {
var current = $(this);

if (current.attr("class") === "date-marker") {
prev.remove();
} else {
return false;
}

prev = current;
}
});
}
});
}, 1000 * 10);

function clear() {
chat.find(".active .messages .msg:not(.unread-marker)").remove();
chat.find(".active .show-more").addClass("show");
chat.find(".active .date-marker").remove();
}

function complete(word) {
Expand Down
12 changes: 11 additions & 1 deletion client/themes/morning.css
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,24 @@ body {
border-color: #333c4a;
}

#chat .unread-marker {
#chat .unread-marker,
#chat .date-marker {
opacity: 1;
}

#chat .unread-marker-text:before {
background-color: #333c4a;
}

#chat .date-marker:before {
border-color: #97ea70;
}

#chat .date-marker-text:before {
background-color: #333c4a;
color: #97ea70;
}

/* Setup text colors */
#chat .msg {
color: #f3f3f3;
Expand Down
12 changes: 11 additions & 1 deletion client/themes/zenburn.css
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,24 @@ body {
border-color: #3f3f3f;
}

#chat .unread-marker {
#chat .unread-marker,
.date-marker {
opacity: 1;
}

#chat .unread-marker-text:before {
background-color: #3f3f3f;
}

#chat .date-marker:before {
border-color: #97ea70;
}

#chat .date-marker-text:before {
background-color: #3f3f3f;
color: #97ea70;
}

/* Setup text colors */
#chat .msg {
color: #ffcfaf;
Expand Down
3 changes: 3 additions & 0 deletions client/views/date-marker.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="date-marker">
<span class="date-marker-text" data-date="{{localedate msgDate}}"></span>
</div>
2 changes: 1 addition & 1 deletion client/views/msg.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="msg {{type}}{{#if self}} self{{/if}}{{#if highlight}} highlight{{/if}}" id="msg-{{id}}">
<div class="msg {{type}}{{#if self}} self{{/if}}{{#if highlight}} highlight{{/if}}" id="msg-{{id}}" data-time="{{time}}">
<span class="time" title="{{localetime time}}">
{{tz time}}
</span>
Expand Down
2 changes: 1 addition & 1 deletion client/views/msg_action.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="msg {{type}}{{#if self}} self{{/if}}{{#if highlight}} highlight{{/if}}" id="msg-{{id}}">
<div class="msg {{type}}{{#if self}} self{{/if}}{{#if highlight}} highlight{{/if}}" id="msg-{{id}}" data-time="{{time}}">
<span class="time" title="{{localetime time}}">
{{tz time}}
</span>
Expand Down
2 changes: 1 addition & 1 deletion client/views/msg_unhandled.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="msg {{type}}{{#if self}} self{{/if}}{{#if highlight}} highlight{{/if}}">
<div class="msg {{type}}{{#if self}} self{{/if}}{{#if highlight}} highlight{{/if}}" data-time="{{time}}">
<span class="time" title="{{localetime time}}">
{{tz time}}
</span>
Expand Down

0 comments on commit 89a2f46

Please sign in to comment.