Commit fda23bd6 authored by Paolo Amadini's avatar Paolo Amadini
Browse files

Bug 1457027 - Part 8 - Add a HandlerListItem class. r=jaws

MozReview-Commit-ID: 2ohtRX1w0D3

--HG--
extra : rebase_source : 380cb9eb707998db595cbf6d9bc1a75f09d01749
parent 2be8193d
Loading
Loading
Loading
Loading
+46 −38
Original line number Diff line number Diff line
@@ -244,6 +244,8 @@ if (AppConstants.MOZ_UPDATER) {
// We store this in a global so tests can await it.
var promiseLoadHandlersList;

var gNodeToObjectMap = new WeakMap();

var gMainPane = {
  // The set of types the app knows how to handle.  A hash of HandlerInfoWrapper
  // objects, indexed by type.
@@ -1505,7 +1507,7 @@ var gMainPane = {

  _rebuildView() {
    let lastSelectedType = this._list.selectedItem &&
                           this._list.selectedItem.getAttribute("type");
      HandlerListItem.forNode(this._list.selectedItem).handlerInfoWrapper.type;

    // Clear the list of entries.
    while (this._list.childNodes.length > 1)
@@ -1518,21 +1520,11 @@ var gMainPane = {
      visibleTypes = visibleTypes.filter(this._matchesFilter, this);

    for (let visibleType of visibleTypes) {
      let item = document.createElement("richlistitem");
      item.setAttribute("type", visibleType.type);
      item.setAttribute("typeDescription", visibleType.typeDescription);
      if (visibleType.smallIcon)
        item.setAttribute("typeIcon", visibleType.smallIcon);
      item.setAttribute("actionDescription", visibleType.actionDescription);

      if (!this._setIconClassForPreferredAction(visibleType, item)) {
        item.setAttribute("actionIcon", visibleType.actionIcon);
      }

      this._list.appendChild(item);
      let item = new HandlerListItem(visibleType);
      this._list.appendChild(item.node);

      if (visibleType.type === lastSelectedType) {
        this._list.selectedItem = item;
        this._list.selectedItem = item.node;
      }
    }
  },
@@ -1950,10 +1942,7 @@ var gMainPane = {
    handlerInfo.handledOnlyByPlugin = false;

    // Update the action label and image to reflect the new preferred action.
    typeItem.setAttribute("actionDescription", handlerInfo.actionDescription);
    if (!this._setIconClassForPreferredAction(handlerInfo, typeItem)) {
      typeItem.setAttribute("actionIcon", handlerInfo.actionIcon);
    }
    HandlerListItem.forNode(typeItem).refreshAction();
  },

  manageApp(aEvent) {
@@ -1970,10 +1959,7 @@ var gMainPane = {
      this.rebuildActionsMenu();

      // update the richlistitem too. Will be visible when selecting another row
      typeItem.setAttribute("actionDescription", handlerInfo.actionDescription);
      if (!this._setIconClassForPreferredAction(handlerInfo, typeItem)) {
        typeItem.setAttribute("actionIcon", handlerInfo.actionIcon);
      }
      HandlerListItem.forNode(typeItem).refreshAction();
    };

    gSubDialog.open("chrome://browser/content/preferences/applicationManager.xul",
@@ -2067,21 +2053,6 @@ var gMainPane = {
    }
  },

  _setIconClassForPreferredAction(aHandlerInfo, aElement) {
    // If this returns true, the attribute that CSS sniffs for was set to something
    // so you shouldn't manually set an icon URI.
    // This removes the existing actionIcon attribute if any, even if returning false.
    aElement.removeAttribute("actionIcon");

    if (aHandlerInfo.actionIconClass) {
      aElement.setAttribute(APP_ICON_ATTR_NAME, aHandlerInfo.actionIconClass);
      return true;
    }

    aElement.removeAttribute(APP_ICON_ATTR_NAME);
    return false;
  },

  _getIconURLForHandlerApp(aHandlerApp) {
    if (aHandlerApp instanceof Ci.nsILocalHandlerApp)
      return this._getIconURLForFile(aHandlerApp.executable);
@@ -2456,7 +2427,44 @@ function isFeedType(t) {
  return t == TYPE_MAYBE_FEED || t == TYPE_MAYBE_VIDEO_FEED || t == TYPE_MAYBE_AUDIO_FEED;
}

// HandlerInfoWrapper
/**
 * This is associated to <richlistitem> elements in the handlers view.
 */
class HandlerListItem {
  static forNode(node) {
    return gNodeToObjectMap.get(node);
  }

  constructor(handlerInfoWrapper) {
    this.handlerInfoWrapper = handlerInfoWrapper;
    this.node = document.createElement("richlistitem");
    gNodeToObjectMap.set(this.node, this);

    this.node.setAttribute("type", this.handlerInfoWrapper.type);
    this.node.setAttribute("typeDescription",
                           this.handlerInfoWrapper.typeDescription);
    if (this.handlerInfoWrapper.smallIcon) {
      this.node.setAttribute("typeIcon", this.handlerInfoWrapper.smallIcon);
    } else {
      this.node.removeAttribute("typeIcon");
    }

    this.refreshAction();
  }

  refreshAction() {
    this.node.setAttribute("actionDescription",
                           this.handlerInfoWrapper.actionDescription);
    if (this.handlerInfoWrapper.actionIconClass) {
      this.node.setAttribute(APP_ICON_ATTR_NAME,
                             this.handlerInfoWrapper.actionIconClass);
      this.node.removeAttribute("actionIcon");
    } else {
      this.node.removeAttribute(APP_ICON_ATTR_NAME);
      this.node.setAttribute("actionIcon", this.handlerInfoWrapper.actionIcon);
    }
  }
}

/**
 * This object wraps nsIHandlerInfo with some additional functionality