configurator: autoload board and scenario xml when exist

autoload board and scenario xml when exist

Tracked-On: #6691
Signed-off-by: Weiyi Feng <weiyix.feng@intel.com>
This commit is contained in:
Weiyi-Feng 2022-03-31 15:33:25 +08:00 committed by acrnsi-robot
parent 3c4f14ede7
commit 260718d544
2 changed files with 45 additions and 7 deletions

View File

@ -2,9 +2,10 @@ import React, {Component} from "react";
import {Accordion, Button, Col, Form, Row} from "react-bootstrap"; import {Accordion, Button, Col, Form, Row} from "react-bootstrap";
import CreateScenarioModal from "./CreateScenarioModal/CreateScenarioModal"; import CreateScenarioModal from "./CreateScenarioModal/CreateScenarioModal";
import Banner from "../../../components/Banner";
import {dialog} from "@tauri-apps/api"; import {dialog} from "@tauri-apps/api";
import {ACRNContext} from "../../../ACRNContext"; import {ACRNContext} from "../../../ACRNContext";
import {invoke} from "@tauri-apps/api/tauri";
import _ from "lodash/fp";
export default class CreateNewOrImportAnExistingScenario extends Component { export default class CreateNewOrImportAnExistingScenario extends Component {
constructor(props) { constructor(props) {
@ -21,7 +22,22 @@ export default class CreateNewOrImportAnExistingScenario extends Component {
} }
componentDidMount() { componentDidMount() {
let {configurator} = this.context
this.scenarioHistoryUpdate() this.scenarioHistoryUpdate()
invoke('fs_read_dir', {
path: configurator.WorkingFolder,
recursive: false
}).then((files) => {
for (let i = 0; i < files.length; i++) {
let isScenario = _.endsWith("/scenario.xml", files[i].path.replace(/\\/g, '/'));
console.log("files", isScenario, files[i].path)
if (isScenario) {
this.scenarioChange(files[i].path).then(() => {
this.importScenario()
})
}
}
})
} }
scenarioHistoryUpdate() { scenarioHistoryUpdate() {
@ -39,7 +55,7 @@ export default class CreateNewOrImportAnExistingScenario extends Component {
scenarioChange = (filepath) => { scenarioChange = (filepath) => {
console.log(filepath) console.log(filepath)
let {configurator} = this.context let {configurator} = this.context
configurator.addHistory('scenario', filepath).then(() => { return configurator.addHistory('scenario', filepath).then(() => {
this.scenarioHistoryUpdate().then(() => { this.scenarioHistoryUpdate().then(() => {
this.scenarioXMLSelect.current.value = filepath this.scenarioXMLSelect.current.value = filepath
}) })
@ -48,9 +64,17 @@ export default class CreateNewOrImportAnExistingScenario extends Component {
importScenario = () => { importScenario = () => {
let {configurator} = this.context let {configurator} = this.context
configurator.programLayer.loadScenario(this.scenarioXMLSelect.current.value) return configurator.programLayer.loadScenario(this.scenarioXMLSelect.current.value)
.then(() => { .then(() => {
this.setState({selected: configurator.WorkingFolder + '/scenario.xml'}) let printPath = configurator.WorkingFolder;
if (_.endsWith(configurator.WorkingFolder, "/") || _.endsWith(configurator.WorkingFolder, "\\")) {
printPath = printPath + 'scenario.xml'
} else {
printPath = printPath + (configurator.WorkingFolder[1] === ":" ? "\\" : '/') + 'scenario.xml'
}
this.setState({selected: printPath})
}).then(() => {
document.querySelectorAll(".accordion-button")[2].click()
}) })
.catch((reason) => { .catch((reason) => {
console.log(reason) console.log(reason)

View File

@ -1,8 +1,9 @@
import React, {Component} from "react"; import React, {Component} from "react";
import {Accordion, Button, Col, Form, Row} from "react-bootstrap"; import {Accordion, Button, Col, Form, Row} from "react-bootstrap";
import Banner from "../../../components/Banner";
import {dialog} from "@tauri-apps/api"; import {dialog} from "@tauri-apps/api";
import {ACRNContext} from "../../../ACRNContext"; import {ACRNContext} from "../../../ACRNContext";
import {invoke} from "@tauri-apps/api/tauri";
import _ from "lodash/fp";
export default class ImportABoardConfigurationFile extends Component { export default class ImportABoardConfigurationFile extends Component {
constructor(props) { constructor(props) {
@ -19,12 +20,25 @@ export default class ImportABoardConfigurationFile extends Component {
} }
} }
componentDidMount() { componentDidMount = () => {
let {configurator} = this.context let {configurator} = this.context
configurator.getHistory('board').then((boardFiles) => { configurator.getHistory('board').then((boardFiles) => {
let disableImport = boardFiles.length === 0 let disableImport = boardFiles.length === 0
this.setState({boardFiles, disableImport}) this.setState({boardFiles, disableImport})
}) })
invoke('fs_read_dir', {
path: configurator.WorkingFolder,
recursive: false
}).then((files) => {
for (let i = 0; i < files.length; i++) {
console.log("files", files[i].path)
if (_.endsWith(".board.xml", files[i].path)) {
this.boardChange(files[i].path).then(() => {
this.importBoard()
})
}
}
})
} }
@ -37,7 +51,7 @@ export default class ImportABoardConfigurationFile extends Component {
boardChange = (filepath) => { boardChange = (filepath) => {
let {configurator} = this.context let {configurator} = this.context
configurator.addHistory('board', filepath).then(() => { return configurator.addHistory('board', filepath).then(() => {
return configurator.getHistory('board') return configurator.getHistory('board')
}).then((boardFiles) => { }).then((boardFiles) => {
console.log(boardFiles) console.log(boardFiles)