Repository navigation
Expand file tree
/
Copy pathsearch.js
More file actions
178 lines (156 loc) · 5.17 KB
/
Copy pathsearch.js
File metadata and controls
178 lines (156 loc) · 5.17 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
var oldValue = document.getElementById('search').value;
var currentView = null;
$(document).ready(function(){
$("#search").keyup(function(){
// Retrieve the input field text and reset the count to zero
var search = $(this).val();
$("#selection_list").empty()
search.toLowerCase()
search_data(search);
});
$("#search").click(function(){
return false;
});
});
$(function() {
var names = nameList; // Defined in functions.js // getNames();
var thing = $( "#search" ).autocomplete({
width: 320,
source: names,
messages: {
noResults: "",
results: function() {}
},
select: function( event, ui ) {
event.preventDefault();
}
});
$('#search').keypress(function(e){
if (e.which == 13) {
return false;
}
// if (e.which == 13) e.preventDefault();
});
var renderItem = thing.data("autocomplete")._renderItem;
/* Override the _renderItem function to display the placeholder item */
thing.data("autocomplete")._renderItem = function(ul, item) {
return $("<li class=\"ui-menu-item\"></li>")
.data("ui-menu-item", item)
.append("<a>" + item.value + "</a>")
.appendTo(ul);
};
});
function checkSearch() {
var searchBar = document.getElementById('search');
var searchTerm = document.getElementById('search').value;
if (searchTerm.length == 0 || searchTerm == "Search...") {
} else {
console.log("The search term " + searchTerm)
search_data(searchTerm);
}
}
$(document).ready(function() {
$('a.ui-corner-all').click(function(e) {
e.preventDefault();
});
});
function OnSubtreeModified () {
var searchTerm = document.getElementById('search').value;
if (searchTerm.length > 0) {
console.log("The search term " + searchTerm)
search_data(searchTerm);
}
}
document.getElementById('search').addEventListener(Event.CHANGE, checkSearch, false);
setInterval(function() {
var searchBar = document.getElementById('search');
var searchTerm = document.getElementById('search').value;
if ((searchTerm.length > 0 && oldValue != searchTerm )|| search.valueOf() === "Search...") {
var anchors = document.getElementsByTagName('a')
for (var i = 0; i < anchors.length + anchors.length; i++ ) {
var anchor = ( i < anchors.length ) ? anchors[i] : anchors[i-anchors.length];
anchor.addEventListener("click", OnSubtreeModified, false);
}
console.log("The search term " + searchTerm)
oldValue = searchTerm;
search_data(searchTerm);
}
}, 100);
// Popover code ------------------------
// Close the popover by tapping anywhere
// http://stackoverflow.com/questions/8947749/
var isVisible = false;
var clickedAway = false;
$('#zippopover').popover({
html: true,
trigger: 'manual'
}).click(function(e) {
$(this).popover('show');
$('#zip').focus();
clickedAway = false
isVisible = true
e.preventDefault()
});
// Must be placed here. Can't be inside the document click function.
function whoisrep_callback(jsonData){
if (jsonData.response.legislators.length > 0) {
// TODO handle multiple reps per zip code.
// if (jsonData.response.legislators.length > 3) {
// alert("Multiple reps found for that zip code. Try entering your full zip such as 91361-2038");
// };
// Special case for the district that stanford is in because
// the current rep lost and we are showing future reps.
var crpid = jsonData.response.legislators[0].legislator.crp_id;
if (crpid == "N00007364") {
crpid = "N00029649";
};
// Switch to Rep. View if not in it already
if(!$('#house').hasClass('active')) {
$('#house').button('toggle');
d3.select("svg").remove();
var svg = d3.select("body").append("svg")
.attr("id", "canvas")
.attr("width", 1100)
.attr("height", 900);
whois_reps_toselect.push(crpid);
initializeVizAndMenu("house");
} else {
var oneArray = new Array();
oneArray.push(crpid);
select_cand_ids(oneArray);
}
} else {
alert("A representative for that zipcode could not be found.");
};
}
// For closing the popover and clicking submit
$(document).click(function(e) {
// If you press the submit button
if (e.target.id == "zipbutton") {
$(document).ready(function(){
$.ajax({
type: 'get',
url: 'http://services.sunlightlabs.com/api/legislators.allForZip?apikey=de9c6b058ea542a4a5aab1d845d56c83&zip='+ $('#zip').val() +'&jsonp=whoisrep_callback',
dataType: 'jsonp',
success: whoisrep_callback
});
})
}
// Don't close if you are touching the zip textfield
if (e.target.id != "zip") {
var clickedClass = e.target.getAttribute("class");
console.log(clickedClass)
if (clickedClass != "candidate cand_unlocked" && clickedClass != "subcl" && clickedClass != "menuitem" && clickedClass != "sector_bar" && clickedClass != "test") {
$("#search").attr("value", "");
console.log("IN SELECTION");
select_all_cand();
};
if(isVisible & clickedAway) {
$('#zippopover').popover('hide')
isVisible = clickedAway = false
}
else {
clickedAway = true
}
};
});