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

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  * 
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  * 
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  * 
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  */
  20.  
  21. /*  This file contains the js functions necessary to implement view navigation within the 3 pane. */
  22.  
  23. // These are the types of navigation you can do
  24. var navigateAny=0;
  25. var navigateUnread = 1;
  26. var navigateFlagged = 2;
  27. var navigateNew = 3;
  28.  
  29.  
  30. /*GoNextMessage finds the message that matches criteria and selects it.  
  31.   nextFunction is the function that will be used to detertime if a message matches criteria.
  32.   It must take a node and return a boolean.
  33.   nextResourceFunction is the function that will be used to determine if a message in the form of a resource
  34.   matches criteria.  Takes a resource and returns a boolean
  35.   nextThreadFunction is an optional function that can be used to optimize whether or not a thread will have a
  36.   message that matches the criteria.  Takes the top level message in the form of a node and returns a boolean.
  37.   startFromBeginning is a boolean that states whether or not we should start looking at the beginning
  38.   if we reach the end 
  39. */
  40. function GoNextMessage(type, startFromBeginning )
  41. {
  42.     var beforeGoNextMessage = new Date();
  43.  
  44.     var tree = GetThreadTree();
  45.     
  46.     var selArray = tree.selectedItems;
  47.     var length = selArray.length;
  48.  
  49.     if ( selArray && ((length == 0) || (length == 1)) )
  50.     {
  51.         var currentMessage;
  52.  
  53.         if(length == 0)
  54.             currentMessage = null;
  55.         else
  56.             currentMessage = selArray[0];
  57.  
  58.         var nextMessage = msgNavigationService.FindNextMessage(type, tree, currentMessage, RDF, document, startFromBeginning, messageView.showThreads);
  59.         //Only change the selection if there's a valid nextMessage
  60.         if(nextMessage && (nextMessage != currentMessage))
  61.             ChangeSelection(tree, nextMessage);
  62.     }
  63.  
  64.     var afterGoNextMessage = new Date();
  65.     var timeToGetNext = (afterGoNextMessage.getTime() - beforeGoNextMessage.getTime())/1000;
  66.     dump("time to GoNextMessage is " + timeToGetNext + "seconds\n");
  67.  
  68. }
  69.  
  70.  
  71. /*GoPreviousMessage finds the message that matches criteria and selects it.  
  72.   previousFunction is the function that will be used to detertime if a message matches criteria.
  73.   It must take a node and return a boolean.
  74.   startFromEnd is a boolean that states whether or not we should start looking at the end
  75.   if we reach the beginning 
  76. */
  77. function GoPreviousMessage(type, startFromEnd)
  78. {
  79.     var tree = GetThreadTree();
  80.     
  81.     var selArray = tree.selectedItems;
  82.     if ( selArray && (selArray.length == 1) )
  83.     {
  84.         var currentMessage = selArray[0];
  85.         var previousMessage = msgNavigationService.FindPreviousMessage(type, tree, currentMessage, RDF, document, startFromEnd, messageView.showThreads);
  86.         //Only change selection if there's a valid previous message.
  87.         if(previousMessage && (previousMessage != currentMessage))
  88.             ChangeSelection(tree, previousMessage);
  89.     }
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
  96. // type is the the type of the next thread we are looking for.
  97. // startFromBeginning is true if we should start looking from the beginning after we get to the end of the thread pane.
  98. // gotoNextInThread is true if once we find an unrad thread we should select the first message in that thread that fits criteria
  99. function GoNextThread(type, startFromBeginning, gotoNextInThread)
  100. {
  101.  
  102.     if(messageView.showThreads)
  103.     {
  104.         var tree = GetThreadTree();
  105.         
  106.         var selArray = tree.selectedItems;
  107.         var length = selArray.length;
  108.  
  109.         if ( selArray && ((length == 0) || (length == 1)) )
  110.         {
  111.             var currentMessage;
  112.  
  113.             if(length == 0)
  114.                 currentMessage = null;
  115.             else
  116.                 currentMessage = selArray[0];
  117.  
  118.             var nextMessage;
  119.             var currentTopMessage;
  120.             var checkCurrentTopMessage;
  121.             //Need to get the parent message for the current selection to begin to find thread
  122.             if(currentMessage)
  123.             {
  124.                 //need to find its top level message and we don't want it to be checked for criteria
  125.                 currentTopMessage = FindTopLevelMessage(currentMessage);
  126.                 checkCurrentTopMessage = false;
  127.             }
  128.             else
  129.             {
  130.                 //currentTopmessage is the first one in the tree and we want it to be checked for criteria.
  131.                 currentTopMessage = msgNavigationService.FindFirstMessage(tree);
  132.                 checkCurrentTopMessage = true;
  133.             }
  134.  
  135.             var nextTopMessage = msgNavigationService.FindNextThread(type, tree, currentTopMessage, RDF, document, startFromBeginning, checkCurrentTopMessage);
  136.  
  137.             var changeSelection = (nextTopMessage != null && ((currentTopMessage != nextTopMessage) || checkCurrentTopMessage));
  138.             if(changeSelection)
  139.             {
  140.                 if(gotoNextInThread)
  141.                 {
  142.                     nextMessage = msgNavigationService.FindNextInThread(type, tree, nextTopMessage, RDF, document);
  143.                     ChangeSelection(tree, nextMessage);
  144.                 }
  145.                 else
  146.                     ChangeSelection(tree, nextTopMessage);    
  147.             }
  148.         }
  149.     }
  150.  
  151. }
  152.  
  153. function FindTopLevelMessage(startMessage)
  154. {
  155.     var currentTop = startMessage;
  156.     var parent = startMessage.parentNode.parentNode;
  157.  
  158.     while(parent.localName == 'treeitem')
  159.     {
  160.         currentTop = parent;
  161.         parent = parent.parentNode.parentNode;
  162.     }
  163.  
  164.     return currentTop;
  165. }
  166.  
  167.  
  168. function ScrollToFirstNewMessage()
  169. {
  170.     var tree = GetThreadTree();
  171.     var treeFolder = GetThreadTreeFolder();
  172.  
  173.     var folderURI = treeFolder.getAttribute('ref');
  174.     var folderResource = RDF.GetResource(folderURI);
  175.     var folder = folderResource.QueryInterface(Components.interfaces.nsIMsgFolder);
  176.     var hasNew = folder.hasNewMessages;
  177.     if(hasNew)
  178.     {
  179.         var newMessage = folder.firstNewMessage;
  180.  
  181.         if(messageView.showThreads)
  182.         {
  183.             //if we're in thread mode, then we need to actually make sure the message is showing.
  184.             var topLevelMessage = GetTopLevelMessageForMessage(newMessage, folder);
  185.             var topLevelResource = topLevelMessage.QueryInterface(Components.interfaces.nsIRDFResource);
  186.             var topLevelURI = topLevelResource.Value;
  187.             var topElement = document.getElementById(topLevelURI);
  188.             if(topElement)
  189.             {
  190.                 msgNavigationService.OpenTreeitemAndDescendants(topElement);
  191.             }
  192.  
  193.         }
  194.         
  195.         var messageResource = newMessage.QueryInterface(Components.interfaces.nsIRDFResource);
  196.         var messageURI = messageResource.Value;
  197.         var messageElement = document.getElementById(messageURI);
  198.  
  199.         if(messageElement)
  200.         {
  201.             tree.ensureElementIsVisible(messageElement); 
  202.         }
  203.     }
  204. }
  205.  
  206. function GetTopLevelMessageForMessage(message, folder)
  207. {
  208.     if(!folder)
  209.         folder = message.msgFolder;
  210.  
  211.     var thread = folder.getThreadForMessage(message);
  212.     var outIndex = new Object();
  213.     var rootHdr = thread.GetRootHdr(outIndex);
  214.  
  215.     var topMessage = folder.createMessageFromMsgDBHdr(rootHdr);
  216.  
  217.     return topMessage;
  218.  
  219. }
  220.  
  221.  
  222.