Skip to content

Commit

Permalink
Improved Search typeahead a bit with terms
Browse files Browse the repository at this point in the history
  • Loading branch information
bnvk committed Oct 15, 2014
1 parent e34f593 commit a528494
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion static/default/css/default.css

Large diffs are not rendered by default.

41 changes: 30 additions & 11 deletions static/default/html/jsapi/app/activities.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Mailpile.activities.compose = function(address) {

Mailpile.activities.render_typeahead = function() {

var tagMatcher = function(strs) {
var baseMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substrRegex;
// an array that will be populated with substring matches
Expand All @@ -26,7 +26,7 @@ Mailpile.activities.render_typeahead = function() {
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str.name)) {
if (substrRegex.test(str.term)) {
// the typeahead jQuery plugin expects suggestions to a
// JavaScript object, refer to typeahead docs for more info
matches.push(str);
Expand All @@ -36,13 +36,28 @@ Mailpile.activities.render_typeahead = function() {
};
};

var tagMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substrRegex;
matches = [];
substrRegex = new RegExp(q, 'i');
$.each(strs, function(i, str) {
if (substrRegex.test(str.name)) {
matches.push(str);
}
});
cb(matches);
};
};

var peopleMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substrRegex;
matches = [];
substrRegex = new RegExp(q, 'i');
$.each(strs, function(i, str) {
if (substrRegex.test(str.fn) || substrRegex.test(str.address)) {
str.term = 'from:';
matches.push(str);
}
});
Expand All @@ -51,26 +66,30 @@ Mailpile.activities.render_typeahead = function() {
};

// List of basic suggestions for search helpers
var helpers = ['to: ', 'from: ', 'subject: ', 'contacts: ', 'keys: '];
var helpers = [
{ value: 'dates:', helper: '2014-10-13' },
{ value: 'subject:', helper: 'any normal words' },
{ value: 'contacts: ', helper: 'name@email.com' },
{ value: 'to:', helper: 'name@email.com' },
{ value: 'keys:', helper: 'name@email.com / keyid' }
];

// Create Typeahead
$('#form-search .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 0
},{
/*
name: 'search',
displayKey: 'value',
source: tagMatcher(helpers),
source: baseMatcher(helpers),
templates: {
suggestion: function(value) {
var template = _.template('<div class="tt-suggestion"><p><span class="icon-search"></span> <%= value %></p></div>');
return template(value);
suggestion: function(data) {
var template = _.template('<div class="tt-suggestion"><p><span class="icon-search"></span> <%= value %> <span class="helper"><%= helper %></span></p></div>');
return template(data);
}
}
},{
*/
name: 'tags',
displayKey: function(value) {
return 'in:' + value.slug
Expand All @@ -88,15 +107,15 @@ Mailpile.activities.render_typeahead = function() {
},{
name: 'people',
displayKey: function(value) {
return 'from:' + value.address;
return value.term + value.address;
},
source: peopleMatcher(Mailpile.instance.addresses),
templates: {
header: '<span class="separator"></span>',
empty: '<div class="tt-suggestion"><p><span class="icon-user"></span> No people match your search</p></div>',
suggestion: function(data) {
if (data.photo === undefined) { data.photo = '/static/img/avatar-default.png'; }
var template = _.template('<div class="tt-suggestion"><p><img class="avatar" src="<%= photo %>"> <%= fn %></p></div>');
var template = _.template('<div class="tt-suggestion"><p><img class="avatar" src="<%= photo %>"> <%= term %> <%= fn %></p></div>');
return template(data);
}
}
Expand Down
4 changes: 4 additions & 0 deletions static/default/less/libraries/typeahead.less
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
border-top: 1px solid @grayMid;
}

.tt-suggestion .helper {
color: @gray;
}

.tt-suggestion .avatar {
width: 24px;
border-radius: @base-border-radius;
Expand Down

0 comments on commit a528494

Please sign in to comment.