forked from benkrikler/plotify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlotify.js
448 lines (396 loc) · 18.5 KB
/
Plotify.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<!--
////////////////////////////////////////////////////////////////////////////////////////////////
//
// Updating InnerHTML class
//
////////////////////////////////////////////////////////////////////////////////////////////////
function InnerHTML(name,text_contents){
// name (string): The name of the parameter which is used in html tag IDs.
// text_contents (Array of strings and Parameters):
// A list used to build the innerHTML for the named tags. Every
// Parameter in the list is replaced by the filename segment for it's
// current value and then all strings are concatanated in the order
// provided in this argument.
//---------------- Data members -----------------//
this.Name=name;
this.Dependencies=text_contents;
this.TagType=typeof tag_type!== 'undefined' ?tag_type:'h1';
//----------------- makeHtml method -----------------//
// Function to make the Html code for the image
this.makeHtml=function(tagType, cssClass){
//var html="<a id="+this.DownloadID+" target='_blank'>\n";
var html="<"+tagType;
if(cssClass) html+=" class='"+cssClass+"'";
html+=" id='"+this.Name+"'";
html+="</"+tagType+">";
return html;
}
//----------------- writeHtml method -----------------//
// Function to write the Html code for the image
this.writeHtml=function(tagType, cssClass, DivID){
var html=this.makeHtml(tagType, cssClass);
// Define the target for the parameter table.
// If DivID is supplied, use that, else use the parameter's name as the ID of the block.
var target_div=typeof DivID !=='undefined' ? DivID: this.Name;
// Place html for the parameter table into the parameter's div
var section=document.getElementById(target_div);
if(!section) {
alert("Unable to find div with ID:"+ target_div+ " in Image.writeHtml")
return;
}
section.innerHTML+=html;
}
//----------------- update method -----------------//
// Function to update the innerHtml of the block
this.update=function(ChangedParam){
// Check image depends on the parameter (to avoid downloading a new copy of the same image)
if(ChangedParam && this.Dependencies.indexOf(ChangedParam)==-1) return;
// Convert the dependency array to a string (uses the overriden toString
// method in the Parameter class)
var text=this.Dependencies.join("");
var section=document.getElementById(this.Name)
if(!section) {
//alert("Unable to find image html for image: "+ this.Name+
//"\rPerhaps you haven't called writeHtml on this image");
return;
}
section.innerHTML=text
}
//----------------- Register this element -----------------//
// Register with the controllable elements list so that it's informed when
// the parameters are changed
ControllableElements.addElement(this);
}
////////////////////////////////////////////////////////////////////////////////////////////////
//
// Image class
//
////////////////////////////////////////////////////////////////////////////////////////////////
function Image(name,alt,parameter_dependency,display_extension,download_extension){
// name (string): The name of the parameter which is used in html tag IDs.
// alt (string): The alt text used by the picture on mouseover and if the image can't be found.
// parameter_dependency (Array of strings and Parameters):
// A list used to build the filenames for the images. Every Parameter in the list is replaced by the
// filename segment for it's current value and then all strings are concatanated in the order
// provided in this argument.
// display_extension (string): File extension of the images on the webpage
// download_extension [optional] (string): File extension of the images that download when clicked.
// If ommitted, the value of 'display_extension' is used.
//---------------- Data members -----------------//
this.Name=name;
this.Title=alt;
this.Dependencies=parameter_dependency;
this.DisplayExt=display_extension;
this.DownloadExt=typeof download_extension!== 'undefined' ?download_extension:this.DisplayExt;
this.DisplayID=this.Name+"_pic";
this.DownloadID=this.Name+"_file";
//----------------- makeHtml method -----------------//
// Function to make the Html code for the image
this.makeHtml=function(cssClass){
var html="<a id="+this.DownloadID+" target='_blank'>\n";
if(cssClass) html+=" <img class='"+cssClass+"'";
else html+=" <img class='plot'";
html+=" id='"+this.DisplayID+"'";
html+=" alt='"+this.Title+"'";
html+=" title='"+this.Title+"'";
html+="/></a>";
return html;
}
//----------------- writeHtml method -----------------//
// Function to write the Html code for the image
this.writeHtml=function(cssClass,DivID){
var html=this.makeHtml(cssClass);
// Define the target for the parameter table.
// If DivID is supplied, use that, else use the parameter's name as the ID of the block.
var target_div=typeof DivID !=='undefined' ? DivID: this.Name;
// Place html for the parameter table into the parameter's div
var section=document.getElementById(target_div);
if(!section) {
alert("Unable to find div with ID:"+ target_div+ " in Image.writeHtml")
return;
}
section.innerHTML+=html;
}
//----------------- update method -----------------//
// Function to update the image
this.update=function(ChangedParam){
// Check image depends on the parameter (to avoid downloading a new copy of the same image)
if(ChangedParam && this.Dependencies.indexOf(ChangedParam)==-1) return;
// Convert the dependency array to a string (uses the overriden toString method in the Parameter class)
var filename=this.Dependencies.join("");
var section=document.getElementById(this.DisplayID)
if(!section) {
//alert("Unable to find image html for image: "+ this.Name+ "\rPerhaps you haven't called writeHtml on this image");
return;
}
section.setAttribute('src',filename+"."+this.DisplayExt);
document.getElementById(this.DownloadID).setAttribute('href',filename+"."+this.DownloadExt);
}
//----------------- Register this image -----------------//
// Register image with the controllable elements list so that it's informed when the parameters are changed
ControllableElements.addElement(this);
}
////////////////////////////////////////////////////////////////////////////////////////////////
//
// Parameter class
//
////////////////////////////////////////////////////////////////////////////////////////////////
function Parameter(name,values,filenames){
// name (string): The name of the parameter which is used in html tag IDs.
// values (Array of strings): The possible values of the parameter as to be displayed on the webpage
// filenames (Array of strings): An array that corresponds to 'values' array and provides the
// component for the filename for a given parameter value
//----------------- Data members -----------------//
this.Name=name;
this.Values=values.slice(0);
if(filenames) this.Filenames=filenames.slice(0);
else this.Filenames=values.slice(0);
this.CurrentVal=0;
this.Type="";
//----------------- writeHtml -----------------//
// Create the html code for the Parameter
this.writeHtml=function(startup,columns,divID,type){
// startup (integer): An integer giving the index of the startup value for the parameter.
// Must be in the range [0,values.length[
// columns (integer): An integer specifying the number of columns to use in the parameter value table.
// divID [optional]: The ID of the div block that should contain the parameter.
// If not supplied, the parameter's name (this.Name) is used instead.
// type (string) [optional]: what sort of Html should be written. If
// left blank a series of links are placed in
// a table. Values are: table, select
this.CurrentVal=startup;
this.Type=type;
// Create html for the table
var html=""
if(this.Type=="table" || !this.Type ){
html=this.makeHtmlTable(columns);
}else if (this.Type =="select"){
html=this.makeHtmlSelect(columns);
}
// Define the target for the parameter table.
// If divID is supplied, use that, else use the parameter's name as the ID of the block.
var target_div=typeof divID !=='undefined' ? divID: this.Name;
// Place html for the parameter table into the parameter's div
var section=document.getElementById(target_div);
if(!section) {
alert("Unable to find div with ID:"+ target_div+ " in Image.writeHtml")
return;
}
section.innerHTML+=html;
//Set the link styles
this.setLinks()
}
//----------------- makeHtmlTable method -----------------//
// Function to make the Html code for the parameter as a table of links
this.makeHtmlTable=function(columns){
var html= "<table class='value_table' id='tab_"+this.Name+"'>";
for(i=0;i<this.Values.length;){
html=html.concat("<tr>");
for(var j=0;j<columns;j++){
html=html.concat("<td>");
html=html.concat("<a id="+this.getValueID(i)+" href=javascript:"+this.Name+".changeValue("+i+")> "+this.Values[i]+" </a>");
html=html.concat("</td>");
i++; //Increment parameter counter as we've added another value
if(i>this.Values.length) break; // Not all rows may be complete
}
html=html.concat("</tr>");
}
html=html.concat("</table>");
return html;
}
//----------------- makeHtmlSelect method -----------------//
// Function to make the Html code for the parameter as a select list
this.makeHtmlSelect=function(columns){
var tag_id="select_"+this.Name;
this.TagId=tag_id;
var html= "<select class='value_select' id='"+tag_id+"' onchange=\"ParamSelectValue("+this.Name+",'"+tag_id+"')\">";
for(i=0;i<this.Values.length;i++){
html=html.concat("<option id="+this.getValueID(i)+" value="+i+"> "+this.Values[i]+" </options>");
}
html=html.concat("</select>");
return html;
}
//----------------- changeValue -----------------//
// This function is used as the href target for the parameter value links
this.changeValue=function(newVal){
if(newVal==-1) return;
this.CurrentVal=newVal;
this.setLinks();
if(this.Type=="select") this.updateSelectBox();
ControllableElements.updateControllables(this);
}
//----------------- incrementValue -----------------//
// This function allows us to step through the values of this parameter easily
this.addToValue=function(step){
if(!step) step=1;
var newVal=this.Values.length + this.CurrentVal+step;
newVal=newVal%this.Values.length;
this.changeValue(newVal);
}
//----------------- makeCycleButton method -----------------//
// Function to make the Html code for a button that toggles cycling of
// values
this.makeCycleButton=function(target_div,period){
//var html="<div ";
//html+="id='button_"+this.Name+"'";
//html+="class=play_button ";
//html+="title='start/stop cycling through each value' ";
//html+="onclick='"+this.Name+".toggleCycling("+period+")'";
//html+="</div>";
//var html="<button ";
//html+="onclick='javascript:"+this.Name+".toggleCycling("+period+")' ";
//html+="id='button_"+this.Name+"'>Start Loop</button>";
var html="<a class=loop_button ";
html+="href='javascript:"+this.Name+".toggleCycling("+period+")' ";
html+="id='button_"+this.Name+"'>Loop</a>";
// Place html for the parameter table into the parameter's div
var section=document.getElementById(target_div);
if(!section) {
alert("Unable to find div with ID:"+ target_div+ " in Image.writeHtml")
return;
}
section.innerHTML+=html;
}
//----------------- toggleCycling -----------------//
// Begin/end a loop over the values of this parameter
this.toggleCycling=function(period){
// do we start or stop cycling?
var button=document.getElementById("button_"+this.Name);
var startText="Loop";
var stopText="Stop";
if(button.innerHTML==startText){
button.innerHTML=stopText;
//if(button.getAttribute("class")=="play_button"){
// button.setAttribute("class","stop_button");
this.startCycling(period);
} else if(button.innerHTML==stopText){
button.innerHTML=startText;
//}else if(button.getAttribute("class")=="stop_button"){
// button.setAttribute("class","play_button");
this.stopCycling();
}
}
//----------------- startCycling -----------------//
// Begin a loop over the values of this parameter
this.startCycling=function(period){
this.LoopTimer=setInterval(this.Name+".addToValue(1)",period);
}
//----------------- startCycling -----------------//
// Begin a loop over the values of this parameter
this.stopCycling=function(){
clearInterval(this.LoopTimer);
}
//----------------- setLinks -----------------//
// Function to set the style of the parameter values
// Called when the link is clicked
this.setLinks=function(){
for(var i=0;i<this.Values.length;i++){
if(i==this.CurrentVal){
document.getElementById(this.getValueID(i)).setAttribute('class','current_value');
}else{
document.getElementById(this.getValueID(i)).setAttribute('class','value');
}
}
}
//----------------- updateSelectBox -----------------//
// Function to set value of the select in case it was changed indirectly
this.updateSelectBox=function(){
document.getElementById(this.TagId).value=this.CurrentVal;
}
//----------------- getValueID -----------------//
//Helper function to produce the id for the requested cell in the parameter table
this.getValueID=function(index){
return this.Name+"_"+index;
}
//----------------- getIndexOfValue -----------------//
//Helper function to produce the id for the requested cell in the parameter table
this.getIndexOfValue=function(value){
var index= this.Filenames.indexOf(parseFloat(value));
if(index<0) index=this.Filenames.indexOf(value);
return index;
}
//----------------- Register this parameter -----------------//
// Register parameter on the ParameterList object
ParameterList[this.Name]=this;
}
//----------------- toString -----------------//
// Override the default toString function to return the current value of the parameter
Parameter.prototype.toString=function(){
//return (this.Name+"_"+this.Filenames[this.CurrentVal]);
return (this.Filenames[this.CurrentVal]);
}
//----------------- Parameter List -----------------//
// An object to store all created parameters on (for ease of looping over all
// parameters and setting values from the URL
ParameterList=new Object();
////////////////////////////////////////////////////////////////////////////////////////////////
//
// Controllable Element List
//
////////////////////////////////////////////////////////////////////////////////////////////////
// Object to control a register of the controllable elements (images, tables, etc etc), that depend on the parameters.
// Any time a parameter has its value changed, the parameter tells this object to update all images with the new value.
ControllableElements = new function(){
// List of all images to loop over when a parameter is changed
this.ElementList=new Array();
// extend image list
this.addElement=function(element){
this.ElementList.push(element);
}
// Function to alert all elements that a parameter has changed
this.updateControllables=function(ChangedParam){
// Loop over all images and tell them to update
for(i=0;i<this.ElementList.length;i++){
this.ElementList[i].update(ChangedParam);
}
}
// Function to obtain a given image
this.findElement=function(name){
for(var i in this.ElementList){
if(this.ElementList[i].Name==name){
return this.ElementList[i];
}
}
return null;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
//
// Functions for loading the page
//
////////////////////////////////////////////////////////////////////////////////////////////////
function ProcessUserOptions(){
// Get all the user supplied parameters
var parameters = location.search.substring(1);
if(parameters=="") {return;}
parameters = parameters.split("&");
// Iterate over each parameter and if such a parameter exists set it's
// current value to be the requested one
for (var i in parameters){
var input =parameters[i].split("=");
if(ParameterList.hasOwnProperty(input[0])){
var param=ParameterList[input[0]];
var index =param.getIndexOfValue(input[1]);
if(index>=0)param.changeValue(index);
else alert("Parameter '"+input[0]+"' has no value, '"+input[1]+"'")
} else {
alert("Parameter '"+input[0]+"' not found.");
}
}
}
function ParamSelectValue(parameter,id){
//alert(parameter);
//alert(id);
var section=document.getElementById(id);
if(!section) {
alert("Unable to find div with ID:"+ id+ " in ParamSelectValue")
return;
}
var newVal=section.options[section.selectedIndex].value;
parameter.changeValue(newVal);
}
window.onload=function(){
ProcessUserOptions();
ControllableElements.updateControllables();
}
// -->