home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / mozil06.zip / bin / chrome / messenger.jar / content / messenger / FilterEditor.js < prev    next >
Text File  |  2001-02-14  |  7KB  |  223 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is mozilla.org code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  * Alec Flett <alecf@netscape.com>
  22.  */
  23.  
  24. // the actual filter that we're editing
  25. var gFilter;
  26.  
  27. // cache the key elements we need
  28. var gFilterNameElement;
  29. var gActionElement;
  30. var gActionTargetElement;
  31. var gActionValueDeck;
  32. var gActionPriority;
  33. var Bundle;
  34.  
  35. var nsIMsgSearchValidityManager = Components.interfaces.nsIMsgSearchValidityManager;
  36.  
  37. var nsMsgFilterAction = Components.interfaces.nsMsgFilterAction;
  38.  
  39. function filterEditorOnLoad()
  40. {
  41.     initializeSearchWidgets();
  42.     initializeFilterWidgets();
  43.  
  44.     Bundle = srGetStrBundle("chrome://messenger/locale/filter.properties");
  45.     
  46.     if (window.arguments && window.arguments[0]) {
  47.         var args = window.arguments[0];
  48.         if (args.filter) {
  49.             gFilter = window.arguments[0].filter;
  50.  
  51.             initializeDialog(gFilter);
  52.         } else {
  53.             gFilterList = args.filterList;
  54.             if (gFilterList)
  55.                 setSearchScope(getScopeFromFilterList(gFilterList));
  56.             // fake the first more button press
  57.             onMore(null);
  58.         }
  59.     }
  60.  
  61.     gFilterNameElement.focus();
  62.     doSetOKCancel(onOk, null);
  63.     moveToAlertPosition();
  64. }
  65.  
  66. function onOk()
  67. {
  68.     if (!saveFilter()) return;
  69.  
  70.     // parent should refresh filter list..
  71.     // this should REALLY only happen when some criteria changes that
  72.     // are displayed in the filter dialog, like the filter name
  73.     window.arguments[0].refresh = true;
  74.     window.close();
  75. }
  76.  
  77. function getScopeFromFilterList(filterList)
  78. {
  79.     if (!filterList) return false;
  80.     var type = filterList.folder.server.type;
  81.     if (type == "nntp") return nsIMsgSearchValidityManager.news;
  82.     if (type == "pop3") return nsIMsgSearchValidityManager.offlineMail;
  83.     return nsIMsgSearchValidityManager.onlineMailFilter;
  84. }
  85.  
  86. function getScope(filter) {
  87.     return getScopeFromFilterList(filter.filterList);
  88. }
  89.  
  90. function initializeFilterWidgets()
  91. {
  92.     gFilterNameElement = document.getElementById("filterName");
  93.     gActionElement = document.getElementById("actionMenu");
  94.     gActionTargetElement = document.getElementById("actionTargetFolder");
  95.     gActionValueDeck = document.getElementById("actionValueDeck");
  96.     gActionPriority = document.getElementById("actionValuePriority");
  97. }
  98.  
  99. function initializeDialog(filter)
  100. {
  101.         var selectedPriority;
  102.     gFilterNameElement.value = filter.filterName;
  103.  
  104.     gActionElement.selectedItem=gActionElement.getElementsByAttribute("data", filter.action)[0];
  105.     showActionElementFor(gActionElement.selectedItem);
  106.  
  107.     if (filter.action == nsMsgFilterAction.MoveToFolder) {
  108.         dump("pre-selecting target folder: " + filter.actionTargetFolderUri + "\n");
  109.         // there are multiple sub-items that have given attribute
  110.         var targets = gActionTargetElement.getElementsByAttribute("data", filter.actionTargetFolderUri);
  111.  
  112.         if (targets && targets.length > 0) {
  113.             var target = targets[0];
  114.             if (target.localName == "menuitem"){
  115.                 gActionTargetElement.selectedItem = target;
  116.                 PickedMsgFolder(gActionTargetElement.selectedItem, gActionTargetElement.id)
  117.                         }
  118.         }
  119.     } else if (filter.action == nsMsgFilterAction.ChangePriority) {
  120.         dump("initializing priority..\n");
  121.         selectedPriority = gActionPriority.getElementsByAttribute("data", filter.actionPriority);
  122.  
  123.         if (selectedPriority && selectedPriority.length > 0) {
  124.             selectedPriority = selectedPriority[0];
  125.             gActionPriority.selectedItem = selectedPriority;
  126.         }
  127.     }
  128.         
  129.  
  130.     var scope = getScope(filter);
  131.     setSearchScope(scope);
  132.     initializeSearchRows(scope, filter.searchTerms);
  133.     if (filter.searchTerms.Count() > 1)
  134.         gSearchLessButton.removeAttribute("disabled", "false");
  135.  
  136. }
  137.  
  138.  
  139. // move to overlay
  140.  
  141. function saveFilter() {
  142.  
  143.     var isNewFilter;
  144.         var str;
  145.  
  146.     var filterName= gFilterNameElement.value;
  147.     if (!filterName || filterName == "") {
  148.         str = Bundle.GetStringFromName("mustEnterName");
  149.         window.alert(str);
  150.         gFilterNameElement.focus();
  151.         return false;
  152.     }
  153.  
  154.  
  155.     var targetUri;
  156.     var action = gActionElement.selectedItem.getAttribute("data");
  157.     dump("Action = " + action + "\n");
  158.     
  159.     if (action == nsMsgFilterAction.MoveToFolder) {
  160.         if (gActionTargetElement)
  161.             targetUri = gActionTargetElement.selectedItem.getAttribute("data");
  162.         //dump("folder target = " + gActionTargetElement.selectedItem + "\n");
  163.         if (!targetUri || targetUri == "") {
  164.             str = Bundle.GetStringFromName("mustSelectFolder");
  165.             window.alert(str);
  166.             return false;
  167.         }
  168.     }
  169.     
  170.     else if (action == nsMsgFilterAction.ChangePriority) {
  171.         if (!gActionPriority.selectedItem) {
  172.             var str = Bundle.GetStringFromName("mustSelectPriority");
  173.             window.alert(str);
  174.             return false;
  175.         }
  176.     }
  177.  
  178.     // this must happen after everything has 
  179.     if (!gFilter) {
  180.         gFilter = gFilterList.createFilter(gFilterNameElement.value);
  181.         isNewFilter = true;
  182.     } else {
  183.         gFilter.filterName = gFilterNameElement.value;
  184.         isNewFilter = false;
  185.     }
  186.  
  187.     gFilter.enabled=true;
  188.     gFilter.action = action;
  189.     if (action == nsMsgFilterAction.MoveToFolder)
  190.         gFilter.actionTargetFolderUri = targetUri;
  191.     else if (action == nsMsgFilterAction.ChangePriority)
  192.         gFilter.actionPriority = gActionPriority.selectedItem.getAttribute("data");
  193.  
  194.     saveSearchTerms(gFilter.searchTerms, gFilter);
  195.     
  196.     if (isNewFilter) {
  197.         dump("new filter.. inserting into " + gFilterList + "\n");
  198.         gFilterList.insertFilterAt(0, gFilter);
  199.     }
  200.  
  201.     // success!
  202.     return true;
  203. }
  204.  
  205. function onTargetFolderSelected(event)
  206. {
  207.     SetFolderPicker(event.target.id, gActionTargetElement.id);
  208. }
  209.  
  210.  
  211. function onActionChanged(event)
  212. {
  213.     var menuitem = event.target;
  214.     showActionElementFor(menuitem);
  215. }
  216.  
  217. function showActionElementFor(menuitem)
  218. {
  219.     if (!menuitem) return;
  220.     dump("showActionElementFor(" + menuitem.getAttribute("actionvalueindex") + ")\n");
  221.     gActionValueDeck.setAttribute("index", menuitem.getAttribute("actionvalueindex"));
  222. }
  223.