From b1ae686ea4a3259ce490e63a03a33450fa1532ef Mon Sep 17 00:00:00 2001 From: Florent Bruneau Date: Sun, 27 Feb 2011 12:22:55 +0100 Subject: [PATCH] Make the quicksearch stuff more flexible so it can be used more widely as a user selector. Signed-off-by: Florent Bruneau --- htdocs/javascript/xorg.js | 70 ++++++++++++++++++++++++++++++----------------- include/userset.inc.php | 8 ++++-- modules/api.php | 2 +- 3 files changed, 52 insertions(+), 28 deletions(-) diff --git a/htdocs/javascript/xorg.js b/htdocs/javascript/xorg.js index a77981a..1c18fbe 100644 --- a/htdocs/javascript/xorg.js +++ b/htdocs/javascript/xorg.js @@ -880,19 +880,10 @@ function sendTestEmail(token, hruid) ''); - function buildPopup(input, linkBindFunction) + function buildPopup(input, destination, linkBindFunction) { var pos = findPos(input.get(0)); - var $popup = $('
').hide() - .addClass('contact-list') - .css({ - position: 'absolute', - width: '300px', - top: input.css('bottom'), - left: pos.x - 300 + input.width(), - clear: 'both', - 'text-align': 'left' - }); + var $popup = destination; var selected = null; function updateSelection() @@ -927,7 +918,19 @@ function sendTestEmail(token, hruid) return data; } - input.after($popup); + if (!$popup) { + $popup = $('
').hide() + .addClass('contact-list') + .css({ + position: 'absolute', + width: '300px', + top: input.css('bottom'), + left: pos.x - 300 + input.width(), + clear: 'both', + 'text-align': 'left' + }); + input.after($popup); + } return { hide: function(ignoreIfHover) { @@ -1009,24 +1012,41 @@ function sendTestEmail(token, hruid) $.fn.extend({ quickSearch: function(options) { - var previous = null; var $this = this; var $input = $this.get(0); var $popup; + var previous = null; var pending = false; var disabled = false; var updatePopup; + var loadingClass; options = options || { }; - options.queryParams = options.queryParams || { }; - options.loadingClass = options.loadingClass || - ($this.css('text-align') === 'right' ? 'ac_loading_left' : 'ac_loading'); - - $popup = buildPopup(this, options.selectAction || function() { - $(this).popWin(840, 600) - .click(function() { - $popup.hide(); - }); + options = $.extend({ + destination: null, + minChars: 3, + shortChars: 5, + shortTimeout: 300, + longTimeout: 100, + queryParams: { + offset: 0, + count: 10, + }, + loadingClassLeft: 'ac_loading', + loadingClassRight: 'ac_loading_left', + selectAction: function() { + $(this).popWin(840, 600); + } + }, options); + console.log(options); + options.loadingClass = $this.css('text-align') === 'right' ? options.loadingClassRight + : options.loadingClassLeft; + + $popup = buildPopup(this, options.destination, function() { + options.selectAction.apply(this, arguments); + $(this).click(function() { + $popup.hide(); + }); }); function markPending() { @@ -1041,7 +1061,7 @@ function sendTestEmail(token, hruid) updatePopup = markPending; $this.addClass(options.loadingClass); $.xapi('search', $.extend({ 'quick': quick }, options.queryParams), function(data) { - if (data.profile_count > 10 || data.profile_count < 0) { + if (data.profile_count > options.queryParams.count || data.profile_count < 0) { return $popup.hide(); } $popup.updateContent(data.profiles); @@ -1067,11 +1087,11 @@ function sendTestEmail(token, hruid) quick = quick.trim(); } pending = false; - if (disabled || quick.length < 3) { + if (disabled || quick.length < options.minChars) { previous = quick; return $popup.hide(); } else if (!dontDelay) { - var timeout = quick.length < 5 ? 300 : 100; + var timeout = quick.length < options.shortChars ? options.shortTimeout : options.longTimeout; setTimeout(function() { updatePopup(true); }, timeout); diff --git a/include/userset.inc.php b/include/userset.inc.php index 1274a86..2c58419 100644 --- a/include/userset.inc.php +++ b/include/userset.inc.php @@ -358,16 +358,20 @@ class AddressesView implements PlView class JSonView implements PlView { private $set; + private $params; public function __construct(PlSet $set, array $params) { - $this->set = $set; + $this->set = $set; + $this->params = $params; } public function apply(PlPage $page) { $export = array(); - $profiles = $this->set->get(new PlLimit(10)); + $start = isset($this->params['offset']) ? $this->params['offset'] : 0; + $count = isset($this->params['count']) ? $this->params['count'] : 10; + $profiles = $this->set->get(new PlLimit($start, $count)); foreach ($profiles as $profile) { $export[] = $profile->export(); } diff --git a/modules/api.php b/modules/api.php index ecdd645..0df1ae8 100644 --- a/modules/api.php +++ b/modules/api.php @@ -70,7 +70,7 @@ class ApiModule extends PlModule require_once 'userset.inc.php'; $view = new QuickSearchSet(); - $view->addMod('json', 'JSon', true, array('with_score' => true, 'starts_with' => $byletter)); + $view->addMod('json', 'JSon', true, $payload); $view->apply('api/1/search', $page, 'json'); return PL_JSON; } -- 2.1.4