Skip to content

Commit

Permalink
Pulled survey construction out of parseConfigInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeTschudi committed Apr 25, 2016
1 parent 18b0eb4 commit 93c2a62
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 138 deletions.
4 changes: 2 additions & 2 deletions js/app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ define(['lib/i18n.min!nls/resources.js', 'prepareAppConfigInfo', 'handleUserSign
});

// Create survey
survey.create($("#surveyContainer")[0], prepareAppConfigInfo.survey, isReadOnly);
survey.createNewForm($("#surveyContainer")[0], prepareAppConfigInfo.survey, isReadOnly);

// Show the content
$("#contentPage").fadeIn("fast");
Expand Down Expand Up @@ -568,7 +568,7 @@ define(['lib/i18n.min!nls/resources.js', 'prepareAppConfigInfo', 'handleUserSign
});
$("#submitBtn").on('click', function () {
var firstMissing =
survey.validate($('#surveyContainer'), prepareAppConfigInfo.survey, main.candidate.obj.attributes);
survey.validateForm($('#surveyContainer'), prepareAppConfigInfo.survey, main.candidate.obj.attributes);

// Submit the survey if it has the important responses
if (firstMissing === undefined) {
Expand Down
139 changes: 19 additions & 120 deletions js/app/parseConfigInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,84 +23,6 @@ define(function () {

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

/**
* Parses HTML text such as appears in a webmap's feature layer's popup to generate a set of survey questions.
* @param {string} source Text from source
* @param {object} fieldDomains List of field domains and field required/optional state as created by function
* createSurveyDictionary using the 'fields' property of a feature service
* @return {array} List of survey question objects, each of which contains question, field, style, domain, important
* properties
*/
parseSurvey: function (source, fieldDomains) {
// Survey is written as a series of lines in the popup. Each line is expected to have arbitrary text followed by
// a feature layer field name in braces followed by a question style flag also in braces.
// Here is a sample source:
// <p>Is there a Structure on the Property? <b>{<font color='#0000ff'>Structure</font>} </b><b>{<span
// style='background-color:rgb(255, 0, 0);'>button</span>}</b></p><p><ul><li>Is the lot overgrown? <b>{Lot}
// </b><b>{button}</b><br /></li><li>Foundation type: <b>{<font color='#ffff00' style='background-color:
// rgb(255, 69, 0);'>FoundationType</font>} </b><b>{radio}</b><br /></li></ul></p><p><b><br /></b></p><p>Is
// there roof damage? <b>{RoofDamage} </b><b>{button}</b></p><p>Is the exterior damaged? <b>{ExteriorDamage}
// </b><b>{button}</b></p><p></p><ol><li>Is there graffiti? <b>{Graffiti} </b><b>{button}</b><br /></li><li>
// Are there boarded windows/doors? <b>{Boarded} </b><b>{button}</b><br /></li></ol>
var survey = [], descriptionSplit1, descriptionSplit2, descriptionSplit3, taggedSurveyLines, surveyLines;

// 1. split on <div>, <p>, <br />, and <li>, all of which could be used to separate lines
descriptionSplit2 = [];
descriptionSplit3 = [];
taggedSurveyLines = [];
descriptionSplit1 = source.split("<div>");
$.each(descriptionSplit1, function (ignore, line) {
$.merge(descriptionSplit2, line.split("<p>"));
});
$.each(descriptionSplit2, function (ignore, line) {
$.merge(descriptionSplit3, line.split("<br />"));
});
$.each(descriptionSplit3, function (ignore, line) {
$.merge(taggedSurveyLines, line.split("<li>"));
});

// 2. remove all html tags (could have <b>, <i>, <u>, <ol>, <ul>, <li>, <a>, <font>, <span>, <br>,
// and their closures included or explicit)
surveyLines = [];
$.each(taggedSurveyLines, function (ignore, line) {
var cleanedLine = parseConfigInfo.textOnly(line).trim();
if (cleanedLine.length > 0) {
surveyLines.push(cleanedLine);
}
});

// 3. Separate into question, field, and style
// e.g., "Is there a Structure on the Property? {Structure} {button}"
$.each(surveyLines, function (ignore, line) {
var paramParts, trimmedParts, fieldName, surveyQuestion;
paramParts = line.split("{");
trimmedParts = [];
$.each(paramParts, function (ignore, part) {
var trimmed = part.replace("}", "").trim();
if (trimmed.length > 0) {
trimmedParts.push(trimmed);
}
});

// Should have three parts now: question, field, style; we can add in the question's
// domain and importance from the fieldDomain dictionary created just above
if (trimmedParts.length === 3) {
fieldName = trimmedParts[1];
if (fieldDomains[fieldName]) {
surveyQuestion = {
question: trimmedParts[0],
field: fieldName,
style: trimmedParts[2],
domain: fieldDomains[fieldName].domain,
important: fieldDomains[fieldName].important
};
survey.push(surveyQuestion);
}
}
});
return survey;
},

/**
* Parses HTML text such as appears in an AGOL item's description to extract a set of configuration parameters.
* @param {string} source Text from source
Expand Down Expand Up @@ -152,7 +74,7 @@ define(function () {
// 10: "best photo field: BSTPHOTOID"
configLines = [];
$.each(taggedConfigLines, function (ignore, line) {
var cleanedLine = parseConfigInfo.textOnly(line).trim();
var cleanedLine = parseConfigInfo._textOnly(line).trim();
if (cleanedLine.length > 0) {
configLines.push(cleanedLine);
}
Expand All @@ -171,22 +93,22 @@ define(function () {
if (lineParts[0].toLowerCase().indexOf(keywordParts[iKeyword]) >= 0) {
switch (iKeyword) {
case 0: // "0"
parseConfigInfo.extractContribLevel(0, lineParts[1], contribLevels);
parseConfigInfo._extractContribLevel(0, lineParts[1], contribLevels);
break;
case 1: // "1"
parseConfigInfo.extractContribLevel(1, lineParts[1], contribLevels);
parseConfigInfo._extractContribLevel(1, lineParts[1], contribLevels);
break;
case 2: // "2"
parseConfigInfo.extractContribLevel(2, lineParts[1], contribLevels);
parseConfigInfo._extractContribLevel(2, lineParts[1], contribLevels);
break;
case 3: // "3"
parseConfigInfo.extractContribLevel(3, lineParts[1], contribLevels);
parseConfigInfo._extractContribLevel(3, lineParts[1], contribLevels);
break;
case 4: // "4"
parseConfigInfo.extractContribLevel(4, lineParts[1], contribLevels);
parseConfigInfo._extractContribLevel(4, lineParts[1], contribLevels);
break;
case 5: // "5"
parseConfigInfo.extractContribLevel(5, lineParts[1], contribLevels);
parseConfigInfo._extractContribLevel(5, lineParts[1], contribLevels);
if (contribLevels !== null) {
config.contribLevels = contribLevels;
}
Expand All @@ -209,6 +131,16 @@ define(function () {
return config;
},

/**
* Tests that an item is a string of length greater than zero.
* @param {string} item Item to test
* @return {boolean} True if the item is a string with length greater than zero
* @private
*/
isUsableString: function (item) {
return typeof item === "string" && item.length > 0;
},

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

/**
Expand All @@ -220,7 +152,7 @@ define(function () {
* minimumSurveysNeeded properties. The object created from this levelDescrip is pushed onto the end of the array.
* @private
*/
extractContribLevel: function (iLevel, levelDescrip, contribLevels) {
_extractContribLevel: function (iLevel, levelDescrip, contribLevels) {
var levelParts, minimumSurveysNeeded;

if (contribLevels !== null && levelDescrip !== null) {
Expand All @@ -240,47 +172,14 @@ define(function () {
}
},

/**
* Converts a list of feature service fields into a dictionary of fields with their domains and nullability;
* skips fields without coded-value domains.
* @param {array} featureSvcFields List of fields such as the one supplied by a feature service
* @return {object} Object containing the field names as its properties; each property's value consists of the
* '|'-separated coded values in the field's domain
* @private
*/
createSurveyDictionary: function (featureSvcFields) {
var fieldDomains = {};
$.each(featureSvcFields, function (ignore, field) {
if (field.domain && field.domain.codedValues) {
fieldDomains[field.name] = {
domain: $.map(field.domain.codedValues, function (item) {
return item.name;
}).join("|"),
important: !field.nullable
};
}
});
return fieldDomains;
},

/**
* Extracts the text from an HTML passage.
* @param {string} original Text which may contain HTML
* @return {string} Text-only version of original
* @private
*/
textOnly: function (original) {
_textOnly: function (original) {
return $("<div>" + original + "</div>").text();
},

/**
* Tests that an item is a string of length greater than zero.
* @param {string} item Item to test
* @return {boolean} True if the item is a string with length greater than zero
* @private
*/
isUsableString: function (item) {
return typeof item === "string" && item.length > 0;
}

};
Expand Down
10 changes: 4 additions & 6 deletions js/app/prepareAppConfigInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
| limitations under the License.
*/
//============================================================================================================================//
define(['parseConfigInfo', 'fetchConfigInfo'], function (parseConfigInfo, fetchConfigInfo) {
define(['parseConfigInfo', 'fetchConfigInfo', 'survey'], function (parseConfigInfo, fetchConfigInfo, survey) {
'use strict';
var prepareAppConfigInfo;
prepareAppConfigInfo = {
Expand Down Expand Up @@ -180,11 +180,9 @@ define(['parseConfigInfo', 'fetchConfigInfo'], function (parseConfigInfo, fetchC
prepareAppConfigInfo.featureSvcParams.objectIdField = data.featureSvcParams.objectIdField;
prepareAppConfigInfo.featureSvcParams.canBeUpdated = data.featureSvcParams.canBeUpdated;

// Create dictionary of domains
dictionary = parseConfigInfo.createSurveyDictionary(data.featureSvcParams.fields);

// Parse survey
prepareAppConfigInfo.survey = parseConfigInfo.parseSurvey(data.opLayerParams.popupInfo.description, dictionary);
// Create survey
prepareAppConfigInfo.survey = survey.createSurvey(
data.opLayerParams.popupInfo.description, data.featureSvcParams.fields);
prepareAppConfigInfo.surveyReady.resolve();
} else {
prepareAppConfigInfo.featureSvcParams = {};
Expand Down
Loading

0 comments on commit 93c2a62

Please sign in to comment.