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 @@
require.config({
paths: {
"jed": "Libraries/jed",
"locales": "Libraries/locales",
"af": "locale/af/LC_MESSAGES/af",
"en": "locale/en/LC_MESSAGES/en",
"sjcl": "Libraries/sjcl",
"tinysort": "Libraries/jquery.tinysort",
"underscore": "Libraries/underscore",
......@@ -49,7 +52,7 @@
});
define("converse", [
"jed",
"locales",
"localstorage",
"tinysort",
"sjcl",
......@@ -63,7 +66,7 @@
evaluate : /\{\[([\s\S]+?)\]\}/g,
interpolate : /\{\{([\s\S]+?)\}\}/g
};
return factory(jQuery, _, Jed, console);
return factory(jQuery, _, console);
}
);
} else {
......@@ -74,44 +77,36 @@
};
root.converse = factory(jQuery, _, console || {log: function(){}});
}
}(this, function ($, _, Jed, console) {
}(this, function ($, _, console) {
var converse = {};
var i18n = new Jed({
domain: "messages",
// This is the translation data, which is often generated by
// a po2json converter. You would ideally have one per locale
// and only pull in the locale_data that you need.
"locale_data" : {
// This is the domain key
"messages" : {
// The empty string key is used as the configuration
// block for each domain
"" : {
"domain" : "messages",
"lang" : "en",
"plural_forms" : "nplurals=2; plural=(n != 1);"
}
}
}
});
converse.initialize = function (settings) {
// 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;
this.i18n = locales.en;
_.extend(this, settings);
var __ = function (str) {
var t = i18n.translate(str);
var t = converse.i18n.translate(str);
if (arguments.length>1) {
return t.fetch.apply(t, [].slice.call(arguments,1));
} else {
return t.fetch();
}
};
converse.msg_counter = 0;
converse.autoLink = function (text) {
this.msg_counter = 0;
this.autoLink = function (text) {
// Convert URLs into hyperlinks
var re = /((http|https|ftp):\/\/[\w?=&.\/\-;#~%\-]+(?![\w\s?&.\/;#~%"=\-]*>))/g;
return text.replace(re, '<a target="_blank" href="$1">$1</a>');
};
converse.toISOString = function (date) {
this.toISOString = function (date) {
var pad;
if (typeof date.toISOString !== 'undefined') {
return date.toISOString();
......@@ -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.
*/
    var numericKeys = [1, 4, 5, 6, 7, 10, 11],
......@@ -153,7 +148,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 (document.title.search(/^Messages \(\d+\) /) == -1) {
document.title = "Messages (" + this.msg_counter + ") " + document.title;
......@@ -167,17 +162,17 @@
}
};
converse.incrementMsgCounter = function () {
this.incrementMsgCounter = function () {
this.msg_counter += 1;
this.updateMsgCounter();
};
converse.clearMsgCounter = function () {
this.clearMsgCounter = function () {
this.msg_counter = 0;
this.updateMsgCounter();
};
converse.collections = {
this.collections = {
/* 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
* Jabber servers.
......@@ -185,7 +180,7 @@
'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),
iq = $iq({'type':'get'})
.c('list', {'xmlns': this.URI,
......@@ -203,7 +198,7 @@
});
};
converse.collections.getLastMessages = function (jid, callback) {
this.collections.getLastMessages = function (jid, callback) {
var that = this;
this.getLastCollection(jid, function (result) {
// Retrieve the last page of a collection (max 30 elements).
......@@ -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
});
converse.ChatBox = Backbone.Model.extend({
this.ChatBox = Backbone.Model.extend({
initialize: function () {
if (this.get('box_id') !== 'controlbox') {
this.messages = new converse.Messages();
......@@ -287,7 +282,7 @@
}
});
converse.ChatBoxView = Backbone.View.extend({
this.ChatBoxView = Backbone.View.extend({
length: 200,
tagName: 'div',
className: 'chatbox',
......@@ -618,7 +613,7 @@
}
});
converse.ContactsPanel = Backbone.View.extend({
this.ContactsPanel = Backbone.View.extend({
tagName: 'div',
className: 'oc-chat-content',
id: 'users',
......@@ -757,7 +752,7 @@
}
});
converse.RoomsPanel = Backbone.View.extend({
this.RoomsPanel = Backbone.View.extend({
tagName: 'div',
id: 'chatrooms',
events: {
......@@ -854,6 +849,13 @@
}, 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) {
converse.connection.muc.listRooms(
this.muc_domain,
......@@ -863,7 +865,9 @@
$available_chatrooms = this.$el.find('#available-chatrooms');
this.rooms = $(iq).find('query').find('item');
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();
for (i=0; i<this.rooms.length; i++) {
name = Strophe.unescapeNode($(this.rooms[i]).attr('name')||$(this.rooms[i]).attr('jid'));
......@@ -876,15 +880,12 @@
$available_chatrooms.append(fragment);
$('input#show-rooms').show().siblings('span.spinner').remove();
} else {
$available_chatrooms.html('<dt>No rooms on '+this.muc_domain+'</dt>');
$('input#show-rooms').show().siblings('span.spinner').remove();
this.informNoRoomsFound();
}
return true;
}, this),
$.proxy(function (iq) { // Failure
var $available_chatrooms = this.$el.find('#available-chatrooms');
$available_chatrooms.html('<dt>No rooms on '+this.muc_domain+'</dt>');
$('input#show-rooms').show().siblings('span.spinner').remove();
this.informNoRoomsFound();
}, this));
},
......@@ -985,7 +986,7 @@
}
});
converse.ControlBoxView = converse.ChatBoxView.extend({
this.ControlBoxView = converse.ChatBoxView.extend({
tagName: 'div',
className: 'chatbox',
id: 'controlbox',
......@@ -1099,7 +1100,7 @@
}
});
converse.ChatRoomView = converse.ChatBoxView.extend({
this.ChatRoomView = converse.ChatBoxView.extend({
length: 300,
tagName: 'div',
className: 'chatroom',
......@@ -1410,19 +1411,19 @@
// # For translations: %1$s will be replaced with the user's nickname
// # Don't translate "strong"
// # 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
// # Don't translate "strong"
// # 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
// # Don't translate "strong"
// # 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
// # Don't translate "strong"
// # 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: {
......@@ -1627,7 +1628,7 @@
}
});
converse.ChatBoxes = Backbone.Collection.extend({
this.ChatBoxes = Backbone.Collection.extend({
model: converse.ChatBox,
onConnected: function () {
......@@ -1667,8 +1668,7 @@
messageReceived: function (message) {
var partner_jid, $message = $(message),
message_from = $message.attr('from'),
roster_item, chatbox;
message_from = $message.attr('from');
if (message_from == converse.connection.jid) {
// FIXME: Forwarded messages should be sent to specific resources,
// not broadcasted
......@@ -1680,7 +1680,7 @@
}
var from = Strophe.getBareJidFromJid(message_from),
to = Strophe.getBareJidFromJid($message.attr('to')),
resource, chatbox;
resource, chatbox, roster_item;
if (from == converse.bare_jid) {
// I am the sender, so this must be a forwarded message...
partner_jid = to;
......@@ -1707,7 +1707,7 @@
}
});
converse.ChatBoxesView = Backbone.View.extend({
this.ChatBoxesView = Backbone.View.extend({
el: '#collective-xmpp-chat-data',
initialize: function () {
......@@ -1738,7 +1738,7 @@
}
});
converse.RosterItem = Backbone.Model.extend({
this.RosterItem = Backbone.Model.extend({
initialize: function (attributes, options) {
var jid = attributes.jid;
if (!attributes.fullname) {
......@@ -1756,7 +1756,7 @@
}
});
converse.RosterItemView = Backbone.View.extend({
this.RosterItemView = Backbone.View.extend({
tagName: 'dd',
events: {
......@@ -1850,7 +1850,7 @@
}
});
converse.getVCard = function (jid, callback, errback) {
this.getVCard = function (jid, callback, errback) {
converse.connection.vcard.get($.proxy(function (iq) {
$vcard = $(iq).find('vCard');
var fullname = $vcard.find('FN').text(),
......@@ -1871,7 +1871,7 @@
}, this), jid, errback);
}
converse.RosterItems = Backbone.Collection.extend({
this.RosterItems = Backbone.Collection.extend({
model: converse.RosterItem,
comparator : function (rosteritem) {
var chat_status = rosteritem.get('chat_status'),
......@@ -2124,7 +2124,7 @@
}
});
converse.RosterView = Backbone.View.extend({
this.RosterView = Backbone.View.extend({
tagName: 'dl',
id: 'converse-roster',
rosteritemviews: {},
......@@ -2255,7 +2255,7 @@
}
});
converse.XMPPStatus = Backbone.Model.extend({
this.XMPPStatus = Backbone.Model.extend({
initialize: function () {
this.set({
'status' : this.get('status'),
......@@ -2310,7 +2310,7 @@
}
});
converse.XMPPStatusView = Backbone.View.extend({
this.XMPPStatusView = Backbone.View.extend({
el: "span#xmpp-status-holder",
events: {
......@@ -2366,13 +2366,15 @@
getPrettyStatus: function (stat) {
if (stat === 'chat') {
pretty_status = 'online';
pretty_status = __('online');
} else if (stat === 'dnd') {
pretty_status = 'busy';
pretty_status = __('busy');
} else if (stat === 'xa') {
pretty_status = 'away for long';
pretty_status = __('away for long');
} else if (stat === 'away') {
pretty_status = __('away');
} else {
pretty_status = stat || 'online';
pretty_status = __(stat) || __('online'); // XXX: Is 'online' the right default choice here?
}
return pretty_status;
},
......@@ -2434,8 +2436,8 @@
}
});
converse.Feature = Backbone.Model.extend();
converse.Features = Backbone.Collection.extend({
this.Feature = Backbone.Model.extend();
this.Features = Backbone.Collection.extend({
/* Service Discovery
* -----------------
* This collection stores Feature Models, representing features
......@@ -2482,7 +2484,7 @@
}
});
converse.LoginPanel = Backbone.View.extend({
this.LoginPanel = Backbone.View.extend({
tagName: 'div',
id: "login-dialog",
events: {
......@@ -2586,7 +2588,7 @@
}
});
converse.showControlBox = function () {
this.showControlBox = function () {
var controlbox = this.chatboxes.get('controlbox');
if (!controlbox) {
this.chatboxes.add({
......@@ -2602,7 +2604,7 @@
}
};
converse.toggleControlBox = function () {
this.toggleControlBox = function () {
if ($("div#controlbox").is(':visible')) {
var controlbox = this.chatboxes.get('controlbox');
if (this.connection) {
......@@ -2615,7 +2617,7 @@
}
};
converse.giveFeedback = function (message, klass) {
this.giveFeedback = function (message, klass) {
$('.conn-feedback').text(message);
$('.conn-feedback').attr('class', 'conn-feedback');
if (klass) {
......@@ -2623,7 +2625,7 @@
}
};
converse.onConnected = function (connection) {
this.onConnected = function (connection) {
this.connection = connection;
this.connection.xmlInput = function (body) { console.log(body); };
this.connection.xmlOutput = function (body) { console.log(body); };
......@@ -2668,16 +2670,7 @@
this.giveFeedback(__('Online Contacts'));
};
converse.initialize = function (settings) {
// 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 is the end of the initialize method.
this.chatboxes = new this.ChatBoxes();
this.chatboxesview = new this.ChatBoxesView({model: this.chatboxes});
$('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