month-table.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. // remove the first element that satisfies `pred` from arr
  37. // return a new array if modification occurs
  38. // return the original array otherwise
  39. const removeFromArray = function(arr, pred) {
  40. const idx = typeof pred === 'function' ? arrayFindIndex(arr, pred) : arr.indexOf(pred);
  41. return idx >= 0 ? [...arr.slice(0, idx), ...arr.slice(idx + 1)] : arr;
  42. };
  43. export default {
  44. props: {
  45. disabledDate: {},
  46. value: {},
  47. selectionMode: {
  48. default: 'month'
  49. },
  50. minDate: {},
  51. maxDate: {},
  52. defaultValue: {
  53. validator(val) {
  54. // null or valid Date Object
  55. return val === null || isDate(val) || (Array.isArray(val) && val.every(isDate));
  56. }
  57. },
  58. date: {},
  59. rangeState: {
  60. default() {
  61. return {
  62. endDate: null,
  63. selecting: false
  64. };
  65. }
  66. }
  67. },
  68. mixins: [Locale],
  69. watch: {
  70. 'rangeState.endDate'(newVal) {
  71. this.markRange(this.minDate, newVal);
  72. },
  73. minDate(newVal, oldVal) {
  74. if (getMonthTimestamp(newVal) !== getMonthTimestamp(oldVal)) {
  75. this.markRange(this.minDate, this.maxDate);
  76. }
  77. },
  78. maxDate(newVal, oldVal) {
  79. if (getMonthTimestamp(newVal) !== getMonthTimestamp(oldVal)) {
  80. this.markRange(this.minDate, this.maxDate);
  81. }
  82. }
  83. },
  84. data() {
  85. return {
  86. months: ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'],
  87. tableRows: [ [], [], [] ],
  88. lastRow: null,
  89. lastColumn: null
  90. };
  91. },
  92. methods: {
  93. cellMatchesDate(cell, date) {
  94. const value = new Date(date);
  95. return this.date.getFullYear() === value.getFullYear() && Number(cell.text) === value.getMonth();
  96. },
  97. getCellStyle(cell) {
  98. const style = {};
  99. const year = this.date.getFullYear();
  100. const today = new Date();
  101. const month = cell.text;
  102. const defaultValue = this.defaultValue ? Array.isArray(this.defaultValue) ? this.defaultValue : [this.defaultValue] : [];
  103. style.disabled = typeof this.disabledDate === 'function'
  104. ? datesInMonth(year, month).every(this.disabledDate)
  105. : false;
  106. style.current = arrayFindIndex(coerceTruthyValueToArray(this.value), date => date.getFullYear() === year && date.getMonth() === month) >= 0;
  107. style.today = today.getFullYear() === year && today.getMonth() === month;
  108. style.default = defaultValue.some(date => this.cellMatchesDate(cell, date));
  109. if (cell.inRange) {
  110. style['in-range'] = true;
  111. if (cell.start) {
  112. style['start-date'] = true;
  113. }
  114. if (cell.end) {
  115. style['end-date'] = true;
  116. }
  117. }
  118. return style;
  119. },
  120. getMonthOfCell(month) {
  121. const year = this.date.getFullYear();
  122. return new Date(year, month, 1);
  123. },
  124. markRange(minDate, maxDate) {
  125. minDate = getMonthTimestamp(minDate);
  126. maxDate = getMonthTimestamp(maxDate) || minDate;
  127. [minDate, maxDate] = [Math.min(minDate, maxDate), Math.max(minDate, maxDate)];
  128. const rows = this.rows;
  129. for (let i = 0, k = rows.length; i < k; i++) {
  130. const row = rows[i];
  131. for (let j = 0, l = row.length; j < l; j++) {
  132. const cell = row[j];
  133. const index = i * 4 + j;
  134. const time = new Date(this.date.getFullYear(), index).getTime();
  135. cell.inRange = minDate && time >= minDate && time <= maxDate;
  136. cell.start = minDate && time === minDate;
  137. cell.end = maxDate && time === maxDate;
  138. }
  139. }
  140. },
  141. handleMouseMove(event) {
  142. if (!this.rangeState.selecting) return;
  143. let target = event.target;
  144. if (target.tagName === 'A') {
  145. target = target.parentNode.parentNode;
  146. }
  147. if (target.tagName === 'DIV') {
  148. target = target.parentNode;
  149. }
  150. if (target.tagName !== 'TD') return;
  151. const row = target.parentNode.rowIndex;
  152. const column = target.cellIndex;
  153. // can not select disabled date
  154. if (this.rows[row][column].disabled) return;
  155. // only update rangeState when mouse moves to a new cell
  156. // this avoids frequent Date object creation and improves performance
  157. if (row !== this.lastRow || column !== this.lastColumn) {
  158. this.lastRow = row;
  159. this.lastColumn = column;
  160. this.$emit('changerange', {
  161. minDate: this.minDate,
  162. maxDate: this.maxDate,
  163. rangeState: {
  164. selecting: true,
  165. endDate: this.getMonthOfCell(row * 4 + column)
  166. }
  167. });
  168. }
  169. },
  170. handleMonthTableClick(event) {
  171. let target = event.target;
  172. if (target.tagName === 'A') {
  173. target = target.parentNode.parentNode;
  174. }
  175. if (target.tagName === 'DIV') {
  176. target = target.parentNode;
  177. }
  178. if (target.tagName !== 'TD') return;
  179. if (hasClass(target, 'disabled')) return;
  180. const column = target.cellIndex;
  181. const row = target.parentNode.rowIndex;
  182. const month = row * 4 + column;
  183. const newDate = this.getMonthOfCell(month);
  184. if (this.selectionMode === 'range') {
  185. if (!this.rangeState.selecting) {
  186. this.$emit('pick', {minDate: newDate, maxDate: null});
  187. this.rangeState.selecting = true;
  188. } else {
  189. if (newDate >= this.minDate) {
  190. this.$emit('pick', {minDate: this.minDate, maxDate: newDate});
  191. } else {
  192. this.$emit('pick', {minDate: newDate, maxDate: this.minDate});
  193. }
  194. this.rangeState.selecting = false;
  195. }
  196. } else if (this.selectionMode === 'months') {
  197. const value = this.value || [];
  198. const year = this.date.getFullYear();
  199. const newValue = arrayFindIndex(value, date => date.getFullYear() === year && date.getMonth() === month) >= 0
  200. ? removeFromArray(value, date => date.getTime() === newDate.getTime())
  201. : [...value, newDate];
  202. this.$emit('pick', newValue);
  203. } else {
  204. this.$emit('pick', month);
  205. }
  206. }
  207. },
  208. computed: {
  209. rows() {
  210. // TODO: refactory rows / getCellClasses
  211. const rows = this.tableRows;
  212. const disabledDate = this.disabledDate;
  213. const selectedDate = [];
  214. const now = getMonthTimestamp(new Date());
  215. for (let i = 0; i < 3; i++) {
  216. const row = rows[i];
  217. for (let j = 0; j < 4; j++) {
  218. let cell = row[j];
  219. if (!cell) {
  220. cell = { row: i, column: j, type: 'normal', inRange: false, start: false, end: false };
  221. }
  222. cell.type = 'normal';
  223. const index = i * 4 + j;
  224. const time = new Date(this.date.getFullYear(), index).getTime();
  225. cell.inRange = time >= getMonthTimestamp(this.minDate) && time <= getMonthTimestamp(this.maxDate);
  226. cell.start = this.minDate && time === getMonthTimestamp(this.minDate);
  227. cell.end = this.maxDate && time === getMonthTimestamp(this.maxDate);
  228. const isToday = time === now;
  229. if (isToday) {
  230. cell.type = 'today';
  231. }
  232. cell.text = index;
  233. let cellDate = new Date(time);
  234. cell.disabled = typeof disabledDate === 'function' && disabledDate(cellDate);
  235. cell.selected = arrayFind(selectedDate, date => date.getTime() === cellDate.getTime());
  236. this.$set(row, j, cell);
  237. }
  238. }
  239. return rows;
  240. }
  241. }
  242. };
  243. </script>