提交 f69d506e 编写于 作者: C chenboowu 提交者: ninecents

fix #134

上级 28d55e29
...@@ -2,14 +2,10 @@ ...@@ -2,14 +2,10 @@
<div> <div>
<option-block style="text-align: center"> <option-block style="text-align: center">
<FormItem> <FormItem>
<ButtonGroup> <RadioGroup v-model="type" type="button" button-style="solid">
<Button :type="current.type === v.key ? 'info' : 'primary'" <Radio :style="radioGroupStyle" :label="v.key" v-for="v in categories" :key="v.key">{{ v.name }}
@click="handle(v.key)" </Radio>
v-for="v in unitLists" </RadioGroup>
style="padding: 5px 10px 6px;"
:key="v.key">{{ v.name }}
</Button>
</ButtonGroup>
</FormItem> </FormItem>
</option-block> </option-block>
<option-block style="text-align: center"> <option-block style="text-align: center">
...@@ -18,14 +14,13 @@ ...@@ -18,14 +14,13 @@
</FormItem> </FormItem>
<FormItem> <FormItem>
<Select v-model="current.from" style="width:200px"> <Select v-model="current.from" style="width:200px">
<template v-if="unitGroup.length > 1"> <template v-if="groups.length > 1">
<OptionGroup :label="group.name" v-for="(group,gk) in unitGroup" :key="gk"> <OptionGroup :label="group.name" v-for="group in groups" :key="group.key">
<Option v-for="(unit,k) in group.list" :value="unit.name" :key="k">{{ unit.unit }}</Option> <Option v-for="unit in group.list" :value="unit" :key="unit">{{ unitName(unit) }}</Option>
</OptionGroup> </OptionGroup>
</template> </template>
<template v-else> <template v-else>
<Option v-for="(unit,k) in unitGroup[0].list" :value="unit.name" :key="k">{{ unit.unit }} <Option v-for="unit in groups[0].list" :value="unit" :key="unit">{{ unitName(unit) }}</Option>
</Option>
</template> </template>
</Select> </Select>
</FormItem> </FormItem>
...@@ -34,31 +29,30 @@ ...@@ -34,31 +29,30 @@
</FormItem> </FormItem>
<FormItem> <FormItem>
<Select v-model="current.to" style="width:200px"> <Select v-model="current.to" style="width:200px">
<Option value="all">全部</Option> <Option value="all">{{ $t('unit_all') }}</Option>
<template v-if="unitGroup.length > 1"> <template v-if="groups.length > 1">
<OptionGroup :label="group.name" v-for="(group,gk) in unitGroup" :key="gk"> <OptionGroup :label="group.name" v-for="group in groups" :key="group.key">
<Option v-for="(unit,k) in group.list" :value="unit.name" :key="k">{{ unit.unit }}</Option> <Option v-for="unit in group.list" :value="unit" :key="unit">{{ unitName(unit) }}</Option>
</OptionGroup> </OptionGroup>
</template> </template>
<template v-else> <template v-else>
<Option v-for="(unit,k) in unitGroup[0].list" :value="unit.name" :key="k">{{ unit.unit }} <Option v-for="unit in groups[0].list" :value="unit" :key="unit">{{ unitName(unit) }}</Option>
</Option>
</template> </template>
</Select> </Select>
</FormItem> </FormItem>
</option-block> </option-block>
<div v-if="isShowResult" style="padding: 0 30px"> <div style="padding: 0 30px; min-height: 200px">
<template v-if="current.to !== 'all' && assignResult"> <template v-if="assignResult !== null">
<div style="text-align: center;margin-top: 30px;font-size: 18px;font-weight: bold"> <div style="text-align: center;margin-top: 30px;font-size: 18px;font-weight: bold">
<span style="color: red">{{ current.input }}</span> {{ current.from }} = <span <span style="color: red">{{ current.input }}</span> {{ unitName(current.from) }} = <span
style="color: red">{{ assignResult.num }}</span> style="color: red">{{ assignResult }}</span>
{{ current.to }} {{ unitName(current.to) }}
</div> </div>
</template> </template>
<CellGroup v-if="current.to === 'all'"> <CellGroup v-if="current.to === 'all'">
<Row :gutter="16"> <Row :gutter="16">
<Col span="12" v-for="(item,k) in result" :key="k"> <Col span="12" v-for="(result,unitKey) in output" :key="unitKey">
<Cell :title="item.num" :extra="item.unit"/> <Cell :title="result" :extra="unitName(unitKey)"/>
</Col> </Col>
</Row> </Row>
</CellGroup> </CellGroup>
...@@ -67,58 +61,61 @@ ...@@ -67,58 +61,61 @@
</template> </template>
<script> <script>
import U from './library/unit' import U from './library/unit'
import {getCurrentLocale} from "../../i18n";
export default { export default {
created() { created() {
let history = this.$getToolData() let history = this.$getToolData()
this.current.type = history['type'] ? history['type'] : 'temperature' this.type = this.current.type = history['type'] ? history['type'] : 'temperature'
this.getToolData() this.getToolData(this.type)
}, },
computed: { computed: {
unitGroup() { radioGroupStyle() {
return U.getData(this.current.type) return getCurrentLocale() === "en" ? "padding: 0 5px" : "padding: 0 10px";
},
categories() {
return U.CONFIG.map((category) => {
return {name: category.name, key: category.key}
})
},
groups() {
return U.getCategory(this.current.type).group
}, },
isShowResult() { isShowResult() {
return this.current.from && this.current.type && this.current.input return this.current.from && this.current.type && this.current.input
}, },
result() { output() {
let r = [] let result = {}
if (this.isShowResult) { if (this.isShowResult) {
this.saveToolData() this.saveToolData()
this.unitGroup.forEach((Group) => { for (let unit of U.getCategory(this.current.type).unit) {
Group.list.forEach((unit) => { result[unit.key] = U.calculate(
let temp = U.calc( this.current.type,
this.current.type, this.current.input,
this.current.input, this.current.from,
this.current.from, unit.key
unit.name )
) }
r.push({
unit: unit.name,
num: temp.num,
})
})
})
} }
return r return result
}, },
assignResult() { assignResult() {
if (this.current.to === 'all') { if (this.current.to !== 'all') {
return {} if (this.current.to in this.output) {
} return this.output[this.current.to]
let unit = this.current.to
for (let i = 0; i < this.result.length; i++) {
if (this.result[i].unit === unit) {
return this.result[i]
} }
} }
return null return null
} }
}, },
watch:{
type(value){
this.handle(value)
}
},
methods: { methods: {
handle(v) { handle(v) {
this.current.type = v this.getToolData(v)
this.getToolData()
}, },
saveToolData() { saveToolData() {
let history = this.$getToolData() let history = this.$getToolData()
...@@ -131,13 +128,15 @@ export default { ...@@ -131,13 +128,15 @@ export default {
} }
this.$saveToolData(history) this.$saveToolData(history)
}, },
getToolData() { getToolData(type) {
let type = this.current.type
let history = this.$getToolData() let history = this.$getToolData()
let unitHistory = history['data'] && history['data'][type] ? history['data'][type] : null let unitHistory = history['data'] && history['data'][type] ? history['data'][type] : null
this.current.from = unitHistory ? unitHistory.from : U.getMain(this.current.type) this.current = Object.assign(this.current, {
this.current.to = unitHistory ? unitHistory.to : 'all' from : unitHistory ? unitHistory.from : U.getCategory(type).main,
this.current.input = unitHistory ? unitHistory.input : '' to : unitHistory ? unitHistory.to : 'all',
input : unitHistory ? unitHistory.input : '',
type : type
})
}, },
exchange() { exchange() {
if (this.current.to === 'all') { if (this.current.to === 'all') {
...@@ -146,10 +145,11 @@ export default { ...@@ -146,10 +145,11 @@ export default {
if (!this.current.from || !this.current.to) { if (!this.current.from || !this.current.to) {
return return
} }
[this.current.from, this.current.to] = [this.current.to, this.current.from]
let temp = this.current.from },
this.current.from = this.current.to unitName(unitKey) {
this.current.to = temp let unit = U.getUnit(this.current.type, unitKey)
return `${unit.name} (${unit['unit']})`
} }
}, },
data() { data() {
...@@ -160,7 +160,7 @@ export default { ...@@ -160,7 +160,7 @@ export default {
to: '', to: '',
input: '', input: '',
}, },
unitLists: U.list(), type:"",
} }
}, },
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册