|
| 1 | +import back_schemas from "@geode/opengeodeweb-back/opengeodeweb_back_schemas.json" |
| 2 | +import fileDownload from "js-file-download" |
| 3 | +import viewer_schemas from "@geode/opengeodeweb-viewer/opengeodeweb_viewer_schemas.json" |
| 4 | + |
| 5 | +export function useProjectManager() { |
| 6 | + const exportProject = async function () { |
| 7 | + console.log("[export triggered]") |
| 8 | + const appStore = useAppStore() |
| 9 | + const geode = useGeodeStore() |
| 10 | + const infraStore = useInfraStore() |
| 11 | + const snapshot = appStore.exportStores() |
| 12 | + const schema = back_schemas.opengeodeweb_back.export_project |
| 13 | + const defaultName = "project.vease" |
| 14 | + |
| 15 | + await infraStore.create_connection() |
| 16 | + let downloaded = false |
| 17 | + const result = await api_fetch( |
| 18 | + { schema, params: { snapshot, filename: defaultName } }, |
| 19 | + { |
| 20 | + response_function: function (response) { |
| 21 | + if (downloaded) return |
| 22 | + downloaded = true |
| 23 | + const data = response._data |
| 24 | + const headerName = |
| 25 | + (response.headers && |
| 26 | + typeof response.headers.get === "function" && |
| 27 | + (response.headers |
| 28 | + .get("Content-Disposition") |
| 29 | + ?.match(/filename=\"(.+?)\"/)?.[1] || |
| 30 | + response.headers.get("new-file-name"))) || |
| 31 | + defaultName |
| 32 | + if (!headerName.toLowerCase().endsWith(".vease")) { |
| 33 | + throw new Error("Server returned non-.vease project archive") |
| 34 | + } |
| 35 | + fileDownload(data, headerName) |
| 36 | + }, |
| 37 | + }, |
| 38 | + ) |
| 39 | + return result |
| 40 | + } |
| 41 | + |
| 42 | + const importProjectFile = async function (file) { |
| 43 | + const geode = useGeodeStore() |
| 44 | + const viewerStore = useViewerStore() |
| 45 | + const dataBaseStore = useDataBaseStore() |
| 46 | + const treeviewStore = useTreeviewStore() |
| 47 | + const hybridViewerStore = useHybridViewerStore() |
| 48 | + const infraStore = useInfraStore() |
| 49 | + |
| 50 | + await infraStore.create_connection() |
| 51 | + await viewerStore.ws_connect() |
| 52 | + |
| 53 | + const client = viewerStore.client |
| 54 | + if (client && client.getConnection && client.getConnection().getSession) { |
| 55 | + await client |
| 56 | + .getConnection() |
| 57 | + .getSession() |
| 58 | + .call("opengeodeweb_viewer.release_database", [{}]) |
| 59 | + } |
| 60 | + |
| 61 | + await viewer_call({ |
| 62 | + schema: viewer_schemas.opengeodeweb_viewer.viewer.reset_visualization, |
| 63 | + params: {}, |
| 64 | + }) |
| 65 | + |
| 66 | + treeviewStore.clear() |
| 67 | + dataBaseStore.clear() |
| 68 | + hybridViewerStore.clear() |
| 69 | + |
| 70 | + const schemaImport = back_schemas.opengeodeweb_back.import_project |
| 71 | + const form = new FormData() |
| 72 | + const originalFileName = file && file.name ? file.name : "project.vease" |
| 73 | + if (!originalFileName.toLowerCase().endsWith(".vease")) { |
| 74 | + throw new Error("Uploaded file must be a .vease") |
| 75 | + } |
| 76 | + form.append("file", file, originalFileName) |
| 77 | + |
| 78 | + const result = await $fetch(schemaImport.$id, { |
| 79 | + baseURL: geode.base_url, |
| 80 | + method: "POST", |
| 81 | + body: form, |
| 82 | + }) |
| 83 | + const snapshot = result && result.snapshot ? result.snapshot : {} |
| 84 | + |
| 85 | + treeviewStore.isImporting = true |
| 86 | + |
| 87 | + const client2 = viewerStore.client |
| 88 | + if ( |
| 89 | + client2 && |
| 90 | + client2.getConnection && |
| 91 | + client2.getConnection().getSession |
| 92 | + ) { |
| 93 | + await client2 |
| 94 | + .getConnection() |
| 95 | + .getSession() |
| 96 | + .call("opengeodeweb_viewer.import_project", [{}]) |
| 97 | + } |
| 98 | + |
| 99 | + await treeviewStore.importStores(snapshot.treeview) |
| 100 | + await hybridViewerStore.initHybridViewer() |
| 101 | + await hybridViewerStore.importStores(snapshot.hybridViewer) |
| 102 | + |
| 103 | + const snapshotDataBase = |
| 104 | + snapshot && snapshot.dataBase && snapshot.dataBase.db |
| 105 | + ? snapshot.dataBase.db |
| 106 | + : {} |
| 107 | + const items = Object.entries(snapshotDataBase).map(function (pair) { |
| 108 | + const id = pair[0] |
| 109 | + const item = pair[1] |
| 110 | + const binaryLightViewable = |
| 111 | + item && item.vtk_js && item.vtk_js.binary_light_viewable |
| 112 | + ? item.vtk_js.binary_light_viewable |
| 113 | + : undefined |
| 114 | + return { |
| 115 | + id: id, |
| 116 | + object_type: item.object_type, |
| 117 | + geode_object: item.geode_object, |
| 118 | + native_filename: item.native_filename, |
| 119 | + viewable_filename: item.viewable_filename, |
| 120 | + displayed_name: item.displayed_name, |
| 121 | + vtk_js: { binary_light_viewable: binaryLightViewable }, |
| 122 | + } |
| 123 | + }) |
| 124 | + |
| 125 | + await importWorkflowFromSnapshot(items) |
| 126 | + await hybridViewerStore.importStores(snapshot.hybridViewer) |
| 127 | + |
| 128 | + { |
| 129 | + const dataStyleStore = useDataStyleStore() |
| 130 | + await dataStyleStore.importStores(snapshot.dataStyle) |
| 131 | + } |
| 132 | + { |
| 133 | + const dataStyleStore = useDataStyleStore() |
| 134 | + await dataStyleStore.applyAllStylesFromState() |
| 135 | + } |
| 136 | + |
| 137 | + treeviewStore.finalizeImportSelection() |
| 138 | + treeviewStore.isImporting = false |
| 139 | + } |
| 140 | + |
| 141 | + return { exportProject, importProjectFile } |
| 142 | +} |
| 143 | + |
| 144 | +export default useProjectManager |
0 commit comments