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) {
12 this._super (options, element);
13 // subscribe to the various messages channels
15 for (level in options.levels)
17 $.subscribe("/messages/"+l, function (e, msg){ self.display_message (msg,l)});
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');
29 initialize: function () {
31 this.elmt().find("div.messages-buttons>input").each(
33 self.init_button (input, self.options.levels);
34 self.arm_button (input, self.toggle_handler);
39 init_button: function (input,levels) {
40 /* set initial 'checked' state for that input from global 'levels' above */
42 if (levels[level]) $(input).attr('checked','checked');
45 arm_button: function (input,handler) {
46 $(input).click (handler);
49 is_active: function (level) {
50 return this.elmt().find("div.messages-buttons>input[name="+level+"]").attr('checked');
53 display_message: function (incoming, level) {
54 var domid=this.elmt().attr('id');
56 html += "<li class='" + level +"'";
57 if ( ! this.is_active(level) ) html += " style='display:none'";
59 html += "<span class='messages-fixed'>";
60 html += "<span class='messages-level'>" + level + "</span>";
61 html += "<span class='messages-date'>";
64 html += d.getHours() + ":" +d.getMinutes() + ":" + d.getSeconds() + "--" + d.getMilliseconds();
66 // html += "[" + domid + "]";
67 html += " " + incoming + "</li>";
68 $("ul#"+domid+".messages").append(html);
71 /* as an event handler toggle_handler will see the DOM <input> as 'this' */
72 toggle_handler : function (e) {
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
79 var display = $this.attr('checked') ? "list-item" : "none";
80 var elmt=$this.closest("div.Messages");
81 elmt.find("li."+level).css("display",display);
86 $.plugin('Messages', Messages);
90 /* turn this on for an auto-test on startup
92 // set this to 0 to disable
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);
106 messages_test.interval_id=window.setInterval(messages_test.sample , messages_test.period);