Friday, November 21, 2008

Change Cases Associated Default View

This is a complementary post to the “Changing Activity / History Default View”.
The code sample uses exactly the same concept as the former post so if you’re using that then you should integrate both solution to avoid duplicate functions.


//Case History statecode options
var ServiceOptions =
{
Active : "0",
Resolved : "1",
Canceled : "2",
All : "All"
}

var _loadarea = loadArea;
loadArea = function(sArea, sParams, sUrl, bIsvMode)
{
//load the iframe
_loadarea(sArea, sParams, sUrl, bIsvMode);

if( sArea != "areaService" ) return;

//create the iframe object
var iframe = document.getElementById(sArea + "Frame");
//wait until the iframe is fully loaded ("complete")
iframe.onreadystatechange = function()
{
if( iframe.readyState == "complete")
{
var picklist,option;
//reference to the iframe document
var iframeDoc = iframe.contentWindow.document;
switch(sArea)
{
case "areaService":
picklist = iframeDoc.all.statecode[0];
option = ServiceOptions.All;
break;
}
picklist.value = option;
picklist.FireOnChange();
}
}
}

Friday, November 14, 2008

Creating an UpDown Control

The updown control is merely a control that enables the user to select a number from a predefined range either by direct assignment or by using up and down selectors (arrows). This is usually very useful when the changing of the number has an immediate affect of some sort on the form where it’s placed. This is just a UI enhancement that should be used only where appropriate.

The idea behind the mechanics of the updown control is that it uses a span with an overflow-y: scroll. The span has an inner span which is set to hidden but its height is set to the updown max value. This hack actually creates the range for you. When the user scrolls down the scrollTop attribute reflects the current position (selected number) which is written back to the text field. An opposite approach is used when the users assigns a value directly, then the span scrollTop is updated with the current field datavalue.





/*
parameters: fieldId - crm field id
minValue - positive number
maxValue - positive number
*/
function UpDownControl( fieldId , minValue , maxValue )
{
var Instance = this;
/* crm field */
var upDownCtrlBox = crmForm.all[ fieldId ];
/* field parent element field_d */
var parent = upDownCtrlBox.parentElement;
/* this span affects the spanUpDown scroll */
var span = document.createElement( "<SPAN style='visibility:hidden;width:1;'>" );
var spanUpDownHeight = upDownCtrlBox.clientHeight - 2;
/* this span is the scroller */
var spanUpDown = document.createElement( "<SPAN style='margin-left:-18px;overflow-y:scroll;width:1;height:" + spanUpDownHeight + ";'>" );
/* max range */
Instance.Max = maxValue;
/* min range */
Instance.Min = minValue;
/* fires when the scroll is pressed */
function onScrollChange()
{
/* get the current scroll position */
var scrollTop = parseInt( spanUpDown.scrollTop );
/* adjust size if value is outside of the scroll limits */
if( scrollTop < Instance.Min ){
scrollTop = Instance.Min;
spanUpDown.scrollTop = scrollTop;
}
else if( scrollTop > Instance.Max ){
scrollTop = Instance.Max;
spanUpDown.scrollTop = scrollTop;
}
/* assign the scroll value back to the field */
upDownCtrlBox.DataValue = scrollTop;
}
/* fires when the field value changes */
function onValueChange()
{
/* if the value is valid */
if( upDownCtrlBox.DataValue != null )
{
/* get the value */
var dataValue = parseInt( upDownCtrlBox.DataValue );
/* check that the vlaue is within permitted limits */
if( dataValue < Instance.Min ){
dataValue = Instance.Min;
upDownCtrlBox.DataValue = dataValue;
return false;
}
else if( dataValue > Instance.Max ){
dataValue = Instance.Max;
upDownCtrlBox.DataValue = dataValue;
return false;
}
/* assign the field value back to the scroll */
spanUpDown.scrollTop = dataValue;
}
/* assign min value is the datavalue is null */
else spanUpDown.scrollTop = Instance.Min;
}

/* fire onValueChange when the field is changed */
upDownCtrlBox.attachEvent( "onchange" , onValueChange );
/* adjust inner span height = maxValue + spanHeight */
span.style.height = maxValue + spanUpDownHeight;
/* construct the updown control */
spanUpDown.appendChild( span );
/* fire onScrollChange when the user presses the scrollbar */
spanUpDown.attachEvent( "onscroll" , onScrollChange );
parent.appendChild( spanUpDown );
/* call onValueChange when the form loads / control is created */
onValueChange();
}

widthUpDownCtrl = new UpDownControl("gi_width" , 20 , 10000 );

Thursday, November 13, 2008

Creating a Dynamic Picklist Control

