toolkit/modules/PageMenu.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/modules/PageMenu.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,139 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +this.EXPORTED_SYMBOLS = ["PageMenu"];
     1.9 +
    1.10 +this.PageMenu = function PageMenu() {
    1.11 +}
    1.12 +
    1.13 +PageMenu.prototype = {
    1.14 +  PAGEMENU_ATTR: "pagemenu",
    1.15 +  GENERATEDITEMID_ATTR: "generateditemid",
    1.16 +
    1.17 +  popup: null,
    1.18 +  builder: null,
    1.19 +
    1.20 +  maybeBuildAndAttachMenu: function(aTarget, aPopup) {
    1.21 +    var pageMenu = null;
    1.22 +    var target = aTarget;
    1.23 +    while (target) {
    1.24 +      var contextMenu = target.contextMenu;
    1.25 +      if (contextMenu) {
    1.26 +        pageMenu = contextMenu;
    1.27 +        break;
    1.28 +      }
    1.29 +      target = target.parentNode;
    1.30 +    }
    1.31 +
    1.32 +    if (!pageMenu) {
    1.33 +      return false;
    1.34 +    }
    1.35 +
    1.36 +    var insertionPoint = this.getInsertionPoint(aPopup);
    1.37 +    if (!insertionPoint) {
    1.38 +      return false;
    1.39 +    }
    1.40 +
    1.41 +    pageMenu.QueryInterface(Components.interfaces.nsIHTMLMenu);
    1.42 +    pageMenu.sendShowEvent();
    1.43 +    // the show event is not cancelable, so no need to check a result here
    1.44 +
    1.45 +    var fragment = aPopup.ownerDocument.createDocumentFragment();
    1.46 +
    1.47 +    var builder = pageMenu.createBuilder();
    1.48 +    if (!builder) {
    1.49 +      return false;
    1.50 +    }
    1.51 +    builder.QueryInterface(Components.interfaces.nsIXULContextMenuBuilder);
    1.52 +    builder.init(fragment, this.GENERATEDITEMID_ATTR);
    1.53 +
    1.54 +    pageMenu.build(builder);
    1.55 +
    1.56 +    var pos = insertionPoint.getAttribute(this.PAGEMENU_ATTR);
    1.57 +    if (pos == "start") {
    1.58 +      insertionPoint.insertBefore(fragment,
    1.59 +                                  insertionPoint.firstChild);
    1.60 +    } else {
    1.61 +      insertionPoint.appendChild(fragment);
    1.62 +    }
    1.63 +
    1.64 +    this.builder = builder;
    1.65 +    this.popup = aPopup;
    1.66 +
    1.67 +    this.popup.addEventListener("command", this);
    1.68 +    this.popup.addEventListener("popuphidden", this);
    1.69 +
    1.70 +    return true;
    1.71 +  },
    1.72 +
    1.73 +  handleEvent: function(event) {
    1.74 +    var type = event.type;
    1.75 +    var target = event.target;
    1.76 +    if (type == "command" && target.hasAttribute(this.GENERATEDITEMID_ATTR)) {
    1.77 +      this.builder.click(target.getAttribute(this.GENERATEDITEMID_ATTR));
    1.78 +    } else if (type == "popuphidden" && this.popup == target) {
    1.79 +      this.removeGeneratedContent(this.popup);
    1.80 +
    1.81 +      this.popup.removeEventListener("popuphidden", this);
    1.82 +      this.popup.removeEventListener("command", this);
    1.83 +
    1.84 +      this.popup = null;
    1.85 +      this.builder = null;
    1.86 +    }
    1.87 +  },
    1.88 +
    1.89 +  getImmediateChild: function(element, tag) {
    1.90 +    var child = element.firstChild;
    1.91 +    while (child) {
    1.92 +      if (child.localName == tag) {
    1.93 +        return child;
    1.94 +      }
    1.95 +      child = child.nextSibling;
    1.96 +    }
    1.97 +    return null;
    1.98 +  },
    1.99 +
   1.100 +  getInsertionPoint: function(aPopup) {
   1.101 +    if (aPopup.hasAttribute(this.PAGEMENU_ATTR))
   1.102 +      return aPopup;
   1.103 +
   1.104 +    var element = aPopup.firstChild;
   1.105 +    while (element) {
   1.106 +      if (element.localName == "menu") {
   1.107 +        var popup = this.getImmediateChild(element, "menupopup");
   1.108 +        if (popup) {
   1.109 +          var result = this.getInsertionPoint(popup);
   1.110 +          if (result) {
   1.111 +            return result;
   1.112 +          }
   1.113 +        }
   1.114 +      }
   1.115 +      element = element.nextSibling;
   1.116 +    }
   1.117 +
   1.118 +    return null;
   1.119 +  },
   1.120 +
   1.121 +  removeGeneratedContent: function(aPopup) {
   1.122 +    var ungenerated = [];
   1.123 +    ungenerated.push(aPopup);
   1.124 +
   1.125 +    var count;
   1.126 +    while (0 != (count = ungenerated.length)) {
   1.127 +      var last = count - 1;
   1.128 +      var element = ungenerated[last];
   1.129 +      ungenerated.splice(last, 1);
   1.130 +
   1.131 +      var i = element.childNodes.length;
   1.132 +      while (i-- > 0) {
   1.133 +        var child = element.childNodes[i];
   1.134 +        if (!child.hasAttribute(this.GENERATEDITEMID_ATTR)) {
   1.135 +          ungenerated.push(child);
   1.136 +          continue;
   1.137 +        }
   1.138 +        element.removeChild(child);
   1.139 +      }
   1.140 +    }
   1.141 +  }
   1.142 +}

mercurial