-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
143 lines (127 loc) · 6.63 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { COMMON_LAYOUT, COMMON_STYLE, COMMON_SCRIPT } from 'source/common/module/HTML.js'
import { DR_BROWSER_SCRIPT_TAG } from 'source/node/server/function.js'
import { initAuthMask } from 'source/node/server/Feature/Auth/HTML.js'
import { initModal } from 'source/node/server/Feature/@/HTML/Modal.js'
import { initLoadingMask } from 'source/node/server/Feature/@/HTML/LoadingMask.js'
import { pathContentStyle, initPathContent } from './pathContent.js'
import { initUploader } from './uploader.js'
const getHTML = ({
URL_AUTH_CHECK_ABBR, URL_ACTION_JSON_ABBR, URL_FILE_SERVE_ABBR, URL_FILE_UPLOAD,
IS_SKIP_AUTH, IS_READ_ONLY, IS_EXTRA_TAR, IS_EXTRA_AUTO,
ACTION_TYPE
}) => COMMON_LAYOUT([
'<title>Explorer</title>',
COMMON_STYLE(),
mainStyle,
pathContentStyle
], [
'<div id="control-panel" style="overflow-x: auto; white-space: nowrap; box-shadow: 0 0 8px 0 #888;"></div>',
'<div id="main-panel" style="position: relative; overflow: auto; flex: 1; min-height: 0;"></div>',
COMMON_SCRIPT({
INIT: [ // NOTE: shorter after minify
URL_AUTH_CHECK_ABBR, URL_ACTION_JSON_ABBR, URL_FILE_SERVE_ABBR, URL_FILE_UPLOAD,
IS_SKIP_AUTH, IS_READ_ONLY, IS_EXTRA_TAR, IS_EXTRA_AUTO,
ACTION_TYPE
],
initModal, initLoadingMask, initAuthMask, initPathContent, initUploader,
onload: onLoadFunc
}),
DR_BROWSER_SCRIPT_TAG()
])
const mainStyle = `<style>
* { font-family: monospace; }
</style>`
const onLoadFunc = () => {
const {
document, URL, location,
qS, cE, aCL,
INIT: [
URL_AUTH_CHECK_ABBR, URL_ACTION_JSON_ABBR, URL_FILE_SERVE_ABBR, URL_FILE_UPLOAD,
IS_SKIP_AUTH, IS_READ_ONLY, IS_EXTRA_TAR, IS_EXTRA_AUTO,
ACTION_TYPE
],
initModal, initLoadingMask, initAuthMask, initPathContent, initUploader,
Dr: {
Common: { String: { lazyEncodeURI }, Immutable: { StateStore: { createStateStore } } },
Browser: {
DOM: { applyReceiveFileListListener },
Module: { HistoryStateStore: { createHistoryStateStore } }
}
}
} = window
const initExplorer = async ({ authRevoke, authUrl, authFetch, authDownload }) => {
const { withAlertModal, withConfirmModal, withPromptModal } = initModal()
const { initialLoadingMaskState, wrapLossyLoading, renderLoadingMask } = initLoadingMask()
const { initialPathContentState, authFetchActionJSON, cyclePathSortType, getLoadPathAsync, getPathActionAsync, getPreviewFile, getDownloadFile, renderPathContent } = initPathContent(
URL_ACTION_JSON_ABBR, URL_FILE_SERVE_ABBR,
IS_READ_ONLY, IS_EXTRA_TAR, IS_EXTRA_AUTO,
ACTION_TYPE,
authFetch, withConfirmModal, withPromptModal
)
const { initialUploaderState, getUploadFileAsync, getAppendUploadFileList, renderUploader } = IS_READ_ONLY ? {} : initUploader(
URL_FILE_UPLOAD,
IS_READ_ONLY ? () => { throw new Error('deny file upload, read only') } : authFetch
)
const loadingMaskStore = createStateStore(initialLoadingMaskState)
const pathContentStore = createStateStore(initialPathContentState)
const uploaderStore = !IS_READ_ONLY && createStateStore(initialUploaderState)
const loadPathAsync = getLoadPathAsync(pathContentStore)
const loadPath = wrapLossyLoading(loadingMaskStore, loadPathAsync)
const pathAction = wrapLossyLoading(loadingMaskStore, getPathActionAsync(pathContentStore))
const previewFile = getPreviewFile(pathContentStore, authUrl)
const downloadFile = getDownloadFile(pathContentStore, authDownload)
const uploadFile = !IS_READ_ONLY && wrapLossyLoading(loadingMaskStore, getUploadFileAsync(uploaderStore, loadPathAsync))
const showStorageStatus = !IS_READ_ONLY && wrapLossyLoading(loadingMaskStore, async () => {
const { status } = await authFetchActionJSON(ACTION_TYPE.STATUS_SERVER_COMMON)
await withAlertModal(status)
})
const appendUploadFileList = !IS_READ_ONLY && getAppendUploadFileList(uploaderStore, () => ({
shouldAppend: !loadingMaskStore.getState().isLoading,
relativePath: pathContentStore.getState().pathContent.relativePath
}))
const createNewDirectory = async () => pathAction(
[ await withPromptModal('Directory Name', `new-directory-${Date.now().toString(36)}`) ], // TODO: cancel will still fetch and cause server 400
ACTION_TYPE.PATH_DIRECTORY_CREATE,
pathContentStore.getState().pathContent.relativePath
)
const updateSort = () => { qS('#button-sort').innerText = `Sort: ${pathContentStore.getState().pathSortType}` }
const cycleSort = () => {
cyclePathSortType(pathContentStore)
updateSort()
}
const historyStateStore = createHistoryStateStore()
const historyStateListener = (url) => loadPath(decodeURIComponent((new URL(url)).hash.slice(1)))
historyStateStore.subscribe(historyStateListener)
const loadPathWithHistoryState = (relativePath = pathContentStore.getState().pathContent.relativePath) => {
const urlObject = new URL(historyStateStore.getState())
urlObject.hash = `#${lazyEncodeURI(relativePath)}`
historyStateStore.setState(String(urlObject))
}
loadingMaskStore.subscribe(() => renderLoadingMask(loadingMaskStore))
pathContentStore.subscribe(() => renderPathContent(pathContentStore, qS('#main-panel'), loadPathWithHistoryState, pathAction, previewFile, downloadFile))
!IS_READ_ONLY && uploaderStore.subscribe(() => renderUploader(uploaderStore, uploadFile, appendUploadFileList))
aCL(qS('#control-panel'), [
cE('button', { innerText: 'To Root', onclick: () => loadPathWithHistoryState('.') }), // NOTE: using `loadPath` will skip update URL
cE('button', { innerText: 'Refresh', onclick: () => loadPath(pathContentStore.getState().pathContent.relativePath) }),
cE('button', { id: 'button-sort', onclick: cycleSort }),
...(IS_READ_ONLY ? [] : [
cE('span', { innerText: '|' }),
cE('button', { innerText: 'New Directory', onclick: createNewDirectory }),
cE('button', { innerText: 'Toggle Upload', onclick: () => uploaderStore.setState({ isActive: !uploaderStore.getState().isActive }) }),
cE('span', { innerText: '|' }),
cE('button', { innerText: 'Storage Status', onclick: () => showStorageStatus() }),
!IS_SKIP_AUTH && cE('button', { innerText: 'Auth Revoke', onclick: () => authRevoke().then(() => location.reload()) })
])
])
!IS_READ_ONLY && applyReceiveFileListListener(document.body, (fileList) => appendUploadFileList(fileList))
historyStateListener(historyStateStore.getState())
updateSort()
if (__DEV__) window.DEBUG = { loadingMaskStore, pathContentStore, uploaderStore }
}
initAuthMask({
IS_SKIP_AUTH,
URL_AUTH_CHECK_ABBR,
onAuthPass: initExplorer
})
}
export { getHTML }