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