-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate Navigation from server
Signed-off-by: John Molakvoæ <[email protected]>
- Loading branch information
Showing
10 changed files
with
432 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* @copyright Copyright (c) 2022 John Molakvoæ <[email protected]> | ||
* | ||
* @author John Molakvoæ <[email protected]> | ||
* | ||
* @license AGPL-3.0-or-later | ||
* | ||
* 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 { View } from './view' | ||
import { Node } from '../files/node' | ||
|
||
interface ColumnData { | ||
/** Unique column ID */ | ||
id: string | ||
/** Translated column title */ | ||
title: string | ||
/** The content of the cell. The element will be appended within */ | ||
render: (node: Node, view: View) => HTMLElement | ||
/** Function used to sort Nodes between them */ | ||
sort?: (nodeA: Node, nodeB: Node) => number | ||
/** | ||
* Custom summary of the column to display at the end of the list. | ||
* Will not be displayed if nothing is provided | ||
*/ | ||
summary?: (node: Node[], view: View) => string | ||
} | ||
|
||
export class Column implements ColumnData { | ||
|
||
private _column: ColumnData | ||
|
||
constructor(column: ColumnData) { | ||
isValidColumn(column) | ||
this._column = column | ||
} | ||
|
||
get id() { | ||
return this._column.id | ||
} | ||
|
||
get title() { | ||
return this._column.title | ||
} | ||
|
||
get render() { | ||
return this._column.render | ||
} | ||
|
||
get sort() { | ||
return this._column.sort | ||
} | ||
|
||
get summary() { | ||
return this._column.summary | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Typescript cannot validate an interface. | ||
* Please keep in sync with the Column interface requirements. | ||
* | ||
* @param {ColumnData} column the column to check | ||
* @return {boolean} true if the column is valid | ||
*/ | ||
const isValidColumn = function(column: ColumnData): boolean { | ||
if (!column.id || typeof column.id !== 'string') { | ||
throw new Error('A column id is required') | ||
} | ||
|
||
if (!column.title || typeof column.title !== 'string') { | ||
throw new Error('A column title is required') | ||
} | ||
|
||
if (!column.render || typeof column.render !== 'function') { | ||
throw new Error('A render function is required') | ||
} | ||
|
||
// Optional properties | ||
if (column.sort && typeof column.sort !== 'function') { | ||
throw new Error('Column sortFunction must be a function') | ||
} | ||
|
||
if (column.summary && typeof column.summary !== 'function') { | ||
throw new Error('Column summary must be a function') | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* @copyright Copyright (c) 2022 John Molakvoæ <[email protected]> | ||
* | ||
* @author John Molakvoæ <[email protected]> | ||
* | ||
* @license AGPL-3.0-or-later | ||
* | ||
* 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 { View } from './view' | ||
import logger from '../utils/logger' | ||
|
||
export class Navigation { | ||
|
||
private _views: View[] = [] | ||
private _currentView: View | null = null | ||
|
||
register(view: View) { | ||
if (this._views.find(search => search.id === view.id)) { | ||
throw new Error(`View id ${view.id} is already registered`) | ||
} | ||
|
||
this._views.push(view) | ||
} | ||
|
||
remove(id: string) { | ||
const index = this._views.findIndex(view => view.id === id) | ||
if (index !== -1) { | ||
this._views.splice(index, 1) | ||
} | ||
} | ||
|
||
get views(): View[] { | ||
return this._views | ||
} | ||
|
||
setActive(view: View | null) { | ||
this._currentView = view | ||
} | ||
|
||
get active(): View | null { | ||
return this._currentView | ||
} | ||
|
||
} | ||
|
||
export const getNavigation = function(): Navigation { | ||
if (typeof window._nc_navigation === 'undefined') { | ||
window._nc_navigation = new Navigation() | ||
logger.debug('Navigation service initialized') | ||
} | ||
|
||
return window._nc_navigation | ||
} |
Oops, something went wrong.