Skip to content

Commit

Permalink
Pulled survey out of main
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeTschudi committed Apr 11, 2016
1 parent e6c2e0c commit 34230c5
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 106 deletions.
113 changes: 7 additions & 106 deletions js/app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
| limitations under the License.
*/
//============================================================================================================================//
define(['lib/i18n.min!nls/resources.js', 'prepareAppConfigInfo', 'handleUserSignin', 'dataAccess', 'diag'],
function (i18n, prepareAppConfigInfo, handleUserSignin, dataAccess, diag) {
define(['lib/i18n.min!nls/resources.js', 'prepareAppConfigInfo', 'handleUserSignin', 'dataAccess', 'survey', 'diag'],
function (i18n, prepareAppConfigInfo, handleUserSignin, dataAccess, survey, diag) {
'use strict';
var main, unsupported = false, needProxy = false, proxyReady;

Expand Down Expand Up @@ -318,76 +318,6 @@ define(['lib/i18n.min!nls/resources.js', 'prepareAppConfigInfo', 'handleUserSign
}
},

startQuestion: function (ignore, iQuestion, questionInfo) {
// <div class='form-group'>
// <label for='q1'>Is there a structure on the property? <span class='glyphicon glyphicon-star'></span></label><br>
var start =
"<div id='qg" + iQuestion + "' class='form-group'>"
+ "<label for='q" + iQuestion + "'>" + questionInfo.question + (questionInfo.important
? "&nbsp;<div class='importantQuestion sprites star' title=\"" + i18n.tooltips.flag_important_question + "\"></div>"
: "")
+ "</label><br>";
return start;
},

createButtonChoice: function (ignore, iQuestion, questionInfo, isReadOnly) {
// <div id='q1' class='btn-group'>
// <button type='button' class='btn'>Yes</button>
// <button type='button' class='btn'>No</button>
// <button type='button' class='btn'>Not sure</button>
// </div>
var buttons = "<div id='q" + iQuestion + "' class='btn-group'>";
var domain = questionInfo.domain.split('|');
$.each(domain, function (i, choice) {
buttons += "<button type='button' class='btn' value='" + i + "' " + (isReadOnly
? "disabled"
: "") + ">" + choice + "</button>";
});
buttons += "</div>";
return buttons;
},

createListChoice: function (ignore, iQuestion, questionInfo, isReadOnly) {
// <div class='radio'><label><input type='radio' name='q1' id='optionFound1' value='0'>Crawlspace</label></div>
// <div class='radio'><label><input type='radio' name='q1' id='optionFound2' value='1'>Raised</label></div>
// <div class='radio'><label><input type='radio' name='q1' id='optionFound3' value='2'>Elevated</label></div>
// <div class='radio'><label><input type='radio' name='q1' id='optionFound4' value='3'>Slab on grade</label></div>
// <div class='radio'><label><input type='radio' name='q1' id='optionFound0' value='4'>Not sure</label></div>
var list = "";
var domain = questionInfo.domain.split('|');
$.each(domain, function (i, choice) {
list += "<div class='radio'><label><input type='radio' name='q" + iQuestion + "' value='" + i + "' " + (isReadOnly
? "disabled"
: "") + ">" + choice + "</label></div>";
});
return list;
},

wrapupQuestion: function () {
// </div>
// <div class='clearfix'></div>
var wrap = "</div><div class='clearfix'></div>";
return wrap;
},

addQuestion: function (surveyContainer, iQuestion, questionInfo, isReadOnly) {
var question = main.startQuestion(surveyContainer, iQuestion, questionInfo);
if (questionInfo.style === "button") {
question += main.createButtonChoice(surveyContainer, iQuestion, questionInfo, isReadOnly);
} else {
question += main.createListChoice(surveyContainer, iQuestion, questionInfo, isReadOnly);
}
question += main.wrapupQuestion(surveyContainer, iQuestion, questionInfo);
$(surveyContainer).append(question);

// Fix radio-button toggling
if (questionInfo.style === "button") {
$('#q' + iQuestion + ' button').click(function (evt) {
$(evt.currentTarget).addClass('active').siblings().removeClass('active');
});
}
},

addPhoto: function (carouselSlidesHolder, indexInArray, isActive, photoUrl) {
// <div id='carousel0' class='item active'><img src='__test/VIRB0125.JPG' alt='VIRB0125.JPG'></div>
// var content = "<div id='c" + indexInArray + "' class='item" + (isActive ? " active" : "") +
Expand Down Expand Up @@ -601,13 +531,8 @@ define(['lib/i18n.min!nls/resources.js', 'prepareAppConfigInfo', 'handleUserSign
$(document).triggerHandler('show:noSurveys');
});

// Survey
var surveyContainer = $("#surveyContainer")[0];
$(surveyContainer).children().remove(); // remove children and their events
$.each(prepareAppConfigInfo.survey, function (indexInArray, questionInfo) {
main.addQuestion(surveyContainer, indexInArray, questionInfo, isReadOnly);
});
$(".btn-group").trigger('create');
// Create survey
survey.create($("#surveyContainer")[0], prepareAppConfigInfo.survey, isReadOnly);

// Show the content
$("#contentPage").fadeIn("fast");
Expand Down Expand Up @@ -639,35 +564,11 @@ define(['lib/i18n.min!nls/resources.js', 'prepareAppConfigInfo', 'handleUserSign
$(document).triggerHandler('show:newSurvey');
});
$("#submitBtn").on('click', function () {
var surveyContainer, iQuestionResult, hasImportants = true, firstMissing;

surveyContainer = $('#surveyContainer');
$.each(prepareAppConfigInfo.survey, function (iQuestion, questionInfo) {
if (questionInfo.style === "button") {
iQuestionResult = $('#q' + iQuestion + ' .active', surveyContainer).val();
} else {
iQuestionResult = $('input[name=q' + iQuestion + ']:checked', surveyContainer).val();
}
if (iQuestionResult) {
main.candidate.obj.attributes[questionInfo.field] = questionInfo.domain.split("|")[iQuestionResult];
}

// Flag missing importants
if (questionInfo.important) {
if (iQuestionResult) {
$("#qg" + iQuestion).removeClass("flag-error");
} else {
$("#qg" + iQuestion).addClass("flag-error");
hasImportants = false;
if (firstMissing === undefined) {
firstMissing = $("#qg" + iQuestion)[0];
}
}
}
});
var firstMissing =
survey.validate($('#surveyContainer'), prepareAppConfigInfo.survey, main.candidate.obj.attributes);

// Submit the survey if it has the important responses
if (hasImportants) {
if (firstMissing === undefined) {
main.candidate.obj.attributes[prepareAppConfigInfo.appParams.surveyorNameField] = handleUserSignin.getUser().name;
if (main.iSelectedPhoto >= 0) {
main.candidate.obj.attributes[prepareAppConfigInfo.appParams.bestPhotoField] = main.candidate.attachments[main.iSelectedPhoto].id;
Expand Down
141 changes: 141 additions & 0 deletions js/app/survey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*global define,$,window */
/*jslint browser:true */
/** @license
| Copyright 2015 Esri
|
| Licensed under the Apache License, Version 2.0 (the "License");
| you may not use this file except in compliance with the License.
| You may obtain a copy of the License at
|
| http://www.apache.org/licenses/LICENSE-2.0
|
| Unless required by applicable law or agreed to in writing, software
| distributed under the License is distributed on an "AS IS" BASIS,
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
| See the License for the specific language governing permissions and
| limitations under the License.
*/
//============================================================================================================================//
define(['lib/i18n.min!nls/resources.js'], function (i18n) {
'use strict';
var survey;
survey = {

//--------------------------------------------------------------------------------------------------------------------//

startQuestion: function (ignore, iQuestion, questionInfo) {
// <div class='form-group'>
// <label for='q1'>Is there a structure on the property? <span class='glyphicon glyphicon-star'></span></label><br>
var start =
"<div id='qg" + iQuestion + "' class='form-group'>"
+ "<label for='q" + iQuestion + "'>" + questionInfo.question + (questionInfo.important
? "&nbsp;<div class='importantQuestion sprites star' title=\"" + i18n.tooltips.flag_important_question + "\"></div>"
: "")
+ "</label><br>";
return start;
},

createButtonChoice: function (ignore, iQuestion, questionInfo, isReadOnly) {
// <div id='q1' class='btn-group'>
// <button type='button' class='btn'>Yes</button>
// <button type='button' class='btn'>No</button>
// <button type='button' class='btn'>Not sure</button>
// </div>
var buttons = "<div id='q" + iQuestion + "' class='btn-group'>";
var domain = questionInfo.domain.split('|');
$.each(domain, function (i, choice) {
buttons += "<button type='button' class='btn' value='" + i + "' " + (isReadOnly
? "disabled"
: "") + ">" + choice + "</button>";
});
buttons += "</div>";
return buttons;
},

createListChoice: function (ignore, iQuestion, questionInfo, isReadOnly) {
// <div class='radio'><label><input type='radio' name='q1' id='optionFound1' value='0'>Crawlspace</label></div>
// <div class='radio'><label><input type='radio' name='q1' id='optionFound2' value='1'>Raised</label></div>
// <div class='radio'><label><input type='radio' name='q1' id='optionFound3' value='2'>Elevated</label></div>
// <div class='radio'><label><input type='radio' name='q1' id='optionFound4' value='3'>Slab on grade</label></div>
// <div class='radio'><label><input type='radio' name='q1' id='optionFound0' value='4'>Not sure</label></div>
var list = "";
var domain = questionInfo.domain.split('|');
$.each(domain, function (i, choice) {
list += "<div class='radio'><label><input type='radio' name='q" + iQuestion + "' value='" + i + "' " + (isReadOnly
? "disabled"
: "") + ">" + choice + "</label></div>";
});
return list;
},

wrapupQuestion: function () {
// </div>
// <div class='clearfix'></div>
var wrap = "</div><div class='clearfix'></div>";
return wrap;
},

addQuestion: function (surveyContainer, iQuestion, questionInfo, isReadOnly) {
var question = survey.startQuestion(surveyContainer, iQuestion, questionInfo);
if (questionInfo.style === "button") {
question += survey.createButtonChoice(surveyContainer, iQuestion, questionInfo, isReadOnly);
} else {
question += survey.createListChoice(surveyContainer, iQuestion, questionInfo, isReadOnly);
}
question += survey.wrapupQuestion(surveyContainer, iQuestion, questionInfo);
$(surveyContainer).append(question);

// Fix radio-button toggling
if (questionInfo.style === "button") {
$('#q' + iQuestion + ' button').click(function (evt) {
$(evt.currentTarget).addClass('active').siblings().removeClass('active');
});
}
},

create: function (surveyContainer, surveyDefinition, isReadOnly) {
// Remove children and their events
$(surveyContainer).children().remove();

// Create the questions
$.each(surveyDefinition, function (indexInArray, questionInfo) {
survey.addQuestion(surveyContainer, indexInArray, questionInfo, isReadOnly);
});

// Render any radiobutton groups
$(".btn-group").trigger('create');
},

validate: function (surveyContainer, surveyDefinition, objAttributes) {
var iQuestionResult, firstMissing;

$.each(surveyDefinition, function (iQuestion, questionInfo) {
if (questionInfo.style === "button") {
iQuestionResult = $('#q' + iQuestion + ' .active', surveyContainer).val();
} else {
iQuestionResult = $('input[name=q' + iQuestion + ']:checked', surveyContainer).val();
}
if (iQuestionResult) {
objAttributes[questionInfo.field] = questionInfo.domain.split("|")[iQuestionResult];
}

// Flag missing importants
if (questionInfo.important) {
if (iQuestionResult) {
$("#qg" + iQuestion).removeClass("flag-error");
} else {
$("#qg" + iQuestion).addClass("flag-error");
if (firstMissing === undefined) {
firstMissing = $("#qg" + iQuestion)[0];
}
}
}
});

// Return the first missing important (if any)
return firstMissing;
}

};
return survey;
});

0 comments on commit 34230c5

Please sign in to comment.