moved admin search into its own modules

v1.18.x
psychobunny 10 years ago
parent dcd47f7ec5
commit e9c48f50f9

@ -1,11 +1,9 @@
"use strict";
/*global define, app, ajaxify, socket, Mousetrap, Hammer, RELATIVE_PATH*/
/*global define, socket, app, Mousetrap, Hammer, RELATIVE_PATH*/
var admin = {};
(function() {
admin.searchIndex = null;
admin.enableColorPicker = function(inputEl, callback) {
(inputEl instanceof jQuery ? inputEl : $(inputEl)).each(function() {
var $this = $(this);
@ -30,7 +28,9 @@ var admin = {};
setupKeybindings();
if(!/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
getSearchIndex();
require(['admin/modules/search'], function(search) {
search.init();
});
} else {
activateMobile();
}
@ -142,113 +142,4 @@ var admin = {};
$('#breadcrumbs').html(caret + Array.prototype.slice.call(arguments).join(caret));
}
function getSearchIndex() {
$.getJSON(RELATIVE_PATH + '/templates/indexed.json', function (data) {
admin.searchIndex = data;
for (var file in admin.searchIndex) {
if (admin.searchIndex.hasOwnProperty(file)) {
admin.searchIndex[file] = admin.searchIndex[file].replace(/<img/g, '<none'); // can't think of a better solution, see #2153
admin.searchIndex[file] = $('<div class="search-container">' + admin.searchIndex[file] + '</div>');
admin.searchIndex[file].find('script').remove();
admin.searchIndex[file] = admin.searchIndex[file].text().toLowerCase().replace(/[ |\r|\n]+/g, ' ');
}
}
delete admin.searchIndex['/admin/header.tpl'];
delete admin.searchIndex['/admin/footer.tpl'];
setupACPSearch();
});
}
function setupACPSearch() {
var menu = $('#acp-search .dropdown-menu'),
routes = [],
input = $('#acp-search input'),
firstResult = null;
input.on('keyup', function() {
$('#acp-search .dropdown').addClass('open');
});
$('#acp-search').parents('form').on('submit', function(ev) {
var input = $(this).find('input'),
href = firstResult ? firstResult : RELATIVE_PATH + '/search/' + input.val();
ajaxify.go(href.replace(/^\//, ''));
setTimeout(function() {
$('#acp-search .dropdown').removeClass('open');
$(input).blur();
}, 150);
ev.preventDefault();
return false;
});
$('.sidebar-nav a').each(function(idx, link) {
routes.push($(link).attr('href'));
});
input.on('blur', function() {
$(this).val('').attr('placeholder', '/');
});
input.on('keyup focus', function() {
var $input = $(this),
value = $input.val().toLowerCase(),
menuItems = $('#acp-search .dropdown-menu').html('');
function toUpperCase(txt){
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
}
$input.attr('placeholder', '');
firstResult = null;
if (value.length >= 3) {
for (var file in admin.searchIndex) {
if (admin.searchIndex.hasOwnProperty(file)) {
var position = admin.searchIndex[file].indexOf(value);
if (position !== -1) {
var href = file.replace('.tpl', ''),
title = href.replace(/^\/admin\//, '').split('/'),
description = admin.searchIndex[file].substring(Math.max(0, position - 25), Math.min(admin.searchIndex[file].length - 1, position + 25))
.replace(value, '<span class="search-match">' + value + '</span>');
for (var t in title) {
if (title.hasOwnProperty(t)) {
title[t] = title[t]
.replace('-', ' ')
.replace(/\w\S*/g, toUpperCase);
}
}
title = title.join(' > ');
href = RELATIVE_PATH + href;
firstResult = firstResult ? firstResult : href;
if ($.inArray(href, routes) !== -1) {
menuItems.append('<li role="presentation"><a role="menuitem" href="' + href + '">' + title + '<br /><small><code>...' + description + '...</code></small></a></li>');
}
}
}
}
if (menuItems.html() !== '') {
menuItems.append('<li role="presentation" class="divider"></li>');
}
}
if (value.length > 0) {
menuItems.append('<li role="presentation"><a role="menuitem" href="' + RELATIVE_PATH + '/search/' + value + '">Search the forum for <strong>' + value + '</strong></a></li>');
} else {
menuItems.append('<li role="presentation"><a role="menuitem" href="#">Start typing to see results...</a></li>');
}
});
}
}());

@ -0,0 +1,118 @@
"use strict";
/*globals define, admin, ajaxify, RELATIVE_PATH*/
define(function() {
var search = {},
searchIndex;
search.init = function() {
$.getJSON(RELATIVE_PATH + '/templates/indexed.json', function (data) {
searchIndex = data;
for (var file in searchIndex) {
if (searchIndex.hasOwnProperty(file)) {
searchIndex[file] = searchIndex[file].replace(/<img/g, '<none'); // can't think of a better solution, see #2153
searchIndex[file] = $('<div class="search-container">' + searchIndex[file] + '</div>');
searchIndex[file].find('script').remove();
searchIndex[file] = searchIndex[file].text().toLowerCase().replace(/[ |\r|\n]+/g, ' ');
}
}
delete searchIndex['/admin/header.tpl'];
delete searchIndex['/admin/footer.tpl'];
setupACPSearch();
});
};;
function setupACPSearch() {
var menu = $('#acp-search .dropdown-menu'),
routes = [],
input = $('#acp-search input'),
firstResult = null;
input.on('keyup', function() {
$('#acp-search .dropdown').addClass('open');
});
$('#acp-search').parents('form').on('submit', function(ev) {
var input = $(this).find('input'),
href = firstResult ? firstResult : RELATIVE_PATH + '/search/' + input.val();
ajaxify.go(href.replace(/^\//, ''));
setTimeout(function() {
$('#acp-search .dropdown').removeClass('open');
$(input).blur();
}, 150);
ev.preventDefault();
return false;
});
$('.sidebar-nav a').each(function(idx, link) {
routes.push($(link).attr('href'));
});
input.on('blur', function() {
$(this).val('').attr('placeholder', '/');
});
input.on('keyup focus', function() {
var $input = $(this),
value = $input.val().toLowerCase(),
menuItems = $('#acp-search .dropdown-menu').html('');
function toUpperCase(txt){
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
}
$input.attr('placeholder', '');
firstResult = null;
if (value.length >= 3) {
for (var file in searchIndex) {
if (searchIndex.hasOwnProperty(file)) {
var position = searchIndex[file].indexOf(value);
if (position !== -1) {
var href = file.replace('.tpl', ''),
title = href.replace(/^\/admin\//, '').split('/'),
description = searchIndex[file].substring(Math.max(0, position - 25), Math.min(searchIndex[file].length - 1, position + 25))
.replace(value, '<span class="search-match">' + value + '</span>');
for (var t in title) {
if (title.hasOwnProperty(t)) {
title[t] = title[t]
.replace('-', ' ')
.replace(/\w\S*/g, toUpperCase);
}
}
title = title.join(' > ');
href = RELATIVE_PATH + href;
firstResult = firstResult ? firstResult : href;
if ($.inArray(href, routes) !== -1) {
menuItems.append('<li role="presentation"><a role="menuitem" href="' + href + '">' + title + '<br /><small><code>...' + description + '...</code></small></a></li>');
}
}
}
}
if (menuItems.html() !== '') {
menuItems.append('<li role="presentation" class="divider"></li>');
}
}
if (value.length > 0) {
menuItems.append('<li role="presentation"><a role="menuitem" href="' + RELATIVE_PATH + '/search/' + value + '">Search the forum for <strong>' + value + '</strong></a></li>');
} else {
menuItems.append('<li role="presentation"><a role="menuitem" href="#">Start typing to see results...</a></li>');
}
});
}
return search;
});
Loading…
Cancel
Save