miniprogram-xigua-datepicker
Version:
miniprogram datepicker
422 lines (394 loc) • 14.8 kB
JavaScript
module.exports =
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var createNum = function createNum(num) {
var weight = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
var result = [];
while (result.length < num) {
result.push(result.length + weight);
}
return result;
};
Component({
data: {
show: false,
start: {
currentDate: '',
currentIndex: [],
years: [],
months: createNum(12, 1),
days: createNum(31, 1),
hours: createNum(24),
minutes: createNum(60)
},
end: {
currentDate: '',
currentIndex: [],
years: [],
months: createNum(12, 1),
days: createNum(31, 1),
hours: createNum(24),
minutes: createNum(60)
}
},
methods: {
open: function open() {
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var defaultOpts = this._getDefaultOpts(opts);
this.opts = Object.assign(defaultOpts, opts);
if (this.data.start.years.length === 0) {
this.createColumnYeays();
}
this.setViewDataFromOpts(this.opts);
this.setValue(opts.value);
this.setData({ show: true });
},
setViewDataFromOpts: function setViewDataFromOpts(option) {
this.setData({
title: option.title,
zIndex: option.zIndex,
confirmColor: option.confirmColor,
showTime: option.showTime,
range: option.range
});
},
setValue: function setValue(value) {
var _this = this;
value = value || [this._getFormatDate(null, true), this._getFormatDate(null, true)];
if (typeof value === 'string') {
value = [value];
}
var startDate = this._moment(value[0]);
var startYearIndex = this.data.start.years.findIndex(function (e) {
return e === startDate.getFullYear();
});
var startMonthIndex = this.data.start.months.findIndex(function (e) {
return e === startDate.getMonth() + 1;
});
var staryDayIndex = this.data.start.days.findIndex(function (e) {
return e === startDate.getDate();
});
var startHourIndex = this.data.start.hours.findIndex(function (e) {
return e === startDate.getHours();
});
var startMinuteIndex = this.data.start.minutes.findIndex(function (e) {
return e === startDate.getMinutes();
});
var endDate = value[1] ? this._moment(value[1]) : new Date();
var endYearIndex = this.data.end.years.findIndex(function (e) {
return e === endDate.getFullYear();
});
var endMonthIndex = this.data.end.months.findIndex(function (e) {
return e === endDate.getMonth() + 1;
});
var endDayIndex = this.data.end.days.findIndex(function (e) {
return e === endDate.getDate();
});
var endHourIndex = this.data.end.hours.findIndex(function (e) {
return e === endDate.getHours();
});
var endtMinuteIndex = this.data.end.minutes.findIndex(function (e) {
return e === endDate.getMinutes();
});
wx.nextTick(function () {
_this.setData({
start: Object.assign(_this.data.start, {
currentDate: _this._getFormatDate(startDate, _this.data.showTime),
currentIndex: [startYearIndex, startMonthIndex, staryDayIndex, startHourIndex, startMinuteIndex],
days: _this.createColumnDays(startDate.getMonth() + 1)
}),
end: Object.assign(_this.data.end, {
currentDate: _this._getFormatDate(endDate, _this.data.showTime),
currentIndex: [endYearIndex, endMonthIndex, endDayIndex, endHourIndex, endtMinuteIndex],
days: _this.createColumnDays(endDate.getMonth() + 1)
})
});
});
},
handleConfirm: function handleConfirm() {
var data = this.getSubmitData();
if (!this.opts.beforeHook(data)) return;
this.setData({ show: false });
this.opts.onChange(data);
},
handleCancel: function handleCancel() {
this.setData({ show: false });
this.opts.onCancel();
},
createColumnYeays: function createColumnYeays() {
var currentYear = new Date().getFullYear();
var years = [currentYear];
for (var i = 0; i < 100; i++) {
years.unshift(currentYear - i - 1);
years.push(currentYear + i + 1);
}
this.data.start.years = years;
this.data.end.years = years;
this.setData({
start: this.data.start,
end: this.data.end
});
},
createColumnDays: function createColumnDays(month) {
// ---创建天
var maxDay = 31;
if (month === 2) {
if (this.isRunNian()) {
maxDay = 29;
} else {
maxDay = 28;
}
} else if ([4, 6, 9, 11].indexOf(month) > -1) {
maxDay = 30;
}
return createNum(maxDay, 1);
},
isRunNian: function isRunNian() {
// ---返回是否闰年
var currentYear = new Date().getFullYear;
if (currentYear % 400 === 0) {
return true;
}
if (currentYear % 100 !== 0 && currentYear % 4 === 0) {
return true;
}
return false;
},
handleStartChange: function handleStartChange(ev) {
// ---监听开始时间改变
var index = ev.detail.value;
var start = this.data.start;
// 是否正常范围,不正常需要把当前值改为正常
if (!this._isValidDate(ev, 'start') && this.data.range) {
wx.showToast({
title: '截止时间不能小于开始时间',
icon: 'none'
});
this.setData({ start: this.data.start });
return;
}
// 判断是否滚动了年或月 , 是的话需要重新生成日
if (this.data.start.currentIndex[0] !== index[0] || this.data.start.currentIndex[1] !== index[1]) {
var month = this.data.start.months[index[1]];
var days = this.createColumnDays(month);
start.days = days;
}
// 赋值当前的日期和下标
start.currentIndex = index;
start.currentDate = start.years[index[0]] + '-' + this._padStart(start.months[index[1]], 2, '0') + '-' + this._padStart(start.days[index[2]], 2, '0') + (this.data.showTime ? ' ' + this._padStart(start.hours[index[3]], 2, '0') + ':' + this._padStart(start.minutes[index[4]], 2, '0') : '');
this.setData({ start: start });
this.opts.onScroll('start', this.getSubmitData());
},
getSubmitData: function getSubmitData() {
// let start = null
// let end = null
if (this.data.range) {
return [this.data.start.currentDate, this.data.end.currentDate];
} else {
return this.data.start.currentDate;
}
},
handleEndChange: function handleEndChange(ev) {
// ---监听结束时间改变
var index = ev.detail.value;
var end = this.data.end;
// 是否正常范围,不正常需要把当前值改为正常
if (!this._isValidDate(ev, 'end') && this.data.range) {
wx.showToast({
title: '截止时间不能小于开始时间',
icon: 'none'
});
this.setData({ end: this.data.end });
return;
}
if (this.data.end.currentIndex[0] !== index[0] || this.data.end.currentIndex[1] !== index[1]) {
var month = this.data.end.months[index[1]];
var days = this.createColumnDays(month);
end.days = days;
}
this.data.end.currentIndex = index;
this.data.end.currentDate = end.years[index[0]] + '-' + this._padStart(end.months[index[1]], 2, '0') + '-' + this._padStart(end.days[index[2]], 2, '0') + (this.data.showTime ? ' ' + this._padStart(end.hours[index[3] || 0], 2, '0') + ':' + this._padStart(end.minutes[index[4] || 0], 2, '0') : '');
this.setData({ end: end });
this.opts.onScroll('end', this.getSubmitData());
},
_moment: function _moment(time) {
if (typeof time === 'number' || (typeof time === 'undefined' ? 'undefined' : _typeof(time)) === 'object') {
return new Date(time);
}
if (typeof time === 'string') {
if (time.length === 13) {
time = parseFloat(time);
return new Date(time);
}
time = time.replace(/-/g, ':').replace(' ', ':');
time = time.replace(/\//g, ':').replace(' ', ':');
time = time.split(':');
for (var i = time.length; i < 6; i += 1) {
time.push('0');
}
return new Date(time[0], time[1] - 1, time[2], time[3], time[4], time[5]);
}
return new Error('请检查time的数据格式');
},
_padStart: function _padStart(val, length, fillString) {
if (val === undefined) {
return null;
}
val += '';
while (val.length < length) {
val = fillString + val;
}
return val;
},
_isValidDate: function _isValidDate(ev, type) {
// ---对比时间范围是否正常
var currentIndex = ev.detail.value;
if (type === 'start') {
var oldEndIndex = this.data.end.currentIndex;
var result = true;
for (var i = 0; i < currentIndex.length; i++) {
if (currentIndex[i] > oldEndIndex[i]) {
result = false;
break;
}
if (currentIndex[i] < oldEndIndex[i]) {
break;
}
}
return result;
} else {
var oldStartIndex = this.data.start.currentIndex;
var _result = true;
for (var _i = 0; _i < currentIndex.length; _i++) {
if (oldStartIndex[_i] > currentIndex[_i]) {
_result = false;
break;
}
if (oldStartIndex[_i] < currentIndex[_i]) {
break;
}
}
return _result;
}
},
_getFormatDate: function _getFormatDate(date, showTime) {
// --- 格式化日期
date = date || new Date();
var year = date.getFullYear();
var month = this._padStart(date.getMonth() + 1, 2, '0');
var day = this._padStart(date.getDate(), 2, '0');
var hour = this._padStart(date.getHours(), 2, '0');
var minute = this._padStart(date.getMinutes(), 2, '0');
return year + '-' + month + '-' + day + (showTime ? ' ' + hour + ':' + minute : '');
},
_getDefaultOpts: function _getDefaultOpts() {
// --- 默认的参数配置
return {
title: '选择日期',
range: true,
showTime: true,
zIndex: 1,
confirmColor: '#e74845',
beforeHook: function beforeHook() {
return true;
},
onChange: function onChange() {
console.warn('未传入日期监听函数');
},
onScroll: function onScroll() {},
onCancel: function onCancel() {}
};
}
}
});
/***/ })
/******/ ]);