Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
pithyone committed Feb 28, 2019
1 parent 219baf6 commit 1102e75
Show file tree
Hide file tree
Showing 20 changed files with 744 additions and 1,139 deletions.
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 王兵
Copyright (c) 2019 王兵

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
10 changes: 5 additions & 5 deletions gulpfile.js
Expand Up @@ -5,11 +5,11 @@ const config = require('./tools/config')
const BuildTask = require('./tools/build')
const id = require('./package.json').name || 'miniprogram-custom-component'

// build task instance
// 构建任务实例
// eslint-disable-next-line no-new
new BuildTask(id, config.entry)

// clean the generated folders and files
// 清空生成目录和文件
gulp.task('clean', gulp.series(() => gulp.src(config.distPath, {read: false, allowEmpty: true}).pipe(clean()), done => {
if (config.isDev) {
return gulp.src(config.demoDist, {read: false, allowEmpty: true})
Expand All @@ -18,9 +18,9 @@ gulp.task('clean', gulp.series(() => gulp.src(config.distPath, {read: false, all

return done()
}))
// watch files and build
// 监听文件变化并进行开发模式构建
gulp.task('watch', gulp.series(`${id}-watch`))
// build for develop
// 开发模式构建
gulp.task('dev', gulp.series(`${id}-dev`))
// build for publish
// 生产模式构建
gulp.task('default', gulp.series(`${id}-default`))
22 changes: 12 additions & 10 deletions package.json
Expand Up @@ -20,8 +20,7 @@
"testEnvironment": "jsdom",
"testURL": "https://jest.test",
"collectCoverageFrom": [
"src/**/*.js",
"!src/utils/calendar.js"
"src/**/*.js"
],
"moduleDirectories": [
"node_modules",
Expand All @@ -45,23 +44,26 @@
"babel-preset-env": "^1.7.0",
"colors": "^1.3.1",
"eslint": "^5.3.0",
"eslint-config-airbnb-base": "13.1.0",
"eslint-loader": "^2.1.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-node": "^7.0.1",
"eslint-plugin-promise": "^3.8.0",
"gulp": "^4.0.0",
"gulp-clean": "^0.4.0",
"gulp-if": "^2.0.2",
"gulp-install": "^1.1.0",
"gulp-less": "^3.5.0",
"gulp-less": "^4.0.1",
"gulp-rename": "^1.4.0",
"gulp-sourcemaps": "^2.6.4",
"j-component": "git+https://github.com/JuneAndGreen/j-component.git",
"jest": "^23.5.0",
"miniprogram-simulate": "latest",
"through2": "^2.0.3",
"vinyl": "^2.2.0",
"webpack": "^4.16.5",
"webpack-node-externals": "^1.7.2",
"eslint-config-airbnb-base": "13.1.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-node": "^7.0.1",
"eslint-plugin-promise": "^3.8.0"
"webpack-node-externals": "^1.7.2"
},
"dependencies": {}
"dependencies": {
"calendar": "github:pithyone/calendar.js"
}
}
31 changes: 15 additions & 16 deletions src/index.js
@@ -1,12 +1,11 @@
const calendar = require('utils/calendar')
const calendar = require('calendar')

const start = 1901
const years = Array.from(new Array(200), (val, index) => index + start + '年')
const months = Array.from(new Array(12), (val, index) => index + 1 + '月')
const chinaMonths = Array.from(new Array(12), (val, index) => calendar.toChinaMonth(index + 1))

Component({
externalClasses: ['picker-class'],
properties: {
value: {
type: String,
Expand All @@ -25,17 +24,17 @@ Component({
},
},
methods: {
_bindchange(e) {
_bindMultiPickerChange(e) {
this.setData({
multiIndex: e.detail.value,
})

const value = e.detail.value
const [y, m, d] = [value[0] + start, value[1] + 1, value[2] + 1]

this.triggerEvent('change', {value: y + '-' + this._fill(m) + '-' + this._fill(d)})
this.triggerEvent('change', {value: y + '-' + this._dateFormat(m) + '-' + this._dateFormat(d)})
},
_bindcolumnchange(e) {
_bindMultiPickerColumnChange(e) {
const multiArray = this.data.multiArray
const multiIndex = this.data.multiIndex

Expand All @@ -51,17 +50,17 @@ Component({
}

if (this.data.chinese) {
multiArray[2] = this._computedMonthDays(y, m)
multiArray[2] = this._monthDaysCompute(y, m)
} else {
multiArray[2] = this._computedSolarDays(y, m)
multiArray[2] = this._solarDaysCompute(y, m)
}

this.setData({
multiArray,
multiIndex,
})
},
_bindcancel() {
_bindMultiPickerCancel() {
this._init()
},
_init() {
Expand All @@ -71,40 +70,40 @@ Component({
[y, m, d] = this.data.value.split('-')
.map(Number)
} else {
const day = new Date();
[y, m, d] = [day.getFullYear(), day.getMonth() + 1, day.getDate()]
const lunar = calendar.solar2lunar();
[y, m, d] = [lunar.cYear, lunar.cMonth, lunar.cDay]
}

if (this.data.chinese) {
const day = this.data.value ? calendar.lunar2solar(y, m, d) : calendar.solar2lunar(y, m, d);
[y, m, d] = [day.lYear, day.lMonth, day.lDay]
const days = this._computedMonthDays(y, m)
const days = this._monthDaysCompute(y, m)
this.setData({
multiArray: [years, chinaMonths, days],
multiIndex: [y - start, m - 1, d - 1],
})
} else {
const days = this._computedSolarDays(y, m)
const days = this._solarDaysCompute(y, m)
this.setData({
multiArray: [years, months, days],
multiIndex: [y - start, m - 1, d - 1],
})
}
},
_computedSolarDays(y, m) {
_solarDaysCompute(y, m) {
const day = calendar.solarDays(y, m)
return Array.from(new Array(day), (val, index) => {
const date = index + 1
const lunar = calendar.solar2lunar(y, m, date)
return date + '日 ' + lunar.ncWeek
})
},
_computedMonthDays(y, m) {
_monthDaysCompute(y, m) {
const day = calendar.monthDays(y, m)
return Array.from(new Array(day), (val, index) => calendar.toChinaDay(index + 1))
},
_fill(d) {
return (d < 10 ? '0' : '') + d
_dateFormat(n) {
return (n < 10 ? '0' : '') + n
},
},
})
10 changes: 8 additions & 2 deletions src/index.wxml
@@ -1,4 +1,10 @@
<picker mode="multiSelector" range="{{multiArray}}" value="{{multiIndex}}" bindchange="_bindchange"
bindcolumnchange="_bindcolumnchange" bindcancel="_bindcancel" class="picker-class">
<picker
mode="multiSelector"
bindchange="_bindMultiPickerChange"
bindcolumnchange="_bindMultiPickerColumnChange"
bindcancel="_bindMultiPickerCancel"
value="{{multiIndex}}"
range="{{multiArray}}"
>
<slot></slot>
</picker>

0 comments on commit 1102e75

Please sign in to comment.