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

Revision 2, 9.7 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

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