2 * Description: display messages in a dedicated area, with buttons for filtering on level
3 * Copyright (c) 2012 UPMC Sorbonne Universite - INRIA
9 /* Method calling logic */
10 $.fn.Messages = function( method ) {
11 if ( methods[method] ) {
12 return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
13 } else if ( typeof method === 'object' || ! method ) {
14 return methods.init.apply( this, arguments );
16 $.error( 'Method ' + method + ' does not exist on $.Messages' );
21 init : function( options ) {
23 return this.each (function() {
25 instance=new Messages (options,$this);
26 $this.data('Messages',instance);
27 for (level in options.levels) {
28 (function (instance,level) {
29 $.subscribe("/messages/"+level, function (e, msg){ instance.display_message (msg,level)});
32 // kind of patchy, notify the convenience functions that somebody is listening...
33 try {messages.ready=true;}
34 catch (err) { console.log("Could not set messages.ready");}
35 // this happens very early - even before the document is loaded
36 // so it won't show right away; no big deal though
37 $.publish ("/messages/info", 'Subscribed to all 5 message channels');
40 destroy : function( ) {
42 return this.each(function(){
43 var $this = $(this), instance = $this.data('Messages');
44 $(window).unbind('Messages');
46 $this.removeData('Messages');
50 reposition : function( ) { // ... },
51 show : function( ) { // ... },
52 hide : function( ) { // ... },
54 update : function( content ) { },
57 function Messages (options,plugindiv) {
59 this.plugindiv=plugindiv;
60 /* cannot use 'this' directly of course */
61 (function (instance) { $( function () {instance.initialize();}) }) (this);
63 this.is_active = function (level) {
64 return this.plugindiv.find("div.messages-buttons>input[name="+level+"]").attr('checked');
66 this.display_message = function (incoming, level) {
67 var domid=this.plugindiv.attr('id');
69 html += "<li class='" + level +"'";
70 if ( ! this.is_active(level) ) html += " style='display:none'";
72 html += "<span class='messages-date'>" + new Date() + "</span>";
73 html += "<span class='messages-level'>" + level + "</span>";
74 // html += "[" + domid + "]";
75 html += " " + incoming + "</li>";
76 $("ul#"+domid+".messages").append(html);
79 this.initialize = function () {
80 var init_button=this.init_button;
81 var levels=this.options.levels;
82 this.plugindiv.find("div.messages-buttons>input").each(
83 function (i,input) {init_button (input, levels)});
84 var arm_button=this.arm_button;
85 var toggle_handler=this.toggle_handler;
86 this.plugindiv.find("div.messages-buttons>input").each(
87 function (i,input) {arm_button (input,toggle_handler); });
89 this.init_button = function (input,levels) {
90 /* set initial 'checked' state for that input from global 'levels' above */
92 if (levels[level]) $(input).attr('checked','checked');
94 this.arm_button = function (input,handler) {
95 $(input).click (handler);
97 /* as an event handler toggle_handler will see the DOM <input> as 'this' */
98 this.toggle_handler = function (e) {
100 // toggle the state of the checkbox
101 if ($this.attr('checked')) $this.removeAttr('checked');
102 else $this.attr('checked',true);
103 // turn messages on or off
105 var display = $this.attr('checked') ? "list-item" : "none";
106 var plugindiv=$this.closest("div.Messages");
107 plugindiv.find("li."+level).css("display",display);
114 /* turn this on for an auto-test on startup
115 var messages_test = {
116 // set this to 0 to disable
119 sample : function () {
120 $.publish("/messages/fatal","a fatal message (" + messages_test.counter + " runs to go)");
121 $.publish("/messages/error","an error message");
122 $.publish("/messages/warning","a warning message");
123 $.publish("/messages/info","an info message");
124 $.publish("/messages/debug","a debug message");
125 messages_test.counter -= 1;
126 if (messages_test.counter == 0)
127 window.clearInterval (messages_test.interval_id);
130 messages_test.interval_id=window.setInterval(messages_test.sample , messages_test.period);