-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoRepl.js
101 lines (80 loc) · 2.64 KB
/
autoRepl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var toRun = [
{
description: "Pig Latin",
buttons: [{title: "Translate", action: config => config.showOutput(pigLatinWords(config.getInput())) }]
},
{
description: "This shows probabilities for where a character might end up when starting in a location with equal probability of moving in any direction for a certain number of moves.",
buttons: [{title: "Animate", action: boardInteractor.showAnimation}, {title: "Show", action: boardInteractor.showStep}]
}
];
const buildDisplay = (elementId, runConfig) => {
console.log(`Buidling display for element ${elementId}`);
const contentElement = document.getElementById(elementId);
contentElement.className = "contentArea";
const addInDiv = (newElement) => {
const div = document.createElement("div");
div.className = "contentItem";
div.appendChild(newElement);
contentElement.appendChild(div);
};
_.tap(document.createTextNode(runConfig.description), (desc) => {
addInDiv(desc);
});
const input = _.tap(document.createElement("input"), (input) => {
input.className = "form-control";
addInDiv(input);
});
const output = _.tap(document.createElement("textArea"), (output) => {
output.className = "form-control";
});
const getInput = () => {
const inputVal = input.value;
console.log(`${elementId}: Returning input value ${inputVal}`);
return inputVal;
};
const showOutput = (content) => {
console.log(`${elementId}: Showing output ${content}`);
output.value = content;
}
const elementConfig = {
getInput,
showOutput
};
_.tap(document.createElement("div"), (buttonsDiv) => {
_.each(runConfig.buttons, (buttonConfig) => {
_.tap(document.createElement("button"), (runButton) => {
runButton.appendChild(document.createTextNode(buttonConfig.title));
runButton.className = "btn btn-default";
runButton.onclick = () => buttonConfig.action(elementConfig);
buttonsDiv.appendChild(runButton);
});
});
addInDiv(buttonsDiv);
});
addInDiv(output);
}
$.ajaxPrefilter( function (options) {
if (options.crossDomain && jQuery.support.cors) {
var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
}
});
const makeRequest = (path, handler, error) => {
console.log(`Making request to ${path}`);
$.ajax({
url: path,
type: "GET",
dataType: "json",
jsonpCallback: 'callback',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
success: handler,
error: error
});
};
const makeAndDisplayRequest = () => {
// http://xkcd.com/615/info.0.json
makeRequest(getInput(), (result) => showOutput(JSON.stringify(result, null, 2)));
}