messages can be passed a levels dict, or for convenience 'ALL' or 'NONE'
authorThierry Parmentelat <thierry.parmentelat@inria.fr>
Thu, 11 Apr 2013 09:18:34 +0000 (11:18 +0200)
committerThierry Parmentelat <thierry.parmentelat@inria.fr>
Thu, 11 Apr 2013 09:18:34 +0000 (11:18 +0200)
plugins/messages/messages.js
plugins/messages/messages.py

index eb22aca..76f1fb3 100644 (file)
@@ -17,8 +17,6 @@
        }    
     };
 
-    var levels = {'fatal': true, 'error': true, 'warning' : true, 'info' : true, 'debug' : false};
-
     var methods = {
         init : function( options ) {
 
                 var $this = $(this);
                instance=new Messages (options,$this);
                $this.data('Messages',instance);
-               for (level in levels) {
+               for (level in options.levels) {
                    (function (instance,level) {
-                       $.subscribe("messages:"+level, function (e, msg){ instance.display_message (msg,level)});
+                       $.subscribe("/messages/"+level, function (e, msg){ instance.display_message (msg,level)});
                    }) (instance,level);
                }
                // this happens very early - even before the document is loaded
                // so it won't show right away; no big deal though
-                $.publish  ("messages:info", 'Subscribed to all 5 message channels');
+                $.publish  ("/messages/info", 'Subscribed to all 5 message channels');
             });
         },
         destroy : function( ) {
        },
 
        this.initialize = function () {
-           this.plugindiv.find("div.messages-buttons>input").each(this.init_button);
+           var init_button=this.init_button;
+           var levels=this.options.levels;
+           this.plugindiv.find("div.messages-buttons>input").each(
+               function (i,input) {init_button (input, levels)});
            var arm_button=this.arm_button;
            var toggle_handler=this.toggle_handler;
            this.plugindiv.find("div.messages-buttons>input").each(
-               function (i,input) {arm_button (input,toggle_handler); }
-           );
+               function (i,input) {arm_button (input,toggle_handler); });
        },
-       this.init_button = function (_,input) {
+       this.init_button = function (input,levels) {
            /* set initial 'checked' state for that input from global 'levels' above */
            var level=input.name;
            if (levels[level]) $(input).attr('checked','checked');
@@ -114,11 +114,11 @@ var messages_test = {
     counter : 2,
     period : 1000,
     sample : function () { 
-       $.publish("messages:fatal","a fatal message (" + messages_test.counter + " runs to go)");
-       $.publish("messages:error","an error message");
-       $.publish("messages:warning","a warning message");
-       $.publish("messages:info","an info message");
-       $.publish("messages:debug","a debug message");
+       $.publish("/messages/fatal","a fatal message (" + messages_test.counter + " runs to go)");
+       $.publish("/messages/error","an error message");
+       $.publish("/messages/warning","a warning message");
+       $.publish("/messages/info","an info message");
+       $.publish("/messages/debug","a debug message");
        messages_test.counter -= 1;
        if (messages_test.counter == 0)
            window.clearInterval (messages_test.interval_id);
index dd1373d..0faee93 100644 (file)
@@ -1,9 +1,17 @@
 from unfold.plugin import Plugin
 
+# lists levels and sets them to enabled or not at startup
+default_levels = {'fatal': True, 'error': True, 'warning' : True, 'info' : True, 'debug' : False}
+
 class Messages (Plugin):
 
-    def __init__ (self, **settings):
+    def __init__ (self, levels=None, **settings):
         Plugin.__init__ (self, **settings)
+        if levels is None: levels=default_levels
+        # shortcut: 'ALL' turn everything on
+        elif levels=='ALL': levels=dict( [ (k,True) for k in default_levels ] )
+        elif levels=='NONE': levels=dict( [ (k,False) for k in default_levels ] )
+        self.levels=levels
 
     def template_file (self):
         return "messages.html"
@@ -19,7 +27,7 @@ class Messages (Plugin):
         return True
     # the js plugin expects a domid
     def json_settings_list (self):
-        return [ 'plugin_uuid' ]
+        return [ 'plugin_uuid', 'levels' ]
 
     # and we don't need a spin wheel 
     def start_with_spin (self):