messages plugins is now new-style
[myslice.git] / plugins / messages / static / js / messages.js
1 /**
2  * Description: display messages in a dedicated area, with buttons for filtering on level
3  * Copyright (c) 2012 UPMC Sorbonne Universite - INRIA
4  * License: GPLv3
5  */
6
7 ( function($) {
8
9     var Messages = Plugin.extend ({
10
11         init: function (options, element) {
12             this._super (options, element);
13             // subscribe to the various messages channels
14             var self=this;
15             for (level in options.levels)
16                 (function (l) {
17                     $.subscribe("/messages/"+l, function (e, msg){ self.display_message (msg,l)});
18                 })(level);
19             // kind of patchy, notify the convenience functions that somebody is listening...
20             try {messages.ready=true;}
21             catch (err) { console.log("Could not set messages.ready");}
22             // this happens very early - even before the document is loaded
23             // so it won't show right away; no big deal though
24             $.publish ("/messages/info", 'Subscribed to all 5 message channels');
25
26             this.initialize();
27         },
28
29         initialize: function () {
30             var self=this;
31             this.elmt().find("div.messages-buttons>input").each(
32                 function (i,input) {
33                     self.init_button (input, self.options.levels);
34                     self.arm_button (input, self.toggle_handler);
35                 }
36             );
37         },
38         
39         init_button: function (input,levels) {
40             /* set initial 'checked' state for that input from global 'levels' above */
41             var level=input.name;
42             if (levels[level]) $(input).attr('checked','checked');
43         },
44
45         arm_button: function (input,handler) {
46             $(input).click (handler);
47         },
48         
49         is_active: function (level) { 
50             return this.elmt().find("div.messages-buttons>input[name="+level+"]").attr('checked');
51         },
52             
53         display_message: function (incoming, level) {
54             var domid=this.elmt().attr('id');
55             var html="";
56             html += "<li class='" + level +"'"; 
57             if ( ! this.is_active(level) ) html += " style='display:none'";
58             html += ">";
59             html += "<span class='messages-fixed'>";
60             html += "<span class='messages-level'>" + level + "</span>";
61             html += "<span class='messages-date'>";
62             html += "</html>";
63             d=new Date();
64             html += d.getHours() + ":" +d.getMinutes() + ":" + d.getSeconds() + "--" + d.getMilliseconds();
65             html += "</span>";
66             //  html += "[" + domid + "]";
67             html += " " + incoming + "</li>";
68             $("ul#"+domid+".messages").append(html);
69         },
70
71         /* as an event handler toggle_handler will see the DOM <input> as 'this' */
72         toggle_handler : function (e) {
73             var $this=$(this);
74             // toggle the state of the checkbox
75             if ($this.attr('checked')) $this.removeAttr('checked');
76             else $this.attr('checked',true);
77             // turn messages on or off
78             var level=this.name;
79             var display = $this.attr('checked') ? "list-item" : "none";
80             var elmt=$this.closest("div.Messages");
81             elmt.find("li."+level).css("display",display);
82         },
83         
84     });
85
86     $.plugin('Messages', Messages);
87
88 })(jQuery);
89
90 /* turn this on for an auto-test on startup
91 var messages_test = {
92     // set this to 0 to disable
93     counter : 2,
94     period : 1000,
95     sample : function () { 
96         $.publish("/messages/fatal","a fatal message (" + messages_test.counter + " runs to go)");
97         $.publish("/messages/error","an error message");
98         $.publish("/messages/warning","a warning message");
99         $.publish("/messages/info","an info message");
100         $.publish("/messages/debug","a debug message");
101         messages_test.counter -= 1;
102         if (messages_test.counter == 0)
103             window.clearInterval (messages_test.interval_id);
104     },
105     run: function () {
106         messages_test.interval_id=window.setInterval(messages_test.sample , messages_test.period);
107     }
108 }
109 messages_test.run()
110 */