ACP search and title improvements

- Search uses translated titles if available
- Use `advanced` for `development` route titles
- Remove route title from showing up in results
- Highlight matching part of result title
- Don't show empty result contents when only title is matched
v1.18.x
Peter Jaszkowiak 8 years ago committed by Julian Lam
parent 3008794797
commit 862908d0eb

@ -58,6 +58,7 @@
"advanced/errors": "Errors", "advanced/errors": "Errors",
"advanced/cache": "Cache", "advanced/cache": "Cache",
"development/logger": "Logger", "development/logger": "Logger",
"development/info": "Info",
"reload-forum": "Reload Forum", "reload-forum": "Reload Forum",
"restart-forum": "Restart Forum", "restart-forum": "Restart Forum",

@ -116,13 +116,14 @@
}); });
}); });
var title; var title = url;
if (/admin\/general\/dashboard$/.test(url)) { if (/admin\/general\/dashboard$/.test(title)) {
title = '[[admin/menu:general/dashboard]]'; title = '[[admin/menu:general/dashboard]]';
} else { } else {
title = url.match(/admin\/(.+?)\/(.+?)$/); title = title.match(/admin\/(.+?)\/(.+?)$/);
title = '[[admin/menu:section-' + title[1] + ']]' + title = '[[admin/menu:section-' +
(title[2] ? (' > [[admin/menu:' + (title[1] === 'development' ? 'advanced' : title[1]) +
']]' + (title[2] ? (' > [[admin/menu:' +
title[1] + '/' + title[2] + ']]') : ''); title[1] + '/' + title[2] + ']]') : '');
} }

@ -4,39 +4,41 @@
define('admin/modules/search', ['mousetrap'], function (mousetrap) { define('admin/modules/search', ['mousetrap'], function (mousetrap) {
var search = {}; var search = {};
function nsToTitle(namespace) {
return namespace.replace('admin/', '').split('/').map(function (str) {
return str[0].toUpperCase() + str.slice(1);
}).join(' > ');
}
function find(dict, term) { function find(dict, term) {
var html = dict.filter(function (elem) { var html = dict.filter(function (elem) {
return elem.translations.toLowerCase().includes(term); return elem.translations.toLowerCase().includes(term);
}).map(function (params) { }).map(function (params) {
var namespace = params.namespace; var namespace = params.namespace;
var translations = params.translations; var translations = params.translations;
var title = params.title == null ? nsToTitle(namespace) : params.title; var title = params.title;
var results = translations var results = translations
// remove all lines without a match // remove all lines without a match
.replace(new RegExp('^(?:(?!' + term + ').)*$', 'gmi'), '') .replace(new RegExp('^(?:(?!' + term + ').)*$', 'gmi'), '')
// get up to 25 characaters of context on both sides of the match // remove lines that only match the title
.replace(new RegExp('(^|\\n).*?' + title + '.*?(\\n|$)', 'g'), '')
// get up to 25 characters of context on both sides of the match
// and wrap the match in a `.search-match` element // and wrap the match in a `.search-match` element
.replace( .replace(
new RegExp('^[\\s\\S]*?(.{0,25})(' + term + ')(.{0,25})[\\s\\S]*?$', 'gmi'), new RegExp('^[\\s\\S]*?(.{0,25})(' + term + ')(.{0,25})[\\s\\S]*?$', 'gmi'),
'...$1<span class="search-match">$2</span>$3...<br>' '...$1<span class="search-match">$2</span>$3...<br>'
) )
// collapse whitespace // collapse whitespace
.replace(/(?:\n ?)+/g, '\n'); .replace(/(?:\n ?)+/g, '\n')
.trim();
title = title.replace(
new RegExp('(^.*?)(' + term + ')(.*?$)', 'gi'),
'$1<span class="search-match">$2</span>$3'
);
return '<li role="presentation" class="result">' + return '<li role="presentation" class="result">' +
'<a role= "menuitem" href= "' + config.relative_path + '/' + namespace + '" >' + '<a role= "menuitem" href= "' + config.relative_path + '/' + namespace + '" >' +
title + title +
'<br>' + '<br>' + (!results ? '' :
'<small><code>' + ('<small><code>' +
results + results +
'</small></code>' + '</small></code>')) +
'</a>' + '</a>' +
'</li>'; '</li>';
}).join(''); }).join('');

@ -54,7 +54,7 @@ function simplify(translations) {
function nsToTitle(namespace) { function nsToTitle(namespace) {
return namespace.replace('admin/', '').split('/').map(function (str) { return namespace.replace('admin/', '').split('/').map(function (str) {
return str[0].toUpperCase() + str.slice(1); return str[0].toUpperCase() + str.slice(1);
}).join(' > '); }).join(' > ').replace(/[^a-zA-Z> ]/g, ' ');
} }
var fallbackCacheInProgress = {}; var fallbackCacheInProgress = {};
@ -67,15 +67,17 @@ function initFallback(namespace, callback) {
} }
var template = file.toString(); var template = file.toString();
var title = nsToTitle(namespace);
var translations = sanitize(template); var translations = sanitize(template);
translations = Translator.removePatterns(translations); translations = Translator.removePatterns(translations);
translations = simplify(translations); translations = simplify(translations);
translations += '\n' + nsToTitle(namespace); translations += '\n' + title;
callback(null, { callback(null, {
namespace: namespace, namespace: namespace,
translations: translations, translations: translations,
title: title,
}); });
}); });
} }
@ -124,17 +126,32 @@ function initDict(language, callback) {
var str = Object.keys(translations).map(function (key) { var str = Object.keys(translations).map(function (key) {
return translations[key]; return translations[key];
}).join('\n'); }).join('\n');
str = sanitize(str);
var title = namespace;
if (/admin\/general\/dashboard$/.test(title)) {
title = '[[admin/menu:general/dashboard]]';
} else {
title = title.match(/admin\/(.+?)\/(.+?)$/);
title = '[[admin/menu:section-' +
(title[1] === 'development' ? 'advanced' : title[1]) +
']]' + (title[2] ? (' > [[admin/menu:' +
title[1] + '/' + title[2] + ']]') : '');
}
Translator.create(language).translate(title).then(function (title) {
next(null, { next(null, {
namespace: namespace, namespace: namespace,
translations: str, translations: str + '\n' + title,
title: title,
}); });
} }).catch(err);
},
], function (err, params) { ], function (err, params) {
if (err) { if (err) {
return fallback(namespace, function (err, params) { return fallback(namespace, function (err, params) {
if (err) { if (err) {
return cb({ return cb(null, {
namespace: namespace, namespace: namespace,
translations: '', translations: '',
}); });

Loading…
Cancel
Save