-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithm.js
More file actions
300 lines (235 loc) · 7.68 KB
/
Algorithm.js
File metadata and controls
300 lines (235 loc) · 7.68 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright 2011 David Galles, University of San Francisco. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of the University of San Francisco
function addLabelToAlgorithmBar(labelName)
{
var element = document.createTextNode(labelName);
var tableEntry = document.createElement("td");
tableEntry.appendChild(element);
var controlBar = document.getElementById("AlgorithmSpecificControls");
//Append the element in page (in span).
controlBar.appendChild(tableEntry);
return element;
}
// TODO: Make this stackable like radio butons
// (keep backwards compatible, thought)
function addCheckboxToAlgorithmBar(boxLabel)
{
var element = document.createElement("input");
element.setAttribute("type", "checkbox");
element.setAttribute("value", boxLabel);
var label = document.createTextNode(boxLabel);
var tableEntry = document.createElement("td");
tableEntry.appendChild(element);
tableEntry.appendChild(label);
var controlBar = document.getElementById("AlgorithmSpecificControls");
//Append the element in page (in span).
controlBar.appendChild(tableEntry);
return element;
}
function addRadioButtonGroupToAlgorithmBar(buttonNames, groupName)
{
var buttonList = [];
var newTable = document.createElement("table");
for (var i = 0; i < buttonNames.length; i++)
{
var midLevel = document.createElement("tr");
var bottomLevel = document.createElement("td");
var button = document.createElement("input");
button.setAttribute("type", "radio");
button.setAttribute("name", groupName);
button.setAttribute("value", buttonNames[i]);
bottomLevel.appendChild(button);
midLevel.appendChild(bottomLevel);
var txtNode = document.createTextNode(" " + buttonNames[i]);
bottomLevel.appendChild(txtNode);
newTable.appendChild(midLevel);
buttonList.push(button);
}
var topLevelTableEntry = document.createElement("td");
topLevelTableEntry.appendChild(newTable);
var controlBar = document.getElementById("AlgorithmSpecificControls");
controlBar.appendChild(topLevelTableEntry);
return buttonList
}
function addControlToAlgorithmBar(type, name) {
var element = document.createElement("input");
element.setAttribute("type", type);
element.setAttribute("value", name);
element.setAttribute("name", name);
var tableEntry = document.createElement("td");
tableEntry.appendChild(element);
var controlBar = document.getElementById("AlgorithmSpecificControls");
//Append the element in page (in span).
controlBar.appendChild(tableEntry);
return element;
}
function Algorithm(am)
{
}
Algorithm.prototype.init = function(am, w, h)
{
this.animationManager = am;
am.addListener("AnimationStarted", this, this.disableUI);
am.addListener("AnimationEnded", this, this.enableUI);
am.addListener("AnimationUndo", this, this.undo);
this.canvasWidth = w;
this.canvasHeight = h;
this.actionHistory = [];
this.recordAnimation = true;
this.commands = []
}
// Overload in subclass
Algorithm.prototype.sizeChanged = function(newWidth, newHeight)
{
}
Algorithm.prototype.implementAction = function(funct, val)
{
var nxt = [funct, val];
this.actionHistory.push(nxt);
var retVal = funct(val);
this.animationManager.StartNewAnimation(retVal);
}
Algorithm.prototype.isAllDigits = function(str)
{
for (var i = str.length - 1; i >= 0; i--)
{
if (str.charAt(i) < "0" || str.charAt(i) > "9")
{
return false;
}
}
return true;
}
Algorithm.prototype.normalizeNumber = function(input, maxLen)
{
if (!this.isAllDigits(input) || input == "")
{
return input;
}
else
{
return ("OOO0000" +input).substr(-maxLen, maxLen);
}
}
Algorithm.prototype.disableUI = function(event)
{
// to be overridden in base class
}
Algorithm.prototype.enableUI = function(event)
{
// to be overridden in base class
}
function controlKey(keyASCII)
{
return keyASCII < 47 || keyASCII == 37 || keyASCII == 38 ||
keyASCII == 39 || keyASCII == 40;
}
Algorithm.prototype.returnSubmit = function(field, funct, maxsize, intOnly)
{
if (maxsize != undefined)
{
field.size = maxsize;
}
return function(event)
{
var keyASCII = 0;
if(window.event) // IE
{
keyASCII = event.keyCode
}
else if (event.which) // Netscape/Firefox/Opera
{
keyASCII = event.which
}
if (keyASCII == 13)
{
funct();
}
else if (keyASCII == 59 )
{
return false;
}
else if ((maxsize != undefined && field.value.length >= maxsize) ||
intOnly && (keyASCII < 48 || keyASCII > 57))
{
if (!controlKey(keyASCII))
return false;
}
}
}
Algorithm.prototype.addReturnSubmit = function(field, action)
{
field.onkeydown = this.returnSubmit(field, action, 4, false);
}
Algorithm.prototype.reset = function()
{
// to be overriden in base class
// (Throw exception here?)
}
Algorithm.prototype.undo = function(event)
{
// Remvoe the last action (the one that we are going to undo)
this.actionHistory.pop();
// Clear out our data structure. Be sure to implement reset in
// every AlgorithmAnimation subclass!
this.reset();
// Redo all actions from the beginning, throwing out the animation
// commands (the animation manager will update the animation on its own).
// Note that if you do something non-deterministic, you might cause problems!
// Be sure if you do anything non-deterministic (that is, calls to a random
// number generator) you clear out the undo stack here and in the animation
// manager.
//
// If this seems horribly inefficient -- it is! However, it seems to work well
// in practise, and you get undo for free for all algorithms, which is a non-trivial
// gain.
var len = this.actionHistory.length;
this.recordAnimation = false;
for (var i = 0; i < len; i++)
{
this.actionHistory[i][0](this.actionHistory[i][1]);
}
this.recordAnimation = true;
}
Algorithm.prototype.clearHistory = function()
{
this.actionHistory = [];
}
// Helper method to add text input with nice border.
// AS3 probably has a built-in way to do this. Replace when found.
// Helper method to create a command string from a bunch of arguments
Algorithm.prototype.cmd = function()
{
if (this.recordAnimation)
{
var command = arguments[0];
for(i = 1; i < arguments.length; i++)
{
command = command + "<;>" + String(arguments[i]);
}
this.commands.push(command);
}
}