Ver código fonte

Add new dependency moment.js

Use it to better compare dates when trying to determine whether a message falls
on a different day.

Also upgraded some dependencies' versions.
JC Brand 11 anos atrás
pai
commit
7d472eb26d
7 arquivos alterados com 38 adições e 41 exclusões
  1. 5 4
      bower.json
  2. 11 20
      converse.js
  3. 1 0
      main.js
  4. 6 6
      package.json
  5. 3 7
      spec/chatbox.js
  6. 6 2
      src/deps-full.js
  7. 6 2
      src/deps-no-otr.js

+ 5 - 4
bower.json

@@ -3,12 +3,12 @@
   "version": "0.8.0",
   "devDependencies": {
     "jasmine": "https://github.com/jcbrand/jasmine.git#1_3_x",
-    "otr": "0.2.7",
+    "otr": "0.2.12",
     "requirejs-text": "~2.0.10",
     "requirejs-tpl-jcbrand": "*"
   },
   "dependencies": {
-    "requirejs": "2.1.8",
+    "requirejs": "2.1.11",
     "jquery": "1.8.3",
     "jed": "0.5.4",
     "tinysort": "git://github.com/Sjeiti/TinySort.git",
@@ -20,11 +20,12 @@
     "strophe.vcard": "https://raw.github.com/jcbrand/strophejs-plugins/75c8693992bc357c699b6d615eeb396e799f5c02/vcard/strophe.vcard.js",
     "strophe.disco": "https://raw.github.com/jcbrand/strophejs-plugins/75c8693992bc357c699b6d615eeb396e799f5c02/disco/strophe.disco.js",
     "strophe.muc": "https://raw.github.com/strophe/strophejs-plugins/02310ad1b8da2962cd05b0f4bceaecca134efed4/muc/strophe.muc.js",
-    "otr": "0.2.7",
+    "otr": "0.2.12",
     "crypto-js-evanvosberg": "~3.1.2",
     "almond": "~0.2.6",
     "requirejs-text": "~2.0.10",
-    "requirejs-tpl-jcbrand": "*"
+    "requirejs-tpl-jcbrand": "*",
+    "momentjs": "~2.6.0"
   },
   "exportsOverride": {}
 }

+ 11 - 20
converse.js

@@ -12,11 +12,12 @@
         define("converse",
               ["converse-dependencies", "converse-templates"],
             function(dependencies, templates) {
-                var otr = dependencies[0];
+                var otr = dependencies.otr,
+                    moment = dependencies.moment;
                 if (typeof otr !== "undefined") {
-                    return factory(jQuery, _, otr.OTR, otr.DSA, templates);
+                    return factory(jQuery, _, otr.OTR, otr.DSA, templates, moment);
                 } else {
-                    return factory(jQuery, _, undefined, undefined, templates);
+                    return factory(jQuery, _, undefined, undefined, templates, moment);
                 }
             }
         );
@@ -30,7 +31,7 @@
         // TODO Templates not defined
         root.converse = factory(jQuery, _, OTR, DSA, templates);
     }
