<%= javascript_include_tag 'widgets/histogram' %>
<%= javascript_include_tag 'select-list' %>
- <%= javascript_include_tag 'jquery.autocomplete' %>
+ <%= javascript_include_tag 'top-search' %>
<%= javascript_include_tag 'sortable' %>
<%= javascript_include_tag 'navigator/handlebars-extensions' %>
jQuery(function() {
- jQuery('#searchInput').autocomplete({
+ jQuery('#searchInput').topSearch({
minLength: 2,
-
- searchUrl: baseUrl + '/search',
- searchTerm: 's',
-
results: '#searchResourcesResults',
spinner: '#searchingResources'
});
+++ /dev/null
-(function($) {
-
- var autocomplete = function(options) {
-
- var el = $(this),
- resultsEl = $(options.results),
- spinnerEl = $(options.spinner);
-
- var index, total, selected, items, symbol = false;
-
-
- var select = function() {
- if (selected) {
- selected.removeClass('selected');
- }
-
- selected = items.eq(index);
- selected.addClass('selected');
- },
-
- selectPrev = function() {
- if (index > 0) {
- index--;
- }
- select();
- },
-
- selectNext = function() {
- if (index < total - 1) {
- index++;
- }
- select();
- },
-
- choose = function() {
- if (selected) {
- var id = selected.prop('id');
- window.location = baseUrl + '/dashboard/index/' + id;
- }
- },
-
- show = function(content) {
- resultsEl.html(content).show();
- items = resultsEl.find('li');
- index = -1;
- total = items.length;
- selectNext();
-
- items
- .on('mouseover', function() {
- index = items.index($(this));
- select();
- })
- .on('click', function() {
- index = items.index($(this));
- select();
- choose();
- });
- },
-
- hide = function() {
- resultsEl.fadeOut();
- };
-
-
- el
- .on('keydown', function(e) {
- function prevent(e) {
- e.preventDefault();
- symbol = false;
- }
-
-
- switch (e.keyCode) {
- case 13: // return
- prevent(e);
- choose();
- return;
- case 38: // up
- prevent(e);
- selectPrev();
- return;
- case 40: // down
- prevent(e);
- selectNext();
- return;
- case 37: // left
- case 39: // right
- symbol = false;
- return;
- default:
- symbol = true;
- }
- })
- .on('keyup', function() {
- if (symbol) {
- if (el.val().length >= options.minLength) {
- var data = {};
- data[options.searchTerm] = el.val();
-
- spinnerEl.show();
- $.ajax({
- url: options.searchUrl,
- data: data
- })
- .done(function(r) {
- show(r);
- })
- .fail(hide)
- .always(function() {
- spinnerEl.hide();
- });
- } else {
- hide();
- }
- }
- })
- .on('focus', function() {
- el.data('placeholder', el.val());
- el.val('');
- })
- .on('focusout', function() {
- if (el.val().length === 0) {
- el.val(el.data('placeholder') || '');
- }
- hide();
- });
-
- $('body').on('mousedown', function() {
- hide();
- });
- };
-
- $.extend($.fn, {
- autocomplete: autocomplete
- });
-
-})(jQuery);
--- /dev/null
+(function($) {
+
+ $.fn.topSearch = function(options) {
+
+ var el = $(this),
+ resultsEl = $(options.results),
+ spinnerEl = $(options.spinner);
+
+ var index, total, selected, items, term, symbol = false;
+
+
+ var select = function() {
+ if (selected) {
+ selected.removeClass('selected');
+ }
+
+ selected = items.eq(index);
+ selected.addClass('selected');
+ },
+
+ selectPrev = function() {
+ if (index > 0) {
+ index--;
+ }
+ select();
+ },
+
+ selectNext = function() {
+ if (index < total - 1) {
+ index++;
+ }
+ select();
+ },
+
+ choose = function() {
+ if (selected) {
+ var key = selected.data('key');
+ window.location = baseUrl + '/dashboard/index/' + key;
+ }
+ },
+
+ show = function(r) {
+ resultsEl.empty();
+
+ var ul = $('<ul></ul>').appendTo(resultsEl);
+
+ r.results.forEach(function(qualifier) {
+ qualifier.items.forEach(function(item, index) {
+ var el = $('<li></li>')
+ .data('key', item.id),
+
+ q = $('<div></div>')
+ .addClass('q')
+ .appendTo(el),
+
+ highlightRegexp = new RegExp(term, 'gi'),
+ highlightedName = item.name.replace(highlightRegexp, '<strong>$&</strong>'),
+
+ label = $('<span></span>')
+ .html(' ' + highlightedName)
+ .appendTo(el);
+
+ $('<img>')
+ .prop('src', baseUrl + qualifier.icon)
+ .prop('width', 16)
+ .prop('height', 16)
+ .prependTo(label);
+
+ if (index === 0) {
+ q.text(qualifier.name);
+ }
+
+ el.appendTo(ul);
+ });
+ });
+
+ resultsEl.show();
+ items = resultsEl.find('li');
+ index = -1;
+ total = items.length;
+ selectNext();
+
+ items
+ .on('mouseover', function() {
+ index = items.index($(this));
+ select();
+ })
+ .on('click', function() {
+ index = items.index($(this));
+ select();
+ choose();
+ });
+ },
+
+ hide = function() {
+ resultsEl.fadeOut();
+ },
+
+ onKeyup = function() {
+ if (symbol) {
+ if (el.val().length >= options.minLength) {
+ term = el.val();
+
+ spinnerEl.show();
+ $.ajax({
+ url: baseUrl + '/api/components/suggestions',
+ data: { s: term }
+ })
+ .done(function(r) {
+ show(r);
+ })
+ .fail(hide)
+ .always(function() {
+ spinnerEl.hide();
+ });
+ } else {
+ hide();
+ }
+ }
+ },
+
+ debouncedKeyup = _.debounce(onKeyup, 250);
+
+
+ el
+ .on('keydown', function(e) {
+ function prevent(e) {
+ e.preventDefault();
+ symbol = false;
+ }
+
+ switch (e.keyCode) {
+ case 13: // return
+ prevent(e);
+ choose();
+ return;
+ case 38: // up
+ prevent(e);
+ selectPrev();
+ return;
+ case 40: // down
+ prevent(e);
+ selectNext();
+ return;
+ case 37: // left
+ case 39: // right
+ symbol = false;
+ return;
+ default:
+ symbol = true;
+ }
+ })
+ .on('keyup', debouncedKeyup)
+ .on('focus', function() {
+ el.data('placeholder', el.val());
+ el.val('');
+ })
+ .on('focusout', function() {
+ if (el.val().length === 0) {
+ el.val(el.data('placeholder') || '');
+ }
+ hide();
+ });
+
+ $('body').on('mousedown', function() {
+ hide();
+ });
+ };
+
+})(jQuery);
<js>/javascripts/widgets/histogram.js</js>
<js>/javascripts/select-list.js</js>
- <js>/javascripts/jquery.autocomplete.js</js>
+ <js>/javascripts/top-search.js</js>
<js>/javascripts/sortable.js</js>
<js>/javascripts/navigator/handlebars-extensions.js</js>