UNPKG

glodon-bimface-vue3

Version:

Bimface Component for Vue3

453 lines (452 loc) 18.1 kB
import { ref as Y, toRefs as Z, onMounted as ee, watch as te, onBeforeUnmount as ne, openBlock as oe, createElementBlock as ae } from "vue"; var D = window.hostConfig || { APIHost: "https://api.bimface.com", resourceHost: "https://m.bimface.com", staticHost: "https://static.bimface.com", dataEnvType: "BIMFACE" }; Object.assign === void 0 && (Object.assign = function(o) { if (o == null) throw new TypeError("Cannot convert undefined or null to object"); for (var e = Object(o), t = 1; t < arguments.length; t++) { var n = arguments[t]; if (n != null) for (var a in n) Object.prototype.hasOwnProperty.call(n, a) && (e[a] = n[a]); } return e; }); let m = Object.freeze({ Release: "Release", Debug: "Debug" }), b = Object.freeze({ Normal: "Normal", DrawingView: "drawingView" }), V = Object.freeze({ BIMFACE: "BIMFACE", Local: "Local" }), ie = Object.freeze({ zh_CN: "zh_CN", en_GB: "en_GB", sv_SE: "sv_SE" }), re = Object.freeze({ Normal: "Normal", Bake: "Bake" }); var G = function() { if (window.hostConfig) for (let o in window.hostConfig) D[o] = window.hostConfig[o]; return { staticHost: `${D.staticHost}/api`, APIHost: D.APIHost, language: "zh_CN", viewToken: null, configuration: m.Release, dataEnvType: D.dataEnvType || "BIMFACE", viewType: b.Normal, visualStyle: re.Bake, version: "" }; }; window.BimfaceSDKLoaderConfig = G, window.BimfaceEnvOption = V, window.BimfaceLanguageOption = ie, window.BimfaceConfigrationOption = m, window.BimfaceViewTypeOption = b; var se = "2021-4-2-18-18"; let y = function(o) { let e = o.split("/"); return e.pop(), e.join("/") + "/"; }; var ce = function(o) { let e = Object.assign({}, o), t = "/Glodon"; return o.path ? (e.dataEnvType = V.Local, e.url = e.path, e.staticHost = e.sdkPath || y(e.path), e.resourcePath = y(e.path), e.path = y(e.path), t = e.sdkPath ? "" : "/jssdk") : o.resourcePath ? (e.dataEnvType = V.Local, e.url = e.resourcePath, e.resourcePath = y(e.resourcePath)) : (e.sdkPath && (t = "", e.staticHost = e.sdkPath), e.url = `${o.APIHost}/inside/databag?viewToken=${o.viewToken}`), e.staticHost += t, o.build != m.Debug && e.configuration != m.Release || (e.configuration = ""), e.configuration = e.configuration ? `-${e.configuration.toLowerCase()}` : "", e; }, K = function(o, e) { for (var t = 0; t < e.length; t++) e[t] = o + e[t]; }, de = function(o, e) { return o.renderType == "drawingView" || e.viewType == b.DrawingView; }, N = function(o) { var e = o.sdkVersion, t = o.options, n = t.configuration, a = [], i = [`/${e}/${t.language}.js`, `/${e}/Application${n}.js`]; return a = de(o.metadata, o.options) ? [...a, ...i, `/${e}/Drawing.css`, `/${e}/bimface.bufferfly.js`, `/${e}/Drawing${n}.js`] : [...a, ...i, `/${e}/Bimface.css`, `/${e}/thirdparty.js`, `/${e}/lib/loaders/BimTilesLoader.js`, `/${e}/Bimface${n}.js`]; }; window.postProcessing = function(o) { var e = o.metadata, t = o.options, n = o.successCb, a = N(o); K(t.staticHost, a), e.databagId = `${e.databagId}`, t.path ? (e.path = t.path, e.dataPath = "./") : t.resourcePath && (e.path = t.resourcePath.replace("viewToken", "")), e.sdkPath = t.sdkPath, a.length == 0 ? n(e) : E(a, function() { if (t.build === m.Debug && t.dataPath) { let i = t.dataPath.split("/"); n({ databagId: i.pop(), path: i.join("/") }); } n(e); }); }; var E = function(o, e, t) { var n = o.length, a = 0, i = function(r) { if (r && r.message == "error" && r.element.indexOf("bimface.index") > -1) { a = 0; var s = t.options; s.build, m.Release; var c = N(t); return K(s.staticHost, c), void E(c, e); } ++a == n ? e() : $(o[a], i); }; $(o[a], i); }, _ = [], $ = function(o, e) { if (!(_.indexOf(o.split("/").pop()) > -1)) { var t, n = document.getElementsByTagName("head")[0]; return o.indexOf(".css") > -1 ? ((t = document.createElement("link")).setAttribute("href", o), t.setAttribute("rel", "stylesheet")) : (t = document.createElement("script")).setAttribute("src", o), t.url = o, n.appendChild(t), t.addEventListener("load", function() { _.push(this.url.split("/").pop()), e && e({ message: "success" }); }), t.addEventListener("error", function() { e && e({ element: o, message: "error" }); }), t; } e(); }; window.loadResource = E; var le = function(o) { var e, t = Object.assign({ type: "get", data: null, success: null, failure: null }, o); (e = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP")).onreadystatechange = function() { if (e.readyState == 4) { var n = e.status; n >= 200 && n < 300 || n == 304 || n === 0 && window.location.protocol === "file:" ? t.success && t.success(e.responseText, e.responseXML) : t.failure && t.failure(n); } }, e.open(t.type, t.url, t.async), e.send(t.data); }; class ue { constructor(e) { this.indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB, this.indexedDB || console.log("IndexedDB not supported"), this._db = null, this._opt = e; } open(e, t) { const n = e || this._opt.name, a = t || this._opt.version || 1, i = this.indexedDB.open(n, a); return new Promise((r, s) => { i.onsuccess = (c) => { this._db = i.result, r(this._db); }, i.onupgradeneeded = (c) => { let f = this._db = c.target.result; (this._opt.storeList || []).forEach((l) => !f.objectStoreNames.contains(l) && f.createObjectStore(l)); }, i.onerror = (c) => { s(c); }; }); } getDB() { return new Promise((e, t) => { this._db ? e(this._db) : this.open().then(e).catch(t); }); } addObject(e, t, n) { return new Promise((a, i) => { this.getDB().then((r) => { const s = r.transaction(e, "readwrite"); s.objectStore(e).put(t, n).onsuccess = (c) => { a(c.target.result); }, s.onerror = (c) => { i(c); }; }).catch(i); }); } getObject(e, t) { return new Promise((n, a) => { this.getDB().then((i) => { const r = i.transaction(e, "readonly"); r.objectStore(e).get(t).onsuccess = (s) => { let c = s.target.result; c ? n(c) : a(s); }, r.onerror = (s) => { a(s); }; }).catch(a); }); } deleteObject(e, t) { return new Promise((n, a) => { this.getDB().then((i) => { const r = i.transaction(e, "readwrite"); r.objectStore(e).delete(t).onsuccess = (s) => { n(s.target.result); }, r.onerror = (s) => { a(s); }; }).catch(a); }); } clearStore(e) { return new Promise((t, n) => { this.getDB().then((a) => { const i = a.transaction(e, "readwrite"); i.objectStore(e).clear().onsuccess = (r) => { t(r.target.result); }, i.onerror = (r) => { n(r); }; }).catch(n); }); } deleteDB(e) { return new Promise((t) => { this.indexedDB.deleteDatabase(e), t(); }); } getAllKeys(e) { return new Promise((t, n) => { this.getDB().then((a) => { const i = a.transaction(e, "readonly"); i.objectStore(e).getAllKeys().onsuccess = (r) => t(r.target.result), i.onerror = n; }).catch(n); }); } getAll(e) { return new Promise((t, n) => { this.getDB().then((a) => { const i = a.transaction(e, "readonly"); i.objectStore(e).getAll().onsuccess = (r) => t(r.target.result), i.onerror = n; }).catch(n); }); } } class T extends ue { constructor() { super({ name: "Bf_Loader", version: 1, storeList: ["d", "t"] }); } getDatabagInfo(e, t) { return new Promise((n, a) => { t ? this.getObject("d", e).then((i) => this.addTemp(i, t).then(() => n(i)).catch(a)).catch(a) : this.getObject("d", e).then(n).catch(a); }); } addDatabagInfo(e, t) { return new Promise((n, a) => { const i = e.modelId, r = () => Promise.all([this.addObject("d", e, i), this.addTemp(e, t)]).then(n).catch(a); this.getDatabagInfo(i).then((s) => { s.databagId !== e.databagId && this.deleteDB(`Bf_${data.databagId}`), r(); }).catch(() => { r(); }); }); } deleteDatabagInfo(e) { return new Promise((t, n) => { this.deleteObject("d", e).then(t).catch(n); }); } addTemp(e, t) { return new Promise((n, a) => { this.clearStore("t").then(() => { this.addObject("t", e, t).then(n).catch(a); }).catch(a); }); } getTemp(e) { return new Promise((t, n) => { this.getObject("t", e).then(t).catch(n); }); } deleteStorageByModelId(e) { return new Promise((t) => { this.getDatabagInfo(e).then((n) => { let a = n.renderType === "gisView" ? `Bg_${n.modelId}` : `Bf_${n.databagId}`; Promise.allSettled([this.deleteDB(a), this.deleteDatabagInfo(e)]).then(t); }).catch(t); }); } getStoredModelIds() { return this.getAllKeys("d"); } getStoredModelInfo() { return new Promise((e, t) => { this.getAll("d").then((n) => { let a = n.map((i) => { let { modelId: r, name: s, renderType: c } = i; return { modelId: r, name: s, type: c }; }); e(a); }).catch(t); }); } } var fe = function(o, e, t) { const n = (a) => { o.enableStorage && o.modelId ? new T().getDatabagInfo(o.modelId, o.viewToken).then((i) => t && t(i)).catch(() => { console.error("[BIMFACE ERROR]: failed get model info from storage"), e && e(a); }) : e && e(a); }; o.viewToken || o.dataEnvType === "Local" ? le({ url: o.url, async: !0, success: function(a) { var i = JSON.parse(a); if (o.dataEnvType !== "Local" && i.code !== "success") return i.message && console.error(`[BIMFACE ERROR]: ${i.message}`), void n(a); i = i.data || i, o.enableStorage ? new T().addDatabagInfo(i, o.viewToken).then(() => t && t(i)).catch(() => t && t(i)) : t && t(i); }, failure: n }) : n(); }, ge = function(o, e) { let t, n = e.version; if (o.renderVersion, /\d+?\.\d+?\.\d+/.test(n) && n.split(".")[0] >= 3) { const [r, s, c] = n.split("."); s === "6" && Number(c) < 143 && (n = `Bimface@${n}`); } else if (e.build == "Debug") n = "Bimface", t = "Application"; else if (e.sdkPath) n = t = "bimface"; else if (e.viewType == b.DrawingView && o.renderType != "drawingView") { var a = o.subRenders; if (a && a.length != 0) for (var i = 0; i < a.length; i++) a[i].renderType == b.DrawingView && (n = a[i].jsSDKVersion, t = a[i].jsSDKVersion); } else n = o.jsSDKVersion, t = o.jsSDKVersion; return { ui: t, sdk: n }; }, H = function(o, e, t) { fe(o, t, function(n) { var a = ge(n, o); window.BimfaceLoaderConfig.fullStaticHost = BimfaceLoaderConfig.dataEnvType == "Local" ? o.staticHost + "/bimface" : o.staticHost + "/" + a.sdk; var i = { metadata: n, options: o, successCb: e, sdkVersion: a.sdk, uiVersion: a.ui }, r = window.BimfaceLoaderConfig.fullStaticHost + "/bimface.index.js"; E([r], function() { postProcessing(i); }, i); }); }, x = function(o, e) { var t = o.renderType == "bimView" ? "3DView" : o.renderType, n = (o.subRenders, { dataEnvType: e.dataEnvType, viewToken: e.viewToken, staticHost: e.staticHost, APIHost: e.APIHost, viewType: t }); return Object.assign(n, o); }, pe = { getStorage() { return this.storage = this.storage || new T(), this.storage; }, deleteStorageByModelId(o) { return this.getStorage().deleteStorageByModelId(o); }, getStoredModelIds() { return this.getStorage().getStoredModelIds(); }, getStoredModelInfo() { return this.getStorage().getStoredModelInfo(); }, store(o) { let { url: e, viewToken: t, storeMaterialOverride: n, successCallback: a, progressCallback: i, errorCallback: r, conditions: s, storeByQueue: c } = o; const f = c && c.enable, l = f && (c.delay || 2e3); if (!t) return; e = e || [], a = a || function() { }, i = i || function() { }, r = r || function() { }; const u = new BimfaceSDKLoaderConfig(); u.enableStorage = !0, u.viewToken = t, BimfaceSDKLoader.load(u).then((P) => { if (P.viewType == "3DView") { const z = document.createElement("div"), S = new Glodon.Bimface.Viewer.Viewer3DConfig(); S.domElement = z, S.enableStorage = !0; const d = new Glodon.Bimface.Viewer.Viewer3D(S), W = new Promise((X) => { d.addModel(P), f || d.addEventListener(Glodon.Bimface.Viewer.Viewer3DEvent.ViewLoading, (v) => i(v.progress)); const h = () => { requestAnimationFrame(() => { d.destroy(), X(); }); }; let j = !1; d.addEventListener(Glodon.Bimface.Viewer.Viewer3DEvent.ViewAdded, () => { if (j) return; const v = () => { j = !0; const L = d.getDefaultModel(), A = L.getCloudViewer().getModelManager().getModel(L.modelId), I = [...A._handler.layerProvider.getAllLayerIdxData().layerKeys], q = [...A._handler.layerProvider.getLayerKeyAttributes()]; let B = 0; const O = {}, M = () => { if (i(Math.floor(100 * (B + 1) / I.length)), B === I.length) return void h(); I[B].split("-").forEach((U, Q) => O[q[Q]] = U); const k = d.getDefaultModel(); k && k.destroy(); const J = isNaN(l) ? 2e3 : Number(l); setTimeout(() => { d.addView(t); }, J), B++; }; d.addEventListener(Glodon.Bimface.Viewer.Viewer3DEvent.ModelAdded, () => { d.showExclusiveComponentsByObjectData([O], null, M); }), M(); }; n ? d.loadMaterialOverrideSet(d.getModel().modelId, t, function() { s ? d.showExclusiveComponentsByObjectData(s, null, h) : f ? v() : (d.showAllComponents(), d.addEventListener(Glodon.Bimface.Viewer.Viewer3DEvent.DemandLoaded, h)); }) : s ? d.showExclusiveComponentsByObjectData(s, null, h) : f ? v() : h(); }); }); Promise.all([W, d._storeData(e)]).then(a).catch(r); } }).catch(); } }, we = { Version: se, load: function(o, e, t) { window.BimfaceLoaderConfig = o, o.build == null && (o.build = m.Release); var n = ce(o); if (!e && !t) return new Promise((a, i) => { H(n, function(r) { a(x(r, o)); }, i); }); H(n, function(a) { e && e(x(a, o)); }, function(a) { t && t(); }); }, Storage: pe }; window.BimfaceSDKLoader = we, window.Glodon || (window.Glodon = {}); const g = window.Glodon, me = window.BimfaceSDKLoader, p = {}, w = {}, C = (o, e, t = null) => { t ? p[t] ? console.warn("BIMFACE Component Error: Multiple Bimface Component instances with same instanceId") : (p[t] = { app: o, viewer: e }, w[t] && w[t].forEach((n) => n instanceof Function && n({ app: o, viewer: e }))) : Object.keys(p).length === 0 ? (p.default = { app: o, viewer: e }, w.default && w.default.forEach((n) => n instanceof Function && n({ app: o, viewer: e }))) : console.warn("BIMFACE Component Error: Multiple Bimface Component instances but no instanceId"); }, be = (o, e = null) => { const t = e || "default"; w[t] || (w[t] = []), w[t].push(o); }, R = (o, e, t) => { const n = t || "default"; p[n] && F(n); let a, i; const r = () => { let s = new G(); s.viewToken = e, me.load(s, c, f); function c(l) { if (!l.viewType) return !1; if (l.viewType === "3DView") { const u = new g.Bimface.Application.WebApplication3DConfig(); u.domElement = o, a = new g.Bimface.Application.WebApplication3D( u ), i = a.getViewer(), a.addView(l.viewToken), i.addEventListener( g.Bimface.Viewer.Viewer3DEvent.ViewAdded, () => { C(a, i, t); } ); } else if (l.viewType === "drawingView") { const u = new g.Bimface.Application.WebApplicationDrawingConfig(); u.domElement = o, a = new g.Bimface.Application.WebApplicationDrawing( u ), i = a.getViewer(), i.loadDrawing({ viewToken: e }), i.addEventListener( g.Bimface.Viewer.ViewerDrawingEvent.Loaded, () => { C(a, i, t); } ); } else if (l.viewType === "gisView") { const u = new g.Bimface.Application.WebApplicationGISConfig(); u.domElement = o, a = new g.Bimface.Application.WebApplicationGIS( u ), i = a.getViewer(), i.addScene(e), i.addEventListener( g.Bimface.Viewer.ViewerGISEvent.SceneAdded, () => { C(a, i, t); } ); } } function f(l) { console.log(`BimfaceSDKLoader.load Error: ${l}`); } }; if (e) r(); else { const s = new XMLHttpRequest(); s.open("GET", "https://bimface.com/api/console/share/preview/viewtoken?token=d228cd31", !0), s.onreadystatechange = function() { if (s.readyState == 4) { const c = JSON.parse(s.responseText); c.data && (e = c.data, r()); } }, s.send(); } }, F = (o) => { const e = o || "default", t = p[e]; if (t && t.app) { let n = null, a = null; t.viewer.viewerType === "ViewerDrawing" && (n = t.viewer.getDomElement().parentElement, a = t.viewer.getDomElement()), t.app.destroy && t.app.destroy(), n && a && n.removeChild(a); } t.app = null, t.viewer = null, delete p[e]; }, ve = { __name: "BimfaceComponent", props: { viewToken: String, instanceId: String }, setup(o) { const e = o, t = Y(null), { viewToken: n, instanceId: a } = Z(e); return ee(() => { n.value && R(t.value, n.value, a.value), te(n, (i) => { R(t.value, i, a.value); }); }), ne(() => { F(a.value); }), (i, r) => (oe(), ae("div", { ref_key: "bimfaceContainer", ref: t }, null, 512)); } }; export { ve as BimfaceComponent, g as Glodon, be as onBimfaceModelLoaded };