Commit 9b9e514e authored by JC Brand's avatar JC Brand

Move everything into the initialize method

This is so that the code only gets parsed once initialized is called.

We want the code to be parsed only then, because this allows us to
specifiy the translation language as a configuration setting to initialize.
parent 62e701f3
...@@ -15,6 +15,9 @@ ...@@ -15,6 +15,9 @@
require.config({ require.config({
paths: { paths: {
"jed": "Libraries/jed", "jed": "Libraries/jed",
"locales": "Libraries/locales",
"af": "locale/af/LC_MESSAGES/af",
"en": "locale/en/LC_MESSAGES/en",
"sjcl": "Libraries/sjcl", "sjcl": "Libraries/sjcl",
"tinysort": "Libraries/jquery.tinysort", "tinysort": "Libraries/jquery.tinysort",
"underscore": "Libraries/underscore", "underscore": "Libraries/underscore",
...@@ -49,7 +52,7 @@ ...@@ -49,7 +52,7 @@
}); });
define("converse", [ define("converse", [
"jed", "locales",
"localstorage", "localstorage",
"tinysort", "tinysort",
"sjcl", "sjcl",
...@@ -63,7 +66,7 @@ ...@@ -63,7 +66,7 @@
evaluate : /\{\[([\s\S]+?)\]\}/g, evaluate : /\{\[([\s\S]+?)\]\}/g,
interpolate : /\{\{([\s\S]+?)\}\}/g interpolate : /\{\{([\s\S]+?)\}\}/g
}; };
return factory(jQuery, _, Jed, console); return factory(jQuery, _, console);
} }
); );
} else { } else {
...@@ -74,44 +77,36 @@ ...@@ -74,44 +77,36 @@
}; };
root.converse = factory(jQuery, _, console || {log: function(){}}); root.converse = factory(jQuery, _, console || {log: function(){}});
} }
}(this, function ($, _, Jed, console) { }(this, function ($, _, console) {
var converse = {}; var converse = {};
var i18n = new Jed({ converse.initialize = function (settings) {
domain: "messages", // Default values
// This is the translation data, which is often generated by this.bosh_service_url = ''; // The BOSH connection manager URL. Required if you are not prebinding.
// a po2json converter. You would ideally have one per locale this.animate = true;
// and only pull in the locale_data that you need. this.auto_list_rooms = false;
"locale_data" : { this.auto_subscribe = false;
// This is the domain key this.hide_muc_server = false;
"messages" : { this.prebind = false;
// The empty string key is used as the configuration this.xhr_user_search = false;
// block for each domain this.i18n = locales.en;
"" : { _.extend(this, settings);
"domain" : "messages",
"lang" : "en",
"plural_forms" : "nplurals=2; plural=(n != 1);"
}
}
}
});
var __ = function (str) { var __ = function (str) {
var t = i18n.translate(str); var t = converse.i18n.translate(str);
if (arguments.length>1) { if (arguments.length>1) {
return t.fetch.apply(t, [].slice.call(arguments,1)); return t.fetch.apply(t, [].slice.call(arguments,1));
} else { } else {
return t.fetch(); return t.fetch();
} }
}; };
this.msg_counter = 0;
converse.msg_counter = 0; this.autoLink = function (text) {
converse.autoLink = function (text) {
// Convert URLs into hyperlinks // Convert URLs into hyperlinks
var re = /((http|https|ftp):\/\/[\w?=&.\/\-;#~%\-]+(?![\w\s?&.\/;#~%"=\-]*>))/g; var re = /((http|https|ftp):\/\/[\w?=&.\/\-;#~%\-]+(?![\w\s?&.\/;#~%"=\-]*>))/g;
return text.replace(re, '<a target="_blank" href="$1">$1</a>'); return text.replace(re, '<a target="_blank" href="$1">$1</a>');
}; };
converse.toISOString = function (date) { this.toISOString = function (date) {
var pad; var pad;
if (typeof date.toISOString !== 'undefined') { if (typeof date.toISOString !== 'undefined') {
return date.toISOString(); return date.toISOString();
...@@ -129,7 +124,7 @@ ...@@ -129,7 +124,7 @@
} }
}; };
converse.parseISO8601 = function (datestr) { this.parseISO8601 = function (datestr) {
/* Parses string formatted as 2013-02-14T11:27:08.268Z to a Date obj. /* Parses string formatted as 2013-02-14T11:27:08.268Z to a Date obj.
*/ */
    var numericKeys = [1, 4, 5, 6, 7, 10, 11],     var numericKeys = [1, 4, 5, 6, 7, 10, 11],
...@@ -153,7 +148,7 @@ ...@@ -153,7 +148,7 @@
return new Date(Date.UTC(struct[1], struct[2], struct[3], struct[4], struct[5] + minutesOffset, struct[6], struct[7])); return new Date(Date.UTC(struct[1], struct[2], struct[3], struct[4], struct[5] + minutesOffset, struct[6], struct[7]));
}; };
converse.updateMsgCounter = function () { this.updateMsgCounter = function () {
if (this.msg_counter > 0) { if (this.msg_counter > 0) {
if (document.title.search(/^Messages \(\d+\) /) == -1) { if (document.title.search(/^Messages \(\d+\) /) == -1) {
document.title = "Messages (" + this.msg_counter + ") " + document.title; document.title = "Messages (" + this.msg_counter + ") " + document.title;
...@@ -167,17 +162,17 @@ ...@@ -167,17 +162,17 @@
} }
}; };
converse.incrementMsgCounter = function () { this.incrementMsgCounter = function () {
this.msg_counter += 1; this.msg_counter += 1;
this.updateMsgCounter(); this.updateMsgCounter();
}; };
converse.clearMsgCounter = function () { this.clearMsgCounter = function () {
this.msg_counter = 0; this.msg_counter = 0;
this.updateMsgCounter(); this.updateMsgCounter();
}; };
converse.collections = { this.collections = {
/* FIXME: XEP-0136 specifies 'urn:xmpp:archive' but the mod_archive_odbc /* FIXME: XEP-0136 specifies 'urn:xmpp:archive' but the mod_archive_odbc
* add-on for ejabberd wants the URL below. This might break for other * add-on for ejabberd wants the URL below. This might break for other
* Jabber servers. * Jabber servers.
...@@ -185,7 +180,7 @@ ...@@ -185,7 +180,7 @@
'URI': 'http://www.xmpp.org/extensions/xep-0136.html#ns' 'URI': 'http://www.xmpp.org/extensions/xep-0136.html#ns'
}; };
converse.collections.getLastCollection = function (jid, callback) { this.collections.getLastCollection = function (jid, callback) {
var bare_jid = Strophe.getBareJidFromJid(jid), var bare_jid = Strophe.getBareJidFromJid(jid),
iq = $iq({'type':'get'}) iq = $iq({'type':'get'})
.c('list', {'xmlns': this.URI, .c('list', {'xmlns': this.URI,
...@@ -203,7 +198,7 @@ ...@@ -203,7 +198,7 @@
}); });
}; };
converse.collections.getLastMessages = function (jid, callback) { this.collections.getLastMessages = function (jid, callback) {
var that = this; var that = this;
this.getLastCollection(jid, function (result) { this.getLastCollection(jid, function (result) {
// Retrieve the last page of a collection (max 30 elements). // Retrieve the last page of a collection (max 30 elements).
...@@ -222,13 +217,13 @@ ...@@ -222,13 +217,13 @@
}); });
}; };
converse.Message = Backbone.Model.extend(); this.Message = Backbone.Model.extend();
converse.Messages = Backbone.Collection.extend({ this.Messages = Backbone.Collection.extend({
model: converse.Message model: converse.Message
}); });
converse.ChatBox = Backbone.Model.extend({ this.ChatBox = Backbone.Model.extend({
initialize: function () { initialize: function () {
if (this.get('box_id') !== 'controlbox') { if (this.get('box_id') !== 'controlbox') {
this.messages = new converse.Messages(); this.messages = new converse.Messages();
...@@ -287,7 +282,7 @@ ...@@ -287,7 +282,7 @@
} }
}); });
converse.ChatBoxView = Backbone.View.extend({ this.ChatBoxView = Backbone.View.extend({
length: 200, length: 200,
tagName: 'div', tagName: 'div',
className: 'chatbox', className: 'chatbox',
...@@ -618,7 +613,7 @@ ...@@ -618,7 +613,7 @@
} }
}); });
converse.ContactsPanel = Backbone.View.extend({ this.ContactsPanel = Backbone.View.extend({
tagName: 'div', tagName: 'div',
className: 'oc-chat-content', className: 'oc-chat-content',
id: 'users', id: 'users',
...@@ -757,7 +752,7 @@ ...@@ -757,7 +752,7 @@
} }
}); });
converse.RoomsPanel = Backbone.View.extend({ this.RoomsPanel = Backbone.View.extend({
tagName: 'div', tagName: 'div',
id: 'chatrooms', id: 'chatrooms',
events: { events: {
...@@ -854,6 +849,13 @@ ...@@ -854,6 +849,13 @@
}, this)); }, this));
}, },
informNoRoomsFound: function () {
var $available_chatrooms = this.$el.find('#available-chatrooms');
// # For translators: %1$s is a variable and will be replaced with the XMPP server name
$available_chatrooms.html('<dt>'+__('No rooms on %1$s',this.muc_domain)+'</dt>');
$('input#show-rooms').show().siblings('span.spinner').remove();
},
updateRoomsList: function (domain) { updateRoomsList: function (domain) {
converse.connection.muc.listRooms( converse.connection.muc.listRooms(
this.muc_domain, this.muc_domain,
...@@ -863,7 +865,9 @@ ...@@ -863,7 +865,9 @@
$available_chatrooms = this.$el.find('#available-chatrooms'); $available_chatrooms = this.$el.find('#available-chatrooms');
this.rooms = $(iq).find('query').find('item'); this.rooms = $(iq).find('query').find('item');
if (this.rooms.length) { if (this.rooms.length) {
$available_chatrooms.html('<dt>Rooms on '+this.muc_domain+'</dt>'); // # For translators: %1$s is a variable and will be
// # replaced with the XMPP server name
$available_chatrooms.html('<dt>'+__('Rooms on %1$s',this.muc_domain)+'</dt>');
fragment = document.createDocumentFragment(); fragment = document.createDocumentFragment();
for (i=0; i<this.rooms.length; i++) { for (i=0; i<this.rooms.length; i++) {
name = Strophe.unescapeNode($(this.rooms[i]).attr('name')||$(this.rooms[i]).attr('jid')); name = Strophe.unescapeNode($(this.rooms[i]).attr('name')||$(this.rooms[i]).attr('jid'));
...@@ -876,15 +880,12 @@ ...@@ -876,15 +880,12 @@
$available_chatrooms.append(fragment); $available_chatrooms.append(fragment);
$('input#show-rooms').show().siblings('span.spinner').remove(); $('input#show-rooms').show().siblings('span.spinner').remove();
} else { } else {
$available_chatrooms.html('<dt>No rooms on '+this.muc_domain+'</dt>'); this.informNoRoomsFound();
$('input#show-rooms').show().siblings('span.spinner').remove();
} }
return true; return true;
}, this), }, this),
$.proxy(function (iq) { // Failure $.proxy(function (iq) { // Failure
var $available_chatrooms = this.$el.find('#available-chatrooms'); this.informNoRoomsFound();
$available_chatrooms.html('<dt>No rooms on '+this.muc_domain+'</dt>');
$('input#show-rooms').show().siblings('span.spinner').remove();
}, this)); }, this));
}, },
...@@ -985,7 +986,7 @@ ...@@ -985,7 +986,7 @@
} }
}); });
converse.ControlBoxView = converse.ChatBoxView.extend({ this.ControlBoxView = converse.ChatBoxView.extend({
tagName: 'div', tagName: 'div',
className: 'chatbox', className: 'chatbox',
id: 'controlbox', id: 'controlbox',
...@@ -1099,7 +1100,7 @@ ...@@ -1099,7 +1100,7 @@
} }
}); });
converse.ChatRoomView = converse.ChatBoxView.extend({ this.ChatRoomView = converse.ChatBoxView.extend({
length: 300, length: 300,
tagName: 'div', tagName: 'div',
className: 'chatroom', className: 'chatroom',
...@@ -1410,19 +1411,19 @@ ...@@ -1410,19 +1411,19 @@
// # For translations: %1$s will be replaced with the user's nickname // # For translations: %1$s will be replaced with the user's nickname
// # Don't translate "strong" // # Don't translate "strong"
// # Example: <strong>jcbrand</strong> has been banned // # Example: <strong>jcbrand</strong> has been banned
301: i18n.translate('<strong>%1$s</strong> has been banned'), 301: converse.i18n.translate('<strong>%1$s</strong> has been banned'),
// # For translations: %1$s will be replaced with the user's nickname // # For translations: %1$s will be replaced with the user's nickname
// # Don't translate "strong" // # Don't translate "strong"
// # Example: <strong>jcbrand</strong> has been kicked out // # Example: <strong>jcbrand</strong> has been kicked out
307: i18n.translate('<strong>%1$s</strong> has been kicked out'), 307: converse.i18n.translate('<strong>%1$s</strong> has been kicked out'),
// # For translations: %1$s will be replaced with the user's nickname // # For translations: %1$s will be replaced with the user's nickname
// # Don't translate "strong" // # Don't translate "strong"
// # Example: <strong>jcbrand</strong> has been removed because of an affiliasion change // # Example: <strong>jcbrand</strong> has been removed because of an affiliasion change
321: i18n.translate("<strong>%1$s</strong> has been removed because of an affiliation change"), 321: converse.i18n.translate("<strong>%1$s</strong> has been removed because of an affiliation change"),
// # For translations: %1$s will be replaced with the user's nickname // # For translations: %1$s will be replaced with the user's nickname
// # Don't translate "strong" // # Don't translate "strong"
// # Example: <strong>jcbrand</strong> has been removed for not being a member // # Example: <strong>jcbrand</strong> has been removed for not being a member
322: i18n.translate("<strong>%1$s</strong> has been removed for not being a member") 322: converse.i18n.translate("<strong>%1$s</strong> has been removed for not being a member")
}, },
disconnectMessages: { disconnectMessages: {
...@@ -1627,7 +1628,7 @@ ...@@ -1627,7 +1628,7 @@
} }
}); });
converse.ChatBoxes = Backbone.Collection.extend({ this.ChatBoxes = Backbone.Collection.extend({
model: converse.ChatBox, model: converse.ChatBox,
onConnected: function () { onConnected: function () {
...@@ -1667,8 +1668,7 @@ ...@@ -1667,8 +1668,7 @@
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');
roster_item, chatbox;
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
...@@ -1680,7 +1680,7 @@ ...@@ -1680,7 +1680,7 @@
} }
var from = Strophe.getBareJidFromJid(message_from), var from = Strophe.getBareJidFromJid(message_from),
to = Strophe.getBareJidFromJid($message.attr('to')), to = Strophe.getBareJidFromJid($message.attr('to')),
resource, chatbox; resource, chatbox, roster_item;
if (from == converse.bare_jid) { if (from == converse.bare_jid) {
// I am the sender, so this must be a forwarded message... // I am the sender, so this must be a forwarded message...
partner_jid = to; partner_jid = to;
...@@ -1707,7 +1707,7 @@ ...@@ -1707,7 +1707,7 @@
} }
}); });
converse.ChatBoxesView = Backbone.View.extend({ this.ChatBoxesView = Backbone.View.extend({
el: '#collective-xmpp-chat-data', el: '#collective-xmpp-chat-data',
initialize: function () { initialize: function () {
...@@ -1738,7 +1738,7 @@ ...@@ -1738,7 +1738,7 @@
} }
}); });
converse.RosterItem = Backbone.Model.extend({ this.RosterItem = Backbone.Model.extend({
initialize: function (attributes, options) { initialize: function (attributes, options) {
var jid = attributes.jid; var jid = attributes.jid;
if (!attributes.fullname) { if (!attributes.fullname) {
...@@ -1756,7 +1756,7 @@ ...@@ -1756,7 +1756,7 @@
} }
}); });
converse.RosterItemView = Backbone.View.extend({ this.RosterItemView = Backbone.View.extend({
tagName: 'dd', tagName: 'dd',
events: { events: {
...@@ -1850,7 +1850,7 @@ ...@@ -1850,7 +1850,7 @@
} }
}); });
converse.getVCard = function (jid, callback, errback) { this.getVCard = function (jid, callback, errback) {
converse.connection.vcard.get($.proxy(function (iq) { converse.connection.vcard.get($.proxy(function (iq) {
$vcard = $(iq).find('vCard'); $vcard = $(iq).find('vCard');
var fullname = $vcard.find('FN').text(), var fullname = $vcard.find('FN').text(),
...@@ -1871,7 +1871,7 @@ ...@@ -1871,7 +1871,7 @@
}, this), jid, errback); }, this), jid, errback);
} }
converse.RosterItems = Backbone.Collection.extend({ this.RosterItems = Backbone.Collection.extend({
model: converse.RosterItem, model: converse.RosterItem,
comparator : function (rosteritem) { comparator : function (rosteritem) {
var chat_status = rosteritem.get('chat_status'), var chat_status = rosteritem.get('chat_status'),
...@@ -2124,7 +2124,7 @@ ...@@ -2124,7 +2124,7 @@
} }
}); });
converse.RosterView = Backbone.View.extend({ this.RosterView = Backbone.View.extend({
tagName: 'dl', tagName: 'dl',
id: 'converse-roster', id: 'converse-roster',
rosteritemviews: {}, rosteritemviews: {},
...@@ -2255,7 +2255,7 @@ ...@@ -2255,7 +2255,7 @@
} }
}); });
converse.XMPPStatus = Backbone.Model.extend({ this.XMPPStatus = Backbone.Model.extend({
initialize: function () { initialize: function () {
this.set({ this.set({
'status' : this.get('status'), 'status' : this.get('status'),
...@@ -2310,7 +2310,7 @@ ...@@ -2310,7 +2310,7 @@
} }
}); });
converse.XMPPStatusView = Backbone.View.extend({ this.XMPPStatusView = Backbone.View.extend({
el: "span#xmpp-status-holder", el: "span#xmpp-status-holder",
events: { events: {
...@@ -2366,13 +2366,15 @@ ...@@ -2366,13 +2366,15 @@
getPrettyStatus: function (stat) { getPrettyStatus: function (stat) {
if (stat === 'chat') { if (stat === 'chat') {
pretty_status = 'online'; pretty_status = __('online');
} else if (stat === 'dnd') { } else if (stat === 'dnd') {
pretty_status = 'busy'; pretty_status = __('busy');
} else if (stat === 'xa') { } else if (stat === 'xa') {
pretty_status = 'away for long'; pretty_status = __('away for long');
} else if (stat === 'away') {
pretty_status = __('away');
} else { } else {
pretty_status = stat || 'online'; pretty_status = __(stat) || __('online'); // XXX: Is 'online' the right default choice here?
} }
return pretty_status; return pretty_status;
}, },
...@@ -2434,8 +2436,8 @@ ...@@ -2434,8 +2436,8 @@
} }
}); });
converse.Feature = Backbone.Model.extend(); this.Feature = Backbone.Model.extend();
converse.Features = Backbone.Collection.extend({ this.Features = Backbone.Collection.extend({
/* Service Discovery /* Service Discovery
* ----------------- * -----------------
* This collection stores Feature Models, representing features * This collection stores Feature Models, representing features
...@@ -2482,7 +2484,7 @@ ...@@ -2482,7 +2484,7 @@
} }
}); });
converse.LoginPanel = Backbone.View.extend({ this.LoginPanel = Backbone.View.extend({
tagName: 'div', tagName: 'div',
id: "login-dialog", id: "login-dialog",
events: { events: {
...@@ -2586,7 +2588,7 @@ ...@@ -2586,7 +2588,7 @@
} }
}); });
converse.showControlBox = function () { this.showControlBox = function () {
var controlbox = this.chatboxes.get('controlbox'); var controlbox = this.chatboxes.get('controlbox');
if (!controlbox) { if (!controlbox) {
this.chatboxes.add({ this.chatboxes.add({
...@@ -2602,7 +2604,7 @@ ...@@ -2602,7 +2604,7 @@
} }
}; };
converse.toggleControlBox = function () { this.toggleControlBox = function () {
if ($("div#controlbox").is(':visible')) { if ($("div#controlbox").is(':visible')) {
var controlbox = this.chatboxes.get('controlbox'); var controlbox = this.chatboxes.get('controlbox');
if (this.connection) { if (this.connection) {
...@@ -2615,7 +2617,7 @@ ...@@ -2615,7 +2617,7 @@
} }
}; };
converse.giveFeedback = function (message, klass) { this.giveFeedback = function (message, klass) {
$('.conn-feedback').text(message); $('.conn-feedback').text(message);
$('.conn-feedback').attr('class', 'conn-feedback'); $('.conn-feedback').attr('class', 'conn-feedback');
if (klass) { if (klass) {
...@@ -2623,7 +2625,7 @@ ...@@ -2623,7 +2625,7 @@
} }
}; };
converse.onConnected = function (connection) { this.onConnected = function (connection) {
this.connection = connection; this.connection = connection;
this.connection.xmlInput = function (body) { console.log(body); }; this.connection.xmlInput = function (body) { console.log(body); };
this.connection.xmlOutput = function (body) { console.log(body); }; this.connection.xmlOutput = function (body) { console.log(body); };
...@@ -2668,16 +2670,7 @@ ...@@ -2668,16 +2670,7 @@
this.giveFeedback(__('Online Contacts')); this.giveFeedback(__('Online Contacts'));
}; };
converse.initialize = function (settings) { // This is the end of the initialize method.
// Default values
this.bosh_service_url = ''; // The BOSH connection manager URL. Required if you are not prebinding.
this.animate = true;
this.auto_list_rooms = false;
this.auto_subscribe = false;
this.hide_muc_server = false;
this.prebind = false;
this.xhr_user_search = false;
_.extend(this, settings);
this.chatboxes = new this.ChatBoxes(); this.chatboxes = new this.ChatBoxes();
this.chatboxesview = new this.ChatBoxesView({model: this.chatboxes}); this.chatboxesview = new this.ChatBoxesView({model: this.chatboxes});
$('a.toggle-online-users').bind( $('a.toggle-online-users').bind(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment