mcfxkh-Web/src/models/runtime/index.js

135 lines
3.0 KiB
JavaScript

import config from "../../config";
import { latestWarnResp } from "../_/warnresp";
let popupId = 1;
function initState() {
return {
logoDisplaying: window.location.hostname !== 'localhost',
infoDlg: undefined,
cameraTarget: undefined,
featureTip: undefined,
featurePops: [],
layerSetting: {
},
yyObj:{},
markers: {}, // type -> [{ id, lgtd, lttd, elev }]
warnresp: {},
warnrespTick: 1,
}
}
const runtime = {
state: initState(),
reducers: {
setInfoDlg(state, props) {
return { ...state, infoDlg: props }
},
setYyfa(state, props) {
return { ...state, yyObj: props }
},
setLogoDisplaying(state, val) {
return { ...state, logoDisplaying: val }
},
// 地图飞行定位, center [lgtd, lttd] | bound [[x1, y1], [x2, y2]]
setCameraTarget(state, o) {
return { ...state, cameraTarget: o }
},
setHome(state, o) {
return {
...state,
cameraTarget: {
bounds: config.initalExtent,
pitch: config.homePitch,
bearing: 0,
},
}
},
setFeatureTip(state, o) {
return { ...state, featureTip: o }
},
// component, coordinates
setFeaturePop(state, o) {
if (Array.isArray(o)) {
console.log('not impl');
return state;
}
if (!o || !o.type || !o.properties || !o.coordinates) {
return { ...state, featurePops: [] }
} else {
return { ...state, featurePops: [{ ...o, id: popupId++ }] }
}
},
setLayerSetting(state, so) {
const s = { ...state };
s.layerSetting = {
...s.layerSetting,
...so,
};
for (const key in s.layerSetting) {
if (s.layerSetting[key] === undefined) {
delete s.layerSetting[key];
}
}
return s;
},
setMarkers(state, so) {
const s = { ...state };
s.markers = {
...s.markers,
...so,
};
return s;
},
setWarnResp(state, val) {
return {
...state,
warnresp: val,
}
},
addWarnRespTick(state) {
return {
...state,
warnrespTick: state.warnrespTick + 1,
}
},
reset() {
return initState();
},
},
effects: dispatch => ({
// 增加contour的索引
addContourIndex({ startIndex }, state) {
const index = state.runtime.layerSetting?.contour?.index;
const shps = state.runtime.layerSetting?.contour?.shps;
if (typeof index === 'number' && Array.isArray(shps)) {
const contour = { ...state.runtime.layerSetting.contour };
contour.index = index + 1;
if (contour.index >= shps.length) {
contour.index = startIndex || 0;
}
this.setLayerSetting({ contour });
}
},
async loadWarnResp() {
let datas = await latestWarnResp();
if (Array.isArray(datas) && datas.length > 0) {
this.setWarnResp(datas[0]);
} else {
this.setWarnResp({});
}
}
})
};
export default runtime;