2016-07-27 06:15:02 +00:00
|
|
|
<template>
|
2016-12-08 16:06:34 +00:00
|
|
|
<el-input
|
2016-07-27 06:15:02 +00:00
|
|
|
class="el-date-editor"
|
2016-12-28 10:33:45 +00:00
|
|
|
:class="'el-date-editor--' + type"
|
2016-12-08 16:06:34 +00:00
|
|
|
:readonly="!editable || readonly"
|
|
|
|
:disabled="disabled"
|
|
|
|
:size="size"
|
2017-07-30 08:26:05 +00:00
|
|
|
:name="name"
|
2016-08-09 02:10:33 +00:00
|
|
|
v-clickoutside="handleClose"
|
2016-12-08 16:06:34 +00:00
|
|
|
:placeholder="placeholder"
|
|
|
|
@focus="handleFocus"
|
|
|
|
@blur="handleBlur"
|
|
|
|
@keydown.native="handleKeydown"
|
2017-01-03 09:38:44 +00:00
|
|
|
:value="displayValue"
|
|
|
|
@change.native="displayValue = $event.target.value"
|
2017-02-12 15:29:48 +00:00
|
|
|
:validateEvent="false"
|
2016-12-08 16:06:34 +00:00
|
|
|
ref="reference">
|
|
|
|
<i slot="icon"
|
|
|
|
class="el-input__icon"
|
|
|
|
@click="handleClickIcon"
|
2016-11-13 07:57:34 +00:00
|
|
|
:class="[showClose ? 'el-icon-close' : triggerClass]"
|
|
|
|
@mouseenter="handleMouseEnterIcon"
|
|
|
|
@mouseleave="showClose = false"
|
2016-07-27 06:15:02 +00:00
|
|
|
v-if="haveTrigger">
|
2016-12-08 16:06:34 +00:00
|
|
|
</i>
|
|
|
|
</el-input>
|
2016-07-27 06:15:02 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import Vue from 'vue';
|
2016-10-13 03:12:24 +00:00
|
|
|
import Clickoutside from 'element-ui/src/utils/clickoutside';
|
2017-01-03 09:38:44 +00:00
|
|
|
import { formatDate, parseDate, getWeekNumber, equalDate, isDate } 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';
|
2017-07-18 05:47:35 +00:00
|
|
|
import Focus from 'element-ui/src/mixins/focus';
|
2016-12-08 16:06:34 +00:00
|
|
|
import ElInput from 'element-ui/packages/input';
|
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 DEFAULT_FORMATS = {
|
|
|
|
date: 'yyyy-MM-dd',
|
|
|
|
month: 'yyyy-MM',
|
|
|
|
datetime: 'yyyy-MM-dd HH:mm:ss',
|
|
|
|
time: 'HH:mm:ss',
|
2017-03-24 07:44:23 +00:00
|
|
|
week: 'yyyywWW',
|
2016-07-27 06:15:02 +00:00
|
|
|
timerange: 'HH:mm:ss',
|
|
|
|
daterange: 'yyyy-MM-dd',
|
2016-11-26 15:19:08 +00:00
|
|
|
datetimerange: 'yyyy-MM-dd HH:mm:ss',
|
|
|
|
year: 'yyyy'
|
2016-07-27 06:15:02 +00:00
|
|
|
};
|
|
|
|
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) {
|
|
|
|
return parseDate(text, format);
|
|
|
|
};
|
2017-03-09 07:53:25 +00:00
|
|
|
const RANGE_FORMATTER = function(value, format, separator) {
|
2016-07-27 06:15:02 +00:00
|
|
|
if (Array.isArray(value) && value.length === 2) {
|
|
|
|
const start = value[0];
|
|
|
|
const end = value[1];
|
|
|
|
|
|
|
|
if (start && end) {
|
2017-03-09 07:53:25 +00:00
|
|
|
return formatDate(start, format) + separator + formatDate(end, format);
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
};
|
2017-03-09 07:53:25 +00:00
|
|
|
const RANGE_PARSER = function(text, format, separator) {
|
|
|
|
const array = text.split(separator);
|
2016-07-27 06:15:02 +00:00
|
|
|
if (array.length === 2) {
|
2016-12-13 14:51:43 +00:00
|
|
|
const range1 = array[0];
|
|
|
|
const range2 = array[1];
|
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
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: {
|
2017-03-24 07:44:23 +00:00
|
|
|
formatter(value, format) {
|
|
|
|
let date = formatDate(value, format);
|
|
|
|
const week = getWeekNumber(value);
|
|
|
|
|
|
|
|
date = /WW/.test(date)
|
|
|
|
? date.replace(/WW/, week < 10 ? '0' + week : week)
|
|
|
|
: date.replace(/W/, week);
|
|
|
|
return date;
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
|
|
|
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: {
|
2016-11-26 15:19:08 +00:00
|
|
|
formatter: DATE_FORMATTER,
|
|
|
|
parser: DATE_PARSER
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
|
|
|
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',
|
2017-07-20 03:27:01 +00:00
|
|
|
center: 'bottom',
|
2016-09-21 04:46:53 +00:00
|
|
|
right: 'bottom-end'
|
|
|
|
};
|
2016-07-27 06:15:02 +00:00
|
|
|
|
2017-05-04 10:11:47 +00:00
|
|
|
// only considers date-picker's value: Date or [Date, Date]
|
|
|
|
const valueEquals = function(a, b) {
|
|
|
|
const aIsArray = a instanceof Array;
|
|
|
|
const bIsArray = b instanceof Array;
|
|
|
|
if (aIsArray && bIsArray) {
|
|
|
|
return new Date(a[0]).getTime() === new Date(b[0]).getTime() &&
|
|
|
|
new Date(a[1]).getTime() === new Date(b[1]).getTime();
|
|
|
|
}
|
|
|
|
if (!aIsArray && !bIsArray) {
|
|
|
|
return new Date(a).getTime() === new Date(b).getTime();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
export default {
|
2017-07-18 05:47:35 +00:00
|
|
|
mixins: [Emitter, NewPopper, Focus('reference')],
|
2016-08-29 11:19:14 +00:00
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
props: {
|
2016-12-08 16:06:34 +00:00
|
|
|
size: String,
|
2016-07-27 06:15:02 +00:00
|
|
|
format: String,
|
|
|
|
readonly: Boolean,
|
|
|
|
placeholder: String,
|
2017-07-30 08:26:05 +00:00
|
|
|
name: String,
|
2016-11-13 03:45:38 +00:00
|
|
|
disabled: Boolean,
|
2016-12-26 15:02:40 +00:00
|
|
|
clearable: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true
|
|
|
|
},
|
2016-12-20 08:38:47 +00:00
|
|
|
popperClass: String,
|
2016-11-13 03:45:38 +00:00
|
|
|
editable: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true
|
|
|
|
},
|
2016-09-21 04:46:53 +00:00
|
|
|
align: {
|
|
|
|
type: String,
|
|
|
|
default: 'left'
|
|
|
|
},
|
2016-07-27 06:15:02 +00:00
|
|
|
value: {},
|
2017-04-14 15:33:20 +00:00
|
|
|
defaultValue: {},
|
2017-01-23 07:35:06 +00:00
|
|
|
rangeSeparator: {
|
|
|
|
default: ' - '
|
|
|
|
},
|
2016-07-27 06:15:02 +00:00
|
|
|
pickerOptions: {}
|
|
|
|
},
|
|
|
|
|
2016-12-08 16:06:34 +00:00
|
|
|
components: { ElInput },
|
|
|
|
|
2016-10-20 12:12:21 +00:00
|
|
|
directives: { Clickoutside },
|
2016-07-27 06:15:02 +00:00
|
|
|
|
|
|
|
data() {
|
|
|
|
return {
|
2016-11-13 07:57:34 +00:00
|
|
|
pickerVisible: false,
|
|
|
|
showClose: false,
|
2017-04-28 04:28:09 +00:00
|
|
|
currentValue: '',
|
|
|
|
unwatchPickerOptions: null
|
2016-07-27 06:15:02 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
watch: {
|
|
|
|
pickerVisible(val) {
|
2017-02-12 15:29:48 +00:00
|
|
|
if (!val) this.dispatch('ElFormItem', 'el.form.blur');
|
2016-11-13 03:45:38 +00:00
|
|
|
if (this.readonly || this.disabled) return;
|
2016-10-02 10:31:29 +00:00
|
|
|
val ? this.showPicker() : this.hidePicker();
|
2016-08-29 11:19:14 +00:00
|
|
|
},
|
2017-01-03 09:38:44 +00:00
|
|
|
currentValue(val) {
|
|
|
|
if (val) return;
|
|
|
|
if (this.picker && typeof this.picker.handleClear === 'function') {
|
2016-11-07 08:26:03 +00:00
|
|
|
this.picker.handleClear();
|
2017-01-03 09:38:44 +00:00
|
|
|
} else {
|
|
|
|
this.$emit('input');
|
2016-11-07 08:26:03 +00:00
|
|
|
}
|
2016-11-13 07:57:34 +00:00
|
|
|
},
|
|
|
|
value: {
|
|
|
|
immediate: true,
|
|
|
|
handler(val) {
|
2017-01-03 09:38:44 +00:00
|
|
|
this.currentValue = isDate(val) ? new Date(val) : val;
|
2016-11-13 07:57:34 +00:00
|
|
|
}
|
2017-01-03 09:38:44 +00:00
|
|
|
},
|
|
|
|
displayValue(val) {
|
|
|
|
this.$emit('change', val);
|
2017-02-12 15:29:48 +00:00
|
|
|
this.dispatch('ElFormItem', 'el.form.change');
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
computed: {
|
2016-12-08 16:06:34 +00:00
|
|
|
reference() {
|
|
|
|
return this.$refs.reference.$el;
|
|
|
|
},
|
|
|
|
|
|
|
|
refInput() {
|
|
|
|
if (this.reference) return this.reference.querySelector('input');
|
|
|
|
return {};
|
|
|
|
},
|
|
|
|
|
2016-11-13 07:57:34 +00:00
|
|
|
valueIsEmpty() {
|
2017-01-03 09:38:44 +00:00
|
|
|
const val = this.currentValue;
|
2016-11-13 07:57:34 +00:00
|
|
|
if (Array.isArray(val)) {
|
2016-12-08 16:06:34 +00:00
|
|
|
for (let i = 0, len = val.length; i < len; i++) {
|
2016-11-13 07:57:34 +00:00
|
|
|
if (val[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (val) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
triggerClass() {
|
|
|
|
return this.type.indexOf('time') !== -1 ? 'el-icon-time' : 'el-icon-date';
|
|
|
|
},
|
|
|
|
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
2017-01-03 09:38:44 +00:00
|
|
|
displayValue: {
|
2016-07-27 06:15:02 +00:00
|
|
|
get() {
|
2017-01-03 09:38:44 +00:00
|
|
|
const value = this.currentValue;
|
2016-12-08 16:06:34 +00:00
|
|
|
if (!value) return;
|
2016-07-27 06:15:02 +00:00
|
|
|
const formatter = (
|
|
|
|
TYPE_VALUE_RESOLVER_MAP[this.type] ||
|
|
|
|
TYPE_VALUE_RESOLVER_MAP['default']
|
|
|
|
).formatter;
|
|
|
|
const format = DEFAULT_FORMATS[this.type];
|
|
|
|
|
2017-03-09 07:53:25 +00:00
|
|
|
return formatter(value, this.format || format, this.rangeSeparator);
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set(value) {
|
|
|
|
if (value) {
|
|
|
|
const type = this.type;
|
|
|
|
const parser = (
|
|
|
|
TYPE_VALUE_RESOLVER_MAP[type] ||
|
|
|
|
TYPE_VALUE_RESOLVER_MAP['default']
|
|
|
|
).parser;
|
2017-03-09 07:53:25 +00:00
|
|
|
const parsedValue = parser(value, this.format || DEFAULT_FORMATS[type], this.rangeSeparator);
|
2016-07-27 06:15:02 +00:00
|
|
|
|
2016-12-19 07:31:31 +00:00
|
|
|
if (parsedValue && this.picker) {
|
2016-10-20 12:12:21 +00:00
|
|
|
this.picker.value = parsedValue;
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
2016-12-23 08:44:39 +00:00
|
|
|
} else {
|
2017-04-19 10:05:18 +00:00
|
|
|
this.$emit('input', value);
|
2016-12-23 08:44:39 +00:00
|
|
|
this.picker.value = value;
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
2016-12-23 09:34:40 +00:00
|
|
|
this.$forceUpdate();
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-10-12 09:41:49 +00:00
|
|
|
created() {
|
|
|
|
// vue-popper
|
2017-01-18 09:17:59 +00:00
|
|
|
this.popperOptions = {
|
2016-10-12 09:41:49 +00:00
|
|
|
boundariesPadding: 0,
|
|
|
|
gpuAcceleration: false
|
|
|
|
};
|
|
|
|
this.placement = PLACEMENT_MAP[this.align] || PLACEMENT_MAP.left;
|
|
|
|
},
|
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
methods: {
|
2016-11-13 07:57:34 +00:00
|
|
|
handleMouseEnterIcon() {
|
2016-11-24 06:14:02 +00:00
|
|
|
if (this.readonly || this.disabled) return;
|
2016-12-26 15:02:40 +00:00
|
|
|
if (!this.valueIsEmpty && this.clearable) {
|
2016-11-13 07:57:34 +00:00
|
|
|
this.showClose = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleClickIcon() {
|
2016-11-24 06:14:02 +00:00
|
|
|
if (this.readonly || this.disabled) return;
|
2016-12-26 15:02:40 +00:00
|
|
|
if (this.showClose) {
|
2017-05-03 09:32:38 +00:00
|
|
|
this.currentValue = this.$options.defaultValue || '';
|
2017-01-03 09:38:44 +00:00
|
|
|
this.showClose = false;
|
2016-12-26 15:02:40 +00:00
|
|
|
} else {
|
2016-11-13 07:57:34 +00:00
|
|
|
this.pickerVisible = !this.pickerVisible;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-01-03 09:38:44 +00:00
|
|
|
dateChanged(dateA, dateB) {
|
|
|
|
if (Array.isArray(dateA)) {
|
|
|
|
let len = dateA.length;
|
|
|
|
if (!dateB) return true;
|
|
|
|
while (len--) {
|
|
|
|
if (!equalDate(dateA[len], dateB[len])) return true;
|
|
|
|
}
|
2016-11-13 07:57:34 +00:00
|
|
|
} else {
|
2017-01-03 09:38:44 +00:00
|
|
|
if (!equalDate(dateA, dateB)) return true;
|
2016-11-13 07:57:34 +00:00
|
|
|
}
|
2016-12-23 08:44:39 +00:00
|
|
|
|
2017-01-03 09:38:44 +00:00
|
|
|
return false;
|
2016-11-13 07:57:34 +00:00
|
|
|
},
|
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
|
|
|
handleKeydown(event) {
|
|
|
|
const keyCode = event.keyCode;
|
|
|
|
|
2017-05-29 13:01:00 +00:00
|
|
|
// TAB or ESC
|
|
|
|
if (keyCode === 9 || keyCode === 27) {
|
2016-10-13 08:11:06 +00:00
|
|
|
this.pickerVisible = false;
|
2017-05-29 13:01:00 +00:00
|
|
|
event.stopPropagation();
|
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() {
|
2016-12-26 02:45:20 +00:00
|
|
|
if (this.$isServer) return;
|
2016-07-27 06:15:02 +00:00
|
|
|
if (!this.picker) {
|
2017-04-28 04:28:09 +00:00
|
|
|
this.mountPicker();
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
2017-04-28 04:28:09 +00:00
|
|
|
this.pickerVisible = this.picker.visible = true;
|
2016-07-27 06:15:02 +00:00
|
|
|
|
2016-10-12 09:41:49 +00:00
|
|
|
this.updatePopper();
|
2016-07-27 06:15:02 +00:00
|
|
|
|
2017-01-03 09:38:44 +00:00
|
|
|
if (this.currentValue instanceof Date) {
|
|
|
|
this.picker.date = new Date(this.currentValue.getTime());
|
2016-07-27 06:15:02 +00:00
|
|
|
} else {
|
2017-01-03 09:38:44 +00:00
|
|
|
this.picker.value = this.currentValue;
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
2016-12-08 16:06:34 +00:00
|
|
|
this.picker.resetView && this.picker.resetView();
|
2016-07-27 06:15:02 +00:00
|
|
|
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.picker.ajustScrollTop && this.picker.ajustScrollTop();
|
|
|
|
});
|
2017-04-28 04:28:09 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
mountPicker() {
|
2017-05-03 09:32:38 +00:00
|
|
|
this.panel.defaultValue = this.defaultValue || this.currentValue;
|
2017-04-28 04:28:09 +00:00
|
|
|
this.picker = new Vue(this.panel).$mount();
|
|
|
|
this.picker.popperClass = this.popperClass;
|
|
|
|
this.popperElm = this.picker.$el;
|
|
|
|
this.picker.width = this.reference.getBoundingClientRect().width;
|
|
|
|
this.picker.showTime = this.type === 'datetime' || this.type === 'datetimerange';
|
|
|
|
this.picker.selectionMode = this.selectionMode;
|
|
|
|
if (this.format) {
|
|
|
|
this.picker.format = this.format;
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateOptions = () => {
|
|
|
|
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, this.rangeSeparator));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const option in options) {
|
|
|
|
if (options.hasOwnProperty(option) &&
|
|
|
|
// 忽略 time-picker 的该配置项
|
|
|
|
option !== 'selectableRange') {
|
|
|
|
this.picker[option] = options[option];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
updateOptions();
|
|
|
|
this.unwatchPickerOptions = this.$watch('pickerOptions', () => updateOptions(), { deep: true });
|
|
|
|
|
|
|
|
this.$el.appendChild(this.picker.$el);
|
|
|
|
this.picker.resetView && this.picker.resetView();
|
|
|
|
|
|
|
|
this.picker.$on('dodestroy', this.doDestroy);
|
|
|
|
this.picker.$on('pick', (date = '', visible = false) => {
|
2017-05-04 10:11:47 +00:00
|
|
|
// do not emit if values are same
|
|
|
|
if (!valueEquals(this.value, date)) {
|
|
|
|
this.$emit('input', date);
|
|
|
|
}
|
2017-04-28 04:28:09 +00:00
|
|
|
this.pickerVisible = this.picker.visible = visible;
|
|
|
|
this.picker.resetView && this.picker.resetView();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.picker.$on('select-range', (start, end) => {
|
|
|
|
this.refInput.setSelectionRange(start, end);
|
|
|
|
this.refInput.focus();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
unmountPicker() {
|
|
|
|
if (this.picker) {
|
|
|
|
this.picker.$destroy();
|
|
|
|
this.picker.$off();
|
|
|
|
if (typeof this.unwatchPickerOptions === 'function') {
|
|
|
|
this.unwatchPickerOptions();
|
|
|
|
}
|
|
|
|
this.picker.$el.parentNode.removeChild(this.picker.$el);
|
|
|
|
}
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|