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