var dialogManager = {
defaultButton: {
text: "OK",
right: true,
active: true
},
showDialog: function (caption, message, buttons) {
var dialogContainer = $("#dialog-container");
var dialogTitle = dialogContainer.find(".dialog-title");
var dialogMessage = dialogContainer.find(".dialog-message");
var actionLeft = dialogContainer.find(".dialog-card-action-left");
var actionRight = dialogContainer.find(".dialog-card-action-right");
dialogTitle.html(caption);
dialogMessage.html(message);
var leftButtons = [];
var rightButtons = [];
if (!buttons) {
buttons = [this.defaultButton];
}
for (var i = 0; i < buttons.length; i++) {
var button = $("");
button.addClass("btn");
if (!buttons[i].active) {
button.addClass("btn-flat");
}
button.html(buttons[i].text);
if (buttons[i].callback) {
button.on("click", buttons[i].callback);
}
else {
button.on("click", dialogManager.hideDialog);
}
if (buttons[i].right) {
rightButtons.push(button);
} else {
leftButtons.push(button);
}
}
actionLeft.html(leftButtons);
actionRight.html(rightButtons);
dialogContainer.show();
},
hideDialog: function () {
var dialogContainer = $("#dialog-container");
dialogContainer.hide();
}
};
var browserHelper = {
//http://stackoverflow.com/questions/11582512/how-to-get-url-parameters-with-javascript/11582513#11582513
getURLParameter: function(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [null, ''])[1].replace(/\+/g, '%20')) || null;
}
}
var stringHelper = {
getVariable: function(key){
for (var variable in survey.currentPage.Variables) {
if (survey.currentPage.Variables[variable].ID == key) {
return survey.currentPage.Variables[variable].Value;
}
}
return null;
},
formatString: function (stringarray) {
var stringToFormat = stringarray[0];
var i = 0;
var placeholder = "\\{" + i + "\\}";
var re = new RegExp(placeholder, 'g');
while (re.test(stringToFormat)) {
i++;
stringToFormat = stringToFormat.replace(re, stringarray[i]); // replace is done with regex, because otherwise, only the first accurence is getting replaced
placeholder = "\\{" + i + "\\}";
re = new RegExp(placeholder, 'g');
}
return stringToFormat;
},
formatStringsWithVariables: function (stringarray) {
var replacedStrings = [];
replacedStrings.push(stringarray[0]);
for (var i = 1; i < stringarray.length; i++) {
replacedStrings.push(stringHelper.getVariable(stringarray[i]));
}
return stringHelper.formatString(replacedStrings);
},
escapeHTMLEntityMap : {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'/': '/',
'`': '`',
'=': '='
},
escapeHtml: function(html) {
return String(html).replace(/[&<>"'`=\/]/g, function (s) {
return stringHelper.escapeHTMLEntityMap[s];
});
}
}
/* includes for IE-Support */
// http://stackoverflow.com/a/31221374/6154762
if (!String.prototype.includes) {
String.prototype.includes = function () {
'use strict';
return String.prototype.indexOf.apply(this, arguments) !== -1;
};
}
var navigationManager = {
getUserTimeStamp: function myfunction() {
var dateString = new Date($.now()).toJSON();
return dateString;
},
jump: function (nextHistoryKey) {
var nextUrl = "/Survey/Jump?nextHistoryKey=" + nextHistoryKey;
this.submitPage(nextUrl);
},
goToPageGroup: function (pageGroupTo) {
var nextUrl = "/Survey/GoToPageGroup?pageGroupTo=" + pageGroupTo;
this.submitPage(nextUrl);
},
goBack: function () {
var currentHistoryKey = browserHelper.getURLParameter("historyKey");
var nextUrl = "/Survey/GoBack?currentKey=" + currentHistoryKey;
this.submitPage(nextUrl);
},
proceed: function () {
if (this.pageValid()) {
var currentHistoryKey = this.getCurrentHistoryKey();
var nextUrl = "/Survey/Proceed?currentKey=" + currentHistoryKey;
this.submitPage(nextUrl);
} else {
var message = toolTipMessages.DefaultValidationErrorMessage;
if (survey.currentPage.CustomValidationErrorMessage) {
message = survey.currentPage.CustomValidationErrorMessage;
}
dialogManager.showDialog(toolTipMessages.DefaultValidationErrorTitle, message);
}
},
onPageLoaded: function () {
},
finish: function () {
var nextUrl = "/Survey/Finish?currentKey=" + this.getCurrentHistoryKey();
this.submitPage(nextUrl);
},
end: function () {
var nextUrl = "/Survey/End?currentKey=" + this.getCurrentHistoryKey();
this.submitPage(nextUrl);
},
pageValid: function () {
$("input.validate").blur();
$("input.validate:not(.valid)").addClass("invalid");
var validationFunction = function () { return true; };
if (survey.currentPage.Validation) {
eval("validationFunction = " + survey.currentPage.Validation);
}
return validationFunction();
},
submitPage: function (nextUrl) {
if (typeof pauseCountdown === "function") {
pauseCountdown();
}
var form = $("form");
var userAnswers = form.serialize()
showLoadingBar();
(userAnswers) ? userData = userAnswers + "&timeStampUserSent=" + this.getUserTimeStamp() : userData = "timeStampUserSent=" + this.getUserTimeStamp()
$.ajax({
url: nextUrl + "&isAjax=true",
type: "post",
dataType: "json",
data: userData,
success: function (data, textstatus, xhrreq) {
if (data.RedirectURL) {
location.href = data.RedirectURL;
return;
}
navigationManager.updateHistoryState(xhrreq.getResponseHeader("history-key"));
buildPage(data);
if (navigationManager.onPageSubmitted) {
navigationManager.onPageSubmitted();
}
},
error: function (data, textStatus, errorThrown) {
dialogManager.showDialog(toolTipMessages.DefaultRequestErrorTitle,
toolTipMessages.DefaultRequestErrorMessage,
[
{
text: "Neu laden",
callback: function () {
location.reload();
}
},
dialogManager.defaultButton
]);
}
});
},
updateHistoryState: function (currentHistoryKey) {
if (currentHistoryKey) {
window.history.pushState({}, "Survey", "/Survey/Show?historyKey=" + currentHistoryKey);
} else {
window.history.pushState({}, "Survey", "/Survey/End");
}
},
getCurrentPageIndex: function () {
var currentHistoryKey = this.getCurrentHistoryKey();
return survey.currentPage.PageInformations.findIndex((p) => p.HistoryKey == currentHistoryKey);
},
getCurrentHistoryKey: function () {
return browserHelper.getURLParameter("historyKey");
},
getNumberOfTotalPages: function () {
return survey.currentPage.PageInformations.length;
},
isFirstPage: function () {
return navigationManager.getCurrentPageIndex() == 0;
},
isLastPage: function () {
return navigationManager.getCurrentPageIndex() == navigationManager.getNumberOfTotalPages() - 1;
}
};
window.onpopstate = function () {
location.reload();
}
class Page extends React.Component {
constructor(props) {
super(props);
this.state = {
page: this.props.page,
key: Math.random()
}
this.onPageChanged = this.onPageChanged.bind(this);
this.getViews = this.getViews.bind(this);
pageElementAccessor.onPageChanged = this.onPageChanged;
}
getViews() {
var views = [];
var key = this.state.key;
this.state.page.Content.forEach(function (content, index) {
views.push(
Error Loading View