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' );
20 var levels = {'fatal': true, 'error': true, 'warning' : true, 'info' : true, 'debug' : false};
23 init : function( options ) {
25 return this.each (function() {
27 instance=new Messages (options,$this);
28 $this.data('Messages',instance);
29 for (level in levels) {
30 (function (instance,level) {
31 $.subscribe("messages:"+level, function (e, msg){ instance.display_message (msg,level)});
34 $.publish ("messages:info", 'Subscribed to all 5 message channels');
37 destroy : function( ) {
39 return this.each(function(){
40 var $this = $(this), instance = $this.data('Messages');
41 $(window).unbind('Messages');
43 $this.removeData('Messages');
47 reposition : function( ) { // ... },
48 show : function( ) { // ... },
49 hide : function( ) { // ... },
51 update : function( content ) { },
54 function Messages (options,plugindiv) {
56 this.plugindiv=plugindiv;
57 /* cannot use 'this' directly of course */
58 (function (instance) { $( function () {instance.initialize();}) }) (this);
60 this.is_active = function (level) {
61 return this.plugindiv.find("div.messages-buttons>input[name="+level+"]").attr('checked');
63 this.display_message = function (incoming, level) {
64 var domid=this.plugindiv.attr('id');
66 html += "<li class='" + level +"'";
67 if ( ! this.is_active(level) ) html += " style='display:none'";
69 html += "<span class='messages-date'>" + new Date() + "</span>";
70 html += "<span class='messages-level'>" + level + "</span>";
71 // html += "[" + domid + "]";
72 html += " " + incoming + "</li>";
73 $("ul#"+domid+".messages").append(html);
76 this.initialize = function () {
77 this.plugindiv.find("div.messages-buttons>input").each(this.init_button);
78 var arm_button=this.arm_button;
79 var toggle_handler=this.toggle_handler;
80 this.plugindiv.find("div.messages-buttons>input").each(
81 function (i,input) {arm_button (input,toggle_handler); }
84 this.init_button = function (_,input) {
85 /* set initial 'checked' state for that input from global 'levels' above */
87 if (levels[level]) $(input).attr('checked','checked');
89 this.arm_button = function (input,handler) {
90 $(input).click (handler);
92 /* as an event handler toggle_handler will see the DOM <input> as 'this' */
93 this.toggle_handler = function (e) {
95 var before=$this.attr('checked');
96 if ($this.attr('checked')) $this.removeAttr('checked');
97 else $this.attr('checked',true);
99 var after=$this.attr('checked');
100 var display = $this.attr('checked') ? "list-item" : "none";
101 var plugindiv=$this.closest("div.Messages");
102 plugindiv.find("li."+level).css("display",display);
111 var messages_test = {
112 // set this to 0 to disable
115 sample : function () {
116 $.publish("messages:fatal","a fatal message (" + messages_test.counter + " runs to go)");
117 $.publish("messages:error","an error message");
118 $.publish("messages:warning","a warning message");
119 $.publish("messages:info","an info message");
120 $.publish("messages:debug","a debug message");
121 messages_test.counter -= 1;
122 if (messages_test.counter == 0)
123 window.clearInterval (messages_test.interval_id);
126 messages_test.interval_id=window.setInterval(messages_test.sample , messages_test.period);