Commit 01c3a50c authored by JC Brand's avatar JC Brand

Add code to generically and recursively update user settings.

Also moved chatview specific settings.
parent cb79ccb3
(function (root, factory) {
define(["converse-api"], factory);
} (this, function (converse_api) {
var utils = converse_api.env.utils,
_ = converse_api.env._;
return describe("Converse.js Utilities", function() {
it("applyUserSettings: recursively applies user settings", function () {
var context = {};
var settings = {
show_toolbar: true,
chatview_avatar_width: 32,
chatview_avatar_height: 32,
visible_toolbar_buttons: {
'emoticons': true,
'call': false,
'clear': true,
'toggle_occupants': true
}
};
_.extend(context, settings);
var user_settings = {
something_else: 'xxx',
show_toolbar: false,
chatview_avatar_width: 32,
chatview_avatar_height: 48,
visible_toolbar_buttons: {
'emoticons': false,
'call': false,
'toggle_occupants':false,
'invalid': false
}
};
utils.applyUserSettings(context, settings, user_settings);
expect(context.something_else).toBeUndefined();
expect(context.show_toolbar).toBeFalsy();
expect(context.chatview_avatar_width).toBe(32);
expect(context.chatview_avatar_height).toBe(48);
expect(Object.keys(context.visible_toolbar_buttons)).toEqual(Object.keys(settings.visible_toolbar_buttons));
expect(context.visible_toolbar_buttons.emoticons).toBeFalsy();
expect(context.visible_toolbar_buttons.call).toBeFalsy();
expect(context.visible_toolbar_buttons.toggle_occupants).toBeFalsy();
expect(context.visible_toolbar_buttons.invalid).toBeFalsy();
});
});
}));
......@@ -81,13 +81,19 @@
show_toolbar: true,
chatview_avatar_width: 32,
chatview_avatar_height: 32,
visible_toolbar_buttons: {
'emoticons': true,
'call': false,
'clear': true,
'toggle_occupants': true // Leaky abstraction from MUC
},
});
converse.ChatBoxView = Backbone.View.extend({
length: 200,
tagName: 'div',
className: 'chatbox',
is_chatroom: false, // This is not a multi-user chatroom
is_chatroom: false, // Leaky abstraction from MUC
events: {
'click .close-chatbox-button': 'close',
......
......@@ -179,29 +179,10 @@
this.PAUSED = 'paused';
this.GONE = 'gone';
// Detect support for the user's locale
// ------------------------------------
this.isConverseLocale = function (locale) { return typeof locales[locale] !== "undefined"; };
this.isMomentLocale = function (locale) { return moment.locale() !== moment.locale(locale); };
this.user_settings = settings; // Save the user settings so that they can be used by plugins
this.wrappedChatBox = function (chatbox) {
/* Wrap a chatbox for outside consumption (i.e. so that it can be
* returned via the API.
*/
if (!chatbox) { return; }
var view = converse.chatboxviews.get(chatbox.get('id'));
return {
'close': view.close.bind(view),
'focus': view.focus.bind(view),
'get': chatbox.get.bind(chatbox),
'open': view.show.bind(view),
'set': chatbox.set.bind(chatbox)
};
};
if (!moment.locale) { //moment.lang is deprecated after 2.8.1, use moment.locale instead
moment.locale = moment.lang;
}
......@@ -250,17 +231,10 @@
message_storage: 'session',
strict_plugin_dependencies: false,
synchronize_availability: true, // Set to false to not sync with other clients or with resource name of the particular client that it should synchronize with
visible_toolbar_buttons: {
'emoticons': true,
'call': false,
'clear': true,
'toggle_occupants': true
},
websocket_url: undefined,
xhr_custom_status: false,
xhr_custom_status_url: '',
};
_.extend(this, this.default_settings);
// Allow only whitelisted configuration attributes to be overwritten
_.extend(this, _.pick(settings, Object.keys(this.default_settings)));
......@@ -276,14 +250,6 @@
}
}
if (settings.visible_toolbar_buttons) {
_.extend(
this.visible_toolbar_buttons,
_.pick(settings.visible_toolbar_buttons, [
'emoticons', 'call', 'clear', 'toggle_occupants'
]
));
}
$.fx.off = !this.animate;
// Module-level variables
......@@ -299,9 +265,24 @@
*/
this.send_initial_presence = true;
this.msg_counter = 0;
this.user_settings = settings; // Save the user settings so that they can be used by plugins
// Module-level functions
// ----------------------
this.wrappedChatBox = function (chatbox) {
/* Wrap a chatbox for outside consumption (i.e. so that it can be
* returned via the API.
*/
if (!chatbox) { return; }
var view = converse.chatboxviews.get(chatbox.get('id'));
return {
'close': view.close.bind(view),
'focus': view.focus.bind(view),
'get': chatbox.get.bind(chatbox),
'open': view.show.bind(view),
'set': chatbox.set.bind(chatbox)
};
};
this.generateResource = function () {
return '/converse.js-' + Math.floor(Math.random()*139749825).toString();
......@@ -1960,7 +1941,7 @@
*/
_.extend(converse.default_settings, settings);
_.extend(converse, settings);
_.extend(converse, _.pick(converse.user_settings, Object.keys(settings)));
utils.applyUserSettings(converse, settings, converse.user_settings);
};
converse.pluggable.initializePlugins({
'updateSettings': updateSettings,
......
......@@ -208,8 +208,7 @@
hide_muc_server: false,
muc_history_max_stanzas: undefined,
muc_instant_rooms: true,
muc_nickname_from_jid: false,
show_toolbar: true,
muc_nickname_from_jid: false
});
......
......@@ -233,6 +233,22 @@
return false;
},
applyUserSettings: function applyUserSettings (context, settings, user_settings) {
/* Configuration settings might be nested objects. We only want to
* add settings which are whitelisted.
*/
for (var k in settings) {
if (_.isUndefined(user_settings[k])) {
continue;
}
if (_.isObject(settings[k])) {
applyUserSettings(context[k], settings[k], user_settings[k]);
} else {
context[k] = user_settings[k];
}
}
},
refreshWebkit: function () {
/* This works around a webkit bug. Refreshes the browser's viewport,
* otherwise chatboxes are not moved along when one is closed.
......
......@@ -69,6 +69,7 @@ require([
require([
"console-runner",
//"spec/transcripts",
"spec/utils",
"spec/converse",
"spec/bookmarks",
"spec/headline",
......
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