d4265a303feccdd5607cda6d0397bb7ace37f0c7
[myslice.git] / plugins / messages / messages.js
1 /**
2  * MySlice Messages plugin
3  * Version: 0.1.0
4  * URL: http://www.myslice.info
5  * Description: Display debug messages in a DIV
6  * Requires: 
7  * Author: The MySlice Team
8  * Copyright: Copyright 2012 UPMC Sorbonne Universités
9  * License: GPLv3
10  */
11
12 ( function($) {
13
14     /* Method calling logic */
15     $.fn.Messages = function( method ) {
16         if ( methods[method] ) {
17             return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
18         } else if ( typeof method === 'object' || ! method ) {
19             return methods.init.apply( this, arguments );
20         } else {
21             $.error( 'Method ' +  method + ' does not exist on $.Messages' );
22         }    
23     };
24
25     var methods = {
26         init : function( options ) {
27
28             return this.each(function(){
29                 var $this = $(this),
30                   data = $this.data('Messages'), 
31                   Messages = $('<div />', { text : $this.attr('title') });
32
33                 // If the plugin hasn't been initialized yet
34                 if ( ! data ) {
35                     $(this).data('Messages', {
36                         plugin_uuid: options.plugin_uuid,
37                         target : $this,
38                         Messages : Messages,
39                     });
40          
41                     /* Plugin initialization */
42                     $.subscribe("messages:fatal", {'plugindiv': $this,'level':'fatal'}, display_message);
43                     $.subscribe("messages:error", {'plugindiv': $this,'level':'error'}, display_message);
44                     $.subscribe("messages:warning", {'plugindiv': $this,'level':'warning'}, display_message);
45                     $.subscribe("messages:info", {'plugindiv': $this,'level':'info'}, display_message);
46                     $.subscribe("messages:debug", {'plugindiv': $this,'level':'debug'}, display_message);
47                     $.publish  ("messages:info", 'Subscribed to all 5 message channels');
48                     /* End of plugin initialization */
49                 }
50             });
51         },
52         destroy : function( ) {
53
54             return this.each(function(){
55                 var $this = $(this), data = $this.data('Messages');
56                 $(window).unbind('Messages');
57                 data.Messages.remove();
58                 $this.removeData('Messages');
59             });
60         },
61 /*
62     reposition : function( ) { // ... },
63     show : function( ) { // ... },
64     hide : function( ) { // ... },
65 */
66         update : function( content ) { },
67     };
68
69     /* Private methods */
70
71     function display_message(e, message) {
72         var level=e.data.level;
73         var domid=e.data.plugindiv.data('Messages').plugin_uuid;
74         var html="";
75         html += "<li class='" + level + "'>";
76         html += "[" + level + "]";
77         html += " " + new Date() + " ";
78 //      html += "[" + domid + "]";
79         html += " " + message + "</li>";
80         $("ul#"+domid+".messages").append(html);
81     }
82     
83 })(jQuery);
84
85 // temporary
86
87 var tests=true;
88 if (tests) // arm this with a timeout rather
89     window.setInterval(
90         function () { 
91             $.publish("messages:fatal","a fatal message");
92             $.publish("messages:error","an error message");
93             $.publish("messages:warning","a warning message");
94             $.publish("messages:info","an info message");
95             $.publish("messages:debug","a debug message");
96         }, 5000);