Make apps handle the order logic

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
This commit is contained in:
John Molakvoæ (skjnldsv) 2020-08-04 18:15:09 +02:00
parent 71b62c4203
commit d98f7c1bd8
No known key found for this signature in database
GPG Key ID: 60C25B8C072916CF
11 changed files with 33 additions and 49 deletions

View File

@ -77,7 +77,7 @@ class CommentsSearchProvider implements IProvider {
/**
* @inheritDoc
*/
public function getOrder(): int {
public function getOrder(string $from): int {
return 10;
}

View File

@ -96,7 +96,7 @@ class ContactsSearchProvider implements IProvider {
/**
* @inheritDoc
*/
public function getOrder(): int {
public function getOrder(string $from): int {
return 7;
}

View File

@ -82,7 +82,7 @@ class EventsSearchProvider extends ACalendarSearchProvider {
/**
* @inheritDoc
*/
public function getOrder(): int {
public function getOrder(string $from): int {
return 10;
}

View File

@ -74,7 +74,7 @@ class TasksSearchProvider extends ACalendarSearchProvider {
/**
* @inheritDoc
*/
public function getOrder(): int {
public function getOrder(string $from): int {
return 10;
}

View File

@ -77,7 +77,7 @@ class FilesSearchProvider implements IProvider {
/**
* @inheritDoc
*/
public function getOrder(): int {
public function getOrder(string $from): int {
return 5;
}

View File

@ -75,7 +75,11 @@ class SectionSearch implements IProvider {
/**
* @inheritDoc
*/
public function getOrder(): int {
public function getOrder(string $from): int {
if (strpos($from, $this->urlGenerator->linkToRoute('settings.PersonalSettings.index') === 0)
|| strpos($from, $this->urlGenerator->linkToRoute('settings.AdminSettings.index')) === 0) {
return -1;
}
return 20;
}

View File

@ -54,10 +54,14 @@ class UnifiedSearchController extends Controller {
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $from the url the user is currently at
*
* @return JSONResponse
*/
public function getProviders(): JSONResponse {
public function getProviders(string $from): JSONResponse {
return new JSONResponse(
$this->composer->getProviders()
$this->composer->getProviders($from)
);
}

View File

@ -33,9 +33,15 @@ export const activeApp = loadState('core', 'active-app')
*/
export async function getTypes() {
try {
const { data } = await axios.get(generateUrl('/search/providers'))
const { data } = await axios.get(generateUrl('/search/providers'), {
params: {
// Sending which location we're currently at
from: window.location.pathname.replace('/index.php', '') + window.location.search,
},
})
if (Array.isArray(data) && data.length > 0) {
return sortProviders(data)
// Providers are sorted by the api based on their order key
return data
}
} catch (error) {
console.error(error)
@ -43,29 +49,6 @@ export async function getTypes() {
return []
}
/**
* Sort the providers by the current active app
*
* @param {Array} providers the providers list
* @returns {Array}
*/
export function sortProviders(providers) {
providers.sort((a, b) => {
if (a.id.startsWith(activeApp) && b.id.startsWith(activeApp)) {
return a.order - b.order
}
if (a.id.startsWith(activeApp)) {
return -1
}
if (b.id.startsWith(activeApp)) {
return 1
}
return 0
})
return providers
}
/**
* Get the list of available search providers
*

View File

@ -169,18 +169,7 @@ export default {
orderedResults() {
const ordered = {}
Object.keys(this.results)
.sort((a, b) => {
if (a.startsWith(activeApp) && b.startsWith(activeApp)) {
return this.typesMap[a].order - this.typesMap[b].order
}
if (a.startsWith(activeApp)) {
return -1
}
if (b.startsWith(activeApp)) {
return 1
}
return 0
})
.sort((a, b) => this.typesMap[a].order - this.typesMap[b].order)
.forEach(type => {
ordered[type] = this.results[type]
})

View File

@ -109,17 +109,19 @@ class SearchComposer {
* Get a list of all provider IDs & Names for the consecutive calls to `search`
* Sort the list by the order property
*
* @param string $from the url the user is currently at
*
* @return array
*/
public function getProviders(): array {
public function getProviders(string $from): array {
$this->loadLazyProviders();
$providers = array_values(
array_map(function (IProvider $provider) {
array_map(function (IProvider $provider) use ($from) {
return [
'id' => $provider->getId(),
'name' => $provider->getName(),
'order' => $provider->getOrder()
'order' => $provider->getOrder($from)
];
}, $this->providers)
);

View File

@ -68,11 +68,13 @@ interface IProvider {
* Get the search provider order
* The lower the int, the higher it will be sorted (0 will be before 10)
*
* @param string $from the url the user is currently at. (e.g. /apps/files/?dir=/&fileid=982)
*
* @return int
*
* @since 20.0.0
*/
public function getOrder(): int;
public function getOrder(string $from): int;
/**
* Find matching search entries in an app