Commit b307d517 authored by JC Brand's avatar JC Brand

Date handling is now done with moment.js

parent 22c72430
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
* Copyright (c) 2012, Jan-Carel Brand <jc@opkode.com> * Copyright (c) 2012, Jan-Carel Brand <jc@opkode.com>
* Dual licensed under the MIT and GPL Licenses * Dual licensed under the MIT and GPL Licenses
*/ */
"use strict";
// AMD/global registrations // AMD/global registrations
(function (root, factory) { (function (root, factory) {
...@@ -33,6 +32,7 @@ ...@@ -33,6 +32,7 @@
root.converse = factory(jQuery, _, OTR, DSA, templates); root.converse = factory(jQuery, _, OTR, DSA, templates);
} }
}(this, function ($, _, OTR, DSA, templates, moment) { }(this, function ($, _, OTR, DSA, templates, moment) {
"use strict";
if (typeof console === "undefined" || typeof console.log === "undefined") { if (typeof console === "undefined" || typeof console.log === "undefined") {
console = { log: function () {}, error: function () {} }; console = { log: function () {}, error: function () {} };
} }
...@@ -56,7 +56,7 @@ ...@@ -56,7 +56,7 @@
}; };
$.fn.addEmoticons = function() { $.fn.addEmoticons = function() {
if (converse.visible_toolbar_buttons['emoticons']) { if (converse.visible_toolbar_buttons.emoticons) {
if (this.length > 0) { if (this.length > 0) {
this.each(function(i, obj) { this.each(function(i, obj) {
var text = $(obj).html(); var text = $(obj).html();
...@@ -324,7 +324,7 @@ ...@@ -324,7 +324,7 @@
'image_type': img_type, 'image_type': img_type,
'image': img, 'image': img,
'url': url, 'url': url,
'vcard_updated': converse.toISOString(new Date()) 'vcard_updated': moment().format()
}); });
} }
} }
...@@ -338,7 +338,7 @@ ...@@ -338,7 +338,7 @@
var rosteritem = converse.roster.get(jid); var rosteritem = converse.roster.get(jid);
if (rosteritem) { if (rosteritem) {
rosteritem.save({ rosteritem.save({
'vcard_updated': converse.toISOString(new Date()) 'vcard_updated': moment().format()
}); });
} }
if (errback) { if (errback) {
...@@ -409,48 +409,6 @@ ...@@ -409,48 +409,6 @@
} }
}; };
this.toISOString = function (date) {
var pad;
if (typeof date.toISOString !== 'undefined') {
return date.toISOString();
} else {
// IE <= 8 Doesn't have toISOStringMethod
pad = function (num) {
return (num < 10) ? '0' + num : '' + num;
};
return date.getUTCFullYear() + '-' +
pad(date.getUTCMonth() + 1) + '-' +
pad(date.getUTCDate()) + 'T' +
pad(date.getUTCHours()) + ':' +
pad(date.getUTCMinutes()) + ':' +
pad(date.getUTCSeconds()) + '.000Z';
}
};
this.parseISO8601 = function (datestr) {
/* Parses string formatted as 2013-02-14T11:27:08.268Z to a Date obj.
*/
var numericKeys = [1, 4, 5, 6, 7, 10, 11],
struct = /^\s*(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}\.?\d*)Z\s*$/.exec(datestr),
minutesOffset = 0,
i, k;
for (i = 0; (k = numericKeys[i]); ++i) {
struct[k] = +struct[k] || 0;
}
// allow undefined days and months
struct[2] = (+struct[2] || 1) - 1;
struct[3] = +struct[3] || 1;
if (struct[8] !== 'Z' && struct[9] !== undefined) {
minutesOffset = struct[10] * 60 + struct[11];
if (struct[9] === '+') {
minutesOffset = 0 - minutesOffset;
}
}
return new Date(Date.UTC(struct[1], struct[2], struct[3], struct[4], struct[5] + minutesOffset, struct[6], struct[7]));
};
this.applyHeightResistance = function (height) { this.applyHeightResistance = function (height) {
/* This method applies some resistance/gravity around the /* This method applies some resistance/gravity around the
* "default_box_height". If "height" is close enough to * "default_box_height". If "height" is close enough to
...@@ -691,7 +649,7 @@ ...@@ -691,7 +649,7 @@
'box_id' : b64_sha1(this.get('jid')), 'box_id' : b64_sha1(this.get('jid')),
'otr_status': this.get('otr_status') || UNENCRYPTED, 'otr_status': this.get('otr_status') || UNENCRYPTED,
'minimized': this.get('minimized') || false, 'minimized': this.get('minimized') || false,
'time_minimized': this.get('time_minimized') || converse.toISOString(new Date()), 'time_minimized': this.get('time_minimized') || moment(),
'height': height 'height': height
}); });
} else { } else {
...@@ -840,7 +798,7 @@ ...@@ -840,7 +798,7 @@
fullname: fullname, fullname: fullname,
sender: 'them', sender: 'them',
delayed: delayed, delayed: delayed,
time: converse.toISOString(new Date()), time: moment().format(),
composing: composing.length composing: composing.length
}); });
} }
...@@ -849,7 +807,7 @@ ...@@ -849,7 +807,7 @@
stamp = $message.find('delay').attr('stamp'); stamp = $message.find('delay').attr('stamp');
time = stamp; time = stamp;
} else { } else {
time = converse.toISOString(new Date()); time = moment().format();
} }
if (from == converse.bare_jid) { if (from == converse.bare_jid) {
sender = 'me'; sender = 'me';
...@@ -997,8 +955,7 @@ ...@@ -997,8 +955,7 @@
showMessage: function (msg_dict) { showMessage: function (msg_dict) {
var $content = this.$el.find('.chat-content'), var $content = this.$el.find('.chat-content'),
iso_time = msg_dict.time || converse.toISOString(new Date()), msg_time = moment(msg_dict.time) || moment,
msg_date = converse.parseISO8601(iso_time),
text = msg_dict.message, text = msg_dict.message,
match = text.match(/^\/(.*?)(?: (.*))?$/), match = text.match(/^\/(.*?)(?: (.*))?$/),
fullname = msg_dict.fullname || this.model.get('fullname'), // XXX Perhaps always use model's? fullname = msg_dict.fullname || this.model.get('fullname'), // XXX Perhaps always use model's?
...@@ -1015,13 +972,13 @@ ...@@ -1015,13 +972,13 @@
$content.find('div.chat-event').remove(); $content.find('div.chat-event').remove();
var message = template({ var message = template({
'sender': msg_dict.sender, 'sender': msg_dict.sender,
'time': msg_date.toTimeString().substring(0,5), 'time': msg_time.format('hh:mm'),
'username': username, 'username': username,
'message': '', 'message': '',
'extra_classes': msg_dict.delayed && 'delayed' || '' 'extra_classes': msg_dict.delayed && 'delayed' || ''
}); });
$content.append($(message).children('.chat-message-content').first().text(text).addHyperlinks().addEmoticons().parent()); $content.append($(message).children('.chat-message-content').first().text(text).addHyperlinks().addEmoticons().parent());
if (this.model.get('minimized') && (iso_time > this.model.get('time_minimized'))) { if (this.model.get('minimized') && (msg_time.isAfter(this.model.get('time_minimized')))) {
this.updateUnreadMessagesCounter(); this.updateUnreadMessagesCounter();
} }
this.scrollDown(); this.scrollDown();
...@@ -1056,7 +1013,7 @@ ...@@ -1056,7 +1013,7 @@
this_date = moment(time); this_date = moment(time);
this.$el.find('.chat-content').append(converse.templates.new_day({ this.$el.find('.chat-content').append(converse.templates.new_day({
isodate: this_date.format("YYYY-MM-DD"), isodate: this_date.format("YYYY-MM-DD"),
datestring: this_date.format("dddd, MMMM Do YYYY") datestring: this_date.format("dddd MMM Do YYYY")
})); }));
} }
} }
...@@ -1126,7 +1083,7 @@ ...@@ -1126,7 +1083,7 @@
this.model.messages.create({ this.model.messages.create({
fullname: fullname, fullname: fullname,
sender: 'me', sender: 'me',
time: converse.toISOString(new Date()), time: moment().format(),
message: text message: text
}); });
this.sendMessageStanza(text); this.sendMessageStanza(text);
...@@ -1349,7 +1306,7 @@ ...@@ -1349,7 +1306,7 @@
flyout.addClass('minimized'); flyout.addClass('minimized');
this.model.save({ this.model.save({
'minimized': true, 'minimized': true,
'time_minimized': converse.toISOString(new Date()) 'time_minimized': moment().format()
}); });
} }
return this; return this;
...@@ -1438,9 +1395,9 @@ ...@@ -1438,9 +1395,9 @@
label_whats_this: __("What\'s this?"), label_whats_this: __("What\'s this?"),
otr_status_class: OTR_CLASS_MAPPING[data.otr_status], otr_status_class: OTR_CLASS_MAPPING[data.otr_status],
otr_translated_status: OTR_TRANSLATED_MAPPING[data.otr_status], otr_translated_status: OTR_TRANSLATED_MAPPING[data.otr_status],
show_call_button: converse.visible_toolbar_buttons['call'], show_call_button: converse.visible_toolbar_buttons.call,
show_clear_button: converse.visible_toolbar_buttons['clear'], show_clear_button: converse.visible_toolbar_buttons.clear,
show_emoticons: converse.visible_toolbar_buttons['emoticons'] show_emoticons: converse.visible_toolbar_buttons.emoticons
}) })
) )
); );
...@@ -2355,26 +2312,25 @@ ...@@ -2355,26 +2312,25 @@
sender = resource && Strophe.unescapeNode(resource) || '', sender = resource && Strophe.unescapeNode(resource) || '',
delayed = $message.find('delay').length > 0, delayed = $message.find('delay').length > 0,
subject = $message.children('subject').text(), subject = $message.children('subject').text(),
match, template, message_datetime, message_date, dates, isodate, stamp; match, template, dates, message_datetime, message_date, message_date_str;
if (delayed) { if (delayed) {
stamp = $message.find('delay').attr('stamp'); message_datetime = moment($message.find('delay').attr('stamp'));
message_datetime = converse.parseISO8601(stamp);
} else { } else {
message_datetime = new Date(); message_datetime = moment();
} }
// If this message is on a different day than the one received // If this message is on a different day than the one received
// prior, then indicate it on the chatbox. // prior, then indicate it on the chatbox.
dates = $chat_content.find("time").map(function(){return $(this).attr("datetime");}).get(); dates = $chat_content.find("time").map(function(){return $(this).attr("datetime");}).get();
message_date = new Date(message_datetime.getTime()); message_date = message_datetime.clone().startOf('day');
message_date.setUTCHours(0,0,0,0); message_date_str = message_date.format("YYYY-MM-DD");
isodate = converse.toISOString(message_date); if (_.indexOf(dates, message_date_str) === -1) {
if (_.indexOf(dates, isodate) == -1) {
$chat_content.append(converse.templates.new_day({ $chat_content.append(converse.templates.new_day({
isodate: isodate, isodate: message_date_str,
datestring: message_date.toString().substring(0,15) datestring: message_date.format("dddd MMM Do YYYY")
})); }));
} }
this.showStatusMessages($message); this.showStatusMessages($message);
if (subject) { if (subject) {
this.$el.find('.chatroom-topic').text(subject).attr('title', subject); this.$el.find('.chatroom-topic').text(subject).attr('title', subject);
...@@ -2391,7 +2347,7 @@ ...@@ -2391,7 +2347,7 @@
'message': body, 'message': body,
'sender': display_sender, 'sender': display_sender,
'fullname': sender, 'fullname': sender,
'time': converse.toISOString(message_datetime) 'time': message_datetime.format()
}); });
if (display_sender === 'room') { if (display_sender === 'room') {
// We only emit an event if it's not our own message // We only emit an event if it's not our own message
...@@ -2914,7 +2870,7 @@ ...@@ -2914,7 +2870,7 @@
image: img, image: img,
image_type: img_type, image_type: img_type,
url: url, url: url,
vcard_updated: converse.toISOString(new Date()), vcard_updated: moment().format(),
is_last: true is_last: true
}); });
}, this), }, this),
......
...@@ -9,6 +9,7 @@ Changelog ...@@ -9,6 +9,7 @@ Changelog
2. Configuration options for the chat toolbar have changed. 2. Configuration options for the chat toolbar have changed.
Please refer to the `relevant documentation http://devbox:8890/docs/html/index.html#visible-toolbar-buttons`_. Please refer to the `relevant documentation http://devbox:8890/docs/html/index.html#visible-toolbar-buttons`_.
* All date handling is now done with moment.js. [jcbrand]
* Add a new toolbar button for clearing chat messages. [jcbrand] * Add a new toolbar button for clearing chat messages. [jcbrand]
* Chat boxes and rooms can now be resized vertically. [jcbrand] * Chat boxes and rooms can now be resized vertically. [jcbrand]
* Upgraded many dependencies to their latest versions. [jcbrand] * Upgraded many dependencies to their latest versions. [jcbrand]
......
...@@ -57,7 +57,7 @@ ...@@ -57,7 +57,7 @@
chatbox = utils.openChatBoxFor(contact_jid); chatbox = utils.openChatBoxFor(contact_jid);
chatboxview = this.chatboxviews.get(contact_jid); chatboxview = this.chatboxviews.get(contact_jid);
spyOn(chatboxview, 'focus'); spyOn(chatboxview, 'focus');
var $el = this.rosterview.$el.find('a.open-chat:contains("'+chatbox.get('fullname')+'")'); $el = this.rosterview.$el.find('a.open-chat:contains("'+chatbox.get('fullname')+'")');
jid = $el.text().replace(' ','.').toLowerCase() + '@localhost'; jid = $el.text().replace(' ','.').toLowerCase() + '@localhost';
view = this.rosterview.get(jid); view = this.rosterview.get(jid);
spyOn(view, 'openChat').andCallThrough(); spyOn(view, 'openChat').andCallThrough();
...@@ -291,7 +291,7 @@ ...@@ -291,7 +291,7 @@
spyOn(converse, 'emit'); spyOn(converse, 'emit');
// First check that the button doesn't show if it's not enabled // First check that the button doesn't show if it's not enabled
// via "visible_toolbar_buttons" // via "visible_toolbar_buttons"
converse.visible_toolbar_buttons['call'] = false; converse.visible_toolbar_buttons.call = false;
utils.openChatBoxFor(contact_jid); utils.openChatBoxFor(contact_jid);
view = this.chatboxviews.get(contact_jid); view = this.chatboxviews.get(contact_jid);
$toolbar = view.$el.find('ul.chat-toolbar'); $toolbar = view.$el.find('ul.chat-toolbar');
...@@ -300,7 +300,7 @@ ...@@ -300,7 +300,7 @@
view.closeChat(); view.closeChat();
// Now check that it's shown if enabled and that it emits // Now check that it's shown if enabled and that it emits
// onCallButtonClicked // onCallButtonClicked
converse.visible_toolbar_buttons['call'] = true; // enable the button converse.visible_toolbar_buttons.call = true; // enable the button
utils.openChatBoxFor(contact_jid); utils.openChatBoxFor(contact_jid);
view = this.chatboxviews.get(contact_jid); view = this.chatboxviews.get(contact_jid);
$toolbar = view.$el.find('ul.chat-toolbar'); $toolbar = view.$el.find('ul.chat-toolbar');
...@@ -315,7 +315,7 @@ ...@@ -315,7 +315,7 @@
var contact_jid = mock.cur_names[2].replace(' ','.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[2].replace(' ','.').toLowerCase() + '@localhost';
// First check that the button doesn't show if it's not enabled // First check that the button doesn't show if it's not enabled
// via "visible_toolbar_buttons" // via "visible_toolbar_buttons"
converse.visible_toolbar_buttons['clear'] = false; converse.visible_toolbar_buttons.clear = false;
utils.openChatBoxFor(contact_jid); utils.openChatBoxFor(contact_jid);
view = this.chatboxviews.get(contact_jid); view = this.chatboxviews.get(contact_jid);
view = this.chatboxviews.get(contact_jid); view = this.chatboxviews.get(contact_jid);
...@@ -325,7 +325,7 @@ ...@@ -325,7 +325,7 @@
view.closeChat(); view.closeChat();
// Now check that it's shown if enabled and that it calls // Now check that it's shown if enabled and that it calls
// clearMessages // clearMessages
converse.visible_toolbar_buttons['clear'] = true; // enable the button converse.visible_toolbar_buttons.clear = true; // enable the button
utils.openChatBoxFor(contact_jid); utils.openChatBoxFor(contact_jid);
view = this.chatboxviews.get(contact_jid); view = this.chatboxviews.get(contact_jid);
$toolbar = view.$el.find('ul.chat-toolbar'); $toolbar = view.$el.find('ul.chat-toolbar');
...@@ -470,7 +470,8 @@ ...@@ -470,7 +470,8 @@
var contact_jid = contact_name.replace(' ','.').toLowerCase() + '@localhost'; var contact_jid = contact_name.replace(' ','.').toLowerCase() + '@localhost';
utils.openChatBoxFor(contact_jid); utils.openChatBoxFor(contact_jid);
utils.clearChatBoxMessages(contact_jid); utils.clearChatBoxMessages(contact_jid);
var one_day_ago = new Date(new Date().setDate(new Date().getDate()-1)); var one_day_ago = moment()
one_day_ago.subtract('days', 1);
var message = 'This is a day old message'; var message = 'This is a day old message';
var chatbox = this.chatboxes.get(contact_jid); var chatbox = this.chatboxes.get(contact_jid);
var chatboxview = this.chatboxviews.get(contact_jid); var chatboxview = this.chatboxviews.get(contact_jid);
...@@ -483,9 +484,9 @@ ...@@ -483,9 +484,9 @@
from: contact_jid, from: contact_jid,
to: this.connection.jid, to: this.connection.jid,
type: 'chat', type: 'chat',
id: one_day_ago.getTime() id: one_day_ago.unix()
}).c('body').t(message).up() }).c('body').t(message).up()
.c('delay', { xmlns:'urn:xmpp:delay', from: 'localhost', stamp: converse.toISOString(one_day_ago) }) .c('delay', { xmlns:'urn:xmpp:delay', from: 'localhost', stamp: one_day_ago.format() })
.c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree(); .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
this.chatboxes.onMessage(msg); this.chatboxes.onMessage(msg);
expect(converse.emit).toHaveBeenCalledWith('onMessage', msg); expect(converse.emit).toHaveBeenCalledWith('onMessage', msg);
...@@ -517,7 +518,7 @@ ...@@ -517,7 +518,7 @@
expect($time.length).toEqual(1); expect($time.length).toEqual(1);
expect($time.attr('class')).toEqual('chat-date'); expect($time.attr('class')).toEqual('chat-date');
expect($time.attr('datetime')).toEqual(moment(message_date).format("YYYY-MM-DD")); expect($time.attr('datetime')).toEqual(moment(message_date).format("YYYY-MM-DD"));
expect($time.text()).toEqual(moment(message_date).format("dddd, MMMM Do YYYY")); expect($time.text()).toEqual(moment(message_date).format("dddd MMM Do YYYY"));
// Normal checks for the 2nd message // Normal checks for the 2nd message
expect(chatbox.messages.length).toEqual(2); expect(chatbox.messages.length).toEqual(2);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment