Permalink
Browse files

improve message date handling and formatting

  • Loading branch information...
1 parent f598194 commit cad9aaea71c4adcd57608aff46ccb08edd3b512e @dbecher dbecher committed Mar 9, 2013
Showing with 28 additions and 28 deletions.
  1. +19 −1 js/app.js
  2. +6 −18 js/templates.js
  3. +3 −9 templates/message.html
View
20 js/app.js
@@ -18,6 +18,8 @@
},
initialize: function() {
+ // store the last date header we rendered so we know when to show another one
+ this.lastDate = null;
this.listenTo(this.collection, 'reset', this.render);
this.listenTo(this.collection, 'add', this.addMessage);
if(!this.collection.hasUsername()) this.settings();
@@ -51,9 +53,25 @@
}
},
renderMessage: function(message) {
+ var dateString;
+ if(!this.lastDate || message.get('timestamp') - this.lastDate > 300000) {
+ console.log('ts');
+ this.lastDate = message.get('timestamp');
+ dateString = '';
+ var date = new Date(message.get('timestamp'));
+ var now = new Date();
+ if(date.getDate() === now.getDate() && date.getMonth() === now.getMonth() && date.getFullYear() === now.getFullYear()) {
+ dateString = 'Today';
+ } else {
+ dateString = (date.getMonth()+1) + '/' + date.getDate() + '/' + date.getFullYear();
+ }
+ dateString += ' ' + (date.getHours() > 12 ? (date.getHours()-12) : date.getHours());
+ dateString += ':' + (date.getMinutes() < 10 ? ('0'+date.getMinutes()) : date.getMinutes());
+ dateString += date.getHours() > 12 ? ' PM' : ' AM';
+ }
return Templates.message(_.extend(message.toJSON(), {
cid: message.cid,
- date: new Date(message.get('timestamp'))
+ date: dateString
}));
},
View
24 js/templates.js
@@ -3,25 +3,13 @@
var Templates = exports.Templates;
Templates["message"] = function(data){
var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
-__p+='<p class="chat-message-date">\n ';
- var now = new Date();
-__p+='\n ';
- if(data.date.getDate() === now.getDate() && data.date.getMonth() === now.getMonth() && data.date.getFullYear() === now.getFullYear()) {
-__p+='\n Today\n ';
- } else {
-__p+='\n '+
-((__t=( data.date.getMonth()+1 ))==null?'':__t)+
-'/'+
-((__t=( data.date.getDate() ))==null?'':__t)+
-'/'+
-((__t=( data.date.getFullYear() ))==null?'':__t)+
-'\n ';
+__p+='';
+ if(data.date) {
+__p+='\n <p class="chat-message-date">'+
+((__t=( data.date ))==null?'':__t)+
+'</p>\n';
}
-__p+='\n '+
-((__t=( data.date.getHours() ))==null?'':__t)+
-':'+
-((__t=( data.date.getMinutes() ))==null?'':__t)+
-'\n</p>\n<li class="chat-message '+
+__p+='\n<li class="chat-message '+
((__t=( (data.mine ? 'chat-message-mine' : '') ))==null?'':__t)+
'" data-message-id="'+
((__t=( data.cid ))==null?'':__t)+
View
12 templates/message.html
@@ -1,12 +1,6 @@
-<p class="chat-message-date">
- <% var now = new Date(); %>
- <% if(data.date.getDate() === now.getDate() && data.date.getMonth() === now.getMonth() && data.date.getFullYear() === now.getFullYear()) { %>
- Today
- <% } else { %>
- <%= data.date.getMonth()+1 %>/<%= data.date.getDate() %>/<%= data.date.getFullYear() %>
- <% } %>
- <%= data.date.getHours() %>:<%= data.date.getMinutes() %>
-</p>
+<% if(data.date) { %>
+ <p class="chat-message-date"><%= data.date %></p>
+<% } %>
<li class="chat-message <%= (data.mine ? 'chat-message-mine' : '') %>" data-message-id="<%= data.cid %>" >
<h3 class="chat-message-user" ><%= data.user %></h3>
<p class="chat-message-text"><%= data.body %></p>

0 comments on commit cad9aae

Please sign in to comment.