customizetoolbar.js

来自「现在很火的邮件客户端软件thunderbird的源码」· JavaScript 代码 · 共 960 行 · 第 1/2 页

JS
960
字号
{  aWrapper.setAttribute("place", "palette");  setWrapperType(aItem, aWrapper);  if (aItem.hasAttribute("title"))    aWrapper.setAttribute("title", aItem.getAttribute("title"));  else if (isSpecialItem(aItem)) {    var stringBundle = document.getElementById("stringBundle");    var title = stringBundle.getString(aItem.id + "Title");    aWrapper.setAttribute("title", title);  }    // Remove attributes that screw up our appearance.  aItem.removeAttribute("command");  aItem.removeAttribute("observes");  aItem.removeAttribute("disabled");  aItem.removeAttribute("type");    if (aItem.localName == "toolbaritem" && aItem.firstChild) {    aItem.firstChild.removeAttribute("observes");    // So the throbber doesn't throb in the dialog,    // cute as that may be...    aItem.firstChild.removeAttribute("busy");  }}/** * Makes sure that an item that has been cloned from a template * is stripped of all properties that may adversely affect it's * appearance in the toolbar.  Store critical properties on the  * wrapper so they can be put back on the item when we're done. */function cleanupItemForToolbar(aItem, aWrapper){  setWrapperType(aItem, aWrapper);  aWrapper.setAttribute("place", "toolbar");  if (aItem.hasAttribute("command")) {    aWrapper.setAttribute("itemcommand", aItem.getAttribute("command"));    aItem.removeAttribute("command");  }  if (aItem.hasAttribute("observes"))  {    aWrapper.setAttribute("itemobserves", aItem.getAttribute("observes"));    aItem.removeAttribute("observes");  }  if (aItem.disabled) {    aWrapper.setAttribute("itemdisabled", "true");    aItem.disabled = false;  }}function setWrapperType(aItem, aWrapper){  if (aItem.localName == "toolbarseparator") {    aWrapper.setAttribute("type", "separator");  } else if (aItem.localName == "toolbarspring") {    aWrapper.setAttribute("type", "spring");  } else if (aItem.localName == "toolbarspacer") {    aWrapper.setAttribute("type", "spacer");  } else if (aItem.localName == "toolbaritem" && aItem.firstChild) {    aWrapper.setAttribute("type", aItem.firstChild.localName);  }}function setDragActive(aItem, aValue){  var node = aItem;  var direction = window.getComputedStyle(aItem, null).direction;  var value = direction == "ltr"? "left" : "right";  if (aItem.localName == "toolbar") {    node = aItem.lastChild;    value = direction == "ltr"? "right" : "left";  }    if (!node)    return;    if (aValue) {    if (!node.hasAttribute("dragover"))      node.setAttribute("dragover", value);  } else {    node.removeAttribute("dragover");  }}/** * Restore the default set of buttons to fixed toolbars, * remove all custom toolbars, and rebuild the palette. */function restoreDefaultSet(){  // Restore the defaultset for fixed toolbars.  unwrapToolbarItems(false);  var toolbar = gToolbox.firstChild;  while (toolbar) {    if (isCustomizableToolbar(toolbar)) {      if (!toolbar.hasAttribute("customindex")) {        var defaultSet = toolbar.getAttribute("defaultset");        if (defaultSet)        {          toolbar.currentSet = defaultSet;        }      }    }    toolbar = toolbar.nextSibling;  }  // Restore the default icon size (large) and mode (icons and text).  updateIconSize(false);  document.getElementById("smallicons").checked = false;  updateToolbarMode("full");  document.getElementById("modelist").value = "full";  // Remove all of the customized toolbars.  var child = gToolbox.lastChild;  while (child) {    if (child.hasAttribute("customindex")) {      var thisChild = child;      child = child.previousSibling;      gToolbox.removeChild(thisChild);    } else {      child = child.previousSibling;    }  }    // Now rebuild the palette.  buildPalette();  // Now re-wrap the items on the toolbar, but don't clobber previousset.  wrapToolbarItems(false);  repositionDialog();  gToolboxChanged = true;}function updateIconSize(aUseSmallIcons){  var val = aUseSmallIcons ? "small" : null;    setAttribute(gToolbox, "iconsize", val);  gToolboxDocument.persist(gToolbox.id, "iconsize");    for (var i = 0; i < gToolbox.childNodes.length; ++i) {    var toolbar = getToolbarAt(i);    if (isCustomizableToolbar(toolbar)) {      setAttribute(toolbar, "iconsize", val);      gToolboxDocument.persist(toolbar.id, "iconsize");    }  }  repositionDialog();}function updateToolbarMode(aModeValue){  setAttribute(gToolbox, "mode", aModeValue);  gToolboxDocument.persist(gToolbox.id, "mode");  for (var i = 0; i < gToolbox.childNodes.length; ++i) {    var toolbar = getToolbarAt(i);    if (isCustomizableToolbar(toolbar)) {      setAttribute(toolbar, "mode", aModeValue);      gToolboxDocument.persist(toolbar.id, "mode");    }  }  var iconSizeCheckbox = document.getElementById("smallicons");  iconSizeCheckbox.disabled = aModeValue == "text";  repositionDialog();}function setAttribute(aElt, aAttr, aVal){ if (aVal)    aElt.setAttribute(aAttr, aVal);  else    aElt.removeAttribute(aAttr);}function isCustomizableToolbar(aElt){  return aElt.localName == "toolbar" &&         aElt.getAttribute("customizable") == "true";}function isSpecialItem(aElt){  return aElt.localName == "toolbarseparator" ||         aElt.localName == "toolbarspring" ||         aElt.localName == "toolbarspacer";}function isToolbarItem(aElt){  return aElt.localName == "toolbarbutton" ||         aElt.localName == "toolbaritem" ||         aElt.localName == "toolbarseparator" ||         aElt.localName == "toolbarspring" ||         aElt.localName == "toolbarspacer";}/////////////////////////////////////////////////////////////////////////////// Drag and Drop observersfunction onToolbarDragGesture(aEvent){  nsDragAndDrop.startDrag(aEvent, dragStartObserver);}function onToolbarDragOver(aEvent){  nsDragAndDrop.dragOver(aEvent, toolbarDNDObserver);}function onToolbarDragDrop(aEvent){  nsDragAndDrop.drop(aEvent, toolbarDNDObserver);}function onToolbarDragExit(aEvent){  if (gCurrentDragOverItem)    setDragActive(gCurrentDragOverItem, false);}var dragStartObserver ={  onDragStart: function (aEvent, aXferData, aDragAction) {    var documentId = gToolboxDocument.documentElement.id;        var item = aEvent.target;    while (item && item.localName != "toolbarpaletteitem")      item = item.parentNode;        item.setAttribute("dragactive", "true");        aXferData.data = new TransferDataSet();    var data = new TransferData();    data.addDataForFlavour("text/toolbarwrapper-id/"+documentId, item.firstChild.id);    aXferData.data.push(data);  }}var toolbarDNDObserver ={  onDragOver: function (aEvent, aFlavour, aDragSession)  {    var toolbar = aEvent.target;    var dropTarget = aEvent.target;    while (toolbar && toolbar.localName != "toolbar") {      dropTarget = toolbar;      toolbar = toolbar.parentNode;    }        var previousDragItem = gCurrentDragOverItem;    // Make sure we are dragging over a customizable toolbar.    if (!isCustomizableToolbar(toolbar)) {      gCurrentDragOverItem = null;      return;    }        if (dropTarget.localName == "toolbar") {      gCurrentDragOverItem = dropTarget;    } else {      gCurrentDragOverItem = null;      var direction = window.getComputedStyle(dropTarget.parentNode, null).direction;      var dropTargetCenter = dropTarget.boxObject.x + (dropTarget.boxObject.width / 2);      if (direction == "ltr")        dragAfter = aEvent.clientX > dropTargetCenter;      else        dragAfter = aEvent.clientX < dropTargetCenter;              if (dragAfter) {        gCurrentDragOverItem = dropTarget.nextSibling;        if (!gCurrentDragOverItem)          gCurrentDragOverItem = toolbar;      } else        gCurrentDragOverItem = dropTarget;    }        if (previousDragItem && gCurrentDragOverItem != previousDragItem) {      setDragActive(previousDragItem, false);    }        setDragActive(gCurrentDragOverItem, true);        aDragSession.canDrop = true;  },    onDrop: function (aEvent, aXferData, aDragSession)  {    if (!gCurrentDragOverItem)      return;        setDragActive(gCurrentDragOverItem, false);    var draggedItemId = aXferData.data;    if (gCurrentDragOverItem.id == draggedItemId)      return;    var toolbar = aEvent.target;    while (toolbar.localName != "toolbar")      toolbar = toolbar.parentNode;    var draggedPaletteWrapper = document.getElementById("wrapper-"+draggedItemId);           if (!draggedPaletteWrapper) {      // The wrapper has been dragged from the toolbar.            // Get the wrapper from the toolbar document and make sure that      // it isn't being dropped on itself.      var wrapper = gToolboxDocument.getElementById("wrapper-"+draggedItemId);      if (wrapper == gCurrentDragOverItem)        return;      // Don't allow static kids (e.g., the menubar) to move.      if (wrapper.parentNode.firstPermanentChild && wrapper.parentNode.firstPermanentChild.id == wrapper.firstChild.id)        return;      if (wrapper.parentNode.lastPermanentChild && wrapper.parentNode.lastPermanentChild.id == wrapper.firstChild.id)        return;      // Remove the item from it's place in the toolbar.      wrapper.parentNode.removeChild(wrapper);      // Determine which toolbar we are dropping on.      var dropToolbar = null;      if (gCurrentDragOverItem.localName == "toolbar")        dropToolbar = gCurrentDragOverItem;      else        dropToolbar = gCurrentDragOverItem.parentNode;            // Insert the item into the toolbar.      if (gCurrentDragOverItem != dropToolbar)        dropToolbar.insertBefore(wrapper, gCurrentDragOverItem);      else        dropToolbar.appendChild(wrapper);    } else {      // The item has been dragged from the palette            // Create a new wrapper for the item. We don't know the id yet.      var wrapper = createWrapper("");      // Ask the toolbar to clone the item's template, place it inside the wrapper, and insert it in the toolbar.      var newItem = toolbar.insertItem(draggedItemId, gCurrentDragOverItem == toolbar ? null : gCurrentDragOverItem, wrapper);            // Prepare the item and wrapper to look good on the toolbar.      cleanupItemForToolbar(newItem, wrapper);      wrapper.id = "wrapper-"+newItem.id;      wrapper.flex = newItem.flex;      // Remove the wrapper from the palette.      var currentRow = draggedPaletteWrapper.parentNode;      if (draggedItemId != "separator" &&          draggedItemId != "spring" &&          draggedItemId != "spacer")      {        currentRow.removeChild(draggedPaletteWrapper);        while (currentRow) {          // Pull the first child of the next row up          // into this row.          var nextRow = currentRow.nextSibling;                    if (!nextRow) {            var last = currentRow.lastChild;            var first = currentRow.firstChild;            if (first == last) {              // Kill the row.              currentRow.parentNode.removeChild(currentRow);              break;            }            if (last.localName == "spacer") {              var flex = last.getAttribute("flex");              last.setAttribute("flex", ++flex);              // Reflow doesn't happen for some reason.  Trigger it with a hide/show. ICK! -dwh              last.hidden = true;              last.hidden = false;              break;            } else {              // Make a spacer and give it a flex of 1.              var spacer = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",                                                    "spacer");              spacer.setAttribute("flex", "1");              currentRow.appendChild(spacer);            }            break;          }                    currentRow.appendChild(nextRow.firstChild);          currentRow = currentRow.nextSibling;        }      }    }        gCurrentDragOverItem = null;    repositionDialog();    gToolboxChanged = true;  },    _flavourSet: null,    getSupportedFlavours: function ()  {    if (!this._flavourSet) {      this._flavourSet = new FlavourSet();      var documentId = gToolboxDocument.documentElement.id;      this._flavourSet.appendFlavour("text/toolbarwrapper-id/"+documentId);    }    return this._flavourSet;  }}var paletteDNDObserver ={  onDragOver: function (aEvent, aFlavour, aDragSession)  {    aDragSession.canDrop = true;  },    onDrop: function(aEvent, aXferData, aDragSession)  {    var itemId = aXferData.data;        var wrapper = gToolboxDocument.getElementById("wrapper-"+itemId);    if (wrapper) {      // Don't allow static kids (e.g., the menubar) to move.      if (wrapper.parentNode.firstPermanentChild && wrapper.parentNode.firstPermanentChild.id == wrapper.firstChild.id)        return;      if (wrapper.parentNode.lastPermanentChild && wrapper.parentNode.lastPermanentChild.id == wrapper.firstChild.id)        return;      // The item was dragged out of the toolbar.      wrapper.parentNode.removeChild(wrapper);            var wrapperType = wrapper.getAttribute("type");      if (wrapperType != "separator" && wrapperType != "spacer" && wrapperType != "spring") {        // Find the template node in the toolbox palette        var templateNode = gToolbox.palette.firstChild;        while (templateNode) {          if (templateNode.id == itemId)            break;          templateNode = templateNode.nextSibling;        }        if (!templateNode)          return;                // Clone the template and add it to our palette.        var paletteItem = templateNode.cloneNode(true);        appendPaletteItem(paletteItem);      }    }        repositionDialog();    gToolboxChanged = true;  },    _flavourSet: null,    getSupportedFlavours: function ()  {    if (!this._flavourSet) {      this._flavourSet = new FlavourSet();      var documentId = gToolboxDocument.documentElement.id;      this._flavourSet.appendFlavour("text/toolbarwrapper-id/"+documentId);    }    return this._flavourSet;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?