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.

VTimePickerClock.js 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  6. // Mixins
  7. require('../../../src/stylus/components/_time-picker-clock.styl');
  8. var _colorable = require('../../mixins/colorable');
  9. var _colorable2 = _interopRequireDefault(_colorable);
  10. var _themeable = require('../../mixins/themeable');
  11. var _themeable2 = _interopRequireDefault(_themeable);
  12. var _mixins = require('../../util/mixins');
  13. var _mixins2 = _interopRequireDefault(_mixins);
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. exports.default = (0, _mixins2.default)(_colorable2.default, _themeable2.default
  16. /* @vue/component */
  17. ).extend({
  18. name: 'v-time-picker-clock',
  19. props: {
  20. allowedValues: Function,
  21. disabled: Boolean,
  22. double: Boolean,
  23. format: {
  24. type: Function,
  25. default: function _default(val) {
  26. return val;
  27. }
  28. },
  29. max: {
  30. type: Number,
  31. required: true
  32. },
  33. min: {
  34. type: Number,
  35. required: true
  36. },
  37. scrollable: Boolean,
  38. readonly: Boolean,
  39. rotate: {
  40. type: Number,
  41. default: 0
  42. },
  43. step: {
  44. type: Number,
  45. default: 1
  46. },
  47. value: Number
  48. },
  49. data: function data() {
  50. return {
  51. inputValue: this.value,
  52. isDragging: false,
  53. valueOnMouseDown: null,
  54. valueOnMouseUp: null
  55. };
  56. },
  57. computed: {
  58. count: function count() {
  59. return this.max - this.min + 1;
  60. },
  61. degreesPerUnit: function degreesPerUnit() {
  62. return 360 / this.roundCount;
  63. },
  64. degrees: function degrees() {
  65. return this.degreesPerUnit * Math.PI / 180;
  66. },
  67. displayedValue: function displayedValue() {
  68. return this.value == null ? this.min : this.value;
  69. },
  70. innerRadiusScale: function innerRadiusScale() {
  71. return 0.62;
  72. },
  73. roundCount: function roundCount() {
  74. return this.double ? this.count / 2 : this.count;
  75. }
  76. },
  77. watch: {
  78. value: function value(_value) {
  79. this.inputValue = _value;
  80. }
  81. },
  82. methods: {
  83. wheel: function wheel(e) {
  84. e.preventDefault();
  85. var delta = Math.sign(-e.deltaY || 1);
  86. var value = this.displayedValue;
  87. do {
  88. value = value + delta;
  89. value = (value - this.min + this.count) % this.count + this.min;
  90. } while (!this.isAllowed(value) && value !== this.displayedValue);
  91. if (value !== this.displayedValue) {
  92. this.update(value);
  93. }
  94. },
  95. isInner: function isInner(value) {
  96. return this.double && value - this.min >= this.roundCount;
  97. },
  98. handScale: function handScale(value) {
  99. return this.isInner(value) ? this.innerRadiusScale : 1;
  100. },
  101. isAllowed: function isAllowed(value) {
  102. return !this.allowedValues || this.allowedValues(value);
  103. },
  104. genValues: function genValues() {
  105. var children = [];
  106. for (var value = this.min; value <= this.max; value = value + this.step) {
  107. var color = value === this.value && (this.color || 'accent');
  108. children.push(this.$createElement('span', this.setBackgroundColor(color, {
  109. staticClass: 'v-time-picker-clock__item',
  110. 'class': {
  111. 'v-time-picker-clock__item--active': value === this.displayedValue,
  112. 'v-time-picker-clock__item--disabled': this.disabled || !this.isAllowed(value)
  113. },
  114. style: this.getTransform(value),
  115. domProps: { innerHTML: '<span>' + this.format(value) + '</span>' }
  116. })));
  117. }
  118. return children;
  119. },
  120. genHand: function genHand() {
  121. var scale = 'scaleY(' + this.handScale(this.displayedValue) + ')';
  122. var angle = this.rotate + this.degreesPerUnit * (this.displayedValue - this.min);
  123. var color = this.value != null && (this.color || 'accent');
  124. return this.$createElement('div', this.setBackgroundColor(color, {
  125. staticClass: 'v-time-picker-clock__hand',
  126. 'class': {
  127. 'v-time-picker-clock__hand--inner': this.isInner(this.value)
  128. },
  129. style: {
  130. transform: 'rotate(' + angle + 'deg) ' + scale
  131. }
  132. }));
  133. },
  134. getTransform: function getTransform(i) {
  135. var _getPosition = this.getPosition(i),
  136. x = _getPosition.x,
  137. y = _getPosition.y;
  138. return {
  139. left: 50 + x * 50 + '%',
  140. top: 50 + y * 50 + '%'
  141. };
  142. },
  143. getPosition: function getPosition(value) {
  144. var rotateRadians = this.rotate * Math.PI / 180;
  145. return {
  146. x: Math.sin((value - this.min) * this.degrees + rotateRadians) * this.handScale(value),
  147. y: -Math.cos((value - this.min) * this.degrees + rotateRadians) * this.handScale(value)
  148. };
  149. },
  150. onMouseDown: function onMouseDown(e) {
  151. e.preventDefault();
  152. this.valueOnMouseDown = null;
  153. this.valueOnMouseUp = null;
  154. this.isDragging = true;
  155. this.onDragMove(e);
  156. },
  157. onMouseUp: function onMouseUp() {
  158. this.isDragging = false;
  159. if (this.valueOnMouseUp !== null && this.isAllowed(this.valueOnMouseUp)) {
  160. this.$emit('change', this.valueOnMouseUp);
  161. }
  162. },
  163. onDragMove: function onDragMove(e) {
  164. e.preventDefault();
  165. if (!this.isDragging && e.type !== 'click') return;
  166. var _$refs$clock$getBound = this.$refs.clock.getBoundingClientRect(),
  167. width = _$refs$clock$getBound.width,
  168. top = _$refs$clock$getBound.top,
  169. left = _$refs$clock$getBound.left;
  170. var _$refs$innerClock$get = this.$refs.innerClock.getBoundingClientRect(),
  171. innerWidth = _$refs$innerClock$get.width;
  172. var _ref = 'touches' in e ? e.touches[0] : e,
  173. clientX = _ref.clientX,
  174. clientY = _ref.clientY;
  175. var center = { x: width / 2, y: -width / 2 };
  176. var coords = { x: clientX - left, y: top - clientY };
  177. var handAngle = Math.round(this.angle(center, coords) - this.rotate + 360) % 360;
  178. var insideClick = this.double && this.euclidean(center, coords) < (innerWidth + innerWidth * this.innerRadiusScale) / 4;
  179. var value = (Math.round(handAngle / this.degreesPerUnit) + (insideClick ? this.roundCount : 0)) % this.count + this.min;
  180. // Necessary to fix edge case when selecting left part of the value(s) at 12 o'clock
  181. var newValue = void 0;
  182. if (handAngle >= 360 - this.degreesPerUnit / 2) {
  183. newValue = insideClick ? this.max - this.roundCount + 1 : this.min;
  184. } else {
  185. newValue = value;
  186. }
  187. if (this.isAllowed(value)) {
  188. if (this.valueOnMouseDown === null) {
  189. this.valueOnMouseDown = newValue;
  190. }
  191. this.valueOnMouseUp = newValue;
  192. this.update(newValue);
  193. }
  194. },
  195. update: function update(value) {
  196. if (this.inputValue !== value) {
  197. this.inputValue = value;
  198. this.$emit('input', value);
  199. }
  200. },
  201. euclidean: function euclidean(p0, p1) {
  202. var dx = p1.x - p0.x;
  203. var dy = p1.y - p0.y;
  204. return Math.sqrt(dx * dx + dy * dy);
  205. },
  206. angle: function angle(center, p1) {
  207. var value = 2 * Math.atan2(p1.y - center.y - this.euclidean(center, p1), p1.x - center.x);
  208. return Math.abs(value * 180 / Math.PI);
  209. }
  210. },
  211. render: function render(h) {
  212. var _this = this;
  213. var data = {
  214. staticClass: 'v-time-picker-clock',
  215. class: _extends({
  216. 'v-time-picker-clock--indeterminate': this.value == null
  217. }, this.themeClasses),
  218. on: this.readonly || this.disabled ? undefined : Object.assign({
  219. mousedown: this.onMouseDown,
  220. mouseup: this.onMouseUp,
  221. mouseleave: function mouseleave() {
  222. return _this.isDragging && _this.onMouseUp();
  223. },
  224. touchstart: this.onMouseDown,
  225. touchend: this.onMouseUp,
  226. mousemove: this.onDragMove,
  227. touchmove: this.onDragMove
  228. }, this.scrollable ? {
  229. wheel: this.wheel
  230. } : {}),
  231. ref: 'clock'
  232. };
  233. return h('div', data, [h('div', {
  234. staticClass: 'v-time-picker-clock__inner',
  235. ref: 'innerClock'
  236. }, [this.genHand(), this.genValues()])]);
  237. }
  238. });
  239. //# sourceMappingURL=VTimePickerClock.js.map