This commit is contained in:
djerom
2022-03-17 10:36:15 +05:00
parent b0cb3e60d6
commit 5fc055a05d
28 changed files with 3007 additions and 17 deletions

237
src/App.svelte Normal file
View File

@@ -0,0 +1,237 @@
<script>
import { onMount } from "svelte";
import axios from "axios";
import { toasts, ToastContainer, FlatToast } from "svelte-toasts";
// import file_lib from './libs/file'
import SvgNodes from "./svgmap/SvgNodes";
import SvgMapUI from "./svgmap/SvgMapUI";
import file_lib from "./libs/file";
import Modal from "./components/Modal.svelte";
import MapApp from './services/map'
import get_bbox from './libs/bbox'
const back_url = "http://localhost:4000";
let cur_map = null;
import styles from './moc/styles.json'
let map_host;
let map;
onMount(() => {
map = new SvgMapUI(map_host);
});
let WellsRenderer = {
// [{x,y}]
heads(wells, r, style) {
return wells.map((w) => SvgNodes.circle(r).set_style(style).move(w.x, w.y));
},
// [{x,y,name}]
names(wells, style, shift) {
return wells.map((w) =>
SvgNodes.text(w.name)
.set_style(style)
.move(w.x + shift.x, w.y + shift.y)
);
},
// {x,y,ring[1,2,3,4,5]}
ring(x, y, r0, r1, a0, a1, style) {
return SvgNodes.ring_sector(r0, r1, a0, a1).move(x, y).set_style(style);
},
};
function save() {
file_lib.download("map.svg", cur_map);
// console.log(cur_map);
}
async function upload_file_wells() {
const form = `Выберите файл скважин для загрузки<br>
<input type="file" name="file"/>`; // multiple
let res = await ui.modal.show(form, ["ok", "cancel"]);
console.log(res);
// console.log(res.form['some'].value)
let file = await file_lib.toBase64(res.form["file"]);
let ans = await axios.post(`${back_url}/import/excel`, { name: "wells", file });
console.log(ans);
toasts.add({
description: "File loaded",
type: 'success',
});
// ui.modal.show(`Hallo ${res.form['some'].value}`, ['ok'])
}
async function upload_file_mer() {
const form = `Выберите файл мэр для загрузки<br>
<input type="file" name="file"/>`; // multiple
let res = await ui.modal.show(form, ["ok", "cancel"]);
console.log(res);
// console.log(res.form['some'].value)
let file = await file_lib.toBase64(res.form["file"]);
let ans = await axios.post(`${back_url}/import/excel`, { name: "production_injection", file });
console.log(ans);
// ui.modal.show(`Hallo ${res.form['some'].value}`, ['ok'])
toasts.add({
description: "File loaded",
type: 'success',
});
}
let ui = {
modal: {},
};
// ui.modal.show('<div>Выберите файл для загрузки в базу<br><ui-filedialog name="file1"></ui-filedialog></div>', ['ok', 'cancel'])
// ui.modal.hide()
// file_lib.download()
/**
*
* @param r0
* @param r1
* @param a0
* @param a1
* @param { [{v, style}] } rings
*/
function sectored_ring(r0, r1, rings){
let sum = rings.reduce((s,c) => s + c.v, 0)
let angs = rings.reduce((s, c, i) => [...s, {...c, a0: i && s[i - 1].a1, a1: c.v + (i && s[i - 1].a1)}], [])
let items = angs.map(x => SvgNodes.ring_sector(r0, r1, x.a0 * 360 / sum, x.a1 * 360 / sum).set_style(x.style))
return SvgNodes.group(items)
}
async function build_map() {
const query = "SELECT * FROM wells";
let wells = await axios.post(`${back_url}/proxy/sqlite`, { query }).then((x) => x.data.data);
const mapscale = 1 / 1;
const scale = (w) => w * mapscale;
const whr = 10;
const shift = { x: 10, y: -10 };
// map = new map()
let bbox = get_bbox(wells.map(w => ({x: w.whx, y: w.why})))
console.log(bbox);
let scaled_wells = wells.map((x) => ({ ...x, whx: scale(x.whx - bbox.l), why: scale(x.why - bbox.t) }));
let r0 = 0;
let r1 = 30;
let a0 = 0;
let a1 = 130;
let layer_gpt = scaled_wells.map((w) =>
SvgNodes.ring_sector(r0, r1, a0, a1).move(w.whx, w.why).set_style(styles.gpt)
);
let layer_wells = scaled_wells.map((w) =>
SvgNodes.circle(whr).set_attrs({ cx: w.whx, cy: w.why }).set_style(styles.wellhead)
);
let layer_wellnames = scaled_wells.map((w) =>
SvgNodes.text(w.well)
.set_style(styles.style_wellhead)
.move(w.whx + shift.x, w.why + shift.y)
);
let layer_rings = scaled_wells.map((w) =>
sectored_ring(0, 50, [{v: 10, style: styles.gpt}, {v: 20, style: styles.opt}, {v: 30, style: styles.wopt}]).move(w.whx, w.why)
);
// let layer_wells_wpr = wells.map((w) => svg_sector(70, 0, 70).set_style(style_wir).move(w.whx, w.why).render());
// let layer_wells_gpr = wells.map((w) =>
// svg_sector(70, 70, 130).set_style(style_gpr).move(w.whx, w.why).render()
// );
// let layer_wells_opr = wells.map((w) =>
// svg_sector(70, 130, 360).set_style(style_opr).move(w.whx, w.why).render()
// );
// let layer_well_names = wells.map((w) =>
// svg_text(w.well)
// .set_style(style_wellhead_text)
// .move(w.whx + 10, w.why - 10)
// .render()
// );
// let layers = [...layer_wells, ...layer_wells_wpr, ...layer_well_names]
// let layers = layer_wells_wpr
let svg = SvgNodes.svg(); //.set_attr("viewBox", `${bbox.l} ${bbox.t} ${bbox.r - bbox.l} ${bbox.b - bbox.t}`);
console.log(svg);
// svg.append(layer_gpt);
svg.append(layer_rings)
svg.append(layer_wells);
svg.append(layer_wellnames);
cur_map = svg.render();
map.set(cur_map);
}
function func() {
toasts.add({
title: "Message title",
description: "Message body",
});
}
</script>
<main>
<Modal bind:this={ui.modal} />
<ToastContainer {toasts} let:data>
<FlatToast {data} />
</ToastContainer>
<button on:click={() => upload_file_wells()}>Upload file Wells</button>
<button on:click={() => upload_file_mer()}>Upload file mer(prod_inj)</button>
<button on:click={() => build_map()}>Build map</button>
<button on:click={() => save()}>Download</button>
<button on:click={() => func()}>Func</button>
<div bind:this={map_host} style="border: 1px solid #f00;" />
</main>
<style>
main {
text-align: center;
padding: 1em;
max-width: 240px;
margin: 0 auto;
}
h1 {
color: #ff3e00;
text-transform: uppercase;
font-size: 4em;
font-weight: 100;
}
@media (min-width: 640px) {
main {
max-width: none;
}
}
</style>

View File

@@ -0,0 +1,50 @@
<script>
import { onMount } from "svelte";
// export function init() {
// document.body.appendChild(_dialog);
// }
export function show(html, buttons) {
_html = html
_visible = true
_buttons = buttons
return new Promise(resolve => _resolve = resolve)
}
export function hide(code) {
_visible = false
_resolve({code, form: _form})
}
let _dialog, _form, _html, _visible = false, _buttons = [], _resolve
onMount(() => {
document.body.appendChild(_dialog);
})
</script>
<!-- <div class="modal" tabindex="-1" role="dialog" bind:this={dialog}> -->
<div class="modal fade show" style="display: {_visible ? 'block' : 'none'};" tabindex="-1" role="dialog" bind:this={_dialog}> -->
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close" on:click={() => hide()}>
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<form bind:this={_form}>
{@html _html}
</form>
</div>
<div class="modal-footer">
{#each _buttons as b}
<button type="button" class="btn btn-primary" on:click={hide(b)}>{b}</button>
{/each}
</div>
</div>
</div>
</div>

View File

@@ -0,0 +1,8 @@
<script>
import { onMount } from "svelte";
</script>
<div>
</div>

View File

11
src/libs/bbox.js Normal file
View File

@@ -0,0 +1,11 @@
module.exports = function (data) {
return data.reduce(
(s, c) => ({
l: Math.min(s.l, c.x),
t: Math.min(s.t, c.y),
r: Math.max(s.r, c.x),
b: Math.max(s.b, c.y),
}),
{ l: data[0].x, t: data[0].y, r: data[0].x, b: data[0].y }
);
};

56
src/libs/file.js Normal file
View File

@@ -0,0 +1,56 @@
export default {
/**
* Downloads file
* @param {*} filename
* @param {*} data
*/
download(filename, data) {
var element = document.createElement("a");
element.setAttribute("href", "data:text/plain;charset=utf-8," + encodeURIComponent(data));
element.setAttribute("download", filename);
element.style.display = "none";
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
},
/**
* Convert data of file selected in input to base64 string
* @param {*} input
*/
async toBase64(input) {
console.log(input)
return new Promise((resolve) => {
if (input.files.length == 0){
resolve(null)
}
else if (input.files.length == 1){
this.fileToBase64(input.files[0]).then(resolve)
}
else
Promise.all(Array(...input.files).map(this.fileToBase64)).then(resolve)
}
);
},
/**
* Convert data of file selected in input to base64 string
* @param {*} input
*/
async fileToBase64(file) {
return new Promise((resolve, reject) => {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
resolve(reader.result)
};
reader.onerror = function (error) {
reject(error)
};
});
}
};

10
src/main.js Normal file
View File

@@ -0,0 +1,10 @@
import App from './App.svelte';
const app = new App({
target: document.body,
props: {
name: 'world'
}
});
export default app;

22
src/moc/styles.json Normal file
View File

@@ -0,0 +1,22 @@
{
"wellhead": {
"stroke-width": 1,
"stroke": "#000",
"fill": "#fff"
},
"gpt": {
"stroke-width": 0,
"stroke": "#ff0",
"fill": "#ff0"
},
"opt": {
"stroke-width": 1,
"stroke": "#f00",
"fill": "#00f"
},
"wpt": {
"stroke-width": 1,
"stroke": "#f00",
"fill": "#00f"
}
}

26
src/services/map.js Normal file
View File

@@ -0,0 +1,26 @@
module.exports = class {
set_scale(scale){
this.scale = scale
}
get_bbox(data){
return data.reduce(
(s, c) => ({
l: Math.min(s.l, c.x),
t: Math.min(s.t, c.y),
r: Math.max(s.r, c.x),
b: Math.max(s.b, c.y),
}),
{ l: data[0].x, t: data[0].y, r: data[0].x, b: data[0].y }
)
}
wells_layer(wells){
}
render(){
}
}

9
src/services/wells.js Normal file
View File

@@ -0,0 +1,9 @@
module.exports = class {
constructor(url){
this.url = url
}
get_wells(){
}
}

87
src/svgmap/SvgMapUI.js Normal file
View File

@@ -0,0 +1,87 @@
export default class SvgMapUI {
constructor(host) {
this.host = host
this.scale = 1
this.pos = { x: 0, y: 0 }
console.log(host)
this.host.onwheel = this.handle_wheel.bind(this)
this.host.onmousedown = this.handle_mousedown.bind(this)
this.host.onmousemove = this.handle_mousemove.bind(this)
this.host.onmouseup = this.handle_mouseup.bind(this)
this.is_down = false
this.set_scale(1)
}
set(html){
this.host.innerHTML = html
}
handle_mousedown(event) {
this.is_down = true
this.downin = { x: event.clientX, y: event.clientY }
console.log(event)
// clientX: 451, clientY: 297
// layerX: 451, layerY: 297
}
handle_mousemove(event) {
if (this.is_down) {
event.preventDefault()
let dx = event.clientX - this.downin.x
let dy = event.clientY - this.downin.y
this.pos.x -= dx * this.scale
this.pos.y -= dy * this.scale
this.set_scale(this.scale)
this.downin = { x: event.clientX, y: event.clientY }
// this.downin = {x: event.clientX, y: event.clientY}
}
// console.log(event.x, event.clientX + this.pos.x, this.scale)
}
handle_mouseup(event) {
this.is_down = false
}
handle_wheel(event) {
event.preventDefault()
if (event.ctrlKey) {
if (event.deltaY > 0) {
this.scale_up()
}
else
this.scale_down()
}
else if (event.shiftKey) {
console.log('shifted')
}
else {
}
console.log(event)
}
set_scale(scale) {
this.scale = scale
if (!this.host.childNodes.length) return;
this.host.childNodes[0].setAttribute("viewBox", `${this.pos.x} ${this.pos.y} ${2000 * this.scale} ${2000 * this.scale}`)
// this.host.childNodes[0].setAttribute("width", `1000`)
// this.host.childNodes[0].setAttribute("height", `1000`)
}
scale_up() {
this.set_scale(this.scale * 1.1)
}
scale_down() {
this.set_scale(this.scale * 0.9)
}
}

61
src/svgmap/SvgNode.js Normal file
View File

