source: trunk/workflow/js/jscode/wf_select_duplex.js @ 795

Revision 795, 5.2 KB checked in by viani, 15 years ago (diff)

Ticket #488 - Inclusão do módulo workflow no ramo trunk do repositório Expresso.

Line 
1/*
2* Move the selected items from selectbox "from" to "to",
3* where "from" and "to" are the selectbox names.
4*/
5function moveOptions(from, to) {
6        fromList = eval('document.forms[0].' + from);
7        toList = eval('document.forms[0].' + to);
8        var sel = false;
9
10        // walk through "from" list searching the selected items,
11        // copy them to "to" and remove from "from"
12        for (i = fromList.options.length - 1; i >= 0; i--)
13        {
14                var current = fromList.options[i];
15                if (current.selected)
16                {
17                        sel = true;
18                        txt = current.text;
19                        val = current.value;
20                        toList.options[toList.length] = new Option(txt,val);
21                        fromList.options[i] = null;
22                }
23        }
24
25        if (!sel)
26                alert ("Selecione um item!");
27
28        // sort selectbox "to"
29        sortOptions(to);
30}
31
32/*
33* Populate selectbox with the array elements
34*/
35function populateSelect(objName, array) {
36        obj = eval('document.forms[0].' + objName);
37        obj.length = 0;
38
39        // walk through array and add elements to the selectbox
40        for (i = 0; i < array.length; i++) {
41                if( isArray(array[i]) ) {
42                        obj.options[obj.length] = new Option(array[i].text, array[i].value);
43                }
44        }
45}
46
47/*
48* Populate the selectboxes.
49* @param objNameL Left selectbox name
50* @param arrayL   Array with the elements of the left selectbox
51* @param sortL    Boolean value to sort the left selectbox
52* @param objNameR Right selectbox name
53* @param arrayR   Array with the elements of the right selectbox
54* @param sortR    Boolean value to sort the right selectbox
55* @param execDiff This value indicates the direction of the diff
56*                 Values:  LEFT  - remove the left elements found in the right selectbox
57*                          RIGHT - remove the right elements found in the left selectbox
58*                          NONE  - don't execute diff
59*/
60function populateSelects(objNameL, arrayL, sortL,
61                                                 objNameR, arrayR, sortR,
62                                                 execDiff) {
63        if( isArray(arrayL) )
64                populateSelect(objNameL, arrayL);
65
66        if( isArray(arrayR) )
67                populateSelect(objNameR, arrayR);
68
69        if( sortL )
70                sortOptions(objNameL);
71
72        if( sortR )
73                sortOptions(objNameR);
74
75        switch(execDiff) {
76                case "LEFT":
77                        selectDiff( objNameR, objNameL );
78                        break;
79                case "RIGHT":
80                        selectDiff( objNameL, objNameR );
81                        break;
82                default:
83                        break;
84        }
85}
86
87/*
88* Select all items in a selectbox.
89*/
90function selectAllOptions( objName ) {
91        obj = eval('document.forms[0].' + objName);
92
93        for(i = 0; i < obj.length; i++) {
94                obj.options[i].selected = true;
95        }
96}
97
98/*
99* Check if the parameter is an array.
100* Return true if it's an array, and false, if it's not.
101*/
102function isArray( obj ) {
103        if (typeof obj == 'object') {
104                var criterion = obj.constructor.toString().match(/array/i);
105                return (criterion != null);
106        }
107        return false;
108}
109
110/*
111* Get the values of a selectbox and return them in an array.
112*/
113function select2array(objName) {
114        obj = eval('document.forms[0].' + objName);
115
116        if(obj){
117                array = new Array();
118
119                for(i = 0; i < obj.length; i++) {
120                        array[i] = new Array(2);
121                        array[i].text  = obj.options[i].text;
122                        array[i].value = obj.options[i].value;
123                }
124
125                return array;
126        }
127        return null;
128}
129
130/*
131* Copy the values of an array to a selectbox.
132*/
133function array2select(array, objName)
134{
135        obj = eval('document.forms[0].' + objName);
136
137        /* remove all elements from the selectbox */
138        for (i = obj.length - 1; i >= 0; i--)
139                obj.options[i] = null;
140
141        if (isArray(array))
142                for (i = 0; i < array.length; i++)
143                        obj.options[i] = new Option(array[i].text,array[i].value);
144}
145
146/*
147* Redefine the javascript sort function.
148* We need to compare the attributes a.text and b.text.
149*/
150function sortFunction(a, b) {
151        if(a.text > b.text)
152                return 1;
153        if(a.text < b.text)
154                return -1;
155        return 0;
156}
157
158/*
159* Sort the elements of a selectbox.
160*/
161function sortOptions(objName) {
162        arrTexts = select2array(objName);
163        arrTexts.sort(sortFunction);
164        array2select(arrTexts, objName);
165}
166
167/*
168* Remove the accents of a string.
169*/
170function removeAccents( text ){
171        var accents    = "áàãââÁÀÃÂéêÉÊíÍóõôÓÔÕúüÚÜçÇ";
172        var traduction = "aaaaaAAAAeeEEiIoooOOOuuUUcC";
173        var pos, chr;
174        var newTxt = "";
175
176        for (var i = 0; i < text.length; i++){
177                chr = text.charAt(i);
178                pos  = accents.indexOf(chr);
179                if (pos > -1)
180                        newTxt += traduction.charAt(pos);
181                else
182                        newTxt += text.charAt(i);
183        }
184        return newTxt;
185}
186
187function search(array, key) {
188        for (i = 0; i < array.length; i++){
189                if (key == array[i].text)
190                        return i;
191        }
192        return -1;
193
194}
195
196/*
197* Binary search in a SORTED ARRAY.
198* Returns the element position, or -1.
199*/
200function binarySearch(array, key) {
201        var i, lo, hi;
202
203        // get the first and the last positions of the array,
204        lo = 0;
205        hi = array.length - 1;
206
207        while( lo <= hi ) {
208                // get the middle value
209                var mid = Math.floor((lo + hi) / 2);
210
211                // if the key is in the 'mid' position, return 'mid'
212                if( key == array[mid].text )
213                        return mid;
214                // if key > 'mid' text,
215                else if( key > array[mid].text)
216                        lo = mid + 1;
217                // key < array[mid]
218                else
219                        hi = mid - 1;
220        }
221        return -1;
222}
223
224/*
225* Difference between two selectboxes. Remove the elements of the objName2
226* that are found in objName1.
227*/
228function selectDiff(objName1, objName2) {
229        array1 = select2array(objName1);
230
231        obj2 = eval('document.forms[0].' + objName2);
232
233        for( var i = array1.length - 1; i >= 0; i-- ) {
234                array2 = select2array(objName2);
235                j = search(array2, array1[i].text);
236                if( j != -1 )
237                        obj2.options[j] = null;
238        }
239}
Note: See TracBrowser for help on using the repository browser.