//******************************************************** // Class Button // Display a button // // use JQuery // str value: Value to display in the button // dom parent: in which dom element to draw the button // int id: id of the dom element // func onclick: callback function when button clicked // str className : className of the general button // str classNameText : className for the button's text //******************************************************** var Button = Class.create( { initialize: function( param ) { // Init this.id = param.id ? param.id : ""; this.parent = param.parent ? param.parent : false; this.name = param.name ? param.name : ""; this.value = param.value ? param.value : ""; this.title = param.title ? param.title : false; this.className = param.className ? param.className : false; this.classNameText = param.classNameText ? param.classNameText : false; this.callbackOnClick = param.onClick ? param.onClick : false; this.callbackOnMouseOver = param.onMouseOver ? param.onMouseOver : false; this.callbackOnMouseOut = param.onMouseOut ? param.onMouseOut : false; this.contextToSave = param.contextToSave ? param.contextToSave : false; this.boolDisabled = false; this.boolSelected = false; this.boolDisplayed = false; // Create button elements this.divContainer = $( document.createElement( "button" ) ); if( this.className ) this.divContainer.addClass( this.className ); if( this.title ) this.divContainer.attr( {id:this.id, title:this.title} ); else this.divContainer.attr( {id:this.id} ); if( this.parent ) this.parent.append( this.divContainer ); this.divText = $( document.createElement( "div" ) ); if( this.classNameText ) this.divText.addClass( this.classNameText ); this.divText.html( this.value ); this.divContainer.append( this.divText ); // Define button events if( this.contextToSave ) this.divContainer.bind( 'click', [this, this.contextToSave], this.onClick ); else this.divContainer.bind( 'click', this, this.onClick ); this.divContainer.bind( 'mouseover', this, this.onHover ); this.divContainer.bind( 'mouseout', this, this.onOut ); }, // Getter / Setter ******************************************************** getContainer: function() { return this.divContainer; }, isDisable : function() { return this.boolDisabled; }, isSelected: function() { return this.boolSelected; }, getValue : function() { return this.value; }, setValue : function( value ) { this.value = value; this.divText.html( this.value ); }, getName : function() { return this.name; }, setName : function( name ) { this.name = name; this.divText.html( this.name ); }, setCallbackOnClick : function( value ) { this.callbackOnClick = value; }, // Actions ******************************************************** disable : function() { this.boolDisabled = true; this.divContainer.addClass( "disable" ); }, enable : function() { this.boolDisabled = false; this.divContainer.removeClass( "disable" ); }, select : function( value ) { this.boolSelected = value; if( this.boolSelected ) this.divContainer.addClass( "selected" ); else this.divContainer.removeClass( "selected" ); }, show : function() { this.boolDisplayed = true; this.divContainer.show(); }, hide : function() { this.boolDisplayed = false; this.divContainer.hide(); }, // Events ******************************************************** onClick : function( event ) { var contextButton = event.data; var isContextToPut = false; if( jQuery.isArray( event.data ) ) { isContextToPut = true; contextButton = event.data[0]; } if( !contextButton.isDisable() && (undefined == event.detail || 1 == event.detail) && contextButton.callbackOnClick ) if( isContextToPut ) contextButton.callbackOnClick( event.data[1] ); else contextButton.callbackOnClick(); }, onHover : function( event ) { var contextButton = event.data; if( !contextButton.isDisable() && contextButton.callbackOnMouseOver ) contextButton.callbackOnMouseOver(); }, onOut: function( event ) { var contextButton = event.data; if( !contextButton.isDisable() && contextButton.callbackOnMouseOut ) contextButton.callbackOnMouseOut(); } } );