海鲜小程序

index.js 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. import { VantComponent } from '../common/component';
  2. import { isDef } from '../common/utils';
  3. import { pickerProps } from '../picker/shared';
  4. const currentYear = new Date().getFullYear();
  5. function isValidDate(date) {
  6. return isDef(date) && !isNaN(new Date(date).getTime());
  7. }
  8. function range(num, min, max) {
  9. return Math.min(Math.max(num, min), max);
  10. }
  11. function padZero(val) {
  12. return `00${val}`.slice(-2);
  13. }
  14. function times(n, iteratee) {
  15. let index = -1;
  16. const result = Array(n < 0 ? 0 : n);
  17. while (++index < n) {
  18. result[index] = iteratee(index);
  19. }
  20. return result;
  21. }
  22. function getTrueValue(formattedValue) {
  23. if (!formattedValue) return;
  24. while (isNaN(parseInt(formattedValue, 10))) {
  25. formattedValue = formattedValue.slice(1);
  26. }
  27. return parseInt(formattedValue, 10);
  28. }
  29. function getMonthEndDay(year, month) {
  30. return 32 - new Date(year, month - 1, 32).getDate();
  31. }
  32. const defaultFormatter = (_, value) => value;
  33. VantComponent({
  34. classes: ['active-class', 'toolbar-class', 'column-class'],
  35. props: Object.assign(Object.assign({}, pickerProps), {
  36. value: {
  37. type: null,
  38. observer: 'updateValue',
  39. },
  40. filter: null,
  41. type: {
  42. type: String,
  43. value: 'datetime',
  44. observer: 'updateValue',
  45. },
  46. showToolbar: {
  47. type: Boolean,
  48. value: true,
  49. },
  50. formatter: {
  51. type: null,
  52. value: defaultFormatter,
  53. },
  54. minDate: {
  55. type: Number,
  56. value: new Date(currentYear - 10, 0, 1).getTime(),
  57. observer: 'updateValue',
  58. },
  59. maxDate: {
  60. type: Number,
  61. value: new Date(currentYear + 10, 11, 31).getTime(),
  62. observer: 'updateValue',
  63. },
  64. minHour: {
  65. type: Number,
  66. value: 0,
  67. observer: 'updateValue',
  68. },
  69. maxHour: {
  70. type: Number,
  71. value: 23,
  72. observer: 'updateValue',
  73. },
  74. minMinute: {
  75. type: Number,
  76. value: 0,
  77. observer: 'updateValue',
  78. },
  79. maxMinute: {
  80. type: Number,
  81. value: 59,
  82. observer: 'updateValue',
  83. },
  84. }),
  85. data: {
  86. innerValue: Date.now(),
  87. columns: [],
  88. },
  89. methods: {
  90. updateValue() {
  91. const { data } = this;
  92. const val = this.correctValue(this.data.value);
  93. const isEqual = val === data.innerValue;
  94. if (!isEqual) {
  95. this.updateColumnValue(val).then(() => {
  96. this.$emit('input', val);
  97. });
  98. } else {
  99. this.updateColumns();
  100. }
  101. },
  102. getPicker() {
  103. if (this.picker == null) {
  104. this.picker = this.selectComponent('.van-datetime-picker');
  105. const { picker } = this;
  106. const { setColumnValues } = picker;
  107. picker.setColumnValues = (...args) =>
  108. setColumnValues.apply(picker, [...args, false]);
  109. }
  110. return this.picker;
  111. },
  112. updateColumns() {
  113. const { formatter = defaultFormatter } = this.data;
  114. const results = this.getOriginColumns().map((column) => ({
  115. values: column.values.map((value) => formatter(column.type, value)),
  116. }));
  117. return this.set({ columns: results });
  118. },
  119. getOriginColumns() {
  120. const { filter } = this.data;
  121. const results = this.getRanges().map(({ type, range }) => {
  122. let values = times(range[1] - range[0] + 1, (index) => {
  123. let value = range[0] + index;
  124. value = type === 'year' ? `${value}` : padZero(value);
  125. return value;
  126. });
  127. if (filter) {
  128. values = filter(type, values);
  129. }
  130. return { type, values };
  131. });
  132. return results;
  133. },
  134. getRanges() {
  135. const { data } = this;
  136. if (data.type === 'time') {
  137. return [
  138. {
  139. type: 'hour',
  140. range: [data.minHour, data.maxHour],
  141. },
  142. {
  143. type: 'minute',
  144. range: [data.minMinute, data.maxMinute],
  145. },
  146. ];
  147. }
  148. const {
  149. maxYear,
  150. maxDate,
  151. maxMonth,
  152. maxHour,
  153. maxMinute,
  154. } = this.getBoundary('max', data.innerValue);
  155. const {
  156. minYear,
  157. minDate,
  158. minMonth,
  159. minHour,
  160. minMinute,
  161. } = this.getBoundary('min', data.innerValue);
  162. const result = [
  163. {
  164. type: 'year',
  165. range: [minYear, maxYear],
  166. },
  167. {
  168. type: 'month',
  169. range: [minMonth, maxMonth],
  170. },
  171. {
  172. type: 'day',
  173. range: [minDate, maxDate],
  174. },
  175. {
  176. type: 'hour',
  177. range: [minHour, maxHour],
  178. },
  179. {
  180. type: 'minute',
  181. range: [minMinute, maxMinute],
  182. },
  183. ];
  184. if (data.type === 'date') result.splice(3, 2);
  185. if (data.type === 'year-month') result.splice(2, 3);
  186. return result;
  187. },
  188. correctValue(value) {
  189. const { data } = this;
  190. // validate value
  191. const isDateType = data.type !== 'time';
  192. if (isDateType && !isValidDate(value)) {
  193. value = data.minDate;
  194. } else if (!isDateType && !value) {
  195. const { minHour } = data;
  196. value = `${padZero(minHour)}:00`;
  197. }
  198. // time type
  199. if (!isDateType) {
  200. let [hour, minute] = value.split(':');
  201. hour = padZero(range(hour, data.minHour, data.maxHour));
  202. minute = padZero(range(minute, data.minMinute, data.maxMinute));
  203. return `${hour}:${minute}`;
  204. }
  205. // date type
  206. value = Math.max(value, data.minDate);
  207. value = Math.min(value, data.maxDate);
  208. return value;
  209. },
  210. getBoundary(type, innerValue) {
  211. const value = new Date(innerValue);
  212. const boundary = new Date(this.data[`${type}Date`]);
  213. const year = boundary.getFullYear();
  214. let month = 1;
  215. let date = 1;
  216. let hour = 0;
  217. let minute = 0;
  218. if (type === 'max') {
  219. month = 12;
  220. date = getMonthEndDay(value.getFullYear(), value.getMonth() + 1);
  221. hour = 23;
  222. minute = 59;
  223. }
  224. if (value.getFullYear() === year) {
  225. month = boundary.getMonth() + 1;
  226. if (value.getMonth() + 1 === month) {
  227. date = boundary.getDate();
  228. if (value.getDate() === date) {
  229. hour = boundary.getHours();
  230. if (value.getHours() === hour) {
  231. minute = boundary.getMinutes();
  232. }
  233. }
  234. }
  235. }
  236. return {
  237. [`${type}Year`]: year,
  238. [`${type}Month`]: month,
  239. [`${type}Date`]: date,
  240. [`${type}Hour`]: hour,
  241. [`${type}Minute`]: minute,
  242. };
  243. },
  244. onCancel() {
  245. this.$emit('cancel');
  246. },
  247. onConfirm() {
  248. this.$emit('confirm', this.data.innerValue);
  249. },
  250. onChange() {
  251. const { data } = this;
  252. let value;
  253. const picker = this.getPicker();
  254. if (data.type === 'time') {
  255. const indexes = picker.getIndexes();
  256. value = `${+data.columns[0].values[indexes[0]]}:${+data.columns[1]
  257. .values[indexes[1]]}`;
  258. } else {
  259. const values = picker.getValues();
  260. const year = getTrueValue(values[0]);
  261. const month = getTrueValue(values[1]);
  262. const maxDate = getMonthEndDay(year, month);
  263. let date = getTrueValue(values[2]);
  264. if (data.type === 'year-month') {
  265. date = 1;
  266. }
  267. date = date > maxDate ? maxDate : date;
  268. let hour = 0;
  269. let minute = 0;
  270. if (data.type === 'datetime') {
  271. hour = getTrueValue(values[3]);
  272. minute = getTrueValue(values[4]);
  273. }
  274. value = new Date(year, month - 1, date, hour, minute);
  275. }
  276. value = this.correctValue(value);
  277. this.updateColumnValue(value).then(() => {
  278. this.$emit('input', value);
  279. this.$emit('change', picker);
  280. });
  281. },
  282. updateColumnValue(value) {
  283. let values = [];
  284. const { type, formatter = defaultFormatter } = this.data;
  285. const picker = this.getPicker();
  286. if (type === 'time') {
  287. const pair = value.split(':');
  288. values = [formatter('hour', pair[0]), formatter('minute', pair[1])];
  289. } else {
  290. const date = new Date(value);
  291. values = [
  292. formatter('year', `${date.getFullYear()}`),
  293. formatter('month', padZero(date.getMonth() + 1)),
  294. ];
  295. if (type === 'date') {
  296. values.push(formatter('day', padZero(date.getDate())));
  297. }
  298. if (type === 'datetime') {
  299. values.push(
  300. formatter('day', padZero(date.getDate())),
  301. formatter('hour', padZero(date.getHours())),
  302. formatter('minute', padZero(date.getMinutes()))
  303. );
  304. }
  305. }
  306. return this.set({ innerValue: value })
  307. .then(() => this.updateColumns())
  308. .then(() => picker.setValues(values));
  309. },
  310. },
  311. created() {
  312. const innerValue = this.correctValue(this.data.value);
  313. this.updateColumnValue(innerValue).then(() => {
  314. this.$emit('input', innerValue);
  315. });
  316. },
  317. });