source: branches/2.2/expressoMail1_2/js/DropDownContacts.js @ 3252

Revision 3252, 10.0 KB checked in by rafaelraymundo, 14 years ago (diff)

Ticket #1280 - Contatos dinâmicos do e-mail, pesquisar por parte do nome

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1/**********************************************************************************\
2* Written by Joao Alfredo Knopik Junior (JakJr) <joao.alfredo@gmail.com>          *
3* ------------------------------------------------------------------------------- *
4*  This program is free software; you can redistribute it and/or modify it        *
5*  under the terms of the GNU General Public License as published by the              *
6*  Free Software Foundation; either version 2 of the License, or (at your option) *
7*  any later version.                                                             *
8\**********************************************************************************/
9// variavel global que salva o contato atual selecionado.
10var actualSelectedContact = 0;
11var setTimeOutLayer = 0;
12var div_message_scroll = 0;
13var tst_i = 0;
14function search_contacts(key_pressed, fld_id)
15{
16        div_message_scroll = Element('div_message_scroll_'+ fld_id.substring(fld_id.length - 1, fld_id.length));
17
18        var string_contacts = contacts
19        if (Element(fld_id))
20                var mail = Element(fld_id).value;
21        else
22                return;
23       
24        var array_contacts = string_contacts.split(",");
25        var tmp = mail.split(",");
26        mail = trim(tmp[tmp.length - 1]);
27        tmp_mail = mail;
28       
29        seekDot = /\./gi;
30        mail = mail.replace(seekDot, "[.]");
31        mail = mail.replace('"', "&quot;");
32        mail = mail.replace("\n", "");
33        mail = mail.replace("\r", "");
34        mail = mail.replace("\t", "");
35        posX = findPosX(document.getElementById(fld_id));
36        posY = findPosY(document.getElementById(fld_id));
37
38        if ((!string_contacts) || (string_contacts.length==0) || (mail.length==0)){
39                hideTip();
40                return;
41        }
42               
43        if (mail.length == 0){
44                hideTip();
45                return;
46        }
47       
48        var RegExp_name = new RegExp(mail, "i");
49        var RegExp_mail = new RegExp(mail+".*@", "i");
50
51        var match_contacts = new Array();
52        var match_index = 0;
53
54        for (var i=0; i<array_contacts.length; i++){
55                tmp = array_contacts[i].split(";");
56                if ( (RegExp_name.test(tmp[0])) || (RegExp_mail.test(tmp[1])) ){
57                        if (tmp[1])
58                        {
59                                var _tmp1 = RegExp_mail.exec(tmp[1]);
60                                if ( _tmp1 )
61                                        tmp[1] = tmp[1].replace(RegExp_mail, _tmp1[0].bold());
62                                var _tmp0 = RegExp_name.exec(tmp[0]);
63                                if ( _tmp0 )
64                                        tmp[0] = tmp[0].replace(RegExp_name, _tmp0[0].bold());
65                                match_contacts[match_index] = '&quot;' + tmp[0] + '&quot; &lt;' + tmp[1] + '&gt;';
66                                match_index++;
67                        }
68                }
69        }
70
71        if (match_contacts.length == 0){
72                hideTip();
73                return;
74        }
75
76        var table_contacts_header = "<table style='font-family:arial;font-size:12;color:#0000CF' border=0 cellpadding=0 cellspacing=0>";
77        var table_contacts_foot = "</table>";
78        var lines = '';
79        var REG_EXP = /^[^\#|^\$|^\%|^\!|^\?|^\"|^\']+$/;
80        var match_cont = "";
81        for (var i=0; i<match_contacts.length; i++)
82        {
83                match_contacts[i] = unescape(match_contacts[i]);
84                var aux = match_contacts[i].split("");
85                for(var j in aux){
86                        if(REG_EXP.test(aux[j])){
87                                match_cont += aux[j];
88                        }else{
89                                match_cont += "";
90                        }
91                }
92                //lines = lines + "<tr><td id=td_DD_"+i+" onClick=\"javascript:hideTip();makeMailList('"+match_contacts[i]+"','"+fld_id+"');document.getElementById('" + fld_id + "').focus();\" onmouseover=\"selectContact("+i+")\">" + match_contacts[i] + "</td></tr>"
93                lines = lines + "<tr><td id=td_DD_"+i+" onMouseDown=\"javascript:hideTip();makeMailList('"+match_cont+"','"+fld_id+"');setTimeout('document.getElementById(\\'"+fld_id+"\\').focus()',300);\" onmouseover=\"selectContact("+i+")\">" + match_cont + "</td></tr>"
94                match_cont = "";
95        }
96
97        // treat especials keys
98        // key ENTER
99        if ((key_pressed == 13) && (document.getElementById('tipDiv').style.visibility))
100        {
101                //Bug, sometimes the actualSelectedContact do not exist.
102                try{
103                        makeMailList(document.getElementById('td_DD_' + actualSelectedContact).innerHTML,fld_id);
104                        hideTip();
105                }
106                catch(e){}
107                return;
108        }
109        // key lostfocus
110        if ((key_pressed == 'lostfocus') && (document.getElementById('tipDiv').style.visibility)){
111                hideTip();
112                return;
113        }
114        // key DOWN
115        if ((key_pressed == 40) && (document.getElementById('tipDiv').style.visibility)){
116                if (actualSelectedContact != (match_contacts.length - 1)){
117                        selectContact(actualSelectedContact + 1);
118                }
119                return;
120        }
121        // key UP
122        if ((key_pressed == 38) && (document.getElementById('tipDiv').style.visibility)){
123                if (actualSelectedContact != 0){                       
124                        selectContact(actualSelectedContact - 1);
125                        return;
126                }
127        }
128
129        if (lines != ''){
130                table_contacts = table_contacts_header + lines + table_contacts_foot
131                doTooltip(posX, posY, table_contacts)
132        }
133        else
134                hideTip();
135               
136        return true;
137}
138
139function makeMailList(mail,fld_id)
140{
141        list = Element(fld_id);
142        for (var i = list.value.length; ((i!=0) && (list.value.substring(i-1,i)!=',')); i--){}
143        mail = mail.replace(/&lt;/g,"<");
144        mail = mail.replace(/&gt;/g,">");
145        mail = mail.replace(/<[bB]>/g,"");
146        mail = mail.replace(/<\/[bB]>/g,"");
147        if (i == 0)
148                list.value = list.value.substring(0,i) + mail + ', ';
149        else
150                list.value = list.value.substring(0,i) + ' ' + mail + ', ';
151}
152
153function selectContact(newContact){     
154        //Desabilitar o atual
155        var elAtual = document.getElementById('td_DD_' + actualSelectedContact);
156        if(elAtual)
157                elAtual.bgColor = "#efefef";   
158               
159        //Habilitar o novo.             
160        var elNew = document.getElementById('td_DD_' + newContact).bgColor = "#c0e0ff";
161        if(elNew)
162                elNew.bgColor = "#c0e0ff";
163
164        actualSelectedContact = newContact;
165}
166
167function doTooltip(x,y,msg) {
168        if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
169        Tooltip.show(x,y,msg);
170}
171
172function hideTip() {
173        if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
174        actualSelectedContact=0;
175        Tooltip.hide();
176}
177
178function findPosX(obj)
179{
180        var curleft = 0;
181        if (obj.offsetParent)
182        {
183                while (obj.offsetParent)
184                {
185                        curleft += obj.offsetLeft
186                        obj = obj.offsetParent;
187                }
188        }
189        else if (obj.x)
190                curleft += obj.x;
191        return curleft;
192}
193
194function findPosY(obj)
195{
196        var curtop = 0;
197        if (obj.offsetParent)
198        {
199                while (obj.offsetParent)
200                {
201                        curtop += obj.offsetTop
202                        obj = obj.offsetParent;
203                }
204        }
205        else if (obj.y)
206                curtop += obj.y;
207        return curtop;
208}
209
210function trim(inputString) {
211   if (typeof inputString != "string")
212        return inputString;
213     
214   var retValue = inputString;
215   var ch = retValue.substring(0, 1);
216   while (ch == " ") {
217          retValue = retValue.substring(1, retValue.length);
218          ch = retValue.substring(0, 1);
219   }
220   ch = retValue.substring(retValue.length-1, retValue.length);
221   while (ch == " ") {
222          retValue = retValue.substring(0, retValue.length-1);
223          ch = retValue.substring(retValue.length-1, retValue.length);
224   }
225   while (retValue.indexOf("  ") != -1) {
226          retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
227   }
228   return retValue;
229}
230
231var Tooltip = {
232    overlaySelects: true,  // iframe shim for select lists (ie win)
233    offX: 0,
234    offY: 50,
235    tipID: "tipDiv",
236    showDelay: 0,
237    hideDelay: 0,
238   
239    ovTimer: 0, // for overlaySelects
240    ready:false, timer:null, tip:null, shim:null, supportsOverlay:false,
241 
242    init: function() {
243            var el_dropdowncontact = document.createElement("DIV");
244                        el_dropdowncontact.id = this.tipID;
245                        document.body.appendChild(el_dropdowncontact);
246            this.supportsOverlay = this.checkOverlaySupport();
247            this.ready = true;
248    },
249   
250    show: function(x, y, msg) {
251        this.tip = document.getElementById( this.tipID );
252        this.writeTip(msg);
253       
254        this.positionTipStatic(x,y);
255                this.handleOverlay(1, this.showDelay, x+this.offX,y+this.offY);
256                document.getElementById('td_DD_0').bgColor = "#c0e0ff";
257        this.timer = setTimeout("Tooltip.toggleVis('" + this.tipID + "', 'visible')", this.showDelay);
258    },
259   
260    writeTip: function(msg) {
261        if ( this.tip && typeof this.tip.innerHTML != "undefined" ) this.tip.innerHTML = msg;
262        this.tip.style.width = 'auto';
263    },
264   
265    positionTipStatic: function(x,y) {
266        if ( this.tip && this.tip.style ) {
267                        this.tip.style.left = x + this.offX + "px";
268                        this.tip.style.top = y + this.offY + "px";
269        }
270     },
271
272        scrollChanged: function() {
273                Element('tipDiv').style.visibility = 'hidden';
274                Tooltip.hide();
275        },
276
277    hide: function() {
278        if (this.timer) { clearTimeout(this.timer);     this.timer = 0; }
279                this.handleOverlay(0, this.hideDelay);
280        this.timer = setTimeout("Tooltip.toggleVis('" + this.tipID + "', 'hidden')", this.hideDelay);
281        this.tip = null;
282    },
283   
284  toggleVis: function(id, vis) { // to check for el_dropdowncontact, prevent (rare) error
285      var el_dropdowncontact = document.getElementById(id);
286      if (el_dropdowncontact) el_dropdowncontact.style.visibility = vis;
287  },
288
289        // check need for and support of iframe shim
290        checkOverlaySupport: function() {
291                return (is_ie);
292        },
293   
294        handleOverlay: function(bVis, d, x ,y) {
295                var _scrollY = div_message_scroll ? div_message_scroll.scrollTop : 0;
296        if(_scrollY > 0 && this.tip) {
297                this.tip.style.top = (findPosY(this.tip) - _scrollY)+ "px";
298            }
299                if ( this.overlaySelects && this.supportsOverlay ) {
300                        if (this.ovTimer) { clearTimeout(this.ovTimer); this.ovTimer = 0; }
301                        switch (bVis) {
302                        case 1 :
303                                if ( !document.getElementById('tipShim') )
304                                        document.body.insertAdjacentHTML("beforeEnd", '<iframe id="tipShim" src="about:blank" style="position:absolute; left:' + x + '; top:' + y + '; z-index:500; visibility:hidden" scrolling="no" frameborder="0"></iframe>');
305                                        this.shim = document.getElementById('tipShim');
306                                        if (this.shim && this.tip) {
307                                                this.shim.style.width = this.tip.offsetWidth + "px";
308                        this.shim.style.height = this.tip.offsetHeight + "px";
309                        this.shim.style.top = findPosY(this.tip)+ "px";
310                                        }
311                                        this.ovTimer = setTimeout("Tooltip.toggleVis('tipShim', 'visible')", d);
312                                break;
313                        case 0 :
314                                this.ovTimer = setTimeout("Tooltip.toggleVis('tipShim', 'hidden')", d);
315                                if (this.shim) this.shim = null;
316                                break;
317                        }
318                }
319        }
320}
321Tooltip.init();
322// Criar um estilo no html (tpl) com os seguintes parametros:
323//div#tipDiv {
324//  position:absolute; visibility:hidden; left:0; top:0; z-index:10000;
325//  background-color:#EFEFEF; border:1px solid #337;
326//  width:220px; padding:3px;
327//  color:#000; font-size:11px; line-height:1.2;
328//  cursor: default;
Note: See TracBrowser for help on using the repository browser.