!import
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is mozilla.org code.
15 *
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1998
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 * Seth Spitzer <sspitzer@netscape.com>
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38 top.MAX_RECIPIENTS = 1;
39 var inputElementType = "";
40
41 var gListCard;
42 var gEditList;
43 var gOkCallback = null;
44 var oldListName = "";
45 var rdf = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService);
46 var gPromptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
47 var gHeaderParser = Components.classes["@mozilla.org/messenger/headerparser;1"].getService(Components.interfaces.nsIMsgHeaderParser);
48
49 try
50 {
51 var gDragService = Components.classes["@mozilla.org/widget/dragservice;1"]
52 .getService(Components.interfaces.nsIDragService);
53 }
54 catch (e)
55 {
56 }
57
handleKeyPress
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
58 function handleKeyPress(element, event)
59 {
60 // allow dialog to close on enter if focused textbox has no value
61 if (element.value != "" && event.keyCode == 13) {
62 event.stopPropagation();
63 event.preventDefault();
64 }
65 }
66
mailingListExists
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
67 function mailingListExists(listname)
68 {
69 var addressbook = Components.classes["@mozilla.org/abmanager;1"]
70 .getService(Components.interfaces.nsIAbManager);
71
72 if (addressbook.mailListNameExists(listname))
73 {
74 gPromptService.alert(window,
75 gAddressBookBundle.getString("mailListNameExistsTitle"),
76 gAddressBookBundle.getString("mailListNameExistsMessage"));
77 return true;
78 }
79 return false;
80 }
81
GetListValue
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
82 function GetListValue(mailList, doAdd)
83 {
84 var listname = document.getElementById('ListName').value.replace(/^\s+|\s+$/g, '');
85
86 if (listname.length == 0)
87 {
88 var alertText = gAddressBookBundle.getString("emptyListName");
89 alert(alertText);
90 return false;
91 }
92 else
93 {
94 var canonicalNewListName = listname.toLowerCase();
95 var canonicalOldListName = oldListName.toLowerCase();
96 if (doAdd)
97 {
98 if (mailingListExists(canonicalNewListName))
99 return false;
100 }
101 else if (canonicalOldListName != canonicalNewListName)
102 {
103 if (mailingListExists(canonicalNewListName))
104 return false;
105 }
106 }
107
108 mailList.isMailList = true;
109 mailList.dirName = listname;
110 mailList.listNickName = document.getElementById('ListNickName').value;
111 mailList.description = document.getElementById('ListDescription').value;
112
113 var oldTotal = mailList.addressLists.Count();
114 var i = 1;
115 var pos = 0;
116 var inputField, fieldValue, cardproperty;
117 while ((inputField = awGetInputElement(i)))
118 {
119
120 fieldValue = inputField.value;
121
122 if (doAdd || (!doAdd && pos >= oldTotal))
123 cardproperty = Components.classes["@mozilla.org/addressbook/cardproperty;1"].createInstance();
124 else
125 cardproperty = mailList.addressLists.GetElementAt(pos);
126
127 if (fieldValue == "")
128 {
129 if (!doAdd && cardproperty)
130 try
131 {
132 mailList.addressLists.DeleteElementAt(pos);
133 }
134 catch(ex)
135 {
136 // Ignore attempting to remove an item
137 // at a position greater than the number
138 // of elements in the addressLists attribute
139 }
140 }
141 else if (cardproperty)
142 {
143 cardproperty = cardproperty.QueryInterface(Components.interfaces.nsIAbCard);
144 if (cardproperty)
145 {
146 var addresses = {};
147 var names = {};
148 var fullNames = {};
149 var numAddresses = gHeaderParser.parseHeadersWithArray(fieldValue, addresses, names, fullNames);
150 for (var j = 0; j < numAddresses; j++)
151 {
152 if (j > 0)
153 {
154 cardproperty = Components.classes["@mozilla.org/addressbook/cardproperty;1"].createInstance();
155 cardproperty = cardproperty.QueryInterface(Components.interfaces.nsIAbCard);
156 }
157 cardproperty.primaryEmail = addresses.value[j];
158 cardproperty.displayName = names.value[j];
159
160 if (doAdd || (doAdd == false && pos >= oldTotal))
161 mailList.addressLists.AppendElement(cardproperty);
162 }
163 pos++;
164 }
165 }
166 i++;
167 }
168
169 --i;
170
171 if (doAdd == false && i < oldTotal)
172 {
173 for (var j = i; j < oldTotal; j++)
174 mailList.addressLists.DeleteElementAt(j);
175 }
176 return true;
177 }
178
MailListOKButton
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
179 function MailListOKButton()
180 {
181 var popup = document.getElementById('abPopup');
182 if ( popup )
183 {
184 var uri = popup.getAttribute('value');
185
186 // FIX ME - hack to avoid crashing if no ab selected because of blank option bug from template
187 // should be able to just remove this if we are not seeing blank lines in the ab popup
188 if ( !uri )
189 return false; // don't close window
190 // -----
191
192 //Add mailing list to database
193 var mailList = Components.classes["@mozilla.org/addressbook/directoryproperty;1"].createInstance();
194 mailList = mailList.QueryInterface(Components.interfaces.nsIAbDirectory);
195
196 if (GetListValue(mailList, true)) {
197 var parentDirectory = GetDirectoryFromURI(uri);
198 parentDirectory.addMailList(mailList);
199 }
200 else
201 return false;
202 }
203
204 return true; // close the window
205 }
206
OnLoadNewMailList
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
207 function OnLoadNewMailList()
208 {
209 var selectedAB = null;
210
211 InitCommonJS();
212
213 if (window.arguments && window.arguments[0])
214 {
215 var abURI = window.arguments[0].selectedAB;
216 if (abURI) {
217 var directory = GetDirectoryFromURI(abURI);
218 if (directory.isMailList) {
219 var parentURI = GetParentDirectoryFromMailingListURI(abURI);
220 if (parentURI) {
221 selectedAB = parentURI;
222 }
223 }
224 else if (!(directory.operations & directory.opWrite)) {
225 selectedAB = kPersonalAddressbookURI;
226
227 }
228 else {
229 selectedAB = abURI;
230 }
231 }
232 }
233
234 if (!selectedAB)
235 selectedAB = kPersonalAddressbookURI;
236
237 // set popup with address book names
238 var abPopup = document.getElementById('abPopup');
239 abPopup.value = selectedAB;
240
241 AppendNewRowAndSetFocus();
242 awFitDummyRows(1);
243
244 document.addEventListener("keypress", awDocumentKeyPress, true);
245
246 // focus on first name
247 var listName = document.getElementById('ListName');
248 if ( listName )
anon:249:16
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
249 setTimeout( function(firstTextBox) { firstTextBox.focus(); }, 0, listName );
250
251 moveToAlertPosition();
252 }
253
EditListOKButton
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
254 function EditListOKButton()
255 {
256 //edit mailing list in database
257 if (GetListValue(gEditList, false))
258 {
259 if (gListCard) {
260 // modify the list card (for the results pane) from the mailing list
261 gListCard.displayName = gEditList.dirName;
262 gListCard.lastName = gEditList.dirName;
263 gListCard.nickName = gEditList.listNickName;
264 gListCard.notes = gEditList.description;
265 }
266
267 gEditList.editMailListToDatabase(gListCard);
268
269 if (gOkCallback)
270 gOkCallback();
271 return true; // close the window
272 }
273 else
274 return false;
275 }
276
OnLoadEditList
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
277 function OnLoadEditList()
278 {
279 InitCommonJS();
280
281 gListCard = window.arguments[0].abCard;
282 var listUri = window.arguments[0].listURI;
283 gOkCallback = window.arguments[0].okCallback;
284
285 gEditList = GetDirectoryFromURI(listUri);
286
287 document.getElementById('ListName').value = gEditList.dirName;
288 document.getElementById('ListNickName').value = gEditList.listNickName;
289 document.getElementById('ListDescription').value = gEditList.description;
290 oldListName = gEditList.dirName;
291
292 if (gEditList.addressLists)
293 {
294 var total = gEditList.addressLists.Count();
295 if (total)
296 {
297 var listbox = document.getElementById('addressingWidget');
298 var newListBoxNode = listbox.cloneNode(false);
299 var templateNode = listbox.getElementsByTagName("listitem")[0];
300
301 top.MAX_RECIPIENTS = 0;
302 for ( var i = 0; i < total; i++ )
303 {
304 var card = gEditList.addressLists.GetElementAt(i);
305 card = card.QueryInterface(Components.interfaces.nsIAbCard);
306 var address = gHeaderParser.makeFullAddressWString(card.displayName, card.primaryEmail);
307 SetInputValue(address, newListBoxNode, templateNode);
308 }
309 var parent = listbox.parentNode;
310 parent.replaceChild(newListBoxNode, listbox);
311 }
312 }
313
314 document.addEventListener("keypress", awDocumentKeyPress, true);
315
316 moveToAlertPosition();
317
318 // workaround for bug 118337 - for mailing lists that have more rows than fits inside
319 // the display, the value of the textbox inside the new row isn't inherited into the input -
320 // the first row then appears to be duplicated at the end although it is actually empty.
321 // see awAppendNewRow which copies first row and clears it
322 setTimeout(AppendLastRow, 0);
323 }
324
AppendLastRow
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
325 function AppendLastRow()
326 {
327 AppendNewRowAndSetFocus();
328 awFitDummyRows(1);
329
330 // focus on first name
331 var listName = document.getElementById('ListName');
332 if ( listName )
333 listName.focus();
334 }
335
AppendNewRowAndSetFocus
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
336 function AppendNewRowAndSetFocus()
337 {
338 var lastInput = awGetInputElement(top.MAX_RECIPIENTS);
339 if ( lastInput && lastInput.value )
340 awAppendNewRow(true);
341 else
342 awSetFocus(top.MAX_RECIPIENTS, lastInput);
343 }
344
SetInputValue
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
345 function SetInputValue(inputValue, parentNode, templateNode)
346 {
347 top.MAX_RECIPIENTS++;
348
349 var newNode = templateNode.cloneNode(true);
350 parentNode.appendChild(newNode); // we need to insert the new node before we set the value of the select element!
351
352 var input = newNode.getElementsByTagName(awInputElementName());
353 if ( input && input.length == 1 )
354 {
355 //We need to set the value using both setAttribute and .value else we will
356 // lose the content when the field is not visible. See bug 37435
357 input[0].setAttribute("value", inputValue);
358 input[0].value = inputValue;
359 input[0].setAttribute("id", "addressCol1#" + top.MAX_RECIPIENTS);
360 }
361 }
362
awNotAnEmptyArea
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
363 function awNotAnEmptyArea(event)
364 {
365 //This is temporary until i figure out how to ensure to always having an empty space after the last row
366
367 var lastInput = awGetInputElement(top.MAX_RECIPIENTS);
368 if ( lastInput && lastInput.value )
369 awAppendNewRow(false);
370
371 event.stopPropagation();
372 }
373
awClickEmptySpace
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
374 function awClickEmptySpace(target, setFocus)
375 {
376 if (target == null ||
377 (target.localName != "listboxbody" &&
378 target.localName != "listcell" &&
379 target.localName != "listitem"))
380 return;
381
382 var lastInput = awGetInputElement(top.MAX_RECIPIENTS);
383
384 if ( lastInput && lastInput.value )
385 awAppendNewRow(setFocus);
386 else
387 if (setFocus)
388 awSetFocus(top.MAX_RECIPIENTS, lastInput);
389 }
390
awReturnHit
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
391 function awReturnHit(inputElement)
392 {
393 var row = awGetRowByInputElement(inputElement);
394 if ( inputElement.value )
395 {
396 var nextInput = awGetInputElement(row+1);
397 if ( !nextInput )
398 awAppendNewRow(true);
399 else
400 awSetFocus(row+1, nextInput);
401 }
402 }
403
awDeleteRow
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
404 function awDeleteRow(rowToDelete)
405 {
406 /* When we delete a row, we must reset the id of others row in order to not break the sequence */
407 var maxRecipients = top.MAX_RECIPIENTS;
408 awRemoveRow(rowToDelete);
409
410 var numberOfCols = awGetNumberOfCols();
411 for (var row = rowToDelete + 1; row <= maxRecipients; row ++)
412 for (var col = 1; col <= numberOfCols; col++)
413 awGetElementByCol(row, col).setAttribute("id", "addressCol" + (col) + "#" + (row-1));
414
415 awTestRowSequence();
416 }
417
awInputChanged
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
418 function awInputChanged(inputElement)
419 {
420 // AutoCompleteAddress(inputElement);
421
422 //Do we need to add a new row?
423 var lastInput = awGetInputElement(top.MAX_RECIPIENTS);
424 if ( lastInput && lastInput.value && !top.doNotCreateANewRow)
425 awAppendNewRow(false);
426 top.doNotCreateANewRow = false;
427 }
428
awInputElementName
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
429 function awInputElementName()
430 {
431 if (inputElementType == "")
432 inputElementType = document.getElementById("addressCol1#1").localName;
433 return inputElementType;
434 }
435
awAppendNewRow
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
436 function awAppendNewRow(setFocus)
437 {
438 var body = document.getElementById("addressingWidget");
439 var listitem1 = awGetListItem(1);
440
441 if ( body && listitem1 )
442 {
443 var nextDummy = awGetNextDummyRow();
444 var newNode = listitem1.cloneNode(true);
445 if (nextDummy)
446 body.replaceChild(newNode, nextDummy);
447 else
448 body.appendChild(newNode);
449
450 top.MAX_RECIPIENTS++;
451
452 var input = newNode.getElementsByTagName(awInputElementName());
453 if ( input && input.length == 1 )
454 {
455 input[0].setAttribute("value", "");
456 input[0].setAttribute("id", "addressCol1#" + top.MAX_RECIPIENTS);
457
458 //this copies the autocomplete sessions list from recipient#1
459 input[0].syncSessions(document.getElementById('addressCol1#1'));
460
461 if (input[0].getAttribute('focused') != '')
462 input[0].removeAttribute('focused');
463 }
464 // focus on new input widget
465 if (setFocus && input )
466 awSetFocus(top.MAX_RECIPIENTS, input[0]);
467 }
468 }
469
470
471 // functions for accessing the elements in the addressing widget
472
awGetInputElement
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
473 function awGetInputElement(row)
474 {
475 return document.getElementById("addressCol1#" + row);
476 }
477
478
_awSetFocus
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
479 function _awSetFocus()
480 {
481 var listbox = document.getElementById('addressingWidget');
482 try
483 {
484 var theNewRow = awGetListItem(top.awRow);
485
486 listbox.ensureElementIsVisible(theNewRow);
487 top.awInputElement.focus();
488 }
489 catch(ex)
490 {
491 top.awFocusRetry ++;
492 if (top.awFocusRetry < 8)
493 {
494 dump("_awSetFocus failed, try it again...\n");
495 setTimeout("_awSetFocus();", 0);
496 }
497 else
498 dump("_awSetFocus failed, forget about it!\n");
499 }
500 }
501
awTabFromRecipient
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
502 function awTabFromRecipient(element, event)
503 {
504 //If we are the last element in the listbox, we don't want to create a new row.
505 if (element == awGetInputElement(top.MAX_RECIPIENTS))
506 top.doNotCreateANewRow = true;
507 }
508
DragOverAddressListTree
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
509 function DragOverAddressListTree(event)
510 {
511 var validFlavor = false;
512 var dragSession = gDragService.getCurrentSession();
513
514 // XXX add support for other flavors here
515 if (dragSession.isDataFlavorSupported("text/x-moz-address")) {
516 dragSession.canDrop = true;
517 }
518 }
519
DropOnAddressListTree
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
520 function DropOnAddressListTree(event)
521 {
522 var dragSession = gDragService.getCurrentSession();
523 var trans;
524
525 try {
526 trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
527 trans.addDataFlavor("text/x-moz-address");
528 }
529 catch (ex) {
530 return;
531 }
532
533 for ( var i = 0; i < dragSession.numDropItems; ++i )
534 {
535 dragSession.getData ( trans, i );
536 var dataObj = new Object();
537 var bestFlavor = new Object();
538 var len = new Object();
539 trans.getAnyTransferData ( bestFlavor, dataObj, len );
540 if ( dataObj ) dataObj = dataObj.value.QueryInterface(Components.interfaces.nsISupportsString);
541 if ( !dataObj ) continue;
542
543 // pull the URL out of the data object
544 var address = dataObj.data.substring(0, len.value);
545 if (!address)
546 continue;
547
548 DropListAddress(event.target, address);
549 }
550 }
551
DropListAddress
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
552 function DropListAddress(target, address)
553 {
554 awClickEmptySpace(target, true); //that will automatically set the focus on a new available row, and make sure is visible
555 if (top.MAX_RECIPIENTS == 0)
556 top.MAX_RECIPIENTS = 1;
557 var lastInput = awGetInputElement(top.MAX_RECIPIENTS);
558 lastInput.value = address;
559 awAppendNewRow(true);
560 }