Merge pull request #4608 from rabuzarus/20180316_-_hashtag_autocomplete

hashtag autocomplete
This commit is contained in:
Hypolite Petovan 2018-03-16 09:45:43 -04:00 committed by GitHub
commit 7057018ec7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 2 deletions

40
src/Module/Hashtag.php Normal file
View file

@ -0,0 +1,40 @@
<?php
/**
* @file src/Module/Hashtag.php
*/
namespace Friendica\Module;
use Friendica\BaseModule;
use Friendica\Core\System;
use dba;
require_once 'include/dba.php';
require_once 'include/text.php';
/**
* Hashtag module.
*/
class Hashtag extends BaseModule
{
public static function content()
{
$result = [];
$t = escape_tags($_REQUEST['t']);
if (empty($t)) {
System::jsonExit($result);
}
$taglist = dba::p("SELECT DISTINCT(`term`) FROM `term` WHERE `term` LIKE ? AND `type` = ? ORDER BY `term`",
$t . '%',
intval(TERM_HASHTAG)
);
while ($tag = dba::fetch($taglist)) {
$result[] = ['text' => strtolower($tag['term'])];
}
dba::close($taglist);
System::jsonExit($result);
}
}

View file

@ -77,6 +77,10 @@ function contact_format(item) {
return "<div>" + item.text + "</div>"; return "<div>" + item.text + "</div>";
} }
function tag_format(item) {
return "<div class='dropdown-item'>" + '#' + item.text + "</div>";
}
function editor_replace(item) { function editor_replace(item) {
if (typeof item.replace !== 'undefined') { if (typeof item.replace !== 'undefined') {
return '$1$2' + item.replace; return '$1$2' + item.replace;
@ -212,6 +216,15 @@ function string2bb(element) {
template: contact_format, template: contact_format,
}; };
// Autocomplete hashtags
tags = {
match: /(^|\s)(\#)([^ \n]{2,})$/,
index: 3,
search: function(term, callback) { $.getJSON(baseurl + '/hashtag/' + '?f=&t=' + term).done(function(data) { callback($.map(data, function(entry) { return entry.text.indexOf(term) === 0 ? entry : null; })); }); },
replace: function(item) { return "$1$2" + item.text + ' '; },
template: tag_format
};
// Autocomplete smilies e.g. ":like" // Autocomplete smilies e.g. ":like"
smilies = { smilies = {
match: /(^|\s)(:[a-z]{2,})$/, match: /(^|\s)(:[a-z]{2,})$/,
@ -222,7 +235,7 @@ function string2bb(element) {
}; };
this.attr('autocomplete','off'); this.attr('autocomplete','off');
this.textcomplete([contacts, forums, smilies], {className:'acpopup', zIndex:10000}); this.textcomplete([contacts, forums, smilies, tags], {className:'acpopup', zIndex:10000});
}; };
})( jQuery ); })( jQuery );
@ -248,8 +261,18 @@ function string2bb(element) {
replace: webbie_replace, replace: webbie_replace,
template: contact_format, template: contact_format,
}; };
// Autocomplete hashtags
tags = {
match: /(^|\s)(\#)([^ \n]{2,})$/,
index: 3,
search: function(term, callback) { $.getJSON(baseurl + '/hashtag/' + '?f=&t=' + term).done(function(data) { callback($.map(data, function(entry) { return entry.text.indexOf(term) === 0 ? entry : null; })); }); },
replace: function(item) { return "$1$2" + item.text; },
template: tag_format
};
this.attr('autocomplete', 'off'); this.attr('autocomplete', 'off');
var a = this.textcomplete([contacts, community], {className:'acpopup', maxCount:100, zIndex: 10000, appendTo:'nav'}); var a = this.textcomplete([contacts, community, tags], {className:'acpopup', maxCount:100, zIndex: 10000, appendTo:'nav'});
a.on('textComplete:select', function(e, value, strategy) { submit_form(this); }); a.on('textComplete:select', function(e, value, strategy) { submit_form(this); });
}; };
})( jQuery ); })( jQuery );