Skip to content

Commit

Permalink
+ Auto-suggest is now better built to support other suggestion types …
Browse files Browse the repository at this point in the history
…than just members. Seriously, it's weird that this system was built exclusively for member search and then given a generic name..?! (suggest.js)
  • Loading branch information
Nao committed Feb 20, 2017
1 parent 12bbb33 commit 342308c
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions core/javascript/suggest.js
Expand Up @@ -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 || '<input type="hidden" name="%post_name%[]" value="%item_id%"><a href="%item_href%" class="extern" onclick="window.open(this.href, \'_blank\'); return false;">%item_name%</a>&nbsp;<img src="%assets_url%/pm_recipient_delete.gif" alt="%delete_text%" title="%delete_text%"> &nbsp; ';

this.sItemTemplate = this.opt.sItemTemplate || '<input type="hidden" name="%post_name%[]" value="%item_id%"><a href="%item_href%" class="extern" onclick="window.open(this.href, \'_blank\'); return false;">%item_name%</a>&nbsp;<img src="%assets_url%/pm_recipient_delete.gif" alt="%delete_text%" title="%delete_text%"> &nbsp; ';
this.sURLMask = this.opt.sURLMask || 'action=profile;u=%item_id%';
this.oTextHandle = oText;
this.oSuggestDivHandle = null;
this.oXmlRequestHandle = null;
Expand All @@ -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 = $('<div></div>').addClass('auto_suggest').hide().appendTo('body')[0];

Expand All @@ -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 = $('<div></div>').insertBefore(oText);

// Are there any items that should be added in advance?
Expand Down Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -259,10 +257,10 @@ weAutoSuggest.prototype.addItemLink = function (sItemId, sItemName, bFromSubmit)
if (!$('#' + eid).length)
{
$('<span id="' + eid + '"></span>').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']
Expand Down Expand Up @@ -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] = $('<div></div>')
.data({ sItemId: aResults[i].sItemId, that: this })
.html(aResults[i].sItemName)
.html(this.sSearchType == 'member' ? aResults[i].sItemName : '<a href="' + this.sURLMask.wereplace({ item_id: aResults[i].sItemId }) + '">' + aResults[i].sItemName + '</a>')
.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];
Expand Down Expand Up @@ -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()
};
Expand Down

0 comments on commit 342308c

Please sign in to comment.