浏览代码

Add code to generically and recursively update user settings.

Also moved chatview specific settings.
JC Brand 8 年之前
父节点
当前提交
01c3a50cc2
共有 6 个文件被更改,包括 90 次插入38 次删除
  1. 49 0
      spec/utils.js
  2. 7 1
      src/converse-chatview.js
  3. 16 35
      src/converse-core.js
  4. 1 2
      src/converse-muc.js
  5. 16 0
      src/utils.js
  6. 1 0
      tests/main.js

+ 49 - 0
spec/utils.js

@@ -0,0 +1,49 @@
+(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();
+        });
+    });
+}));

+ 7 - 1
src/converse-chatview.js

@@ -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',

+ 16 - 35
src/converse-core.js

@@ -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,

+ 1 - 2
src/converse-muc.js

@@ -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
             });
 
 

+ 16 - 0
src/utils.js

@@ -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.

+ 1 - 0
tests/main.js

@@ -69,6 +69,7 @@ require([
             require([
                 "console-runner",
                 //"spec/transcripts",
+                "spec/utils",
                 "spec/converse",
                 "spec/bookmarks",
                 "spec/headline",