@@ -0,0 +1,61 @@
export default class SvgNode {
constructor(tag, attrs, items) {
this.tag = tag
this.attrs = attrs || {}
this.items = items || []
}
append(items){
this.items.push(items)
return this
}
set_attr(attr, value) {
this.attrs[attr] = value
return this
}
set_attrs(attrs) {
this.attrs = { ...this.attrs, ...attrs }
return this
}
set_style(style) {
this.style = { ...this.style || {}, ...style }
return this
}
clear_style(style) {
this.style = null
return this
}
move(x, y) {
this.transform = { x, y }
return this
}
get(attr) {
}
_render_node(node) {
// console.log(typeof node)
// if (typeof node == 'undefined') return ''
if (typeof node == 'string') return node
else if (typeof node == 'object' && node.tag) return node.render()
else if (Array.isArray(node)) return node.map(this._render_node).join('')
else return ''
}
render() {
let attrs = Object.keys(this.attrs).map(x => ` ${x}="${this.attrs[x]}"`).join('')
// console.log(this.style ? Object.keys(this.style).map(x => `${x}:${this.style[x]}`) : '')
let style = this.style ? ' style="' + Object.keys(this.style).map(x => `${x}:${this.style[x]}`).join('; ') + '"' : ''
let transfrom = this.transform ? ` transform="translate(${this.transform.x},${this.transform.y})"` : ''
let items = this.items ? this.items.map(x => this._render_node(x)).join('') : ''
// console.log('this.items', this.items, items)
return `<${this.tag}${attrs}${style}${transfrom}>${items}</${this.tag}>`
}
}

71
src/svgmap/SvgNodes.js Normal file
View File

@@ -0,0 +1,71 @@
import SvgNode from "./SvgNode";
export default {
// sector(r, a0, a1) {
// const k = Math.PI / 180;
// let s0 = -Math.sin(a0 * k) * r;
// let c0 = Math.cos(a0 * k) * r;
// let s1 = -Math.sin(a1 * k) * r;
// let c1 = Math.cos(a1 * k) * r;
// var aa = a1 - a0 > 180 ? 1 : 0;
// // var p = new TimalSvg.Data.SvgPath(string.Format("M0 0 L{0} 0 A {0} {0} 0 {3} 0 {1} {2} z", FN(r), ca, sa, aa));
// // p.Transform = string.Format("rotate({0})", FN(ang / 2));
// return `<path d="M0 0 L${c0} ${s0} A ${r} ${r} 0 ${aa} 0 ${c1} ${s1} z" stroke-width="3" stroke="#f00" fill="#0f0"/>`;
// },
svg(){
return new SvgNode('svg', {
version: "1.1",
xmlns: "http://www.w3.org/2000/svg"
// viewBox: "0 0 2000 2000"
})
},
group(items){
return new SvgNode("g", null, items)
},
circle(r) {
return new SvgNode("circle", {r})
},
sector(r, a0, a1) {
const k = Math.PI / 180;
let s0 = -Math.sin(a0 * k) * r;
let c0 = Math.cos(a0 * k) * r;
let s1 = -Math.sin(a1 * k) * r;
let c1 = Math.cos(a1 * k) * r;
var aa = a1 - a0 > 180 ? 1 : 0;
// var p = new TimalSvg.Data.SvgPath(string.Format("M0 0 L{0} 0 A {0} {0} 0 {3} 0 {1} {2} z", FN(r), ca, sa, aa));
// p.Transform = string.Format("rotate({0})", FN(ang / 2));
return new SvgNode("path", { d: `M0 0 L${c0} ${s0} A ${r} ${r} 0 ${aa} 0 ${c1} ${s1} z` });
},
ring_sector(r0, r1, a0, a1) {
const k = Math.PI / 180;
let s0 = -Math.sin(a0 * k);
let c0 = Math.cos(a0 * k);
let s1 = -Math.sin(a1 * k);
let c1 = Math.cos(a1 * k);
var aa = a1 - a0 > 180 ? 1 : 0;
return new SvgNode("path", {
d: `M${c0 * r0} ${s0 * r0}
A ${r0} ${r0} 0 ${aa} 0 ${c1 * r0} ${s1 * r0}
L${c1 * r1} ${s1 * r1}
A ${r1} ${r1} 0 ${aa} 1 ${c0 * r1} ${s0 * r1} z`,
});
},
text(text) {
let node = new SvgNode("text");
node.items = [text];
node.set_attrs({x: 0, y: 0})
return node;
},
};

0
src/svgmap/index.js Normal file
View File