source: trunk/workflow/js/jscode/wf_autocomplete_input.js @ 1935

Revision 1935, 4.3 KB checked in by asaikawa, 14 years ago (diff)

Ticket #442 - Inserido atributo name nos elementos e ajustada a atribuicao do valor a ser carregado

Line 
1var _cache = new Array();
2
3function checkDataLoaded(elementId)
4{
5        if (_cache['elements'][elementId]['populated'] !== true
6                && _cache['requests'][_cache['elements'][elementId]['hash']]['populated'] == true)
7        {
8                autocompletePopulate(elementId);
9        }
10}
11
12function showResult(elementId)
13{
14        var index = $(elementId).value;
15        var textResponse = (index == -1)? "Nenhum resultado encontrado!" : "OK";
16        $('response' + elementId).innerHTML = textResponse;
17        $('response' + elementId).className = (index == -1)? "span_message_error" : "span_message_success";
18}
19
20function selectAutocompleteElement(elementId)
21{
22        if ($('input' + elementId) == null) return -1;
23        var value = $('input' + elementId).value;
24        var items = _cache['requests'][_cache['elements'][elementId]['hash']]['data'];
25        var index = autocompleteIndexOf(items, value);
26        $(elementId).value = index;
27
28        $('response' + elementId).innerHTML = typeof(items);
29        showResult(elementId);
30}
31
32function autocompleteIndexOf(array, value)
33{
34        for (key in array){
35                if (array[key].toLowerCase() == value.toLowerCase()){
36                        return key;
37                }
38        }
39        return -1;
40}
41
42function arrayValues(array)
43{
44        var i = 0;
45        var arr = Array();
46        for (key in array){
47                arr[i++] = array[key];
48        }
49        return arr;
50}
51
52function autocompletePopulate(elementId)
53{
54        if (_cache['elements'][elementId]['populated'] == null){
55                var items = _cache['requests'][_cache['elements'][elementId]['hash']]['data'];
56                var values = arrayValues(items);
57                new Autocompleter.Local('input' + elementId, 'list' + elementId, values, {
58                        'choices':9,
59                        'partialChars': _cache['elements'][elementId]['minLength']
60                });
61        }
62        _cache['elements'][elementId]['populated'] = true;
63}
64
65function catchJsonError(dados){ return false; }
66
67function autocompleteSelect(elementId, ajaxClass, ajaxMethod, methodParam, extraParams)
68{
69        // se o parâmetro for um objeto, transforma em uma string para gerar o hash
70        var _param = (typeof(methodParam) == 'object')? JSON.stringify(methodParam) : methodParam;
71        var str = ajaxClass + ajaxMethod + _param;
72        // Cria hash que identifica classe, método e parâmetro.
73        // Componente verifica se hash já existe para não fazer requisições ajax desnecessárias
74        var hash = new SHA1(str).hexdigest();
75
76        if(extraParams['minLength'] == null){
77                extraParams['minLength'] = 1;
78        }
79
80        if (extraParams['idValue'] != null){
81                if (extraParams['textValue'] != null){
82                        // se texto do input já foi preenchido no momento da montagem do componente, não escreve via javascript
83                        if ($('input' + elementId).value.lenght == 0)
84                                $('input' + elementId).value = extraParams['textValue'];
85
86                        $(elementId).value = extraParams['idValue'];
87                        showResult(elementId);
88                }
89        }
90
91        func = function (dados)
92        {
93                var result = dados[ajaxMethod]['data'];
94                if (result !== false){
95                        // guarda valores localmente
96                        _cache['requests'][hash]['data'] = result;
97                        _cache['requests'][hash]['populated'] = true;
98                        // Envia dados para o componente
99                        autocompletePopulate(elementId);
100                }
101        };
102
103        // Relaciona chamada ajax ao objeto
104        if(_cache['elements'] == null){
105                _cache['elements'] = new Array();
106        }
107
108        if(_cache['elements'][elementId] == null){
109                _cache['elements'][elementId] = new Array();
110                _cache['elements'][elementId]['hash'] = hash;
111                _cache['elements'][elementId]['minLength'] = extraParams['minLength'];
112        } else {
113                // por algum motivo componente já existe.
114                // Se o _cache na posição hash não for null, requisição ajax já foi feita.
115                // Se o objeto já foi populado e está sendo escrito novamente, manda popular denovo
116                //   (isso pode acontecer quando o componente é escrito através de javascript)
117                if(_cache['requests'][hash] != null)
118                        if (_cache['requests'][hash]['populated'] == true){
119                                _cache['elements'][elementId]['populated'] = null;
120                                autocompletePopulate(elementId);
121                        }
122        }
123
124        // Cria array para armazenar localmente os valores da requisição ajax
125        if(_cache['requests'] == null)
126                _cache['requests'] = new Array();
127
128        if(_cache['requests'][hash] == null){
129                _cache['requests'][hash] = new Array();
130                _cache['requests'][hash]['populated'] = null;
131                _cache['requests'][hash]['data'] = new Array();
132
133                // Faz a requisição ajax/Json
134                var nc = new NanoController();
135                nc.setWfUrl();
136                nc.setSuccessHandler(func);
137                nc.setExceptionHandler(catchJsonError);
138                nc.setErrorHandler(catchJsonError);
139                nc.addVirtualRequest(ajaxMethod,
140                {
141                        action: ajaxClass,
142                        mode:   ajaxMethod
143                }, methodParam);
144                nc.sendRequest();
145        }
146}
Note: See TracBrowser for help on using the repository browser.