Adding Items to the Navigator Menu

During the course of removing the modifications from IFL I wanted a way to run some of the C# developments directly so I thought I’d do some investigation.

Using the MenuExtensionService we can add new entries to the Navigator menu, the key being that we need to force a refresh and then we need a way to do this when Smart Office itself launches.

I created a little script which will read an xml file which contains the name to appear in the menu and the URI I want to launch and add these to the M3 Navigator. This script can be run on Smart Offices startup.

For the launching of Smart Office and running a script I referred to Karins post here:
http://smartofficeblog.com/2014/01/21/controlling-the-users-canvas-with-stand-alone-jscripts/

Which for me became:

http://ifsonp.indfish.co.nz:22107/mango/MangoClient.application?server=https://IFSONP.indfish.co.nz:22108&task=mforms%3a%2f%2f_runscript%3fname%3dtestLoadMenu.js

http://ifsonp.indfish.co.nz:22107 and https://IFSONP.indfish.co.nz:22108 is IFLs Smart Office server, these should be substituted with your own. testLoadMenu.js is the name of my Jscript.

Note: when you run a script in the command line the controller object won’t be initialised, so you need to comment out the code line the Jscript editor adds by default:

var content : Object = controller.RenderEngine.Content;

Anyway, the script is pretty simple, and not too hard to follow so…

 

/*
**	Load Menu
**
**	This script will read an xml file and create menus in the navigator
*/
import System;
import System.Windows;
import System.Windows.Controls;
import MForms;

import System.Xml;
import System.Xml.XPath;
import System.Xml.Serialization;

import System.IO;

import Mango.UI.Core;
import Mango.UI.Services;

package MForms.JScript
{
	class testLoadMenu
	{
		// path to the menus
		var xmlfile = "D:\\Development\\JScript\\Menus.xml";
		
		public function Init(element: Object, args: Object, controller : Object, debug : Object)
		{
			try
			{
				// var content : Object = controller.RenderEngine.Content;
				
				// I am reading a file off the filesystem, in a production environment you wouldn't
				// do this, you'd have the URI in the initialisation of the XPathDocument
				var stream = new StreamReader(xmlfile);
				
				var doc  : XPathDocument = new XPathDocument(stream);
				var nav  : XPathNavigator = doc.CreateNavigator();

				// get a list of MenuItems 
				var menuItems = nav.Select("//MenuItem");
				
				if(null != menuItems)
				{
					// loop through our list of menu items
					for(var currentItem in menuItems)
					{
						// extract the Menu Name
						var menuName = currentItem.Evaluate("string(Name)");
						// extract the URI
						var menuURI = currentItem.Evaluate("string(URI)");

						// add to the navigator
						MenuExtensionService.Current.AddNode(new Task(new Uri(menuURI.ToString()), menuName.ToString()));
					}

				}
				stream.Close();
				
				// this will cause the navigator to refresh
				TasksService.Invalidate();
			}
			catch(ex)
			{
				debug.Error(ex);
			}
		}
	}
}

And an example XML file
 

<?xml version="1.0" encoding="UTF-8"?>
<Menu>
	<MenuItem>
		<Name>JScript</Name>
		<URI>mforms://jscript</URI>
	</MenuItem>
	<MenuItem>
		<Name>Clear Cache</Name>
		<URI>mforms://jscript/clearcache</URI>
	</MenuItem>
</Menu>
This entry was posted in Development, M3 / MoveX. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s