diff --git a/launch.go b/launch.go index dceb00af..758bb69f 100644 --- a/launch.go +++ b/launch.go @@ -57,7 +57,7 @@ func serveTemplate(templateName string, data interface{}, w http.ResponseWriter, } type page struct { - Schemas surveys.LauncherSchemas + Schemas map[string][]surveys.LauncherSchema AccountServiceURL string AccountServiceLogOutURL string } diff --git a/surveys/surveys.go b/surveys/surveys.go index 0ff4e5e8..f77243a2 100644 --- a/surveys/surveys.go +++ b/surveys/surveys.go @@ -17,16 +17,9 @@ import ( // LauncherSchema is a representation of a schema in the Launcher type LauncherSchema struct { - Name string - URL string -} - -// LauncherSchemas is a separation of Test and Live schemas -type LauncherSchemas struct { - Business []LauncherSchema - Social []LauncherSchema - Test []LauncherSchema - Other []LauncherSchema + Name string + SurveyType string + URL string } // RegisterResponse is the response from the eq-survey-register request @@ -46,35 +39,29 @@ type Schema struct { var eqIDFormTypeRegex = regexp.MustCompile(`^(?P[a-z0-9]+)_(?P\w+)`) // LauncherSchemaFromFilename creates a LauncherSchema record from a schema filename -func LauncherSchemaFromFilename(filename string) LauncherSchema { +func LauncherSchemaFromFilename(filename string, surveyType string) LauncherSchema { return LauncherSchema{ - Name: filename, + Name: filename, + SurveyType: surveyType, } } // GetAvailableSchemas Gets the list of static schemas an joins them with any schemas from the eq-survey-register if defined -func GetAvailableSchemas() LauncherSchemas { - schemaList := LauncherSchemas{} - +func GetAvailableSchemas() map[string][]LauncherSchema { runnerSchemas := getAvailableSchemasFromRunner() + registerSchemas := getAvailableSchemasFromRegister() - for _, launcherSchema := range runnerSchemas { - if strings.HasPrefix(launcherSchema.Name, "test_") { - schemaList.Test = append(schemaList.Test, launcherSchema) - } else if strings.HasPrefix(launcherSchema.Name, "lms_") { - schemaList.Social = append(schemaList.Social, launcherSchema) - } else { - schemaList.Business = append(schemaList.Business, launcherSchema) - } - } + allSchemas := []LauncherSchema{} + allSchemas = append(runnerSchemas, registerSchemas...) - schemaList.Other = getAvailableSchemasFromRegister() + sort.Sort(ByFilename(allSchemas)) - sort.Sort(ByFilename(schemaList.Business)) - sort.Sort(ByFilename(schemaList.Social)) - sort.Sort(ByFilename(schemaList.Test)) + schemasBySurveyType := map[string][]LauncherSchema{} + for _, schema := range allSchemas { + schemasBySurveyType[strings.Title(schema.SurveyType)] = append(schemasBySurveyType[strings.Title(schema.SurveyType)], schema) + } - return schemaList + return schemasBySurveyType } // ByFilename implements sort.Interface based on the Name field. @@ -118,8 +105,9 @@ func getAvailableSchemasFromRegister() []LauncherSchema { for _, schema := range schemas { url := schema.Links["self"] schemaList = append(schemaList, LauncherSchema{ - Name: schema.Name, - URL: url.Href, + Name: schema.Name, + URL: url.Href, + SurveyType: "Other", }) } } @@ -138,6 +126,7 @@ func getAvailableSchemasFromRunner() []LauncherSchema { url := fmt.Sprintf("%s/schemas", hostURL) resp, err := clients.GetHTTPClient().Get(url) + if err != nil { return []LauncherSchema{} } @@ -152,15 +141,17 @@ func getAvailableSchemasFromRunner() []LauncherSchema { return []LauncherSchema{} } - var schemaListResponse []string + var schemaMapResponse = map[string][]string{} - if err := json.Unmarshal(responseBody, &schemaListResponse); err != nil { + if err := json.Unmarshal(responseBody, &schemaMapResponse); err != nil { log.Print(err) return []LauncherSchema{} } - for _, schema := range schemaListResponse { - schemaList = append(schemaList, LauncherSchemaFromFilename(schema)) + for surveyType, schemas := range schemaMapResponse { + for _, schemaName := range schemas { + schemaList = append(schemaList, LauncherSchemaFromFilename(schemaName, surveyType)) + } } return schemaList @@ -170,25 +161,13 @@ func getAvailableSchemasFromRunner() []LauncherSchema { func FindSurveyByName(name string) LauncherSchema { availableSchemas := GetAvailableSchemas() - for _, survey := range availableSchemas.Business { - if survey.Name == name { - return survey - } - } - for _, survey := range availableSchemas.Social { - if survey.Name == name { - return survey - } - } - for _, survey := range availableSchemas.Test { - if survey.Name == name { - return survey - } - } - for _, survey := range availableSchemas.Other { - if survey.Name == name { - return survey + for _, schemasBySurveyType := range availableSchemas { + for _, schema := range schemasBySurveyType { + if schema.Name == name { + return schema + } } } - panic("Survey not found") + + panic("Schema not found") } diff --git a/templates/launch.html b/templates/launch.html index 0f235636..ffd093a9 100644 --- a/templates/launch.html +++ b/templates/launch.html @@ -10,30 +10,18 @@

Launch a survey

-
+

Survey Metadata

@@ -131,16 +119,16 @@

Runner Data

// uuidv4: from https://github.com/kelektiv/node-uuid !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var n;n="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,n.uuidv4=e()}}(function(){return function e(n,r,o){function t(f,u){if(!r[f]){if(!n[f]){var a="function"==typeof require&&require;if(!u&&a)return a(f,!0);if(i)return i(f,!0);var d=new Error("Cannot find module '"+f+"'");throw d.code="MODULE_NOT_FOUND",d}var p=r[f]={exports:{}};n[f][0].call(p.exports,function(e){var r=n[f][1][e];return t(r?r:e)},p,p.exports,e,n,r,o)}return r[f].exports}for(var i="function"==typeof require&&require,f=0;f>>((3&n)<<3)&255;return i}}},{}],3:[function(e,n,r){function o(e,n,r){var o=n&&r||0;"string"==typeof e&&(n="binary"===e?new Array(16):null,e=null),e=e||{};var f=e.random||(e.rng||t)();if(f[6]=15&f[6]|64,f[8]=63&f[8]|128,n)for(var u=0;u<16;++u)n[o+u]=f[u];return n||i(f)}var t=e("./lib/rng"),i=e("./lib/bytesToUuid");n.exports=o},{"./lib/bytesToUuid":1,"./lib/rng":2}]},{},[3])(3)}); - function clearBusinessClaims() { - document.getElementById('business_claims').innerHTML = "" + function clearSurveyMetadataFields() { + document.querySelector('#survey_metadata_fields').innerHTML = "" } - function includeBusinessClaims(schema_name) { + function includeSurveyMetadataFields(schema_name, survey_type) { let eqIdValue = schema_name.split('_')[0] let formTypeValue = schema_name.split('_')[1] - document.getElementById('business_claims').innerHTML = ` -

Business Survey Metadata

+ document.querySelector('#survey_metadata_fields').innerHTML = ` +

${survey_type} Survey Metadata

@@ -153,15 +141,16 @@

Business Survey Metadata

} function loadMetadata() { - document.getElementById("submit-btn").disabled = true; - document.getElementById("flush-btn").disabled = true; + document.querySelector("#submit-btn").disabled = true; + document.querySelector("#flush-btn").disabled = true; - const schema_name = document.getElementById("schema_name").value + const schema_name = document.querySelector("#schema_name").value + const survey_type = document.querySelector(`#schema_name option[value=${schema_name}]`).dataset.surveyType - if (schema_name.startsWith('test_')) { - clearBusinessClaims() + if (survey_type.toLowerCase() === "test") { + clearSurveyMetadataFields() } else { - includeBusinessClaims(schema_name) + includeSurveyMetadataFields(schema_name, survey_type) } var xhttp = new XMLHttpRequest(); @@ -169,7 +158,7 @@

Business Survey Metadata

if (this.readyState == 4) { if (this.status == 200) { - document.getElementById("survey_metadata").innerHTML = "" + document.querySelector("#survey_metadata").innerHTML = "" var response = JSON.parse(this.responseText); @@ -212,26 +201,26 @@

Business Survey Metadata

"
" } - document.getElementById("survey_metadata").innerHTML = document.getElementById("survey_metadata").innerHTML + metadataFieldHtml; + document.querySelector("#survey_metadata").innerHTML = document.querySelector("#survey_metadata").innerHTML + metadataFieldHtml; } } else { - document.getElementById("survey_metadata").innerHTML = "No metadata required for this survey"; + document.querySelector("#survey_metadata").innerHTML = "No metadata required for this survey"; } - document.getElementById("submit-btn").disabled = false; - document.getElementById("flush-btn").disabled = false; + document.querySelector("#submit-btn").disabled = false; + document.querySelector("#flush-btn").disabled = false; } else { - document.getElementById("survey_metadata").innerHTML = "Failed to load Schema Metadata"; + document.querySelector("#survey_metadata").innerHTML = "Failed to load Schema Metadata"; } } }; - xhttp.open("GET", "/metadata?schema=" + document.getElementById('schema_name').value, true); + xhttp.open("GET", "/metadata?schema=" + document.querySelector("#schema_name").value, true); xhttp.send(); } function uuid(el_id) { - document.getElementById(el_id).value = uuidv4(); + document.querySelector(`#${el_id}`).value = uuidv4(); } function numericId(el_id) { @@ -240,7 +229,7 @@

Business Survey Metadata

for (var i = 16; i > 0; --i) { result += chars[Math.round(Math.random() * (chars.length - 1))]; } - document.getElementById(el_id).value = result; + document.querySelector(`#${el_id}`).value = result; } function ruref(el_id) { @@ -249,13 +238,13 @@

Business Survey Metadata

for (var i = 11; i > 0; --i) result += chars[Math.round(Math.random() * (chars.length - 1))]; var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; result += chars[Math.round(Math.random() * (chars.length - 1))]; - document.getElementById(el_id).value = result; + document.querySelector(`#${el_id}`).value = result; } function setResponseExpiry(days_offset=7) { var dt = new Date(); dt.setDate(dt.getDate()+days_offset) - document.getElementById('response_expires_at').value=dt.toISOString().replace(/(\.\d*)/, '').replace(/Z/, '+00:00'); + document.querySelector('#response_expires_at').value=dt.toISOString().replace(/(\.\d*)/, '').replace(/Z/, '+00:00'); } function validateForm() { @@ -263,9 +252,9 @@

Business Survey Metadata

} function validateResponseExpiresAt() { - response_expires_at = Date.parse(document.getElementById('response_expires_at').value) + response_expires_at = Date.parse(document.querySelector('#response_expires_at').value) if (isNaN(response_expires_at)) { - document.getElementById('response_expires_at').remove() + document.querySelector('#response_expires_at').remove() } }