There are situations where you need to create a Picklist with values from an external source or just don’t want to keep a static list on the Picklist attribute. Initially I posted a simple solution which illustrates how to convert a text attribute into a Picklist.

This post offers a different and more robust solution to the creation of dynamic Picklists.
I call the control a TextList which obviously is a hybridization of a text and a Picklist fields.

The TextList also contains a Dependent Field Collection whose fields are bind to its onchange event. This way, each time the Picklist changes, the option properties are mapped to the form dependent fields. In the code sample bellow I’ve created 2 additional attributes on each option. The first is called Color and the second is called JustANubmer. When the user selects an option the color and number are written to two dependent fields (gi_dependentfield1 and gi_dependentfield2). The TextList also exposes other wrapper methods on the inner Picklist control so you won’t have to deal with two objects.

I’ve added comments which explain the methods functionality.


// JScript File
function OnCrmPageLoad()
{
/* Change gi_name text field to a textlist */
var nameTextList = new TextList( "gi_name" );
/* Set 2 dependent fields */
nameTextList.DependentFields.Add( "gi_dependentfield1" , "JustANumber" );
nameTextList.DependentFields.Add( "gi_dependentfield2" , "Color" );
/* Add blank option */
nameTextList.Add( "" , "" );
for( var i = 0 ; i < 6 ; i++ )
{
/* Fill some values */
var option = nameTextList.Add( "Text " + i , "Value " + i );
/* create 2 new attributes on each option (Color,JustANumber) */
option.Color = "A" + i + "B" + i + "C" + i;
option.JustANumber = i.toString();
}
/* Set the initial value */
nameTextList.SetInitValue(true);
/* Finish the job */
nameTextList.Transform();
}

/*
Holds a list of all dependent fields.
Each textlist has a dependent fields collection
*/
DependentFieldCollection = function()
{
this.List = [];
this.Add = function( name , prop ){
this.List.push( new DependentField( name , prop ) );
}
}
/*
Single Dependent Field.
The Object maps between an option attribute an a crmForm field
*/
DependentField = function( name , prop )
{
this.Property = prop;
this.Name = name;
}
/* Text list control */
TextList = function( txtId )
{
var textList = this;
/* Referech the original text field */
var textCtrl = crmForm.all[ txtId ];
if (!textCtrl) return alert("TextList: " + txtId + " Is Missing");

/* Create a new Picklist control */
textList.Picklist = document.createElement("SELECT");
textList.Picklist.id = textCtrl.id;
textList.Picklist.req = textCtrl.req;
textList.Picklist.tabIndex = textCtrl.tabIndex;
textList.Picklist.className = "ms-crm-SelectBox ";
textList.Picklist.defaultSelected = textCtrl.DataValue;
textList.DependentFields = new DependentFieldCollection();

/* Add the new picklist and remove the text control */
textCtrl.parentElement.appendChild( textList.Picklist );
textCtrl.parentElement.removeChild( textCtrl );

/*
Finish the textlist transformation
Create inline dependent fields properties on the textlist
Attach an update function to the picklist onchange event
*/
textList.Transform = function()
{
for( var i = 0;i < textList.DependentFields.List.length;i++ ){
var depField = textList.DependentFields.List[ i ];
textList[ depField.Name ] = crmForm.all[ depField.Name ];
}
textList.Picklist.attachEvent( "onchange" , UpdateDependentFields );
}
/* Sets the picklist value */
textList.SetValue = function( value )
{
textList.Picklist.DataValue = value;
textList.Picklist.FireOnChange();
}
/*
Sets the picklist initial value.
This should be called when the page load after you fill the picklist options
*/
textList.SetInitValue = function( fireEvent )
{
textList.Picklist.DataValue = textList.Picklist.defaultSelected;
if(fireEvent) textList.Picklist.FireOnChange();
}
/* Add a new picklist Option */
textList.Add = function( text , value )
{
return textList.Picklist.AddOption( text , value );
}
/* Remove a picklist Option by its value */
textList.Remove = function( value )
{
textList.Picklist.DeleteOption( value );
}
/* Clear all Picklist Options */
textList.Clear = function()
{
textList.Picklist.length = 0;
}
/* Helper property for attaching to an event from within the picklist control */
textList.Attach = function( targetId , eventName , callback )
{
var target = document.all[ targetId ];
if ( target ) target.attachEvent( eventName , callback );
}
/* Updates the dependent fields when the picklist onchange fires */
function UpdateDependentFields()
{
if ( textList.Picklist.selectedIndex > -1 )
for( var i = 0;i < textList.DependentFields.List.length;i++ )
{
var depField = textList.DependentFields.List[ i ];
textList[ depField.Name ].DataValue = textList.Picklist.SelectedOption[ depField.Property ];
textList[ depField.Name ].FireOnChange();
}
}
}


OnCrmPageLoad();