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.0KB

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