Saturday, August 2, 2014

Coding Navigation items

In my former posts I generally discussed about creating wrappers for CRM built-in objects and gave basic examples. I think the best way to make sense of it all is to provide a more concrete example.

Our "defendant", this time, will be the Navigation Item object.
I chose it mainly, since it has a small and simple API implementation.

Following are the steps we’re going to take to achieve our goal:
1. Create an object that receives a navigation item id or index and creates a wrapper.
2. Create matching methods for each built-in CRM method.
3. Add magic to our XrmForm so it carries the weight against our wrapper object.
This will make our coding against CRM navigation item even simpler.

Note: jquery library is required.

// navigation item wrapper object
function XrmNavItem(oNavId) {
    if (!oNavId)
        return;

    var xItem = this,
        oItem = nav && nav.items.get(oNavId);

    xItem.Exists = function() {return !!oItem;}
    if (!xItem.Exists()) return xItem;

    xItem.GetId = function() {return oItem.getId();}
    xItem.GetLabel = function() {return oItem.getLabel();}
    xItem.IsVisible = function() {return oItem.getVisible();}
    xItem.IsHidden = function() {return !xItem.IsVisible();}
    xItem.Focus = function() {oItem.setFocus();return xItem;}
    xItem.SetLabel = function(sName) {oItem.setLabel(sName);return xItem;}
    xItem.Hide = function() {oItem.setVisible(false);return xItem;}
    xItem.Show = function() {oItem.setVisible(true);return xItem;}
    xItem.toString = function() {return xItem.GetId();}
}

Note: The following is a partial implementation of XrmForm and contains methods only relevant to this post. For an updated XrmForm click here.

function XrmForm() {
    //...new additions, see complete XrmForm Implementation here
    
    //return multiple nav items
    xfrm.GetNavItems = function() {
        var aItems = [], i,
            args = $.IsArray(arguments[0]) 
              ? arguments[0] 
              : arguments;
        
        for(i = 0; i < args.length ; i++) {
            switch (typeof(args[i])) {
                case "string":
                case "number":
                    //find item by label or index
                    aItem.push(new XrmNavItem(args[i]));
                case "function":
                    //find item by custom function. 
                    nav && nav.items.get(function(item, index) {
                        var bResult = args[i](item, index);
                        if (bResult)
                            aItem.push(new XrmNavItem(item.getId()));
                        return bResult;
                    });
                    break;
            }
        }
        
        if (!i) {
            //get all navigation items
            nav && nav.items.get(function(item, index) {
                aItem.push(new XrmNavItem(item.getId()));
                return true;
            });
        }
        
        return aItem;
    }
    //return a single nav item
    xfrm.GetNavItem = function () { 
        return xfrm.GetNavItems(arguments[0])[0]; 
    }
    //hide item or items
    xfrm.HideNavItems = xfrm.HideNavItem = function() {
        private.applyCommand(
            xfrm.GetNavItems.apply(xfrm, arguments),
            'Hide');
        return xfrm;
    }
    //show item or items
    xfrm.ShowNavItems = xfrm.ShowNavItem = function() {
        private.applyCommand(
            xfrm.GetNavItems.apply(xfrm, arguments),
            'Show');
        return xfrm;
    }
    
    //display all nav item ids and names (for debugging)
    xfrm.DebugNavItems = function() {
        var items = xfrm.GetNavItems(),
            summary = "";
            
        for (var i in items)
            summary += items[i].GetLabel() + " - " + 
                       items[i].GetId() + "\n";
        alert(summary);
    }    
    
    var private = {
        applyCommand : function(args,command,param) {
            var item, i;
            for(i = 0 ; i < args.length ; i++) {
                item = args[i]; 
                item.Exists() && item[command](param);
            }   
        }
    }
}

var xrmForm = new XrmForm();
Some benefits of using the XrmForm concept and Wrapper objects are: 1. The method names are easy to remember. 2. The methods are straight forward and easy to code. 3. This type of coding keeps your business logic clean and understandable. Here are some usage examples.
//alert nav item names and ids
xrmForm.DebugNavItems();

//example 1 - hide all items 
xrmForm.HideNavItems();

//example 2 - show 3 item using item id or index
xrmForm.ShowNavItems("item1 id", 2 , "item3 id");

//example 3 - hide array containing 3 items
xrmForm.HideNavItems(["item1 id", 2 , "item3 id"]);

//example 4 - hide item 1,2 and show 3
xrmForm.HideNavItem("item1 id")
       .HideNavItem("Item2 id")
       .ShowNavItem(3) //by index

//example 5 - get item, if it's hidden show it.
var navItem1 = xrmForm.GetItemNav("item1 id");
navItem1.IsHidden() && navItem1.Show();
       
//example 6 - manipulate item 1
xrmForm.GetNavItem("item id 1")
       .SetLabel("new Lable for item 1")
       .Show()
       .Focus();
Cheers

No comments:

Post a Comment