', { text : $this.attr('title') });
-
- // If the plugin hasn't been initialized yet
- if ( ! data ) {
- $(this).data('Messages', {
- plugin_uuid: options.plugin_uuid,
- target : $this,
- Messages : Messages,
- });
-
- /* Plugin initialization */
- $.subscribe("messages:fatal", {'plugindiv': $this,'level':'fatal'}, display_message);
- $.subscribe("messages:error", {'plugindiv': $this,'level':'error'}, display_message);
- $.subscribe("messages:warning", {'plugindiv': $this,'level':'warning'}, display_message);
- $.subscribe("messages:info", {'plugindiv': $this,'level':'info'}, display_message);
- $.subscribe("messages:debug", {'plugindiv': $this,'level':'debug'}, display_message);
- $.publish ("messages:info", 'Subscribed to all 5 message channels');
- /* End of plugin initialization */
- }
+ return this.each (function() {
+ var $this = $(this);
+ instance=new Messages (options,$this);
+ $this.data('Messages',instance);
+ for (level in options.levels) {
+ (function (instance,level) {
+ $.subscribe("/messages/"+level, function (e, msg){ instance.display_message (msg,level)});
+ }) (instance,level);
+ }
+ // this happens very early - even before the document is loaded
+ // so it won't show right away; no big deal though
+ $.publish ("/messages/info", 'Subscribed to all 5 message channels');
});
},
destroy : function( ) {
return this.each(function(){
- var $this = $(this), data = $this.data('Messages');
+ var $this = $(this), instance = $this.data('Messages');
$(window).unbind('Messages');
- data.Messages.remove();
+ instance.remove();
$this.removeData('Messages');
});
},
@@ -66,40 +51,96 @@
update : function( content ) { },
};
- /* Private methods */
+ function Messages (options,plugindiv) {
+ this.options=options;
+ this.plugindiv=plugindiv;
+ /* cannot use 'this' directly of course */
+ (function (instance) { $( function () {instance.initialize();}) }) (this);
- function display_message(e, message) {
- var level=e.data.level;
- var domid=e.data.plugindiv.data('Messages').plugin_uuid;
- var html="";
- html += "
";
- html += "" + new Date() + "";
- html += "[" + level + "]";
-// html += "[" + domid + "]";
- html += " " + message + "
";
- $("ul#"+domid+".messages").append(html);
- }
+ this.is_active = function (level) {
+ return this.plugindiv.find("div.messages-buttons>input[name="+level+"]").attr('checked');
+ }
+ this.display_message = function (incoming, level) {
+ var domid=this.plugindiv.attr('id');
+ var html="";
+ html += "
";
+ html += "" + new Date() + "";
+ html += "" + level + "";
+ // html += "[" + domid + "]";
+ html += " " + incoming + "
";
+ $("ul#"+domid+".messages").append(html);
+ },
+
+ this.initialize = function () {
+ var init_button=this.init_button;
+ var levels=this.options.levels;
+ this.plugindiv.find("div.messages-buttons>input").each(
+ function (i,input) {init_button (input, levels)});
+ var arm_button=this.arm_button;
+ var toggle_handler=this.toggle_handler;
+ this.plugindiv.find("div.messages-buttons>input").each(
+ function (i,input) {arm_button (input,toggle_handler); });
+ },
+ this.init_button = function (input,levels) {
+ /* set initial 'checked' state for that input from global 'levels' above */
+ var level=input.name;
+ if (levels[level]) $(input).attr('checked','checked');
+ },
+ this.arm_button = function (input,handler) {
+ $(input).click (handler);
+ },
+ /* as an event handler toggle_handler will see the DOM as 'this' */
+ this.toggle_handler = function (e) {
+ var $this=$(this);
+ // toggle the state of the checkbox
+ if ($this.attr('checked')) $this.removeAttr('checked');
+ else $this.attr('checked',true);
+ // turn messages on or off
+ var level=this.name;
+ var display = $this.attr('checked') ? "list-item" : "none";
+ var plugindiv=$this.closest("div.Messages");
+ plugindiv.find("li."+level).css("display",display);
+ }
+
+ };
})(jQuery);
-// temporary
+// messages runtime -- convenience functions messages.fatal and the like
+// in addition, messages can get lost if the UI is not ready to accept them
+// so we use console.log in this case
+var messages = {
+ ready : false,
+ levels : ['fatal','error','warning','info','debug'],
+ handler : function (level,msg) {
+ if (messages.ready) $.publish("/messages/"+level+"/",msg);
+ else console.log("/messages/"+level+"/: "+msg);
+ },
+};
+for (var i in messages.levels) { var level=messages.levels[i]; messages[level]=function (msg) {messages.handler (level,msg)};}
+$(function(){messages.ready=true;})
+
+/* turn this on for an auto-test on startup
var messages_test = {
// set this to 0 to disable
- counter : 3,
- period : 3000,
+ counter : 2,
+ period : 1000,
sample : function () {
- $.publish("messages:fatal","a fatal message (" + messages_test.counter + " runs to go)");
- $.publish("messages:error","an error message");
- $.publish("messages:warning","a warning message");
- $.publish("messages:info","an info message");
- $.publish("messages:debug","a debug message");
+ $.publish("/messages/fatal","a fatal message (" + messages_test.counter + " runs to go)");
+ $.publish("/messages/error","an error message");
+ $.publish("/messages/warning","a warning message");
+ $.publish("/messages/info","an info message");
+ $.publish("/messages/debug","a debug message");
messages_test.counter -= 1;
if (messages_test.counter == 0)
window.clearInterval (messages_test.interval_id);
},
run: function () {
- messages_test.interval_id=window.setInterval(messages_test.sample , 5000);
+ messages_test.interval_id=window.setInterval(messages_test.sample , messages_test.period);
}
}
messages_test.run()
+*/