浏览代码

Make converse.js more modular

- Removed the $(document).ready handler. Instead, converse.initialize must be
  called manually together with settings data.
- As a result the Plone specific event listeners are now also gone.
- Update tests and index.html to deal wit these changes
JC Brand 12 年之前
父节点
当前提交
58241cbb74
共有 3 个文件被更改,包括 58 次插入64 次删除
  1. 34 53
      converse.js
  2. 16 6
      index.html
  3. 8 5
      spec/MainSpec.js

+ 34 - 53
converse.js

@@ -357,8 +357,8 @@
 
 
         isDifferentDay: function (prev_date, next_date) {
         isDifferentDay: function (prev_date, next_date) {
             return (
             return (
-                (next_date.getDate() != prev_date.getDate()) || 
-                (next_date.getFullYear() != prev_date.getFullYear()) || 
+                (next_date.getDate() != prev_date.getDate()) ||
+                (next_date.getFullYear() != prev_date.getFullYear()) ||
                 (next_date.getMonth() != prev_date.getMonth()));
                 (next_date.getMonth() != prev_date.getMonth()));
         },
         },
 
 
@@ -412,7 +412,7 @@
                 fullname: 'me',
                 fullname: 'me',
                 sender: 'me',
                 sender: 'me',
                 time: converse.toISOString(new Date()),
                 time: converse.toISOString(new Date()),
-                message: text 
+                message: text
             });
             });
         },
         },
 
 
@@ -778,7 +778,7 @@
                 if (_.has(item.changed, 'visible')) {
                 if (_.has(item.changed, 'visible')) {
                     if (item.changed.visible === true) {
                     if (item.changed.visible === true) {
                         this.show();
                         this.show();
-                    } 
+                    }
                 }
                 }
             }, this));
             }, this));
 
 
@@ -787,7 +787,7 @@
             this.model.on('hide', this.hide, this);
             this.model.on('hide', this.hide, this);
             if (this.model.get('visible')) {
             if (this.model.get('visible')) {
                 this.show();
                 this.show();
-            } 
+            }
         },
         },
 
 
         template: _.template(
         template: _.template(
@@ -830,7 +830,7 @@
                 this.contactspanel.$parent = this.$el;
                 this.contactspanel.$parent = this.$el;
                 this.contactspanel.render();
                 this.contactspanel.render();
                 // TODO: Only add the rooms panel if the server supports MUC
                 // TODO: Only add the rooms panel if the server supports MUC
-                this.roomspanel = new converse.RoomsPanel(); 
+                this.roomspanel = new converse.RoomsPanel();
                 this.roomspanel.$parent = this.$el;
                 this.roomspanel.$parent = this.$el;
                 this.roomspanel.render();
                 this.roomspanel.render();
             }
             }
@@ -918,14 +918,14 @@
 
 
 
 
             this.model.messages.on('add', this.showMessage, this);
             this.model.messages.on('add', this.showMessage, this);
-            this.model.on('destroy', function (model, response, options) { 
-                this.$el.hide('fast'); 
+            this.model.on('destroy', function (model, response, options) {
+                this.$el.hide('fast');
                 converse.connection.muc.leave(
                 converse.connection.muc.leave(
                     this.model.get('jid'),
                     this.model.get('jid'),
                     this.model.get('nick'),
                     this.model.get('nick'),
                     this.onLeave,
                     this.onLeave,
                     undefined);
                     undefined);
-            }, 
+            },
             this);
             this);
             this.$el.appendTo(converse.chatboxesview.$el);
             this.$el.appendTo(converse.chatboxesview.$el);
             this.render().show().model.messages.fetch({add: true});
             this.render().show().model.messages.fetch({add: true});
@@ -1061,14 +1061,14 @@
                         this.get('controlbox').set({visible:true}).save();
                         this.get('controlbox').set({visible:true}).save();
                     }
                     }
                 }, this)
                 }, this)
