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

Revision 2616, 9.9 KB checked in by amuller, 14 years ago (diff)

Ticket #911 - Aproveitamento de funções comuns do expressoMail

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