Permalink
Browse files

Added pushState support for searching reader's corner

  • Loading branch information...
mhlakhani committed Feb 27, 2014
1 parent 8663ceb commit a6cb2008b958f3bd3dce9968112ae602ac698b99
Showing with 61 additions and 12 deletions.
  1. +61 −12 content/readerscornersearch.haml
@@ -61,16 +61,22 @@
for (var key in termResults) {
_tmp.push(termResults[key]);
}
var andResults = _tmp.reduce(function(p, c, i, a) {
return intersectSafe(p, c);
});
var andResults = [];
if (_tmp.length > 0) {
andResults = _tmp.reduce(function(p, c, i, a) {
return intersectSafe(p, c);
});
}
andResults = andResults.sort(function(a,b){return a-b}).filter(function(el,i,a){if(i==a.indexOf(el))return 1;return 0});
var orResults = _tmp.reduce(function(p, c, i, a) {
for (k in c) {
p.push(c[k]);
}
return p;
});
var orResults = [];
if (_tmp.length > 0) {
orResults = _tmp.reduce(function(p, c, i, a) {
for (k in c) {
p.push(c[k]);
}
return p;
});
}
orResults = orResults.sort(function(a,b){return a-b}).filter(function(el,i,a){if(i==a.indexOf(el))return 1;return 0});

for (r in andResults) {
@@ -147,7 +153,10 @@
$( "#search" ).val( ui.item.label );
return false;
},
select: selectfn,
select: function( event, ui ) {
changeStateTo( $( "#search" ).val() );
return false;
},
search: function( event, ui ) {
results = $( "#results" );
results.empty();
@@ -176,8 +185,7 @@
}).keypress(function(e) {
if (e.keyCode === 13)
{
selectfn(0, {item: {label: $( "#search" ).val()}});
$( "#search" ).autocomplete( "close" );
changeStateTo( $( "#search" ).val() );
}
}).data( "ui-autocomplete" )._renderItem = function( ul, item ) {
query = $("#search").val();
@@ -187,6 +195,47 @@
right = item.label.substring(pos+query.length, item.label.length);
return $( "<li>" ).append( "<a>" + left + "<b>" + mid + "</b>" + right + " (" + item.value + ")" + "</a>" ).appendTo( ul )
};

var parseQueryString = function( qs ) {
var a = qs.split('&');
if (a == "") return {};
var b = {};
for (var i = 0; i < a.length; ++i)
{
var p=a[i].split('=');
if (p.length != 2) continue;
b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}
return b;
}

var laststate = "";
var handleStateChange = function( state, type ) {
if (state == laststate) {
return;
}
laststate = state;
var qs = parseQueryString(state);
qs = qs['q'];
if (qs[qs.length-1] == "/") {
qs = qs.substring(0, qs.length-1);
}
$( "#search" ).val(qs);
selectfn(0, {item: {label: $( "#search" ).val()}});
$( "#search" ).autocomplete( "close" );
};

$(window).bind( 'popstate', function(e){
handleStateChange( location.search.replace( /^\?/, '' ), e.type );
}).trigger( 'popstate' );

var changeStateTo = function( state ) {
if ( state !== laststate ) {
history.pushState( {}, this.title || '', '?q=' + encodeURIComponent(state) );
handleStateChange( 'q=' + encodeURIComponent(state), 'click' );
}
}

});

-block css

0 comments on commit a6cb200

Please sign in to comment.