Skip to content

Commit

Permalink
fixed search and tag forms close #246
Browse files Browse the repository at this point in the history
  • Loading branch information
jywarren committed Mar 6, 2015
1 parent e3d3baf commit d652a76
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 101 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//
//= require jquery
//= require jquery_ujs
//= require bootstrap-css/js/bootstrap.min.js
//= require bootstrap-css/js/bootstrap.js
//= require bootstrap-lightbox/build/bootstrap-lightbox.min.js
//= require bootstrap-datepicker/js/bootstrap-datepicker.js
//= require jquery-file-upload/js/vendor/jquery.ui.widget.js
Expand Down
26 changes: 14 additions & 12 deletions app/assets/javascripts/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,25 @@ jQuery(document).ready(function() {

/* tag autocomplete */
$('#taginput').typeahead({
source: function (typeahead, input) {
query = input.split(',')[input.split(',').length-1]
if (query.length > 2) {
return $.post('/tag/suggested/'+query, {}, function (data) {
return typeahead.process(data)
items: 8,
minLength: 3,
source: function (query, process) {
var term = query.split(',').pop()
if (term.length > 2) {
return $.post('/tag/suggested/'+term, {}, function (data) {
return process(data)
})
}
},
menu: '<ul id="tagtypeahead" class="typeahead dropdown-menu"></ul>',
autoselect: false,
matcher: function() { return true; },
onselect: function(text,original_text) {
original_text = original_text.split(',')
matcher: function() {
return true
},
updater: function(text) {
original_text = $('#taginput').val().split(',')
original_text.pop()
original_text = original_text.join(',')
if (original_text == '') $('#taginput').val(text)
else $('#taginput').val(original_text+','+text)
if (original_text == '') return text
else return original_text+','+text
}
});

Expand Down
33 changes: 18 additions & 15 deletions app/assets/javascripts/search.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
$('#searchform').submit(function(e){
e.preventDefault()
window.location = '/search/'+$('#searchform_input').val()
})
// working off of http://stackoverflow.com/questions/9232748/twitter-bootstrap-typeahead-ajax-example
$('#searchform_input').typeahead({
source: function (typeahead, query) {
if (query.length > 2) {
jQuery(document).ready(function() {

$('#searchform').submit(function(e){
e.preventDefault()
window.location = '/search/'+$('#searchform_input').val()
})

$('#searchform_input').typeahead({
items: 15,
minLength: 3,
source: function (query, process) {
return $.post('/search/typeahead/'+query, {}, function (data) {
return typeahead.process(data)
return process(data)
})
},
updater: function(item) {
var url = $(item)[0].attributes['data-url'].value
window.location = url
}
},
items: 15,
//highlighter: function(a) {a},
autoselect: false,
autowidth: false,
menu: '<ul id="searchtypeahead" class="typeahead dropdown-menu"></ul>'
})

})
8 changes: 5 additions & 3 deletions app/assets/javascripts/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ jQuery(document).ready(function($) {
}

/* there may or may not actually be a carousel to activate */
$('#sidebar-carousel').carousel({
interval: 6000
})
if ($('#sidebar-carousel * *').length > 0) {
$('#sidebar-carousel').carousel({
interval: 6000
})
}
})
13 changes: 8 additions & 5 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def index
@tagnames = params[:id].split(',')
@users = DrupalUsers.find(:all, :limit => 5, :order => "uid", :conditions => ['name LIKE ? AND access != 0', "%"+params[:id]+"%"])
set_sidebar :tags, [params[:id]]
@notes = DrupalNode.paginate(:order => "node.nid DESC", :conditions => ['status = 1 AND (node.title LIKE ? OR node_revisions.title LIKE ? OR node_revisions.body LIKE ?)', "%"+params[:id]+"%","%"+params[:id]+"%","%"+params[:id]+"%"], :page => params[:page], :include => :drupal_node_revision, :page => params[:page])
@notes = DrupalNode.paginate(:order => "node.nid DESC", :conditions => ['(type = "note" OR type = "page" OR type = "map") AND status = 1 AND (node.title LIKE ? OR node_revisions.title LIKE ? OR node_revisions.body LIKE ?)', "%"+params[:id]+"%","%"+params[:id]+"%","%"+params[:id]+"%"], :page => params[:page], :include => :drupal_node_revision, :page => params[:page])
render :template => 'search/index'
end

Expand All @@ -26,16 +26,19 @@ def advanced
def typeahead
matches = []
DrupalNode.find(:all, :limit => 5, :order => "nid DESC", :conditions => ['type = "note" AND status = 1 AND title LIKE ?', "%"+params[:id]+"%"], :select => "title,type,nid,path").each do |match|
matches << {:string => "<i class='icon-file'></i> "+match.title, :url => match.path}
matches << "<i data-url='"+match.path+"' class='icon-file'></i> "+match.title
end
DrupalNode.find(:all, :limit => 5, :order => "nid DESC", :conditions => ['(type = "page" OR type = "place" OR type = "tool") AND status = 1 AND title LIKE ?', "%"+params[:id]+"%"], :select => "title,type,nid,path").each do |match|
matches << {:string => match.icon+" "+match.title, :url => match.path}
matches << "<i data-url='"+match.path+"' class='icon-"+match.icon+"'></i> "+match.title
end
DrupalNode.find(:all, :limit => 5, :order => "nid DESC", :conditions => ['type = "map" AND status = 1 AND title LIKE ?', "%"+params[:id]+"%"], :select => "title,type,nid,path").each do |match|
matches << {:string => "<i class='icon-map-marker'></i> "+match.title, :url => match.path}
matches << "<i data-url='"+match.path+"' class='icon-"+match.icon+"'></i> "+match.title
end
DrupalUsers.find(:all, :limit => 5, :order => "uid", :conditions => ['name LIKE ? AND access != 0', "%"+params[:id]+"%"]).each do |match|
matches << {:string => "<i class='icon-user'></i> "+match.name, :url => "/profile/"+match.name}
matches << "<i data-url='/profile/"+match.name+"' class='icon-user'></i> "+match.name
end
DrupalTag.includes(:drupal_node).where('node.status = 1').limit(5).where('name LIKE ?', "%"+params[:id]+"%").each do |match|
matches << "<i data-url='/tag/"+match.name+"' class='icon-tag'></i> "+match.name
end
render :json => matches
end
Expand Down
13 changes: 9 additions & 4 deletions app/controllers/tag_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,16 @@ def delete
end

def suggested
suggestions = []
DrupalTag.find(:all, :conditions => ['name LIKE ?', "%"+params[:id]+"%"], :limit => 10).each do |tag|
suggestions << {:string => tag.name.downcase}
if params[:id].length > 2
suggestions = []
# filtering out tag spam by requiring tags attached to a published node
DrupalTag.where('name LIKE ?', "%"+params[:id]+"%").includes(:drupal_node).where('node.status = 1').limit(10).each do |tag|
suggestions << tag.name.downcase
end
render :json => suggestions.uniq
else
render :json => []
end
render :json => suggestions.uniq
end

def rss
Expand Down
10 changes: 5 additions & 5 deletions app/models/drupal_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,11 @@ def mailing_list

# used in typeahead autocomplete search results
def icon
icon = "<i class='icon-file'></i>" if self.type == "note"
icon = "<i class='icon-book'></i>" if self.type == "page"
icon = "<i class='icon-map-marker'></i>" if self.type == "map"
icon = "<i class='icon-flag'></i>" if self.type == "place"
icon = "<i class='icon-wrench'></i>" if self.type == "tool"
icon = "file" if self.type == "note"
icon = "book" if self.type == "page"
icon = "map-marker" if self.type == "map"
icon = "flag" if self.type == "place"
icon = "wrench" if self.type == "tool"
icon
end

Expand Down
34 changes: 16 additions & 18 deletions app/views/map/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= javascript_include_tag "dragdrop" %>
<div class="span3">
<%= javascript_include_tag "dragdrop" %>

<h4>Use good metadata!</h4>
<p>Remember to tag maps with useful terms like "ndvi", "gulf-coast", etc.</p>
Expand Down Expand Up @@ -84,24 +84,27 @@
<script>
jQuery(document).ready(function() {

/* tag autocomplete */
$('#taginput').typeahead({
source: function (typeahead, input) {
query = input.split(',')[input.split(',').length-1]
if (query.length > 2) {
return $.post('/tag/suggested/'+query, {}, function (data) {
return typeahead.process(data)
items: 8,
minLength: 3,
source: function (query, process) {
var term = query.split(',').pop()
if (term.length > 2) {
return $.post('/tag/suggested/'+term, {}, function (data) {
return process(data)
})
}
},
menu: '<ul id="tagtypeahead" class="typeahead dropdown-menu"></ul>',
autoselect: false,
matcher: function() { return true; },
onselect: function(text,original_text) {
original_text = original_text.split(',')
matcher: function() {
return true
},
updater: function(text) {
original_text = $('#taginput').val().split(',')
original_text.pop()
original_text = original_text.join(',')
if (original_text == '') $('#taginput').val(text)
else $('#taginput').val(original_text+','+text)
if (original_text == '') return text
else return original_text+','+text
}
});

Expand Down Expand Up @@ -154,8 +157,3 @@
})
})()
</script>
<link rel="stylesheet" href="/jquery-file-upload/css/jquery.fileupload-ui.css">
<script src="/jquery-file-upload/js/vendor/jquery.ui.widget.js"></script>
<script src="/jquery-file-upload/js/jquery.iframe-transport.js"></script>
<script src="/jquery-file-upload/js/jquery.fileupload.js"></script>
<script src="/assets/js/dragdrop.js?5"></script>
15 changes: 0 additions & 15 deletions app/views/map/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,6 @@

<div class="span9">

<!--
<div class="navbar">
<div class="navbar-inner">
<form class="navbar-form pull-right" style="margin-left:10px;" action="/search/advanced">
<input type="hidden" name="maps" value="on" />
<input placeholder="search maps" type="text" name="q" class="input search-query" data-provide="typeahead" data-source='["Balloon mapping","Thermal photography","Spectrometry"]'>
<button type="submit" class="btn btn-inverse"><i class="icon-search icon-white"></i></button>
</form>
<ul class="nav">
<li class="active"><a href="#">Recent</a></li>
<li><a href="#">Popular</a></li>
</ul>
</div>
</div> -->

<%= render :partial => "map/maps" %>

<hr />
Expand Down
22 changes: 10 additions & 12 deletions app/views/search/_search.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@
e.preventDefault()
window.location = '/search/'+$('#inline_searchform_input').val()
})
// working off of http://stackoverflow.com/questions/9232748/twitter-bootstrap-typeahead-ajax-example
$('#inline_searchform_input').typeahead({
source: function (typeahead, query) {
return $.post('/search/typeahead/'+query, {}, function (data) {
return typeahead.process(data)
})
},
minLength: 3,
items: 15,
highlighter: function(a) {a},
// Autoselect is disabled so that users can enter new tags
autoselect: false,
autowidth: false
items: 8,
minLength: 3,
source: function (query, process) {
var term = query.split(',').pop()
if (term.length > 2) {
return $.post('/search/typeahead/'+term, {}, function (data) {
return process(data)
})
}
}
})
})
</script>
Expand Down
4 changes: 2 additions & 2 deletions app/views/search/advanced.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
<% @nodes.each do |node| %>
<tr class="<%= node.type %>">
<% if node.type == "comment" %>
<td><%= raw node.icon %> Comment by <a href="<%= node.author.name %>"><%= node.author.name %></a></td>
<td><i class="icon-<%= node.icon %>"></i> Comment by <a href="<%= node.author.name %>"><%= node.author.name %></a></td>
<% else %>
<td><%= raw node.icon %> <a href="<%= node.path %>"><%= node.title %></a></td>
<td><i class="icon-<%= node.icon %>"></i> <a href="<%= node.path %>"><%= node.title %></a></td>
<% end %>
<td><% if node.type != "page" %><a href="/profile/<%= node.author.name %>"><%= node.author.name %></a><% end %></td>
<td><% node.tags.each do |tag| %><a class="label label-info" href="/tag/<%= tag.name %>"><%= tag.name %></a> <% end %></td>
Expand Down
19 changes: 10 additions & 9 deletions app/views/tag/_tagging.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
<input autocomplete="off" id="taginput" name="name" type="text" placeholder="enter tags" data-provide="typeahead" />
<script>
jQuery(document).ready(function() {

$('#tagform').bind('ajax:beforeSend', function(){
$("#taginput").prop('disabled',true)
});

$('#tagform').bind('ajax:success', function(e,response){
response = JSON.parse(response)
$.each(response['saved'],function(i,tag) {
Expand All @@ -40,6 +42,7 @@
}
$('#taginput').prop('disabled',false)
});

$('#tagform').bind('ajax:error', function(e,response){
$('#tagform .control-group').addClass('error')
$('#taginput').prop('disabled',false)
Expand All @@ -52,16 +55,14 @@
});

$('#taginput').typeahead({
source: function (typeahead, query) {
if (query.length > 2) {
return $.post('/tag/suggested/'+query, {}, function (data) {
return typeahead.process(data)
})
}
items: 8,
minLength: 3,
source: function (query, process) {
return $.post('/tag/suggested/'+query, {}, function (data) {
return process(data)
})
},
menu: '<ul id="tagtypeahead" class="typeahead dropdown-menu"></ul>',
autoselect: false,
onselect: function(text,original_text) {
updater: function(text) {
$('#taginput').val(text)
$('#tagform').submit()
}
Expand Down

0 comments on commit d652a76

Please sign in to comment.