2 * Description: display messages in a dedicated area, with buttons for filtering on level
3 * Copyright (c) 2012 UPMC Sorbonne Universite - INRIA
9 var Messages = Plugin.extend ({
11 init: function (options, element) {
13 this._super (options, element);
14 // subscribe to the various messages channels
16 for (level in options.levels)
18 $.subscribe("/messages/"+l, function (e, msg){ self.display_message (msg,l)});
20 // kind of patchy, notify the convenience functions that somebody is listening...
21 try {messages.ready=true;}
22 catch (err) { console.log("Could not set messages.ready");}
23 // this happens very early - even before the document is loaded
24 // so it won't show right away; no big deal though
25 $.publish ("/messages/info", 'Subscribed to all 5 message channels');
30 initialize: function () {
32 this.elmt().find("div.messages-buttons>input").each(
34 self.init_button (input, self.options.levels);
35 self.arm_button (input, self.toggle_handler);
40 init_button: function (input,levels) {
41 /* set initial 'checked' state for that input from global 'levels' above */
43 if (levels[level]) $(input).attr('checked','checked');
46 arm_button: function (input,handler) {
47 $(input).click (handler);
50 is_active: function (level) {
51 return this.elmt().find("div.messages-buttons>input[name="+level+"]").attr('checked');
54 display_message: function (incoming, level) {
55 var domid=this.elmt().attr('id');
57 html += "<li class='" + level +"'";
58 if ( ! this.is_active(level) ) html += " style='display:none'";
60 html += "<span class='messages-fixed'>";
61 html += "<span class='messages-level'>" + level + "</span>";
62 html += "<span class='messages-date'>";
65 html += d.getHours() + ":" +d.getMinutes() + ":" + d.getSeconds() + "--" + d.getMilliseconds();
67 // html += "[" + domid + "]";
68 html += " " + incoming + "</li>";
69 $("ul#"+domid+".messages").append(html);
72 /* as an event handler toggle_handler will see the DOM <input> as 'this' */
73 toggle_handler : function (e) {
75 // toggle the state of the checkbox
76 if ($this.attr('checked')) $this.removeAttr('checked');
77 else $this.attr('checked',true);
78 // turn messages on or off
80 var display = $this.attr('checked') ? "list-item" : "none";
81 var elmt=$this.closest("div.Messages");
82 elmt.find("li."+level).css("display",display);
87 $.plugin('Messages', Messages);
91 /* turn this on for an auto-test on startup
93 // set this to 0 to disable
96 sample : function () {
97 $.publish("/messages/fatal","a fatal message (" + messages_test.counter + " runs to go)");
98 $.publish("/messages/error","an error message");
99 $.publish("/messages/warning","a warning message");
100 $.publish("/messages/info","an info message");
101 $.publish("/messages/debug","a debug message");
102 messages_test.counter -= 1;
103 if (messages_test.counter == 0)
104 window.clearInterval (messages_test.interval_id);
107 messages_test.interval_id=window.setInterval(messages_test.sample , messages_test.period);