turn off auto-test in messages + minor tweaks
[myslice.git] / plugins / messages / 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     /* 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 );
15         } else {
16             $.error( 'Method ' +  method + ' does not exist on $.Messages' );
17         }    
18     };
19
20     var levels = {'fatal': true, 'error': true, 'warning' : true, 'info' : true, 'debug' : false};
21
22     var methods = {
23         init : function( options ) {
24
25             return this.each (function() {
26                 var $this = $(this);
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)});
32                     }) (instance,level);
33                 }
34                 // this happens very early - even before the document is loaded
35                 // so it won't show right away; no big deal though
36                 $.publish  ("messages:info", 'Subscribed to all 5 message channels');
37             });
38         },
39         destroy : function( ) {
40
41             return this.each(function(){
42                 var $this = $(this), instance = $this.data('Messages');
43                 $(window).unbind('Messages');
44                 instance.remove();
45                 $this.removeData('Messages');
46             });
47         },
48 /*
49     reposition : function( ) { // ... },
50     show : function( ) { // ... },
51     hide : function( ) { // ... },
52 */
53         update : function( content ) { },
54     };
55
56     function Messages (options,plugindiv) {
57         this.options=options;
58         this.plugindiv=plugindiv;
59         /* cannot use 'this' directly of course */
60         (function (instance) { $( function () {instance.initialize();}) }) (this);
61
62         this.is_active = function (level) { 
63             return this.plugindiv.find("div.messages-buttons>input[name="+level+"]").attr('checked');
64         }
65         this.display_message = function (incoming, level) {
66             var domid=this.plugindiv.attr('id');
67             var html="";
68             html += "<li class='" + level +"'"; 
69             if ( ! this.is_active(level) ) html += " style='display:none'";
70             html += ">";
71             html += "<span class='messages-date'>" + new Date() + "</span>";
72             html += "<span class='messages-level'>" + level + "</span>";
73             //  html += "[" + domid + "]";
74             html += " " + incoming + "</li>";
75             $("ul#"+domid+".messages").append(html);
76         },
77
78         this.initialize = function () {
79             this.plugindiv.find("div.messages-buttons>input").each(this.init_button);
80             var arm_button=this.arm_button;
81             var toggle_handler=this.toggle_handler;
82             this.plugindiv.find("div.messages-buttons>input").each(
83                 function (i,input) {arm_button (input,toggle_handler); }
84             );
85         },
86         this.init_button = function (_,input) {
87             /* set initial 'checked' state for that input from global 'levels' above */
88             var level=input.name;
89             if (levels[level]) $(input).attr('checked','checked');
90         },
91         this.arm_button = function (input,handler) {
92             $(input).click (handler);
93         },
94         /* as an event handler toggle_handler will see the DOM <input> as 'this' */
95         this.toggle_handler = function (e) {
96             var $this=$(this);
97             // toggle the state of the checkbox
98             if ($this.attr('checked')) $this.removeAttr('checked');
99             else $this.attr('checked',true);
100             // turn messages on or off
101             var level=this.name;
102             var display = $this.attr('checked') ? "list-item" : "none";
103             var plugindiv=$this.closest("div.Messages");
104             plugindiv.find("li."+level).css("display",display);
105         }
106
107     };
108     
109 })(jQuery);
110
111 /* turn this on for an auto-test on startup
112 var messages_test = {
113     // set this to 0 to disable
114     counter : 2,
115     period : 1000,
116     sample : function () { 
117         $.publish("messages:fatal","a fatal message (" + messages_test.counter + " runs to go)");
118         $.publish("messages:error","an error message");
119         $.publish("messages:warning","a warning message");
120         $.publish("messages:info","an info message");
121         $.publish("messages:debug","a debug message");
122         messages_test.counter -= 1;
123         if (messages_test.counter == 0)
124             window.clearInterval (messages_test.interval_id);
125     },
126     run: function () {
127         messages_test.interval_id=window.setInterval(messages_test.sample , messages_test.period);
128     }
129 }
130 messages_test.run()
131 */