Thursday, October 16, 2014

Grails platform-core plugin navigation menu

Took on the responsibility to develop a new application lately and decided to use grails platform-core navigation plugin to implement the 2 level menu I had in mind.

A quick look at the online documentation of the platform-core navigation plugin made me think that it's a 20 minutes job. Started by adding the "compile ":platform-core:1.0.0"" into BuildConfig.groovy, and  adding the <nav:primary/> & <nav:secondary/> tags into main.gsp,  then run "grail run-app". hooray, it worked. All the controller showed up as primary menu items, and their actioned showed up as secondary menu items.  Simple enough!

But here comes the headache, I need to customize the menu so it doesn't show all my controllers & actions, and also need to have some user friendly names for my menu items. So following the online manual, I added "AppNavigation.groovy" file and the navigation closure. But it doesn't work! Adding the following causing grails to generate 2 menu items in the 1st level menu for the same controller(say if I have a controller JanProject) -
           Project(controller:"janProject")
If you think you can remove the duplicate menu item by setting the navigationScope for the JanProject controller to a wildly different name than "app", then you will be surprised to see that when you click on the controller in the primary menu, you get forwarded to the automatically grails generated menu again, with all the default controllers & action names.

After many many tries based on the Github navigation DSL sample online, things just got worse. At one point, I started to suspect that this 1.0.0 version in fact doesn't work at all. But switching back to 1.0.RC3 as someone's instruction online, ended up with connection time out error, because the 1.0.RC3 package can no longer be found?

Then started to google like a mad woman trying to find some online successful samples people can provide for a 2 level menu.  The best I could find is a one level menu. Seems that nobody has posted their success story!

Desperate, I downloaded the source code of the platform-core plugin((which I should have done in the first place, but don't we all like shortcuts?) , the "grails-platform-core-master.zip" file. And read the NavigationImpl.groovy, which parses different types of DSL blocks. Then turned on the debug for this plugin. After tweaking the AppNavigation.groovy 20+ times, run-app 20+ times and checking the debug logs of how NavigationImpl processes it, I finally figured out how this stuff works -
1.  if you define a controller with the same name as your controller, grails will not generate a duplicate one for you.
2. If you want to customize your controller name, use titleText
3. instead of using "visible", all you have to do is to provide a list of actions you want to have menu items created.
4.the navigation scope(say, "abc") defined in your controller will cause the menu flow go into that scope (in this case, "abc"), instead of the default "app".  I know, this sounds very basic, but it's certainly not very obvious when it first started, and certainly takes trial and error to figure out.

So there you go. A 20 minutes' plan turned into a 2 day struggle (partly my fault, why didn't I download the plugin source code earlier?). Glad that I finally figured it out though, and now I can create and customize my grails menu in 2 minutes :)


No comments:

Post a Comment