Browse Source

Move code from plugins to core.

Move init of RosterGroups collection and presenceHandler registration out of
the controlbox and rosterview plugins and into core, since this code is
generally useful (indeed necessary) even without those plugins.
JC Brand 9 years ago
parent
commit
081d377881
3 changed files with 29 additions and 19 deletions
  1. 1 5
      src/converse-controlbox.js
  2. 28 3
      src/converse-core.js
  3. 0 11
      src/converse-rosterview.js

+ 1 - 5
src/converse-controlbox.js

@@ -18,7 +18,6 @@
     "use strict";
     "use strict";
     // Strophe methods for building stanzas
     // Strophe methods for building stanzas
     var Strophe = converse_api.env.Strophe,
     var Strophe = converse_api.env.Strophe,
-        b64_sha1 = converse_api.env.b64_sha1,
         utils = converse_api.env.utils;
         utils = converse_api.env.utils;
     // Other necessary globals
     // Other necessary globals
     var $ = converse_api.env.jQuery,
     var $ = converse_api.env.jQuery,
@@ -246,10 +245,7 @@
                     /* We initialize the roster, which will appear inside the
                     /* We initialize the roster, which will appear inside the
                      * Contacts Panel.
                      * Contacts Panel.
                      */
                      */
-                    var rostergroups = new converse.RosterGroups();
-                    rostergroups.browserStorage = new Backbone.BrowserStorage[converse.storage](
-                        b64_sha1('converse.roster.groups'+converse.bare_jid));
-                    converse.rosterview = new converse.RosterView({model: rostergroups});
+                    converse.rosterview = new converse.RosterView({model: converse.rostergroups});
                     this.contactspanel.$el.append(converse.rosterview.$el);
                     this.contactspanel.$el.append(converse.rosterview.$el);
                     converse.rosterview.render().fetch().update();
                     converse.rosterview.render().fetch().update();
                     return this;
                     return this;

+ 28 - 3
src/converse-core.js

@@ -623,6 +623,7 @@
         };
         };
 
 
         this.afterReconnected = function () {
         this.afterReconnected = function () {
+            this.registerPresenceHandler();
             this.chatboxes.registerMessageHandler();
             this.chatboxes.registerMessageHandler();
             this.xmppstatus.sendPresence();
             this.xmppstatus.sendPresence();
             this.giveFeedback(__('Contacts'));
             this.giveFeedback(__('Contacts'));
@@ -664,13 +665,36 @@
             this.connection.send(carbons_iq);
             this.connection.send(carbons_iq);
         };
         };
 
 
-
-        this.onStatusInitialized = function () {
-            this.registerIntervalHandler();
+        this.initRoster = function () {
             this.roster = new this.RosterContacts();
             this.roster = new this.RosterContacts();
             this.roster.browserStorage = new Backbone.BrowserStorage[this.storage](
             this.roster.browserStorage = new Backbone.BrowserStorage[this.storage](
                 b64_sha1('converse.contacts-'+this.bare_jid));
                 b64_sha1('converse.contacts-'+this.bare_jid));
+            this.rostergroups = new converse.RosterGroups();
+            this.rostergroups.browserStorage = new Backbone.BrowserStorage[converse.storage](
+                b64_sha1('converse.roster.groups'+converse.bare_jid));
+        };
+
+        this.unregisterPresenceHandler = function () {
+            if (typeof this.presence_ref !== 'undefined') {
+                this.connection.deleteHandler(this.presence_ref);
+                delete this.presence_ref;
+            }
+        };
+
+        this.registerPresenceHandler = function () {
+            this.unregisterPresenceHandler();
+            this.presence_ref = converse.connection.addHandler(
+                function (presence) {
+                    converse.roster.presenceHandler(presence);
+                    return true;
+                }, null, 'presence', null);
+        };
+
+        this.onStatusInitialized = function () {
+            this.registerIntervalHandler();
+            this.initRoster();
             this.chatboxes.onConnected();
             this.chatboxes.onConnected();
+            this.registerPresenceHandler();
             this.giveFeedback(__('Contacts'));
             this.giveFeedback(__('Contacts'));
             if (typeof this.callback === 'function') {
             if (typeof this.callback === 'function') {
                 // XXX: Deprecate in favor of init_deferred
                 // XXX: Deprecate in favor of init_deferred
@@ -1806,6 +1830,7 @@
             /* Remove those views which are only allowed with a valid
             /* Remove those views which are only allowed with a valid
              * connection.
              * connection.
              */
              */
+            this.unregisterPresenceHandler();
             if (this.roster) {
             if (this.roster) {
                 this.roster.off().reset(); // Removes roster contacts
                 this.roster.off().reset(); // Removes roster contacts
             }
             }

+ 0 - 11
src/converse-rosterview.js

@@ -49,7 +49,6 @@
 
 
             afterReconnected: function () {
             afterReconnected: function () {
                 this.rosterview.registerRosterXHandler();
                 this.rosterview.registerRosterXHandler();
-                this.rosterview.registerPresenceHandler();
                 this._super.afterReconnected.apply(this, arguments);
                 this._super.afterReconnected.apply(this, arguments);
             },
             },
 
 
@@ -223,7 +222,6 @@
                 initialize: function () {
                 initialize: function () {
                     this.roster_handler_ref = this.registerRosterHandler();
                     this.roster_handler_ref = this.registerRosterHandler();
                     this.rosterx_handler_ref = this.registerRosterXHandler();
                     this.rosterx_handler_ref = this.registerRosterXHandler();
-                    this.presence_ref = this.registerPresenceHandler();
                     converse.roster.on("add", this.onContactAdd, this);
                     converse.roster.on("add", this.onContactAdd, this);
                     converse.roster.on('change', this.onContactChange, this);
                     converse.roster.on('change', this.onContactChange, this);
                     converse.roster.on("destroy", this.update, this);
                     converse.roster.on("destroy", this.update, this);
@@ -271,8 +269,6 @@
                     delete this.roster_handler_ref;
                     delete this.roster_handler_ref;
                     converse.connection.deleteHandler(this.rosterx_handler_ref);
                     converse.connection.deleteHandler(this.rosterx_handler_ref);
                     delete this.rosterx_handler_ref;
                     delete this.rosterx_handler_ref;
-                    converse.connection.deleteHandler(this.presence_ref);
-                    delete this.presence_ref;
                 },
                 },
 
 
                 update: _.debounce(function () {
                 update: _.debounce(function () {
@@ -391,13 +387,6 @@
                     );
                     );
                 },
                 },
 
 
-                registerPresenceHandler: function () {
-                    converse.connection.addHandler(
-                        function (presence) {
-                            converse.roster.presenceHandler(presence);
-                            return true;
-                        }.bind(this), null, 'presence', null);
-                },
 
 
                 onGroupAdd: function (group) {
                 onGroupAdd: function (group) {
                     var view = new converse.RosterGroupView({model: group});
                     var view = new converse.RosterGroupView({model: group});