-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
136 lines (111 loc) · 4.03 KB
/
main.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
indexMap = {"search":""}; // 'Maps' list from database to objects in javascript
page_loads = 0; // Tracks how many times the page has been loaded
// Search function
function filter() {
var input = document.getElementById('search');
if (page_loads == 1 && window.location.hash != "") {
var search = window.location.hash.split("#")[1];
}
else{
if (input == "") {
window.location.hash = "";
}
var search = input.value;
window.location.hash = search;
}
indexMap["search"] = search;
return;
}
// Highlights letters that have been searched
function highlight_search(name) {
var new_search = indexMap["search"];
var new_split = name.split(new_search);
var new_name = "";
var html_name = "";
for (var l = 0; l < new_split.length; l++) {
new_name += new_split[l];
html_name += new_split[l];
if (name.indexOf(new_name + new_search) != -1) {
new_name += new_search;
html_name += "<font class='bg-success'>"+new_search+"</font>";
}
}
return html_name;
}
function load_page(php_out) {
var data = make_json(php_out);
page_loads += 1;
filter();
var cur_url = window.location.href.split("#")[0];
var to_append = ""; // to store string of html to append to ul
var div = $("#main_list"); // points to the 'div' (division) html element
div.html(""); // clear all html contained by <div></div>
search = indexMap["search"];
var results_exist = false;
$.each(data, function(key, val) {
if (val["name"].indexOf(search) >= 0) {
results_exist = true;
var html_name = highlight_search(val["name"]);
to_append += "<div class='container' id='"+val["name"]+"'><hr><div class='col-sm-4'><div class='text-center'><p><h4><a href='"+cur_url+val["name"]+"'>"+html_name+"</a></h4></p>"
+"<a href='"+cur_url+val["name"]+"'><img id='"+val["img"].split(".")[0]+"' src='"+val["img"]+"' width='"+val["img_width"]+"' height='"+val["img_height"]+"'></a></div></div>"
+"<div class='col-sm-8'><p><h4>Properties</h4>Contains: "+val["count"]+" plot(s)<br />Last modified: "+val["modified"]+"</p></div></div>";
}
});
if (results_exist == false) {
if (data.length == 0) {
div.append("<div class='container text-center'><p>No entries exist.</p></div>")
}
else {
div.append("<div class='container text-center'><p>No entries matching '"+search+"' exist.</p></div>")
}
}
div.append(to_append);
}
function make_json(php_out) {
var new_json = [];
for (var i = 0; i < php_out.length; i++) {
var dir_obj = php_out[i];
var name = dir_obj["name"];
var count = dir_obj["count"];
var img = dir_obj["img"];
var img_width = dir_obj["img_width"];
var img_height = dir_obj["img_height"];
var modified = new Date(Number(dir_obj["modified"]*1000));
new_json.push({
"name": name,
"count": count,
"img": img,
"img_width": 0.5*Number(img_width),
"img_height": 0.5*Number(img_height),
"modified": modified,
});
}
console.log(new_json);
return new_json;
}
// Refreshes page, need to call here because can't pass php_out arg from html
function refresh() {
load_page(php_out);
}
// Main function
$(function() {
console.log(localStorage);
load_page(php_out);
// Initital hides
$("#load").hide();
$("#load_msg").hide();
$("#finished").hide();
$("#input_err").hide();
$("#internal_err").hide();
$("#SingleMuon").hide();
$("#Cosmics").hide();
// Initial Disables
$("#submit").attr('disabled', 'disabled');
// Prevent 'enter' key from submitting forms (gives 404 error with full data set name form)
$(window).keydown(function(event) {
if (event.keyCode == 13) {
event.preventDefault();
return false;
}
});
});