diff options
author | Jochen Topf <jochen@topf.org> | 2013-01-18 19:16:40 +0100 |
---|---|---|
committer | Jochen Topf <jochen@topf.org> | 2013-01-18 19:16:40 +0100 |
commit | 71e774e39117dc51e27902d64ee8a70341a08ef3 (patch) | |
tree | 5a214d90dbfdb65585cc64fd8ca768dbd026b643 /web | |
parent | ee83b7feeca5e0bf0d2e29275ee73919f28bdb60 (diff) | |
download | taginfo-71e774e39117dc51e27902d64ee8a70341a08ef3.tar taginfo-71e774e39117dc51e27902d64ee8a70341a08ef3.tar.gz |
Add role search
Diffstat (limited to 'web')
-rw-r--r-- | web/lib/api/v4/search.rb | 41 | ||||
-rw-r--r-- | web/views/search.erb | 6 | ||||
-rw-r--r-- | web/viewsjs/search.js.erb | 35 |
3 files changed, 78 insertions, 4 deletions
diff --git a/web/lib/api/v4/search.rb b/web/lib/api/v4/search.rb index cf8d5e1..edc2710 100644 --- a/web/lib/api/v4/search.rb +++ b/web/lib/api/v4/search.rb @@ -84,6 +84,47 @@ class Taginfo < Sinatra::Base }.to_json end + api(4, 'search/by_role', { + :description => 'Search for relation roles.', + :parameters => { :query => 'Role to search for (substring search, required).' }, + :sort => %w( count_all rtype role ), + :paging => :optional, + :result => paging_results([ + [:rtype, :STRING, 'Relation type.'], + [:role, :STRING, 'Role'], + [:count_all, :INT, 'Number of objects in the database with this role.'] + ]), + :example => { :query => 'foo', :page => 1, :rp => 10 }, + :ui => '/search?q=foo#roles' + }) do + query = params[:query] + + total = @db.count('db.relation_roles'). + condition_if("role LIKE '%' || ? || '%'", query). + get_first_value().to_i + + res = @db.select('SELECT * FROM db.relation_roles'). + condition_if("role LIKE '%' || ? || '%'", query). + order_by(@ap.sortname, @ap.sortorder) { |o| + o.count_all + o.rtype + o.role + }. + paging(@ap). + execute() + + return { + :page => @ap.page, + :rp => @ap.results_per_page, + :total => total, + :data => res.map{ |row| { + :rtype => row['rtype'], + :role => row['role'], + :count_all => row['count_all'].to_i, + }} + }.to_json + end + api(4, 'search/by_value', { :description => 'Search for tags by value.', :parameters => { :query => 'Value to search for (substring search, required).' }, diff --git a/web/views/search.erb b/web/views/search.erb index 30212f7..8b0af63 100644 --- a/web/views/search.erb +++ b/web/views/search.erb @@ -6,6 +6,7 @@ <ul> <li><a href="#keys"><%= t.osm.keys %></a></li> <li><a href="#values"><%= t.osm.values %></a></li> + <li><a href="#roles"><%= t.osm.relation_member_roles %></a></li> <li><a href="#fulltext"><%= t.pages.search.fulltext %></a></li> </ul> <div id="keys"> @@ -18,6 +19,11 @@ <table id="grid-values"> </table> </div> + <div id="roles"> + <h2><%= t.osm.relation_member_roles %></h2> + <table id="grid-roles"> + </table> + </div> <div id="fulltext"> <h2><%= t.pages.search.fulltext %></h2> <p class="boxpre" style="color: #f00000;">This search is experimental. It shows keys and tags that might be related to the word you searched for. This doesn't work if there are several words.</p> diff --git a/web/viewsjs/search.js.erb b/web/viewsjs/search.js.erb index 5ce7e72..9dcafa2 100644 --- a/web/viewsjs/search.js.erb +++ b/web/viewsjs/search.js.erb @@ -3,17 +3,22 @@ misc = @trans.t.misc search = @trans.t.pages.search %> -function link_to_key_with_highlight(key, highlight) { + +function highlight(str, query) { + return str.replace(new RegExp('(' + query + ')', 'gi'), "<b>$1</b>") +} + +function link_to_key_with_highlight(key, query) { return link( url_for_key(key), - key.replace(new RegExp('(' + highlight + ')', 'gi'), "<b>$1</b>") + highlight(key, query) ); } -function link_to_value_with_highlight(key, value, highlight) { +function link_to_value_with_highlight(key, value, query) { return link( url_for_tag(key, value), - value.replace(new RegExp('(' + highlight + ')', 'gi'), "<b>$1</b>") + highlight(value, query) ); } @@ -60,6 +65,28 @@ var create_flexigrid_for = { } }); }, + roles: function(query) { + create_flexigrid('grid-roles', { + url: '/api/4/search/by_role?query=' + encodeURIComponent(query), + colModel: [ + { display: '<%= misc.count %>', name: 'count_all', width: 80, sortable: true, align: 'right' }, + { display: '<%= osm.relation_types %>', name: 'rtype', width: 250, sortable: true }, + { display: '<%= osm.relation_member_roles %>', name: 'role', width: 500, sortable: true } + ], + sortname: 'count_all', + sortorder: 'desc', + preProcess: function(data) { + data.rows = jQuery.map(data.data, function(row, i) { + return { 'cell': [ + fmt_with_ts(row.count_all), + link_to_rtype(row.rtype), + highlight(html_escape(row.role), query) + ] }; + }); + return data; + } + }); + }, tags: function(query) { var q = query.split('=', 2); create_flexigrid('grid-tags', { |