index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /**
  2. * 上滑
  3. * @param {object} e 事件对象
  4. * @returns {boolean} 布尔值
  5. */
  6. export function isUpSlide(e) {
  7. const { startX, startY } = this.data.gesture;
  8. if (this.slideLock) {
  9. const t = e.touches[0];
  10. const deltaX = t.clientX - startX;
  11. const deltaY = t.clientY - startY;
  12. if (deltaY < -60 && deltaX < 20 && deltaX > -20) {
  13. this.slideLock = false;
  14. return true;
  15. } else {
  16. return false;
  17. }
  18. }
  19. }
  20. /**
  21. * 下滑
  22. * @param {object} e 事件对象
  23. * @returns {boolean} 布尔值
  24. */
  25. export function isDownSlide(e) {
  26. const { startX, startY } = this.data.gesture;
  27. if (this.slideLock) {
  28. const t = e.touches[0];
  29. const deltaX = t.clientX - startX;
  30. const deltaY = t.clientY - startY;
  31. if (deltaY > 60 && deltaX < 20 && deltaX > -20) {
  32. this.slideLock = false;
  33. return true;
  34. } else {
  35. return false;
  36. }
  37. }
  38. }
  39. /**
  40. * 左滑
  41. * @param {object} e 事件对象
  42. * @returns {boolean} 布尔值
  43. */
  44. export function isLeftSlide(e) {
  45. const { startX, startY } = this.data.gesture;
  46. if (this.slideLock) {
  47. const t = e.touches[0];
  48. const deltaX = t.clientX - startX;
  49. const deltaY = t.clientY - startY;
  50. if (deltaX < -60 && deltaY < 20 && deltaY > -20) {
  51. this.slideLock = false;
  52. return true;
  53. } else {
  54. return false;
  55. }
  56. }
  57. }
  58. /**
  59. * 右滑
  60. * @param {object} e 事件对象
  61. * @returns {boolean} 布尔值
  62. */
  63. export function isRightSlide(e) {
  64. const { startX, startY } = this.data.gesture;
  65. if (this.slideLock) {
  66. const t = e.touches[0];
  67. const deltaX = t.clientX - startX;
  68. const deltaY = t.clientY - startY;
  69. if (deltaX > 60 && deltaY < 20 && deltaY > -20) {
  70. this.slideLock = false;
  71. return true;
  72. } else {
  73. return false;
  74. }
  75. }
  76. }
  77. const conf = {
  78. /**
  79. * 计算指定月份共多少天
  80. * @param {number} year 年份
  81. * @param {number} month 月份
  82. */
  83. getThisMonthDays(year, month) {
  84. return new Date(year, month, 0).getDate();
  85. },
  86. /**
  87. * 计算指定月份第一天星期几
  88. * @param {number} year 年份
  89. * @param {number} month 月份
  90. */
  91. getFirstDayOfWeek(year, month) {
  92. return new Date(Date.UTC(year, month - 1, 1)).getDay();
  93. },
  94. /**
  95. * 计算当前月份前后两月应占的格子
  96. * @param {number} year 年份
  97. * @param {number} month 月份
  98. */
  99. calculateEmptyGrids(year, month) {
  100. conf.calculatePrevMonthGrids.call(this, year, month);
  101. conf.calculateNextMonthGrids.call(this, year, month);
  102. },
  103. /**
  104. * 计算上月应占的格子
  105. * @param {number} year 年份
  106. * @param {number} month 月份
  107. */
  108. calculatePrevMonthGrids(year, month) {
  109. const prevMonthDays = conf.getThisMonthDays(year, month - 1);
  110. const firstDayOfWeek = conf.getFirstDayOfWeek(year, month);
  111. let empytGrids = [];
  112. if (firstDayOfWeek > 0) {
  113. const len = prevMonthDays - firstDayOfWeek;
  114. for (let i = prevMonthDays; i > len; i--) {
  115. empytGrids.push(i);
  116. }
  117. this.setData({
  118. 'datepicker.empytGrids': empytGrids.reverse()
  119. });
  120. } else {
  121. this.setData({
  122. 'datepicker.empytGrids': null
  123. });
  124. }
  125. },
  126. /**
  127. * 计算下月应占的格子
  128. * @param {number} year 年份
  129. * @param {number} month 月份
  130. */
  131. calculateNextMonthGrids(year, month) {
  132. const thisMonthDays = conf.getThisMonthDays(year, month);
  133. const lastDayWeek = new Date(`${year}-${month}-${thisMonthDays}`).getDay();
  134. let lastEmptyGrids = [];
  135. if (+lastDayWeek !== 6) {
  136. const len = 7 - (lastDayWeek + 1);
  137. for (let i = 1; i <= len; i++) {
  138. lastEmptyGrids.push(i);
  139. }
  140. this.setData({
  141. 'datepicker.lastEmptyGrids': lastEmptyGrids
  142. });
  143. } else {
  144. this.setData({
  145. 'datepicker.lastEmptyGrids': null
  146. });
  147. }
  148. },
  149. /**
  150. * 设置日历面板数据
  151. * @param {number} year 年份
  152. * @param {number} month 月份
  153. */
  154. calculateDays(year, month, curDate) {
  155. const { todayTimestamp } = this.data.datepicker;
  156. let days = [];
  157. let day;
  158. let selectMonth;
  159. let selectYear;
  160. const thisMonthDays = conf.getThisMonthDays(year, month);
  161. const selectedDay = this.data.datepicker.selectedDay;
  162. if (selectedDay && selectedDay.length) {
  163. day = selectedDay[0].day;
  164. selectMonth = selectedDay[0].month;
  165. selectYear = selectedDay[0].year;
  166. }
  167. for (let i = 1; i <= thisMonthDays; i++) {
  168. days.push({
  169. day: i,
  170. choosed: curDate
  171. ? i === curDate
  172. : year === selectYear && month === selectMonth && i === day,
  173. year,
  174. month
  175. });
  176. }
  177. days.map(item => {
  178. const timestamp = new Date(
  179. `${item.year}-${item.month}-${item.day}`
  180. ).getTime();
  181. if (this.config.disablePastDay && timestamp - todayTimestamp < 0) {
  182. item.disable = true;
  183. }
  184. });
  185. const tmp = {
  186. 'datepicker.days': days
  187. };
  188. if (curDate) {
  189. tmp['datepicker.selectedDay'] = [
  190. {
  191. day: curDate,
  192. choosed: true,
  193. year,
  194. month
  195. }
  196. ];
  197. }
  198. this.setData(tmp);
  199. },
  200. /**
  201. * 跳转至今天
  202. */
  203. jumpToToday() {
  204. const date = new Date();
  205. const curYear = date.getFullYear();
  206. const curMonth = date.getMonth() + 1;
  207. const curDate = date.getDate();
  208. conf.renderCalendar.call(this, curYear, curMonth, curDate);
  209. },
  210. /**
  211. * 渲染日历
  212. * @param {number} year
  213. * @param {number} month
  214. * @param {number} day
  215. */
  216. renderCalendar(year, month, day) {
  217. const timestamp = new Date(`${year}-${month}-${day}`).getTime();
  218. this.setData({
  219. 'datepicker.curYear': year,
  220. 'datepicker.curMonth': month,
  221. 'datepicker.todayTimestamp': timestamp
  222. });
  223. conf.calculateEmptyGrids.call(this, year, month);
  224. conf.calculateDays.call(this, year, month, day);
  225. },
  226. /**
  227. * 初始化日历选择器
  228. * @param {number} curYear
  229. * @param {number} curMonth
  230. * @param {number} curDate
  231. */
  232. init(curYear, curMonth, curDate) {
  233. const self = _getCurrentPage();
  234. const weeksCh = ['日', '一', '二', '三', '四', '五', '六'];
  235. self.setData({
  236. 'datepicker.weeksCh': weeksCh,
  237. 'datepicker.showDatePicker': true
  238. });
  239. if (!curYear && !curMonth && !curDate) return conf.jumpToToday.call(self);
  240. conf.renderCalendar.call(self, curYear, curMonth, curDate);
  241. },
  242. /**
  243. * 点击输入框调起日历选择器
  244. * @param {object} e 事件对象
  245. */
  246. showDatepicker(e) {
  247. const value = e.detail.value;
  248. if (value && typeof value === 'string') {
  249. const tmp = value.split('-');
  250. conf.init(+tmp[0], +tmp[1], +tmp[2]);
  251. } else {
  252. conf.init();
  253. }
  254. },
  255. /**
  256. * 当输入日期时
  257. * @param {object} e 事件对象
  258. */
  259. onInputDate(e) {
  260. const self = _getCurrentPage();
  261. this.inputTimer && clearTimeout(this.inputTimer);
  262. this.inputTimer = setTimeout(() => {
  263. const v = e.detail.value;
  264. const _v = (v && v.split('-')) || [];
  265. const RegExpYear = /^\d{4}$/;
  266. const RegExpMonth = /^(([0]?[1-9])|([1][0-2]))$/;
  267. const RegExpDay = /^(([0]?[1-9])|([1-2][0-9])|(3[0-1]))$/;
  268. if (_v && _v.length === 3) {
  269. if (
  270. RegExpYear.test(_v[0]) &&
  271. RegExpMonth.test(_v[1]) &&
  272. RegExpDay.test(_v[2])
  273. ) {
  274. conf.renderCalendar.call(self, +_v[0], +_v[1], +_v[2]);
  275. }
  276. }
  277. }, 500);
  278. },
  279. /**
  280. * 计算当前日历面板月份的前一月数据
  281. */
  282. choosePrevMonth() {
  283. const { curYear, curMonth } = this.data.datepicker;
  284. let newMonth = curMonth - 1;
  285. let newYear = curYear;
  286. if (newMonth < 1) {
  287. newYear = curYear - 1;
  288. newMonth = 12;
  289. }
  290. conf.calculateDays.call(this, newYear, newMonth);
  291. conf.calculateEmptyGrids.call(this, newYear, newMonth);
  292. this.setData({
  293. 'datepicker.curYear': newYear,
  294. 'datepicker.curMonth': newMonth
  295. });
  296. },
  297. /**
  298. * 计算当前日历面板月份的后一月数据
  299. */
  300. chooseNextMonth() {
  301. const { curYear, curMonth } = this.data.datepicker;
  302. let newMonth = curMonth + 1;
  303. let newYear = curYear;
  304. if (newMonth > 12) {
  305. newYear = curYear + 1;
  306. newMonth = 1;
  307. }
  308. conf.calculateDays.call(this, newYear, newMonth);
  309. conf.calculateEmptyGrids.call(this, newYear, newMonth);
  310. this.setData({
  311. 'datepicker.curYear': newYear,
  312. 'datepicker.curMonth': newMonth
  313. });
  314. },
  315. /**
  316. * 切换月份
  317. * @param {!object} e 事件对象
  318. */
  319. handleCalendar(e) {
  320. const handle = e.currentTarget.dataset.handle;
  321. if (handle === 'prev') {
  322. conf.choosePrevMonth.call(this);
  323. } else {
  324. conf.chooseNextMonth.call(this);
  325. }
  326. },
  327. /**
  328. * 选择具体日期
  329. * @param {!object} e 事件对象
  330. */
  331. tapDayItem(e) {
  332. const { idx, disable } = e.currentTarget.dataset;
  333. if (disable) return;
  334. const config = this.config;
  335. const { afterTapDay, onTapDay } = config;
  336. const { curYear, curMonth, days } = this.data.datepicker;
  337. const key = `datepicker.days[${idx}].choosed`;
  338. const selectedValue = `${curYear}-${curMonth}-${days[idx].day}`;
  339. if (this.config.type === 'timearea') {
  340. if (onTapDay && typeof onTapDay === 'function') {
  341. config.onTapDay(this.data.datepicker.days[idx], e);
  342. return;
  343. }
  344. this.setData({
  345. [key]: !days[idx].choosed
  346. });
  347. } else if (this.config.type === 'normal' && !days[idx].choosed) {
  348. const prev = days.filter(item => item.choosed)[0];
  349. const prevKey = prev && `datepicker.days[${prev.day - 1}].choosed`;
  350. if (onTapDay && typeof onTapDay === 'function') {
  351. config.onTapDay(days[idx], e);
  352. return;
  353. }
  354. const data = {
  355. [key]: true,
  356. 'datepicker.selectedValue': selectedValue,
  357. 'datepicker.selectedDay': [days[idx]]
  358. };
  359. if (prevKey) {
  360. data[prevKey] = false;
  361. }
  362. this.setData(data);
  363. }
  364. if (afterTapDay && typeof afterTapDay === 'function') {
  365. config.afterTapDay(days[idx]);
  366. }
  367. },
  368. /**
  369. * 关闭日历选择器
  370. */
  371. closeDatePicker() {
  372. this.setData({
  373. 'datepicker.showDatePicker': false
  374. });
  375. },
  376. datepickerTouchstart(e) {
  377. const t = e.touches[0];
  378. const startX = t.clientX;
  379. const startY = t.clientY;
  380. this.slideLock = true; // 滑动事件加锁
  381. this.setData({
  382. 'gesture.startX': startX,
  383. 'gesture.startY': startY
  384. });
  385. },
  386. datepickerTouchmove(e) {
  387. if (isLeftSlide.call(this, e)) {
  388. conf.chooseNextMonth.call(this);
  389. }
  390. if (isRightSlide.call(this, e)) {
  391. conf.choosePrevMonth.call(this);
  392. }
  393. }
  394. };
  395. function _getCurrentPage() {
  396. const pages = getCurrentPages();
  397. const last = pages.length - 1;
  398. return pages[last];
  399. }
  400. /**
  401. * 跳转至今天
  402. */
  403. export const jumpToToday = () => {
  404. const self = _getCurrentPage();
  405. conf.jumpToToday.call(self);
  406. };
  407. export default (config = {}) => {
  408. const self = _getCurrentPage();
  409. if (!config.type) config.type = 'normal';
  410. self.config = config;
  411. self.setData({
  412. datepicker: {
  413. showDatePicker: false,
  414. showInput: config.showInput === true || config.showInput === undefined,
  415. placeholder: config.placeholder || '请选择日期'
  416. }
  417. });
  418. self.datepickerTouchstart = conf.datepickerTouchstart.bind(self);
  419. self.datepickerTouchmove = conf.datepickerTouchmove.bind(self);
  420. self.showDatepicker = conf.showDatepicker.bind(self);
  421. self.onInputDate = conf.onInputDate.bind(self);
  422. self.closeDatePicker = conf.closeDatePicker.bind(self);
  423. self.tapDayItem = conf.tapDayItem.bind(self);
  424. self.handleCalendar = conf.handleCalendar.bind(self);
  425. };
  426. /**
  427. * 获取已选择的日期
  428. */
  429. export const getSelectedDay = () => {
  430. const self = _getCurrentPage();
  431. return self.data.datepicker.selectedDay;
  432. };