"use strict";
(function () {
"use strict";
angular.module('myitsmApp')
.factory('utilityFunctions', [function () {
var utilityFunctions = {}, id = 1;
utilityFunctions.calculateConditionalQuestionVisibility = function (parentQuestion, question, skipHiddenQuestion) {
return (question.isHidden === !skipHiddenQuestion) && _.matchConditionValues(parentQuestion, question) && !parentQuestion.isHidden && parentQuestion.visibility;
};
utilityFunctions.matchConditionValues = function (parentQuestion, question) {
if (parentQuestion.format === 'RADIO_BUTTONS' || parentQuestion.format === 'STATIC_MENU') {
return question.conditionValues === parentQuestion.answer;
}
else if (parentQuestion.format === 'CHECK_BOXES' && parentQuestion.answer) {
return parentQuestion.answer.sort().join(';') === question.conditionValues.split(';').sort().join(';');
}
return false;
};
utilityFunctions.windowPostMessage = function (context, dataObj, domain) {
if (!domain || !context) {
return;
}
context.postMessage(JSON.stringify(dataObj), domain);
};
utilityFunctions.checkIfJson = function (str) {
if (str.length === 0) {
return false;
}
try {
JSON.parse(str);
if (!isNaN(str)) {
return false;
}
}
catch (e) {
return false;
}
return true;
};
utilityFunctions.convertDateTimeAnswerToISO8601 = function (answer) {
var result;
if (answer.date && answer.time) {
var time = moment(answer.time);
result = moment(answer.date)
.hour(time.hour())
.minute(time.minute());
}
else {
result = moment(answer);
}
return result.format();
};
utilityFunctions.getTicketCreatedSuccessMessageWithLink = function (ticketId, ticketDisplayId, ticketType, newTab) {
var msg;
if (newTab) {
msg = '' + ticketDisplayId + '';
}
else {
msg = '' + ticketDisplayId + '';
}
return msg;
};
utilityFunctions.getUniqueId = function () {
return id++;
};
//Remove first 3 special characters
utilityFunctions.trimFirstThreeWildCardCharacters = function (text, charArr) {
var showTooltip = false;
for (var index = 0; index < text.length; index++) {
if (index === 3)
break;
if (charArr.indexOf(text[index]) !== -1) {
text = text.substring(0, index) + text.substring(index + 1, text.length);
index--;
showTooltip = true;
}
}
return { text: text, showTooltip: showTooltip };
};
//Escape HTML characters
utilityFunctions.escapeHTML = function (htmlString) {
return htmlString
.replace(/&/g, "&")
.replace(//g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
};
//Decode HTML characters
utilityFunctions.decodeHTML = function (htmlString) {
return htmlString
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, '"')
.replace(/'/g, "'");
};
utilityFunctions.isValidHttpUrl = function (string) {
var url;
try {
url = new URL(string);
}
catch (_) {
return false;
}
return url.protocol === "http:" || url.protocol === "https:";
};
//Returns Date object after converting the selected date to user preferred timezone
//timezone format: America/Los_Angeles
utilityFunctions.convertToUserPreferenceTimezone = function (existingEpochDateTime, timezone) {
if (!existingEpochDateTime) {
return null;
}
var localDateTimeString = new Date(existingEpochDateTime).toLocaleString("en-US");
return moment.tz(localDateTimeString, "M/D/YYYY, h:mm:ss A", timezone).toDate();
};
//Returns Epoch Date after converting the existing date in users timezone and then setting same date to browser timezone
//timezone format: America/Los_Angeles
utilityFunctions.convertToLocalTimezone = function (existingEpochDateTime, timezone) {
if (!existingEpochDateTime) {
return null;
}
var userPrefDateTimeArr = moment.tz(existingEpochDateTime, timezone).toArray();
var localDate = new (Date.bind.apply(Date, [void 0].concat(userPrefDateTimeArr)))();
return localDate.getTime();
};
return utilityFunctions;
}]);
})();