0 | 0 |
/*
|
1 | |
* This file is part of yoob.js version 0.6
|
|
1 |
* This file is part of yoob.js version 0.7
|
2 | 2 |
* Available from https://github.com/catseye/yoob.js/
|
3 | 3 |
* This file is in the public domain. See http://unlicense.org/ for details.
|
4 | 4 |
*/
|
|
18 | 18 |
* - step
|
19 | 19 |
* - load
|
20 | 20 |
* - edit
|
|
21 |
* - reset
|
21 | 22 |
*
|
22 | 23 |
* - a slider control which adjusts the speed of program state evolution.
|
23 | 24 |
*
|
|
90 | 91 |
* are the actions a controller can undertake, and the values
|
91 | 92 |
* are either DOM elements or strings; if strings, DOM elements
|
92 | 93 |
* with those ids will be obtained from the document and used.
|
|
94 |
*
|
|
95 |
* When the button associated with e.g. 'start' is clicked,
|
|
96 |
* the corresponding method (in this case, 'click_start()')
|
|
97 |
* on this Controller will be called. These functions are
|
|
98 |
* responsible for changing the state of the Controller (both
|
|
99 |
* the internal state, and the enabled status, etc. of the
|
|
100 |
* controls), and for calling other methods on the Controller
|
|
101 |
* to implement the particulars of the action.
|
|
102 |
*
|
|
103 |
* For example, 'click_step()' calls 'performStep()' which
|
|
104 |
* calls 'step()' (which a subclass or instantiator must
|
|
105 |
* provide an implementation for.)
|
|
106 |
*
|
|
107 |
* To simulate one of the buttons being clicked, you may
|
|
108 |
* call 'click_foo()' yourself in code. However, that will
|
|
109 |
* be subject to the current restrictions of the interface.
|
|
110 |
* You may be better off calling one of the "internal" methods
|
|
111 |
* like 'performStep()'.
|
93 | 112 |
*/
|
94 | 113 |
this.connect = function(dict) {
|
95 | 114 |
var $this = this;
|
96 | 115 |
|
97 | |
var keys = ["start", "stop", "step", "load", "edit"];
|
|
116 |
var keys = ["start", "stop", "step", "load", "edit", "reset"];
|
98 | 117 |
for (var i in keys) {
|
99 | 118 |
var key = keys[i];
|
100 | 119 |
var value = dict[key];
|
|
120 | 139 |
if (key === 'speed') {
|
121 | 140 |
this.speed.value = this.delay;
|
122 | 141 |
this.speed.onchange = function(e) {
|
123 | |
$this.delay = speed.value;
|
|
142 |
$this.setDelayFrom($this.speed);
|
124 | 143 |
if ($this.intervalId !== undefined) {
|
125 | 144 |
$this.stop();
|
126 | 145 |
$this.start();
|
|
205 | 224 |
this.loadSource(text);
|
206 | 225 |
};
|
207 | 226 |
|
|
227 |
/*
|
|
228 |
* This is the basic idea, but not fleshed out yet.
|
|
229 |
* - Should we cache the source somewhere?
|
|
230 |
* - While we're waiting, should we disable the UI / show a spinny?
|
|
231 |
*/
|
|
232 |
this.loadSourceFromURL = function(url, errorCallback) {
|
|
233 |
var http = new XMLHttpRequest();
|
|
234 |
var $this = this;
|
|
235 |
if (!errorCallback) {
|
|
236 |
errorCallback = function(http) {
|
|
237 |
$this.loadSource(
|
|
238 |
"Error: could not load " + url + ": " + http.statusText
|
|
239 |
);
|
|
240 |
}
|
|
241 |
}
|
|
242 |
http.open("get", url, true);
|
|
243 |
http.onload = function(e) {
|
|
244 |
if (http.readyState === 4 && http.responseText) {
|
|
245 |
if (http.status === 200) {
|
|
246 |
$this.loadSource(http.responseText);
|
|
247 |
} else {
|
|
248 |
errorCallback(http);
|
|
249 |
}
|
|
250 |
}
|
|
251 |
};
|
|
252 |
http.send(null);
|
|
253 |
};
|
|
254 |
|
208 | 255 |
this.click_edit = function(e) {
|
209 | 256 |
this.click_stop();
|
210 | 257 |
if (this.controls.edit) this.controls.edit.style.display = "none";
|
|
237 | 284 |
this.click_stop = function(e) {
|
238 | 285 |
this.stop();
|
239 | 286 |
this.state = PAUSED;
|
|
287 |
/* why is this check here? ... */
|
240 | 288 |
if (this.controls.stop && this.controls.stop.disabled) {
|
241 | 289 |
return;
|
242 | 290 |
}
|
|
259 | 307 |
clearInterval(this.intervalId);
|
260 | 308 |
this.intervalId = undefined;
|
261 | 309 |
};
|
|
310 |
|
|
311 |
this.click_reset = function(e) {
|
|
312 |
this.click_stop();
|
|
313 |
this.load(this.source.value);
|
|
314 |
if (this.controls.start) this.controls.start.disabled = false;
|
|
315 |
if (this.controls.step) this.controls.step.disabled = false;
|
|
316 |
if (this.controls.stop) this.controls.stop.disabled = true;
|
|
317 |
};
|
|
318 |
|
|
319 |
/*
|
|
320 |
* Override this to change how the delay is acquired from the 'speed'
|
|
321 |
* element.
|
|
322 |
*/
|
|
323 |
this.setDelayFrom = function(elem) {
|
|
324 |
this.delay = elem.max - elem.value;
|
|
325 |
};
|
262 | 326 |
};
|