1 | //******************************************************** |
---|
2 | // Class Select |
---|
3 | // Display a Select |
---|
4 | // |
---|
5 | // use jQuery |
---|
6 | // obj objParam: {id, [parent], [specificClass], [maxOptionSize], [isDisable], [isDisplayed]} |
---|
7 | // int objParam.id: id of the dom element |
---|
8 | // [dom objParam.parent]: in which dom element to draw the button |
---|
9 | // [str objParam.specificClass] |
---|
10 | // [int objParam.maxOptionSize]: define a max number of line to display |
---|
11 | // [int objParam.isDisable]: is the Select disable by default? |
---|
12 | // [int objParam.isDisplayed]: is the Select opened by default? |
---|
13 | // |
---|
14 | // Public functions |
---|
15 | // Select.getValue |
---|
16 | // Select.getName |
---|
17 | // Select.disable |
---|
18 | // Select.enable |
---|
19 | // Select.add |
---|
20 | // Select.remove |
---|
21 | // Select.clear |
---|
22 | // Select.select |
---|
23 | // Select.selectFirst |
---|
24 | //******************************************************** |
---|
25 | |
---|
26 | var Select = Class.create( { |
---|
27 | |
---|
28 | // Public functions ******************************************************** |
---|
29 | |
---|
30 | initialize: function( objectParameter ) |
---|
31 | { |
---|
32 | // Param |
---|
33 | this.id = objectParameter.id; |
---|
34 | this.parent = objectParameter.parent ? objectParameter.parent : document.body; |
---|
35 | this.isDisplayed = objectParameter.isDisplayed ? objectParameter.isDisplayed : false; |
---|
36 | this.isDisable = objectParameter.isDisable ? objectParameter.isDisable : false; |
---|
37 | this.maxOptionSize = objectParameter.maxOptionSize ? objectParameter.maxOptionSize : 0; |
---|
38 | this.indexOption = 0; |
---|
39 | this.isHover = false; |
---|
40 | this.arrayOptions = $(); |
---|
41 | this.values = $(); |
---|
42 | this.className = objectParameter.className ? "select_container " + objectParameter.className : "select_container"; |
---|
43 | this.classNameOption = objectParameter.classNameOption ? "select_choicelist " + objectParameter.classNameOption : "select_choicelist"; |
---|
44 | |
---|
45 | // Select div |
---|
46 | this.divSelect = $( document.createElement( "div" ) ); |
---|
47 | this.divSelect.attr( {id:this.id, class:this.className } ); |
---|
48 | |
---|
49 | this.divSelectLeft = $( document.createElement( "div" ) ) |
---|
50 | this.divSelectLeft.addClass( "select_selectLeft" ); |
---|
51 | this.divSelect.append( this.divSelectLeft ); |
---|
52 | |
---|
53 | this.divSelectMiddle = $( document.createElement( "div" ) ) |
---|
54 | this.divSelectMiddle.addClass( "select_selectMiddle" ); |
---|
55 | this.divSelect.append( this.divSelectMiddle ); |
---|
56 | |
---|
57 | this.divSelectRight = $( document.createElement( "div" ) ) |
---|
58 | this.divSelectRight.addClass( "select_selectRight" ); |
---|
59 | this.divSelect.append( this.divSelectRight ); |
---|
60 | |
---|
61 | this.parent.append( this.divSelect ); |
---|
62 | |
---|
63 | // Options div |
---|
64 | this.divOption = $( document.createElement( "div" ) ) |
---|
65 | this.divOption.addClass( this.classNameOption ); |
---|
66 | this.parent.append( this.divOption ); |
---|
67 | |
---|
68 | this.divSelect.bind( "mouseup", this, this.toggleOption ); |
---|
69 | this.divSelect.bind( 'mouseover', this, this.onMouseHover ); |
---|
70 | this.divSelect.bind( 'mouseout', this, this.onMouseOut ); |
---|
71 | this.divOption.bind( 'mouseover', this, this.onMouseHover ); |
---|
72 | this.divOption.bind( 'mouseout', this, this.onMouseOut ); |
---|
73 | |
---|
74 | if( this.isDisplayed ) |
---|
75 | this.showOption(); |
---|
76 | |
---|
77 | if( this.isDisable ) |
---|
78 | this.hideOption(); |
---|
79 | }, |
---|
80 | |
---|
81 | getName: function() |
---|
82 | { |
---|
83 | return this.itemName; |
---|
84 | }, |
---|
85 | |
---|
86 | getValue: function() |
---|
87 | { |
---|
88 | return this.itemValue; |
---|
89 | }, |
---|
90 | |
---|
91 | getValues: function() |
---|
92 | { |
---|
93 | return this.values; |
---|
94 | }, |
---|
95 | |
---|
96 | getValuesInString: function( separator ) |
---|
97 | { |
---|
98 | var valulues = $.makeArray( this.values ); |
---|
99 | if( separator ) |
---|
100 | return valulues.join( separator ); |
---|
101 | else |
---|
102 | return valulues.join(); |
---|
103 | }, |
---|
104 | |
---|
105 | getSize: function() |
---|
106 | { |
---|
107 | return this.arrayOptions.size(); |
---|
108 | }, |
---|
109 | |
---|
110 | // private |
---|
111 | toggleOption:function( event ) |
---|
112 | { |
---|
113 | var contextEvent = event.data; |
---|
114 | if( contextEvent.isDisplayed ) |
---|
115 | { |
---|
116 | contextEvent.isHover = false; |
---|
117 | contextEvent.hideOption(); |
---|
118 | } |
---|
119 | else |
---|
120 | contextEvent.showOption(); |
---|
121 | }, |
---|
122 | |
---|
123 | // private |
---|
124 | showOption:function() |
---|
125 | { |
---|
126 | if( this.isDisable ) |
---|
127 | return; |
---|
128 | |
---|
129 | this.isDisplayed = true; |
---|
130 | this.displayOptions(); |
---|
131 | this.divOption.show(); |
---|
132 | }, |
---|
133 | |
---|
134 | // private |
---|
135 | hideOption: function() |
---|
136 | { |
---|
137 | if( this.isHover ) |
---|
138 | return; |
---|
139 | |
---|
140 | this.isDisplayed = false; |
---|
141 | this.divOption.hide(); |
---|
142 | }, |
---|
143 | |
---|
144 | // public |
---|
145 | disable: function() |
---|
146 | { |
---|
147 | this.isDisable = true; |
---|
148 | this.divSelect.addClass( "disable" ); |
---|
149 | this.hideOption(); |
---|
150 | }, |
---|
151 | |
---|
152 | // public |
---|
153 | enable: function() |
---|
154 | { |
---|
155 | this.isDisable = false; |
---|
156 | this.divSelect.removeClass( "disable" ); |
---|
157 | }, |
---|
158 | |
---|
159 | // public |
---|
160 | /** |
---|
161 | * This method indicates if the itemValue is already in the array |
---|
162 | */ |
---|
163 | content: function( itemValue ) |
---|
164 | { |
---|
165 | var result = false; |
---|
166 | jQuery.each( this.arrayOptions, jQuery.proxy( function( i, divOption ) |
---|
167 | { |
---|
168 | if( divOption.itemValue == itemValue ) |
---|
169 | { |
---|
170 | result = true; |
---|
171 | return false; |
---|
172 | } |
---|
173 | }, this ) ); |
---|
174 | return result; |
---|
175 | }, |
---|
176 | |
---|
177 | // public |
---|
178 | add: function( itemValue, itemName, callback ) |
---|
179 | { |
---|
180 | var divOptionMiddle = $( document.createElement( "div" ) ); |
---|
181 | divOptionMiddle.attr( {id:"option_" + itemValue, class: "select_item_middle" } ); |
---|
182 | divOptionMiddle.html( itemName ); |
---|
183 | divOptionMiddle.itemName = itemName; |
---|
184 | divOptionMiddle.itemValue = itemValue; |
---|
185 | divOptionMiddle.callback = callback; |
---|
186 | this.divOption.append( divOptionMiddle ); |
---|
187 | |
---|
188 | divOptionMiddle.bind( 'click', [this, divOptionMiddle], this.onOptionClick ); |
---|
189 | |
---|
190 | this.arrayOptions.push( divOptionMiddle ); |
---|
191 | this.values.push( itemValue ); |
---|
192 | }, |
---|
193 | |
---|
194 | // public |
---|
195 | remove: function( itemValue ) |
---|
196 | { |
---|
197 | jQuery.each( this.arrayOptions, jQuery.proxy( function( i, divOption ) |
---|
198 | { |
---|
199 | if( divOption.itemValue == itemValue ) |
---|
200 | { |
---|
201 | $( "#option_" + itemValue ).first().remove(); |
---|
202 | var index = jQuery.inArray( divOption, this.arrayOptions ); |
---|
203 | this.arrayOptions.splice( index, 1 ); |
---|
204 | this.values.splice( index, 1 ); |
---|
205 | return false; |
---|
206 | } |
---|
207 | }, this ) ); |
---|
208 | }, |
---|
209 | |
---|
210 | // public |
---|
211 | clear: function() |
---|
212 | { |
---|
213 | this.divOption.empty(); |
---|
214 | this.arrayOptions = $(); |
---|
215 | this.values = $(); |
---|
216 | }, |
---|
217 | |
---|
218 | // public |
---|
219 | /** |
---|
220 | * This method changes only the content of the divSelect without add the item to the array |
---|
221 | * @param itemValue |
---|
222 | * @param itemName |
---|
223 | * @param callback |
---|
224 | */ |
---|
225 | changeSelectMiddle: function( itemValue, itemName, callback ) |
---|
226 | { |
---|
227 | this.add( itemValue, itemName, callback ); |
---|
228 | this.select( itemValue, callback ); |
---|
229 | this.remove( itemValue ); |
---|
230 | }, |
---|
231 | |
---|
232 | // public |
---|
233 | // @itemValue : code of the selected line |
---|
234 | // @ execute : true / false. Execute the callback. |
---|
235 | select: function( itemValue, execute ) |
---|
236 | { |
---|
237 | jQuery.each( this.arrayOptions, jQuery.proxy( function( i, divOption ) |
---|
238 | { |
---|
239 | if( divOption.itemValue == itemValue ) |
---|
240 | { |
---|
241 | this.selectOption( divOption ); |
---|
242 | if( execute && undefined != divOption.callback ) |
---|
243 | divOption.callback( divOption.itemValue ); |
---|
244 | return false; |
---|
245 | } |
---|
246 | }, this ) ); |
---|
247 | }, |
---|
248 | |
---|
249 | // public |
---|
250 | // @ execute : true / false. Execute the callback. |
---|
251 | selectFirst: function( execute ) |
---|
252 | { |
---|
253 | var divOption = this.arrayOptions[0]; |
---|
254 | if( undefined == divOption ) |
---|
255 | return; |
---|
256 | |
---|
257 | this.selectOption( divOption ); |
---|
258 | if( execute && undefined != divOption.callback ) |
---|
259 | divOption.callback( divOption.itemValue ); |
---|
260 | }, |
---|
261 | |
---|
262 | // private |
---|
263 | selectOption: function( divOption ) |
---|
264 | { |
---|
265 | this.itemName = divOption.itemName; |
---|
266 | this.itemValue = divOption.itemValue; |
---|
267 | this.divSelectMiddle.html( this.itemName ); |
---|
268 | }, |
---|
269 | |
---|
270 | // private |
---|
271 | onOptionClick: function( event ) |
---|
272 | { |
---|
273 | var contextEvent = event.data[0]; |
---|
274 | var divOption = event.data[1]; |
---|
275 | contextEvent.isHover = false; |
---|
276 | contextEvent.hideOption(); |
---|
277 | contextEvent.select( divOption.itemValue, true ); |
---|
278 | if( undefined != divOption.callback ) |
---|
279 | divOption.callback( divOption.itemValue ); |
---|
280 | }, |
---|
281 | |
---|
282 | // private |
---|
283 | onMouseHover: function( event ) |
---|
284 | { |
---|
285 | event.data.isHover = true; |
---|
286 | }, |
---|
287 | |
---|
288 | // private |
---|
289 | onMouseOut: function( event ) |
---|
290 | { |
---|
291 | event.data.isHover = false; |
---|
292 | }, |
---|
293 | |
---|
294 | // private |
---|
295 | displayOptions: function() |
---|
296 | { |
---|
297 | var fromOption = this.indexOption; |
---|
298 | var toOption = this.indexOption + this.maxOptionSize - 1; |
---|
299 | for( var i = 0; i < this.arrayOptions.size(); i++ ) |
---|
300 | { |
---|
301 | if( 0 == this.maxOptionSize ) |
---|
302 | this.arrayOptions.get( i ).show(); |
---|
303 | else if( i >= fromOption && i <= toOption ) |
---|
304 | this.arrayOptions.get( i ).show(); |
---|
305 | else |
---|
306 | this.arrayOptions.get( i ).hide(); |
---|
307 | } |
---|
308 | } |
---|
309 | } ); |
---|