source: branches/2.5/workflow/js/jscode/participants.js @ 8232

Revision 8232, 8.7 KB checked in by douglas, 11 years ago (diff)

Ticket #0000 - Copiadas as alterações do Trunk. Versão final 2.5.1.

  • Property svn:executable set to *
Line 
1var participantsClone = null;
2var chkOnlyVisibleAccountsClone = null;
3var searchTimer = null;
4var globalSearchEnter = true;
5
6function callAjax(action, mode, handler, parameters)
7{
8        var url = '$this.' + action + '.' + mode;
9        if (parameters)
10                cExecute(url, handler, $H(parameters).toQueryString());
11        else
12                cExecute(url, handler);
13}
14
15function getSectors()
16{
17        function resultGetSectors(data)
18        {
19                setSelectValue($('sector'), data['sectors']);
20                $('sector').onchange = getParticipants;
21                resultGetParticipants(data['participants']);
22        }
23
24        var params = {
25                organization: $F('organization'),
26                onlyVisibleAccounts: $F('onlyVisibleAccounts'),
27                entities: $F('entities'),
28                id: $F('id'),
29                usePreffix: $F('usePreffix'),
30                useCCParams: $F('useCCParams')
31        };
32        callAjax('bo_participants', 'getSectors', resultGetSectors, params);
33}
34
35function getParticipants()
36{
37        var params = {
38                context: $F('sector'),
39                onlyVisibleAccounts: $F('onlyVisibleAccounts'),
40                entities: $F('entities'),
41                id: $F('id'),
42                usePreffix: $F('usePreffix'),
43                useCCParams: $F('useCCParams')
44        };
45        callAjax('bo_participants', 'getEntities', resultGetParticipants, params);
46}
47
48function resultGetParticipants(data)
49{
50        $('search').value = '';
51        setSelectValue($('participants'), data);
52        participantsClone = data;
53        if($('onlyVisibleAccounts'))
54                chkOnlyVisibleAccountsClone = $('onlyVisibleAccounts').checked;
55}
56
57function searchParticipantsTimer(e)
58{
59        if (checkShortcuts((e) ? e : window.event))
60                return true;
61
62        if (searchTimer)
63                clearTimeout(searchTimer);
64
65        searchTimer = setTimeout(function(){searchParticipants($F('search'));}, 250);
66}
67
68function searchParticipants(searchString)
69{
70        var reg = new RegExp("<option[^>]*>[^<]*" + searchString + "[^<]*<\/option>", "gi");
71        setSelectValue($('participants'), participantsClone.match(reg));
72        if($('onlyVisibleAccounts'))
73                $('onlyVisibleAccounts').checked = chkOnlyVisibleAccountsClone;
74
75        var participants = $('participants');
76        if (participants.options[0])
77                participants.selectedIndex = 0;
78}
79
80function checkShortcuts(e)
81{
82        var whichCode = (e.which) ? e.which : e.keyCode;
83        var handled = false;
84
85        if (whichCode == 13) /* ENTER */
86        {
87                $('addUserLink').onclick();
88                handled = true;
89        }
90
91        if (whichCode == 27) /* ESC */
92        {
93                $('exitLink').onclick();
94                handled = true;
95        }
96
97        if (whichCode == 38) /* key up */
98        {
99                var participants = $('participants');
100                if (participants.selectedIndex > 0)
101                        if (!participants[participants.selectedIndex - 1].disabled)
102                                participants.selectedIndex--;
103                handled = true;
104        }
105
106        if (whichCode == 40) /* key down */
107        {
108                var participants = $('participants');
109                if (participants.selectedIndex < participants.length - 1)
110                        if (!participants[participants.selectedIndex + 1].disabled)
111                                participants.selectedIndex++;
112                handled = true;
113        }
114
115        return handled;
116}
117
118if (Event.observe)
119{
120        Event.observe(window, 'load', function() {
121                if (typeof Prototype == 'undefined')
122                        return;
123                /* atribui as ações aos eventos */
124                var obj = $('organization');
125                if (obj)
126                        obj.onchange = getSectors;
127
128                obj = $('sector');
129                if (obj)
130                        obj.onchange = getParticipants;
131
132                obj = $('search');
133                if (obj)
134                        obj.onkeydown = searchParticipantsTimer;
135
136                obj = $('participants');
137                if (obj){
138                        participantsClone = obj.innerHTML;
139                        if($('onlyVisibleAccounts'))
140                                chkOnlyVisibleAccountsClone = $('onlyVisibleAccounts').checked;
141                }
142
143                obj = $('exitLink');
144                if (obj)
145                        obj.onclick = function(){window.close();};
146
147                obj = $('addUserLink');
148                if (obj)
149                        obj.onclick = addUser;
150
151                obj = $('search');
152                if (obj)
153                        obj.focus();
154
155                obj = $('onlyVisibleAccounts');
156                if (obj)
157                        obj.onclick = checkOnlyVisibleAccounts;
158
159                obj = $('useGlobalSearch');
160                if (obj)
161                {
162                        obj.onclick = toggleFullSearch;
163                        toggleFullSearch();
164                }
165        });
166};
167
168function checkOnlyVisibleAccounts()
169{
170        if(!$('useGlobalSearch').checked)
171                getParticipants();
172        else
173                toggleFullSearch();
174}
175
176function participantsFilterName(name)
177{
178        if (!$('useGlobalSearch').checked)
179                return name;
180
181        return name.substr(0, name.lastIndexOf('(') - 1);
182}
183
184function addUser()
185{
186        var participants = $('participants');
187        var target = window.opener.document.getElementById($F('target'));
188        var previous = '';
189        var current = '';
190
191        if ((target.tagName == 'INPUT') || (target.tagName == 'TEXTAREA'))
192        {
193                previous = target.value;
194                if ($F('id') == 'mail')
195                {
196                        var participantsLength = participants.options.length;
197                        var current = null;
198                        for (var i = 0; i < participantsLength; i++)
199                        {
200                                current = participants.options[i];
201                                if (current.selected)
202                                        target.value += '"' + participantsFilterName(current.text) + '" ' + '<' + current.value + '>, ';
203                        }
204                }
205                else
206                {
207                        if (participants.selectedIndex > -1)
208                        {
209                                target.value = participants.options[participants.selectedIndex].value.replace('u','');
210                                window.opener.document.getElementById(target.id + '_desc').value = participantsFilterName(participants.options[participants.selectedIndex].text);
211                        }
212                }
213                current = target.value;
214        }
215        else
216        {
217                previous = target.innerHTML;
218                var options = '';
219                var participantsLength = participants.options.length;
220                var current = null;
221                var insertElement;
222                for (var i = 0; i < participantsLength; i++)
223                {
224                        current = participants.options[i];
225                        if (current.selected)
226                        {
227                                /* checa se o elemento que será inserido já existe na select box destino */
228                                insertElement = true;
229                                for (var j = 0; j < target.options.length; j++)
230                                        if (target.options[j].value == current.value)
231                                        {
232                                                insertElement = false;
233                                                break;
234                                        }
235                                if (insertElement)
236                                        options += '<option value="' + current.value + '">' + participantsFilterName(current.text) + '</option>';
237                        }
238                }
239                if (options.length > 0)
240                {
241                        setSelectValue(target, target.innerHTML + options);
242                        /* refaz o link que se perde quando modifica-se o innerHTML da select box */
243                        target = window.opener.document.getElementById($F('target'));
244                }
245                current = target.innerHTML;
246        }
247
248        /* se o código do desenvolvedor está esperando o evento onchange, dispara o evento */
249        if (target.onchange)
250                if (current != previous)
251                        target.onchange();
252}
253
254function setSelectValue(obj, value)
255{
256        /* IE MAGIC */
257        if (obj.outerHTML)
258        {
259                obj.innerHTML = '';
260                obj.outerHTML = obj.outerHTML.match(/<select[^>]*>/gi) + value + '</select>';
261        }
262        else
263                obj.innerHTML = value;
264}
265
266function participantsRemoveUser(obj)
267{
268        if (obj.tagName == 'INPUT')
269        {
270                obj.value = '';
271                obj = document.getElementById(obj.id + '_desc');
272                if (obj)
273                        obj.value = '';
274        }
275        else
276        {
277                for(var i = 0;i < obj.options.length; i++)
278                        if(obj.options[i].selected)
279                                obj.options[i--] = null;
280        }
281}
282
283function toggleFullSearch()
284{
285        $('search').value = '';
286
287        if ($('useGlobalSearch').checked)
288        {
289                globalSearchEnter = true;
290                if ($('organizationSectors'))
291                        $('organizationSectors').hide();
292                if ($('globalSearchTitle'))
293                        $('globalSearchTitle').show();
294                setSelectValue($('participants'), '');
295                $('search').onkeydown = globalSearchKeyAnalyzer;
296                $('globalSearchWarnings').innerHTML = 'Para executar a busca, pressione ENTER.';
297        }
298        else
299        {
300                $('globalSearchWarnings').innerHTML = '';
301                if ($('globalSearchTitle'))
302                        $('globalSearchTitle').hide();
303                if ($('organizationSectors'))
304                        $('organizationSectors').show();
305                searchParticipants('');
306                $('search').onkeydown = searchParticipantsTimer;
307        }
308        $('search').focus();
309}
310
311function checkGlobalSearchShortcuts(e)
312{
313        var whichCode = (e.which) ? e.which : e.keyCode;
314        var handled = false;
315
316        /* ENTER */
317        if ((whichCode == 13) && globalSearchEnter)
318        {
319                performGlobalSearch();
320                handled = true;
321                globalSearchEnter = false;
322        }
323
324        if (handled == false)
325        {
326                handled = checkShortcuts(e);
327                if (handled == true)
328                        globalSearchEnter = false;
329        }
330
331        if (handled == false)
332                globalSearchEnter = true;
333
334        return handled;
335}
336
337function globalSearchKeyAnalyzer(e)
338{
339        if (checkGlobalSearchShortcuts((e) ? e : window.event))
340                return true;
341
342}
343
344function performGlobalSearch()
345{
346        function resultPerformGlobalSearch(data)
347        {
348                setSelectValue($('participants'), data['participants']);
349                if (data['warnings'])
350                        if (data['warnings'].length > 0)
351                                $('globalSearchWarnings').innerHTML = data['warnings'].join('<br/>');
352        }
353
354        $('globalSearchWarnings').innerHTML = '';
355        var params = {
356                onlyVisibleAccounts: $F('onlyVisibleAccounts'),
357                searchTerm: $F('search'),
358                entities: $F('entities'),
359                id: $F('id'),
360                usePreffix: $F('usePreffix'),
361                useCCParams: $F('useCCParams')
362        };
363        callAjax('bo_participants', 'globalSearch', resultPerformGlobalSearch, params);
364}
365
366function openParticipantsWindow(target, option)
367{
368        newWidth   = 500;
369        newHeight  = 315;
370        newScreenX = screen.width - newWidth;
371        newScreenY = 0;
372        page = 'index.php?menuaction=workflow.ui_participants.form';
373        if (target)
374                page += "&target_element=" + target;
375        if (option)
376                page += "&" + option;
377
378        window.open(page,'','width='+newWidth+',height='+newHeight+',screenX='+newScreenX+',left='+newScreenX+',screenY='+newScreenY+',top='+newScreenY+',toolbar=no,scrollbars=no,resizable=no');
379}
Note: See TracBrowser for help on using the repository browser.