Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/files sorting #650

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* @copyright Copyright (c) 2020 Jakob Röhrl <jakob.roehrl@web.de>
*
* @author Jakob Röhrl <jakob.roehrl@web.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

return [
'routes' => [
// api
['name' => 'Api#getSortInfo', 'url' => '/api/v1/getSortInfo', 'verb' => 'GET'],
]
];
79,569 changes: 79,468 additions & 101 deletions js/viewer-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/viewer-main.js.map

Large diffs are not rendered by default.

79 changes: 79 additions & 0 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Jakob Röhrl <jakob.roehrl@web.de>
*
* @author Jakob Röhrl <jakob.roehrl@web.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Viewer\Controller;

use OCA\Viewer\AppInfo\Application;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUserSession;

class ApiController extends Controller {

/** @var IConfig */
private $config;

/** @var IUserSession */
private $userSession;

public function __construct(IRequest $request,
IConfig $config,
IUserSession $userSession) {
parent::__construct(Application::APP_ID, $request);

$this->config = $config;
$this->userSession = $userSession;
}

/**
* @NoAdminRequired
*
* update preferences (user setting)
*
* @param string key the identifier to change
* @param string value the value to set
*
* @return JSONResponse an empty JSONResponse with respective http status code
*/
public function getSortInfo(): JSONResponse {

$fileSorting = 'name';
$fileSortingDirection = 'asc';

$user = $this->userSession->getUser();
if ($user !== null) {
$fileSorting = $this->config->getUserValue($user->getUID(), 'files', 'file_sorting', 'name');
$fileSortingDirection = $this->config->getUserValue($user->getUID(), 'files', 'file_sorting_direction', 'asc');
}

return new JSONResponse([
'fileSorting' => $fileSorting,
'fileSortingDirection' => $fileSortingDirection,
], Http::STATUS_OK);
}
}
33 changes: 33 additions & 0 deletions lib/Listener/LoadViewerScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,48 @@
use OCA\Viewer\Event\LoadViewer;
use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCP\EventDispatcher\Event;
use OCP\AppFramework\Services\IInitialState;
use OCP\IUserSession;
use OCP\EventDispatcher\IEventListener;
use OCP\IConfig;
use OCP\Util;

class LoadViewerScript implements IEventListener {

/** @var IInitialState */
private $initialStateService;

/** @var IUserSession */
private $userSession;

/** @var IConfig */
private $config;

public function __construct(IInitialState $initialStateService,
IUserSession $userSession,
IConfig $config) {
$this->initialStateService = $initialStateService;
$this->userSession = $userSession;
$this->config = $config;
}

public function handle(Event $event): void {
if (!($event instanceof LoadViewer || $event instanceof LoadAdditionalScriptsEvent)) {
return;
}

$fileSorting = 'name';
$fileSortingDirection = 'asc';

$user = $this->userSession->getUser();
if ($user !== null) {
$fileSorting = $this->config->getUserValue($user->getUID(), 'files', 'file_sorting', 'name');
$fileSortingDirection = $this->config->getUserValue($user->getUID(), 'files', 'file_sorting_direction', 'asc');
}

$this->initialStateService->provideInitialState('file-sorting', $fileSorting);
$this->initialStateService->provideInitialState('file-sorting-direction', $fileSortingDirection);

Util::addScript(Application::APP_ID, 'viewer-main');
}
}
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@nextcloud/auth": "^1.3.0",
"@nextcloud/axios": "^1.4.0",
"@nextcloud/dialogs": "^2.0.1",
"@nextcloud/initial-state": "^1.2.0",
"@nextcloud/paths": "^1.1.2",
"@nextcloud/router": "^1.2.0",
"@nextcloud/vue": "^2.7.0",
Expand All @@ -49,6 +50,7 @@
"nextcloud-server": "^0.15.10",
"path-parse": "^1.0.6",
"regenerator-runtime": "^0.13.7",
"string-natural-compare": "^3.0.1",
"vue": "^2.6.12",
"vue-async-computed": "^3.9.0",
"vue-plyr": "^6.0.4",
Expand Down
35 changes: 35 additions & 0 deletions src/services/FilesSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @copyright Copyright (c) 2019 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'

export default async function(obj) {
let sortInfo = ''
try {
const url = generateUrl('/apps/viewer/api/v1/getSortInfo')
sortInfo = await axios.get(url)
} catch (error) {
console.error(error)
}
return sortInfo
}
19 changes: 18 additions & 1 deletion src/utils/fileUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
import camelcase from 'camelcase'
import { isNumber } from './numberUtil'
import sortInfo from '../services/FilesSort'
import naturalCompare from 'string-natural-compare'

/**
* Get an url encoded path
Expand Down Expand Up @@ -61,7 +63,20 @@ const extractFilePaths = function(path) {
* @param {boolean} [asc=true] sort ascending?
* @returns {number}
*/
const sortCompare = function(fileInfo1, fileInfo2, key, asc = true) {
const sortCompare = async function(fileInfo1, fileInfo2, key, asc = true) {

/*
fileSorting: asc, desc
fileSortingDirection: name, mtime, size

*/

try {
const sortdata = await sortInfo()
console.log(sortdata.data)
} catch (err) {
console.error(err)
}

if (fileInfo1.isFavorite && !fileInfo2.isFavorite) {
return -1
Expand All @@ -81,6 +96,8 @@ const sortCompare = function(fileInfo1, fileInfo2, key, asc = true) {
return 1
}

return naturalCompare(fileInfo1[key].toString(), fileInfo2[key].toString(), { caseInsensitive: true })

// finally sort by name
return asc
? fileInfo1[key].localeCompare(fileInfo2[key], OC.getLanguage())
Expand Down
Empty file.