From d0e8b53b405c9cb1f6eacccd5cb62953f5fcc307 Mon Sep 17 00:00:00 2001 From: cavencj Date: Tue, 22 Oct 2024 16:50:24 +0800 Subject: [PATCH] change the framework --- .npmrc | 1 + docs/zh/api/global.md | 4 +- examples/echarts/airline.html | 790 +-- examples/echarts/logistics.html | 800 +-- examples/echarts/migrate.html | 619 +- examples/echarts/plane.html | 701 +-- examples/echarts/pm.html | 987 ++-- examples/echarts/population_mobility.html | 6511 +++++++++++---------- examples/info/custom.html | 100 +- gulpfile.js | 110 +- package.json | 6 +- src/global-api/index.js | 6 + src/global-api/lib-utils.js | 4 + src/global-api/paramter.js | 23 + src/index.js | 72 +- src/modules/chart/ChartLayer.js | 10 +- src/modules/chart/GLMapCoordSys.js | 69 +- src/modules/chart/GLMapModel.js | 17 - src/modules/chart/GLMapView.js | 30 - src/modules/chart/index.js | 70 +- src/modules/math/index.js | 54 +- src/modules/third-part/index.js | 87 +- src/modules/viewer/Viewer.js | 46 +- src/namespace/index.js | 10 +- 24 files changed, 5582 insertions(+), 5545 deletions(-) create mode 100644 .npmrc create mode 100644 src/global-api/index.js create mode 100644 src/global-api/paramter.js delete mode 100644 src/modules/chart/GLMapModel.js delete mode 100644 src/modules/chart/GLMapView.js diff --git a/.npmrc b/.npmrc new file mode 100644 index 00000000..a251dd73 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +registry = "https://registry.npmmirror.com/" diff --git a/docs/zh/api/global.md b/docs/zh/api/global.md index b9ab1d7f..ade8a17a 100644 --- a/docs/zh/api/global.md +++ b/docs/zh/api/global.md @@ -15,8 +15,6 @@ DC.ready({}).then(() => {}) ```json //属性参数(可选) { - "Cesium": "<自定义的Cesium库,如果未设置,将使用框架内部默认Cesium框架>", - "echarts": "", "baseUrl": "" } ``` @@ -27,7 +25,7 @@ DC.ready({}).then(() => {}) ```js DC.registerLib('turf', turf) -console.log(DC.__namespace.turf) +console.log(DC.getLib('turf')) ``` - 参数 diff --git a/examples/echarts/airline.html b/examples/echarts/airline.html index 0cf18ef6..04066245 100644 --- a/examples/echarts/airline.html +++ b/examples/echarts/airline.html @@ -1,409 +1,411 @@ + + + + dc-example + + + + + - - - - dc-example - - - - - + +
- - -
- - - + DC.ready({ + baseUrl: '../libs/dc-sdk/resources/', + }).then(initViewer) + + diff --git a/examples/echarts/logistics.html b/examples/echarts/logistics.html index 56ed70cf..c91678e5 100644 --- a/examples/echarts/logistics.html +++ b/examples/echarts/logistics.html @@ -1,420 +1,422 @@ + + + + dc-example + + + + + - - - - dc-example - - - - - + +
- - -
- - - + DC.ready({ + baseUrl: '../libs/dc-sdk/resources/', + }).then(initViewer) + + diff --git a/examples/echarts/migrate.html b/examples/echarts/migrate.html index c561ad03..46504371 100644 --- a/examples/echarts/migrate.html +++ b/examples/echarts/migrate.html @@ -1,324 +1,333 @@ + + + + dc-example + + + + + - - - - dc-example - - - - - + +
- + - + DC.ready({ + baseUrl: '../libs/dc-sdk/resources/', + }).then(initViewer) + + diff --git a/examples/echarts/plane.html b/examples/echarts/plane.html index 268fc6e4..706a2fe0 100644 --- a/examples/echarts/plane.html +++ b/examples/echarts/plane.html @@ -1,369 +1,370 @@ + + + + dc-example + + + + + - - - - dc-example - - - - - + +
- - -
- - - + DC.ready({ + baseUrl: '../libs/dc-sdk/resources/', + }).then(initViewer) + + diff --git a/examples/echarts/pm.html b/examples/echarts/pm.html index c55d3941..24a78653 100644 --- a/examples/echarts/pm.html +++ b/examples/echarts/pm.html @@ -1,501 +1,506 @@ + + + + dc-example + + + + + - - - - dc-example - - - - - + +
- - -
- - - + return { + series: [ + { + name: 'pm2.5', + type: 'scatter', + coordinateSystem: 'GLMap', + data: convertData(data), + symbolSize: function (val) { + return val[2] / 10 + }, + label: { + normal: { + formatter: '{b}', + position: 'right', + show: false, + }, + emphasis: { + show: true, + }, + }, + itemStyle: { + normal: { + color: '#ddb926', + }, + }, + }, + { + name: 'Top 5', + type: 'effectScatter', + coordinateSystem: 'GLMap', + data: convertData( + data + .sort(function (a, b) { + return b.value - a.value + }) + .slice(0, 6) + ), + symbolSize: function (val) { + return val[2] / 10 + }, + showEffectOn: 'render', + rippleEffect: { + brushType: 'stroke', + }, + hoverAnimation: true, + label: { + normal: { + formatter: '{b}', + position: 'right', + show: true, + }, + }, + itemStyle: { + normal: { + color: '#f4e925', + shadowBlur: 10, + shadowColor: '#333', + }, + }, + zlevel: 1, + }, + ], + } + } + DC.ready({ + baseUrl: '../libs/dc-sdk/resources/', + echarts: window.echarts, + }).then(initViewer) + + diff --git a/examples/echarts/population_mobility.html b/examples/echarts/population_mobility.html index 38206f4d..ea0b0397 100644 --- a/examples/echarts/population_mobility.html +++ b/examples/echarts/population_mobility.html @@ -1,3265 +1,3266 @@ + + + + dc-example + + + + + - - - - dc-example - - - - - + +
- + - + DC.ready({ + baseUrl: '../libs/dc-sdk/resources/', + }).then(initViewer) + + diff --git a/examples/info/custom.html b/examples/info/custom.html index 73a9b6fa..e2b66090 100644 --- a/examples/info/custom.html +++ b/examples/info/custom.html @@ -1,57 +1,51 @@ + + + + dc-example + + + + + + + - - - - dc-example - - - - - - - - - -
- - + +
+ + diff --git a/gulpfile.js b/gulpfile.js index a1988901..f4c425be 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -10,11 +10,11 @@ import gulp from 'gulp' import esbuild from 'esbuild' import concat from 'gulp-concat' import clean from 'gulp-clean' -import javascriptObfuscator from 'gulp-javascript-obfuscator' import startServer from './server.js' import inlineImage from 'esbuild-plugin-inline-image' import { sassPlugin } from 'esbuild-sass-plugin' import { glsl } from 'esbuild-plugin-glsl' +import GlobalsPlugin from 'esbuild-plugin-globals' import shell from 'shelljs' import chalk from 'chalk' @@ -26,19 +26,6 @@ const c_packageJson = fse.readJsonSync( path.join(dc_common_path, 'package.json') ) -const obfuscatorConfig = { - compact: true, //压缩代码 - identifierNamesGenerator: 'hexadecimal', //标识符的混淆方式 hexadecimal(十六进制) mangled(短标识符) - renameGlobals: false, //是否启用全局变量和函数名称的混淆 - rotateStringArray: true, //通过固定和随机(在代码混淆时生成)的位置移动数组。这使得将删除的字符串的顺序与其原始位置相匹配变得更加困难。如果原始源代码不小,建议使用此选项,因为辅助函数可以引起注意。 - selfDefending: true, //混淆后的代码,不能使用代码美化,同时需要配置 compact:true; - stringArray: true, //删除字符串文字并将它们放在一个特殊的数组中 - stringArrayEncoding: ['base64'], - stringArrayThreshold: 0.75, - transformObjectKeys: false, - unicodeEscapeSequence: false, //允许启用/禁用字符串转换为unicode转义序列。Unicode转义序列大大增加了代码大小,并且可以轻松地将字符串恢复为原始视图。建议仅对小型源代码启用此选项。 -} - const buildConfig = { entryPoints: ['src/DC.js'], bundle: true, @@ -57,6 +44,7 @@ const buildConfig = { glsl(), sassPlugin(), ], + external: ['@dvgis/dc-common', 'turf', 'echarts'], } function getTime() { @@ -105,52 +93,36 @@ async function buildModules(options) { .replace('{{__REPOSITORY__}}', packageJson.repository)} }` - const importNamespace = ` - import {Cesium , Supercluster } from '@dvgis/dc-common' - ` - const exportNamespace = ` - export const __namespace = { - Cesium : Cesium, - Supercluster: Supercluster - } - ` - - // Build IIFE - if (options.iife) { - await fse.outputFile( - dcPath, - ` + await fse.outputFile( + dcPath, + ` ${content} ${cmdOutFunction} ${exportVersion} `, - { - encoding: 'utf8', - } - ) + { + encoding: 'utf8', + } + ) + // Build IIFE + if (options.iife) { await esbuild.build({ ...buildConfig, format: 'iife', globalName: 'DC', + plugins: [ + ...buildConfig.plugins, + GlobalsPlugin({ + '@dvgis/dc-common': 'DC_Common', + charts: 'charts', + }), + ], outfile: path.join('dist', 'modules-iife.js'), }) } // Build Node、 if (options.node) { - await fse.outputFile( - dcPath, - ` - ${importNamespace} - ${content} - ${exportVersion} - ${exportNamespace} - ${cmdOutFunction} - `, - { - encoding: 'utf8', - } - ) await esbuild.build({ ...buildConfig, format: 'esm', @@ -158,7 +130,6 @@ async function buildModules(options) { define: { TransformStream: 'null', }, - external: ['@dvgis/dc-common'], outfile: path.join('dist', 'index.js'), }) } @@ -170,42 +141,23 @@ async function buildModules(options) { async function combineJs(options) { // combine for iife if (options.iife) { - if (options.obfuscate) { - await gulp - .src('dist/modules-iife.js') - .pipe(javascriptObfuscator(obfuscatorConfig)) - .pipe(gulp.src(path.join(dc_common_path, 'dist', '__namespace.js'))) - .pipe(concat('dc.min.js')) - .pipe(gulp.dest('dist')) - .on('end', () => { - addCopyright(options) - deleteTempFile() - }) - } else { - await gulp - .src([ - 'dist/modules-iife.js', - path.join(dc_common_path, 'dist', '__namespace.js'), - ]) - .pipe(concat('dc.min.js')) - .pipe(gulp.dest('dist')) - .on('end', () => { - addCopyright(options) - deleteTempFile() - }) - } + await gulp + .src([ + path.join(dc_common_path, 'dist', 'dc.common.min.js'), + 'dist/modules-iife.js', + ]) + .pipe(concat('dc.min.js')) + .pipe(gulp.dest('dist')) + .on('end', () => { + addCopyright(options) + deleteTempFile() + }) } // combine for node if (options.node && options.obfuscate) { await gulp .src('dist/index.js') - .pipe( - javascriptObfuscator({ - ...obfuscatorConfig, - target: 'browser-no-eval', - }) - ) .pipe(gulp.dest('dist')) .on('end', () => { addCopyright(options) @@ -313,9 +265,9 @@ export const build = gulp.series( export const buildRelease = gulp.series( () => buildModules({ iife: true }), - () => combineJs({ iife: true, obfuscate: true }), + () => combineJs({ iife: true }), () => buildModules({ node: true }), - () => combineJs({ node: true, obfuscate: true }), + () => combineJs({ node: true }), () => buildCSS({ minify: true }), copyAssets ) diff --git a/package.json b/package.json index 9df346b5..edc40ce6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dvgis/dc-sdk", - "version": "3.5.0", + "version": "4.0.0", "license": "MIT", "description": "The SDK is based on Cesium for secondary development of 2, 3D all-in-one WebGis application framework, the framework optimizes the use of Cesium and add some additional features, designed for developers to quickly build WebGis applications.", "repository": "https://github.com/dvgis/dc-sdk.git", @@ -37,6 +37,7 @@ "@babel/preset-env": "^7.21.5", "chalk": "^5.2.0", "esbuild": "^0.20.1", + "esbuild-plugin-globals": "^0.2.0", "esbuild-plugin-glsl": "^1.2.1", "esbuild-plugin-inline-image": "^0.0.9", "esbuild-sass-plugin": "^3.1.0", @@ -66,6 +67,7 @@ "dist" ], "dependencies": { - "@dvgis/dc-common": "^3.5.0" + "@dvgis/dc-common": "^3.5.1", + "echarts": "^5.5.1" } } diff --git a/src/global-api/index.js b/src/global-api/index.js new file mode 100644 index 00000000..02c6270a --- /dev/null +++ b/src/global-api/index.js @@ -0,0 +1,6 @@ +/** + * @Author : Caven Chen + */ + +export { registerLib, getLib } from './lib-utils.js' +export { setParam, getParam } from './paramter.js' diff --git a/src/global-api/lib-utils.js b/src/global-api/lib-utils.js index 16f7a5a6..2d9d53ff 100644 --- a/src/global-api/lib-utils.js +++ b/src/global-api/lib-utils.js @@ -11,6 +11,10 @@ const cache = {} */ export function registerLib(name, lib) { cache[name] = lib + if (name === 'echarts' && this.registerEcharts) { + this.registerEcharts(lib) + } + return this } /** diff --git a/src/global-api/paramter.js b/src/global-api/paramter.js new file mode 100644 index 00000000..7464d351 --- /dev/null +++ b/src/global-api/paramter.js @@ -0,0 +1,23 @@ +/** + * @Author : Caven Chen + */ + +const params = {} + +/** + * set param + * @param name + * @param value + */ +export function setParam(name, value) { + params[name] = value +} + +/** + * get param + * @param name + * @return {*} + */ +export function getParam(name) { + return params[name] +} diff --git a/src/index.js b/src/index.js index 444a57bb..5a5a4645 100644 --- a/src/index.js +++ b/src/index.js @@ -1,16 +1,22 @@ /** @Author: Caven Chen **/ -import { getLib, registerLib } from './global-api/lib-utils.js' - -export { registerLib, getLib } from './global-api/lib-utils.js' +import { setParam } from './global-api' const DEF_BASE_URL = './libs/dc-sdk/resources/' - let _baseUrl = DEF_BASE_URL - let __isInitialized = false +export { registerLib, getLib } from './global-api' + +export * from './modules' + +export * from './modules/third-part' + +export { Math } from './modules/math' + +export { registerEcharts, ChartLayer } from './modules/chart' + export const config = { set baseUrl(baseUrl) { _baseUrl = baseUrl @@ -24,62 +30,14 @@ export function ready(options = {}) { if (__isInitialized) { return Promise.resolve() } - __cmdOut && __cmdOut() + // __cmdOut && __cmdOut() if (options['baseUrl']) { this.config.baseUrl = options['baseUrl'] } - - if (options['Cesium']) { - registerLib('Cesium', options['Cesium']) - if (this.config.baseUrl === DEF_BASE_URL) { - this.config.baseUrl = '' - } - } else { - registerLib('Cesium', this['__namespace']['Cesium']) - } - - if (options['echarts']) { - registerLib('echarts', options['echarts']) - } - - if (options['Supercluster']) { - registerLib('Supercluster', options['Supercluster']) - } else { - registerLib('Supercluster', this['__namespace']['Supercluster']) - } - + __isInitialized = true return new Promise((resolve, reject) => { - const Cesium = getLib('Cesium') - if (!Cesium) { - throw new Error('missing Cesium Lib') - } - this.config.baseUrl && Cesium.buildModuleUrl.setBaseUrl(this.config.baseUrl) - - // register common modules - const modules = require('./modules') - Object.keys(modules).forEach((key) => { - this[key] = modules[key] - }) - // register third part modules - const { THIRD_PART } = require('./modules/third-part') - Object.keys(THIRD_PART).forEach((key) => { - this[key] = THIRD_PART[key] - }) - // register math module - if (this['Math']) { - const maths = require('./modules/math') - Object.keys(maths).forEach((key) => { - this['Math'][key] = maths[key] - }) - } - // register chart module - if (getLib('echarts')) { - const modules = require('./modules/chart') - Object.keys(modules).forEach((key) => { - this[key] = modules[key] - }) - } - __isInitialized = true + setParam('isInitialized', true) + setParam('baseUrl', this.config.baseUrl) resolve() }).catch((e) => { throw new Error(e.message) diff --git a/src/modules/chart/ChartLayer.js b/src/modules/chart/ChartLayer.js index 443259b7..a88ad12a 100644 --- a/src/modules/chart/ChartLayer.js +++ b/src/modules/chart/ChartLayer.js @@ -1,12 +1,10 @@ /** * @Author : Caven Chen */ -import { echarts } from '../../namespace' +import { getLib } from '../../global-api/lib-utils.js' import { Layer } from '../layer' import State from '../state/State.js' -const { init } = echarts - class ChartLayer extends Layer { constructor(id, option) { super(id) @@ -40,10 +38,14 @@ class ChartLayer extends Layer { } _onAdd(viewer) { + let echarts = getLib('echarts') + if (!echarts) { + throw new Error('') + } this._viewer = viewer this._viewer.canvas.setAttribute('tabIndex', '0') this._delegate = this._createChartElement() - this._chart = init(this._delegate) + this._chart = echarts.init(this._delegate) Object(this._chart.getZr()).viewer = viewer this._option && this._chart.setOption({ ...this._option, GLMap: {}, animation: false }) diff --git a/src/modules/chart/GLMapCoordSys.js b/src/modules/chart/GLMapCoordSys.js index f0b76af4..f89bf263 100644 --- a/src/modules/chart/GLMapCoordSys.js +++ b/src/modules/chart/GLMapCoordSys.js @@ -2,28 +2,41 @@ * @Author : Caven Chen */ -import { echarts, Cesium } from '../../namespace' +import { Cesium } from '../../namespace' const { Cartesian3, Ellipsoid, Math: CesiumMath } = Cesium -const { graphic, matrix } = echarts -class GLMapCoordSys { - constructor(api) { +export function createGLMapCoordSys(echarts) { + function GLMapCoordSys(api) { this._api = api this._viewer = Object(api.getZr()).viewer this._mapOffset = [0, 0] this.dimensions = ['lng', 'lat'] } - getViewer() { + /** + * + * @returns + */ + GLMapCoordSys.prototype.getViewer = function () { return this._viewer } - setMapOffset(mapOffset) { + /** + * + * @param {*} mapOffset + * @returns + */ + GLMapCoordSys.prototype.setMapOffset = function (mapOffset) { this._mapOffset = mapOffset return this } - dataToPoint(data) { + /** + * + * @param {*} data + * @returns + */ + GLMapCoordSys.prototype.dataToPoint = function (data) { let result = [] let cartesian3 = Cartesian3.fromDegrees(data[0], data[1]) if (!cartesian3) { @@ -41,7 +54,12 @@ class GLMapCoordSys { return [coords.x - this._mapOffset[0], coords.y - this._mapOffset[1]] } - pointToData(point) { + /** + * + * @param {*} point + * @returns + */ + GLMapCoordSys.prototype.pointToData = function (point) { let ellipsoid = this._viewer.scene.globe.ellipsoid let cartesian3 = new Cartesian3( point[0] + this._mapOffset[0], @@ -55,16 +73,34 @@ class GLMapCoordSys { ] } - getViewRect() { + /** + * + * @returns + */ + GLMapCoordSys.prototype.getViewRect = function () { let api = this._api - return new graphic.BoundingRect(0, 0, api.getWidth(), api.getHeight()) + return new echarts.graphic.BoundingRect( + 0, + 0, + api.getWidth(), + api.getHeight() + ) } - getRoamTransform() { - return matrix.create() + /** + * + * @returns + */ + GLMapCoordSys.prototype.getRoamTransform = function () { + return echarts.matrix.create() } - static create(ecModel, api) { + /** + * + * @param {*} ecModel + * @param {*} api + */ + GLMapCoordSys.create = function (ecModel, api) { let coordinateSys = undefined ecModel.eachComponent('GLMap', function (model) { coordinateSys = new GLMapCoordSys(api) @@ -76,8 +112,7 @@ class GLMapCoordSys { (model.coordinateSystem = coordinateSys) }) } -} -GLMapCoordSys.dimensions = ['lng', 'lat'] - -export default GLMapCoordSys + GLMapCoordSys.dimensions = ['lng', 'lat'] + return GLMapCoordSys +} diff --git a/src/modules/chart/GLMapModel.js b/src/modules/chart/GLMapModel.js deleted file mode 100644 index 00c8a3eb..00000000 --- a/src/modules/chart/GLMapModel.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @Author : Caven Chen - */ - -import { echarts } from '../../namespace' - -const { extendComponentModel } = echarts - -extendComponentModel({ - type: 'GLMap', - getViewer() { - return Object(this.getZr()).viewer - }, - defaultOption: { - roam: false, - }, -}) diff --git a/src/modules/chart/GLMapView.js b/src/modules/chart/GLMapView.js deleted file mode 100644 index df8afc3e..00000000 --- a/src/modules/chart/GLMapView.js +++ /dev/null @@ -1,30 +0,0 @@ -/** - * @Author : Caven Chen - */ - -import { echarts } from '../../namespace' - -const { extendComponentView } = echarts - -extendComponentView({ - type: 'GLMap', - init: function (ecModel, api) { - this.api = api - let viewer = api.getZr().viewer - viewer.clock.onTick.addEventListener(this.moveHandler, this) - }, - moveHandler: function () { - if (this.api.getZr().dom.style.visibility !== 'hidden') { - this.api.dispatchAction({ - type: 'GLMapRoam', - }) - } - }, - - render: function (t, e, i) {}, - - dispose: function () { - let viewer = this.api.getZr().viewer - viewer.clock.onTick.removeEventListener(this.moveHandler, this) - }, -}) diff --git a/src/modules/chart/index.js b/src/modules/chart/index.js index 0690867d..7abce920 100644 --- a/src/modules/chart/index.js +++ b/src/modules/chart/index.js @@ -2,23 +2,61 @@ * @Author : Caven Chen */ -import { echarts } from '../../namespace' - -import './GLMapModel' -import './GLMapView' -import GLMapCoordSys from './GLMapCoordSys' import ChartLayer from './ChartLayer.js' -const { registerAction, registerCoordinateSystem } = echarts - -registerCoordinateSystem('GLMap', GLMapCoordSys) -registerAction( - { - type: 'GLMapRoam', - event: 'GLMapRoam', - update: 'updateLayout', - }, - function (payload, ecModel) {} -) +import { createGLMapCoordSys } from './GLMapCoordSys.js' + +export function registerEcharts(echarts) { + const { + registerAction, + registerCoordinateSystem, + extendComponentModel, + extendComponentView, + } = echarts + + extendComponentModel({ + type: 'GLMap', + getViewer() { + return Object(this.getZr()).viewer + }, + defaultOption: { + roam: false, + }, + }) + + extendComponentView({ + type: 'GLMap', + init: function (ecModel, api) { + this.api = api + let viewer = api.getZr().viewer + viewer.clock.onTick.addEventListener(this.moveHandler, this) + }, + moveHandler: function () { + if (this.api.getZr().dom.style.visibility !== 'hidden') { + this.api.dispatchAction({ + type: 'GLMapRoam', + }) + } + }, + + render: function (t, e, i) {}, + + dispose: function () { + let viewer = this.api.getZr().viewer + viewer.clock.onTick.removeEventListener(this.moveHandler, this) + }, + }) + + registerCoordinateSystem('GLMap', createGLMapCoordSys(echarts)) + + registerAction( + { + type: 'GLMapRoam', + event: 'GLMapRoam', + update: 'updateLayout', + }, + function (payload, ecModel) {} + ) +} export { ChartLayer } diff --git a/src/modules/math/index.js b/src/modules/math/index.js index 665befc2..ff527ac9 100644 --- a/src/modules/math/index.js +++ b/src/modules/math/index.js @@ -2,13 +2,47 @@ * @Author : Caven Chen */ -export { default as area } from './area' -export { default as bounds } from './bounds' -export { default as center } from './center' -export { default as curve } from './curve' -export { default as distance } from './distance' -export { default as heading } from './heading' -export { default as isBetween } from './isBetween' -export { default as midCartesian } from './midCartesian' -export { default as midPosition } from './midPosition' -export { default as parabola } from './parabola' +import { Cesium } from '../../namespace' +import { Util } from '../utils' + +import area from './area' +import bounds from './bounds' +import center from './center' +import curve from './curve' +import distance from './distance' +import heading from './heading' +import isBetween from './isBetween' +import midCartesian from './midCartesian' +import midPosition from './midPosition' +import parabola from './parabola' + +const { Math: CesiumMath } = Cesium + +let Math = {} + +Util.merge(Math, CesiumMath, { + area, + bounds, + center, + curve, + distance, + heading, + isBetween, + midCartesian, + midPosition, + parabola, +}) + +export { + area, + bounds, + center, + curve, + distance, + heading, + isBetween, + midCartesian, + midPosition, + parabola, + Math, +} diff --git a/src/modules/third-part/index.js b/src/modules/third-part/index.js index 1ac0023e..72e55e8f 100644 --- a/src/modules/third-part/index.js +++ b/src/modules/third-part/index.js @@ -1,49 +1,48 @@ /** - * @Author : Caven Chen + * @Author: Caven Chen */ import { Cesium } from '../../namespace' -export const THIRD_PART = { - Cartesian2: Cesium.Cartesian2, - Cartesian3: Cesium.Cartesian3, - Matrix3: Cesium.Matrix3, - Matrix4: Cesium.Matrix4, - Material: Cesium.Material, - SceneMode: Cesium.SceneMode, - SkyBox: Cesium.SkyBox, - Color: Cesium.Color, - Rectangle: Cesium.Rectangle, - ShadowMode: Cesium.ShadowMode, - JulianDate: Cesium.JulianDate, - HeightReference: Cesium.HeightReference, - HorizontalOrigin: Cesium.HorizontalOrigin, - VerticalOrigin: Cesium.VerticalOrigin, - CallbackProperty: Cesium.CallbackProperty, - ClassificationType: Cesium.ClassificationType, - TilesetStyle: Cesium.Cesium3DTileStyle, - GeographicTilingScheme: Cesium.GeographicTilingScheme, - WebMercatorTilingScheme: Cesium.WebMercatorTilingScheme, - ColorMaterialProperty: Cesium.ColorMaterialProperty, - ImageMaterialProperty: Cesium.ImageMaterialProperty, - PolylineDashMaterialProperty: Cesium.PolylineDashMaterialProperty, - PolylineGlowMaterialProperty: Cesium.PolylineGlowMaterialProperty, - PolylineOutlineMaterialProperty: Cesium.PolylineOutlineMaterialProperty, - PolylineArrowMaterialProperty: Cesium.PolylineArrowMaterialProperty, - writeTextToCanvas: Cesium.writeTextToCanvas, - CustomShaderMode: Cesium.CustomShaderMode, - CustomShaderTranslucencyMode: Cesium.CustomShaderTranslucencyMode, - CustomShader: Cesium.CustomShader, - UniformType: Cesium.UniformType, - Math: Cesium.Math, - Ellipsoid: Cesium.Ellipsoid, - Resource: Cesium.Resource, - createGooglePhotorealistic3DTileset: - Cesium.createGooglePhotorealistic3DTileset, - createElevationBandMaterial: Cesium.createElevationBandMaterial, - ModelAnimationLoop: Cesium.ModelAnimationLoop, - ClippingPlane: Cesium.ClippingPlane, - ClippingPlaneCollection: Cesium.ClippingPlaneCollection, - ClippingPolygon: Cesium.ClippingPolygon, - ClippingPolygonCollection: Cesium.ClippingPolygonCollection, -} +export const Cartesian2 = Cesium.Cartesian2 +export const Cartesian3 = Cesium.Cartesian3 +export const Matrix3 = Cesium.Matrix3 +export const Matrix4 = Cesium.Matrix4 +export const Material = Cesium.Material +export const SceneMode = Cesium.SceneMode +export const SkyBox = Cesium.SkyBox +export const Color = Cesium.Color +export const Rectangle = Cesium.Rectangle +export const ShadowMode = Cesium.ShadowMode +export const JulianDate = Cesium.JulianDate +export const HeightReference = Cesium.HeightReference +export const HorizontalOrigin = Cesium.HorizontalOrigin +export const VerticalOrigin = Cesium.VerticalOrigin +export const CallbackProperty = Cesium.CallbackProperty +export const ClassificationType = Cesium.ClassificationType +export const TilesetStyle = Cesium.Cesium3DTileStyle +export const GeographicTilingScheme = Cesium.GeographicTilingScheme +export const ebMercatorTilingScheme = Cesium.WebMercatorTilingScheme +export const ColorMaterialProperty = Cesium.ColorMaterialProperty +export const ImageMaterialProperty = Cesium.ImageMaterialProperty +export const PolylineDashMaterialProperty = Cesium.PolylineDashMaterialProperty +export const PolylineGlowMaterialProperty = Cesium.PolylineGlowMaterialProperty +export const PolylineOutlineMaterialProperty = + Cesium.PolylineOutlineMaterialProperty +export const PolylineArrowMaterialProperty = + Cesium.PolylineArrowMaterialProperty +export const writeTextToCanvas = Cesium.writeTextToCanvas +export const CustomShaderMode = Cesium.CustomShaderMode +export const CustomShaderTranslucencyMode = Cesium.CustomShaderTranslucencyMode +export const CustomShader = Cesium.CustomShader +export const UniformType = Cesium.UniformType +export const Ellipsoid = Cesium.Ellipsoid +export const Resource = Cesium.Resource +export const createGooglePhotorealistic3DTileset = + Cesium.createGooglePhotorealistic3DTileset +export const createElevationBandMaterial = Cesium.createElevationBandMaterial +export const ModelAnimationLoop = Cesium.ModelAnimationLoop +export const ClippingPlane = Cesium.ClippingPlane +export const ClippingPlaneCollection = Cesium.ClippingPlaneCollection +export const ClippingPolygon = Cesium.ClippingPolygon +export const ClippingPolygonCollection = Cesium.ClippingPolygonCollection diff --git a/src/modules/viewer/Viewer.js b/src/modules/viewer/Viewer.js index fd24e4ae..ce0521bd 100644 --- a/src/modules/viewer/Viewer.js +++ b/src/modules/viewer/Viewer.js @@ -4,6 +4,7 @@ */ import { Cesium } from '../../namespace' +import { getParam } from '../../global-api' import Parse from '../parse/Parse' import { LayerGroupEventType, @@ -25,19 +26,32 @@ const DEF_OPTS = { } class Viewer { - constructor(id, options = {}) { - if (!id || (typeof id === 'string' && !document.getElementById(id))) { - throw new Error('Viewer:the id is empty') + constructor(container, options = {}) { + if (!getParam('isInitialized')) { + throw new Error('please do the ready function') } - this._delegate = Cesium.Viewer - ? new Cesium.Viewer(id, { - ...DEF_OPTS, - ...options, - }) - : new CesiumViewer(id, { - ...DEF_OPTS, - ...options, - }) // Initialize the viewer + if ( + !container || + (typeof container === 'string' && !document.getElementById(container)) + ) { + throw new Error('Viewer: the container is empty') + } + if (container instanceof HTMLElement) { + throw new Error('Viewer: not support the type container') + } + + if (typeof container === 'string') { + const baseUrl = getParam('baseUrl') + baseUrl && Cesium.buildModuleUrl.setBaseUrl(baseUrl) // Initialize the Cesium_BASE_URL + } + + this._delegate = + typeof container !== 'string' + ? container + : new CesiumViewer(container, { + ...DEF_OPTS, + ...options, + }) // Initialize the viewer /** * Registers events @@ -52,13 +66,17 @@ class Viewer { this._widgetContainer = DomUtil.create( 'div', 'viewer-widgets', - typeof id === 'string' ? document.getElementById(id) : id + typeof container === 'string' + ? document.getElementById(container) + : this._delegate.container ) //Register the widgets container this._layerContainer = DomUtil.create( 'div', 'viewer-layers', - typeof id === 'string' ? document.getElementById(id) : id + typeof container === 'string' + ? document.getElementById(container) + : this._delegate.container ) //Register the layers container this._baseLayerPicker = new BaseLayerPicker({ diff --git a/src/namespace/index.js b/src/namespace/index.js index 95eab516..ac4de34d 100644 --- a/src/namespace/index.js +++ b/src/namespace/index.js @@ -2,12 +2,12 @@ * @Author : Caven Chen */ -import { getLib } from '../global-api/lib-utils.js' +import { registerLib } from '../global-api' -export const Cesium = getLib('Cesium') +import { Cesium, Supercluster } from '@dvgis/dc-common' -export const turf = getLib('turf') +registerLib('Cesium', Cesium) -export const echarts = getLib('echarts') +registerLib('Supercluster', Supercluster) -export const Supercluster = getLib('Supercluster') +export { Cesium, Supercluster }