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

Revision 804, 10.0 KB checked in by niltonneto, 15 years ago (diff)

Ticket #341 - Ao digitar um nome no Para/CC/CCo em nova mensagem, parte do nome no autocompletar não fica em negrito.

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