From c0b0f7b6ea5a8c79f3ebc57fff144a198b1d38fa Mon Sep 17 00:00:00 2001 From: dolymood Date: Fri, 2 Mar 2018 12:05:58 +0800 Subject: [PATCH] fix issue #8 --- src/core/datetime/index.js | 10 ++++++++++ src/panel/days.js | 1 - src/panel/scroller.js | 8 +++++--- src/panel/time.js | 12 ++++++++---- src/picker/index.js | 3 +-- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/core/datetime/index.js b/src/core/datetime/index.js index c2fe7b8..7aff780 100644 --- a/src/core/datetime/index.js +++ b/src/core/datetime/index.js @@ -22,8 +22,18 @@ var config = { value: 'month', rows: function (dateTime) { var rows = [] + var now = dateTime.now + var nowYear = now.getFullYear() + var minYear = dateTime.options.min.getFullYear() var minMonth = dateTime.options.min.getMonth() + var maxYear = dateTime.options.max.getFullYear() var maxMonth = dateTime.options.max.getMonth() + if (nowYear > minYear) { + minMonth = 0 + } + if (maxYear > nowYear) { + maxMonth = 11 + } var start = minMonth while (start <= maxMonth) { rows.push([start++]) diff --git a/src/panel/days.js b/src/panel/days.js index efca296..6cfa02a 100644 --- a/src/panel/days.js +++ b/src/panel/days.js @@ -15,7 +15,6 @@ utils.extend(DaysPanel.prototype, { } this._renderHead() this.main.innerHTML = buildCalendar(this.picker.prevDateTime, this.picker.config, 'prev') + buildCalendar(this.picker.dateTime, this.picker.config, 'curr') + buildCalendar(this.picker.nextDateTime, this.picker.config, 'next') - this.afterRender() }, _renderHead: function () { this.picker.head.innerHTML = ( diff --git a/src/panel/scroller.js b/src/panel/scroller.js index dfbc013..4c3b261 100644 --- a/src/panel/scroller.js +++ b/src/panel/scroller.js @@ -16,12 +16,12 @@ function ScrollerPanel (picker) { utils.extend(ScrollerPanel.prototype, { render: function () { + this.rows = this.picker.dateTime.getRows() if (!this.main) { this._init() } this._renderHead() this.main.innerHTML = CONFIG[this.type](this.picker.dateTime, this.rows) - this._afterRender() }, _renderHead: function () { var isYears = this.type === 'C' @@ -35,15 +35,17 @@ utils.extend(ScrollerPanel.prototype, { '' ) }, - _afterRender: function () { + afterRender: function () { var activeEle = this.main.querySelector('.picker-active') + if (!activeEle) { + return + } this.itemHeight = activeEle.offsetHeight this.activeEle = activeEle var t = activeEle.offsetTop - (this.contentHeight - this.itemHeight) / 2 this._slideTo(-t, 0) }, _init: function () { - this.rows = this.picker.dateTime.getRows() this._initMain() this._initMidd() }, diff --git a/src/panel/time.js b/src/panel/time.js index bd25017..6f6595b 100644 --- a/src/panel/time.js +++ b/src/panel/time.js @@ -20,6 +20,7 @@ function TimePanel (picker) { utils.extend(TimePanel.prototype, { render: function () { + this.rows = this.picker.dateTime.getRows() if (!this.main) { this._init() } @@ -33,15 +34,15 @@ utils.extend(TimePanel.prototype, { '' ) this.main.innerHTML = CONFIG[this.type](this.picker.dateTime, this.rows) - this.afterRender() }, afterRender: function () { + this.headHourEle = this.picker.head.querySelector('.picker-hour') + this.headMinuteEle = this.picker.head.querySelector('.picker-minute') this.headActiveEle = this.picker.head.querySelector('.picker-head-active') this.activeEle = this.main.querySelector('.picker-active') this.pickerLineEle = this.main.querySelector('.time-picker-line') }, _init: function () { - this.rows = this.picker.dateTime.getRows() this._initMain() }, _initMain: function () { @@ -58,8 +59,11 @@ utils.extend(TimePanel.prototype, { this.mainStyle.display = 'none' }, selfChange: function () { + var h = utils.formatDate(this.picker.dateTime.now, 'HH') + var m = utils.formatDate(this.picker.dateTime.now, 'mm') + this.headHourEle.innerHTML = h + this.headMinuteEle.innerHTML = m var v = this.picker.dateTime.getLevelValue() - this.headActiveEle.innerHTML = utils.pad(v, 2) var newActiveEle = this.main.querySelector('.picker-cell[data-val="' + v + '"]') this.activeEle && this.activeEle.classList.remove('picker-active') newActiveEle.classList.add('picker-active') @@ -93,7 +97,7 @@ utils.extend(TimePanel.prototype, { }, destroy: function () { this.picker.content.removeChild(this.main) - utils.set2Null(['picker', 'main', 'mainStyle', 'headActiveEle', 'activeEle', 'pickerLineEle', '__move', '__end'], this) + utils.set2Null(['picker', 'main', 'mainStyle', 'headHourEle', 'headMinuteEle', 'headActiveEle', 'activeEle', 'pickerLineEle', '__move', '__end'], this) } }) diff --git a/src/picker/index.js b/src/picker/index.js index 69f35f8..33fea4c 100644 --- a/src/picker/index.js +++ b/src/picker/index.js @@ -77,7 +77,6 @@ utils.extend(pickerPro, { this.options.default = now this.setDateTime() }, - afterRender: utils.noop, _setDateTime: utils.noop, init: utils.noop, _init: function () { @@ -316,7 +315,7 @@ utils.extend(pickerPro, { } }, events) -var es = ['selfChange', 'render', '__start', '__move', '__end'] +var es = ['selfChange', 'render', 'afterRender', '__start', '__move', '__end'] es.forEach(function (name) { pickerPro[name] = function () { this.panel && this.panel[name] && this.panel[name].apply(this.panel, arguments)