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'
export class AppController {
constructor() {
this.AppContext = React.createContext()
const history = createBrowserHistory({
basename: url.getApplicationPath() + '#'
})
history.listen(location => {
let route = routes.parse(location.pathname)
if (route.path !== this.getRoute()) {
this.routeChange(route.path, location.state)
}
})
this.history = history
}
init(context) {
this.context = context
context.initState(this.initialState())
}
initialState() {
return {
loaded: false,
loading: false,
session: null,
route: null,
search: null,
pages: [],
error: null,
piotr.kupczyk@id.ethz.ch
committed
lastObjectModifications: {}
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
}
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
}
})
} else {
openbis.useSession(null)
}
} 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)
this.context.setState({
session: {
sessionToken: sessionToken,
userName: username
}
})
cookie.create(cookie.OPENBIS_COOKIE, sessionToken, 7)
const { route } = this.context.getState()
const routeObject = routes.parse(route)
} 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 })
}
}
async errorChange(error) {
await this.context.setState({ error: error })
}
async routeChange(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.context.setState({ route: newRoute.path })
await this.setCurrentRoute(newRoute.page, newRoute.path)
if (newRoute.path !== this.history.location.pathname) {
this.history.push(newRoute.path)
}
}
async routeReplace(route, state) {
}
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 })
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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
}
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
}
getSession() {
return this.context.getState().session
}
getUser() {
const session = this.getSession()
return session ? session.userName : null
}
isSystemUser() {
return this.getUser() === users.SYSTEM
}
getRoute() {
return this.context.getState().route
}
getSearch() {
return this.context.getState().search
}
getError() {
return this.context.getState().error
}
getCurrentPage() {
const { route } = this.context.getState()
return routes.parse(route).page
}
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
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
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(this.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 function setInstance(instance) {
INSTANCE = instance
}
export function getInstance() {
return INSTANCE
}
export default INSTANCE