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

Revision 1428, 4.2 KB checked in by rufino, 15 years ago (diff)

Ticket #442 - Criado o plugin smarty {wf_autocomplete_input} que implementa uma listagem type ahead.

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                        $('input' + elementId).value = extraParams['textValue'];
83                        $(elementId).value = extraParams['idValue'];
84                        showResult(elementId);
85                }
86        }
87
88        func = function (dados)
89        {
90                var result = dados[ajaxMethod]['data'];
91                if (result !== false){
92                        // guarda valores localmente
93                        _cache['requests'][hash]['data'] = result;
94                        _cache['requests'][hash]['populated'] = true;
95                        // Envia dados para o componente
96                        autocompletePopulate(elementId);
97                }
98        };
99
100        // Relaciona chamada ajax ao objeto
101        if(_cache['elements'] == null){
102                _cache['elements'] = new Array();
103        }
104
105        if(_cache['elements'][elementId] == null){
106                _cache['elements'][elementId] = new Array();
107                _cache['elements'][elementId]['hash'] = hash;
108                _cache['elements'][elementId]['minLength'] = extraParams['minLength'];
109        } else {
110                // por algum motivo componente já existe.
111                // Se o _cache na posição hash não for null, requisição ajax já foi feita.
112                // Se o objeto já foi populado e está sendo escrito novamente, manda popular denovo
113                //   (isso pode acontecer quando o componente é escrito através de javascript)
114                if(_cache['requests'][hash] != null)
115                        if (_cache['requests'][hash]['populated'] == true){
116                                _cache['elements'][elementId]['populated'] = null;
117                                autocompletePopulate(elementId);
118                        }
119        }
120
121        // Cria array para armazenar localmente os valores da requisição ajax
122        if(_cache['requests'] == null)
123                _cache['requests'] = new Array();
124
125        if(_cache['requests'][hash] == null){
126                _cache['requests'][hash] = new Array();
127                _cache['requests'][hash]['populated'] = null;
128                _cache['requests'][hash]['data'] = new Array();
129
130                // Faz a requisição ajax/Json
131                var nc = new NanoController();
132                nc.setWfUrl();
133                nc.setSuccessHandler(func);
134                nc.setExceptionHandler(catchJsonError);
135                nc.setErrorHandler(catchJsonError);
136                nc.addVirtualRequest(ajaxMethod,
137                {
138                        action: ajaxClass,
139                        mode:   ajaxMethod
140                }, methodParam);
141                nc.sendRequest();
142        }
143}
Note: See TracBrowser for help on using the repository browser.