frequi_origin/src/components/charts/PlotIndicator.vue

160 lines
4.0 KiB
Vue
Raw Normal View History

<template>
<div>
<div v-if="addNew">
<b-form-group label="Add indicator" label-for="indicatorSelector">
<b-form-select
id="indicatorSelector"
v-model="selAvailableIndicator"
:options="columns"
:select-size="4"
>
</b-form-select>
</b-form-group>
</div>
<b-form-group label="Type" label-for="plotTypeSelector">
<b-form-select
id="plotTypeSelector"
v-model="graphType"
size="sm"
:options="availableGraphTypes"
@change="emitIndicator()"
>
</b-form-select>
</b-form-group>
<hr />
<b-form-group label="Color" label-for="colsel" size="sm">
<b-input-group>
<b-input-group-prepend>
<div :style="{ 'background-color': selColor }" class="colorbox mr-2"></div>
2021-06-23 18:47:17 +00:00
<!-- <b-form-input
id="colsel"
v-model="selColor"
size="sm"
class="colorbox"
type="color"
:style="{ 'background-color': selColor }"
>
</b-form-input> -->
</b-input-group-prepend>
2021-06-23 18:47:17 +00:00
<b-form-input id="colsel" v-model="selColor" size="sm"> </b-form-input>
<b-input-group-append>
<b-button variant="primary" size="sm" @click="newColor">&#x21bb;</b-button>
</b-input-group-append>
</b-input-group>
</b-form-group>
2021-12-11 16:20:57 +00:00
<div class="d-flex d-flex-columns">
2021-11-01 18:53:06 +00:00
<b-button
v-if="addNew"
class="flex-grow-1"
variant="primary"
title="Add "
size="sm"
@click="emitIndicator"
>
Save indicator
</b-button>
<b-button
v-if="addNew"
2021-11-01 18:53:06 +00:00
class="ml-1 flex-grow-1"
variant="primary"
title="Add "
size="sm"
@click="clickCancel"
>
Cancel
</b-button>
</div>
</div>
</template>
<script lang="ts">
import { ChartType, IndicatorConfig } from '@/types';
import randomColor from '@/shared/randomColor';
import { defineComponent, computed, ref, watch } from '@vue/composition-api';
export default defineComponent({
name: 'PlotIndicator',
props: {
value: { required: true, type: Object as () => Record<string, IndicatorConfig> },
columns: { required: true, type: Array as () => string[] },
addNew: { required: true, type: Boolean },
},
emits: ['input'],
setup(props, { emit }) {
const selColor = ref(randomColor());
const graphType = ref<ChartType>(ChartType.line);
const availableGraphTypes = ref(Object.keys(ChartType));
const selAvailableIndicator = ref('');
const cancelled = ref(false);
const newColor = () => {
selColor.value = randomColor();
};
const combinedIndicator = computed(() => {
if (cancelled.value || !selAvailableIndicator.value) {
return {};
}
return {
[selAvailableIndicator.value]: {
color: selColor,
type: graphType,
},
};
});
const emitIndicator = () => {
emit('input', combinedIndicator.value);
};
const clickCancel = () => {
cancelled.value = true;
emitIndicator();
};
watch(
() => props.value,
() => {
[selAvailableIndicator.value] = Object.keys(props.value);
cancelled.value = false;
if (selAvailableIndicator.value && props.value) {
selColor.value = props.value[selAvailableIndicator.value].color || randomColor();
graphType.value = props.value[selAvailableIndicator.value].type || ChartType.line;
}
},
);
watch(selColor, () => {
if (!props.addNew) {
emitIndicator();
}
});
return {
selColor,
graphType,
availableGraphTypes,
selAvailableIndicator,
cancelled,
combinedIndicator,
newColor,
emitIndicator,
clickCancel,
};
},
});
</script>
<style scoped>
.colorbox {
border-radius: 50%;
margin-top: auto;
margin-bottom: auto;
height: 25px;
width: 25px;
vertical-align: center;
}
</style>