namespace SDK {
class
Option {
value: number;
text?: string;
showOptions?: Xrm.OptionSetValue[]
}
class
Mapping {
parent: string;
dependent: string;
options: Option[]
}
export
class
DependentOptionSet {
static
config: Mapping[] = [];
static
init(webResourceName : string) {
var
clientURL = Xrm.Page.context.getClientUrl();
var
pathToWR = clientURL +
"/WebResources/"
+ webResourceName;
var
xhr =
new
XMLHttpRequest();
xhr.open(
"GET"
, pathToWR,
true
);
xhr.setRequestHeader(
"Content-Type"
,
"text/xml"
);
xhr.onreadystatechange =
function
() { SDK.DependentOptionSet.completeInitialization(xhr); };
xhr.send();
}
static
completeInitialization(xhr : XMLHttpRequest) {
if
(xhr.readyState == 4
) {
if
(xhr.status == 200) {
xhr.onreadystatechange =
null
;
var
JSConfig: Mapping[] = [];
var
ParentFields = xhr.responseXML.documentElement.getElementsByTagName(
"ParentField"
);
for
(
var
i = 0; i < ParentFields.length; i++) {
var
ParentField = ParentFields[i];
var
mapping : Mapping = {
parent : ParentField.getAttribute(
"id"
),
dependent : SDK.Util.selectSingleNode(ParentField,
"DependentField"
).getAttribute(
"id"
),
options : []
};
var
options = SDK.Util.selectNodes(ParentField,
"Option"
);
for
(
var
a = 0; a < options.length; a++) {
var
option : Option = {
value: parseInt(options[a].getAttribute(
"value"
)),
showOptions: [],
};
var
optionsToShow = SDK.Util.selectNodes(options[a],
"ShowOption"
);
for
(
var
b = 0; b < optionsToShow.length; b++) {
var
optionToShow : Xrm.OptionSetValue = {
value: parseInt(optionsToShow[b].getAttribute(
"value"
)),
text: optionsToShow[b].getAttribute(
"label"
)
};
option.showOptions.push(optionToShow)
}
mapping.options.push(option);
}
JSConfig.push(mapping);
}
SDK.DependentOptionSet.config = JSConfig;
for
(
var
depOptionSet
in
SDK.DependentOptionSet.config) {
var
parent = SDK.DependentOptionSet.config[depOptionSet].parent;
Xrm.Page.getAttribute(parent).fireOnChange();
}
}
}
}
static
filterDependentField(parentField: string, childField : string) {
for
(
var
depOptionSet
in
SDK.DependentOptionSet.config) {
var
DependentOptionSet = SDK.DependentOptionSet.config[depOptionSet];
if
((DependentOptionSet.parent == parentField) && (DependentOptionSet.dependent == childField)) {
var
ParentField = Xrm.Page.getAttribute<Xrm.Attributes.OptionSetAttribute>(parentField);
var
ChildField = Xrm.Page.getAttribute<Xrm.Attributes.OptionSetAttribute>(childField);
var
CurrentChildFieldValue = ChildField.getValue();
if
(ParentField.getValue() ==
null
) {
ChildField.setValue(
null
);
ChildField.setSubmitMode(
"always"
);
ChildField.fireOnChange();
var
controls = ChildField.controls.get()
for
(
var
ctrl
in
controls) {
controls[ctrl].setDisabled(
true
);
}
return
;
}
for
(
var
os
in
DependentOptionSet.options) {
var
Options = DependentOptionSet.options[os];
var
optionsToShow = Options.showOptions;
if
(ParentField.getValue() == Options.value) {
var
controls = ChildField.controls.get();
for
(
var
ctrl
in
controls) {
controls[ctrl].setDisabled(
false
);
controls[ctrl].clearOptions();
for
(
var
option
in
optionsToShow) {
controls[ctrl].addOption(optionsToShow[option]);
}
}
var
bCurrentValueIsValid =
false
;
var
ChildFieldOptions = optionsToShow;
for
(
var
validOptionIndex
in
ChildFieldOptions) {
var
OptionDataValue = ChildFieldOptions[validOptionIndex].value;
if
(CurrentChildFieldValue == OptionDataValue) {
bCurrentValueIsValid =
true
;
break
;
}
}
if
(bCurrentValueIsValid) {
ChildField.setValue(CurrentChildFieldValue);
}
else
{
ChildField.setValue(
null
);
}
ChildField.setSubmitMode(
"always"
);
ChildField.fireOnChange();
break
;
}
}
}
}
}
}
export
class
Util {
static
selectSingleNode(node: Element, elementName : string) {
if
((<any>node).selectSingleNode) {
return
<Element>(<any>node).selectSingleNode(elementName);
}
else
{
return
node.getElementsByTagName(elementName)[0];
}
}
static
selectNodes(node: Element, elementName : string) {
if
((<any>node).selectNodes) {
return
<NodeListOf<Element>>(<any>node).selectNodes(elementName);
}
else
{
return
node.getElementsByTagName(elementName);
}
}
}
}