Skip to content

Instantly share code, notes, and snippets.

@natlownes
Created January 7, 2010 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natlownes/271265 to your computer and use it in GitHub Desktop.
Save natlownes/271265 to your computer and use it in GitHub Desktop.
// requires yahoo.util.dom
var InfoWidget = Class.create();
// ex. widget = new InfoWidget({
// 'text': "your text",
// 'severity': 'info',
//
// })
// widget.show();
InfoWidget.prototype = {
initialize: function(options){
var severity_classes = {
'error': 'red',
'info': 'lightyellow',
'warning': 'yellow',
'success': 'lightgreen'
}
var options = options || {};
var container_id = options['container'] || document.body;
var container = $(container_id);
var default_options = {
'width': 300,
'height': 300,
'text': "",
'severity': 'info',
'show_fx': function(){ $(this).show(); },
'hide_fx': function(){ $(this).hide(); },
'event_fx': function(info_widget_obj){
$(this).observe('click', function(event){
event.element().fire("infowidget:Clicked", {'widget': info_widget_obj });
}
)}
}
var options = Object.extend(default_options, options);
var severity = options.severity;
this.text = options.text;
var container_element = new Element('div', { 'class': 'info-widget'}).setStyle({'display': 'none'});
var content_element = new Element('div', { 'class': 'content'});
var show_fx = options.show_fx.bind(container_element);
var hide_fx = options.hide_fx.bind(container_element);
var event_fx = options.event_fx.bind(container_element);
container_element.insert(content_element);
$(document.body).insert(container_element);
// private functions
var top_position = function(){
return y();
}
var left_position = function(){
return (x() + ((window.screen.width / 2) - (options['width'] / 2)));
}
var x = function(){
var xcoords = options['x'] || window.scrollX;
return (parseInt(xcoords));
}
var y = function(){
return options['y'] || (window.scrollY);
}
var init_style = function(){
YAHOO.util.Dom.setStyle(container_element, 'position', 'absolute')
YAHOO.util.Dom.setStyle(container_element, 'left', (new String(left_position())+'px'))
YAHOO.util.Dom.setStyle(container_element, 'top', (new String(top_position())+'px'))
}.bind(this);
var set_severity_style = function(){
YAHOO.util.Dom.setStyle(container_element, 'background-color', new String(severity_classes[severity]));
}.bind(this);
// init_style();
set_severity_style();
event_fx(this);
// public functions
this.show = function(){
init_style();
set_severity_style();
content_element.update(this.text);
show_fx();
container_element.fire("infowidget:Show", {'widget': this });
}
this.hide = function(){
hide_fx();
container_element.fire("infowidget:Hide", {'widget': this });
}
this.setSeverity = function(level){
if (severity_classes[level]) {
severity = level;
}
set_severity_style();
}
this.destroy = function(){
container_element.remove();
delete this;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment