From 18458b83c0940cd560423c80394fd7b42a5ee87e Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Fri, 27 Mar 2015 12:03:23 +0900 Subject: [PATCH] Adding sort code to the client --- server.go | 2 +- static/index.html | 14 +++++++------- static/scripts/search.js | 20 +++++++++++++++++++- types.go | 18 +++++++++--------- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/server.go b/server.go index cadaa30..85541dc 100644 --- a/server.go +++ b/server.go @@ -52,7 +52,7 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) { features := fixFeatures(request.Features) foundEntries := findRecords(entries, features, request.MinScore) - sorter := recordSorter{entries: foundEntries, key: request.SortKey, ascending: request.SortAscending} + sorter := recordSorter{entries: foundEntries, key: request.SortKey, ascending: request.SortAsc} sorter.sort() response := jsonQueryResponse{ diff --git a/static/index.html b/static/index.html index e97976f..47cebc6 100644 --- a/static/index.html +++ b/static/index.html @@ -96,13 +96,13 @@ - - - - - - - + + + + + + + {{#each records}} diff --git a/static/scripts/search.js b/static/scripts/search.js index 6c2273d..c4d46b9 100644 --- a/static/scripts/search.js +++ b/static/scripts/search.js @@ -35,7 +35,12 @@ } function onReady(geo) { - _ctx = {geo: geo, query: {}}; + _ctx = { + sortKey: 'score', + sortAsc: false, + query: {}, + geo: geo + }; Handlebars.registerHelper('prettyFloat', function(precision, options) { return parseFloat(options.fn(this)).toFixed(precision); @@ -55,12 +60,25 @@ onSearch(); }; + window.sortReviewsBy = function(sortKey) { + if (sortKey === _ctx.sortKey) { + _ctx.sortAsc = !_ctx.sortAsc; + } + else { + _ctx.sortKey = sortKey; + } + + onSearch(); + }; + onSearch(); } function onSearch() { _ctx.query = { features: _ctx.query.features || {}, + sortKey: _ctx.sortKey, + sortAsc: _ctx.sortAsc, profile: getProfile(), walkingDist: parseFloat($('#walkingDist').val()), minScore: parseFloat($('#minScore').val()), diff --git a/types.go b/types.go index 5d345da..05dfa4a 100644 --- a/types.go +++ b/types.go @@ -37,15 +37,15 @@ type jsonGeoData struct { } type jsonQueryRequest struct { - Features featureMap `json:"features"` - Geo *jsonGeoData `json:"geo"` - HintSteps int `json:"hintSteps"` - MaxResults int `json:"maxResults"` - MinScore float64 `json:"minScore"` - Profile featureMap `json:"profile"` - SortAscending bool `json:"SortAscending"` - SortKey string `json:"sortKey"` - WalkingDist float64 `json:"walkingDist"` + Features featureMap `json:"features"` + Geo *jsonGeoData `json:"geo"` + HintSteps int `json:"hintSteps"` + MaxResults int `json:"maxResults"` + MinScore float64 `json:"minScore"` + Profile featureMap `json:"profile"` + SortAsc bool `json:"sortAsc"` + SortKey string `json:"sortKey"` + WalkingDist float64 `json:"walkingDist"` } type jsonColumn struct {
NameDistance to userClosest stationDistance to stationCompatibilityScoreAccess countNameDistance to userClosest stationDistance to stationCompatibilityScoreAccess count