home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Complet / thunderbird / chrome / mail.jar / content / editor / EditorContextMenu.js < prev    next >
Encoding:
JavaScript  |  2003-07-31  |  4.5 KB  |  145 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  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 Communicator client code,
  14.  * released March 31, 1998.
  15.  *
  16.  * The Initial Developer of the Original Code is Netscape Communications
  17.  * Corporation.  Portions created by Netscape are
  18.  * Copyright (C) 2000 Netscape Communications Corporation. All
  19.  * Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *    Charles Manske (cmanske@netscape.com)
  23.  */
  24.  
  25. function EditorFillContextMenu(event, contextMenuNode)
  26. {
  27.   if ( event.target != contextMenuNode )
  28.     return;
  29.  
  30.   // Setup object property menuitem
  31.   var objectName = InitObjectPropertiesMenuitem("objectProperties_cm");
  32.   var isInLink = objectName == "href";
  33.  
  34.   // Special case of an image inside a link
  35.   if (objectName == "img")
  36.   try {
  37.     isInLink = GetCurrentEditor().getElementOrParentByTagName("href", GetObjectForProperties());
  38.   } catch (e) {}
  39.  
  40.   InitRemoveStylesMenuitems("removeStylesMenuitem_cm", "removeLinksMenuitem_cm", "removeNamedAnchorsMenuitem_cm");
  41.  
  42.   var inCell = IsInTableCell();
  43.   // Set appropriate text for join cells command
  44.   InitJoinCellMenuitem("joinTableCells_cm");
  45.  
  46.   // Update enable states for all table commands
  47.   goUpdateTableMenuItems(document.getElementById("composerTableMenuItems"));
  48.  
  49.   // Loop through all children to hide disabled items
  50.   var children = contextMenuNode.childNodes;
  51.   if (children)
  52.   {
  53.     var count = children.length;
  54.     for (var i = 0; i < count; i++)
  55.       HideDisabledItem(children[i]);
  56.   }
  57.  
  58.   // The above loop will always show all separators and the next two items
  59.   // Hide "Create Link" if in a link
  60.   ShowMenuItem("createLink_cm", !isInLink);
  61.  
  62.   // Hide "Edit link in new Composer" unless in a link
  63.   ShowMenuItem("editLink_cm", isInLink);
  64.  
  65.   // Remove separators if all items in immediate group above are hidden
  66.   // A bit complicated to account if multiple groups are completely hidden!
  67.   var haveUndo =
  68.     IsMenuItemShowing("menu_undo_cm") ||
  69.     IsMenuItemShowing("menu_redo_cm");
  70.  
  71.   var haveEdit =
  72.     IsMenuItemShowing("menu_cut_cm")   ||
  73.     IsMenuItemShowing("menu_copy_cm")  ||
  74.     IsMenuItemShowing("menu_paste_cm") ||
  75.     IsMenuItemShowing("menu_pasteNoFormatting_cm") ||
  76.     IsMenuItemShowing("menu_delete_cm");
  77.  
  78.   var haveStyle =
  79.     IsMenuItemShowing("removeStylesMenuitem_cm") ||
  80.     IsMenuItemShowing("createLink_cm") ||
  81.     IsMenuItemShowing("removeLinksMenuitem_cm") ||
  82.     IsMenuItemShowing("removeNamedAnchorsMenuitem_cm");
  83.  
  84.   var haveProps =
  85.     IsMenuItemShowing("objectProperties_cm");
  86.  
  87.   ShowMenuItem("undoredo-separator", haveUndo && haveEdit);
  88.  
  89.   ShowMenuItem("edit-separator", haveEdit || haveUndo);
  90.  
  91.   // Note: Item "menu_selectAll_cm" and
  92.   // following separator are ALWAYS enabled,
  93.   // so there will always be 1 separator here
  94.  
  95.   var showStyleSep = haveStyle && (haveProps || inCell);
  96.   ShowMenuItem("styles-separator", showStyleSep);
  97.  
  98.   var showPropSep = (haveProps && inCell);
  99.   ShowMenuItem("property-separator", showPropSep);
  100.  
  101.   // Remove table submenus if not in table
  102.   ShowMenuItem("tableInsertMenu_cm",  inCell);
  103.   ShowMenuItem("tableSelectMenu_cm",  inCell);
  104.   ShowMenuItem("tableDeleteMenu_cm",  inCell);
  105. }
  106.  
  107. function IsItemOrCommandEnabled( item )
  108. {
  109.   var command = item.getAttribute("command");
  110.   if (command) {
  111.     // If possible, query the command controller directly
  112.     var controller = document.commandDispatcher.getControllerForCommand(command);
  113.     if (controller)
  114.       return controller.isCommandEnabled(command);
  115.   }
  116.  
  117.   // Fall back on the inefficient observed disabled attribute
  118.   return item.getAttribute("disabled") != "true";
  119. }
  120.  
  121. function HideDisabledItem( item )
  122. {
  123.   item.hidden = !IsItemOrCommandEnabled(item);
  124. }
  125.  
  126. function ShowMenuItem(id, showItem)
  127. {
  128.   var item = document.getElementById(id);
  129.   if (item && !showItem)
  130.   {
  131.     item.hidden = true;
  132.   }
  133.   // else HideDisabledItem showed the item anyway
  134. }
  135.  
  136. function IsMenuItemShowing(menuID)
  137. {
  138.   var item = document.getElementById(menuID);
  139.   if (item)
  140.     return !item.hidden;
  141.  
  142.   return false;
  143. }
  144.  
  145.