mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
3.1 KiB
118 lines
3.1 KiB
3 years ago
|
<script setup lang="ts">
|
||
2 years ago
|
import { ColumnInj, computed, convertDurationToSeconds, convertMS2Duration, durationOptions, inject, ref } from '#imports'
|
||
|
import { EditModeInj } from '~/context'
|
||
3 years ago
|
|
||
3 years ago
|
interface Props {
|
||
2 years ago
|
modelValue: number | string | null | undefined
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
const { modelValue } = defineProps<Props>()
|
||
|
|
||
|
const emit = defineEmits(['update:modelValue'])
|
||
|
|
||
3 years ago
|
const column = inject(ColumnInj)
|
||
3 years ago
|
|
||
2 years ago
|
const editEnabled = inject(EditModeInj)
|
||
2 years ago
|
|
||
3 years ago
|
const showWarningMessage = ref(false)
|
||
2 years ago
|
|
||
3 years ago
|
const durationInMS = ref(0)
|
||
2 years ago
|
|
||
3 years ago
|
const isEdited = ref(false)
|
||
2 years ago
|
|
||
2 years ago
|
const durationType = ref(column?.value?.meta?.duration || 0)
|
||
3 years ago
|
|
||
|
const durationPlaceholder = computed(() => durationOptions[durationType.value].title)
|
||
2 years ago
|
|
||
3 years ago
|
const localState = computed({
|
||
|
get: () => convertMS2Duration(modelValue, durationType.value),
|
||
|
set: (val) => {
|
||
|
isEdited.value = true
|
||
|
const res = convertDurationToSeconds(val, durationType.value)
|
||
|
if (res._isValid) {
|
||
|
durationInMS.value = res._sec
|
||
|
}
|
||
3 years ago
|
},
|
||
3 years ago
|
})
|
||
|
|
||
3 years ago
|
const checkDurationFormat = (evt: KeyboardEvent) => {
|
||
3 years ago
|
evt = evt || window.event
|
||
|
const charCode = evt.which ? evt.which : evt.keyCode
|
||
|
// ref: http://www.columbia.edu/kermit/ascii.html
|
||
|
const PRINTABLE_CTL_RANGE = charCode > 31
|
||
|
const NON_DIGIT = charCode < 48 || charCode > 57
|
||
|
const NON_COLON = charCode !== 58
|
||
|
const NON_PERIOD = charCode !== 46
|
||
|
if (PRINTABLE_CTL_RANGE && NON_DIGIT && NON_COLON && NON_PERIOD) {
|
||
|
showWarningMessage.value = true
|
||
|
evt.preventDefault()
|
||
|
} else {
|
||
|
showWarningMessage.value = false
|
||
|
// only allow digits, '.' and ':' (without quotes)
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
const submitDuration = () => {
|
||
3 years ago
|
if (isEdited.value) {
|
||
3 years ago
|
emit('update:modelValue', durationInMS.value)
|
||
3 years ago
|
}
|
||
|
isEdited.value = false
|
||
|
}
|
||
3 years ago
|
</script>
|
||
|
|
||
3 years ago
|
<template>
|
||
3 years ago
|
<div class="duration-cell-wrapper">
|
||
|
<input
|
||
2 years ago
|
v-if="editEnabled"
|
||
3 years ago
|
ref="durationInput"
|
||
|
v-model="localState"
|
||
|
:placeholder="durationPlaceholder"
|
||
3 years ago
|
@blur="submitDuration"
|
||
3 years ago
|
@keypress="checkDurationFormat($event)"
|
||
3 years ago
|
@keydown.enter="submitDuration"
|
||
3 years ago
|
/>
|
||
2 years ago
|
<span v-else> {{ localState }}</span>
|
||
2 years ago
|
<div v-if="showWarningMessage" class="duration-warning">
|
||
3 years ago
|
<!-- TODO: i18n -->
|
||
|
Please enter a number
|
||
|
</div>
|
||
|
</div>
|
||
3 years ago
|
</template>
|
||
3 years ago
|
|
||
3 years ago
|
<style scoped>
|
||
|
.duration-cell-wrapper {
|
||
|
padding: 10px;
|
||
|
}
|
||
|
|
||
|
.duration-warning {
|
||
|
text-align: left;
|
||
|
margin-top: 10px;
|
||
|
color: #e65100;
|
||
|
}
|
||
|
</style>
|
||
3 years ago
|
|
||
|
<!--
|
||
|
/**
|
||
|
* @copyright Copyright (c) 2021, Xgene Cloud Ltd
|
||
|
*
|
||
|
* @author Wing-Kam Wong <wingkwong.code@gmail.com>
|
||
|
*
|
||
|
* @license GNU AGPL version 3 or any later version
|
||
|
*
|
||
|
* This program is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU Affero General Public License as
|
||
|
* published by the Free Software Foundation, either version 3 of the
|
||
|
* License, or (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU Affero General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Affero General Public License
|
||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*
|
||
|
*/
|
||
|
-->
|