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.

VTimePicker.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.selectingTimes = undefined;
  6. 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
  7. // Mixins
  8. // Utils
  9. var _VTimePickerTitle = require('./VTimePickerTitle');
  10. var _VTimePickerTitle2 = _interopRequireDefault(_VTimePickerTitle);
  11. var _VTimePickerClock = require('./VTimePickerClock');
  12. var _VTimePickerClock2 = _interopRequireDefault(_VTimePickerClock);
  13. var _picker = require('../../mixins/picker');
  14. var _picker2 = _interopRequireDefault(_picker);
  15. var _helpers = require('../../util/helpers');
  16. var _pad = require('../VDatePicker/util/pad');
  17. var _pad2 = _interopRequireDefault(_pad);
  18. var _mixins = require('../../util/mixins');
  19. var _mixins2 = _interopRequireDefault(_mixins);
  20. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  21. var rangeHours24 = (0, _helpers.createRange)(24);
  22. var rangeHours12am = (0, _helpers.createRange)(12);
  23. var rangeHours12pm = rangeHours12am.map(function (v) {
  24. return v + 12;
  25. });
  26. var range60 = (0, _helpers.createRange)(60);
  27. var selectingTimes = { hour: 1, minute: 2, second: 3 };
  28. var selectingNames = { 1: 'hour', 2: 'minute', 3: 'second' };
  29. exports.selectingTimes = selectingTimes;
  30. exports.default = (0, _mixins2.default)(_picker2.default
  31. /* @vue/component */
  32. ).extend({
  33. name: 'v-time-picker',
  34. props: {
  35. allowedHours: Function,
  36. allowedMinutes: Function,
  37. allowedSeconds: Function,
  38. disabled: Boolean,
  39. format: {
  40. type: String,
  41. default: 'ampm',
  42. validator: function validator(val) {
  43. return ['ampm', '24hr'].includes(val);
  44. }
  45. },
  46. min: String,
  47. max: String,
  48. readonly: Boolean,
  49. scrollable: Boolean,
  50. useSeconds: Boolean,
  51. value: null
  52. },
  53. data: function data() {
  54. return {
  55. inputHour: null,
  56. inputMinute: null,
  57. inputSecond: null,
  58. lazyInputHour: null,
  59. lazyInputMinute: null,
  60. lazyInputSecond: null,
  61. period: 'am',
  62. selecting: selectingTimes.hour
  63. };
  64. },
  65. computed: {
  66. selectingHour: {
  67. get: function get() {
  68. return this.selecting === selectingTimes.hour;
  69. },
  70. set: function set(v) {
  71. this.selecting = selectingTimes.hour;
  72. }
  73. },
  74. selectingMinute: {
  75. get: function get() {
  76. return this.selecting === selectingTimes.minute;
  77. },
  78. set: function set(v) {
  79. this.selecting = selectingTimes.minute;
  80. }
  81. },
  82. selectingSecond: {
  83. get: function get() {
  84. return this.selecting === selectingTimes.second;
  85. },
  86. set: function set(v) {
  87. this.selecting = selectingTimes.second;
  88. }
  89. },
  90. isAllowedHourCb: function isAllowedHourCb() {
  91. var _this = this;
  92. if (!this.min && !this.max) return this.allowedHours;
  93. var minHour = this.min ? Number(this.min.split(':')[0]) : 0;
  94. var maxHour = this.max ? Number(this.max.split(':')[0]) : 23;
  95. return function (val) {
  96. return val >= minHour * 1 && val <= maxHour * 1 && (!_this.allowedHours || _this.allowedHours(val));
  97. };
  98. },
  99. isAllowedMinuteCb: function isAllowedMinuteCb() {
  100. var _this2 = this;
  101. var isHourAllowed = !this.allowedHours || this.allowedHours(this.inputHour);
  102. if (!this.min && !this.max) {
  103. return isHourAllowed ? this.allowedMinutes : function () {
  104. return false;
  105. };
  106. }
  107. var _ref = this.min ? this.min.split(':').map(Number) : [0, 0],
  108. _ref2 = _slicedToArray(_ref, 2),
  109. minHour = _ref2[0],
  110. minMinute = _ref2[1];
  111. var _ref3 = this.max ? this.max.split(':').map(Number) : [23, 59],
  112. _ref4 = _slicedToArray(_ref3, 2),
  113. maxHour = _ref4[0],
  114. maxMinute = _ref4[1];
  115. var minTime = minHour * 60 + minMinute * 1;
  116. var maxTime = maxHour * 60 + maxMinute * 1;
  117. return function (val) {
  118. var time = 60 * _this2.inputHour + val;
  119. return time >= minTime && time <= maxTime && isHourAllowed && (!_this2.allowedMinutes || _this2.allowedMinutes(val));
  120. };
  121. },
  122. isAllowedSecondCb: function isAllowedSecondCb() {
  123. var _this3 = this;
  124. var isHourAllowed = !this.allowedHours || this.allowedHours(this.inputHour);
  125. var isMinuteAllowed = !this.allowedMinutes || this.allowedMinutes(this.inputMinute);
  126. if (!this.min && !this.max) {
  127. return isHourAllowed && isMinuteAllowed ? this.allowedSeconds : function () {
  128. return false;
  129. };
  130. }
  131. var _ref5 = this.min ? this.min.split(':').map(Number) : [0, 0, 0],
  132. _ref6 = _slicedToArray(_ref5, 3),
  133. minHour = _ref6[0],
  134. minMinute = _ref6[1],
  135. minSecond = _ref6[2];
  136. var _ref7 = this.max ? this.max.split(':').map(Number) : [23, 59, 59],
  137. _ref8 = _slicedToArray(_ref7, 3),
  138. maxHour = _ref8[0],
  139. maxMinute = _ref8[1],
  140. maxSecond = _ref8[2];
  141. var minTime = minHour * 3600 + minMinute * 60 + (minSecond || 0) * 1;
  142. var maxTime = maxHour * 3600 + maxMinute * 60 + (maxSecond || 0) * 1;
  143. return function (val) {
  144. var time = 3600 * _this3.inputHour + 60 * _this3.inputMinute + val;
  145. return time >= minTime && time <= maxTime && isHourAllowed && isMinuteAllowed && (!_this3.allowedSeconds || _this3.allowedSeconds(val));
  146. };
  147. },
  148. isAmPm: function isAmPm() {
  149. return this.format === 'ampm';
  150. }
  151. },
  152. watch: {
  153. value: 'setInputData'
  154. },
  155. mounted: function mounted() {
  156. this.setInputData(this.value);
  157. },
  158. methods: {
  159. genValue: function genValue() {
  160. if (this.inputHour != null && this.inputMinute != null && (!this.useSeconds || this.inputSecond != null)) {
  161. return (0, _pad2.default)(this.inputHour) + ':' + (0, _pad2.default)(this.inputMinute) + (this.useSeconds ? ':' + (0, _pad2.default)(this.inputSecond) : '');
  162. }
  163. return null;
  164. },
  165. emitValue: function emitValue() {
  166. var value = this.genValue();
  167. if (value !== null) this.$emit('input', value);
  168. },
  169. setPeriod: function setPeriod(period) {
  170. this.period = period;
  171. if (this.inputHour != null) {
  172. var newHour = this.inputHour + (period === 'am' ? -12 : 12);
  173. this.inputHour = this.firstAllowed('hour', newHour);
  174. this.emitValue();
  175. }
  176. },
  177. setInputData: function setInputData(value) {
  178. if (value == null || value === '') {
  179. this.inputHour = null;
  180. this.inputMinute = null;
  181. this.inputSecond = null;
  182. } else if (value instanceof Date) {
  183. this.inputHour = value.getHours();
  184. this.inputMinute = value.getMinutes();
  185. this.inputSecond = value.getSeconds();
  186. } else {
  187. var _ref9 = value.trim().toLowerCase().match(/^(\d+):(\d+)(:(\d+))?([ap]m)?$/) || new Array(6),
  188. _ref10 = _slicedToArray(_ref9, 6),
  189. hour = _ref10[1],
  190. minute = _ref10[2],
  191. second = _ref10[4],
  192. period = _ref10[5];
  193. this.inputHour = period ? this.convert12to24(parseInt(hour, 10), period) : parseInt(hour, 10);
  194. this.inputMinute = parseInt(minute, 10);
  195. this.inputSecond = parseInt(second || 0, 10);
  196. }
  197. this.period = this.inputHour == null || this.inputHour < 12 ? 'am' : 'pm';
  198. },
  199. convert24to12: function convert24to12(hour) {
  200. return hour ? (hour - 1) % 12 + 1 : 12;
  201. },
  202. convert12to24: function convert12to24(hour, period) {
  203. return hour % 12 + (period === 'pm' ? 12 : 0);
  204. },
  205. onInput: function onInput(value) {
  206. if (this.selecting === selectingTimes.hour) {
  207. this.inputHour = this.isAmPm ? this.convert12to24(value, this.period) : value;
  208. } else if (this.selecting === selectingTimes.minute) {
  209. this.inputMinute = value;
  210. } else {
  211. this.inputSecond = value;
  212. }
  213. this.emitValue();
  214. },
  215. onChange: function onChange(value) {
  216. this.$emit('click:' + selectingNames[this.selecting], value);
  217. var emitChange = this.selecting === (this.useSeconds ? selectingTimes.second : selectingTimes.minute);
  218. if (this.selecting === selectingTimes.hour) {
  219. this.selecting = selectingTimes.minute;
  220. } else if (this.useSeconds && this.selecting === selectingTimes.minute) {
  221. this.selecting = selectingTimes.second;
  222. }
  223. if (this.inputHour === this.lazyInputHour && this.inputMinute === this.lazyInputMinute && (!this.useSeconds || this.inputSecond === this.lazyInputSecond)) return;
  224. var time = this.genValue();
  225. if (time === null) return;
  226. this.lazyInputHour = this.inputHour;
  227. this.lazyInputMinute = this.inputMinute;
  228. this.useSeconds && (this.lazyInputSecond = this.inputSecond);
  229. emitChange && this.$emit('change', time);
  230. },
  231. firstAllowed: function firstAllowed(type, value) {
  232. var allowedFn = type === 'hour' ? this.isAllowedHourCb : type === 'minute' ? this.isAllowedMinuteCb : this.isAllowedSecondCb;
  233. if (!allowedFn) return value;
  234. // TODO: clean up
  235. var range = type === 'minute' ? range60 : type === 'second' ? range60 : this.isAmPm ? value < 12 ? rangeHours12am : rangeHours12pm : rangeHours24;
  236. var first = range.find(function (v) {
  237. return allowedFn((v + value) % range.length + range[0]);
  238. });
  239. return ((first || 0) + value) % range.length + range[0];
  240. },
  241. genClock: function genClock() {
  242. return this.$createElement(_VTimePickerClock2.default, {
  243. props: {
  244. allowedValues: this.selecting === selectingTimes.hour ? this.isAllowedHourCb : this.selecting === selectingTimes.minute ? this.isAllowedMinuteCb : this.isAllowedSecondCb,
  245. color: this.color,
  246. dark: this.dark,
  247. disabled: this.disabled,
  248. double: this.selecting === selectingTimes.hour && !this.isAmPm,
  249. format: this.selecting === selectingTimes.hour ? this.isAmPm ? this.convert24to12 : function (val) {
  250. return val;
  251. } : function (val) {
  252. return (0, _pad2.default)(val, 2);
  253. },
  254. light: this.light,
  255. max: this.selecting === selectingTimes.hour ? this.isAmPm && this.period === 'am' ? 11 : 23 : 59,
  256. min: this.selecting === selectingTimes.hour && this.isAmPm && this.period === 'pm' ? 12 : 0,
  257. readonly: this.readonly,
  258. scrollable: this.scrollable,
  259. size: Number(this.width) - (!this.fullWidth && this.landscape ? 80 : 20),
  260. step: this.selecting === selectingTimes.hour ? 1 : 5,
  261. value: this.selecting === selectingTimes.hour ? this.inputHour : this.selecting === selectingTimes.minute ? this.inputMinute : this.inputSecond
  262. },
  263. on: {
  264. input: this.onInput,
  265. change: this.onChange
  266. },
  267. ref: 'clock'
  268. });
  269. },
  270. genPickerBody: function genPickerBody() {
  271. return this.$createElement('div', {
  272. staticClass: 'v-time-picker-clock__container',
  273. key: this.selecting
  274. }, [this.genClock()]);
  275. },
  276. genPickerTitle: function genPickerTitle() {
  277. var _this4 = this;
  278. return this.$createElement(_VTimePickerTitle2.default, {
  279. props: {
  280. ampm: this.isAmPm,
  281. disabled: this.disabled,
  282. hour: this.inputHour,
  283. minute: this.inputMinute,
  284. second: this.inputSecond,
  285. period: this.period,
  286. readonly: this.readonly,
  287. useSeconds: this.useSeconds,
  288. selecting: this.selecting
  289. },
  290. on: {
  291. 'update:selecting': function updateSelecting(value) {
  292. return _this4.selecting = value;
  293. },
  294. 'update:period': this.setPeriod
  295. },
  296. ref: 'title',
  297. slot: 'title'
  298. });
  299. }
  300. },
  301. render: function render() {
  302. return this.genPicker('v-picker--time');
  303. }
  304. });
  305. //# sourceMappingURL=VTimePicker.js.map