Loading ...
Sorry, an error occurred while loading the content.

getItem(0) has no properties when disabling a context menu item

Expand Messages
  • Joseph F. Douglas
    I can not see how I am using the getItem(0) method on the oGoalContextMenu context menu boject incorrectly? I want to disable particular context menu items
    Message 1 of 4 , Apr 26, 2007
    • 0 Attachment
      I can not see how I am using the getItem(0) method on the
      oGoalContextMenu context menu boject incorrectly? I want to disable
      particular context menu items depending on certain conditions. In
      the function onTriggerGoalContextMenu I assume "this" refers to the
      contact menu object oGoalContextMenu. I am trying to disable specific
      menu items when the user right clicks on a tree node. The functions
      work fine, I just want to disable certain menu options.

      Firefox Firebug generates

      this.getItem(0) has no properties
      onTriggerGoalContextMenu("triggerContextMenu", [contextmenu ])
      index.php (line 131)
      fire()yahoo-dom-event.j... (line 80)
      _onTriggerContextMenu(contextmenu , ContextMenu goalcontextmenu
      lazyLoad=true itemData=[4] _aItemGroups=[0])menu.js (line 7871)
      wrappedFn(contextmenu )event.js (line 817)
      [Break on this error] this.getItem(0).cfg.setProperty
      ("disabled",true);

      The basis for the code is from treeviewcontextmenu.html example.

      function onTriggerGoalContextMenu(p_oEvent,p_oMenu){
      // if not an administrator need to disable menu items (may require
      some items to remain accessible for future options)
      var administrator=false;// test case
      if(!administrator){
      // disable context menu items
      this.getItem(0).cfg.setProperty("disabled",true);
      this.getItem(1).cfg.setProperty("disabled",true);
      this.getItem(2).cfg.setProperty("disabled",true);
      this.getItem(3).cfg.setProperty("disabled",true);
      }
      // Returns a TextNode instance that corresponds to the DOM
      element whose "contextmenu" event triggered the display of the
      context menu.
      function getTextNodeFromEventTarget(p_oTarget){
      if(p_oTarget.tagName.toUpperCase()=="A" &&
      p_oTarget.className=="ygtvlabel")
      return oGoal.nodeMap[p_oTarget.id];
      else if(p_oTarget.parentNode)
      return getTextNodeFromEventTarget
      (p_oTarget.parentNode);
      }
      var oTextNode=getTextNodeFromEventTarget
      (this.contextEventTarget);
      if(oTextNode){
      // set current node
      oGoal.currentNode=oTextNode;
      }
      else this.cancel();
      }
      oGoalContextMenu=new YAHOO.widget.ContextMenu(
      "goalcontextmenu",
      {
      trigger: "<?php echo $divGoalTree; ?>",
      itemdata: [
      {text:"Create child" , onclick:{fn:
      addNode}},
      {text:"Create sibling", onclick:{fn:
      addNode}},
      {text:"Edit goal" , onclick:{fn:
      editNode}},
      {text:"Delete goal" , onclick:{fn:
      deleteNode}}
      ],
      lazyload: true,
      effect:
      {effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25}

      }
      );
      // Add a "render" event handler
      oGoalContextMenu.triggerContextMenuEvent.subscribe
      (onTriggerGoalContextMenu,oGoalContextMenu,true);

      I am hoping an experienced eye will be able to see the problem
      easily. I have searched for hours and have posted this only as a
      last resort. Any suggestions would be greatly appreciated. Thanks
      in advance - Joe
    • Joseph F. Douglas
      Workaround received: Thanks Frank, you helped me understand the process much better. I m fairly new at this and while exciting it is proving frustrating as
      Message 2 of 4 , Apr 26, 2007
      • 0 Attachment
        Workaround received:
        Thanks Frank,
        you helped me understand the process much better. I'm fairly new at
        this and while exciting it is proving frustrating as well. I noticed
        that there are no examples of lazyload set to false and when I tried
        setting this to false in the treeviewcontextmenu.html example it
        stopped working. However, lazyload set to false does populate the
        _aItemGroups array which is needed for getItem(0) to work.

        However, your workaround worked a treat.

        // if not inialised then update itemData which is
        used to create the context item items
        if(this.getItems().length==0){
        this.itemData[0].disabled=!administrator;
        }else{
        // disable context menu items
        this.getItem(0).cfg.setProperty("disabled",!
        administrator);
        }

        Many thanks and if I find a different solution I'll be sure to let
        you know, - Joe

        ----- Original Message ----
        From: Frank
        I'm struggling with the same problem at the moment. As far as I've
        been able to figure it, the items aren't initialized the first time
        the "onTriggerGoalContextMenu" function is called. My workaround
        right now is to examine the itemData directly when getItems() returns
        nothing, e.g.

        if(this.getItems().size() == 0) {
        itemData[0].disabled = !administrator;
        ...
        }
        else {
        this.getItem(0).cfg.setProperty("disabled",!administrator);
        ...
        }

        I'd be more than happy to hear that there's an easier way, though.
        Setting lazy-load to false doesn't work for me.

        Hope this helps,
        Frank
      • Frank Steidinger
        After some additional experiments I ve come up with a better solution that doesn t require manipulating the itemData directly. If you move the enable/disable
        Message 3 of 4 , Apr 27, 2007
        • 0 Attachment
          After some additional experiments I've come up with a better solution
          that doesn't require manipulating the itemData directly. If you move
          the enable/disable code into a beforeShowEvent-Listener you can rely
          on the getItems() method returning the right items regardless of
          lazyload:

          myContextMenu.beforeShowEvent.subscribe(function(args, menu) {
          if(this.getItems().size() > 0) {
          this.getItems().each(function(item) {
          item.cfg.setProperty("disabled",
          !administrator);
          })
          }
          }, myContextMenu, true);

          The good thing about this is that you don't have to know if your menu
          items are organized within menu groups. So far it is working fine in
          Firefox and IE (unlike my previous workaround ;-) )

          --- In ydn-javascript@yahoogroups.com, "Joseph F.
          Douglas" <jfdouglas2004@...> wrote:
          >
          > Workaround received:
          > Thanks Frank,
          > you helped me understand the process much better. I'm fairly new
          at
          > this and while exciting it is proving frustrating as well. I
          noticed
          > that there are no examples of lazyload set to false and when I
          tried
          > setting this to false in the treeviewcontextmenu.html example it
          > stopped working. However, lazyload set to false does populate the
          > _aItemGroups array which is needed for getItem(0) to work.
          >
          > However, your workaround worked a treat.
          >
          > // if not inialised then update itemData which is
          > used to create the context item items
          > if(this.getItems().length==0){
          > this.itemData[0].disabled=!administrator;
          > }else{
          > // disable context menu items
          > this.getItem(0).cfg.setProperty("disabled",!
          > administrator);
          > }
          >
          > Many thanks and if I find a different solution I'll be sure to let
          > you know, - Joe
          >
          > ----- Original Message ----
          > From: Frank
          > I'm struggling with the same problem at the moment. As far as I've
          > been able to figure it, the items aren't initialized the first time
          > the "onTriggerGoalContextMenu" function is called. My workaround
          > right now is to examine the itemData directly when getItems()
          returns
          > nothing, e.g.
          >
          > if(this.getItems().size() == 0) {
          > itemData[0].disabled = !administrator;
          > ...
          > }
          > else {
          > this.getItem(0).cfg.setProperty("disabled",!administrator);
          > ...
          > }
          >
          > I'd be more than happy to hear that there's an easier way, though.
          > Setting lazy-load to false doesn't work for me.
          >
          > Hope this helps,
          > Frank
          >
        • Joseph F. Douglas
          Thanks Frank, I m glad you had this problem when I needed a solution. I m sure this will help alot of people.
          Message 4 of 4 , Apr 29, 2007
          • 0 Attachment
            Thanks Frank, I'm glad you had this problem when I needed a solution.
            I'm sure this will help alot of people.
          Your message has been successfully submitted and would be delivered to recipients shortly.