Advertisement
Guest User

Untitled

a guest
Oct 28th, 2011
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 addressbook.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Seth Spitzer <sspitzer@netscape.com>.
  18.  * Portions created by the Initial Developer are Copyright (C) 2001
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. /**
  38.  * Use of items in this file require:
  39.  *
  40.  * GetSelectedDirectory()
  41.  *   returns the URI of the selected directory
  42.  * AbResultsPaneDoubleClick(card)
  43.  *   Is called when the results pane is double-clicked, with the clicked card.
  44.  * AbEditCard(card)
  45.  *   Is called when a card is to be edited, with the card as the parameter.
  46.  *
  47.  * The following function is only required if ResultsPaneController is used:
  48.  *
  49.  * goSetMenuValue()
  50.  *   Core function in globalOverlay.js
  51.  */
  52.  
  53. // List/card selections in the results pane.
  54. const kNothingSelected = 0;
  55. const kListsAndCards = 1;
  56. const kMultipleListsOnly = 2;
  57. const kSingleListOnly = 3;
  58. const kCardsOnly = 4;
  59.  
  60. // Global Variables
  61.  
  62. // gAbView holds an object with an nsIAbView interface
  63. var gAbView = null;
  64. var gAbViewWrapper = null;
  65.  
  66. // Holds a reference to the "abResultsTree" document element. Initially
  67. // set up by SetAbView.
  68. var gAbResultsTree = null;
  69.  
  70. function SetAbView(aURI)
  71. {
  72.   // If we don't have a URI, just clear the view and leave everything else
  73.   // alone.
  74.   if (!aURI) {
  75.     gAbView.clearView();
  76.     return;
  77.   }
  78.  
  79.   // If we do have a URI, we want to allow updating the review even if the
  80.   // URI is the same, as the search results may be different.
  81.  
  82.   var sortColumn = kDefaultSortColumn;
  83.   var sortDirection = kDefaultAscending;
  84.  
  85.   if (!gAbResultsTree) {
  86.     gAbResultsTree = document.getElementById("abResultsTree");
  87.     gAbResultsTree.controllers.appendController(ResultsPaneController);
  88.   }
  89.  
  90.   if (gAbView) {
  91.     sortColumn = gAbView.sortColumn;
  92.     sortDirection = gAbView.sortDirection;
  93.   }
  94.   else {
  95.     if (gAbResultsTree.hasAttribute("sortCol"))
  96.       sortColumn = gAbResultsTree.getAttribute("sortCol");
  97.     var sortColumnNode = document.getElementById(sortColumn);
  98.     if (sortColumnNode && sortColumnNode.hasAttribute("sortDirection"))
  99.       sortDirection = sortColumnNode.getAttribute("sortDirection");
  100.   }
  101.  
  102.   var directory = GetDirectoryFromURI(aURI);
  103.  
  104.   if (!gAbView) {
  105.     gAbView = Components.classes["@mozilla.org/addressbook/abview;1"]
  106.                         .createInstance(Components.interfaces.nsIAbView);
  107.  
  108.     gAbViewWrapper = {
  109.       __proto__: gAbView,
  110.      
  111.       QueryInterface: function(iid) {
  112.         gAbView.QueryInterface(iid);
  113.         return this;
  114.       },
  115.      
  116.       isEditable: function(row, col) {
  117.         Components.utils.reportError("isEditable!");
  118.         // Do something here
  119.       },
  120.      
  121.       setCellText: function(row, col, value) {
  122.         Components.utils.reportError("Done editing (" + row + "," + col + "): " + value);
  123.         //TODO.
  124.       }
  125.     };
  126.   }
  127.  
  128.   var actualSortColumn = gAbView.setView(directory, GetAbViewListener(),
  129.                      sortColumn, sortDirection);
  130.  
  131.   gAbResultsTree.treeBoxObject.view =
  132.     gAbViewWrapper.QueryInterface(Components.interfaces.nsITreeView);
  133.  
  134.   UpdateSortIndicators(actualSortColumn, sortDirection);
  135. }
  136.  
  137. function CloseAbView()
  138. {
  139.   if (gAbView)
  140.     gAbView.clearView();
  141. }
  142.  
  143. function GetOneOrMoreCardsSelected()
  144. {
  145.   return (gAbView && (gAbView.selection.getRangeCount() > 0));
  146. }
  147.  
  148. function GetSelectedAddresses()
  149. {
  150.   return GetAddressesForCards(GetSelectedAbCards());
  151. }
  152.  
  153. function GetNumSelectedCards()
  154. {
  155.  try {
  156.    return gAbView.selection.count;
  157.  }
  158.  catch (ex) {
  159.  }
  160.  
  161.  // if something went wrong, return 0 for the count.
  162.  return 0;
  163. }
  164.  
  165. function GetSelectedCardTypes()
  166. {
  167.   var cards = GetSelectedAbCards();
  168.   if (!cards)
  169.     return kNothingSelected; // no view
  170.  
  171.   var count = cards.length;
  172.   if (count == 0)
  173.     return kNothingSelected;  // nothing selected
  174.  
  175.   var mailingListCnt = 0;
  176.   var cardCnt = 0;
  177.   for (var i = 0; i < count; i++) {
  178.     if (cards[i].isMailList)
  179.       mailingListCnt++;
  180.     else
  181.       cardCnt++;
  182.   }
  183.   return (mailingListCnt == 0) ? kCardsOnly :
  184.            (cardCnt > 0) ? kListsAndCards :
  185.              (mailingListCnt == 1) ? kSingleListOnly :
  186.                kMultipleListsOnly;
  187. }
  188.  
  189. // NOTE, will return -1 if more than one card selected, or no cards selected.
  190. function GetSelectedCardIndex()
  191. {
  192.   if (!gAbView)
  193.     return -1;
  194.  
  195.   var treeSelection = gAbView.selection;
  196.   if (treeSelection.getRangeCount() == 1) {
  197.     var start = new Object;
  198.     var end = new Object;
  199.     treeSelection.getRangeAt(0, start, end);
  200.     if (start.value == end.value)
  201.       return start.value;
  202.   }
  203.  
  204.   return -1;
  205. }
  206.  
  207. // NOTE, returns the card if exactly one card is selected, null otherwise
  208. function GetSelectedCard()
  209. {
  210.   var index = GetSelectedCardIndex();
  211.   return (index == -1) ? null : gAbView.getCardFromRow(index);
  212. }
  213.  
  214. function GetSelectedAbCards()
  215. {
  216.   var abView = gAbView;
  217.  
  218.   // if sidebar is open, and addressbook panel is open and focused,
  219.   // then use the ab view from sidebar (gCurFrame is from sidebarOverlay.js)
  220.   if (document.getElementById("sidebar-box")) {
  221.     const abPanelUrl =
  222.             "chrome://messenger/content/addressbook/addressbook-panel.xul";
  223.     if (gCurFrame &&
  224.         gCurFrame.getAttribute("src") == abPanelUrl &&
  225.         document.commandDispatcher.focusedWindow == gCurFrame.contentDocument.defaultView)
  226.       abView = gCurFrame.contentDocument.defaultView.gAbView;
  227.   }
  228.  
  229.   if (!abView)
  230.     return null;
  231.  
  232.   var cards = new Array(abView.selection.count);
  233.   var i, j;
  234.   var count = abView.selection.getRangeCount();
  235.   var current = 0;
  236.   for (i = 0; i < count; ++i) {
  237.     var start = new Object;
  238.     var end = new Object;
  239.     abView.selection.getRangeAt(i,start,end);
  240.     for (j = start.value; j <= end.value; ++j)
  241.       cards[current++] = abView.getCardFromRow(j);
  242.   }
  243.   return cards;
  244. }
  245.  
  246. // XXX todo
  247. // an optimization might be to make this return
  248. // the selected ranges, which would be faster
  249. // when the user does large selections, but for now, let's keep it simple.
  250. function GetSelectedRows()
  251. {
  252.   var selectedRows = "";
  253.  
  254.   if (!gAbView)
  255.     return selectedRows;
  256.  
  257.   var i, j;
  258.   var rangeCount = gAbView.selection.getRangeCount();
  259.   for (i = 0; i < rangeCount; ++i) {
  260.     var start = new Object;
  261.     var end = new Object;
  262.     gAbView.selection.getRangeAt(i, start, end);
  263.     for (j = start.value;j <= end.value; ++j) {
  264.       if (selectedRows)
  265.         selectedRows += ",";
  266.       selectedRows += j;
  267.     }
  268.   }
  269.  
  270.   return selectedRows;
  271. }
  272.  
  273. function AbSwapFirstNameLastName()
  274. {
  275.   if (gAbView)
  276.     gAbView.swapFirstNameLastName();
  277. }
  278.  
  279. function AbEditSelectedCard()
  280. {
  281.   AbEditCard(GetSelectedCard());
  282. }
  283.  
  284. function AbResultsPaneOnClick(event)
  285. {
  286.   // we only care about button 0 (left click) events
  287.   if (event.button != 0) return;
  288.  
  289.   // all we need to worry about here is double clicks
  290.   // and column header clicks.
  291.   //
  292.   // we get in here for clicks on the "treecol" (headers)
  293.   // and the "scrollbarbutton" (scrollbar buttons)
  294.   // we don't want those events to cause a "double click"
  295.  
  296.   var t = event.originalTarget;
  297.  
  298.   if (t.localName == "treecol") {
  299.     var sortDirection;
  300.     var currentDirection = t.getAttribute("sortDirection");
  301.  
  302.     sortDirection = currentDirection == kDefaultDescending ?
  303.                                         kDefaultAscending : kDefaultDescending;
  304.  
  305.     SortAndUpdateIndicators(t.id, sortDirection);
  306.   }
  307.   else if (t.localName == "treechildren") {
  308.     // figure out what row the click was in
  309.     var orow = {}, ocolumn = {}, opart = {};
  310.    
  311.     //var row = gAbResultsTree.treeBoxObject.getRowAt(event.clientX,
  312.     //                      event.clientY);
  313.     gAbResultsTree.treeBoxObject.getCellAt(event.clientX, event.clientY,
  314.                             orow, ocolumn, opart);
  315.    
  316.     var row = orow.value, column = ocolumn.value.index;
  317.                        
  318.     if (row == -1)
  319.       return;
  320.  
  321.     if (event.detail == 2) {
  322.       Components.utils.reportError("abResultsPane.js" + "; row: " + row + ", column: " + column);
  323.       gAbResultsTree.editable = true;
  324.       Components.utils.reportError("Editable (Wrapper)?");
  325.       gAbViewWrapper.isEditable(orow.value, ocolumn.value);
  326.       Components.utils.reportError("Editable (View)?");
  327.       gAbResultsTree.treeBoxObject.view.isEditable(orow.value, ocolumn.value);
  328.       Components.utils.reportError("Starting edit...");
  329.       gAbResultsTree.startEditing(row, column);
  330.       //AbResultsPaneDoubleClick(gAbView.getCardFromRow(row));
  331.       Components.utils.reportError("done!");
  332.     }
  333.   }
  334. }
  335.  
  336. function AbSortAscending()
  337. {
  338.   var sortColumn = gAbResultsTree.getAttribute("sortCol");
  339.   SortAndUpdateIndicators(sortColumn, kDefaultAscending);
  340. }
  341.  
  342. function AbSortDescending()
  343. {
  344.   var sortColumn = gAbResultsTree.getAttribute("sortCol");
  345.   SortAndUpdateIndicators(sortColumn, kDefaultDescending);
  346. }
  347.  
  348. function SortResultPane(sortColumn)
  349. {
  350.   var sortDirection = kDefaultAscending;
  351.   if (gAbView)
  352.      sortDirection = gAbView.sortDirection;
  353.  
  354.   SortAndUpdateIndicators(sortColumn, sortDirection);
  355. }
  356.  
  357. function SortAndUpdateIndicators(sortColumn, sortDirection)
  358. {
  359.   UpdateSortIndicators(sortColumn, sortDirection);
  360.  
  361.   if (gAbView)
  362.     gAbView.sortBy(sortColumn, sortDirection);
  363. }
  364.  
  365. function UpdateSortIndicators(colID, sortDirection)
  366. {
  367.   var sortedColumn = null;
  368.  
  369.   // set the sort indicator on the column we are sorted by
  370.   if (colID) {
  371.     sortedColumn = document.getElementById(colID);
  372.     if (sortedColumn) {
  373.       sortedColumn.setAttribute("sortDirection",sortDirection);
  374.       gAbResultsTree.setAttribute("sortCol", colID);
  375.     }
  376.   }
  377.  
  378.   // remove the sort indicator from all the columns
  379.   // except the one we are sorted by
  380.   var currCol = gAbResultsTree.firstChild.firstChild;
  381.   while (currCol) {
  382.     if (currCol != sortedColumn && currCol.localName == "treecol")
  383.       currCol.removeAttribute("sortDirection");
  384.     currCol = currCol.nextSibling;
  385.   }
  386. }
  387.  
  388. function InvalidateResultsPane()
  389. {
  390.   if (gAbResultsTree)
  391.     gAbResultsTree.treeBoxObject.invalidate();
  392. }
  393.  
  394. // Controller object for Results Pane
  395. var ResultsPaneController =
  396. {
  397.   supportsCommand: function(command)
  398.   {
  399.     switch (command) {
  400.       case "cmd_selectAll":
  401.       case "cmd_delete":
  402.       case "button_delete":
  403.       case "button_edit":
  404.       case "cmd_newlist":
  405.         return true;
  406.       default:
  407.         return false;
  408.     }
  409.   },
  410.  
  411.   isCommandEnabled: function(command)
  412.   {
  413.     switch (command) {
  414.       case "cmd_selectAll":
  415.         return true;
  416.       case "cmd_delete":
  417.       case "button_delete":
  418.         var numSelected;
  419.         var enabled = false;
  420.         if (gAbView && gAbView.selection) {
  421.           if (gAbView.directory)
  422.             enabled = !gAbView.directory.readOnly;
  423.           numSelected = gAbView.selection.count;
  424.         }
  425.         else
  426.           numSelected = 0;
  427.  
  428.         // fix me, don't update on isCommandEnabled
  429.         if (command == "cmd_delete") {
  430.           switch (GetSelectedCardTypes()) {
  431.             case kSingleListOnly:
  432.               goSetMenuValue(command, "valueList");
  433.               break;
  434.             case kMultipleListsOnly:
  435.               goSetMenuValue(command, "valueLists");
  436.               break;
  437.             case kListsAndCards:
  438.               goSetMenuValue(command, "valueItems");
  439.               break;
  440.             case kCardsOnly:
  441.             default:
  442.               if (numSelected < 2)
  443.                 goSetMenuValue(command, "valueCard");
  444.               else
  445.                 goSetMenuValue(command, "valueCards");
  446.               break;
  447.           }
  448.         }
  449.         return (enabled && (numSelected > 0));
  450.       case "button_edit":
  451.         return (GetSelectedCardIndex() != -1);
  452.       case "cmd_newlist":
  453.         var selectedDir = GetSelectedDirectory();
  454.         if (selectedDir) {
  455.           var abDir = GetDirectoryFromURI(selectedDir);
  456.           if (abDir) {
  457.             return abDir.supportsMailingLists;
  458.           }
  459.         }
  460.         return false;
  461.       default:
  462.         return false;
  463.     }
  464.   },
  465.  
  466.   doCommand: function(command)
  467.   {
  468.     switch (command) {
  469.       case "cmd_selectAll":
  470.         if (gAbView)
  471.           gAbView.selectAll();
  472.         break;
  473.       case "cmd_delete":
  474.       case "button_delete":
  475.         AbDelete();
  476.         break;
  477.       case "button_edit":
  478.         AbEditSelectedCard();
  479.         break;
  480.       case "cmd_newlist":
  481.         AbNewList();
  482.         break;
  483.     }
  484.   },
  485.  
  486.   onEvent: function(event)
  487.   {
  488.     // on blur events set the menu item texts back to the normal values
  489.     if (event == "blur")
  490.       goSetMenuValue("cmd_delete", "valueDefault");
  491.   }
  492. };
  493.  
  494. function SelectFirstCard()
  495. {
  496.   if (gAbView && gAbView.selection)
  497.     gAbView.selection.select(0);
  498. }
  499.  
  500.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement