source: trunk/expressoMail1_2/js/DropDownContacts.js @ 486

Revision 486, 9.9 KB checked in by niltonneto, 16 years ago (diff)

Retirada condicional que monta contato somente com email, quando o nome vem em branco.
O mesmo funciona somente no formato "" <email@…>

  • 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;
13
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("\\b"+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                                tmp[1] = tmp[1].replace(tmp_mail, tmp_mail.bold());
60                                tmp[0] = tmp[0].replace(tmp_mail, tmp_mail.bold());
61                                match_contacts[match_index] = '&quot;' + tmp[0] + '&quot; &lt;' + tmp[1] + '&gt;';
62                                match_index++;
63                        }
64                }
65        }
66
67        if (match_contacts.length == 0){
68                hideTip();
69                return;
70        }
71
72        var table_contacts_header = "<table style='font-family:arial;font-size:12;color:#0000CF' border=0 cellpadding=0 cellspacing=0>";
73        var table_contacts_foot = "</table>";
74        var lines = '';
75        var REG_EXP = /^[^\#|^\$|^\%|^\!|^\?|^\"|^\']+$/;
76        var match_cont = "";
77        for (var i=0; i<match_contacts.length; i++)
78        {
79                var aux = match_contacts[i].split("");
80                for(var j in aux){
81                        if(REG_EXP.test(aux[j])){
82                                match_cont += aux[j];
83                        }else{
84                                match_cont += "";
85                        }
86                }
87                //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>"
88                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>"
89                match_cont = "";
90        }
91
92        // treat especials keys
93        // key ENTER
94        if ((key_pressed == 13) && (document.getElementById('tipDiv').style.visibility))
95        {
96                //Bug, sometimes the actualSelectedContact do not exist.
97                try{
98                        makeMailList(document.getElementById('td_DD_' + actualSelectedContact).innerHTML,fld_id);
99                        hideTip();
100                }
101                catch(e){}
102                return;
103        }
104        // key lostfocus
105        if ((key_pressed == 'lostfocus') && (document.getElementById('tipDiv').style.visibility)){
106                hideTip();
107                return;
108        }
109        // key DOWN
110        if ((key_pressed == 40) && (document.getElementById('tipDiv').style.visibility)){
111                if (actualSelectedContact != (match_contacts.length - 1)){
112                        selectContact(actualSelectedContact + 1);
113                }
114                return;
115        }
116        // key UP
117        if ((key_pressed == 38) && (document.getElementById('tipDiv').style.visibility)){
118                if (actualSelectedContact != 0){                       
119                        selectContact(actualSelectedContact - 1);
120                        return;
121                }
122        }
123
124        if (lines != ''){
125                table_contacts = table_contacts_header + lines + table_contacts_foot
126                doTooltip(posX, posY, table_contacts)
127        }
128        else
129                hideTip();
130               
131        return true;
132}
133
134function makeMailList(mail,fld_id)
135{
136        list = Element(fld_id);
137        for (var i = list.value.length; ((i!=0) && (list.value.substring(i-1,i)!=',')); i--){}
138        mail = mail.replace(/&lt;/g,"<");
139        mail = mail.replace(/&gt;/g,">");
140        mail = mail.replace(/<[bB]>/g,"");
141        mail = mail.replace(/<\/[bB]>/g,"");
142        if (i == 0)
143                list.value = list.value.substring(0,i) + mail + ', ';
144        else
145                list.value = list.value.substring(0,i) + ' ' + mail + ', ';
146}
147
148function selectContact(newContact){     
149        //Desabilitar o atual
150        var elAtual = document.getElementById('td_DD_' + actualSelectedContact);
151        if(elAtual)
152                elAtual.bgColor = "#efefef";   
153               
154        //Habilitar o novo.             
155        var elNew = document.getElementById('td_DD_' + newContact).bgColor = "#c0e0ff";
156        if(elNew)
157                elNew.bgColor = "#c0e0ff";
158
159        actualSelectedContact = newContact;
160}
161
162function doTooltip(x,y,msg) {
163        if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
164        Tooltip.show(x,y,msg);
165}
166
167function hideTip() {
168        if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
169        actualSelectedContact=0;
170        Tooltip.hide();
171}
172
173function findPosX(obj)
174{
175        var curleft = 0;
176        if (obj.offsetParent)
177        {
178                while (obj.offsetParent)
179                {
180                        curleft += obj.offsetLeft
181                        obj = obj.offsetParent;
182                }
183        }
184        else if (obj.x)
185                curleft += obj.x;
186        return curleft;
187}
188
189function findPosY(obj)
190{
191        var curtop = 0;
192        if (obj.offsetParent)
193        {
194                while (obj.offsetParent)
195                {
196                        curtop += obj.offsetTop
197                        obj = obj.offsetParent;
198                }
199        }
200        else if (obj.y)
201                curtop += obj.y;
202        return curtop;
203}
204
205function trim(inputString) {
206   if (typeof inputString != "string")
207        return inputString;
208     
209   var retValue = inputString;
210   var ch = retValue.substring(0, 1);
211   while (ch == " ") {
212          retValue = retValue.substring(1, retValue.length);
213          ch = retValue.substring(0, 1);
214   }
215   ch = retValue.substring(retValue.length-1, retValue.length);
216   while (ch == " ") {
217          retValue = retValue.substring(0, retValue.length-1);
218          ch = retValue.substring(retValue.length-1, retValue.length);
219   }
220   while (retValue.indexOf("  ") != -1) {
221          retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
222   }
223   return retValue;
224}
225
226var Tooltip = {
227    overlaySelects: true,  // iframe shim for select lists (ie win)
228    offX: 0,
229    offY: 50,
230    tipID: "tipDiv",
231    showDelay: 0,
232    hideDelay: 0,
233   
234    ovTimer: 0, // for overlaySelects
235    ready:false, timer:null, tip:null, shim:null, supportsOverlay:false,
236 
237    init: function() {
238            var el_dropdowncontact = document.createElement("DIV");
239                        el_dropdowncontact.id = this.tipID;
240                        document.body.appendChild(el_dropdowncontact);
241            this.supportsOverlay = this.checkOverlaySupport();
242            this.ready = true;
243    },
244   
245    show: function(x, y, msg) {
246        this.tip = document.getElementById( this.tipID );
247        this.writeTip(msg);
248       
249        this.positionTipStatic(x,y);
250                this.handleOverlay(1, this.showDelay, x+this.offX,y+this.offY);
251                document.getElementById('td_DD_0').bgColor = "#c0e0ff";
252        this.timer = setTimeout("Tooltip.toggleVis('" + this.tipID + "', 'visible')", this.showDelay);
253    },
254   
255    writeTip: function(msg) {
256        if ( this.tip && typeof this.tip.innerHTML != "undefined" ) this.tip.innerHTML = msg;
257        this.tip.style.width = 'auto';
258    },
259   
260    positionTipStatic: function(x,y) {
261        if ( this.tip && this.tip.style ) {
262                        this.tip.style.left = x + this.offX + "px";
263                        this.tip.style.top = y + this.offY + "px";
264        }
265     },
266
267        scrollChanged: function() {
268                Element('tipDiv').style.visibility = 'hidden';
269                Tooltip.hide();
270        },
271
272    hide: function() {
273        if (this.timer) { clearTimeout(this.timer);     this.timer = 0; }
274                this.handleOverlay(0, this.hideDelay);
275        this.timer = setTimeout("Tooltip.toggleVis('" + this.tipID + "', 'hidden')", this.hideDelay);
276        this.tip = null;
277    },
278   
279  toggleVis: function(id, vis) { // to check for el_dropdowncontact, prevent (rare) error
280      var el_dropdowncontact = document.getElementById(id);
281      if (el_dropdowncontact) el_dropdowncontact.style.visibility = vis;
282  },
283
284        // check need for and support of iframe shim
285        checkOverlaySupport: function() {
286                return (is_ie);
287        },
288   
289        handleOverlay: function(bVis, d, x ,y) {
290                var _scrollY = div_message_scroll ? div_message_scroll.scrollTop : 0;
291        if(_scrollY > 0 && this.tip) {
292                this.tip.style.top = (findPosY(this.tip) - _scrollY)+ "px";
293            }
294                if ( this.overlaySelects && this.supportsOverlay ) {
295                        if (this.ovTimer) { clearTimeout(this.ovTimer); this.ovTimer = 0; }
296                        switch (bVis) {
297                        case 1 :
298                                if ( !document.getElementById('tipShim') )
299                                        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>');
300                                        this.shim = document.getElementById('tipShim');
301                                        if (this.shim && this.tip) {
302                                                this.shim.style.width = this.tip.offsetWidth + "px";
303                        this.shim.style.height = this.tip.offsetHeight + "px";
304                        this.shim.style.top = findPosY(this.tip)+ "px";
305                                        }
306                                        this.ovTimer = setTimeout("Tooltip.toggleVis('tipShim', 'visible')", d);
307                                break;
308                        case 0 :
309                                this.ovTimer = setTimeout("Tooltip.toggleVis('tipShim', 'hidden')", d);
310                                if (this.shim) this.shim = null;
311                                break;
312                        }
313                }
314        }
315}
316Tooltip.init();
317// Criar um estilo no html (tpl) com os seguintes parametros:
318//div#tipDiv {
319//  position:absolute; visibility:hidden; left:0; top:0; z-index:10000;
320//  background-color:#EFEFEF; border:1px solid #337;
321//  width:220px; padding:3px;
322//  color:#000; font-size:11px; line-height:1.2;
323//  cursor: default;
Note: See TracBrowser for help on using the repository browser.