@canterbury-air-patrol/sar-search-runner
Version:
269 lines (268 loc) • 9.81 kB
JavaScript
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// sar-search-runner.tsx
var sar_search_runner_exports = {};
__export(sar_search_runner_exports, {
SearchRunner: () => SearchRunner,
SearchRunnerConfiguration: () => SearchRunnerConfiguration
});
module.exports = __toCommonJS(sar_search_runner_exports);
var import_react = __toESM(require("react"));
var import_sar_search_patterns = require("@canterbury-air-patrol/sar-search-patterns");
var import_speed_time_distance = require("@canterbury-air-patrol/speed-time-distance");
var import_react_bootstrap = require("react-bootstrap");
var SearchTimer = ({
runTime,
run,
complete
}) => {
const [remainingTime, setRemainingTime] = (0, import_react.useState)(runTime);
const [running, setRunning] = (0, import_react.useState)(run);
const updateRemainingTime = () => {
if (running) {
if (remainingTime <= 1) {
setRunning(false);
if (complete !== void 0) {
complete();
}
}
setRemainingTime(remainingTime - 1);
}
};
(0, import_react.useEffect)(() => {
const interval = setInterval(() => {
updateRemainingTime();
}, 1e3);
return () => clearInterval(interval);
}, [remainingTime]);
return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, "Turn in: ", remainingTime, " seconds");
};
var SearchRunner = class extends import_react.default.Component {
constructor(props) {
super(props);
this.onChangeSpeed = this.onChangeSpeed.bind(this);
this.updateState = this.updateState.bind(this);
this.onCompleteTimer = this.onCompleteTimer.bind(this);
this.handleReset = this.handleReset.bind(this);
this.handlePause = this.handlePause.bind(this);
this.handleRun = this.handleRun.bind(this);
this.handleBackLeg = this.handleBackLeg.bind(this);
this.handleSkipLeg = this.handleSkipLeg.bind(this);
this.state = {
search: this.props.search === void 0 ? new import_sar_search_patterns.CreepingLineAheadSearch(200, 1e3, 5, 0) : this.props.search,
running: false,
searchLeg: 1,
speed: new import_speed_time_distance.Speed(40, "knots")
};
}
onChangeSpeed(newSpeed) {
this.setState({
speed: newSpeed
});
}
updateState() {
if (this.state.search.complete && this.props.complete) {
this.props.complete();
}
}
onCompleteTimer() {
this.setState(function(oldState) {
oldState.search.nextLeg();
return {
search: oldState.search,
searchLeg: oldState.search.currentLeg
};
}, this.updateState);
}
handleReset() {
this.setState(function(oldState) {
oldState.search.currentLeg = 1;
return {
search: oldState.search,
searchLeg: 1,
running: false
};
});
}
handlePause() {
this.setState({
running: false
});
}
handleRun() {
this.setState({
running: true
});
}
handleBackLeg() {
this.setState(function(oldState) {
oldState.search.currentLeg--;
return {
search: oldState.search,
searchLeg: oldState.search.currentLeg
};
});
}
handleSkipLeg() {
this.setState(function(oldState) {
oldState.search.currentLeg++;
return {
search: oldState.search,
searchLeg: oldState.search.currentLeg
};
});
}
humanBearing(bearing) {
if (bearing < 10) {
return `00${bearing}`;
}
if (bearing < 100) {
return `0${bearing}`;
}
return `${bearing}`;
}
render() {
const distance = new import_speed_time_distance.Distance(
this.state.search.complete ? this.state.search.sweepWidth : this.state.search.leg.distance,
"m"
);
const time = new import_speed_time_distance.Time(
distance.getDistance("m") / this.state.speed.getSpeed("m/s"),
"seconds"
);
const buttons = [];
if (this.state.search.complete || this.state.searchLeg > 1) {
buttons.push(
/* @__PURE__ */ import_react.default.createElement(import_react_bootstrap.Button, { key: "reset", onClick: this.handleReset }, "Reset")
);
}
if (!this.state.search.complete) {
if (this.state.running) {
buttons.push(
/* @__PURE__ */ import_react.default.createElement(import_react_bootstrap.Button, { key: "pause", onClick: this.handlePause }, "Pause")
);
} else {
if (this.state.searchLeg > 1) {
buttons.push(
/* @__PURE__ */ import_react.default.createElement(import_react_bootstrap.Button, { key: "back", onClick: this.handleBackLeg }, "Previous")
);
buttons.push(
/* @__PURE__ */ import_react.default.createElement(import_react_bootstrap.Button, { key: "resume", onClick: this.handleRun }, "Resume")
);
} else {
buttons.push(
/* @__PURE__ */ import_react.default.createElement(import_react_bootstrap.Button, { key: "run", onClick: this.handleRun }, "Run")
);
}
if (!this.state.search.complete) {
buttons.push(
/* @__PURE__ */ import_react.default.createElement(import_react_bootstrap.Button, { key: "skip", onClick: this.handleSkipLeg }, "Skip")
);
}
}
}
let timer = /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null);
let instructionText = "Complete";
if (!this.state.search.complete) {
if (this.state.running) {
timer = /* @__PURE__ */ import_react.default.createElement("h1", null, /* @__PURE__ */ import_react.default.createElement(
SearchTimer,
{
key: "timer" + this.state.searchLeg,
runTime: Math.round(time.getTime("seconds")),
run: this.state.running,
complete: this.onCompleteTimer
}
));
}
instructionText = `Head ${this.humanBearing(this.state.search.leg.bearing)}`;
if (this.state.search.currentLeg + 1 <= this.state.search.searchLegs.length) {
instructionText += `, Next: ${this.humanBearing(this.state.search.searchLegs[this.state.search.currentLeg].bearing)}`;
}
}
const instruction = /* @__PURE__ */ import_react.default.createElement("h1", null, instructionText);
const totalDistance = /* @__PURE__ */ import_react.default.createElement("h1", null, "Total Length:", " ", /* @__PURE__ */ import_react.default.createElement(
import_speed_time_distance.DistanceUI,
{
distance: new import_speed_time_distance.Distance(this.state.search.length, "m"),
locked: true
}
));
return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
import_speed_time_distance.SpeedTimeDistanceUI,
{
key: "std" + this.state.searchLeg,
calculate: "time",
lockSelector: true,
lockDistance: true,
speed: this.state.speed,
distance,
time,
updateSpeed: this.onChangeSpeed
}
), /* @__PURE__ */ import_react.default.createElement(import_react_bootstrap.ButtonGroup, null, buttons), timer, instruction, /* @__PURE__ */ import_react.default.createElement(
import_sar_search_patterns.SearchDisplay,
{
key: "display" + this.state.searchLeg,
search: this.state.search
}
), totalDistance);
}
};
var SearchRunnerConfiguration = class extends import_react.default.Component {
constructor(props) {
super(props);
this.onChangeSearch = this.onChangeSearch.bind(this);
this.state = {
search: new import_sar_search_patterns.SectorSearch(200, 1, 1, 0)
};
}
onChangeSearch(search) {
if (search !== void 0) {
this.setState({
search
});
}
}
render() {
return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(import_sar_search_patterns.SearchConfiguration, { updateSearch: this.onChangeSearch }), /* @__PURE__ */ import_react.default.createElement(
SearchRunner,
{
key: `${this.state.search.uniqueKey()}`,
search: this.state.search
}
));
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
SearchRunner,
SearchRunnerConfiguration
});
//# sourceMappingURL=sar-search-runner.js.map