2
0
mirror of https://github.com/tenrok/vue2-datepicker.git synced 2026-06-25 06:30:36 +03:00

添加popup position 检测

This commit is contained in:
mengxiong10
2017-05-16 20:11:39 +08:00
parent c24a73ad62
commit b14834456b
6 changed files with 98 additions and 73 deletions
+2 -2
View File
@@ -20,7 +20,7 @@ export default {
data () { data () {
return { return {
value1: '2017-5-9', value1: '2017-5-9',
value2:'', value2: ''
} }
} }
} }
@@ -29,7 +29,7 @@ export default {
<style> <style>
.demo { .demo {
float:left; float:left;
margin:60px; margin:250px;
} }
.label{ .label{
margin-right: 1em; margin-right: 1em;
+7 -10
View File
@@ -29,14 +29,12 @@
</template> </template>
<script> <script>
import Languages from './languages.js'
export default { export default {
props: { props: {
startAt: null, startAt: null,
endAt: null, endAt: null,
value: null, value: null,
show: Boolean, show: Boolean
}, },
data () { data () {
const translation = this.$parent.translation const translation = this.$parent.translation
@@ -44,7 +42,7 @@ export default {
days: translation.days, days: translation.days,
months: translation.months, months: translation.months,
dates: [], dates: [],
now: new Date(), now: new Date()
} }
}, },
created () { created () {
@@ -58,9 +56,9 @@ export default {
}, },
value: { value: {
handler: 'updateNow', handler: 'updateNow',
immediate: true, immediate: true
}, },
now: 'updateCalendar', now: 'updateCalendar'
}, },
methods: { methods: {
updateNow () { updateNow () {
@@ -78,7 +76,7 @@ export default {
title: date.toLocaleDateString(), title: date.toLocaleDateString(),
date, date,
day, day,
classes, classes
} }
}) })
} }
@@ -154,8 +152,8 @@ export default {
return return
} }
this.$emit('input', cell.date) this.$emit('input', cell.date)
}, }
}, }
} }
</script> </script>
@@ -242,4 +240,3 @@ export default {
} }
</style> </style>
+51 -23
View File
@@ -16,6 +16,8 @@
@click="clickIcon" ></i> @click="clickIcon" ></i>
<div class="datepicker-popup" <div class="datepicker-popup"
:class="{'range':range}" :class="{'range':range}"
:style="position"
ref="calendar"
v-show="showPopup"> v-show="showPopup">
<template v-if="!range"> <template v-if="!range">
<calendar-panel v-model="currentValue" :show="showPopup"></calendar-panel> <calendar-panel v-model="currentValue" :show="showPopup"></calendar-panel>
@@ -40,29 +42,30 @@ export default {
props: { props: {
format: { format: {
type: String, type: String,
default: 'yyyy-MM-dd', default: 'yyyy-MM-dd'
}, },
range: { range: {
type: Boolean, type: Boolean,
default: false, default: false
}, },
width: { width: {
type: [String, Number], type: [String, Number],
default: 210, default: 210
}, },
placeholder: String, placeholder: String,
lang: { lang: {
type: String, type: String,
default: 'zh' default: 'zh'
}, },
value: null, value: null
}, },
data () { data () {
return { return {
showPopup: false, showPopup: false,
showCloseIcon: false, showCloseIcon: false,
currentValue: this.value, currentValue: this.value,
ranges: [], // 快捷选项 position: null,
ranges: [] // 快捷选项
} }
}, },
watch: { watch: {
@@ -74,13 +77,19 @@ export default {
this.currentValue = this.isValidRange(val) ? val : [undefined, undefined] this.currentValue = this.isValidRange(val) ? val : [undefined, undefined]
} }
}, },
immediate: true, immediate: true
}, },
currentValue (val) { currentValue (val) {
if ((!this.range && val) || (this.range && val[0] && val[1])) { if ((!this.range && val) || (this.range && val[0] && val[1])) {
this.$emit('input', val) this.$emit('input', val)
} }
}, },
showPopup (val) {
if (val) {
this.$nextTick(this.displayPopup)
// this.displayPopup()
}
}
}, },
computed: { computed: {
translation () { translation () {
@@ -97,10 +106,7 @@ export default {
return this.stringify(this.currentValue[0]) + ' ~ ' + this.stringify(this.currentValue[1]) return this.stringify(this.currentValue[0]) + ' ~ ' + this.stringify(this.currentValue[1])
} }
return '' return ''
}, }
},
created() {
this.initRanges()
}, },
methods: { methods: {
closePopup () { closePopup () {
@@ -138,7 +144,7 @@ export default {
'm+': date.getMinutes(), // 分 'm+': date.getMinutes(), // 分
's+': date.getSeconds(), // 秒 's+': date.getSeconds(), // 秒
'q+': Math.floor((date.getMonth() + 3) / 3), // 季度 'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
'S': date.getMilliseconds(), // 毫秒 'S': date.getMilliseconds() // 毫秒
} }
let str = fmt.replace(/[Yy]+/g, function (str) { let str = fmt.replace(/[Yy]+/g, function (str) {
return ('' + date.getFullYear()).slice(4 - str.length) return ('' + date.getFullYear()).slice(4 - str.length)
@@ -168,30 +174,51 @@ export default {
}, },
initRanges () { initRanges () {
this.ranges = [{ this.ranges = [{
text: '天', text: '未来7天',
start: new Date(), start: new Date(),
end: new Date(), end: new Date(Date.now() + 3600 * 1000 * 24 * 7)
}, { }, {
text: '未来一周', text: '未来30天',
start: new Date(),
end: new Date(Date.now() + 3600 * 1000 * 24 * 7),
}, {
text: '未来一个月',
start: new Date(), start: new Date(),
end: new Date(Date.now() + 3600 * 1000 * 24 * 30) end: new Date(Date.now() + 3600 * 1000 * 24 * 30)
}, { }, {
text: '最近一周', text: '最近7天',
start: new Date(Date.now() - 3600 * 1000 * 24 * 7), start: new Date(Date.now() - 3600 * 1000 * 24 * 7),
end: new Date(), end: new Date()
}, { }, {
text: '最近一个月', text: '最近30天',
start: new Date(Date.now() - 3600 * 1000 * 24 * 30), start: new Date(Date.now() - 3600 * 1000 * 24 * 30),
end: new Date(), end: new Date()
}] }]
this.ranges.forEach((v, i) => { this.ranges.forEach((v, i) => {
v.text = this.translation.pickers[i] v.text = this.translation.pickers[i]
}) })
}, },
displayPopup () {
console.log()
const dw = document.documentElement.clientWidth
const dh = document.documentElement.clientHeight
const InputRect = this.$el.getBoundingClientRect()
const PopupRect = this.$refs.calendar.getBoundingClientRect()
this.position = {}
if (dw - InputRect.left < PopupRect.width && InputRect.right < PopupRect.width) {
this.position.left = 1 - InputRect.left + 'px'
} else if (InputRect.left + InputRect.width / 2 <= dw / 2) {
this.position.left = 0
} else {
this.position.right = 0
}
if (InputRect.top <= PopupRect.height + 1 && dh - InputRect.bottom <= PopupRect.height + 1) {
this.position.top = dh - InputRect.top - PopupRect.height - 1 + 'px'
} else if (InputRect.top + InputRect.height / 2 <= dh / 2) {
this.position.top = '100%'
} else {
this.position.bottom = '100%'
}
}
},
created () {
this.initRanges()
}, },
directives: { directives: {
clickoutside: { clickoutside: {
@@ -205,7 +232,7 @@ export default {
}, },
unbind (el) { unbind (el) {
document.removeEventListener('click', el['@clickoutside'], true) document.removeEventListener('click', el['@clickoutside'], true)
}, }
} }
} }
} }
@@ -228,6 +255,7 @@ export default {
position: absolute; position: absolute;
width: 248px; width: 248px;
margin-top: 1px; margin-top: 1px;
margin-bottom: 1px;
border: 1px solid #d9d9d9; border: 1px solid #d9d9d9;
background-color: #fff; background-color: #fff;
box-shadow: 0 6px 12px rgba(0, 0, 0, .175); box-shadow: 0 6px 12px rgba(0, 0, 0, .175);
+3 -3
View File
@@ -2,7 +2,7 @@ export default {
'zh': { 'zh': {
'days': ['日', '一', '二', '三', '四', '五', '六'], 'days': ['日', '一', '二', '三', '四', '五', '六'],
'months': ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'], 'months': ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
'pickers': ['今天', '未来一周', '未来一个月', '最近一周', '最近一个月'], 'pickers': ['未来7天', '未来30天', '最近7天', '最近30天'],
'placeholder': { 'placeholder': {
'date': '请选择日期', 'date': '请选择日期',
'dateRange': '请选择日期范围' 'dateRange': '请选择日期范围'
@@ -11,10 +11,10 @@ export default {
'en': { 'en': {
'days': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], 'days': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
'months': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], 'months': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
'pickers': ['today', 'next week', 'next month', 'last week', 'last month'], 'pickers': ['next 7 days', 'next 30 days', 'previous 7 days', 'previous 30 days'],
'placeholder': { 'placeholder': {
'date': 'Select Date', 'date': 'Select Date',
'dateRange': 'Select Date Range' 'dateRange': 'Select Date Range'
} }
}, }
} }
+1 -1
View File
@@ -1,7 +1,7 @@
import Vue from 'vue' import Vue from 'vue'
import App from './App.vue' import App from './App.vue'
new Vue({ new Vue({ // eslint-disable-line
el: '#app', el: '#app',
render: h => h(App) render: h => h(App)
}) })