Commit 5751c333 authored by Eirik Alvær's avatar Eirik Alvær
Browse files

Merge branch 'master' of https://gitlab.nsd.uib.no/components/command-executor

Conflicts:
	src/app/command-executor.js
parents 39fe21cd 22c5337d
/*global Promise, XMLHttpRequest */
function CommandExecutor() {
"use strict";
var helpTexts = {
'help': {
'description': 'Prints help for one of these commands:',
'syntax': '[help-topic]',
'examples': [
{
'example': 'help tabulate',
'description': 'Provides help about the tabulate function'
},
{
'example': 'help correlate',
'description': 'Provides help about the correlate function'
}
],
'relatedcommands': ["describe", "topics", "tabulate", "correlate", "help", "drop", "summarize"]
},
'topics': {
'description': 'lists topics and variables within each topic',
'syntax': '[topic-name]',
'examples': [
{
'example': 'topics',
'description': 'List all topics in the data collection'
},
{
'example': 'topics \'employment\'',
'description': 'List sub-topics and variables for a specified topic'
}
]
},
'describe': {
'description': 'lists variables and variable properties',
'syntax': '[varlist]',
'examples': [
{
'example': 'describe',
'description': 'List all variables and variable properties in the data collection'
},
{
'example': 'describe gender age income',
'description': 'List variable properties for three variables'
}
]
},
'tabulate': {
'description': 'generate tables of frequencies with one or more variables',
'syntax': 'varname1 [varname2] [if] [, options]',
'examples': [
{
'example': 'tabulate gender age',
'description': 'Two-way tabulation'
},
{
'example': 'tabulate gender age, summarize(income)',
'description': 'Two-way tabulation with included summary statistics for income'
}
]
},
'correlate': {
'description': 'generate a correlation matrix',
'syntax': '[varlist] [if]',
'examples': [
{
'example': 'correlate gender age income',
'description': 'Three-way correlation matrix'
},
{
'example': 'correlate age income if gener == \'male\'',
'description': 'Two-way correlation matrix with conditional expression'
}
]
},
'summarize': {
'description': 'list summary statistics for variables',
'syntax': '[varlist] [if]',
'examples': [
{
'example': 'summarize age income',
'description': 'List summary statistics for two variables'
},
{
'example': 'summarize age income if age > 45',
'description': 'List summary statistics for two variables with a conditional expression'
}
]
}
},
ajaxPostPromise = function (postData, url) {
return new Promise(function (resolve, reject) {
var req = new XMLHttpRequest();
req.open('POST', url);
req.onload = function () {
if (req.status === 200) {
resolve(JSON.parse(req.response));
} else {
reject(JSON.parse(req.response));
}
};
req.onerror = function () {
reject({error: {type: "NETWORK", message: "Network Error"}});
};
// Make the request
req.send(JSON.stringify(postData));
});
};
return {
//Eww! This method should not be exported, but it has to be done to
//enable mocking for the tests
requestControllerResult: function (parserObj, controllerUrl) {
return ajaxPostPromise(parserObj, controllerUrl);
},
execute: function (parserObject, controllerUrl, sessionId, callbackFun, errorFun) {
var intermediateResult, helpText;
switch (parserObject.type) {
case "help":
helpText = helpTexts[parserObject.helpTopic || "help"];
intermediateResult = new Promise(function (resolve) {
resolve(helpText ?
{result: helpText} :
{error: { type: "NO_RESULT", message: "Got no result"}}
);
});
break;
case "history":
intermediateResult = new Promise(function (resolve) {
resolve({result: "history"});
});
break;
default:
if (sessionId) {
parserObject.sessionId = sessionId;
}
intermediateResult = this.requestControllerResult(parserObject, controllerUrl);
break;
}
return intermediateResult.then(callbackFun, errorFun);
}
};
}
/*global Promise, XMLHttpRequest */
function CommandExecutor() {
"use strict";
var helpTexts = {
'help': {
'description': 'Prints help for one of these commands:',
'syntax': '[help-topic]',
'examples': [
{
'example': 'help tabulate',
'description': 'Provides help about the tabulate function'
},
{
'example': 'help correlate',
'description': 'Provides help about the correlate function'
}
],
'relatedcommands': ["describe", "topics", "tabulate", "correlate", "help", "drop", "summarize"]
},
'topics': {
'description': 'lists topics and variables within each topic',
'syntax': '[topic-name]',
'examples': [
{
'example': 'topics',
'description': 'List all topics in the data collection'
},
{
'example': 'topics \'employment\'',
'description': 'List sub-topics and variables for a specified topic'
}
]
},
'describe': {
'description': 'lists variables and variable properties',
'syntax': '[varlist]',
'examples': [
{
'example': 'describe',
'description': 'List all variables and variable properties in the data collection'
},
{
'example': 'describe gender age income',
'description': 'List variable properties for three variables'
}
]
},
'tabulate': {
'description': 'generate tables of frequencies with one or more variables',
'syntax': 'varname1 [varname2] [if] [, options]',
'examples': [
{
'example': 'tabulate gender age',
'description': 'Two-way tabulation'
},
{
'example': 'tabulate gender age, summarize(income)',
'description': 'Two-way tabulation with included summary statistics for income'
}
]
},
'correlate': {
'description': 'generate a correlation matrix',
'syntax': '[varlist] [if]',
'examples': [
{
'example': 'correlate gender age income',
'description': 'Three-way correlation matrix'
},
{
'example': 'correlate age income if gender == \'male\'',
'description': 'Two-way correlation matrix with conditional expression'
}
]
},
'summarize': {
'description': 'list summary statistics for variables',
'syntax': '[varlist] [if]',
'examples': [
{
'example': 'summarize age income',
'description': 'List summary statistics for two variables'
},
{
'example': 'summarize age income if age > 45',
'description': 'List summary statistics for two variables with a conditional expression'
}
]
}
},
req = new XMLHttpRequest(),
ajaxPostPromise = function (postData, url) {
return new Promise(function (resolve, reject) {
req.open('POST', url);
req.onload = function () {
if (req.status === 200) {
resolve(JSON.parse(req.response));
} else {
reject(JSON.parse(req.response));
}
};
req.onerror = function () {
reject({error: {type: "NETWORK", message: "Network Error"}});
};
// Make the request
req.send(JSON.stringify(postData));
});
};
return {
//Eww! This method should not be exported, but it has to be done to
//enable mocking for the tests
requestControllerResult: function (parserObj, controllerUrl) {
return ajaxPostPromise(parserObj, controllerUrl);
},
abort: function () {
if (req.readyState !== 0) {
req.abort();
}
},
execute: function (parserObject, controllerUrl, sessionId, callbackFun, errorFun) {
var intermediateResult, helpText;
switch (parserObject.type) {
case "help":
helpText = helpTexts[parserObject.helpTopic || "help"];
intermediateResult = new Promise(function (resolve) {
resolve(helpText ?
{result: helpText} :
{error: { type: "NO_RESULT", message: "Got no result"}}
);
});
break;
case "history":
intermediateResult = new Promise(function (resolve) {
resolve({result: "history"});
});
break;
default:
if (sessionId) {
parserObject.sessionId = sessionId;
}
intermediateResult = this.requestControllerResult(parserObject, controllerUrl);
break;
}
return intermediateResult.then(callbackFun, errorFun);
}
};
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment