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 | |
---|
14 | var 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.title = param.title ? param.title : false; |
---|
22 | this.className = param.className ? param.className : false; |
---|
23 | this.classNameText = param.classNameText ? param.classNameText : false; |
---|
24 | this.callbackOnClick = param.onClick ? param.onClick : false; |
---|
25 | this.callbackOnMouseOver = param.onMouseOver ? param.onMouseOver : false; |
---|
26 | this.callbackOnMouseOut = param.onMouseOut ? param.onMouseOut : false; |
---|
27 | this.contextToSave = param.contextToSave ? param.contextToSave : false; |
---|
28 | this.boolDisabled = false; |
---|
29 | this.boolSelected = false; |
---|
30 | this.boolDisplayed = false; |
---|
31 | |
---|
32 | // Create button elements |
---|
33 | this.divContainer = $( document.createElement( "button" ) ); |
---|
34 | if( this.className ) |
---|
35 | this.divContainer.addClass( this.className ); |
---|
36 | if( this.title ) |
---|
37 | this.divContainer.attr( {id:this.id, title:this.title} ); |
---|
38 | else |
---|
39 | this.divContainer.attr( {id:this.id} ); |
---|
40 | if( this.parent ) |
---|
41 | this.parent.append( this.divContainer ); |
---|
42 | |
---|
43 | this.divText = $( document.createElement( "div" ) ); |
---|
44 | if( this.classNameText ) |
---|
45 | this.divText.addClass( this.classNameText ); |
---|
46 | this.divText.html( this.value ); |
---|
47 | this.divContainer.append( this.divText ); |
---|
48 | |
---|
49 | // Define button events |
---|
50 | if( this.contextToSave ) |
---|
51 | this.divContainer.bind( 'click', [this, this.contextToSave], this.onClick ); |
---|
52 | else |
---|
53 | this.divContainer.bind( 'click', this, this.onClick ); |
---|
54 | this.divContainer.bind( 'mouseover', this, this.onHover ); |
---|
55 | this.divContainer.bind( 'mouseout', this, this.onOut ); |
---|
56 | }, |
---|
57 | |
---|
58 | // Getter / Setter ******************************************************** |
---|
59 | |
---|
60 | getContainer: function() |
---|
61 | { |
---|
62 | return this.divContainer; |
---|
63 | }, |
---|
64 | |
---|
65 | isDisable : function() |
---|
66 | { |
---|
67 | return this.boolDisabled; |
---|
68 | }, |
---|
69 | |
---|
70 | isSelected: function() |
---|
71 | { |
---|
72 | return this.boolSelected; |
---|
73 | }, |
---|
74 | |
---|
75 | setValue : function( value ) |
---|
76 | { |
---|
77 | this.value = value; |
---|
78 | this.divText.html( this.value ); |
---|
79 | }, |
---|
80 | |
---|
81 | setCallbackOnClick : function( value ) |
---|
82 | { |
---|
83 | this.callbackOnClick = value; |
---|
84 | }, |
---|
85 | |
---|
86 | // Actions ******************************************************** |
---|
87 | disable : function() |
---|
88 | { |
---|
89 | this.boolDisabled = true; |
---|
90 | this.divContainer.addClass( "disable" ); |
---|
91 | }, |
---|
92 | |
---|
93 | enable : function() |
---|
94 | { |
---|
95 | this.boolDisabled = false; |
---|
96 | this.divContainer.removeClass( "disable" ); |
---|
97 | }, |
---|
98 | |
---|
99 | select : function( value ) |
---|
100 | { |
---|
101 | this.boolSelected = value; |
---|
102 | if( this.boolSelected ) |
---|
103 | this.divContainer.addClass( "selected" ); |
---|
104 | else |
---|
105 | this.divContainer.removeClass( "selected" ); |
---|
106 | }, |
---|
107 | |
---|
108 | show : function() |
---|
109 | { |
---|
110 | this.boolDisplayed = true; |
---|
111 | this.divContainer.show(); |
---|
112 | }, |
---|
113 | |
---|
114 | hide : function() |
---|
115 | { |
---|
116 | this.boolDisplayed = false; |
---|
117 | this.divContainer.hide(); |
---|
118 | }, |
---|
119 | |
---|
120 | // Events ******************************************************** |
---|
121 | onClick : function( event ) |
---|
122 | { |
---|
123 | var contextButton = event.data; |
---|
124 | var isContextToPut = false; |
---|
125 | if( jQuery.isArray( event.data ) ) |
---|
126 | { |
---|
127 | isContextToPut = true; |
---|
128 | contextButton = event.data[0]; |
---|
129 | } |
---|
130 | |
---|
131 | if( !contextButton.isDisable() && (undefined == event.detail || 1 == event.detail) && contextButton.callbackOnClick ) |
---|
132 | if( isContextToPut ) |
---|
133 | contextButton.callbackOnClick( event.data[1] ); |
---|
134 | else |
---|
135 | contextButton.callbackOnClick(); |
---|
136 | }, |
---|
137 | |
---|
138 | onHover : function( event ) |
---|
139 | { |
---|
140 | var contextButton = event.data; |
---|
141 | if( !contextButton.isDisable() && contextButton.callbackOnMouseOver ) |
---|
142 | contextButton.callbackOnMouseOver(); |
---|
143 | }, |
---|
144 | |
---|
145 | onOut: function( event ) |
---|
146 | { |
---|
147 | var contextButton = event.data; |
---|
148 | if( !contextButton.isDisable() && contextButton.callbackOnMouseOut ) |
---|
149 | contextButton.callbackOnMouseOut(); |
---|
150 | } |
---|
151 | } ); |
---|