source: trunk/expressoMail1_2/js/jscode/DropDownContacts.js @ 2602

Revision 2602, 10.0 KB checked in by amuller, 14 years ago (diff)

Ticket #1036 - Arrumando código com metodos dom ao inves do innerHTML

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