added missing logo
[myslice.git] / plugins / messages / messages.js
index abc1817..47c2ba1 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);
                }
-                $.publish  ("messages:info", 'Subscribed to all 5 message channels');
+               // kind of patchy, notify the convenience functions that somebody is listening...
+               try {messages.ready=true;}
+               catch (err) { console.log("Could not set messages.ready");}
+               // 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');
             });
         },
         destroy : function( ) {
        this.options=options;
        this.plugindiv=plugindiv;
        /* cannot use 'this' directly of course */
-       (function (instance) { $( function () {instance.init_buttons();}) }) (this);
+       (function (instance) { $( function () {instance.initialize();}) }) (this);
 
        this.is_active = function (level) { 
-           return this.plugindiv.find("div.messages-buttons>input[name="+level+"]").get(0).checked;
+           return this.plugindiv.find("div.messages-buttons>input[name="+level+"]").attr('checked');
        }
        this.display_message = function (incoming, level) {
            var domid=this.plugindiv.attr('id');
            if ( ! this.is_active(level) ) html += " style='display:none'";
            html += ">";
            html += "<span class='messages-date'>" + new Date() + "</span>";
-           html += "<span class='messages-level'>[" + level + "]</span>";
+           html += "<span class='messages-level'>" + level + "</span>";
            //  html += "[" + domid + "]";
            html += " " + incoming + "</li>";
            $("ul#"+domid+".messages").append(html);
        },
 
-
-       this.init_buttons = function () {
-           this.plugindiv.find("div.messages-buttons>input").each(this.init_button);
+       this.initialize = function () {
+           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); });
        },
-       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;
-           input.checked=levels[level];
-           console.log ("init_button did set initial status of " + level + " to " + input.checked);
-           $(input).on('click',this.toggle_level);
+           if (levels[level]) $(input).attr('checked','checked');
        },
-
-       this.toggle_level = function () {
-           console.log("clicked, this= " + this + " this.name=" + this.name + " this.checked=" + this.checked);
+       this.arm_button = function (input,handler) {
+           $(input).click (handler);
+       },
+       /* as an event handler toggle_handler will see the DOM <input> as 'this' */
+       this.toggle_handler = function (e) {
+           var $this=$(this);
+           // toggle the state of the checkbox
+           if ($this.attr('checked')) $this.removeAttr('checked');
+           else $this.attr('checked',true);
+           // turn messages on or off
+           var level=this.name;
+           var display = $this.attr('checked') ? "list-item" : "none";
+           var plugindiv=$this.closest("div.Messages");
+           plugindiv.find("li."+level).css("display",display);
        }
 
     };
     
 })(jQuery);
 
-// temporary
-
+/* turn this on for an auto-test on startup
 var messages_test = {
     // set this to 0 to disable
     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);
@@ -113,4 +131,4 @@ var messages_test = {
     }
 }
 messages_test.run()
-
+*/