Skip to content
Snippets Groups Projects
Commit 4b1e5980 authored by piotr.kupczyk@id.ethz.ch's avatar piotr.kupczyk@id.ethz.ch
Browse files

NG_UI : plugins : SSDM-10401 - refactored the routes definition

parent 4f64b581
No related branches found
No related tags found
No related merge requests found
...@@ -3,362 +3,160 @@ import { compile, match } from 'path-to-regexp' ...@@ -3,362 +3,160 @@ import { compile, match } from 'path-to-regexp'
import pages from '@src/js/common/consts/pages.js' import pages from '@src/js/common/consts/pages.js'
import objectTypes from '@src/js/common/consts/objectType.js' import objectTypes from '@src/js/common/consts/objectType.js'
const routes = { class Route {
TYPES: { constructor(pattern, params) {
format: params => { this.pattern = pattern
return _doFormat(params, '/types', { this.params = params
page: pages.TYPES }
})
}, format(params) {
parse: path => { if (_.isMatch(params, this.params)) {
return _doParse(path, '/types', { let toPath = compile(this.pattern, { encode: encodeURIComponent })
page: pages.TYPES
})
}
},
TYPES_SEARCH: {
format: params => {
return _doFormat(params, '/types-search/:id', {
page: pages.TYPES,
type: objectTypes.SEARCH
})
},
parse: path => {
return _doParse(path, '/types-search/:id', {
page: pages.TYPES,
type: objectTypes.SEARCH
})
}
},
NEW_OBJECT_TYPE: {
format: params => {
return _doFormat(params, '/new-object-type/:id', {
page: pages.TYPES,
type: objectTypes.NEW_OBJECT_TYPE
})
},
parse: path => {
return _doParse(path, '/new-object-type/:id', {
page: pages.TYPES,
type: objectTypes.NEW_OBJECT_TYPE
})
}
},
OBJECT_TYPE: {
format: params => {
return _doFormat(params, '/object-type/:id', {
page: pages.TYPES,
type: objectTypes.OBJECT_TYPE
})
},
parse: path => {
return _doParse(path, '/object-type/:id', {
page: pages.TYPES,
type: objectTypes.OBJECT_TYPE
})
}
},
NEW_COLLECTION_TYPE: {
format: params => {
return _doFormat(params, '/new-collection-type/:id', {
page: pages.TYPES,
type: objectTypes.NEW_COLLECTION_TYPE
})
},
parse: path => {
return _doParse(path, '/new-collection-type/:id', {
page: pages.TYPES,
type: objectTypes.NEW_COLLECTION_TYPE
})
}
},
COLLECTION_TYPE: {
format: params => {
return _doFormat(params, '/collection-type/:id', {
page: pages.TYPES,
type: objectTypes.COLLECTION_TYPE
})
},
parse: path => {
return _doParse(path, '/collection-type/:id', {
page: pages.TYPES,
type: objectTypes.COLLECTION_TYPE
})
}
},
NEW_DATA_SET_TYPE: {
format: params => {
return _doFormat(params, '/new-dataset-type/:id', {
page: pages.TYPES,
type: objectTypes.NEW_DATA_SET_TYPE
})
},
parse: path => {
return _doParse(path, '/new-dataset-type/:id', {
page: pages.TYPES,
type: objectTypes.NEW_DATA_SET_TYPE
})
}
},
DATA_SET_TYPE: {
format: params => {
return _doFormat(params, '/dataset-type/:id', {
page: pages.TYPES,
type: objectTypes.DATA_SET_TYPE
})
},
parse: path => {
return _doParse(path, '/dataset-type/:id', {
page: pages.TYPES,
type: objectTypes.DATA_SET_TYPE
})
}
},
NEW_MATERIAL_TYPE: {
format: params => {
return _doFormat(params, '/new-material-type/:id', {
page: pages.TYPES,
type: objectTypes.NEW_MATERIAL_TYPE
})
},
parse: path => {
return _doParse(path, '/new-material-type/:id', {
page: pages.TYPES,
type: objectTypes.NEW_MATERIAL_TYPE
})
}
},
MATERIAL_TYPE: {
format: params => {
return _doFormat(params, '/material-type/:id', {
page: pages.TYPES,
type: objectTypes.MATERIAL_TYPE
})
},
parse: path => {
return _doParse(path, '/material-type/:id', {
page: pages.TYPES,
type: objectTypes.MATERIAL_TYPE
})
}
},
NEW_VOCABULARY_TYPE: {
format: params => {
return _doFormat(params, '/new-vocabulary-type/:id', {
page: pages.TYPES,
type: objectTypes.NEW_VOCABULARY_TYPE
})
},
parse: path => {
return _doParse(path, '/new-vocabulary-type/:id', {
page: pages.TYPES,
type: objectTypes.NEW_VOCABULARY_TYPE
})
}
},
VOCABULARY_TYPE: {
format: params => {
return _doFormat(params, '/vocabulary-type/:id', {
page: pages.TYPES,
type: objectTypes.VOCABULARY_TYPE
})
},
parse: path => {
return _doParse(path, '/vocabulary-type/:id', {
page: pages.TYPES,
type: objectTypes.VOCABULARY_TYPE
})
}
},
USERS: {
format: params => {
return _doFormat(params, '/users', {
page: pages.USERS
})
},
parse: path => {
return _doParse(path, '/users', {
page: pages.USERS
})
}
},
USERS_SEARCH: {
format: params => {
return _doFormat(params, '/users-search/:id', {
page: pages.USERS,
type: objectTypes.SEARCH
})
},
parse: path => {
return _doParse(path, '/users-search/:id', {
page: pages.USERS,
type: objectTypes.SEARCH
})
}
},
NEW_USER: {
format: params => {
return _doFormat(params, '/new-user/:id', {
page: pages.USERS,
type: objectTypes.NEW_USER
})
},
parse: path => {
return _doParse(path, '/new-user/:id', {
page: pages.USERS,
type: objectTypes.NEW_USER
})
}
},
USER: {
format: params => {
return _doFormat(params, '/user/:id', {
page: pages.USERS,
type: objectTypes.USER
})
},
parse: path => {
return _doParse(path, '/user/:id', {
page: pages.USERS,
type: objectTypes.USER
})
}
},
NEW_USER_GROUP: {
format: params => {
return _doFormat(params, '/new-user-group/:id', {
page: pages.USERS,
type: objectTypes.NEW_USER_GROUP
})
},
parse: path => {
return _doParse(path, '/new-user-group/:id', {
page: pages.USERS,
type: objectTypes.NEW_USER_GROUP
})
}
},
USER_GROUP: {
format: params => {
return _doFormat(params, '/user-group/:id', {
page: pages.USERS,
type: objectTypes.USER_GROUP
})
},
parse: path => {
return _doParse(path, '/user-group/:id', {
page: pages.USERS,
type: objectTypes.USER_GROUP
})
}
},
TOOLS: {
format: params => {
return _doFormat(params, '/tools', {
page: pages.TOOLS
})
},
parse: path => {
return _doParse(path, '/tools', {
page: pages.TOOLS
})
}
},
TOOLS_SEARCH: {
format: params => {
return _doFormat(params, '/tools-search/:id', {
page: pages.TOOLS,
type: objectTypes.SEARCH
})
},
parse: path => {
return _doParse(path, '/tools-search/:id', {
page: pages.TOOLS,
type: objectTypes.SEARCH
})
}
},
NEW_DYNAMIC_PROPERTY_PLUGIN: {
format: params => {
return _doFormat(params, '/new-dynamic-property-plugin/:id', {
page: pages.TOOLS,
type: objectTypes.NEW_DYNAMIC_PROPERTY_PLUGIN
})
},
parse: path => {
return _doParse(path, '/new-dynamic-property-plugin/:id', {
page: pages.TOOLS,
type: objectTypes.NEW_DYNAMIC_PROPERTY_PLUGIN
})
}
},
DYNAMIC_PROPERTY_PLUGIN: {
format: params => {
return _doFormat(params, '/dynamic-property-plugin/:id', {
page: pages.TOOLS,
type: objectTypes.DYNAMIC_PROPERTY_PLUGIN
})
},
parse: path => {
return _doParse(path, '/dynamic-property-plugin/:id', {
page: pages.TOOLS,
type: objectTypes.DYNAMIC_PROPERTY_PLUGIN
})
}
},
NEW_ENTITY_VALIDATION_PLUGIN: {
format: params => {
return _doFormat(params, '/new-entity-validation-plugin/:id', {
page: pages.TOOLS,
type: objectTypes.NEW_ENTITY_VALIDATION_PLUGIN
})
},
parse: path => {
return _doParse(path, '/new-entity-validation-plugin/:id', {
page: pages.TOOLS,
type: objectTypes.NEW_ENTITY_VALIDATION_PLUGIN
})
}
},
ENTITY_VALIDATION_PLUGIN: {
format: params => {
return _doFormat(params, '/entity-validation-plugin/:id', {
page: pages.TOOLS,
type: objectTypes.ENTITY_VALIDATION_PLUGIN
})
},
parse: path => {
return _doParse(path, '/entity-validation-plugin/:id', {
page: pages.TOOLS,
type: objectTypes.ENTITY_VALIDATION_PLUGIN
})
}
},
DEFAULT: {
format: () => {
return { return {
path: '/', path: toPath(params),
match: 0 specificity: Object.keys(this.params).length
} }
}, } else {
parse: () => { return null
}
}
parse(path) {
let toPathObject = match(this.pattern, { decode: decodeURIComponent })
let pathObject = toPathObject(path)
if (pathObject) {
return { return {
path: '/', path: pathObject.path,
page: pages.TYPES ...pathObject.params,
...this.params
} }
} else {
return null
} }
} }
} }
class DefaultRoute {
format() {
return {
path: '/',
specificity: 0
}
}
parse() {
return {
path: '/',
page: pages.TYPES
}
}
}
const routes = {
TYPES: new Route('/types', {
page: pages.TYPES
}),
TYPES_SEARCH: new Route('/types-search/:id', {
page: pages.TYPES,
type: objectTypes.SEARCH
}),
NEW_OBJECT_TYPE: new Route('/new-object-type/:id', {
page: pages.TYPES,
type: objectTypes.NEW_OBJECT_TYPE
}),
OBJECT_TYPE: new Route('/object-type/:id', {
page: pages.TYPES,
type: objectTypes.OBJECT_TYPE
}),
NEW_COLLECTION_TYPE: new Route('/new-collection-type/:id', {
page: pages.TYPES,
type: objectTypes.NEW_COLLECTION_TYPE
}),
COLLECTION_TYPE: new Route('/collection-type/:id', {
page: pages.TYPES,
type: objectTypes.COLLECTION_TYPE
}),
NEW_DATA_SET_TYPE: new Route('/new-dataset-type/:id', {
page: pages.TYPES,
type: objectTypes.NEW_DATA_SET_TYPE
}),
DATA_SET_TYPE: new Route('/dataset-type/:id', {
page: pages.TYPES,
type: objectTypes.DATA_SET_TYPE
}),
NEW_MATERIAL_TYPE: new Route('/new-material-type/:id', {
page: pages.TYPES,
type: objectTypes.NEW_MATERIAL_TYPE
}),
MATERIAL_TYPE: new Route('/material-type/:id', {
page: pages.TYPES,
type: objectTypes.MATERIAL_TYPE
}),
NEW_VOCABULARY_TYPE: new Route('/new-vocabulary-type/:id', {
page: pages.TYPES,
type: objectTypes.NEW_VOCABULARY_TYPE
}),
VOCABULARY_TYPE: new Route('/vocabulary-type/:id', {
page: pages.TYPES,
type: objectTypes.VOCABULARY_TYPE
}),
USERS: new Route('/users', {
page: pages.USERS
}),
USERS_SEARCH: new Route('/users-search/:id', {
page: pages.USERS,
type: objectTypes.SEARCH
}),
NEW_USER: new Route('/new-user/:id', {
page: pages.USERS,
type: objectTypes.NEW_USER
}),
USER: new Route('/user/:id', {
page: pages.USERS,
type: objectTypes.USER
}),
NEW_USER_GROUP: new Route('/new-user-group/:id', {
page: pages.USERS,
type: objectTypes.NEW_USER_GROUP
}),
USER_GROUP: new Route('/user-group/:id', {
page: pages.USERS,
type: objectTypes.USER_GROUP
}),
TOOLS: new Route('/tools', {
page: pages.TOOLS
}),
TOOLS_SEARCH: new Route('/tools-search/:id', {
page: pages.TOOLS,
type: objectTypes.SEARCH
}),
NEW_DYNAMIC_PROPERTY_PLUGIN: new Route('/new-dynamic-property-plugin/:id', {
page: pages.TOOLS,
type: objectTypes.NEW_DYNAMIC_PROPERTY_PLUGIN
}),
DYNAMIC_PROPERTY_PLUGIN: new Route('/dynamic-property-plugin/:id', {
page: pages.TOOLS,
type: objectTypes.DYNAMIC_PROPERTY_PLUGIN
}),
NEW_ENTITY_VALIDATION_PLUGIN: new Route('/new-entity-validation-plugin/:id', {
page: pages.TOOLS,
type: objectTypes.NEW_ENTITY_VALIDATION_PLUGIN
}),
ENTITY_VALIDATION_PLUGIN: new Route('/entity-validation-plugin/:id', {
page: pages.TOOLS,
type: objectTypes.ENTITY_VALIDATION_PLUGIN
}),
DEFAULT: new DefaultRoute()
}
function format(params) { function format(params) {
let keys = Object.keys(routes) let keys = Object.keys(routes)
let best = { match: 0, path: null } let best = { specificity: 0, path: null }
for (let i = 0; i < keys.length; i++) { for (let i = 0; i < keys.length; i++) {
let route = routes[keys[i]] let route = routes[keys[i]]
try { try {
let result = route.format(params) let result = route.format(params)
if (result && result.match > best.match) { if (result && result.specificity > best.specificity) {
best = result best = result
} }
} catch (err) { } catch (err) {
...@@ -369,18 +167,6 @@ function format(params) { ...@@ -369,18 +167,6 @@ function format(params) {
return best.path return best.path
} }
function _doFormat(actualParams, pattern, requiredParams) {
if (_.isMatch(actualParams, requiredParams)) {
let toPath = compile(pattern, { encode: encodeURIComponent })
return {
path: toPath(actualParams),
match: Object.keys(requiredParams).length
}
} else {
return null
}
}
function parse(path) { function parse(path) {
let keys = Object.keys(routes) let keys = Object.keys(routes)
for (let i = 0; i < keys.length; i++) { for (let i = 0; i < keys.length; i++) {
...@@ -393,20 +179,6 @@ function parse(path) { ...@@ -393,20 +179,6 @@ function parse(path) {
return null return null
} }
function _doParse(path, pattern, extraParams) {
let toPathObject = match(pattern, { decode: decodeURIComponent })
let pathObject = toPathObject(path)
if (pathObject) {
return {
path: pathObject.path,
...pathObject.params,
...extraParams
}
} else {
return null
}
}
export default { export default {
...routes, ...routes,
format, format,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment