Skip to content

Commit

Permalink
Refactored .Mandatory function.
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsteneckel committed Feb 10, 2016
1 parent 672d294 commit 3d97bdd
Showing 1 changed file with 35 additions and 25 deletions.
60 changes: 35 additions & 25 deletions var/httpd/htdocs/js/Core.Form.Znuny4OTRSInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -777,48 +777,58 @@ Core.Form.Znuny4OTRSInput = (function (TargetNS) {

/*
Manipulates the field to mandatory or optional field (currently only dynamic field support).
Manipulates the field to mandatory or optional field.
var Result = Core.Form.Znuny4OTRSInput.Mandatory('DynamicField_test');
var Result = Core.Form.Znuny4OTRSInput.Mandatory('DynamicField_test', { Mandatory: false });
var Result = Core.Form.Znuny4OTRSInput.Mandatory('DynamicField_Example', true);
var Result = Core.Form.Znuny4OTRSInput.Mandatory('DynamicField_Example', false);
Returns:
Result = true
Or:
var CurrentState = Core.Form.Znuny4OTRSInput.Mandatory('DynamicField_Example');
Returns:
CurrentState = true|false
*/
TargetNS.Mandatory = function ( Attribute, Options ) {
Options = Options || {};
TargetNS.Mandatory = function ( Attribute, Mandatory ) {

var FieldID = TargetNS.FieldID( Attribute );
var Mandatory = true;
if ( Options['Mandatory'] === false ) {
Mandatory = false;
}
var IsMandatory;
var $LabelObject;
var FieldID = TargetNS.FieldID( Attribute );

if ( !FieldID ) {
if (!FieldID) {
return false;
}

if ( FieldID.match(/DynamicField_/) ) {
$LabelObject = $('[for="'+FieldID+'"]');

var Input = $('#' + FieldID);
var Field = $('#Label' + FieldID);
IsMandatory = $LabelObject.hasClass('Mandatory');

if ( Mandatory && !Field.hasClass('Mandatory') ) {
Field.addClass('Mandatory');
Field.prepend('<span class="Marker">*</span>');
Input.addClass('Validate_Required');
}
else if ( !Mandatory && Field.hasClass('Mandatory') ) {
Field.removeClass('Mandatory');
Field.find('.Marker').remove();
Input.removeClass('Validate_Required');
}
if (typeof Mandatory === 'undefined') {
return IsMandatory;
}

if (Mandatory === IsMandatory) {
return true;
}

return false;
if (IsMandatory) {
$LabelObject.removeClass('Mandatory');
$LabelObject.find('.Marker').remove();
$('#'+FieldID).removeClass('Validate_Required');
}
else {
$LabelObject.addClass('Mandatory');
$LabelObject.prepend('<span class="Marker">*</span>');
$('#'+FieldID).addClass('Validate_Required');
}

return true;
}

/*
Expand Down

0 comments on commit 3d97bdd

Please sign in to comment.