]> err.no Git - scalable-opengroupware.org/blob - UI/WebServerResources/UIxMailToSelection.js
initial sync
[scalable-opengroupware.org] / UI / WebServerResources / UIxMailToSelection.js
1 /*
2  Copyright (C) 2005 SKYRIX Software AG
3  
4  This file is part of OpenGroupware.org.
5  
6  OGo is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License as published by the
8  Free Software Foundation; either version 2, or (at your option) any
9  later version.
10  
11  OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12  WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14  License for more details.
15  
16  You should have received a copy of the GNU Lesser General Public
17  License along with OGo; see the file COPYING.  If not, write to the
18  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19  02111-1307, USA.
20 */
21
22 /* Dependencies:
23  * It's required that "currentIndex" is defined in a top level context.
24  *
25  * Exports:
26  * defines UIxRecipientSelectorHasRecipients() returning a bool for the
27  * surrounding context to check.
28  */
29
30 var lastIndex = currentIndex;
31
32 function sanitizedCn(cn) {
33   var parts;
34   parts = cn.split(', ');
35   if(parts.length == 1)
36     return cn;
37   return parts[0];
38 }
39
40 function hasAddress(email) {
41   var e = $(email);
42   if(e)
43     return true;
44   return false;
45 }
46
47 function rememberAddress(email) {
48   var list, span, idx;
49   
50   list    = $('addr_addresses');
51   span    = document.createElement('span');
52   span.id = email;
53   idx     = document.createTextNode(currentIndex);
54   span.appendChild(idx);
55   list.appendChild(span);
56 }
57
58 function checkAddresses() {
59   alert("addressCount: " + this.getAddressCount() + " currentIndex: " + currentIndex + " lastIndex: " + lastIndex);
60 }
61
62 function addAddress(type, email, uid, sn, cn, dn) {
63   var shouldAddRow, s, e;
64   
65   shouldAddRow = true;
66   if (cn)
67     s = this.sanitizedCn(cn) + ' <' + email + '>';
68   else
69     s = email;
70
71   if(this.hasAddress(email))
72     return;
73   
74   e = $('addr_0');
75   if(e.value == '') {
76     e.value = s;
77     shouldAddRow = false;
78   }
79   if(shouldAddRow) {
80     this.fancyAddRow(false, s);
81   }
82   this.rememberAddress(email);
83 }
84
85 function fancyAddRow(shouldEdit, text) {
86   var addr, addressList, lastChild, proto, row, select, input;
87   
88   addr = $('addr_' + lastIndex);
89   if (addr && addr.value == '') {
90     input = $('subjectField');
91     if (input && input.value != '') {
92       input.focus();
93       input.select();
94       return;
95     }
96   }
97   addressList = $("addressList");
98   lastChild = $("lastRow");
99   
100   currentIndex++;
101
102   proto = $('row_' + lastIndex);
103   row = proto.cloneNode(true);
104   row.id = 'row_' + currentIndex;
105
106   // select popup
107   var rowNodes = row.childNodesWithTag("span");
108   select = rowNodes[0].childNodesWithTag("select")[0];
109   select.name = 'popup_' + currentIndex;
110 //   select.value = row.childNodesWithTag("span")[0].childNodesWithTag("select")[0].value;
111   input = rowNodes[1].childNodesWithTag("input")[0];
112   input.name  = 'addr_' + currentIndex;
113   input.id = 'addr_' + currentIndex;
114   input.value = text;
115
116   addressList.insertBefore(row, lastChild);
117
118   if (shouldEdit) {
119     input.setAttribute('autocomplete', 'off');
120     input.focus();
121     input.select();
122     input.setAttribute('autocomplete', 'on');
123   }
124 //   this.adjustInlineAttachmentListHeight(this);
125 }
126
127 function addressFieldGotFocus(sender) {
128   var idx;
129   
130   idx = this.getIndexFromIdentifier(sender.id);
131   if ((lastIndex == idx) || (idx == 0)) return;
132   this.removeLastEditedRowIfEmpty();
133
134   return false;
135 }
136
137 function addressFieldLostFocus(sender) {
138   lastIndex = this.getIndexFromIdentifier(sender.id);
139
140   return false;
141 }
142
143 function removeLastEditedRowIfEmpty() {
144   var idx, addr, addressList, senderRow;
145   
146   idx = lastIndex;
147   if (idx == 0) return;
148   addr = $('addr_' + idx);
149   if (!addr) return;
150   if (addr.value != '') return;
151   addr = this.findAddressWithIndex(idx);
152   if(addr) {
153     var addresses = $('addr_addresses');
154     addresses.removeChild(addr);
155   }
156   addressList = $("addressList");
157   senderRow = $("row_" + idx);
158   addressList.removeChild(senderRow);
159   this.adjustInlineAttachmentListHeight(this);
160 }
161
162 function findAddressWithIndex(idx) {
163   var list, i, count, addr, idx
164   list = $('addr_addresses').childNodes;
165   count = list.length;
166   for(i = 0; i < count; i++) {
167     addr = list[i];
168     if(addr.innerHTML == idx)
169       return addr;
170   }
171   return null;
172 }
173
174 function getIndexFromIdentifier(id) {
175   return id.split('_')[1];
176 }
177
178 function getAddressIDs() {
179   var addressList, rows, i, count, addressIDs;
180
181   addressIDs = new Array();
182
183   addressList = $("addressList");
184   rows  = addressList.childNodes;
185   count = rows.length;
186
187   for (i = 0; i < count; i++) {
188     var row, rowId;
189     
190     row = addressList.childNodes[i];
191     rowId = row.id;
192     if (rowId && rowId != 'row_last') {
193       var idx;
194
195       idx = this.getIndexFromIdentifier(rowId);
196       addressIDs.push(idx);
197     }
198   }
199   return addressIDs;
200 }
201
202 function getAddressCount() {
203   var addressCount, addressIDs, i, count;
204   
205   addressCount = 0;
206   addressIDs   = this.getAddressIDs();
207   count        = addressIDs.length;
208   for (i = 0; i < count; i++) {
209     var idx, input;
210
211     idx   = addressIDs[i];
212     input = $('addr_' + idx);
213     if (input && input.value != '')
214       addressCount++;
215   }
216   return addressCount;
217 }
218
219 function UIxRecipientSelectorHasRecipients() {
220   var count;
221   
222   count = this.getAddressCount();
223   if (count > 0)
224     return true;
225   return false;
226 }
227
228 function adjustInlineAttachmentListHeight(sender) {
229   var e;
230   
231   e = $('attachmentsArea');
232   if (e.style.display != 'none') {
233     /* need to lower left size first, because left auto-adjusts to right! */
234     xHeight('compose_attachments_list', 10);
235
236     var leftHeight, rightHeaderHeight;
237     leftHeight        = xHeight('compose_leftside');
238     rightHeaderHeight = xHeight('compose_attachments_header');
239     xHeight('compose_attachments_list',
240             (leftHeight - rightHeaderHeight) - 16);
241   }
242 }
243
244 /* addressbook helpers */
245