-
Notifications
You must be signed in to change notification settings - Fork 1
/
plots.php
231 lines (205 loc) · 6.22 KB
/
plots.php
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Ben's DataMC plots</title>
<script src="jquery-1.11.3.min.js"></script>
</head>
<body>
<div id="plots"></div>
<?php
// Used for debugging the php
/*echo 'Current PHP version: ' . phpversion();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);*/
function FindDirs($dir) {
$directories = array();
$foldercontent = scandir($dir);
natsort($foldercontent);
foreach ($foldercontent as $item) {
// print("item: $item\n");
if ("$item" === "." or "$item" === ".." or "$item" === ".git") continue;
$absitem = "$dir/$item";
if (is_dir($absitem)) {
$directories[] = "$item";
$subdirs = FindDirs($absitem);
foreach ($subdirs as $subitem) {
$directories[] = "$item/$subitem";
}
}
}
return $directories;
}
function FindImages($folder, $filters = array(), $excludes = array()) {
$images = array();
$files = scandir($folder);
foreach ($files as $file) {
if (substr($file, -4) === ".svg" or substr($file, -4) === ".png") {
$skip = False;
foreach ($filters as $filter) {
if (!strpos("$folder/$file",$filter)) {
$skip = True;
break;
}
}
foreach ($excludes as $exclude) {
if (strpos("$folder/$file",$exclude)) {
$skip = True;
break;
}
}
if ($skip) {continue;}
$images[] = "$folder/$file";
}
}
sort($images);
return $images;
}
// Make file names array key, useful for plot comparison
function FileNameArray($array, $removeIndicies = array(), $fixButtonFunction = "") {
$newarray = array();
foreach ($array as $i => $value) {
foreach ($value as $plot) {
$folders = explode("/", $plot);
$filename = array_pop($folders);
$filename = substr($filename, 0, strlen($filename) - 4);
foreach ($removeIndicies as $removeIndex) {
unset($folders[$removeIndex]);
}
$button = implode("/",$folders);
$button = "$button/$filename";
if ($fixButtonFunction !== "") {
$button = $fixButtonFunction($button);
}
if (array_key_exists($button, $newarray)) {
$newarray[$button][] = $plot;
} else {
$newarray[$button] = array($plot);
}
}
}
return $newarray;
}
// Way to have different display name to actual name
function FixButton($button) {
// Example usage:
// $button = str_replace("geq2j","2ji", $button);
// $button = str_replace("geq2b","2bi", $button);
// $button = str_replace("geq4j","4ji", $button);
// $button = str_replace("geq4b","4bi", $button);
return $button;
}
$baseDir = "./";
$directories = FindDirs($baseDir);
// Strings go in arrays to selectively choose buttons
$filter = array();
$exclude = array();
$categories = array();
foreach ($directories as $dir) {
// print("$dir\n");
$images = findImages("$baseDir/$dir", $filter, $exclude);
if (count($images) !== 0) {
$button = FixButton($dir);
$categories[$button] = $images;
}
}
// Example usage of File Name Array:
// $categories = FileNameArray($categories, array(0,1,2,3), 'FixButton');
?>
<script>
function getUrlParameters(parameter){
parArr = window.location.search.substring(1).split("&");
for(var i = 0; i < parArr.length; i++){
parr = parArr[i].split("=");
if(parr[0] == parameter){
return decodeURIComponent(parr[1]);
}
}
return false;
}
function plotHTML(categories, index) {
var thisplots = categories[index];
var html = ""
if (thisplots && thisplots.length >= 1) {
// Make title
html += "</br>";
html += index;
html += "</br>";
for (var i = 0; i < thisplots.length; i += 1) {
html += "<a href=\"";
html += thisplots[i];
html += "\"><img src=\"";
html += thisplots[i];
html += "\" height=\"275\" /></a>";
}
} else {
html += "</br><big>No plots found in ";
html += index;
html += ":(</big></br>";
}
return html;
}
function sameRoot(folder1, folder2) {
var root1 = folder1.substr(0,folder1.search("/[^/]*$"));
var root2 = folder2.substr(0,folder2.search("/[^/]*$"));
if (root1 === root2) {
return true;
} else {
return false;
}
}
categories = <?php echo json_encode($categories); ?>;
jQuery(document).ready(function() {
var buttons = "";
var lastButton = "";
var tablify = true;
if (tablify) {
buttons += "<table cellpadding=0 cellspacing=0><tr>";
}
Object.keys(categories).forEach(function(category) {
if (!sameRoot(lastButton, category)) {
if (tablify) {
buttons += "</tr><tr>"
} else {
buttons += "</br>";
}
}
if (tablify) {
buttons += "<td>"
}
lastButton = category;
buttons += "<input type='button' value='" + category + "'>";
if (tablify) {
buttons += "</td>"
}
});
if (tablify) {
buttons += "</tr></table>"
}
$("body").prepend(buttons);
// Loop over buttons (input elements)
$("input").each(function( index, element ) {
// Add listener for clicking button
$( element ).on('click', function(event) {
// Change content
$("#plots").empty().append(plotHTML(categories, this.value));
// Change URL
var newURL = window.location.pathname + "?sel=" + this.value;
history.replaceState({}, "", newURL);
// Colour button text
$("input").each(function( index, element ) {$(element).css("color", "")});
$( this ).css("color", "Red");
})
});
var param = getUrlParameters("sel");
var index = Object.keys(categories)[0];
if (param) {index = param;}
$("#plots").empty().append(plotHTML(categories, index));
// Colour button text
$("input").each(function(thisIndex,element) {
if (element.value === index) { $(element).css("color", "Red") }
});
});
</script>
</body>
</html>