source: ether_megapoli/trunk/web/resources/js/Select.js @ 155

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

[Visualization] javascript librairies

  • Property svn:executable set to *
File size: 7.6 KB
Line 
1//********************************************************
2// Class Select
3// Display a Select
4//
5// obj objParam: {id, [parent], [specificClass], [maxOptionSize], [isDisable], [isDisplayed], [arrayData]}
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//    [arr objParam.arrayData]: array of data to display
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 = (true == objectParameter.isDisplayed) ? true : false;
36        this.isDisable = (true == objectParameter.isDisable) ? true : false;
37        this.maxOptionSize = objectParameter.maxOptionSize ? objectParameter.maxOptionSize : 0;
38        this.indexOption = 0;
39        this.isHover = false;
40        this.arrayData = objectParameter.arrayData ? objectParameter.arrayData : $A();
41        this.arrayOptions = $A();
42
43        // Select div
44        this.divSelect = document.createElement( "div" );
45        this.divSelect.id = this.id;
46        this.divSelect.className = "select_container";
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 = "select_choicelist";
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    // private
93    toggleOption:function()
94    {
95        if( this.isDisplayed )
96        {
97            this.isHover = false;
98            this.hideOption();
99        }
100        else
101            this.showOption();
102    },
103
104    // private
105    showOption:function()
106    {
107        if( this.isDisable )
108            return;
109
110        this.isDisplayed = true;
111        this.displayOptions();
112        this.divOption.style.display = "block";
113    },
114
115    // private
116    hideOption: function()
117    {
118        if( this.isHover )
119            return;
120
121        this.isDisplayed = false;
122        this.divOption.style.display = "none";
123    },
124
125    // public
126    disable: function()
127    {
128        this.isDisable = true;
129        this.divSelect.addClassName( "disable" );
130        this.hideOption();
131    },
132
133    // public
134    enable: function()
135    {
136        this.isDisable = false;
137        this.divSelect.removeClassName( "disable" );
138    },
139
140    // public
141    add: function( itemValue, itemName, callback )
142    {
143
144        var divOption = document.createElement( "div" );
145        divOption.className = "select_item_middle";
146        divOption.innerHTML = itemName;
147        divOption.itemName = itemName;
148        divOption.itemValue = itemValue;
149        divOption.callback = callback;
150        this.divOption.appendChild( divOption );
151
152        Event.observe( divOption, 'click', this.onOptionClick.bind( this, divOption ) );
153
154        this.arrayOptions.push( divOption );
155    },
156
157    // public
158    remove: function( itemValue )
159    {
160        this.arrayOptions.each( function( divOption )
161        {
162            if( divOption.itemValue == itemValue )
163            {
164                this.divOption.removeChild( divOption );
165                this.arrayOptions = this.arrayOptions.without( divOption );
166                throw $break;
167            }
168        }.bind( this ) );
169    },
170
171    // public
172    clear: function()
173    {
174        this.arrayOptions.each( function( divOption )
175        {
176            this.divOption.removeChild( divOption );
177            this.arrayOptions = this.arrayOptions.without( divOption );
178        }.bind( this ) );
179    },
180
181    // public
182    // @itemValue : code of the selected line
183    // @ execute : true / false. Execute the callback.
184    select: function( itemValue, execute )
185    {
186        this.arrayOptions.each( function( divOption )
187        {
188            if( divOption.itemValue == itemValue )
189            {
190                this.selectOption( divOption );
191                if( execute && undefined != divOption.callback )
192                    divOption.callback( divOption.itemValue );
193                throw $break;
194            }
195        }.bind( this ) );
196    },
197
198    // public
199    // @ execute : true / false. Execute the callback.
200    selectFirst: function( execute )
201    {
202        var divOption = this.arrayOptions.first();
203        if( undefined == divOption )
204            return;
205
206        this.selectOption( divOption );
207        if( execute && undefined != divOption.callback )
208            divOption.callback( divOption.itemValue );
209    },
210
211    // private
212    selectOption: function( divOption )
213    {
214        this.itemName = divOption.itemName;
215        this.itemValue = divOption.itemValue;
216        this.divSelectMiddle.innerHTML = divOption.itemName;
217    },
218
219    // private
220    onOptionClick: function( divOption )
221    {
222        this.isHover = false;
223        this.hideOption();
224        this.select( divOption.itemValue, true );
225        if( undefined != divOption.callback )
226            divOption.callback( divOption.itemValue );
227    },
228
229    // private
230    onMouseHover: function()
231    {
232        this.isHover = true;
233    },
234
235    // private
236    onMouseOut: function()
237    {
238        this.isHover = false;
239    },
240
241    // private
242    displayOptions: function()
243    {
244        var fromOption = this.indexOption;
245        var toOption = this.indexOption + this.maxOptionSize - 1;
246        for( var i = 0; i < this.arrayOptions.size(); i++ )
247        {
248            if( 0 == this.maxOptionSize )
249                this.arrayOptions[i].style.display = "";
250            else if( i >= fromOption && i <= toOption )
251                this.arrayOptions[i].style.display = "";
252            else
253                this.arrayOptions[i].style.display = "none";
254        }
255    }
256} );
Note: See TracBrowser for help on using the repository browser.