-}(this, function ($, _, OTR, DSA, templates) {
+}(this, function ($, _, OTR, DSA, templates, moment) {
     if (typeof console === "undefined" || typeof console.log === "undefined") {
         console = { log: function () {}, error: function () {} };
     }
@@ -998,22 +999,19 @@
             onMessageAdded: function (message) {
                 var time = message.get('time'),
                     times = this.model.messages.pluck('time'),
-                    this_date = converse.parseISO8601(time),
-                    previous_message, idx, prev_date, isodate, text, match;
+                    previous_message, idx, this_date, prev_date, text, match;
 
                 // If this message is on a different day than the one received
                 // prior, then indicate it on the chatbox.
                 idx = _.indexOf(times, time)-1;
                 if (idx >= 0) {
                     previous_message = this.model.messages.at(idx);
-                    prev_date = converse.parseISO8601(previous_message.get('time'));
-                    isodate = new Date(this_date.getTime());
-                    isodate.setUTCHours(0,0,0,0);
-                    isodate = converse.toISOString(isodate);
-                    if (this.isDifferentDay(prev_date, this_date)) {
+                    prev_date = moment(previous_message.get('time'));
+                    if (prev_date.isBefore(time, 'day')) {
+                        this_date = moment(time);
                         this.$el.find('.chat-content').append(converse.templates.new_day({
-                            isodate: isodate,
-                            datestring: this_date.toString().substring(0,15)
+                            isodate: this_date.format("YYYY-MM-DD"),
+                            datestring: this_date.format("dddd, MMMM Do YYYY")
                         }));
                     }
                 }
@@ -1029,13 +1027,6 @@
                 return this.scrollDown();
             },
 
-            isDifferentDay: function (prev_date, next_date) {
-                return (
-                    (next_date.getDate() != prev_date.getDate()) ||
-                    (next_date.getFullYear() != prev_date.getFullYear()) ||
-                    (next_date.getMonth() != prev_date.getMonth()));
-            },
-
             sendMessageStanza: function (text) {
                 /*
                  * Sends the actual XML stanza to the XMPP server.

+ 1 - 0
main.js

@@ -29,6 +29,7 @@ config = {
         "crypto.mode-ctr": "components/otr/vendor/cryptojs/mode-ctr",
         "crypto": "src/crypto",
         "eventemitter": "components/otr/build/dep/eventemitter",
+        "moment": "components/momentjs/moment",
         "otr": "components/otr/build/otr",
         "converse-dependencies": "src/deps-full"
     },

+ 6 - 6
package.json

@@ -27,16 +27,16 @@
     "url": "https://github.com/jcbrand/converse.js/issues"
   },
   "devDependencies": {
-    "grunt-cli": "~0.1.9",
-    "grunt": "~0.4.1",
-    "grunt-contrib-jshint": "~0.6.0",
+    "grunt-cli": "~0.1.13",
+    "grunt": "~0.4.4",
+    "grunt-contrib-jshint": "~0.10.0",
     "phantom-jasmine": "0.1.8",
     "phantomjs": "~1.9.7-1",
     "bower": "latest",
-    "grunt-contrib-requirejs": "~0.4.1",
-    "grunt-contrib-cssmin": "~0.6.1"
+    "grunt-contrib-requirejs": "~0.4.3",
+    "grunt-contrib-cssmin": "~0.9.0"
   },
   "dependencies": {
-    "requirejs": "~2.1.8"
+    "requirejs": "~2.1.11"
   }
 }

+ 3 - 7
spec/chatbox.js

@@ -412,15 +412,11 @@
                 }, converse));
 
                 it("will indicate when it has a time difference of more than a day between it and its predecessor", $.proxy(function () {
-                    // FIXME: there are issues where with timezones (when
-                    // the browser is in a new day but the XMPP server is in
-                    // the previous day).
                     spyOn(converse, 'emit');
                     var contact_name = mock.cur_names[1];
                     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 message = 'This is a day old message';
                     var chatbox = this.chatboxes.get(contact_jid);
@@ -465,11 +461,10 @@
                     // props.
                     var $time = $chat_content.find('time');
                     var message_date = new Date();
-                    message_date.setUTCHours(0,0,0,0);
                     expect($time.length).toEqual(1);
                     expect($time.attr('class')).toEqual('chat-date');
-                    expect($time.attr('datetime')).toEqual(converse.toISOString(message_date));
-                    expect($time.text()).toEqual(message_date.toString().substring(0,15));
+                    expect($time.attr('datetime')).toEqual(moment(message_date).format("YYYY-MM-DD"));
+                    expect($time.text()).toEqual(moment(message_date).format("dddd, MMMM Do YYYY"));
 
                     // Normal checks for the 2nd message
                     expect(chatbox.messages.length).toEqual(2);
@@ -633,6 +628,7 @@
             it("is cleared when the window is focused", $.proxy(function () {
                 spyOn(converse, 'clearMsgCounter').andCallThrough();
                 runs(function () {
+                    $(window).trigger('blur');
                     $(window).trigger('focus');
                 });
                 waits(50);

+ 6 - 2
src/deps-full.js

@@ -1,5 +1,6 @@
 define("converse-dependencies", [
     "otr",
+    "moment",
     "locales",
     "backbone.localStorage",
     "jquery.tinysort",
@@ -8,6 +9,9 @@ define("converse-dependencies", [
     "strophe.roster",
     "strophe.vcard",
     "strophe.disco"
-], function() {
-    return arguments;
+], function(otr, moment) {
+    return {
+        'otr': otr,
+        'moment': moment
+    };
 });

+ 6 - 2
src/deps-no-otr.js

@@ -1,4 +1,5 @@
 define("converse-dependencies", [
+    "moment",
     "locales",
     "backbone.localStorage",
     "jquery.tinysort",
@@ -7,6 +8,9 @@ define("converse-dependencies", [
     "strophe.roster",
     "strophe.vcard",
     "strophe.disco"
-], function() {
-    return undefined;
+], function(moment) {
+    return {
+        'otr': undefined,
+        'moment': moment
+    };
 });