Newer
Older
import React from 'react'
import openbis from '@src/js/services/openbis.js'
import objectType from '@src/js/common/consts/objectType.js'
import objectOperation from '@src/js/common/consts/objectOperation.js'
import routes from '@src/js/common/consts/routes.js'
import users from '@src/js/common/consts/users.js'
import cookie from '@src/js/common/cookie.js'
const AppContext = React.createContext()
export class AppController {
init(context) {
context.initState(this.initialState())
const history = createBrowserHistory({
basename: url.getApplicationPath() + '#'
})
history.listen(location => {
const route = routes.parse(location.pathname)
this.routeChanged(route.path)
this.context = context
initialState() {
return {
loaded: false,
loading: false,
session: null,
search: null,
pages: [],
error: null,
piotr.kupczyk@id.ethz.ch
committed
lastObjectModifications: {}
}
async load() {
const { loaded } = this.context.getState()
if (!loaded) {
try {
await this.context.setState({ loading: true })
await openbis.init()
const sessionToken = cookie.read(cookie.OPENBIS_COOKIE)
if (sessionToken) {
try {
openbis.useSession(sessionToken)
const sessionInformation = await openbis.getSessionInformation()
if (sessionInformation) {
await this.context.setState({
session: {
sessionToken: sessionToken,
userName: sessionInformation.userName
}
})
const routeObject = routes.parse(this.getRoute())
await this.routeChanged(routeObject.path)
} else {
openbis.useSession(null)
}
piotr.kupczyk@id.ethz.ch
committed
const serverInformation = await openbis.getServerInformation()
await this.context.setState({
serverInformation
})
} catch (e) {
openbis.useSession(null)
}
}
await this.context.setState({ loaded: true })
} catch (e) {
await this.context.setState({ error: e })
} finally {
await this.context.setState({ loading: false })
}
}
}
async login(username, password) {
try {
await this.context.setState({ loading: true })
const sessionToken = await openbis.login(username, password)
if (sessionToken !== null) {
this.context.setState({
session: {
sessionToken: sessionToken,
userName: username
}
})
cookie.create(cookie.OPENBIS_COOKIE, sessionToken, 7)
piotr.kupczyk@id.ethz.ch
committed
const serverInformation = await openbis.getServerInformation()
await this.context.setState({
serverInformation
})
const routeObject = routes.parse(this.getRoute())
await this.routeChange(routeObject.path)
} else {
throw Error('Session token null')
}
} catch (e) {
await this.context.setState({ error: 'Incorrect user or password' })
} finally {
await this.context.setState({ loading: false })
}
}
async logout() {
try {
await this.context.setState({ loading: true })
await openbis.logout()
this.context.setState(state => ({
...this.initialState(),
loaded: state.loaded
}))
cookie.erase(cookie.OPENBIS_COOKIE)
} catch (e) {
await this.context.setState({ error: e })
} finally {
await this.context.setState({ loading: false })
}
}
async search(page, text) {
if (text.trim().length > 0) {
await this.objectOpen(page, objectType.SEARCH, text.trim())
}
await this.context.setState({ search: '' })
}
async searchChange(text) {
await this.context.setState({ search: text })
}
async pageChange(page) {
const pageRoute = this.getCurrentRoute(page)
if (pageRoute) {
} else {
const route = routes.format({ page })
piotr.kupczyk@id.ethz.ch
committed
async loadingChange(loading) {
await this.context.setState({ loading: loading })
}
async errorChange(error) {
await this.context.setState({ error: error })
}
const newRoute = routes.parse(path)
if (newRoute.type && newRoute.id) {
const object = { type: newRoute.type, id: newRoute.id }
const openTabs = this.getOpenTabs(newRoute.page)
if (openTabs) {
let found = false
let id = 1
openTabs.forEach(openTab => {
if (_.isMatch(openTab.object, object)) {
found = true
}
if (openTab.id >= id) {
id = openTab.id + 1
}
})
if (!found) {
await this.addOpenTab(newRoute.page, id, { id, object })
await this.setCurrentRoute(newRoute.page, newRoute.path)
async routeChange(path) {
if (path !== this.history.location.pathname) {
this.history.push(path)
async routeReplace(route) {
this.history.replace(route)
}
async objectNew(page, type) {
let id = 1
const openObjects = this.getOpenObjects(page)
openObjects.forEach(openObject => {
if (openObject.type === type) {
id++
}
})
const route = routes.format({ page, type, id })
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
}
async objectCreate(page, oldType, oldId, newType, newId) {
const openTabs = this.getOpenTabs(page)
const oldTab = _.find(openTabs, { object: { type: oldType, id: oldId } })
if (oldTab) {
const newTab = {
...oldTab,
object: { type: newType, id: newId },
changed: false
}
await this.replaceOpenTab(page, oldTab.id, newTab)
await this.setLastObjectModification(
newType,
objectOperation.CREATE,
Date.now()
)
const route = routes.format({ page, type: newType, id: newId })
await this.routeReplace(route)
}
}
async objectOpen(page, type, id) {
const route = routes.format({ page, type, id })
await this.routeChange(route)
}
async objectUpdate(type) {
await this.setLastObjectModification(
type,
objectOperation.UPDATE,
Date.now()
)
}
async objectDelete(page, type, id) {
await this.objectClose(page, type, id)
await this.setLastObjectModification(
type,
objectOperation.DELETE,
Date.now()
)
}
async objectChange(page, type, id, changed) {
const openTabs = this.getOpenTabs(page)
const oldTab = _.find(openTabs, { object: { type, id } })
if (oldTab) {
const newTab = { ...oldTab, changed }
await this.replaceOpenTab(page, oldTab.id, newTab)
}
}
async objectClose(page, type, id) {
const openTabs = this.getOpenTabs(page)
const objectToClose = { type, id }
let selectedObject = this.getSelectedObject(page)
if (selectedObject && _.isEqual(selectedObject, objectToClose)) {
if (_.size(openTabs) === 1) {
selectedObject = null
} else {
let selectedIndex = _.findIndex(openTabs, { object: selectedObject })
if (selectedIndex === 0) {
selectedObject = openTabs[selectedIndex + 1].object
} else {
selectedObject = openTabs[selectedIndex - 1].object
}
}
}
let tabToClose = _.find(openTabs, { object: objectToClose })
if (tabToClose) {
await this.removeOpenTab(page, tabToClose.id)
}
if (selectedObject) {
const route = routes.format({
page,
type: selectedObject.type,
id: selectedObject.id
})
await this.routeChange(route)
} else {
const route = routes.format({ page })
await this.routeChange(route)
}
}
getLoaded() {
return this.context.getState().loaded
}
getLoading() {
return this.context.getState().loading
}
piotr.kupczyk@id.ethz.ch
committed
getServerInformation(key) {
piotr.kupczyk@id.ethz.ch
committed
const serverInformation = this.context.getState().serverInformation || {}
return serverInformation[key]
piotr.kupczyk@id.ethz.ch
committed
}
getSession() {
return this.context.getState().session
}
piotr.kupczyk@id.ethz.ch
committed
getSessionToken() {
const session = this.getSession()
return session ? session.sessionToken : null
}
getUser() {
const session = this.getSession()
return session ? session.userName : null
}
isSystemUser() {
return this.getUser() === users.SYSTEM
}
return routes.parse(this.history.location.pathname).path
}
getSearch() {
return this.context.getState().search
}
getError() {
return this.context.getState().error
}
getCurrentPage() {
const route = this.getRoute()
return routes.parse(route).page
}
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
getCurrentRoute(page) {
const { pages } = this.context.getState()
return pages[page] ? pages[page].currentRoute : null
}
async setCurrentRoute(page, route) {
await this.context.setState(state => ({
pages: {
...state.pages,
[page]: {
...state.pages[page],
currentRoute: route
}
}
}))
}
getSelectedObject(page) {
const path = this.getCurrentRoute(page)
if (path) {
const route = routes.parse(path)
if (route && route.type && route.id) {
return { type: route.type, id: route.id }
}
}
return null
}
getSelectedTab(page) {
const selectedObject = this.getSelectedObject(page)
if (selectedObject) {
const openTabs = this.getOpenTabs(page)
return _.find(openTabs, { object: selectedObject })
} else {
return null
}
}
getOpenObjects(page) {
const openTabs = this.getOpenTabs(page)
return openTabs.map(openTab => {
return openTab.object
})
}
getOpenTabs(page) {
const { pages } = this.context.getState()
return (pages[page] && pages[page].openTabs) || []
}
async setOpenTabs(page, newOpenTabs) {
await this.context.setState(state => ({
pages: {
...state.pages,
[page]: {
...state.pages[page],
openTabs: newOpenTabs
}
}
}))
}
async addOpenTab(page, id, tab) {
const openTabs = this.getOpenTabs(page)
const index = _.findIndex(openTabs, { id: id }, _.isMatch)
if (index === -1) {
const newOpenTabs = Array.from(openTabs)
newOpenTabs.push(tab)
await this.setOpenTabs(page, newOpenTabs)
}
}
async removeOpenTab(page, id) {
const openTabs = this.getOpenTabs(page)
const index = _.findIndex(openTabs, { id: id }, _.isMatch)
if (index !== -1) {
const newOpenTabs = Array.from(openTabs)
newOpenTabs.splice(index, 1)
await this.setOpenTabs(page, newOpenTabs)
}
}
async replaceOpenTab(page, id, tab) {
const openTabs = this.getOpenTabs(page)
const index = _.findIndex(openTabs, { id: id }, _.isMatch)
if (index !== -1) {
const newOpenTabs = Array.from(openTabs)
newOpenTabs[index] = tab
await this.setOpenTabs(page, newOpenTabs)
}
}
getLastObjectModifications() {
return this.context.getState().lastObjectModifications
}
async setLastObjectModification(type, operation, timestamp) {
piotr.kupczyk@id.ethz.ch
committed
const { lastObjectModifications } = this.context.getState()
if (
!lastObjectModifications[type] ||
!lastObjectModifications[type][operation] ||
lastObjectModifications[type][operation] < timestamp
) {
piotr.kupczyk@id.ethz.ch
committed
lastObjectModifications: {
...lastObjectModifications,
[type]: { ...lastObjectModifications[type], [operation]: timestamp }
}
})
}
withState(additionalPropertiesFn) {
const WithContext = Component => {
const WithConsumer = props => {
return React.createElement(AppContext.Consumer, {}, () => {
const additionalProperties = additionalPropertiesFn
? additionalPropertiesFn(props)
: {}
return React.createElement(Component, {
...props,
}
WithConsumer.displayName = 'WithConsumer'
return WithConsumer
}
WithContext.displayName = 'WithContext'
return WithContext
}
}
let INSTANCE = new AppController()
export default {
AppContext,
AppController,
getInstance() {
return INSTANCE
},
setInstance(instance) {
INSTANCE = instance
}