Friday, August 1, 2014

Building an XrmForm


If you followed my first post you probably noticed that XrmForm plays an important and centralized role in the code.

While writing another interesting post about building dynamic FetchXml it came to me that repeating the same code samples each time is cluttering,
so I decided to write a post that is all about XrmForm and refer to it in Future articles whenever it’s in use.

This post is a work in progress. More methods will be added as more code samples become available.

//companyname_sdk.js web resource
var xrmPage = Xrm.Page;
var data = xrmPage.data;
var ui = xrmPage.ui;
var context = xrmPage.context;
var nav = ui && ui.navigation;
var formSelector = ui && ui.formSelector;

function XrmForm() {
    var xfrm = this,
        EMPTY_GUID = "{00000000-0000-0000-0000-000000000000}",
        userAgent = window.navigator.userAgent;

    xfrm.IsIE = userAgent.indexOf("MSIE") != -1;
    xfrm.IsChrome = userAgent.indexOf("Chrome") != -1;
    //get a query string value by param name
    xfrm.GetQSParam = function(name) {
        var p, aq = location.search.substring(1).split('&'),
            ap = [];
        for (var i = 0; i < aq.length; i++) {
            p = aq[i].split('=');
            ap[p[0]] = decodeURIComponent(p[1]);
        }
        return ap[name];
    }
    //fit window to screen or supplied width and height 
    xfrm.ResizeTo = xfrm.FitToWindow = function() {
        window.resizeTo(arguments[0] || screen.availWidth, 
                        arguments[1] || screen.availHeight);
        return xfrm;
    }
    //apply a command on all attributes
    xfrm.All = function(sMethod, oValue) {
        var attrs, func;
        if (!data) return;

        attrs = data.entity.attributes;
        func = typeof(oValue) != "undefined" 
           ? function(attribute, index) {
                 attribute[sMethod](oValue);
             } 
           : function(attribute, index) {
                 attribute[sMethod]();
           };

        attrs.forEach(func);
    }
    
    xfrm.IsDirty = function() {
        return data && data.entity.getIsDirty();
    }
    
    xfrm.GetUserId = function() {
        return context.getUserId();
    }
         
    xfrm.GetCrmUrl =
    xfrm.GetClientUrl = 
    xfrm.GetServerUrl = function() {
        return context.getClientUrl 
            ? context.getClientUrl() + "/"
            : context.getServerUrl();
    }
    
    xfrm.GetUserLcid = function() {
        return context.getUserLcid();
    }
    
    xfrm.GetUserRoles = function() {
        return context.getUserRoles();
    }
    
    xfrm.GetId = xfrm.GetRecordId = function() {
        return data && data.entity.getId() || EMPTY_GUID;
    }
    
    xfrm.Save = function() {
        data && data.entity.save(arguments[0] || null);
    }
    
    xfrm.SaveAndClose = function() {
        xfrm.Save("saveandclose");
    }
    
    xfrm.SaveAndNew = function() {
        xfrm.Save("saveandnew");
    }
    
    xfrm.HideRecordSet = function() {
        $('#recordSetToolBar').hide();
        return xfrm;
    }
    
    xfrm.ShowRecordSet = function() {
        $('#recordSetToolBar').show();
        return xfrm;
    }
    // close form , bSuppress - true to avoid dirty check
    xfrm.Close = function(bSuppress) {
        bSuppress && xfrm.All("setSubmitMode", "never");
        ui && ui.close();
    }
    
    frm.HideNavigation = function () {
        $('#crmNavBar').parent()
            .css("left", "0.1px")
            .hide()
            .prev()
            .css({ left: "0px", right: "0px" });
        return xfrm;
    }
    //entity name e.g. account, contact
    xfrm.EntityName = data && data.entity.getEntityName();
    //form type e.g. CREATE, UPDATE ...
    xfrm.Type = ui && ui.getFormType();
    
    /*
       SDK Additions: Controling Navigation items.
       Require: XrmNavItem wrapper object
       see: http://totbcrm.blogspot.co.il/2014/08/coding-navigation-items.html
       for more details.
    */

    //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(); 
Possible Usage
//example 1:
xrmForm.HideRecordSet().HideNavigation();
//example 2:
if (xrmForm.IsDirty())
    xrmForm.SaveAndClose();
//example 3:
alert(xrmForm.Type)
Yes! this is just the basics but you can clearly see that utilization is short and simple. You may also notice how the code reuses it own methods like in the xrmForm.Close method. Cheers

No comments:

Post a Comment