calendar.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <template>
  2. <transition :name="animation">
  3. <div class="nut-calendar" v-show="isVisible">
  4. <div class="nut-calendar-control">
  5. <span class="nut-calendar-confirm-btn" @click="confirm" v-if="(type == 'range' && currDate && currDate.length == 2) || type != 'range'">{{nutTranslate('lang.okBtnTxt')}}</span>
  6. <span class="nut-calendar-cancel-btn" @click="closeActionSheet">{{nutTranslate('lang.cancelBtnTxt')}}</span>
  7. <div class="nut-calendar-title">{{title || nutTranslate('lang.calendar.title')}}</div>
  8. <div class="nut-calendar-week">
  9. <span v-for="(item, index) of week" :key="index">{{item}}</span>
  10. </div>
  11. </div>
  12. <div class="nut-calendar-months" @touchstart.stop="touchStart" @touchmove.stop.prevent="touchMove" @touchend.stop="touchEnd">
  13. <div class="nut-calendar-months-panel" ref="months">
  14. <div class="nut-calendar-loading-tip">{{!unLoadPrev ? nutTranslate('lang.calendar.loadPrevMonth') : nutTranslate('lang.calendar.noMoreMonth')}}</div>
  15. <div class="nut-calendar-month" v-for="(month, index) of monthsData " :key="index">
  16. <div class="nut-calendar-month-title">{{month.title}}</div>
  17. <div class="nut-calendar-month-con">
  18. <div class="nut-calendar-month-item">
  19. <template v-for="(day, i) of month.monthData" >
  20. <div class="nut-calendar-month-day" :class="getClass(day, month)" :key="i" @click="chooseDay(day, month)">
  21. <span>{{day.type == 'curr' ? day.day : ''}}</span>
  22. <span class="nut-calendar-day-tip" v-if="isStartTip(day, month)">{{nutTranslate('lang.calendar.start')}}</span>
  23. <span class="nut-calendar-day-tip" v-else-if="isEndTip(day, month)">{{nutTranslate('lang.calendar.end')}}</span>
  24. </div>
  25. </template>
  26. </div>
  27. </div>
  28. </div>
  29. </div>
  30. </div>
  31. </div>
  32. </transition>
  33. </template>
  34. <script>
  35. import Utils from "../../utils/date.js";
  36. import locale from "../../mixins/locale";
  37. export default {
  38. name:'nut-calendar',
  39. mixins: [locale],
  40. props: {
  41. type: {
  42. type: String,
  43. default: 'one'
  44. },
  45. animation: {
  46. type: String,
  47. default: 'nutSlideUp'
  48. },
  49. isAutoBackFill: {
  50. type: Boolean,
  51. default: false
  52. },
  53. isOpenRangeSelect: {
  54. type: Boolean,
  55. default: false
  56. },
  57. isVisible: {
  58. type: Boolean,
  59. default: false
  60. },
  61. title: {
  62. type: String
  63. },
  64. defaultValue: {
  65. type: String | Array,
  66. default: null
  67. },
  68. startDate: {
  69. type: String,
  70. //default: null
  71. default: Utils.getDay(0)
  72. },
  73. endDate: {
  74. type: String,
  75. //default: null
  76. default: Utils.getDay(365)
  77. },
  78. },
  79. data() {
  80. const week = this.nutTranslate('lang.calendar.week');
  81. return {
  82. currDate: null,
  83. week: week.split(','),
  84. unLoadPrev: false,
  85. unLoadNext: false,
  86. touchParams: {
  87. startY: 0,
  88. endY: 0,
  89. startTime: 0,
  90. endTime: 0
  91. },
  92. transformY: 0,
  93. scrollDistance: 0,
  94. defaultData: null,
  95. startData: this.startDate ? this.splitDate(this.startDate) : null,
  96. endData: this.endDate ? this.splitDate(this.endDate) : null,
  97. chooseData: [],
  98. monthsData: [],
  99. dayPrefix: 'nut-calendar-month-day'
  100. };
  101. },
  102. computed: {
  103. isRange: function() {
  104. return this.type === 'range';
  105. }
  106. },
  107. methods: {
  108. isActive(day, month) {
  109. return this.isRange && day.type == 'curr' && this.getClass(day, month) == 'nut-calendar-month-day-active';
  110. },
  111. isStartTip(day, month) {
  112. if (this.isActive(day, month)) {
  113. return this.isStart(this.getCurrDate(day, month));
  114. } else {
  115. return false;
  116. }
  117. },
  118. isEndTip(day, month) {
  119. return this.isActive(day, month);
  120. },
  121. getCurrData(type) {
  122. let monthData = type == 'prev' ? this.monthsData[0] : this.monthsData[this.monthsData.length - 1];
  123. let year = parseInt(monthData.curData[0]);
  124. let month = parseInt(monthData.curData[1].toString().replace(/^0/, ''));
  125. switch(type) {
  126. case 'prev':
  127. month == 1 && (year -= 1);
  128. month = month == 1 ? 12 : --month;
  129. break;
  130. case 'next':
  131. month == 12 && (year += 1);
  132. month = month == 12 ? 1 : ++month;
  133. break;
  134. }
  135. return [year, Utils.getNumTwoBit(month), monthData.curData[2]];
  136. },
  137. getDaysStatus(days, type) {
  138. return Array.from(Array(days), (v,k) => {
  139. return {
  140. day: (k + 1),
  141. type: type
  142. }
  143. });
  144. },
  145. getMonth(curData, type) {
  146. let preMonthDays = Utils.getMonthPreDay(curData[0], curData[1]);
  147. let currMonthDays = Utils.getMonthDays(curData[0], curData[1]);
  148. let nextMonthDays = 42 - preMonthDays - currMonthDays;
  149. let title = this.nutTranslate('lang.calendar.monthTitle', {year: curData[0], month: curData[1]});
  150. let monthInfo = {
  151. curData: curData,
  152. title: title,
  153. monthData: [...this.getDaysStatus(preMonthDays, 'prev'), ...this.getDaysStatus(currMonthDays, 'curr')]
  154. };
  155. if (type == 'next') {
  156. if (!this.endData || !Utils.compareDate(`${this.endData[0]}-${this.endData[1]}-${Utils.getMonthDays(this.endData[0], this.endData[1])}`, `${curData[0]}-${curData[1]}-${curData[2]}`)) {
  157. this.monthsData.push(monthInfo);
  158. } else {
  159. this.unLoadNext = true;
  160. }
  161. } else {
  162. if (!this.startData || !Utils.compareDate(`${curData[0]}-${curData[1]}-${curData[2]}`, `${this.startData[0]}-${this.startData[1]}-01`)) {
  163. this.monthsData.unshift(monthInfo);
  164. } else {
  165. this.unLoadPrev = true;
  166. }
  167. }
  168. },
  169. getCurrDate(day, month, isRange) {
  170. return isRange ? month.curData[3] + '-' + month.curData[4] + '-' + Utils.getNumTwoBit(day.day) : month.curData[0] + '-' + month.curData[1] + '-' + Utils.getNumTwoBit(day.day);
  171. },
  172. isStart(currDate) {
  173. return Utils.isEqual(this.currDate[0], currDate);
  174. },
  175. isEnd(currDate) {
  176. return Utils.isEqual(this.currDate[1], currDate);
  177. },
  178. splitDate(date) {
  179. return date.split('-');
  180. },
  181. getClass(day, month, isRange) {
  182. let currDate = this.getCurrDate(day, month, isRange);
  183. if (day.type == 'curr') {
  184. if ((!this.isRange && Utils.isEqual(this.currDate, currDate)) || (this.isRange && (this.isStart(currDate) || this.isEnd(currDate)))) {
  185. return `${this.dayPrefix}-active`;
  186. } else if (( this.startDate && Utils.compareDate(currDate, this.startDate)) || (this.endDate && Utils.compareDate(this.endDate, currDate))) {
  187. return `${this.dayPrefix}-disabled`;
  188. } else if(this.isRange & this.currDate.length == 2 && Utils.compareDate(this.currDate[0], currDate) && Utils.compareDate(currDate, this.currDate[1])) {
  189. return `${this.dayPrefix}-choose`;
  190. } else {
  191. return null;
  192. }
  193. } else {
  194. return `${this.dayPrefix}-disabled`;
  195. }
  196. },
  197. chooseDay(day, month, isFirst, isRange) {
  198. if (this.getClass(day, month, isRange) != `${this.dayPrefix}-disabled`) {
  199. let days = [...month.curData];
  200. days = isRange ? days.splice(3) : days.splice(0,3);
  201. days[2] = typeof day.day == 'number' ? Utils.getNumTwoBit(day.day) : day.day;
  202. days[3] = `${days[0]}-${days[1]}-${days[2]}`;
  203. days[4] = Utils.getWhatDay(days[0], days[1], days[2]);
  204. if (!this.isRange) {
  205. this.currDate = days[3];
  206. this.chooseData = [...days];
  207. } else {
  208. if (this.currDate.length == 2) {
  209. this.currDate = [days[3]];
  210. } else {
  211. if (Utils.compareDate(this.currDate[0], days[3])) {
  212. this.currDate.push(days[3]);
  213. } else {
  214. this.currDate.unshift(days[3]);
  215. }
  216. }
  217. if (this.chooseData.length == 2 || !this.chooseData.length) {
  218. this.chooseData = [...days];
  219. } else {
  220. if (Utils.compareDate(this.chooseData[3], days[3])) {
  221. this.chooseData = [[...this.chooseData], [...days]];
  222. } else {
  223. this.chooseData = [[...days], [...this.chooseData]];
  224. }
  225. }
  226. }
  227. if (this.isAutoBackFill && !isFirst) {
  228. this.confirm();
  229. }
  230. }
  231. },
  232. confirm() {
  233. if ((this.isRange && this.chooseData.length == 2) || !this.isRange) {
  234. this.$emit('choose', this.chooseData);
  235. this.$emit('close');
  236. }
  237. },
  238. resetRender() {
  239. this.chooseData.splice(0);
  240. this.monthsData.splice(0);
  241. this.scrollDistance = 0;
  242. this.translateY = 0;
  243. this.setTransform(this.scrollDistance);
  244. this.initData();
  245. },
  246. closeActionSheet() {
  247. this.$emit('close');
  248. this.resetRender();
  249. },
  250. touchStart(event) {
  251. let changedTouches = event.changedTouches[0];
  252. this.touchParams.startY = changedTouches.pageY;
  253. this.touchParams.startTime = event.timestamp || Date.now();
  254. this.transformY = this.scrollDistance;
  255. },
  256. touchMove(event) {
  257. //event.preventDefault();
  258. let changedTouches = event.changedTouches[0];
  259. this.touchParams.lastY = changedTouches.pageY;
  260. this.touchParams.lastTime = event.timestamp || Date.now();
  261. let move = this.touchParams.lastY - this.touchParams.startY;
  262. if (Math.abs(move) < 5) {
  263. return false;
  264. }
  265. this.setMove(move);
  266. },
  267. touchEnd(event) {
  268. let changedTouches = event.changedTouches[0];
  269. this.touchParams.lastY = changedTouches.pageY;
  270. this.touchParams.lastTime = event.timestamp || Date.now();
  271. let move = this.touchParams.lastY - this.touchParams.startY;
  272. if (Math.abs(move) < 5) {
  273. return false;
  274. }
  275. let updateMove = move + this.transformY;
  276. let h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
  277. let offsetHeight = this.$refs.months.offsetHeight;
  278. if (updateMove > 0) {
  279. this.getMonth(this.getCurrData('prev'), 'prev');
  280. } else if (updateMove < -offsetHeight + h * 2) {
  281. this.getMonth(this.getCurrData('next'), 'next');
  282. if (Math.abs(move) >= 300) {
  283. this.getMonth(this.getCurrData('next'), 'next');
  284. }
  285. }
  286. let moveTime = this.touchParams.lastTime - this.touchParams.startTime;
  287. if (moveTime <= 300) {
  288. move = move * 2;
  289. moveTime = moveTime + 1000;
  290. this.setMove(move, 'end', moveTime);
  291. } else {
  292. this.setMove(move, 'end');
  293. }
  294. },
  295. setTransform(translateY = 0, type, time = 1000) {
  296. if (type === 'end') {
  297. this.$refs.months.style.webkitTransition = `transform ${time}ms cubic-bezier(0.19, 1, 0.22, 1)`;
  298. //this.$refs.months.style.transition = `transform ${time}ms cubic-bezier(0.19, 1, 0.22, 1)`;
  299. } else {
  300. this.$refs.months.style.webkitTransition = '';
  301. //this.$refs.months.style.transition = '';
  302. }
  303. this.$refs.months.style.webkitTransform = `translateY(${translateY}px)`;
  304. //this.$refs.months.style.transform = `translateY(${translateY}px)`;
  305. this.scrollDistance = translateY;
  306. },
  307. setMove(move, type, time) {
  308. let updateMove = move + this.transformY;
  309. let h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
  310. let offsetHeight = this.$refs.months.offsetHeight;
  311. if (type === 'end') {
  312. // 限定滚动距离
  313. if (updateMove > 0) {
  314. updateMove = 0;
  315. }
  316. if (updateMove < 0 && updateMove < -offsetHeight + h - 70) {
  317. updateMove = -offsetHeight + h - 70;
  318. }
  319. if (offsetHeight <= h && this.monthsData.length == 1) {
  320. updateMove = 0;
  321. }
  322. let endMove = updateMove;
  323. this.setTransform(endMove, type, time);
  324. } else {
  325. if (updateMove > 0 && updateMove > 100) {
  326. updateMove = 100;
  327. }
  328. if (updateMove < -offsetHeight + h - 170 && this.monthsData.length > 1) {
  329. updateMove = -offsetHeight + h -170;
  330. }
  331. if (updateMove < 0 && updateMove < - 100 && this.monthsData.length == 1) {
  332. updateMove = -100;
  333. }
  334. this.setTransform(updateMove);
  335. }
  336. },
  337. initData() {
  338. if (!this.defaultValue) {
  339. this.currDate = this.isRange ? [Utils.date2Str(new Date()), Utils.getDay(2)] : Utils.date2Str(new Date());
  340. } else {
  341. this.currDate = this.isRange ? [...this.defaultValue] : this.defaultValue;
  342. }
  343. if (this.isRange) {
  344. if (this.startDate && Utils.compareDate(this.currDate[0], this.startDate)) {
  345. this.currDate.splice(0, 1, this.startDate);
  346. }
  347. if (this.endDate && Utils.compareDate(this.endDate, this.currDate[1])) {
  348. this.currDate.splice(1, 1, this.endDate);
  349. }
  350. this.defaultData = [...this.splitDate(this.currDate[0]), ...this.splitDate(this.currDate[1])];
  351. } else {
  352. if (this.startDate && Utils.compareDate(this.currDate, this.startDate)) {
  353. this.currDate = this.startDate;
  354. } else if (this.endDate && !Utils.compareDate(this.currDate, this.endDate)) {
  355. this.currDate = this.endDate;
  356. }
  357. this.defaultData = [...this.splitDate(this.currDate)];
  358. }
  359. this.getMonth(this.defaultData, 'next');
  360. let i = 1;
  361. do {
  362. this.getMonth(this.getCurrData('next'), 'next');
  363. } while (i++ < 4);
  364. if (this.isRange) {
  365. this.chooseDay({'day': this.defaultData[2], 'type': 'curr'}, this.monthsData[0], true);
  366. this.chooseDay({'day': this.defaultData[5], 'type': 'curr'}, this.monthsData[0], true, true);
  367. } else {
  368. this.chooseDay({'day': this.defaultData[2], 'type': 'curr'}, this.monthsData[0], true);
  369. }
  370. }
  371. },
  372. mounted() {
  373. this.initData();
  374. }
  375. }
  376. </script>