Skip to content

Commit b137c3d

Browse files
committed
Added function show_hide() with consistent policy for hiding and showing elements.
1 parent 181f927 commit b137c3d

File tree

1 file changed

+54
-33
lines changed

1 file changed

+54
-33
lines changed

media/search.js

+54-33
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,14 @@ function google_link(name) {
146146
' target="_blank">' + name + '</a>';
147147
}
148148

149+
function set_prefix(prefix) {
150+
$('#id_left').val(prefix).focus();
151+
return false;
152+
}
153+
149154
function group_row(prefix, keys) {
150155
var html = '<a class="quiet" href="#"';
151-
html += 'onclick="$(\'#id_left\').val(\'' + prefix + '\'); return false;"';
156+
html += 'onclick="return set_prefix(\'' + prefix + '\');"';
152157
html += '>' + prefix + ' ';
153158
html += '<img src="/static/images/right9.png" alt=">"';
154159
html += ' width="9" height="9">';
@@ -215,6 +220,9 @@ function split_group(groups, old_prefix) {
215220
groups[best_prefix].unshift(key);
216221
}
217222
}
223+
if (groups[old_prefix].length == 0) {
224+
delete groups[old_prefix];
225+
}
218226
}
219227

220228
function make_groups(groups, max_count) {
@@ -229,36 +237,36 @@ function make_groups(groups, max_count) {
229237
}
230238
}
231239
// If the largest group is small enough, we're done.
232-
if (best_count <= 8) break;
240+
if (best_count <= 6) break;
233241
// Split the largest group into two groups.
234242
split_group(groups, best_prefix);
235243
}
236244
}
237245

238246
function update_html() {
247+
$.keys = [];
248+
for (var key in $.domains) $.keys.push(key);
249+
if ($.keys.length == 0) return show_hide();
250+
$.keys.sort(function(a, b) {
251+
return $.domains[b].score - $.domains[a].score });
252+
var top10_index = Math.min($.keys.length - 1, 10);
253+
$.ajax_search.top10 = $.domains[$.keys[top10_index]].score;
254+
var groups = {};
255+
groups[$.ajax_search.left] = $.keys;
256+
make_groups(groups, 25);
257+
var html = p_html(groups);
258+
$("#results_p").html(html);
259+
show_hide();
260+
}
261+
262+
function show_hide() {
239263
if ($.ajax_search.left + $.ajax_search.right == '') {
240264
$("div#results_div").hide();
241265
$("div#results_loading").hide();
242266
$("div#results_empty").hide();
243-
$("div#feedback").hide();
244267
$("div#welcome").show();
245-
return;
246-
}
247-
var keys = [];
248-
for (var key in $.domains) keys.push(key);
249-
keys.sort(function(a, b) {
250-
return $.domains[b].score - $.domains[a].score });
251-
var results_count = keys.length;
252-
if (results_count) {
253-
var top10_index = Math.min(results_count - 1, 10);
254-
$.ajax_search.top10 = $.domains[keys[top10_index]].score;
255-
}
256-
var groups = {};
257-
groups[$.ajax_search.left] = keys;
258-
make_groups(groups, 25);
259-
var html = p_html(groups);
260-
$("div#welcome").hide();
261-
if (results_count == 0) {
268+
} else if ($.keys.length == 0) {
269+
$("div#welcome").hide();
262270
$("div#results_div").hide();
263271
if ($.ajax_search.start) {
264272
$("#results_empty").hide();
@@ -268,16 +276,33 @@ function update_html() {
268276
$("#results_empty").show();
269277
}
270278
} else {
279+
$("div#welcome").hide();
271280
$("#results_empty").hide();
272281
$("#results_loading").hide();
273-
$("#results_p").html(html);
274282
$("#results_div").show();
275-
$("div#feedback").show();
276-
if (results_count <= DEFAULT_LIMIT / 2 && !$.ajax_search.start)
283+
if ($.keys.length <= DEFAULT_LIMIT / 2 && !$.ajax_search.start)
277284
$("#results_few").show();
278285
else
279286
$("#results_few").hide();
280287
}
288+
if ($.mode == 'basic') {
289+
if ($.ajax_search.left || $.ajax_search.right)
290+
$("div#feedback").show();
291+
else
292+
$("div#feedback").hide();
293+
$("div#weights").hide();
294+
$("div#priority").show();
295+
$("div.three").css("top", 438);
296+
$("div.four").css("top", 510);
297+
$("div#left").css("height", 591);
298+
} else {
299+
$("div#feedback").hide();
300+
$("div#priority").hide();
301+
$("div#weights").show();
302+
$("div.three").css("top", 606);
303+
$("div.four").css("top", 678);
304+
$("div#left").css("height", 663);
305+
}
281306
}
282307

283308
function array_unchanged(a, b) {
@@ -436,23 +461,19 @@ function update_if_changed(i) {
436461
}
437462

438463
function show_weights() {
439-
$("div#priority").hide();
440-
$("div#weights").show();
441-
$("div.three").css("top", 606);
442-
$("div.four").css("top", 678);
443-
$("div#left").css("height", 759);
464+
$.mode = 'advanced';
465+
show_hide();
444466
}
445467

446468
function show_priority() {
447-
$("div#weights").hide();
448-
$("div#priority").show();
449-
$("div.three").css("top", 438);
450-
$("div.four").css("top", 510);
451-
$("div#left").css("height", 591);
469+
$.mode = 'basic';
470+
show_hide();
452471
}
453472

454473
function document_ready() {
455474
$.domains = {};
475+
$.keys = [];
476+
$.mode = 'basic';
456477
$.weights = form_weights();
457478
$.ajax_search = {};
458479
$.ajax_search.xhr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

0 commit comments

Comments
 (0)