Explorar o código

Event emitting code. Updates #48

Include the event emitting code in converse.js itself and start emitting events.

Still needs tests and to be thoroughly tested manually.
Still needs docs.
JC Brand %!s(int64=11) %!d(string=hai) anos
pai
achega
e729471bbf
Modificáronse 4 ficheiros con 47 adicións e 12 borrados
  1. 45 8
      converse.js
  2. 2 2
      index.html
  3. 0 1
      main.js
  4. 0 1
      src/deps-full.js

+ 45 - 8
converse.js

@@ -34,6 +34,20 @@
     }
 }(this, function ($, _, OTR, DSA, console) {
     var converse = {};
+
+    converse.emit = function(evt, data) {
+        $(this).trigger(evt, data);
+    };
+    once = function(evt, handler) {
+        $(this).one(evt, handler);
+    };
+    on = function(evt, handler) {
+        $(this).bind(evt, handler);
+    };
+    off = function(evt, handler) {
+        $(this).unbind(evt, handler);
+    };
+    
     converse.initialize = function (settings, callback) {
         var converse = this;
 
@@ -382,8 +396,7 @@
             this.initStatus($.proxy(function () {
                 this.initRoster();
                 this.chatboxes.onConnected();
-                this.connection.roster.get(function () {});
-
+                this.connection.roster.get();
                 $(document).click(function() {
                     if ($('.toggle-otr ul').is(':visible')) {
                         $('.toggle-otr ul', this).slideUp();
@@ -393,7 +406,6 @@
                     }
                 });
 
-
                 $(window).on("blur focus", $.proxy(function(e) {
                     if ((this.windowState != e.type) && (e.type == 'focus')) {
                         converse.clearMsgCounter();
@@ -929,6 +941,7 @@
             },
 
             sendMessage: function (text) {
+                converse.emit('onMessageSend', text);
                 var match = text.replace(/^\s*/, "").match(/^\/(.*)\s*$/), msgs;
                 if (match) {
                     if (match[1] === "clear") {
@@ -1102,9 +1115,11 @@
                             this.$el.find('div.chat-event').remove();
                         }
                     }
+                    converse.emit('onBuddyStatusChanged', item.attributes, item.get('chat_status'));
                 }
                 if (_.has(item.changed, 'status')) {
                     this.showStatusMessage(item.get('status'));
+                    converse.emit('onBuddyStatusMessageChanged', item.attributes, item.get('status'));
                 }
                 if (_.has(item.changed, 'image')) {
                     this.renderAvatar();
@@ -1211,15 +1226,19 @@
             },
 
             hide: function () {
-                if (converse.animate) {
-                    this.$el.hide('fast');
-                } else {
-                    this.$el.hide();
+                if (this.$el.is(':visible') && this.$el.css('opacity') == "1") {
+                    if (converse.animate) {
+                        this.$el.hide('fast');
+                    } else {
+                        this.$el.hide();
+                    }
+                    converse.emit('onChatBoxClosed', this);
                 }
             },
 
             show: function () {
                 if (this.$el.is(':visible') && this.$el.css('opacity') == "1") {
+                    converse.emit('onChatBoxFocused', this);
                     return this.focus();
                 }
                 if (converse.animate) {
@@ -1232,6 +1251,7 @@
                     // localstorage
                     this.model.save();
                 }
+                converse.emit('onChatBoxOpened', this);
                 return this;
             },
 
@@ -2306,6 +2326,7 @@
                     // not broadcasted
                     return true;
                 }
+                converse.emit('onMessage', message);
                 var $forwarded = $message.children('forwarded');
                 if ($forwarded.length) {
                     $message = $forwarded.children('message');
@@ -2656,6 +2677,7 @@
             },
 
             rosterHandler: function (items) {
+                this.emit('onRoster', items);
                 this.cleanCache(items);
                 _.each(items, function (item, index, items) {
                     if (this.isSelf(item.jid)) { return; }
@@ -2979,7 +3001,7 @@
                 this.set({
                     'status' : this.get('status') || 'online'
                 });
-                this.on('change', $.proxy(function () {
+                this.on('change', $.proxy(function (item) {
                     if (this.get('fullname') === undefined) {
                         converse.getVCard(
                             null, // No 'to' attr when getting one's own vCard
@@ -2988,6 +3010,12 @@
                             }, this)
                         );
                     }
+                    if (_.has(item.changed, 'status')) {
+                        converse.emit('onStatusChanged', this.get('status'));
+                    }
+                    if (_.has(item.changed, 'status_message')) {
+                        converse.emit('onStatusMessageChanged', this.get('status_message'));
+                    }
                 }, this));
             },
 
@@ -3382,6 +3410,15 @@
     return {
         'initialize': function (settings, callback) {
             converse.initialize(settings, callback);
+        },
+        'once': function(evt, handler) {
+            converse.once(evt, handler);
+        },
+        'on': function(evt, handler) {
+            converse.on(evt, handler);
+        },
+        'off': function(evt, handler) {
+            converse.off(evt, handler);
         }
     };
 }));

+ 2 - 2
index.html

@@ -7,8 +7,8 @@
     <meta name="description" content="Converse.js: Open Source Browser-Based Instant Messaging" />
     <link rel="stylesheet" type="text/css" media="screen" href="stylesheets/stylesheet.css">
     <link rel="stylesheet" type="text/css" media="screen" href="converse.css">
-    <!--<script data-main="main" src="components/requirejs/require.js"></script>-->
-    <script src="builds/converse.min.js"></script>
+    <script data-main="main" src="components/requirejs/require.js"></script>
+    <!--<script src="builds/converse.min.js"></script>-->
     <title>Converse.js</title>
 </head>
 

+ 0 - 1
main.js

@@ -26,7 +26,6 @@ require.config({
         "crypto.mode-ctr": "components/otr/vendor/cryptojs/mode-ctr",
         "crypto": "src/crypto",
         "eventemitter": "components/otr/build/dep/eventemitter",
-        "jquery.eventemitter": "src/jquery.eventemitter",
         "otr": "components/otr/build/otr",
         "converse-dependencies": "src/deps-full"
     },

+ 0 - 1
src/deps-full.js

@@ -2,7 +2,6 @@ define("converse-dependencies", [
     "otr",
     "locales",
     "backbone.localStorage",
-    "jquery.eventemitter",
     "jquery.tinysort",
     "strophe",
     "strophe.muc",