Skip to content

TagSpaces is an offline, open source, document manager with tagging support

fixing sort order #820

Ilian Sapundshiev81ff228e27eb
diff --git a/app/components/FolderContainer.js b/app/components/FolderContainer.js
index adb5cb677..a59575913 100644
--- a/app/components/FolderContainer.js
+++ b/app/components/FolderContainer.js
@@ -142,8 +142,7 @@ type Props = {
getNextFile: () => string,
getPrevFile: () => string,
openDirectory: () => void,
- sortByCriteria: () => void,
- openFile: (path: string) => void,
+ openFile: (path: string) => void,
deleteDirectory: (path: string) => void,
reflectCreateEntry: (path: string, isFile: boolean) => void,
loadDirectoryContent: (path: string) => void,
@@ -340,7 +339,6 @@ class FolderContainer extends React.Component<Props, State> {
deleteFile={this.props.deleteFile}
renameFile={this.props.renameFile}
openDirectory={this.props.openDirectory}
- sortByCriteria={this.props.sortByCriteria}
currentDirectoryPath={this.props.currentDirectoryPath}
setLastSelectedEntry={this.props.setLastSelectedEntry}
perspectiveCommand={this.state.perspectiveCommand}
@@ -363,7 +361,6 @@ class FolderContainer extends React.Component<Props, State> {
deleteFile={this.props.deleteFile}
renameFile={this.props.renameFile}
openDirectory={this.props.openDirectory}
- sortByCriteria={this.props.sortByCriteria}
currentDirectoryPath={this.props.currentDirectoryPath}
setLastSelectedEntry={this.props.setLastSelectedEntry}
perspectiveCommand={this.state.perspectiveCommand}
@@ -487,7 +484,6 @@ function mapActionCreatorsToProps(dispatch) {
getNextFile: AppActions.getNextFile,
getPrevFile: AppActions.getPrevFile,
openDirectory: AppActions.openDirectory,
- sortByCriteria: AppActions.sortByCriteria,
openFile: AppActions.openFile,
deleteDirectory: AppActions.deleteDirectory,
reflectCreateEntry: AppActions.reflectCreateEntry,
diff --git a/app/perspectives/grid-perspective/src/index.js b/app/perspectives/grid-perspective/src/index.js
index d51b5ae67..3f3d0d7bd 100644
--- a/app/perspectives/grid-perspective/src/index.js
+++ b/app/perspectives/grid-perspective/src/index.js
@@ -77,9 +77,14 @@ import FileSourceDnd from '../../../components/FileSourceDnd';
import AppConfig from '../../../config';
import DragItemTypes from '../../../components/DragItemTypes';
import IOActions from '../../../reducers/io-actions';
+import {
+ actions as AppActions,
+} from '../../../reducers/app';
const maxDescriptionPreviewLength = 90;
+const settings = JSON.parse(localStorage.getItem('tsPerspectiveGrid')); // loading settings
+
type Props = {
classes: Object,
currentDirectoryPath?: string,
@@ -111,12 +116,8 @@ type State = {
layoutType?: string,
sortingContextMenuAnchorEl?: Object | null,
sortingContextMenuOpened?: boolean | null,
+ sortBy?: string,
orderBy?: null | boolean,
- orderByDate?: null | boolean,
- orderByExt?: null | boolean,
- orderByName?: null | boolean,
- orderBySize?: null | boolean,
- orderByTags?: null | boolean,
fileOperationsEnabled?: boolean,
allFilesSelected?: boolean,
showDirectories?: boolean,
@@ -130,43 +131,33 @@ type State = {
};
class GridPerspective extends React.Component<Props, State> {
- state = {
- fileContextMenuOpened: false,
- fileContextMenuAnchorEl: null,
- dirContextMenuOpened: false,
- dirContextMenuAnchorEl: null,
- tagContextMenuOpened: false,
- tagContextMenuAnchorEl: null,
- sortingContextMenuOpened: false,
- sortingContextMenuAnchorEl: null,
- selectedEntryPath: '',
- orderBy: false,
- orderByName: true,
- orderBySize: null,
- orderByDate: null,
- orderByTags: null,
- orderByExt: null,
- layoutType: 'grid',
- fileOperationsEnabled: false,
- allFilesSelected: false,
- showDirectories: true,
- isDeleteMultipleFilesDialogOpened: false,
- isMoveCopyFilesDialogOpened: false,
- isAddRemoveTagsDialogOpened: false,
- isFileRenameDialogOpened: false,
- selectedTag: null,
- selectedEntries: []
- };
-
- componentDidMount() {
- const settings = this.loadSettings();
- if (settings) {
- this.setState({
- showDirectories: settings.showDirectories,
- layoutType: settings.layoutType ? settings.layoutType : 'grid'
- // orderBy: settings.orderBy ? settings.orderBy : '',
- });
- }
+ constructor(props) {
+ super(props);
+ console.log('-->' + JSON.stringify(settings));
+ this.state = {
+ fileContextMenuOpened: false,
+ fileContextMenuAnchorEl: null,
+ dirContextMenuOpened: false,
+ dirContextMenuAnchorEl: null,
+ tagContextMenuOpened: false,
+ tagContextMenuAnchorEl: null,
+ sortingContextMenuOpened: false,
+ sortingContextMenuAnchorEl: null,
+ selectedEntryPath: '',
+ sortBy: settings && settings.sortBy ? settings.sortBy : 'byName',
+ orderBy: settings && settings.orderBy ? settings.orderBy : false,
+ layoutType: settings && settings.layoutType ? settings.layoutType : 'grid',
+ fileOperationsEnabled: false,
+ allFilesSelected: false,
+ showDirectories: settings && settings.showDirectories ? settings.showDirectories : true,
+ isDeleteMultipleFilesDialogOpened: false,
+ isMoveCopyFilesDialogOpened: false,
+ isAddRemoveTagsDialogOpened: false,
+ isFileRenameDialogOpened: false,
+ selectedTag: null,
+ selectedEntries: []
+ };
+ // console.log('Sort by -->' + this.state.sortBy);
}
componentWillReceiveProps = (nextProps: Props) => {
@@ -229,6 +220,8 @@ class GridPerspective extends React.Component<Props, State> {
// Clear selection on directory change
this.clearSelection();
+ this.props.sortByCriteria(this.state.sortBy, this.state.orderBy);
+
const grid = document.querySelector(
'[data-tid="perspectiveGridFileTable"]'
);
@@ -255,18 +248,14 @@ class GridPerspective extends React.Component<Props, State> {
}
};
- loadSettings = () => {
- const extSettings = JSON.parse(localStorage.getItem('tsPerspectiveGrid'));
- return extSettings;
- };
-
saveSettings() {
- const settings = {
+ const settingsObj = {
showDirectories: this.state.showDirectories,
layoutType: this.state.layoutType,
- orderBy: this.state.orderBy
+ orderBy: this.state.orderBy,
+ sortBy: this.state.sortBy
};
- localStorage.setItem('tsPerspectiveGrid', JSON.stringify(settings));
+ localStorage.setItem('tsPerspectiveGrid', JSON.stringify(settingsObj));
}
scrollToBottom = () => {
@@ -282,9 +271,11 @@ class GridPerspective extends React.Component<Props, State> {
handleSortBy = sortBy => {
this.closeSortingMenu();
- this.handleSortMenuIconClick(sortBy, this.state.orderBy);
- this.setState({ orderBy: !this.state.orderBy });
- this.props.sortByCriteria(sortBy, this.state.orderBy);
+ this.props.sortByCriteria(sortBy, !this.state.orderBy);
+ this.setState({
+ orderBy: !this.state.orderBy,
+ sortBy
+ }, this.saveSettings);
};
handleSortingMenu = event => {
@@ -294,108 +285,6 @@ class GridPerspective extends React.Component<Props, State> {
});
};
- handleSortMenuIconClick = sort => {
- switch (sort) {
- case 'byName':
- if (this.state.orderByName === null) {
- this.setState({
- orderBySize: null,
- orderByTags: null,
- orderByExt: null,
- orderByDate: null,
- orderByName: true
- });
- } else {
- this.setState({
- orderBySize: null,
- orderByTags: null,
- orderByExt: null,
- orderByDate: null,
- orderByName: false
- });
- }
- break;
- case 'byFileSize':
- if (this.state.orderBySize === null) {
- this.setState({
- orderByName: null,
- orderByTags: null,
- orderByExt: null,
- orderByDate: null,
- orderBySize: true
- });
- } else {
- this.setState({
- orderByName: null,
- orderByTags: null,
- orderByExt: null,
- orderByDate: null,
- orderBySize: false
- });
- }
- break;
- case 'byTags':
- if (this.state.orderByTags === null) {
- this.setState({
- orderByName: null,
- orderBySize: null,
- orderByExt: null,
- orderByDate: null,
- orderByTags: true
- });
- } else {
- this.setState({
- orderByName: null,
- orderBySize: null,
- orderByExt: null,
- orderByDate: null,
- orderByTags: false
- });
- }
- break;
- case 'byExtension':
- if (this.state.orderByExt === null) {
- this.setState({
- orderByName: null,
- orderBySize: null,
- orderByTags: null,
- orderByDate: null,
- orderByExt: true
- });
- } else {
- this.setState({
- orderByName: null,
- orderBySize: null,
- orderByTags: null,
- orderByDate: null,
- orderByExt: false
- });
- }
- break;
- case 'byDateModified':
- if (this.state.orderByDate === null) {
- this.setState({
- orderByName: null,
- orderBySize: null,
- orderByTags: null,
- orderByExt: null,
- orderByDate: true
- });
- } else {
- this.setState({
- orderByName: null,
- orderBySize: null,
- orderByTags: null,
- orderByExt: null,
- orderByDate: false
- });
- }
- break;
- default:
- break;
- }
- };
-
getLayoutClass = () => {
switch (this.state.layoutType) {
case 'grid':
@@ -615,15 +504,6 @@ class GridPerspective extends React.Component<Props, State> {
}
};
- renderSortMenuIcon = order => {
- if (order === null) {
- return <div>{null}</div>;
- } else if (order) {
- return <ArrowUpIcon />;
- }
- return <ArrowDownIcon />;
- };
-
handleFileMoveDrop = (item, monitor) => {
if (monitor) {
const { path } = monitor.getItem();
@@ -869,7 +749,7 @@ class GridPerspective extends React.Component<Props, State> {
const classes = this.props.classes;
const { selectedEntries = [] } = this.state;
const selectedFilePaths = selectedEntries.filter(fsEntry => fsEntry.isFile).map(fsentry => fsentry.path);
-
+ // console.log('Render grid');
return (
<div style={{ height: '100%' }}>
<style>
@@ -1067,59 +947,48 @@ class GridPerspective extends React.Component<Props, State> {
>
<MenuItem
data-tid="gridPerspectiveSortByName"
- onClick={() => {
- this.handleSortBy('byName');
- }}
+ onClick={() => { this.handleSortBy('byName'); }}
>
- <ListItemIcon>
- {this.renderSortMenuIcon(this.state.orderByName)}
+ <ListItemIcon style={{ minWidth: 25 }}>
+ {(this.state.sortBy === 'byName') && (
+ this.state.orderBy ? <ArrowUpIcon /> : <ArrowDownIcon />
+ )}
</ListItemIcon>
<ListItemText inset primary={i18n.t('core:fileTitle')} />
</MenuItem>
<MenuItem
data-tid="gridPerspectiveSortBySize"
- onClick={() => {
- this.handleSortBy('byFileSize');
- }}
+ onClick={() => { this.handleSortBy('byFileSize'); }}
>
- <ListItemIcon>
- {this.renderSortMenuIcon(this.state.orderBySize)}
+ <ListItemIcon style={{ minWidth: 25 }}>
+ {(this.state.sortBy === 'byFileSize') && (
+ this.state.orderBy ? <ArrowUpIcon /> : <ArrowDownIcon />
+ )}
</ListItemIcon>
<ListItemText inset primary={i18n.t('core:fileSize')} />
</MenuItem>
<MenuItem
data-tid="gridPerspectiveSortByDate"
- onClick={() => {
- this.handleSortBy('byDateModified');
- }}
+ onClick={() => { this.handleSortBy('byDateModified'); }}
>
- <ListItemIcon>
- {this.renderSortMenuIcon(this.state.orderByDate)}
+ <ListItemIcon style={{ minWidth: 25 }}>
+ {(this.state.sortBy === 'byDateModified') && (
+ this.state.orderBy ? <ArrowUpIcon /> : <ArrowDownIcon />
+ )}
</ListItemIcon>
<ListItemText inset primary={i18n.t('core:fileLDTM')} />
</MenuItem>
<MenuItem
data-tid="gridPerspectiveSortByExt"
- onClick={() => {
- this.handleSortBy('byExtension');
- }}
+ onClick={() => { this.handleSortBy('byExtension'); }}
>
- <ListItemIcon>
- {this.renderSortMenuIcon(this.state.orderByExt)}
+ <ListItemIcon style={{ minWidth: 25 }}>
+ {(this.state.sortBy === 'byExtension') && (
+ this.state.orderBy ? <ArrowUpIcon /> : <ArrowDownIcon />
+ )}
</ListItemIcon>
<ListItemText inset primary={i18n.t('core:fileExtension')} />
</MenuItem>
- {/* <MenuItem
- data-tid="gridPerspectiveSortByTags"
- onClick={() => {
- this.handleSortBy('byTags');
- }}
- >
- <ListItemIcon>
- {this.renderSortMenuIcon(this.state.orderByTags)}
- </ListItemIcon>
- <ListItemText inset primary={i18n.t('core:fileTags')} />
- </MenuItem> */}
</Menu>
</div>
);
@@ -1128,7 +997,8 @@ class GridPerspective extends React.Component<Props, State> {
function mapActionCreatorsToProps(dispatch) {
return bindActionCreators({
- moveFiles: IOActions.moveFiles
+ moveFiles: IOActions.moveFiles,
+ sortByCriteria: AppActions.sortByCriteria
}, dispatch);
}
diff --git a/app/utils/misc.js b/app/utils/misc.js
index 871714371..12174cb0e 100644
--- a/app/utils/misc.js
+++ b/app/utils/misc.js
@@ -368,50 +368,38 @@ export function sortByTags(a, b) {
export function sortByCriteria(criteria, data, order) {
switch (criteria) {
- case 'byName':
- if (order) {
- return data.sort(sortByName);
- } else {
- return data.sort((a, b) => -1 * sortByName(a, b));
- }
- break;
- case 'byFileSize':
- if (order) {
- return data.sort(sortBySize);
- } else {
- return data.sort((a, b) => -1 * sortBySize(a, b));
- }
- break;
- case 'byDateModified':
- if (order) {
- return data.sort(sortByDateModified);
- } else {
- return data.sort((a, b) => -1 * sortByDateModified(a, b));
- }
- break;
- case 'byExtension':
- if (order) {
- return data.sort(sortByExtension);
- } else {
- return data.sort((a, b) => -1 * sortByExtension(a, b));
- }
- break;
- case 'byTags':
- if (order) {
- return data.sort(sortByTags);
- } else {
- return data.sort((a, b) => -1 * sortByTags(a, b));
- }
- break;
- case 'byDirectory':
- if (order) {
- return data.sort(sortByIsDirectory);
- } else {
- return data.sort((a, b) => -1 * sortByIsDirectory(a, b));
- }
- break;
- default:
+ case 'byName':
+ if (order) {
return data.sort(sortByName);
+ }
+ return data.sort((a, b) => -1 * sortByName(a, b));
+ case 'byFileSize':
+ if (order) {
+ return data.sort(sortBySize);
+ }
+ return data.sort((a, b) => -1 * sortBySize(a, b));
+ case 'byDateModified':
+ if (order) {
+ return data.sort(sortByDateModified);
+ }
+ return data.sort((a, b) => -1 * sortByDateModified(a, b));
+ case 'byExtension':
+ if (order) {
+ return data.sort(sortByExtension);
+ }
+ return data.sort((a, b) => -1 * sortByExtension(a, b));
+ case 'byTags':
+ if (order) {
+ return data.sort(sortByTags);
+ }
+ return data.sort((a, b) => -1 * sortByTags(a, b));
+ case 'byDirectory':
+ if (order) {
+ return data.sort(sortByIsDirectory);
+ }
+ return data.sort((a, b) => -1 * sortByIsDirectory(a, b));
+ default:
+ return data.sort(sortByName);
}
}