Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VDatePicker.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
  2. // Components
  3. import VDatePickerTitle from './VDatePickerTitle';
  4. import VDatePickerHeader from './VDatePickerHeader';
  5. import VDatePickerDateTable from './VDatePickerDateTable';
  6. import VDatePickerMonthTable from './VDatePickerMonthTable';
  7. import VDatePickerYears from './VDatePickerYears';
  8. // Mixins
  9. import Picker from '../../mixins/picker';
  10. // Utils
  11. import { pad, createNativeLocaleFormatter } from './util';
  12. import _isDateAllowed from './util/isDateAllowed';
  13. import { consoleWarn } from '../../util/console';
  14. import { daysInMonth } from '../VCalendar/util/timestamp';
  15. import mixins from '../../util/mixins';
  16. // Adds leading zero to month/day if necessary, returns 'YYYY' if type = 'year',
  17. // 'YYYY-MM' if 'month' and 'YYYY-MM-DD' if 'date'
  18. function sanitizeDateString(dateString, type) {
  19. var _dateString$split = dateString.split('-'),
  20. _dateString$split2 = _slicedToArray(_dateString$split, 3),
  21. year = _dateString$split2[0],
  22. _dateString$split2$ = _dateString$split2[1],
  23. month = _dateString$split2$ === undefined ? 1 : _dateString$split2$,
  24. _dateString$split2$2 = _dateString$split2[2],
  25. date = _dateString$split2$2 === undefined ? 1 : _dateString$split2$2;
  26. return (year + '-' + pad(month) + '-' + pad(date)).substr(0, { date: 10, month: 7, year: 4 }[type]);
  27. }
  28. export default mixins(Picker
  29. /* @vue/component */
  30. ).extend({
  31. name: 'v-date-picker',
  32. props: {
  33. allowedDates: Function,
  34. // Function formatting the day in date picker table
  35. dayFormat: Function,
  36. disabled: Boolean,
  37. events: {
  38. type: [Array, Function, Object],
  39. default: function _default() {
  40. return null;
  41. }
  42. },
  43. eventColor: {
  44. type: [Array, Function, Object, String],
  45. default: function _default() {
  46. return 'warning';
  47. }
  48. },
  49. firstDayOfWeek: {
  50. type: [String, Number],
  51. default: 0
  52. },
  53. // Function formatting the tableDate in the day/month table header
  54. headerDateFormat: Function,
  55. locale: {
  56. type: String,
  57. default: 'en-us'
  58. },
  59. max: String,
  60. min: String,
  61. // Function formatting month in the months table
  62. monthFormat: Function,
  63. multiple: Boolean,
  64. nextIcon: {
  65. type: String,
  66. default: '$vuetify.icons.next'
  67. },
  68. pickerDate: String,
  69. prevIcon: {
  70. type: String,
  71. default: '$vuetify.icons.prev'
  72. },
  73. reactive: Boolean,
  74. readonly: Boolean,
  75. scrollable: Boolean,
  76. showCurrent: {
  77. type: [Boolean, String],
  78. default: true
  79. },
  80. showWeek: Boolean,
  81. // Function formatting currently selected date in the picker title
  82. titleDateFormat: Function,
  83. type: {
  84. type: String,
  85. default: 'date',
  86. validator: function validator(type) {
  87. return ['date', 'month'].includes(type);
  88. } // TODO: year
  89. },
  90. value: [Array, String],
  91. weekdayFormat: Function,
  92. // Function formatting the year in table header and pickup title
  93. yearFormat: Function,
  94. yearIcon: String
  95. },
  96. data: function data() {
  97. var _this = this;
  98. var now = new Date();
  99. return {
  100. activePicker: this.type.toUpperCase(),
  101. inputDay: null,
  102. inputMonth: null,
  103. inputYear: null,
  104. isReversing: false,
  105. now: now,
  106. // tableDate is a string in 'YYYY' / 'YYYY-M' format (leading zero for month is not required)
  107. tableDate: function () {
  108. if (_this.pickerDate) {
  109. return _this.pickerDate;
  110. }
  111. var date = (_this.multiple ? _this.value[_this.value.length - 1] : _this.value) || now.getFullYear() + '-' + (now.getMonth() + 1);
  112. return sanitizeDateString(date, _this.type === 'date' ? 'month' : 'year');
  113. }()
  114. };
  115. },
  116. computed: {
  117. lastValue: function lastValue() {
  118. return this.multiple ? this.value[this.value.length - 1] : this.value;
  119. },
  120. selectedMonths: function selectedMonths() {
  121. if (!this.value || !this.value.length || this.type === 'month') {
  122. return this.value;
  123. } else if (this.multiple) {
  124. return this.value.map(function (val) {
  125. return val.substr(0, 7);
  126. });
  127. } else {
  128. return this.value.substr(0, 7);
  129. }
  130. },
  131. current: function current() {
  132. if (this.showCurrent === true) {
  133. return sanitizeDateString(this.now.getFullYear() + '-' + (this.now.getMonth() + 1) + '-' + this.now.getDate(), this.type);
  134. }
  135. return this.showCurrent || null;
  136. },
  137. inputDate: function inputDate() {
  138. return this.type === 'date' ? this.inputYear + '-' + pad(this.inputMonth + 1) + '-' + pad(this.inputDay) : this.inputYear + '-' + pad(this.inputMonth + 1);
  139. },
  140. tableMonth: function tableMonth() {
  141. return Number((this.pickerDate || this.tableDate).split('-')[1]) - 1;
  142. },
  143. tableYear: function tableYear() {
  144. return Number((this.pickerDate || this.tableDate).split('-')[0]);
  145. },
  146. minMonth: function minMonth() {
  147. return this.min ? sanitizeDateString(this.min, 'month') : null;
  148. },
  149. maxMonth: function maxMonth() {
  150. return this.max ? sanitizeDateString(this.max, 'month') : null;
  151. },
  152. minYear: function minYear() {
  153. return this.min ? sanitizeDateString(this.min, 'year') : null;
  154. },
  155. maxYear: function maxYear() {
  156. return this.max ? sanitizeDateString(this.max, 'year') : null;
  157. },
  158. formatters: function formatters() {
  159. return {
  160. year: this.yearFormat || createNativeLocaleFormatter(this.locale, { year: 'numeric', timeZone: 'UTC' }, { length: 4 }),
  161. titleDate: this.titleDateFormat || (this.multiple ? this.defaultTitleMultipleDateFormatter : this.defaultTitleDateFormatter)
  162. };
  163. },
  164. defaultTitleMultipleDateFormatter: function defaultTitleMultipleDateFormatter() {
  165. var _this2 = this;
  166. if (this.value.length < 2) {
  167. return function (dates) {
  168. return dates.length ? _this2.defaultTitleDateFormatter(dates[0]) : '0 selected';
  169. };
  170. }
  171. return function (dates) {
  172. return dates.length + ' selected';
  173. };
  174. },
  175. defaultTitleDateFormatter: function defaultTitleDateFormatter() {
  176. var titleFormats = {
  177. year: { year: 'numeric', timeZone: 'UTC' },
  178. month: { month: 'long', timeZone: 'UTC' },
  179. date: { weekday: 'short', month: 'short', day: 'numeric', timeZone: 'UTC' }
  180. };
  181. var titleDateFormatter = createNativeLocaleFormatter(this.locale, titleFormats[this.type], {
  182. start: 0,
  183. length: { date: 10, month: 7, year: 4 }[this.type]
  184. });
  185. var landscapeFormatter = function landscapeFormatter(date) {
  186. return titleDateFormatter(date).replace(/([^\d\s])([\d])/g, function (match, nonDigit, digit) {
  187. return nonDigit + ' ' + digit;
  188. }).replace(', ', ',<br>');
  189. };
  190. return this.landscape ? landscapeFormatter : titleDateFormatter;
  191. }
  192. },
  193. watch: {
  194. tableDate: function tableDate(val, prev) {
  195. // Make a ISO 8601 strings from val and prev for comparision, otherwise it will incorrectly
  196. // compare for example '2000-9' and '2000-10'
  197. var sanitizeType = this.type === 'month' ? 'year' : 'month';
  198. this.isReversing = sanitizeDateString(val, sanitizeType) < sanitizeDateString(prev, sanitizeType);
  199. this.$emit('update:pickerDate', val);
  200. },
  201. pickerDate: function pickerDate(val) {
  202. if (val) {
  203. this.tableDate = val;
  204. } else if (this.lastValue && this.type === 'date') {
  205. this.tableDate = sanitizeDateString(this.lastValue, 'month');
  206. } else if (this.lastValue && this.type === 'month') {
  207. this.tableDate = sanitizeDateString(this.lastValue, 'year');
  208. }
  209. },
  210. value: function value(newValue, oldValue) {
  211. this.checkMultipleProp();
  212. this.setInputDate();
  213. if (!this.multiple && this.value && !this.pickerDate) {
  214. this.tableDate = sanitizeDateString(this.inputDate, this.type === 'month' ? 'year' : 'month');
  215. } else if (this.multiple && this.value.length && !oldValue.length && !this.pickerDate) {
  216. this.tableDate = sanitizeDateString(this.inputDate, this.type === 'month' ? 'year' : 'month');
  217. }
  218. },
  219. type: function type(_type) {
  220. this.activePicker = _type.toUpperCase();
  221. if (this.value && this.value.length) {
  222. var output = (this.multiple ? this.value : [this.value]).map(function (val) {
  223. return sanitizeDateString(val, _type);
  224. }).filter(this.isDateAllowed);
  225. this.$emit('input', this.multiple ? output : output[0]);
  226. }
  227. }
  228. },
  229. created: function created() {
  230. this.checkMultipleProp();
  231. if (this.pickerDate !== this.tableDate) {
  232. this.$emit('update:pickerDate', this.tableDate);
  233. }
  234. this.setInputDate();
  235. },
  236. methods: {
  237. emitInput: function emitInput(newInput) {
  238. var output = this.multiple ? this.value.indexOf(newInput) === -1 ? this.value.concat([newInput]) : this.value.filter(function (x) {
  239. return x !== newInput;
  240. }) : newInput;
  241. this.$emit('input', output);
  242. this.multiple || this.$emit('change', newInput);
  243. },
  244. checkMultipleProp: function checkMultipleProp() {
  245. if (this.value == null) return;
  246. var valueType = this.value.constructor.name;
  247. var expected = this.multiple ? 'Array' : 'String';
  248. if (valueType !== expected) {
  249. consoleWarn('Value must be ' + (this.multiple ? 'an' : 'a') + ' ' + expected + ', got ' + valueType, this);
  250. }
  251. },
  252. isDateAllowed: function isDateAllowed(value) {
  253. return _isDateAllowed(value, this.min, this.max, this.allowedDates);
  254. },
  255. yearClick: function yearClick(value) {
  256. this.inputYear = value;
  257. if (this.type === 'month') {
  258. this.tableDate = '' + value;
  259. } else {
  260. this.tableDate = value + '-' + pad((this.tableMonth || 0) + 1);
  261. }
  262. this.activePicker = 'MONTH';
  263. if (this.reactive && !this.readonly && !this.multiple && this.isDateAllowed(this.inputDate)) {
  264. this.$emit('input', this.inputDate);
  265. }
  266. },
  267. monthClick: function monthClick(value) {
  268. this.inputYear = parseInt(value.split('-')[0], 10);
  269. this.inputMonth = parseInt(value.split('-')[1], 10) - 1;
  270. if (this.type === 'date') {
  271. if (this.inputDay) {
  272. this.inputDay = Math.min(this.inputDay, daysInMonth(this.inputYear, this.inputMonth + 1));
  273. }
  274. this.tableDate = value;
  275. this.activePicker = 'DATE';
  276. if (this.reactive && !this.readonly && !this.multiple && this.isDateAllowed(this.inputDate)) {
  277. this.$emit('input', this.inputDate);
  278. }
  279. } else {
  280. this.emitInput(this.inputDate);
  281. }
  282. },
  283. dateClick: function dateClick(value) {
  284. this.inputYear = parseInt(value.split('-')[0], 10);
  285. this.inputMonth = parseInt(value.split('-')[1], 10) - 1;
  286. this.inputDay = parseInt(value.split('-')[2], 10);
  287. this.emitInput(this.inputDate);
  288. },
  289. genPickerTitle: function genPickerTitle() {
  290. var _this3 = this;
  291. return this.$createElement(VDatePickerTitle, {
  292. props: {
  293. date: this.value ? this.formatters.titleDate(this.value) : '',
  294. disabled: this.disabled,
  295. readonly: this.readonly,
  296. selectingYear: this.activePicker === 'YEAR',
  297. year: this.formatters.year(this.value ? '' + this.inputYear : this.tableDate),
  298. yearIcon: this.yearIcon,
  299. value: this.multiple ? this.value[0] : this.value
  300. },
  301. slot: 'title',
  302. on: {
  303. 'update:selectingYear': function updateSelectingYear(value) {
  304. return _this3.activePicker = value ? 'YEAR' : _this3.type.toUpperCase();
  305. }
  306. }
  307. });
  308. },
  309. genTableHeader: function genTableHeader() {
  310. var _this4 = this;
  311. return this.$createElement(VDatePickerHeader, {
  312. props: {
  313. nextIcon: this.nextIcon,
  314. color: this.color,
  315. dark: this.dark,
  316. disabled: this.disabled,
  317. format: this.headerDateFormat,
  318. light: this.light,
  319. locale: this.locale,
  320. min: this.activePicker === 'DATE' ? this.minMonth : this.minYear,
  321. max: this.activePicker === 'DATE' ? this.maxMonth : this.maxYear,
  322. prevIcon: this.prevIcon,
  323. readonly: this.readonly,
  324. value: this.activePicker === 'DATE' ? pad(this.tableYear, 4) + '-' + pad(this.tableMonth + 1) : '' + pad(this.tableYear, 4)
  325. },
  326. on: {
  327. toggle: function toggle() {
  328. return _this4.activePicker = _this4.activePicker === 'DATE' ? 'MONTH' : 'YEAR';
  329. },
  330. input: function input(value) {
  331. return _this4.tableDate = value;
  332. }
  333. }
  334. });
  335. },
  336. genDateTable: function genDateTable() {
  337. var _this5 = this;
  338. return this.$createElement(VDatePickerDateTable, {
  339. props: {
  340. allowedDates: this.allowedDates,
  341. color: this.color,
  342. current: this.current,
  343. dark: this.dark,
  344. disabled: this.disabled,
  345. events: this.events,
  346. eventColor: this.eventColor,
  347. firstDayOfWeek: this.firstDayOfWeek,
  348. format: this.dayFormat,
  349. light: this.light,
  350. locale: this.locale,
  351. min: this.min,
  352. max: this.max,
  353. readonly: this.readonly,
  354. scrollable: this.scrollable,
  355. showWeek: this.showWeek,
  356. tableDate: pad(this.tableYear, 4) + '-' + pad(this.tableMonth + 1),
  357. value: this.value,
  358. weekdayFormat: this.weekdayFormat
  359. },
  360. ref: 'table',
  361. on: {
  362. input: this.dateClick,
  363. tableDate: function tableDate(value) {
  364. return _this5.tableDate = value;
  365. },
  366. 'click:date': function clickDate(value) {
  367. return _this5.$emit('click:date', value);
  368. },
  369. 'dblclick:date': function dblclickDate(value) {
  370. return _this5.$emit('dblclick:date', value);
  371. }
  372. }
  373. });
  374. },
  375. genMonthTable: function genMonthTable() {
  376. var _this6 = this;
  377. return this.$createElement(VDatePickerMonthTable, {
  378. props: {
  379. allowedDates: this.type === 'month' ? this.allowedDates : null,
  380. color: this.color,
  381. current: this.current ? sanitizeDateString(this.current, 'month') : null,
  382. dark: this.dark,
  383. disabled: this.disabled,
  384. events: this.type === 'month' ? this.events : null,
  385. eventColor: this.type === 'month' ? this.eventColor : null,
  386. format: this.monthFormat,
  387. light: this.light,
  388. locale: this.locale,
  389. min: this.minMonth,
  390. max: this.maxMonth,
  391. readonly: this.readonly && this.type === 'month',
  392. scrollable: this.scrollable,
  393. value: this.selectedMonths,
  394. tableDate: '' + pad(this.tableYear, 4)
  395. },
  396. ref: 'table',
  397. on: {
  398. input: this.monthClick,
  399. tableDate: function tableDate(value) {
  400. return _this6.tableDate = value;
  401. },
  402. 'click:month': function clickMonth(value) {
  403. return _this6.$emit('click:month', value);
  404. },
  405. 'dblclick:month': function dblclickMonth(value) {
  406. return _this6.$emit('dblclick:month', value);
  407. }
  408. }
  409. });
  410. },
  411. genYears: function genYears() {
  412. return this.$createElement(VDatePickerYears, {
  413. props: {
  414. color: this.color,
  415. format: this.yearFormat,
  416. locale: this.locale,
  417. min: this.minYear,
  418. max: this.maxYear,
  419. value: this.tableYear
  420. },
  421. on: {
  422. input: this.yearClick
  423. }
  424. });
  425. },
  426. genPickerBody: function genPickerBody() {
  427. var children = this.activePicker === 'YEAR' ? [this.genYears()] : [this.genTableHeader(), this.activePicker === 'DATE' ? this.genDateTable() : this.genMonthTable()];
  428. return this.$createElement('div', {
  429. key: this.activePicker
  430. }, children);
  431. },
  432. setInputDate: function setInputDate() {
  433. if (this.lastValue) {
  434. var array = this.lastValue.split('-');
  435. this.inputYear = parseInt(array[0], 10);
  436. this.inputMonth = parseInt(array[1], 10) - 1;
  437. if (this.type === 'date') {
  438. this.inputDay = parseInt(array[2], 10);
  439. }
  440. } else {
  441. this.inputYear = this.inputYear || this.now.getFullYear();
  442. this.inputMonth = this.inputMonth == null ? this.inputMonth : this.now.getMonth();
  443. this.inputDay = this.inputDay || this.now.getDate();
  444. }
  445. }
  446. },
  447. render: function render() {
  448. return this.genPicker('v-picker--date');
  449. }
  450. });
  451. //# sourceMappingURL=VDatePicker.js.map