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