Commit b307d517 authored by JC Brand's avatar JC Brand

Date handling is now done with moment.js

parent 22c72430
......@@ -5,7 +5,6 @@
* Copyright (c) 2012, Jan-Carel Brand <jc@opkode.com>
* Dual licensed under the MIT and GPL Licenses
*/
"use strict";
// AMD/global registrations
(function (root, factory) {
......@@ -33,6 +32,7 @@
root.converse = factory(jQuery, _, OTR, DSA, templates);
}
}(this, function ($, _, OTR, DSA, templates, moment) {
"use strict";
if (typeof console === "undefined" || typeof console.log === "undefined") {
console = { log: function () {}, error: function () {} };
}
......@@ -56,7 +56,7 @@
};
$.fn.addEmoticons = function() {
if (converse.visible_toolbar_buttons['emoticons']) {
if (converse.visible_toolbar_buttons.emoticons) {
if (this.length > 0) {
this.each(function(i, obj) {
var text = $(obj).html();
......@@ -324,7 +324,7 @@
'image_type': img_type,
'image': img,
'url': url,
'vcard_updated': converse.toISOString(new Date())
'vcard_updated': moment().format()
});
}
}
......@@ -338,7 +338,7 @@
var rosteritem = converse.roster.get(jid);
if (rosteritem) {
rosteritem.save({
'vcard_updated': converse.toISOString(new Date())
'vcard_updated': moment().format()
});
}
if (errback) {
......@@ -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 method applies some resistance/gravity around the
* "default_box_height". If "height" is close enough to
......@@ -691,7 +649,7 @@
'box_id' : b64_sha1(this.get('jid')),
'otr_status': this.get('otr_status') || UNENCRYPTED,
'minimized': this.get('minimized') || false,
'time_minimized': this.get('time_minimized') || converse.toISOString(new Date()),
'time_minimized': this.get('time_minimized') || moment(),
'height': height
});
} else {
......@@ -840,7 +798,7 @@
fullname: fullname,
sender: 'them',
delayed: delayed,
time: converse.toISOString(new Date()),
time: moment().format(),
composing: composing.length
});
}
......@@ -849,7 +807,7 @@
stamp = $message.find('delay').attr('stamp');
time = stamp;
} else {
time = converse.toISOString(new Date());
time = moment().format();
}
if (from == converse.bare_jid) {
sender = 'me';
......@@ -997,8 +955,7 @@
showMessage: function (msg_dict) {
var $content = this.$el.find('.chat-content'),
iso_time = msg_dict.time || converse.toISOString(new Date()),
msg_date = converse.parseISO8601(iso_time),
msg_time = moment(msg_dict.time) || moment,
text = msg_dict.message,
match = text.match(/^\/(.*?)(?: (.*))?$/),
fullname = msg_dict.fullname || this.model.get('fullname'), // XXX Perhaps always use model's?
......@@ -1015,13 +972,13 @@
$content.find('div.chat-event').remove();
var message = template({
'sender': msg_dict.sender,
'time': msg_date.toTimeString().substring(0,5),
'time': msg_time.format('hh:mm'),
'username': username,
'message': '',
'extra_classes': msg_dict.delayed && 'delayed' || ''
});
$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.scrollDown();
......@@ -1056,7 +1013,7 @@
this_date = moment(time);
this.$el.find('.chat-content').append(converse.templates.new_day({
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 @@
this.model.messages.create({
fullname: fullname,
sender: 'me',
time: converse.toISOString(new Date()),
time: moment().format(),
message: text
});
this.sendMessageStanza(text);
......@@ -1349,7 +1306,7 @@
flyout.addClass('minimized');
this.model.save({
'minimized': true,
'time_minimized': converse.toISOString(new Date())
'time_minimized': moment().format()
});
}
return this;
......@@ -1438,9 +1395,9 @@
label_whats_this: __("What\'s this?"),
otr_status_class: OTR_CLASS_MAPPING[data.otr_status],
otr_translated_status: OTR_TRANSLATED_MAPPING[data.otr_status],
show_call_button: converse.visible_toolbar_buttons['call'],
show_clear_button: converse.visible_toolbar_buttons['clear'],
show_emoticons: converse.visible_toolbar_buttons['emoticons']
show_call_button: converse.visible_toolbar_buttons.call,
show_clear_button: converse.visible_toolbar_buttons.clear,
show_emoticons: converse.visible_toolbar_buttons.emoticons
})
)
);
......@@ -2355,26 +2312,25 @@
sender = resource && Strophe.unescapeNode(resource) || '',
delayed = $message.find('delay').length > 0,
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) {
stamp = $message.find('delay').attr('stamp');
message_datetime = converse.parseISO8601(stamp);
message_datetime = moment($message.find('delay').attr('stamp'));
} else {
message_datetime = new Date();
message_datetime = moment();
}
// If this message is on a different day than the one received
// prior, then indicate it on the chatbox.
dates = $chat_content.find("time").map(function(){return $(this).attr("datetime");}).get();
message_date = new Date(message_datetime.getTime());
message_date.setUTCHours(0,0,0,0);
isodate = converse.toISOString(message_date);
if (_.indexOf(dates, isodate) == -1) {
message_date = message_datetime.clone().startOf('day');
message_date_str = message_date.format("YYYY-MM-DD");
if (_.indexOf(dates, message_date_str) === -1) {
$chat_content.append(converse.templates.new_day({
isodate: isodate,
datestring: message_date.toString().substring(0,15)
isodate: message_date_str,
datestring: message_date.format("dddd MMM Do YYYY")
}));
}
this.showStatusMessages($message);
if (subject) {
this.$el.find('.chatroom-topic').text(subject).attr('title', subject);
......@@ -2391,7 +2347,7 @@
'message': body,
'sender': display_sender,
'fullname': sender,
'time': converse.toISOString(message_datetime)
'time': message_datetime.format()
});
if (display_sender === 'room') {
// We only emit an event if it's not our own message
......@@ -2914,7 +2870,7 @@
image: img,
image_type: img_type,
url: url,
vcard_updated: converse.toISOString(new Date()),
vcard_updated: moment().format(),
is_last: true
});
}, this),
......
......@@ -9,6 +9,7 @@ Changelog
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`_.
* All date handling is now done with moment.js. [jcbrand]
* Add a new toolbar button for clearing chat messages. [jcbrand]
* Chat boxes and rooms can now be resized vertically. [jcbrand]
* Upgraded many dependencies to their latest versions. [jcbrand]
......
......@@ -57,7 +57,7 @@
chatbox = utils.openChatBoxFor(contact_jid);
chatboxview = this.chatboxviews.get(contact_jid);
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';
view = this.rosterview.get(jid);
spyOn(view, 'openChat').andCallThrough();
......@@ -291,7 +291,7 @@
spyOn(converse, 'emit');
// First check that the button doesn't show if it's not enabled
// via "visible_toolbar_buttons"
converse.visible_toolbar_buttons['call'] = false;
converse.visible_toolbar_buttons.call = false;
utils.openChatBoxFor(contact_jid);
view = this.chatboxviews.get(contact_jid);
$toolbar = view.$el.find('ul.chat-toolbar');
......@@ -300,7 +300,7 @@
view.closeChat();
// Now check that it's shown if enabled and that it emits
// onCallButtonClicked
converse.visible_toolbar_buttons['call'] = true; // enable the button
converse.visible_toolbar_buttons.call = true; // enable the button
utils.openChatBoxFor(contact_jid);
view = this.chatboxviews.get(contact_jid);
$toolbar = view.$el.find('ul.chat-toolbar');
......@@ -315,7 +315,7 @@
var contact_jid = mock.cur_names[2].replace(' ','.').toLowerCase() + '@localhost';
// First check that the button doesn't show if it's not enabled
// via "visible_toolbar_buttons"
converse.visible_toolbar_buttons['clear'] = false;
converse.visible_toolbar_buttons.clear = false;
utils.openChatBoxFor(contact_jid);
view = this.chatboxviews.get(contact_jid);
view = this.chatboxviews.get(contact_jid);
......@@ -325,7 +325,7 @@
view.closeChat();
// Now check that it's shown if enabled and that it calls
// clearMessages
converse.visible_toolbar_buttons['clear'] = true; // enable the button
converse.visible_toolbar_buttons.clear = true; // enable the button
utils.openChatBoxFor(contact_jid);
view = this.chatboxviews.get(contact_jid);
$toolbar = view.$el.find('ul.chat-toolbar');
......@@ -470,7 +470,8 @@
var contact_jid = contact_name.replace(' ','.').toLowerCase() + '@localhost';
utils.openChatBoxFor(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 chatbox = this.chatboxes.get(contact_jid);
var chatboxview = this.chatboxviews.get(contact_jid);
......@@ -483,9 +484,9 @@
from: contact_jid,
to: this.connection.jid,
type: 'chat',
id: one_day_ago.getTime()
id: one_day_ago.unix()
}).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();
this.chatboxes.onMessage(msg);
expect(converse.emit).toHaveBeenCalledWith('onMessage', msg);
......@@ -517,7 +518,7 @@
expect($time.length).toEqual(1);
expect($time.attr('class')).toEqual('chat-date');
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
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