From bc46e953f7c13f3a50172ee51acd13145939a942 Mon Sep 17 00:00:00 2001 From: epriestley Date: Wed, 1 Aug 2012 12:36:33 -0700 Subject: [PATCH] Improve ordering of main typeahead results Summary: Always order applications first, then users, then other results (currently, there are no other results, of course). (This is similar to the general ordering algorithm used in JX.Prefab but has enough current/future differences that I split it rather than trying to share them.) Test Plan: Queried results, verified order. Reviewers: vrana Reviewed By: vrana CC: aran Maniphest Tasks: T1569 Differential Revision: https://secure.phabricator.com/D3117 --- src/__celerity_resource_map__.php | 5 +- .../core/behavior-search-typeahead.js | 58 +++++++++++++++++-- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/__celerity_resource_map__.php b/src/__celerity_resource_map__.php index 412e0e63b2..cc32e864ec 100644 --- a/src/__celerity_resource_map__.php +++ b/src/__celerity_resource_map__.php @@ -1462,7 +1462,7 @@ celerity_register_resource_map(array( ), 'javelin-behavior-phabricator-search-typeahead' => array( - 'uri' => '/res/9ceffb09/rsrc/js/application/core/behavior-search-typeahead.js', + 'uri' => '/res/f552b264/rsrc/js/application/core/behavior-search-typeahead.js', 'type' => 'js', 'requires' => array( @@ -1471,7 +1471,8 @@ celerity_register_resource_map(array( 2 => 'javelin-typeahead', 3 => 'javelin-dom', 4 => 'javelin-uri', - 5 => 'javelin-stratcom', + 5 => 'javelin-util', + 6 => 'javelin-stratcom', ), 'disk' => '/rsrc/js/application/core/behavior-search-typeahead.js', ), diff --git a/webroot/rsrc/js/application/core/behavior-search-typeahead.js b/webroot/rsrc/js/application/core/behavior-search-typeahead.js index 1772878cdc..8ed7b2175c 100644 --- a/webroot/rsrc/js/application/core/behavior-search-typeahead.js +++ b/webroot/rsrc/js/application/core/behavior-search-typeahead.js @@ -5,6 +5,7 @@ * javelin-typeahead * javelin-dom * javelin-uri + * javelin-util * javelin-stratcom */ @@ -30,15 +31,64 @@ JX.behavior('phabricator-search-typeahead', function(config) { ]); return { - name : object[0], - display : render, - uri : object[1], - id : object[2] + name: object[0], + display: render, + uri: object[1], + id: object[2], + priority: object[3], + type: object[7] }; } datasource.setTransformer(transform); + // Sort handler that orders results by type (e.g., applications, users) + // and then selects for good matches on the "priority" substrings if they + // exist (for instance, username matches are preferred over real name + // matches, and application name matches are preferred over application + // flavor text matches). + + var sort_handler = function(value, list, cmp) { + var priority_hits = {}; + var type_priority = { + // TODO: Put jump nav hits like "D123" first. + 'apps' : 2, + 'user' : 3 + }; + + var tokens = this.tokenize(value); + + for (var ii = 0; ii < list.length; ii++) { + var item = list[ii]; + if (!item.priority) { + continue; + } + + for (var jj = 0; jj < tokens.length; jj++) { + if (item.priority.substr(0, tokens[jj].length) == tokens[jj]) { + priority_hits[item.id] = true; + } + } + } + + list.sort(function(u, v) { + var u_type = type_priority[u.type] || 999; + var v_type = type_priority[v.type] || 999; + + if (u_type != v_type) { + return u_type - v_type; + } + + if (priority_hits[u.id] != priority_hits[v.id]) { + return priority_hits[v.id] ? 1 : -1; + } + + return cmp(u, v); + }); + }; + + datasource.setSortHandler(JX.bind(datasource, sort_handler)); + var typeahead = new JX.Typeahead(JX.$(config.id), JX.$(config.input)); typeahead.setDatasource(datasource); typeahead.setPlaceholder(config.placeholder);