PS: I'm not sure how familiar you are with the modern toolkit, but in case you're not, just to explain that pervious comment a bit further:
In the legacy toolkit we build the menus just-in-time in onrightclick.
In the modern toolkit the menus are pre-built, but each item has a condition attached to it that is run just-in-time to determine whether or not to display it.
PS: I'm not sure how familiar you are with the modern toolkit, but in case you're not, just to explain that pervious comment a bit further:
In the legacy toolkit we build the menus just-in-time in onrightclick.
In the modern toolkit the menus are pre-built, but each item has a condition attached to it that is run just-in-time to determine whether or not to display it.