Permalink
Browse files

fixed up a bunch of js

  • Loading branch information...
kswedberg committed Sep 9, 2010
1 parent fa6da5c commit 9cc354b127999dc705c8c19a127289d170c1f7f2
Showing with 76 additions and 25 deletions.
  1. +28 −8 scripts/basic-search.js
  2. +48 −17 scripts/search.js
View
@@ -15,7 +15,8 @@ var KS = {
url: 'http://api.jquery.com/jsonp/?callback=?'
};
-var $form = $('#jqas');
+var $form = $('#jqas'),
+ $log = $('#log');
$('#toggle-advanced').bind('click', function() {
var $span = $(this).find('span');
@@ -25,7 +26,6 @@ $('#toggle-advanced').bind('click', function() {
});
$.fn.includeParams = function() {
- KS.params = this.find('input:text, input:radio').serialize();
KS.include = this.find('input:checkbox').serializeArray();
KS.includes = {};
for (var i=0; i < KS.include.length; i++) {
@@ -34,22 +34,41 @@ $.fn.includeParams = function() {
return this;
};
+var textVals = function() {
+ return $form.find('input:text').map(function() {
+ return this.value;
+ }).get().join('');
+};
+
$('#search-again').bind('click', function(event) {
event.preventDefault();
$(this).toggleClass('js-hide');
$form.find('fieldset').slideToggle();
});
-
+$('#clear-results').bind('click', function(event) {
+ event.preventDefault();
+ $form.find('input:text').val(function() {
+ return '';
+ });
+ $form.submit();
+});
$form.bind('submit', function(event) {
event.preventDefault();
+ if ( !textVals() ) {
+ return $log.html('');
+ }
$('#search-again').trigger('click');
+ var $paramInputs = $form.find('input:text, input:radio');
+
+
+ KS.params = $paramInputs.serialize();
$form.includeParams();
- if (KS.params in KS.cache) {
+ if (KS.params in KS.cache) {
outputResults( KS.cache[KS.params] );
} else {
- $('#log').html('<blink style="color: #999;">loading ...</blink>');
+ $log.html('<blink style="color: #999;">loading ...</blink>');
$.getJSON(KS.url, KS.params, function(json) {
outputResults(json, true);
});
@@ -144,7 +163,8 @@ var buildItem = {
// build the long description for each entry
longdesc: function(item) {
if (KS.includes['longdesc']) {
- return '<div class="longdesc">' + item.longdesc + '</div>';
+ var lngdesc = item.longdesc.replace(/(img .*?src=")\//g,'$1http://api.jquery.com/');
+ return '<div class="longdesc">' + lngdesc + '</div>';
}
return '';
}
@@ -184,14 +204,14 @@ function outputResults(json, xhr) {
if (list.length) {
var $list = $('<ol>' + list.join('') + '</ol>');
- $('#log')
+ $log
.empty()
.append($list)
.prepend('<ol class="toc">' + toc.join('') + '</ol>')
.prepend( resultMsg(entryCount) );
} else {
- $('#log').html('<p>Sorry, nothing found.</p>');
+ $log.html('<p>Sorry, nothing found.</p>');
}
}
View
@@ -17,15 +17,10 @@ var KS = {
'http://api.dev/jsonp/?callback=?' :
'http://api.jquery.com/jsonp/?callback=?'
};
-var $form = $('#jqas');
+var $form = $('#jqas'),
+ $log = $('#log');
-$('#title').focus();
-
-$('#search-again').bind('click', function(event) {
- event.preventDefault();
- $(this).toggleClass('js-hide');
- $(this).closest('form').find('fieldset').slideToggle();
-});
+$('#title')[0].focus();
$.fn.includeParams = function() {
KS.include = this.find('input:checkbox').serializeArray();
@@ -36,16 +31,33 @@ $.fn.includeParams = function() {
}
return this;
};
+var textVals = function() {
+ return $form.find('input:text').map(function() {
+ return this.value;
+ }).get().join('');
+};
-$form.bind('submit', function(event) {
+$('#search-again').bind('click', function(event) {
event.preventDefault();
+ $(this).toggleClass('js-hide');
+ $(this).closest('form').find('fieldset').slideToggle();
+});
- $('#search-again').trigger('click');
+$('a[href=#]').live('click', function() {
+ return false;
+});
+
+$form.bind('submit', function(event) {
+ event.preventDefault();
KS.params = $form.find('input:text, input:radio').serialize();
$form.includeParams();
+ if ( !textVals() ) {
+ $log.html('');
+ } else {
+ $.bbq.pushState('#' + KS.params);
+ }
- $.bbq.pushState('#' + KS.params);
});
@@ -56,25 +68,43 @@ $form.find('input:checkbox').bind('click', function(event) {
$(window).bind('hashchange', function(event, initial) {
var search = $.param.fragment();
+
+ if ( !window.location.hash ) {
+ return $log.html('');
+ }
+
if ( $.bbq.getState('scrollTarget') && !initial ) {
return;
}
- if (!window.location.hash) {
- $('#log').html('');
- } else if (search in KS.cache) {
+
+ if (search in KS.cache) {
outputResults( KS.cache[search] );
} else {
$('#log').html('<blink style="color: #999;">loading ...</blink>');
$.getJSON(KS.url, search, function(json) {
outputResults(json, true);
});
}
+ if (!initial) {
+ $('#search-again').trigger('click');
+ }
+
});
+// set up initial set of parameters and populate form if necessary
+$form.includeParams();
+var initialFrag = $.deparam.fragment();
+$form.find('input:text').val(function() {
+ return initialFrag[this.name] || '';
+});
// trigger the hashchange on page load so we can return to the initial state
// and in case the user goes directly to a search
-$form.includeParams();
-$(window).trigger('hashchange', true);
+if (!textVals()) {
+ $.bbq.removeState();
+} else {
+ $(window).trigger('hashchange', true);
+}
+
var buildItem = {
@@ -155,7 +185,8 @@ var buildItem = {
// build the long description for each entry
longdesc: function(item) {
if (KS.includes['longdesc']) {
- return '<div class="longdesc">' + item.longdesc + '</div>';
+ var lngdesc = item.longdesc.replace(/(img .*?src=")\//g,'$1http://api.jquery.com/');
+ return '<div class="longdesc">' + lngdesc + '</div>';
}
return '';
}

0 comments on commit 9cc354b

Please sign in to comment.