source: ether_megapoli/trunk/web/resources/js/classesForJQuery/Button.js @ 240

Last change on this file since 240 was 240, checked in by vmipsl, 13 years ago

Boutons cliquables

  • Property svn:executable set to *
File size: 3.9 KB
Line 
1//********************************************************
2// Class Button
3// Display a button
4//
5// use JQuery
6// str value: Value to display in the button
7// dom parent: in which dom element to draw the button
8// int id: id of the dom element
9// func onclick: callback function when button clicked
10// str className : className of the general button
11// str classNameText : className for the button's text
12//********************************************************
13
14var Button = Class.create( {
15    initialize: function( param )
16    {
17        // Init
18        this.id = param.id ? param.id : "";
19        this.parent = param.parent ? param.parent : false;
20        this.value = param.value ? param.value : "";
21        this.className = param.className ? param.className : false;
22        this.classNameText = param.classNameText ? param.classNameText : false;
23        this.callbackOnClick = param.onClick ? param.onClick : false;
24        this.callbackOnMouseOver = param.onMouseOver ? param.onMouseOver : false;
25        this.callbackOnMouseOut = param.onMouseOut ? param.onMouseOut : false;
26        this.boolDisabled = false;
27        this.boolSelected = false;
28        this.boolDisplayed = false;
29
30        // Create button elements
31        this.divContainer = $( document.createElement( "button" ) );
32        if( this.className )
33            this.divContainer.addClass( this.className );
34        this.divContainer.attr( {id:this.id} );
35        if( this.parent )
36            this.parent.append( this.divContainer );
37
38        this.divText = $( document.createElement( "div" ) );
39        if( this.classNameText )
40            this.divText.addClass( this.classNameText );
41        this.divText.html( this.value );
42        this.divContainer.append( this.divText );
43
44        // Define button events
45        this.divContainer.bind( 'click', this, this.onClick );
46        this.divContainer.bind( 'mouseover', this, this.onHover );
47        this.divContainer.bind( 'mouseout', this, this.onOut );
48    },
49
50    // Getter / Setter ********************************************************
51
52    getContainer: function()
53    {
54        return this.divContainer;
55    },
56
57    isDisable : function()
58    {
59        return this.boolDisabled;
60    },
61
62    isSelected: function()
63    {
64        return this.boolSelected;
65    },
66
67    setValue : function( value )
68    {
69        this.value = value;
70        this.divText.html( this.value );
71    },
72
73    setCallbackOnClick : function( value )
74    {
75        this.callbackOnClick = value;
76    },
77
78    // Actions ********************************************************
79    disable : function()
80    {
81        this.boolDisabled = true;
82        this.divContainer.addClass( "disable" );
83    },
84
85    enable : function()
86    {
87        this.boolDisabled = false;
88        this.divContainer.removeClass( "disable" );
89    },
90
91    select : function( value )
92    {
93        this.boolSelected = value;
94        if( this.boolSelected )
95            this.divContainer.addClass( "selected" );
96        else
97            this.divContainer.removeClass( "selected" );
98    },
99
100    show : function()
101    {
102        this.boolDisplayed = true;
103        this.divContainer.show();
104    },
105
106    hide : function()
107    {
108        this.boolDisplayed = false;
109        this.divContainer.hide();
110    },
111
112    // Events ********************************************************
113    onClick : function( event )
114    {
115        var contextButton = event.data;
116        if( !contextButton.isDisable() && (undefined == event.detail || 1 == event.detail) && contextButton.callbackOnClick )
117            contextButton.callbackOnClick();
118    },
119
120    onHover : function( event )
121    {
122        var contextButton = event.data;
123        if( !contextButton.isDisable() && contextButton.callbackOnMouseOver )
124            contextButton.callbackOnMouseOver();
125    },
126
127    onOut: function( event )
128    {
129        var contextButton = event.data;
130        if( !contextButton.isDisable() && contextButton.callbackOnMouseOut )
131            contextButton.callbackOnMouseOut();
132    }
133} );
Note: See TracBrowser for help on using the repository browser.