-            }); 
+            });
         },
         },
 
 
         messageReceived: function (message) {
         messageReceived: function (message) {
             var  partner_jid, $message = $(message),
             var  partner_jid, $message = $(message),
                  message_from = $message.attr('from');
                  message_from = $message.attr('from');
             if (message_from == converse.connection.jid) {
             if (message_from == converse.connection.jid) {
-                // FIXME: Forwarded messages should be sent to specific resources, 
+                // FIXME: Forwarded messages should be sent to specific resources,
                 // not broadcasted
                 // not broadcasted
                 return true;
                 return true;
             }
             }
@@ -1090,7 +1090,7 @@
             chatbox = this.get(partner_jid);
             chatbox = this.get(partner_jid);
             if (!chatbox) {
             if (!chatbox) {
                 converse.getVCard(
                 converse.getVCard(
-                    partner_jid, 
+                    partner_jid,
                     $.proxy(function (jid, fullname, image, image_type, url) {
                     $.proxy(function (jid, fullname, image, image_type, url) {
                         var chatbox = this.create({
                         var chatbox = this.create({
                             'id': jid,
                             'id': jid,
@@ -1425,7 +1425,7 @@
                     is_last = false;
                     is_last = false;
                     if (index === (items.length-1)) { is_last = true; }
                     if (index === (items.length-1)) { is_last = true; }
                     this.create({
                     this.create({
-                        jid: item.jid, 
+                        jid: item.jid,
                         subscription: item.subscription,
                         subscription: item.subscription,
                         ask: item.ask,
                         ask: item.ask,
                         fullname: item.name,
                         fullname: item.name,
@@ -1487,10 +1487,10 @@
                         converse.connection.roster.authorize(bare_jid);
                         converse.connection.roster.authorize(bare_jid);
                     } else {
                     } else {
                         converse.getVCard(
                         converse.getVCard(
-                            bare_jid, 
+                            bare_jid,
                             $.proxy(function (jid, fullname, img, img_type, url) {
                             $.proxy(function (jid, fullname, img, img_type, url) {
                                 this.add({
                                 this.add({
-                                    jid: bare_jid, 
+                                    jid: bare_jid,
                                     subscription: 'none',
                                     subscription: 'none',
                                     ask: 'request',
                                     ask: 'request',
                                     fullname: fullname,
                                     fullname: fullname,
@@ -1652,8 +1652,7 @@
                 crit = {order:'asc'};
                 crit = {order:'asc'};
             $my_contacts.after($my_contacts.siblings('dd.current-xmpp-contact.offline').tsort('a', crit));
             $my_contacts.after($my_contacts.siblings('dd.current-xmpp-contact.offline').tsort('a', crit));
             $my_contacts.after($my_contacts.siblings('dd.current-xmpp-contact.unavailable').tsort('a', crit));
             $my_contacts.after($my_contacts.siblings('dd.current-xmpp-contact.unavailable').tsort('a', crit));
-        },
-
+        }
     });
     });
 
 
     converse.XMPPStatus = Backbone.Model.extend({
     converse.XMPPStatus = Backbone.Model.extend({
@@ -1674,7 +1673,7 @@
         },
         },
 
 
         sendPresence: function (type) {
         sendPresence: function (type) {
-            var status_message = this.get('status_message'), 
+            var status_message = this.get('status_message'),
                 presence;
                 presence;
             // Most of these presence types are actually not explicitly sent,
             // Most of these presence types are actually not explicitly sent,
             // but I add all of them here fore reference and future proofing.
             // but I add all of them here fore reference and future proofing.
@@ -1782,7 +1781,7 @@
             if (!(_.has(model.changed, 'status')) && !(_.has(model.changed, 'status_message'))) {
             if (!(_.has(model.changed, 'status')) && !(_.has(model.changed, 'status_message'))) {
                 return;
                 return;
             }
             }
-            var stat = model.get('status'), 
+            var stat = model.get('status'),
                 status_message = model.get('status_message') || "I am " + this.getPrettyStatus(stat);
                 status_message = model.get('status_message') || "I am " + this.getPrettyStatus(stat);
             this.$el.find('#fancy-xmpp-status-select').html(
             this.$el.find('#fancy-xmpp-status-select').html(
                 this.status_template({
                 this.status_template({
@@ -1936,22 +1935,6 @@
         $('.conn-feedback').text(message);
         $('.conn-feedback').text(message);
     }
     }
 
 
-    converse.initialize = function (data) {
-        this.prebind = data.attr('prebind');
-        this.fullname = data.attr('fullname');
-        this.xhr_user_search = data.attr('xhr_user_search');
-        this.auto_subscribe = data.attr('auto_subscribe') === "True" || false;
-
-        this.chatboxes = new this.ChatBoxes();
-        this.chatboxesview = new this.ChatBoxesView({model: this.chatboxes});
-        $('a.toggle-online-users').bind(
-            'click', 
-            $.proxy(function (e) { 
-                e.preventDefault(); this.toggleControlBox(); 
-            }, this)
-        );
-    },
-
     converse.onConnected = function (connection) {
     converse.onConnected = function (connection) {
         this.animate = true; // Use animations
         this.animate = true; // Use animations
         this.connection = connection;
         this.connection = connection;
@@ -2002,24 +1985,22 @@
         this.giveFeedback('Online Contacts');
         this.giveFeedback('Online Contacts');
     };
     };
 
 
-    // Event handlers
-    // --------------
-    $(document).ready($.proxy(function () {
-        // TODO: This code is Plone specific and needs to be factored out
-        var data = $('div#collective-xmpp-chat-data');
-        this.initialize(data);
-        $(document).bind('jarnxmpp.connecting', $.proxy(function (ev, conn) {
-        this.giveFeedback('Connecting to chat...');
-        }, this));
-        $(document).bind('jarnxmpp.disconnected', $.proxy(function (ev, conn) {
-            this.giveFeedback('Unable to communicate with chat server').css('background-image', "url(images/error_icon.png)");
-            console.log("Connection Failed :(");
-        }, this));
-        $(document).unbind('jarnxmpp.connected');
-        $(document).bind('jarnxmpp.connected', $.proxy(function (ev, connection) {
-            this.onConnected(connection);
-        }, this));
-    }, converse));
+    converse.initialize = function (settings) {
+        this.prebind = settings.prebind;
+        this.fullname = settings.fullname;
+        this.xhr_user_search = settings.xhr_user_search;
+        this.auto_subscribe = settings.auto_subscribe;
+        this.animate = settings.animate;
+
+        this.chatboxes = new this.ChatBoxes();
+        this.chatboxesview = new this.ChatBoxesView({model: this.chatboxes});
+        $('a.toggle-online-users').bind(
+            'click',
+            $.proxy(function (e) {
+                e.preventDefault(); this.toggleControlBox();
+            }, this)
+        );
+    };
 
 
     return converse;
     return converse;
 }));
 }));

+ 16 - 6
index.html

@@ -131,10 +131,20 @@
     </div>
     </div>
 </div>
 </div>
 
 
-    <script type="text/javascript">
-        var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
-        document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
-    </script>
-    <script type="text/javascript">try { var pageTracker = _gat._getTracker("UA-2128260-8"); pageTracker._trackPageview(); } catch(err) {}</script>
-  </body>
+<script type="text/javascript">
+    $(document).ready(function {
+        converse.initialize({
+            prebind: false,
+            xhr_user_search: false,
+            auto_subscribe: false 
+        });
+    });
+</script>
+
+<script type="text/javascript">
+    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
+    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
+</script>
+<script type="text/javascript">try { var pageTracker = _gat._getTracker("UA-2128260-8"); pageTracker._trackPageview(); } catch(err) {}</script>
+</body>
 </html>
 </html>

+ 8 - 5
spec/MainSpec.js

@@ -21,13 +21,12 @@
             'Lena Grunewald', 'Laura Grunewald', 'Mandy Seiler', 'Sven Bosch', 'Nuriye Cuypers'
             'Lena Grunewald', 'Laura Grunewald', 'Mandy Seiler', 'Sven Bosch', 'Nuriye Cuypers'
         ];
         ];
         var num_contacts = req_names.length + pend_names.length + cur_names.length;
         var num_contacts = req_names.length + pend_names.length + cur_names.length;
-        this.bare_jid = 'dummy@localhost';
         mock_connection  = {
         mock_connection  = {
             'muc': {
             'muc': {
                 'listRooms': function () {},
                 'listRooms': function () {},
                 'join': function () {}
                 'join': function () {}
             },
             },
-            'jid': this.bare_jid,
+            'jid': 'dummy@localhost',
             'addHandler': function (handler, ns, name, type, id, from, options) { 
             'addHandler': function (handler, ns, name, type, id, from, options) { 
                 return function () {};
                 return function () {};
             },
             },
@@ -54,9 +53,13 @@
 
 
         // Clear localStorage
         // Clear localStorage
         window.localStorage.clear();
         window.localStorage.clear();
-        this.prebind = true;
+        this.initialize({
+            prebind: false,
+            xhr_user_search: false,
+            auto_subscribe: false,
+            animate: false
+        });
         this.onConnected(mock_connection);
         this.onConnected(mock_connection);
-        this.animate = false; // don't use animations
 
 
         // Variable declarations for specs
         // Variable declarations for specs
         var open_controlbox;
         var open_controlbox;
@@ -485,7 +488,7 @@
                     var sender_jid = cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
                     var sender_jid = cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
                         msg = $msg({
                         msg = $msg({
                             from: sender_jid,
                             from: sender_jid,
-                            to: this.bare_jid, 
+                            to: this.connection.jid, 
                             type: 'chat', 
                             type: 'chat', 
                             id: (new Date()).getTime()
                             id: (new Date()).getTime()
                         }).c('body').t(message).up()
                         }).c('body').t(message).up()