-
Notifications
You must be signed in to change notification settings - Fork 0
/
prototypeInterface.js
112 lines (90 loc) · 4.15 KB
/
prototypeInterface.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var lastSegmentId = 0, nextSegmentToReveal = 0;
var logLines = [];
function logreport(line){
logLines.push(['<span class="report-line segment-', lastSegmentId, (lastSegmentId >= nextSegmentToReveal) ? ' segment-hidden' : '', '">', line, '<br></span>'].join(''));
flushLog();
}
logreport.debug = function() {
if (logreport.debug.on)
return logreport.call(this, Array.prototype.slice.call(arguments));
}
logreport.debug.on = true;
var resultsContainer;
function flushLog() {
if (resultsContainer) {
var fragment = document.createElement('div');
fragment.innerHTML = logLines.join('');
resultsContainer.appendChild(fragment);
logLines = [];
} else {
console.error('Tried to flush log before window loaded.');
}
}
function writeTableau(tableauContent) {
if (resultsContainer) {
var tableauContainer = document.createElement('div');
tableauContainer.innerHTML = '<h2 style="margin-bottom: 5px">Tableau</h2>';
var textareaNote = document.createElement('strong');
textareaNote.innerHTML = 'For copying and pasting into OTWorkplace: ';
tableauContainer.appendChild(textareaNote);
var textarea = document.createElement('textarea');
textarea.className = 'tableau-textarea';
textarea.value = tableauToCsv(tableauContent, '\t');
textarea.readOnly = true;
tableauContainer.appendChild(textarea);
tableauContainer.appendChild(document.createElement('p'));
tableauContainer.className += ' segment-' + lastSegmentId;
if (lastSegmentId >= nextSegmentToReveal)
tableauContainer.className += ' segment-hidden';
onRevealSegment[lastSegmentId] = function() {
textarea.focus();
textarea.select();
}
var htmlTableauContainer = document.createElement('div');
htmlTableauContainer.innerHTML = tableauToHtml(tableauContent);
tableauContainer.appendChild(htmlTableauContainer);
resultsContainer.appendChild(tableauContainer);
} else {
console.error('Tried to write tableau before window loaded.');
}
}
window.addEventListener('load', function(){
resultsContainer = document.getElementById('results-container');
if(typeof runDemo === 'function')
runDemo();
});
var onRevealSegment = {};
function revealNextSegment() {
if (nextSegmentToReveal > lastSegmentId)
return;
var elements = document.getElementsByClassName('segment-' + nextSegmentToReveal);
for (var i = 0; i < elements.length; i++)
elements[i].className = elements[i].className.replace('segment-hidden', '');
if (onRevealSegment[nextSegmentToReveal])
onRevealSegment[nextSegmentToReveal]();
// window.scrollTo(0, document.body.scrollHeight); //TODO: scroll to top of last segment, not bottom
nextSegmentToReveal++;
}
document.addEventListener('keyup', function(event) {
if (event.keyCode === 32)
revealNextSegment();
});
//Given a string that is the name of a (global) object, returns the object itself.
//Given an object, returns that object.
function globalNameOrDirect(nameOrObject) {
if (!window.disableGlobalNameOrDirect && typeof nameOrObject === 'string') {
if (!window.hasOwnProperty(nameOrObject)) {
console.error('globalNameOrDirect error: ' + nameOrObject + ' is not defined in the global namespace')
}
return window[nameOrObject];
} else {
return nameOrObject;
}
}
function runConstraint(constraint, sname, pname, cat, expectedViolations) {
var pTree = globalNameOrDirect(pname);
logreport(['<span class="main-report-line">Running ', constraint, '(', (cat || ''), ') on (', sname, ', ', parenthesizeTree(pTree), ')', (expectedViolations == null) ? '' : [' - Expected Violations: <span class="expected-violation-count">', expectedViolations, '</span>'].join(''), '</span>'].join(''));
var violationCount = globalNameOrDirect(constraint)(globalNameOrDirect(sname), pTree, cat);
logreport(['<span class="main-report-line" style="background-color: white">Actual Violations: <span class="actual-violation-count">', violationCount, '</span></span><br/><br/>'].join(''));
return violationCount;
}