2016-07-27 06:15:02 +00:00
|
|
|
<template>
|
|
|
|
<span
|
|
|
|
class="el-date-editor"
|
2016-08-09 02:10:33 +00:00
|
|
|
v-clickoutside="handleClose"
|
2016-07-27 06:15:02 +00:00
|
|
|
:class="{
|
|
|
|
'is-have-trigger': haveTrigger,
|
|
|
|
'is-active': pickerVisible,
|
2016-08-09 02:10:33 +00:00
|
|
|
'is-filled': !!this.value
|
2016-07-27 06:15:02 +00:00
|
|
|
}">
|
|
|
|
|
|
|
|
<input
|
|
|
|
class="el-date-editor__editor"
|
|
|
|
:readonly="readonly"
|
2016-10-12 09:27:41 +00:00
|
|
|
type="text"
|
2016-07-27 06:15:02 +00:00
|
|
|
:placeholder="placeholder"
|
|
|
|
@focus="handleFocus"
|
|
|
|
@blur="handleBlur"
|
2016-08-09 02:10:33 +00:00
|
|
|
@keydown="handleKeydown"
|
|
|
|
ref="reference"
|
|
|
|
v-model.lazy="visualValue" />
|
2016-07-27 06:15:02 +00:00
|
|
|
|
|
|
|
<span
|
2016-10-26 14:56:51 +00:00
|
|
|
@click="pickerVisible = !pickerVisible"
|
2016-08-09 02:10:33 +00:00
|
|
|
class="el-date-editor__trigger el-icon"
|
|
|
|
:class="[triggerClass]"
|
2016-07-27 06:15:02 +00:00
|
|
|
v-if="haveTrigger">
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import Vue from 'vue';
|
2016-10-13 03:12:24 +00:00
|
|
|
import Clickoutside from 'element-ui/src/utils/clickoutside';
|
2016-10-13 08:11:06 +00:00
|
|
|
import { formatDate, parseDate, getWeekNumber } from './util';
|
2016-10-13 03:12:24 +00:00
|
|
|
import Popper from 'element-ui/src/utils/vue-popper';
|
2016-10-27 09:31:22 +00:00
|
|
|
import Emitter from 'element-ui/src/mixins/emitter';
|
2016-07-27 06:15:02 +00:00
|
|
|
|
2016-10-27 09:31:22 +00:00
|
|
|
const NewPopper = {
|
2016-10-12 09:41:49 +00:00
|
|
|
props: {
|
|
|
|
appendToBody: Popper.props.appendToBody,
|
|
|
|
offset: Popper.props.offset,
|
|
|
|
boundariesPadding: Popper.props.boundariesPadding
|
|
|
|
},
|
|
|
|
methods: Popper.methods,
|
|
|
|
data: Popper.data,
|
|
|
|
beforeDestroy: Popper.beforeDestroy
|
|
|
|
};
|
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
const RANGE_SEPARATOR = ' - ';
|
|
|
|
const DEFAULT_FORMATS = {
|
|
|
|
date: 'yyyy-MM-dd',
|
|
|
|
month: 'yyyy-MM',
|
|
|
|
datetime: 'yyyy-MM-dd HH:mm:ss',
|
|
|
|
time: 'HH:mm:ss',
|
|
|
|
timerange: 'HH:mm:ss',
|
|
|
|
daterange: 'yyyy-MM-dd',
|
|
|
|
datetimerange: 'yyyy-MM-dd HH:mm:ss'
|
|
|
|
};
|
|
|
|
const HAVE_TRIGGER_TYPES = [
|
|
|
|
'date',
|
|
|
|
'datetime',
|
|
|
|
'time',
|
|
|
|
'time-select',
|
|
|
|
'week',
|
|
|
|
'month',
|
|
|
|
'year',
|
|
|
|
'daterange',
|
|
|
|
'timerange',
|
|
|
|
'datetimerange'
|
|
|
|
];
|
|
|
|
const DATE_FORMATTER = function(value, format) {
|
|
|
|
return formatDate(value, format);
|
|
|
|
};
|
|
|
|
const DATE_PARSER = function(text, format) {
|
|
|
|
text = text.split(':');
|
|
|
|
if (text.length > 1) text = text.map(item => item.slice(-2));
|
|
|
|
text = text.join(':');
|
|
|
|
|
|
|
|
return parseDate(text, format);
|
|
|
|
};
|
|
|
|
const RANGE_FORMATTER = function(value, format) {
|
|
|
|
if (Array.isArray(value) && value.length === 2) {
|
|
|
|
const start = value[0];
|
|
|
|
const end = value[1];
|
|
|
|
|
|
|
|
if (start && end) {
|
|
|
|
return formatDate(start, format) + RANGE_SEPARATOR + formatDate(end, format);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
};
|
|
|
|
const RANGE_PARSER = function(text, format) {
|
|
|
|
const array = text.split(RANGE_SEPARATOR);
|
|
|
|
if (array.length === 2) {
|
|
|
|
const range1 = array[0].split(':').map(item => item.slice(-2)).join(':');
|
|
|
|
const range2 = array[1].split(':').map(item => item.slice(-2)).join(':');
|
|
|
|
return [parseDate(range1, format), parseDate(range2, format)];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
const TYPE_VALUE_RESOLVER_MAP = {
|
|
|
|
default: {
|
|
|
|
formatter(value) {
|
|
|
|
if (!value) return '';
|
|
|
|
return '' + value;
|
|
|
|
},
|
|
|
|
parser(text) {
|
|
|
|
if (text === undefined || text === '') return null;
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
week: {
|
|
|
|
formatter(value) {
|
|
|
|
if (value instanceof Date) {
|
|
|
|
const weekNumber = getWeekNumber(value);
|
|
|
|
return value.getFullYear() + 'w' + (weekNumber > 9 ? weekNumber : '0' + weekNumber);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
parser(text) {
|
|
|
|
const array = (text || '').split('w');
|
|
|
|
if (array.length === 2) {
|
|
|
|
const year = Number(array[0]);
|
|
|
|
const month = Number(array[1]);
|
|
|
|
|
|
|
|
if (!isNaN(year) && !isNaN(month) && month < 54) {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
date: {
|
|
|
|
formatter: DATE_FORMATTER,
|
|
|
|
parser: DATE_PARSER
|
|
|
|
},
|
|
|
|
datetime: {
|
|
|
|
formatter: DATE_FORMATTER,
|
|
|
|
parser: DATE_PARSER
|
|
|
|
},
|
|
|
|
daterange: {
|
|
|
|
formatter: RANGE_FORMATTER,
|
|
|
|
parser: RANGE_PARSER
|
|
|
|
},
|
|
|
|
datetimerange: {
|
|
|
|
formatter: RANGE_FORMATTER,
|
|
|
|
parser: RANGE_PARSER
|
|
|
|
},
|
|
|
|
timerange: {
|
|
|
|
formatter: RANGE_FORMATTER,
|
|
|
|
parser: RANGE_PARSER
|
|
|
|
},
|
|
|
|
time: {
|
|
|
|
formatter: DATE_FORMATTER,
|
|
|
|
parser: DATE_PARSER
|
|
|
|
},
|
|
|
|
month: {
|
|
|
|
formatter: DATE_FORMATTER,
|
|
|
|
parser: DATE_PARSER
|
|
|
|
},
|
|
|
|
year: {
|
|
|
|
formatter(value) {
|
|
|
|
if (!value) return '';
|
|
|
|
return '' + value;
|
|
|
|
},
|
|
|
|
parser(text) {
|
|
|
|
const year = Number(text);
|
|
|
|
if (!isNaN(year)) return year;
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
number: {
|
|
|
|
formatter(value) {
|
|
|
|
if (!value) return '';
|
|
|
|
return '' + value;
|
|
|
|
},
|
|
|
|
parser(text) {
|
|
|
|
let result = Number(text);
|
|
|
|
|
|
|
|
if (!isNaN(text)) {
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-09-21 04:46:53 +00:00
|
|
|
const PLACEMENT_MAP = {
|
|
|
|
left: 'bottom-start',
|
|
|
|
center: 'bottom-center',
|
|
|
|
right: 'bottom-end'
|
|
|
|
};
|
2016-07-27 06:15:02 +00:00
|
|
|
|
|
|
|
export default {
|
2016-10-27 09:31:22 +00:00
|
|
|
mixins: [Emitter, NewPopper],
|
2016-08-29 11:19:14 +00:00
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
props: {
|
|
|
|
format: String,
|
|
|
|
readonly: Boolean,
|
|
|
|
placeholder: String,
|
2016-09-21 04:46:53 +00:00
|
|
|
align: {
|
|
|
|
type: String,
|
|
|
|
default: 'left'
|
|
|
|
},
|
2016-07-27 06:15:02 +00:00
|
|
|
value: {},
|
|
|
|
haveTrigger: {},
|
|
|
|
pickerOptions: {}
|
|
|
|
},
|
|
|
|
|
2016-10-20 12:12:21 +00:00
|
|
|
directives: { Clickoutside },
|
2016-07-27 06:15:02 +00:00
|
|
|
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
pickerVisible: false
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
watch: {
|
|
|
|
pickerVisible(val) {
|
2016-10-02 10:31:29 +00:00
|
|
|
val ? this.showPicker() : this.hidePicker();
|
2016-08-29 11:19:14 +00:00
|
|
|
},
|
|
|
|
value(val) {
|
|
|
|
this.dispatch('form-item', 'el.form.change');
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
computed: {
|
|
|
|
triggerClass() {
|
|
|
|
return this.type.indexOf('time') !== -1 ? 'el-icon-time' : 'el-icon-date';
|
|
|
|
},
|
|
|
|
|
|
|
|
editable() {
|
|
|
|
return this.type.indexOf('range') === -1;
|
|
|
|
},
|
|
|
|
|
|
|
|
selectionMode() {
|
|
|
|
if (this.type === 'week') {
|
|
|
|
return 'week';
|
|
|
|
} else if (this.type === 'month') {
|
|
|
|
return 'month';
|
|
|
|
} else if (this.type === 'year') {
|
|
|
|
return 'year';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'day';
|
|
|
|
},
|
|
|
|
|
|
|
|
haveTrigger() {
|
|
|
|
if (typeof this.showTrigger !== 'undefined') {
|
|
|
|
return this.showTrigger;
|
|
|
|
}
|
|
|
|
return HAVE_TRIGGER_TYPES.indexOf(this.type) !== -1;
|
|
|
|
},
|
|
|
|
|
|
|
|
visualValue: {
|
|
|
|
get() {
|
|
|
|
const value = this.value;
|
|
|
|
const formatter = (
|
|
|
|
TYPE_VALUE_RESOLVER_MAP[this.type] ||
|
|
|
|
TYPE_VALUE_RESOLVER_MAP['default']
|
|
|
|
).formatter;
|
|
|
|
const format = DEFAULT_FORMATS[this.type];
|
|
|
|
|
|
|
|
return formatter(value, this.format || format);
|
|
|
|
},
|
|
|
|
|
|
|
|
set(value) {
|
|
|
|
if (value) {
|
|
|
|
const type = this.type;
|
|
|
|
const parser = (
|
|
|
|
TYPE_VALUE_RESOLVER_MAP[type] ||
|
|
|
|
TYPE_VALUE_RESOLVER_MAP['default']
|
|
|
|
).parser;
|
|
|
|
const parsedValue = parser(value, this.format || DEFAULT_FORMATS[type]);
|
|
|
|
|
|
|
|
if (parsedValue) {
|
2016-10-20 12:12:21 +00:00
|
|
|
this.picker.value = parsedValue;
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2016-10-20 12:12:21 +00:00
|
|
|
this.picker.value = value;
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-10-12 09:41:49 +00:00
|
|
|
created() {
|
|
|
|
// vue-popper
|
|
|
|
this.options = {
|
|
|
|
boundariesPadding: 0,
|
|
|
|
gpuAcceleration: false
|
|
|
|
};
|
|
|
|
this.placement = PLACEMENT_MAP[this.align] || PLACEMENT_MAP.left;
|
|
|
|
},
|
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
methods: {
|
2016-08-09 02:10:33 +00:00
|
|
|
handleClose() {
|
|
|
|
this.pickerVisible = false;
|
|
|
|
},
|
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
handleFocus() {
|
|
|
|
const type = this.type;
|
|
|
|
|
2016-10-02 10:31:29 +00:00
|
|
|
if (HAVE_TRIGGER_TYPES.indexOf(type) !== -1 && !this.pickerVisible) {
|
|
|
|
this.pickerVisible = true;
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
this.$emit('focus', this);
|
|
|
|
},
|
|
|
|
|
|
|
|
handleBlur() {
|
|
|
|
this.$emit('blur', this);
|
2016-08-29 11:19:14 +00:00
|
|
|
this.dispatch('form-item', 'el.form.blur');
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
handleKeydown(event) {
|
|
|
|
const keyCode = event.keyCode;
|
2016-10-20 12:12:21 +00:00
|
|
|
const target = event.target;
|
|
|
|
let selectionStart = target.selectionStart;
|
|
|
|
let selectionEnd = target.selectionEnd;
|
|
|
|
let length = target.value.length;
|
2016-07-27 06:15:02 +00:00
|
|
|
|
2016-10-13 08:11:06 +00:00
|
|
|
// tab
|
|
|
|
if (keyCode === 9) {
|
|
|
|
this.pickerVisible = false;
|
2016-10-20 12:12:21 +00:00
|
|
|
// enter
|
|
|
|
} else if (keyCode === 13) {
|
2016-07-27 06:15:02 +00:00
|
|
|
this.pickerVisible = this.picker.visible = false;
|
2016-10-20 12:12:21 +00:00
|
|
|
this.visualValue = target.value;
|
|
|
|
target.blur();
|
2016-07-27 06:15:02 +00:00
|
|
|
// left
|
|
|
|
} else if (keyCode === 37) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
if (selectionEnd === length && selectionStart === length) {
|
2016-10-20 12:12:21 +00:00
|
|
|
target.selectionStart = length - 2;
|
2016-07-27 06:15:02 +00:00
|
|
|
} else if (selectionStart >= 3) {
|
2016-10-20 12:12:21 +00:00
|
|
|
target.selectionStart -= 3;
|
2016-07-27 06:15:02 +00:00
|
|
|
} else {
|
2016-10-20 12:12:21 +00:00
|
|
|
target.selectionStart = 0;
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
2016-10-20 12:12:21 +00:00
|
|
|
target.selectionEnd = target.selectionStart + 2;
|
2016-07-27 06:15:02 +00:00
|
|
|
// right
|
|
|
|
} else if (keyCode === 39) {
|
|
|
|
event.preventDefault();
|
|
|
|
if (selectionEnd === 0 && selectionStart === 0) {
|
2016-10-20 12:12:21 +00:00
|
|
|
target.selectionEnd = 2;
|
2016-07-27 06:15:02 +00:00
|
|
|
} else if (selectionEnd <= length - 3) {
|
2016-10-20 12:12:21 +00:00
|
|
|
target.selectionEnd += 3;
|
2016-07-27 06:15:02 +00:00
|
|
|
} else {
|
2016-10-20 12:12:21 +00:00
|
|
|
target.selectionEnd = length;
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
2016-10-20 12:12:21 +00:00
|
|
|
target.selectionStart = target.selectionEnd - 2;
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
hidePicker() {
|
|
|
|
if (this.picker) {
|
|
|
|
this.picker.resetView && this.picker.resetView();
|
|
|
|
this.pickerVisible = this.picker.visible = false;
|
|
|
|
this.destroyPopper();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
showPicker() {
|
|
|
|
if (!this.picker) {
|
2016-09-10 02:01:48 +00:00
|
|
|
this.panel.defaultValue = this.value;
|
2016-10-13 08:11:06 +00:00
|
|
|
this.picker = new Vue(this.panel).$mount(document.createElement('div'));
|
2016-10-12 09:41:49 +00:00
|
|
|
this.popperElm = this.picker.$el;
|
|
|
|
this.picker.width = this.$refs.reference.getBoundingClientRect().width;
|
2016-07-27 06:15:02 +00:00
|
|
|
this.picker.showTime = this.type === 'datetime' || this.type === 'datetimerange';
|
|
|
|
this.picker.selectionMode = this.selectionMode;
|
|
|
|
if (this.format) {
|
|
|
|
this.picker.format = this.format;
|
|
|
|
}
|
|
|
|
|
|
|
|
const options = this.pickerOptions;
|
|
|
|
|
|
|
|
if (options && options.selectableRange) {
|
|
|
|
let ranges = options.selectableRange;
|
|
|
|
const parser = TYPE_VALUE_RESOLVER_MAP.datetimerange.parser;
|
|
|
|
const format = DEFAULT_FORMATS.timerange;
|
|
|
|
|
|
|
|
ranges = Array.isArray(ranges) ? ranges : [ranges];
|
|
|
|
this.picker.selectableRange = ranges.map(range => parser(range, format));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.type === 'time-select' && options) {
|
|
|
|
this.$watch('pickerOptions.minTime', val => {
|
|
|
|
this.picker.minTime = val;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const option in options) {
|
2016-09-01 03:45:44 +00:00
|
|
|
if (options.hasOwnProperty(option) &&
|
|
|
|
// 忽略 time-picker 的该配置项
|
|
|
|
option !== 'selectableRange') {
|
2016-07-27 06:15:02 +00:00
|
|
|
this.picker[option] = options[option];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$el.appendChild(this.picker.$el);
|
|
|
|
this.pickerVisible = this.picker.visible = true;
|
|
|
|
this.picker.resetView && this.picker.resetView();
|
|
|
|
|
2016-10-12 09:41:49 +00:00
|
|
|
this.picker.$on('dodestroy', this.doDestroy);
|
2016-07-27 06:15:02 +00:00
|
|
|
this.picker.$on('pick', (date, visible = false) => {
|
2016-08-09 02:10:33 +00:00
|
|
|
this.$emit('input', date);
|
2016-07-27 06:15:02 +00:00
|
|
|
|
|
|
|
if (!visible) {
|
2016-09-29 06:45:00 +00:00
|
|
|
this.pickerVisible = this.picker.visible = !this.picker.visible;
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
this.picker.resetView && this.picker.resetView();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.picker.$on('select-range', (start, end) => {
|
|
|
|
setTimeout(() => {
|
2016-08-09 02:10:33 +00:00
|
|
|
this.$refs.reference.setSelectionRange(start, end);
|
|
|
|
this.$refs.reference.focus();
|
2016-07-27 06:15:02 +00:00
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.pickerVisible = this.picker.visible = true;
|
|
|
|
}
|
|
|
|
|
2016-10-12 09:41:49 +00:00
|
|
|
this.updatePopper();
|
2016-07-27 06:15:02 +00:00
|
|
|
|
|
|
|
if (this.value instanceof Date) {
|
|
|
|
this.picker.date = new Date(this.value.getTime());
|
|
|
|
this.picker.resetView && this.picker.resetView();
|
|
|
|
} else {
|
|
|
|
this.picker.value = this.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.picker.ajustScrollTop && this.picker.ajustScrollTop();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|