source: ether_megapoli/trunk/web/resources/js/classesForJQuery/Select.js @ 234

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

Look apple

File size: 8.7 KB
Line 
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
26var 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.className = objectParameter.className ? "select_container " + objectParameter.className : "select_container";
42        this.classNameOption = objectParameter.classNameOption ? "select_choicelist " + objectParameter.classNameOption : "select_choicelist";
43
44        // Select div
45        this.divSelect = $( document.createElement( "div" ) );
46        this.divSelect.attr( {id:this.id, class:this.className} );
47
48        this.divSelectLeft = $( document.createElement( "div" ) )
49        this.divSelectLeft.addClass( "select_selectLeft" );
50        this.divSelect.append( this.divSelectLeft );
51
52        this.divSelectMiddle = $( document.createElement( "div" ) )
53        this.divSelectMiddle.addClass( "select_selectMiddle" );
54        this.divSelect.append( this.divSelectMiddle );
55
56        this.divSelectRight = $( document.createElement( "div" ) )
57        this.divSelectRight.addClass( "select_selectRight" );
58        this.divSelect.append( this.divSelectRight );
59
60        this.parent.append( this.divSelect );
61
62        // Options div
63        this.divOption = $( document.createElement( "div" ) )
64        this.divOption.addClass( this.classNameOption );
65        this.parent.append( this.divOption );
66
67        this.divSelect.bind( "mouseup", this, this.toggleOption );
68        this.divSelect.bind( 'mouseover', this, this.onMouseHover );
69        this.divSelect.bind( 'mouseout', this, this.onMouseOut );
70        this.divOption.bind( 'mouseover', this, this.onMouseHover );
71        this.divOption.bind( 'mouseout', this, this.onMouseOut );
72
73        if( this.isDisplayed )
74            this.showOption();
75
76        if( this.isDisable )
77            this.hideOption();
78    },
79
80    getName: function()
81    {
82        return this.itemName;
83    },
84
85    getValue: function()
86    {
87        return this.itemValue;
88    },
89
90    getSize: function()
91    {
92        return this.arrayOptions.size();
93    },
94
95    // private
96    toggleOption:function( event )
97    {
98        var contextEvent = event.data;
99        if( contextEvent.isDisplayed )
100        {
101            contextEvent.isHover = false;
102            contextEvent.hideOption();
103        }
104        else
105            contextEvent.showOption();
106    },
107
108    // private
109    showOption:function()
110    {
111        if( this.isDisable )
112            return;
113
114        this.isDisplayed = true;
115        this.displayOptions();
116        this.divOption.show();
117    },
118
119    // private
120    hideOption: function()
121    {
122        if( this.isHover )
123            return;
124
125        this.isDisplayed = false;
126        this.divOption.hide();
127    },
128
129    // public
130    disable: function()
131    {
132        this.isDisable = true;
133        this.divSelect.addClass( "disable" );
134        this.hideOption();
135    },
136
137    // public
138    enable: function()
139    {
140        this.isDisable = false;
141        this.divSelect.removeClass( "disable" );
142    },
143
144    // public
145    /**
146     * This method indicates if the itemValue is already in the array
147     */
148    content: function( itemValue )
149    {
150        var result = false;
151        jQuery.each( this.arrayOptions, jQuery.proxy( function( i, divOption )
152        {
153            if( divOption.itemValue == itemValue )
154            {
155                result = true;
156                return false;
157            }
158        }, this ) );
159        return result;
160    },
161
162    // public
163    add: function( itemValue, itemName, callback )
164    {
165        var divOptionMiddle = $( document.createElement( "div" ) );
166        divOptionMiddle.attr( {id:"option_" + itemValue, class: "select_item_middle" } );
167        divOptionMiddle.html( itemName );
168        divOptionMiddle.itemName = itemName;
169        divOptionMiddle.itemValue = itemValue;
170        divOptionMiddle.callback = callback;
171        this.divOption.append( divOptionMiddle );
172
173        divOptionMiddle.bind( 'click', [this, divOptionMiddle], this.onOptionClick );
174
175        this.arrayOptions.push( divOptionMiddle );
176    },
177
178    // public
179    remove: function( itemValue )
180    {
181        jQuery.each( this.arrayOptions, jQuery.proxy( function( i, divOption )
182        {
183//            alert( itemValue + " " + divOption.itemValue + ", " + this.divOption.size() + ", " + this.arrayOptions.size() );
184            if( divOption.itemValue == itemValue )
185            {
186                $( "#option_" + itemValue ).first().remove();
187//                this.divOption.not( $( "#option_" + itemValue )[0] );
188                var index = jQuery.inArray( divOption, this.arrayOptions );
189                this.arrayOptions.splice( index, 1 );
190//                alert( index + ", " + this.divOption.size() + ", " + this.arrayOptions.size() );
191                return false;
192            }
193        }, this ) );
194    },
195
196    // public
197    clear: function()
198    {
199        this.divOption.empty();
200        this.arrayOptions = $();
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        jQuery.each( this.arrayOptions, jQuery.proxy( function( i, divOption )
223        {
224            if( divOption.itemValue == itemValue )
225            {
226                this.selectOption( divOption );
227                if( execute && undefined != divOption.callback )
228                    divOption.callback( divOption.itemValue );
229                return false;
230            }
231        }, this ) );
232    },
233
234    // public
235    // @ execute : true / false. Execute the callback.
236    selectFirst: function( execute )
237    {
238        var divOption = this.arrayOptions[0];
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.html( this.itemName );
253    },
254
255    // private
256    onOptionClick: function( event )
257    {
258        var contextEvent = event.data[0];
259        var divOption = event.data[1];
260        contextEvent.isHover = false;
261        contextEvent.hideOption();
262        contextEvent.select( divOption.itemValue, true );
263        if( undefined != divOption.callback )
264            divOption.callback( divOption.itemValue );
265    },
266
267    // private
268    onMouseHover: function( event )
269    {
270        event.data.isHover = true;
271    },
272
273    // private
274    onMouseOut: function( event )
275    {
276        event.data.isHover = false;
277    },
278
279    // private
280    displayOptions: function()
281    {
282        var fromOption = this.indexOption;
283        var toOption = this.indexOption + this.maxOptionSize - 1;
284        for( var i = 0; i < this.arrayOptions.size(); i++ )
285        {
286            if( 0 == this.maxOptionSize )
287                this.arrayOptions.get( i ).show();
288            else if( i >= fromOption && i <= toOption )
289                this.arrayOptions.get( i ).show();
290            else
291                this.arrayOptions.get( i ).hide();
292        }
293    }
294} );
Note: See TracBrowser for help on using the repository browser.