From 342308c7d716fa93e09dece3f65ef15875fbec73 Mon Sep 17 00:00:00 2001 From: Nao Date: Mon, 20 Feb 2017 18:53:18 +0100 Subject: [PATCH] + Auto-suggest is now better built to support other suggestion types than just members. Seriously, it's weird that this system was built exclusively for member search and then given a generic name..?! (suggest.js) --- core/javascript/suggest.js | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/core/javascript/suggest.js b/core/javascript/suggest.js index 10a8e247..35fe965f 100644 --- a/core/javascript/suggest.js +++ b/core/javascript/suggest.js @@ -14,13 +14,13 @@ function weAutoSuggest(oOptions) this.opt = oOptions; // Nothing else for now. - this.opt.sSearchType = 'member'; + this.sSearchType = this.opt.sSearchType || 'member'; // Store the handle to the text box. var oText = $('#' + this.opt.sControlId), that = this; - this.opt.sItemTemplate = this.opt.sItemTemplate || '%item_name% %delete_text%   '; - + this.sItemTemplate = this.opt.sItemTemplate || '%item_name% %delete_text%   '; + this.sURLMask = this.opt.sURLMask || 'action=profile;u=%item_id%'; this.oTextHandle = oText; this.oSuggestDivHandle = null; this.oXmlRequestHandle = null; @@ -35,9 +35,6 @@ function weAutoSuggest(oOptions) this.sLastDirtySearch = ''; this.sLastSearch = ''; - // Should selected items be added to a list? - this.bItemList = !!this.opt.bItemList; - // Create a div that'll contain the results later on. this.oSuggestDivHandle = $('
').addClass('auto_suggest').hide().appendTo('body')[0]; @@ -50,7 +47,8 @@ function weAutoSuggest(oOptions) .on('keyup change focus', function () { return that.autoSuggestUpdate(); }) .blur(function () { return that.autoSuggestHide(); }); - if (this.bItemList) + // Do we allow multiple items to be selected, and added to a list? (e.g. member search) + if (this.bItemList = !!this.opt.bItemList) this.oItemList = $('
').insertBefore(oText); // Are there any items that should be added in advance? @@ -156,16 +154,12 @@ weAutoSuggest.prototype.handleSubmit = function() break; } // Not an exact match, but it'll do for now. + else if (entryId != null) + bReturnValue = false; else { - // If we have two matches don't find anything. - if (entryId != null) - bReturnValue = false; - else - { - entryId = entry.sItemId; - entryName = entry.sItemName; - } + entryId = entry.sItemId; + entryName = entry.sItemName; } } } @@ -206,7 +200,11 @@ weAutoSuggest.prototype.itemClicked = function (oCurElement) // Otherwise clear things down. else - this.oTextHandle.val(oCurElement.innerHTML.php_unhtmlspecialchars()); + { + this.oTextHandle.val(oCurElement.innerText.php_unhtmlspecialchars()); + if ($(oCurElement).find('a')) + location.href = $(oCurElement).find('a').attr('href'); + } this.oRealTextHandle.val(this.oTextHandle.val()); this.autoSuggestActualHide(); @@ -259,10 +257,10 @@ weAutoSuggest.prototype.addItemLink = function (sItemId, sItemName, bFromSubmit) if (!$('#' + eid).length) { $('').html( - this.opt.sItemTemplate.wereplace({ + this.sItemTemplate.wereplace({ post_name: this.opt.sPostName, item_id: sItemId, - item_href: weUrl((this.opt.sURLMask || 'action=profile;u=%item_id%').wereplace({ item_id: sItemId })), + item_href: weUrl(this.sURLMask.wereplace({ item_id: sItemId })), item_name: sItemName, assets_url: we_assets, delete_text: this.opt.sTextDeleteItem || $txt['autosuggest_delete_item'] @@ -329,7 +327,7 @@ weAutoSuggest.prototype.populateDiv = function (aResults) // Create the sub element, and attach some events to it so we can do stuff. aNewDisplayData[i] = $('
') .data({ sItemId: aResults[i].sItemId, that: this }) - .html(aResults[i].sItemName) + .html(this.sSearchType == 'member' ? aResults[i].sItemName : '' + aResults[i].sItemName + '') .mouseenter(function (oEvent) { $(this).data('that').itemMouseEnter(this); }) .mouseleave(function (oEvent) { $(this).data('that').itemMouseLeave(this); }) .click(function (oEvent) { $(this).data('that').itemClicked(this); })[0]; @@ -454,7 +452,7 @@ weAutoSuggest.prototype.autoSuggestUpdate = function () this.oXmlRequestHandle.abort(); var data = { - suggest_type: this.opt.sSearchType, + suggest_type: this.sSearchType, search: sSearchString, time: $.now() };