source: tapas/web/resources/js/classesForJQuery/Select.js @ 382

Last change on this file since 382 was 382, checked in by vmipsl, 12 years ago

clean

File size: 8.4 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            if( divOption.itemValue == itemValue )
184            {
185                $( "#option_" + itemValue ).first().remove();
186                var index = jQuery.inArray( divOption, this.arrayOptions );
187                this.arrayOptions.splice( index, 1 );
188                return false;
189            }
190        }, this ) );
191    },
192
193    // public
194    clear: function()
195    {
196        this.divOption.empty();
197        this.arrayOptions = $();
198    },
199
200    // public
201    /**
202     * This method changes only the content of the divSelect without add the item to the array
203     * @param itemValue
204     * @param itemName
205     * @param callback
206     */
207    changeSelectMiddle: function( itemValue, itemName, callback )
208    {
209        this.add( itemValue, itemName, callback );
210        this.select( itemValue, callback );
211        this.remove( itemValue );
212    },
213
214    // public
215    // @itemValue : code of the selected line
216    // @ execute : true / false. Execute the callback.
217    select: function( itemValue, execute )
218    {
219        jQuery.each( this.arrayOptions, jQuery.proxy( function( i, divOption )
220        {
221            if( divOption.itemValue == itemValue )
222            {
223                this.selectOption( divOption );
224                if( execute && undefined != divOption.callback )
225                    divOption.callback( divOption.itemValue );
226                return false;
227            }
228        }, this ) );
229    },
230
231    // public
232    // @ execute : true / false. Execute the callback.
233    selectFirst: function( execute )
234    {
235        var divOption = this.arrayOptions[0];
236        if( undefined == divOption )
237            return;
238
239        this.selectOption( divOption );
240        if( execute && undefined != divOption.callback )
241            divOption.callback( divOption.itemValue );
242    },
243
244    // private
245    selectOption: function( divOption )
246    {
247        this.itemName = divOption.itemName;
248        this.itemValue = divOption.itemValue;
249        this.divSelectMiddle.html( this.itemName );
250    },
251
252    // private
253    onOptionClick: function( event )
254    {
255        var contextEvent = event.data[0];
256        var divOption = event.data[1];
257        contextEvent.isHover = false;
258        contextEvent.hideOption();
259        contextEvent.select( divOption.itemValue, true );
260        if( undefined != divOption.callback )
261            divOption.callback( divOption.itemValue );
262    },
263
264    // private
265    onMouseHover: function( event )
266    {
267        event.data.isHover = true;
268    },
269
270    // private
271    onMouseOut: function( event )
272    {
273        event.data.isHover = false;
274    },
275
276    // private
277    displayOptions: function()
278    {
279        var fromOption = this.indexOption;
280        var toOption = this.indexOption + this.maxOptionSize - 1;
281        for( var i = 0; i < this.arrayOptions.size(); i++ )
282        {
283            if( 0 == this.maxOptionSize )
284                this.arrayOptions.get( i ).show();
285            else if( i >= fromOption && i <= toOption )
286                this.arrayOptions.get( i ).show();
287            else
288                this.arrayOptions.get( i ).hide();
289        }
290    }
291} );
Note: See TracBrowser for help on using the repository browser.