month-table.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <template>
  2. <table @click="handleMonthTableClick" @mousemove="handleMouseMove" class="el-month-table">
  3. <tbody>
  4. <tr v-for="(row, key) in rows" :key="key">
  5. <td :class="getCellStyle(cell)" v-for="(cell, key) in row" :key="key">
  6. <div>
  7. <a class="cell">{{ t('el.datepicker.months.' + months[cell.text]) }}</a>
  8. </div>
  9. </td>
  10. </tr>
  11. </tbody>
  12. </table>
  13. </template>
  14. <script type="text/babel">
  15. import Locale from 'element-ui/src/mixins/locale';
  16. import { isDate, range, getDayCountOfMonth, nextDate } from 'element-ui/src/utils/date-util';
  17. import { hasClass } from 'element-ui/src/utils/dom';
  18. import { arrayFindIndex, coerceTruthyValueToArray, arrayFind } from 'element-ui/src/utils/util';
  19. const datesInMonth = (year, month) => {
  20. const numOfDays = getDayCountOfMonth(year, month);
  21. const firstDay = new Date(year, month, 1);
  22. return range(numOfDays).map(n => nextDate(firstDay, n));
  23. };
  24. const clearDate = (date) => {
  25. return new Date(date.getFullYear(), date.getMonth());
  26. };
  27. const getMonthTimestamp = function(time) {
  28. if (typeof time === 'number' || typeof time === 'string') {
  29. return clearDate(new Date(time)).getTime();
  30. } else if (time instanceof Date) {
  31. return clearDate(time).getTime();
  32. } else {
  33. return NaN;
  34. }
  35. };
  36. export default {
  37. props: {
  38. disabledDate: {},
  39. value: {},
  40. selectionMode: {
  41. default: 'month'
  42. },
  43. minDate: {},
  44. maxDate: {},
  45. defaultValue: {
  46. validator(val) {
  47. // null or valid Date Object
  48. return val === null || isDate(val) || (Array.isArray(val) && val.every(isDate));
  49. }
  50. },
  51. date: {},
  52. rangeState: {
  53. default() {
  54. return {
  55. endDate: null,
  56. selecting: false
  57. };
  58. }
  59. }
  60. },
  61. mixins: [Locale],
  62. watch: {
  63. 'rangeState.endDate'(newVal) {
  64. this.markRange(this.minDate, newVal);
  65. },
  66. minDate(newVal, oldVal) {
  67. if (getMonthTimestamp(newVal) !== getMonthTimestamp(oldVal)) {
  68. this.markRange(this.minDate, this.maxDate);
  69. }
  70. },
  71. maxDate(newVal, oldVal) {
  72. if (getMonthTimestamp(newVal) !== getMonthTimestamp(oldVal)) {
  73. this.markRange(this.minDate, this.maxDate);
  74. }
  75. }
  76. },
  77. data() {
  78. return {
  79. months: ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'],
  80. tableRows: [ [], [], [] ],
  81. lastRow: null,
  82. lastColumn: null
  83. };
  84. },
  85. methods: {
  86. cellMatchesDate(cell, date) {
  87. const value = new Date(date);
  88. return this.date.getFullYear() === value.getFullYear() && Number(cell.text) === value.getMonth();
  89. },
  90. getCellStyle(cell) {
  91. const style = {};
  92. const year = this.date.getFullYear();
  93. const today = new Date();
  94. const month = cell.text;
  95. const defaultValue = this.defaultValue ? Array.isArray(this.defaultValue) ? this.defaultValue : [this.defaultValue] : [];
  96. style.disabled = typeof this.disabledDate === 'function'
  97. ? datesInMonth(year, month).every(this.disabledDate)
  98. : false;
  99. style.current = arrayFindIndex(coerceTruthyValueToArray(this.value), date => date.getFullYear() === year && date.getMonth() === month) >= 0;
  100. style.today = today.getFullYear() === year && today.getMonth() === month;
  101. style.default = defaultValue.some(date => this.cellMatchesDate(cell, date));
  102. if (cell.inRange) {
  103. style['in-range'] = true;
  104. if (cell.start) {
  105. style['start-date'] = true;
  106. }
  107. if (cell.end) {
  108. style['end-date'] = true;
  109. }
  110. }
  111. return style;
  112. },
  113. getMonthOfCell(month) {
  114. const year = this.date.getFullYear();
  115. return new Date(year, month, 1);
  116. },
  117. markRange(minDate, maxDate) {
  118. minDate = getMonthTimestamp(minDate);
  119. maxDate = getMonthTimestamp(maxDate) || minDate;
  120. [minDate, maxDate] = [Math.min(minDate, maxDate), Math.max(minDate, maxDate)];
  121. const rows = this.rows;
  122. for (let i = 0, k = rows.length; i < k; i++) {
  123. const row = rows[i];
  124. for (let j = 0, l = row.length; j < l; j++) {
  125. const cell = row[j];
  126. const index = i * 4 + j;
  127. const time = new Date(this.date.getFullYear(), index).getTime();
  128. cell.inRange = minDate && time >= minDate && time <= maxDate;
  129. cell.start = minDate && time === minDate;
  130. cell.end = maxDate && time === maxDate;
  131. }
  132. }
  133. },
  134. handleMouseMove(event) {
  135. if (!this.rangeState.selecting) return;
  136. let target = event.target;
  137. if (target.tagName === 'A') {
  138. target = target.parentNode.parentNode;
  139. }
  140. if (target.tagName === 'DIV') {
  141. target = target.parentNode;
  142. }
  143. if (target.tagName !== 'TD') return;
  144. const row = target.parentNode.rowIndex;
  145. const column = target.cellIndex;
  146. // can not select disabled date
  147. if (this.rows[row][column].disabled) return;
  148. // only update rangeState when mouse moves to a new cell
  149. // this avoids frequent Date object creation and improves performance
  150. if (row !== this.lastRow || column !== this.lastColumn) {
  151. this.lastRow = row;
  152. this.lastColumn = column;
  153. this.$emit('changerange', {
  154. minDate: this.minDate,
  155. maxDate: this.maxDate,
  156. rangeState: {
  157. selecting: true,
  158. endDate: this.getMonthOfCell(row * 4 + column)
  159. }
  160. });
  161. }
  162. },
  163. handleMonthTableClick(event) {
  164. let target = event.target;
  165. if (target.tagName === 'A') {
  166. target = target.parentNode.parentNode;
  167. }
  168. if (target.tagName === 'DIV') {
  169. target = target.parentNode;
  170. }
  171. if (target.tagName !== 'TD') return;
  172. if (hasClass(target, 'disabled')) return;
  173. const column = target.cellIndex;
  174. const row = target.parentNode.rowIndex;
  175. const month = row * 4 + column;
  176. const newDate = this.getMonthOfCell(month);
  177. if (this.selectionMode === 'range') {
  178. if (!this.rangeState.selecting) {
  179. this.$emit('pick', {minDate: newDate, maxDate: null});
  180. this.rangeState.selecting = true;
  181. } else {
  182. if (newDate >= this.minDate) {
  183. this.$emit('pick', {minDate: this.minDate, maxDate: newDate});
  184. } else {
  185. this.$emit('pick', {minDate: newDate, maxDate: this.minDate});
  186. }
  187. this.rangeState.selecting = false;
  188. }
  189. } else {
  190. this.$emit('pick', month);
  191. }
  192. }
  193. },
  194. computed: {
  195. rows() {
  196. // TODO: refactory rows / getCellClasses
  197. const rows = this.tableRows;
  198. const disabledDate = this.disabledDate;
  199. const selectedDate = [];
  200. const now = getMonthTimestamp(new Date());
  201. for (let i = 0; i < 3; i++) {
  202. const row = rows[i];
  203. for (let j = 0; j < 4; j++) {
  204. let cell = row[j];
  205. if (!cell) {
  206. cell = { row: i, column: j, type: 'normal', inRange: false, start: false, end: false };
  207. }
  208. cell.type = 'normal';
  209. const index = i * 4 + j;
  210. const time = new Date(this.date.getFullYear(), index).getTime();
  211. cell.inRange = time >= getMonthTimestamp(this.minDate) && time <= getMonthTimestamp(this.maxDate);
  212. cell.start = this.minDate && time === getMonthTimestamp(this.minDate);
  213. cell.end = this.maxDate && time === getMonthTimestamp(this.maxDate);
  214. const isToday = time === now;
  215. if (isToday) {
  216. cell.type = 'today';
  217. }
  218. cell.text = index;
  219. let cellDate = new Date(time);
  220. cell.disabled = typeof disabledDate === 'function' && disabledDate(cellDate);
  221. cell.selected = arrayFind(selectedDate, date => date.getTime() === cellDate.getTime());
  222. this.$set(row, j, cell);
  223. }
  224. }
  225. return rows;
  226. }
  227. }
  228. };
  229. </script>