SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/template/srd-question-directives.js

353 lines
17 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('srdModule')
.filter('safeHtml', ['$sce', function ($sce) { return $sce.trustAsHtml; }])
.directive('srdTextQuestion', ['$timeout', function ($timeout) {
return {
restrict: 'AE',
replace: true,
scope: {
data: '=',
containerClass: '@'
},
templateUrl: 'views/template/srd-questions/text.html',
link: function (scope, element) {
var oldValue = scope.data.answer;
if (scope.data.validationExpression) {
scope.data.validationExpression = eval(scope.data.validationExpression); // NOSONAR
}
function keyUpHandler() {
var limit = scope.data.numLines;
if ($.trim(scope.data.answer) !== '') {
scope.data.hasAnswer = true;
}
else {
scope.data.hasAnswer = false;
}
if (limit) {
var value = scope.data.answer;
if (value !== undefined) {
var rows = value.split('\n').length;
if (rows > limit) {
scope.data.answer = oldValue;
$timeout(function () {
scope.$apply();
});
}
else {
oldValue = scope.data.answer;
}
}
}
}
element.bind('keyup', keyUpHandler);
scope.$on('$destroy', function () {
element.off('keyup', keyUpHandler);
});
}
};
}]).directive('srdRadioQuestion', ['$timeout', function ($timeout) {
return {
restrict: 'AE',
replace: true,
scope: {
data: '=',
containerClass: '@'
},
templateUrl: 'views/template/srd-questions/radio.html',
link: function (scope, element) {
scope.dataSelect = function (option) {
if (option) {
scope.data.answer = option.value;
scope.data.answerLabel = option.label;
}
else {
scope.data.answer = '';
scope.data.answerLabel = '';
}
$timeout(function () {
scope.$apply();
scope.processChildControlByCondition();
});
};
scope.processChildControlByCondition = function () {
var context = element.closest('.srd-questions-container');
function searchControlTree(parentId) {
var childElements = $('[data-parent-name="' + parentId + '"]', context), currentValue = null, parentElm = angular.element($('[data-id="' + parentId + '"]', context).find('select'));
if (!parentElm.length) {
parentElm = angular.element($('[data-id="' + parentId + '"]', context).find('input'));
}
if (parentElm.length) {
var parentScopeData = (parentElm.scope().data) ? parentElm.scope().data : parentElm.scope().question;
var parentAnswer = parentScopeData.answer;
parentScopeData.hasAnswer = !!(parentAnswer !== null && typeof parentAnswer !== 'undefined' && parentAnswer !== '');
if (parentElm.attr('multiple') === 'multiple') {
currentValue = (parentElm.val()) ? parentElm.val().sort().join(';') : null;
}
else {
currentValue = parentAnswer;
}
$.each(childElements, function (index, elm) {
var values = $(elm).data('condition-values').toString().split(';'), elmScopeData = ($(this).scope().data) ? $(this).scope().data : $(this).scope().question, answer = elmScopeData.answer, curElmId = null;
//$('option:selected', elm).prop('selected', false).trigger('change');
if ($.inArray(currentValue, values) !== -1 && parentScopeData.visibility) {
if (!elmScopeData.isHidden) {
elmScopeData.visibility = true;
}
elmScopeData.hasAnswer = !!(answer !== null && typeof answer !== 'undefined' && answer !== '');
if (!elmScopeData.hasAnswer && elmScopeData.defaultValue) {
elmScopeData.answer = elmScopeData.defaultValue;
}
}
else {
elmScopeData.visibility = false;
elmScopeData.hasAnswer = false;
elmScopeData.answer = null;
elmScopeData.answerLabel = '';
}
curElmId = $('select', elm).attr('name');
if (!curElmId) {
curElmId = $('input', elm).attr('name');
}
if (curElmId) {
searchControlTree(curElmId);
}
});
}
}
searchControlTree(scope.data.id);
};
}
};
}]).directive('srdCheckboxQuestion', function () {
// multi-select question is not supported by SRM
return {
restrict: 'AE',
replace: true,
scope: {
data: '='
},
templateUrl: 'views/template/srd-questions/checkbox.html',
link: function (scope, element) {
scope.updateAnswer = function (option) {
option.selected = !option.selected;
if (option.selected) {
scope.data.answer.push(option.value);
}
else {
_.remove(scope.data.answer, function (item) {
return item === option.value;
});
}
scope.data.hasAnswer = !_.isEmpty(scope.data.answer);
scope.data.currentValue = (scope.data.answer) ? scope.data.answer.sort().join(';') : null;
var context = element.closest('.srd-questions-container');
function searchControlTree(parentId) {
var childElements = $('[data-parent-name="' + parentId + '"]', context), currentValue = null, parentElm = angular.element($('[data-id="' + parentId + '"]', context).find('select'));
if (!parentElm.length) {
parentElm = angular.element($('[data-id="' + parentId + '"]', context).find('input'));
}
if (parentElm.length) {
var parentScopeData = (parentElm.scope().data) ? parentElm.scope().data : parentElm.scope().question;
var parentAnswer = parentScopeData.answer ? parentScopeData.answer : [];
parentScopeData.hasAnswer = !!(parentAnswer !== null && typeof parentAnswer !== 'undefined' && parentAnswer !== '' && parentAnswer.length > 0);
if (parentScopeData.hasAnswer) {
currentValue = parentAnswer && parentAnswer.join(';');
}
$.each(childElements, function (index, elm) {
var values = $(elm).data('condition-values').toString().split(';').sort().join(';'), elmScopeData = ($(this).scope().data) ? $(this).scope().data : $(this).scope().question, answer = elmScopeData.answer, curElmId = null;
if ((currentValue === values) && parentScopeData.visibility) {
if (!elmScopeData.isHidden) {
elmScopeData.visibility = true;
}
elmScopeData.hasAnswer = !!(answer !== null && typeof answer !== 'undefined' && answer !== '');
}
else {
elmScopeData.visibility = false;
elmScopeData.hasAnswer = false;
if (elmScopeData.format === 'CHECK_BOXES') {
elmScopeData.answer = [];
}
else {
elmScopeData.answer = null;
}
elmScopeData.answerLabel = '';
}
curElmId = $('select', elm).attr('name');
if (!curElmId) {
curElmId = $('input', elm).attr('name');
}
if (curElmId) {
searchControlTree(curElmId);
}
});
}
}
searchControlTree(scope.data.id);
};
if (scope.data.answer && scope.data.answer.length) {
var copiedAnswers = _.cloneDeep(scope.data.answer);
scope.data.answer = [];
_.forEach(copiedAnswers, function (answerValue) {
var option = _.find(scope.data.options, { 'value': answerValue });
if (option) {
scope.updateAnswer(option);
}
});
}
else if (!scope.data.prepopulate) {
_.forEach(scope.data.options, function (option) {
if (option.isDefault) {
scope.updateAnswer(option);
}
});
}
}
};
}).directive('srdNumberQuestion', function () {
return {
restrict: 'AE',
replace: true,
scope: {
data: '=',
containerClass: '@'
},
templateUrl: 'views/template/srd-questions/number.html',
link: function (scope, element) {
element.bind('keyup', function () {
if ($.trim(scope.data.answer) !== '') {
scope.data.hasAnswer = true;
}
else {
scope.data.hasAnswer = false;
}
});
element.bind('mouseup', function () {
if ($.trim(scope.data.answer) !== '') {
scope.data.hasAnswer = true;
}
else {
scope.data.hasAnswer = false;
}
});
}
};
}).directive('srdDateQuestion', ['configurationModel', function (configurationModel) {
return {
restrict: 'AE',
replace: true,
scope: {
data: '=',
containerClass: '@'
},
templateUrl: 'views/template/srd-questions/date.html',
link: function (scope) {
scope.status = {
opened: false
};
scope.open = function () {
scope.status.opened = true;
};
scope.servReqDatePickerOptions = {
startingDay: configurationModel.getWeekStartingDay(),
'show-weeks': false
};
}
};
}]).directive('srdTimeQuestion', function () {
return {
restrict: 'AE',
replace: true,
scope: {
data: '=',
containerClass: '@'
},
templateUrl: 'views/template/srd-questions/time.html',
link: function (scope) {
scope.showMeridian = window.showMeridian;
}
};
}).directive('srdDateTimeQuestion', ['configurationModel', function (configurationModel) {
return {
restrict: 'AE',
replace: true,
scope: {
data: '=',
containerClass: '@'
},
templateUrl: 'views/template/srd-questions/datetime.html',
link: function (scope) {
scope.showMeridian = window.showMeridian;
scope.status = {
opened: false
};
scope.open = function () {
scope.status.opened = true;
};
scope.servReqDatePickerOptions = {
startingDay: configurationModel.getWeekStartingDay(),
'show-weeks': false
};
scope.updateDate = function () {
if (!scope.data.answer.time && scope.data.answer.date) {
scope.data.answer.time = _.cloneDeep(scope.data.answer.date);
}
};
}
};
}]).directive('checkFormValidity', function () {
// hack for input[type="number"] (srdQuestionNumber) under Chrome,
// which does not consider non-numeric input to be invalid
return {
require: 'ngModel',
link: function ($scope, $element, $attrs, modelCtrl) {
if (typeof $element.prop('validity') === 'undefined') {
return;
}
$element.bind('input', function () {
var validity = $element.prop('validity');
$scope.$apply(function () {
modelCtrl.$setValidity('badInput', !validity.badInput);
});
});
}
};
}).directive('srdQueryMenuQuestion', ['ticketTemplateModel',
function (ticketTemplateModel) {
return {
restrict: 'AE',
replace: true,
scope: {
data: '=',
questions: '=',
containerClass: '@'
},
templateUrl: 'views/template/srd-questions/query-menu.html',
link: function (scope, element) {
element.bind('change', function () {
if (scope.data.answer !== '') {
scope.data.hasAnswer = true;
}
else {
scope.data.hasAnswer = false;
}
});
scope.updateDynamicQuestions = function () {
ticketTemplateModel.updateDynamicQuestions(scope.data, scope.questions);
};
scope.dataSelect = function (option) {
if (option) {
scope.data.answer = option.value;
scope.data.answerLabel = option.label;
}
else {
scope.data.answer = '';
scope.data.answerLabel = '';
}
};
}
};
}]);
}());