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 21KB

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