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.
This commit is contained in:
JC Brand 2013-12-15 16:58:46 +02:00
parent c6e57c0051
commit e729471bbf
4 changed files with 47 additions and 12 deletions

View File

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

View File

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

View File

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

View File

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