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.

VSlider.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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; }; // Styles
  6. // Components
  7. // Extensions
  8. // Directives
  9. // Utilities
  10. require('../../../src/stylus/components/_sliders.styl');
  11. var _transitions = require('../transitions');
  12. var _VInput = require('../VInput');
  13. var _VInput2 = _interopRequireDefault(_VInput);
  14. var _clickOutside = require('../../directives/click-outside');
  15. var _clickOutside2 = _interopRequireDefault(_clickOutside);
  16. var _helpers = require('../../util/helpers');
  17. var _console = require('../../util/console');
  18. var _loadable = require('../../mixins/loadable');
  19. var _loadable2 = _interopRequireDefault(_loadable);
  20. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  21. /* @vue/component */
  22. exports.default = _VInput2.default.extend({
  23. name: 'v-slider',
  24. directives: { ClickOutside: _clickOutside2.default },
  25. mixins: [_loadable2.default],
  26. props: {
  27. alwaysDirty: Boolean,
  28. inverseLabel: Boolean,
  29. label: String,
  30. min: {
  31. type: [Number, String],
  32. default: 0
  33. },
  34. max: {
  35. type: [Number, String],
  36. default: 100
  37. },
  38. step: {
  39. type: [Number, String],
  40. default: 1
  41. },
  42. ticks: {
  43. type: [Boolean, String],
  44. default: false,
  45. validator: function validator(v) {
  46. return typeof v === 'boolean' || v === 'always';
  47. }
  48. },
  49. tickLabels: {
  50. type: Array,
  51. default: function _default() {
  52. return [];
  53. }
  54. },
  55. tickSize: {
  56. type: [Number, String],
  57. default: 1
  58. },
  59. thumbColor: {
  60. type: String,
  61. default: null
  62. },
  63. thumbLabel: {
  64. type: [Boolean, String],
  65. default: null,
  66. validator: function validator(v) {
  67. return typeof v === 'boolean' || v === 'always';
  68. }
  69. },
  70. thumbSize: {
  71. type: [Number, String],
  72. default: 32
  73. },
  74. trackColor: {
  75. type: String,
  76. default: null
  77. },
  78. value: [Number, String]
  79. },
  80. data: function data(vm) {
  81. return {
  82. app: {},
  83. isActive: false,
  84. keyPressed: 0,
  85. lazyValue: typeof vm.value !== 'undefined' ? vm.value : Number(vm.min),
  86. oldValue: null
  87. };
  88. },
  89. computed: {
  90. classes: function classes() {
  91. return {
  92. 'v-input--slider': true,
  93. 'v-input--slider--ticks': this.showTicks,
  94. 'v-input--slider--inverse-label': this.inverseLabel,
  95. 'v-input--slider--ticks-labels': this.tickLabels.length > 0,
  96. 'v-input--slider--thumb-label': this.thumbLabel || this.$scopedSlots.thumbLabel
  97. };
  98. },
  99. showTicks: function showTicks() {
  100. return this.tickLabels.length > 0 || !this.disabled && this.stepNumeric && !!this.ticks;
  101. },
  102. showThumbLabel: function showThumbLabel() {
  103. return !this.disabled && (!!this.thumbLabel || this.thumbLabel === '' || this.$scopedSlots['thumb-label']);
  104. },
  105. computedColor: function computedColor() {
  106. if (this.disabled) return null;
  107. return this.validationState || this.color || 'primary';
  108. },
  109. computedTrackColor: function computedTrackColor() {
  110. return this.disabled ? null : this.trackColor || null;
  111. },
  112. computedThumbColor: function computedThumbColor() {
  113. if (this.disabled || !this.isDirty) return null;
  114. return this.validationState || this.thumbColor || this.color || 'primary';
  115. },
  116. internalValue: {
  117. get: function get() {
  118. return this.lazyValue;
  119. },
  120. set: function set(val) {
  121. var min = this.min,
  122. max = this.max;
  123. // Round value to ensure the
  124. // entire slider range can
  125. // be selected with step
  126. var value = this.roundValue(Math.min(Math.max(val, min), max));
  127. if (value === this.lazyValue) return;
  128. this.lazyValue = value;
  129. this.$emit('input', value);
  130. this.validate();
  131. }
  132. },
  133. stepNumeric: function stepNumeric() {
  134. return this.step > 0 ? parseFloat(this.step) : 0;
  135. },
  136. trackFillStyles: function trackFillStyles() {
  137. var left = this.$vuetify.rtl ? 'auto' : 0;
  138. var right = this.$vuetify.rtl ? 0 : 'auto';
  139. var width = this.inputWidth + '%';
  140. if (this.disabled) width = 'calc(' + this.inputWidth + '% - 8px)';
  141. return {
  142. transition: this.trackTransition,
  143. left: left,
  144. right: right,
  145. width: width
  146. };
  147. },
  148. trackPadding: function trackPadding() {
  149. return this.isActive || this.inputWidth > 0 || this.disabled ? 0 : 7;
  150. },
  151. trackStyles: function trackStyles() {
  152. var trackPadding = this.disabled ? 'calc(' + this.inputWidth + '% + 8px)' : this.trackPadding + 'px';
  153. var left = this.$vuetify.rtl ? 'auto' : trackPadding;
  154. var right = this.$vuetify.rtl ? trackPadding : 'auto';
  155. var width = this.disabled ? 'calc(' + (100 - this.inputWidth) + '% - 8px)' : '100%';
  156. return {
  157. transition: this.trackTransition,
  158. left: left,
  159. right: right,
  160. width: width
  161. };
  162. },
  163. tickStyles: function tickStyles() {
  164. var size = Number(this.tickSize);
  165. return {
  166. 'border-width': size + 'px',
  167. 'border-radius': size > 1 ? '50%' : null,
  168. transform: size > 1 ? 'translateX(-' + size + 'px) translateY(-' + (size - 1) + 'px)' : null
  169. };
  170. },
  171. trackTransition: function trackTransition() {
  172. return this.keyPressed >= 2 ? 'none' : '';
  173. },
  174. numTicks: function numTicks() {
  175. return Math.ceil((this.max - this.min) / this.stepNumeric);
  176. },
  177. inputWidth: function inputWidth() {
  178. return (this.roundValue(this.internalValue) - this.min) / (this.max - this.min) * 100;
  179. },
  180. isDirty: function isDirty() {
  181. return this.internalValue > this.min || this.alwaysDirty;
  182. }
  183. },
  184. watch: {
  185. min: function min(val) {
  186. val > this.internalValue && this.$emit('input', parseFloat(val));
  187. },
  188. max: function max(val) {
  189. val < this.internalValue && this.$emit('input', parseFloat(val));
  190. },
  191. value: function value(val) {
  192. this.internalValue = val;
  193. }
  194. },
  195. mounted: function mounted() {
  196. // Without a v-app, iOS does not work with body selectors
  197. this.app = document.querySelector('[data-app]') || (0, _console.consoleWarn)('Missing v-app or a non-body wrapping element with the [data-app] attribute', this);
  198. },
  199. methods: {
  200. genDefaultSlot: function genDefaultSlot() {
  201. var children = [this.genLabel()];
  202. var slider = this.genSlider();
  203. this.inverseLabel ? children.unshift(slider) : children.push(slider);
  204. children.push(this.genProgress());
  205. return children;
  206. },
  207. genListeners: function genListeners() {
  208. return {
  209. blur: this.onBlur,
  210. click: this.onSliderClick,
  211. focus: this.onFocus,
  212. keydown: this.onKeyDown,
  213. keyup: this.onKeyUp
  214. };
  215. },
  216. genInput: function genInput() {
  217. return this.$createElement('input', {
  218. attrs: _extends({
  219. 'aria-label': this.label,
  220. name: this.name,
  221. role: 'slider',
  222. tabindex: this.disabled ? -1 : this.$attrs.tabindex,
  223. value: this.internalValue,
  224. readonly: true,
  225. 'aria-readonly': String(this.readonly),
  226. 'aria-valuemin': this.min,
  227. 'aria-valuemax': this.max,
  228. 'aria-valuenow': this.internalValue
  229. }, this.$attrs),
  230. on: this.genListeners(),
  231. ref: 'input'
  232. });
  233. },
  234. genSlider: function genSlider() {
  235. return this.$createElement('div', {
  236. staticClass: 'v-slider',
  237. 'class': {
  238. 'v-slider--is-active': this.isActive
  239. },
  240. directives: [{
  241. name: 'click-outside',
  242. value: this.onBlur
  243. }]
  244. }, this.genChildren());
  245. },
  246. genChildren: function genChildren() {
  247. return [this.genInput(), this.genTrackContainer(), this.genSteps(), this.genThumbContainer(this.internalValue, this.inputWidth, this.isFocused || this.isActive, this.onThumbMouseDown)];
  248. },
  249. genSteps: function genSteps() {
  250. var _this = this;
  251. if (!this.step || !this.showTicks) return null;
  252. var ticks = (0, _helpers.createRange)(this.numTicks + 1).map(function (i) {
  253. var children = [];
  254. if (_this.tickLabels[i]) {
  255. children.push(_this.$createElement('span', _this.tickLabels[i]));
  256. }
  257. return _this.$createElement('span', {
  258. key: i,
  259. staticClass: 'v-slider__ticks',
  260. class: {
  261. 'v-slider__ticks--always-show': _this.ticks === 'always' || _this.tickLabels.length > 0
  262. },
  263. style: _extends({}, _this.tickStyles, {
  264. left: i * (100 / _this.numTicks) + '%'
  265. })
  266. }, children);
  267. });
  268. return this.$createElement('div', {
  269. staticClass: 'v-slider__ticks-container'
  270. }, ticks);
  271. },
  272. genThumb: function genThumb() {
  273. return this.$createElement('div', this.setBackgroundColor(this.computedThumbColor, {
  274. staticClass: 'v-slider__thumb'
  275. }));
  276. },
  277. genThumbContainer: function genThumbContainer(value, valueWidth, isActive, onDrag) {
  278. var children = [this.genThumb()];
  279. var thumbLabelContent = this.getLabel(value);
  280. this.showThumbLabel && children.push(this.genThumbLabel(thumbLabelContent));
  281. return this.$createElement('div', this.setTextColor(this.computedThumbColor, {
  282. staticClass: 'v-slider__thumb-container',
  283. 'class': {
  284. 'v-slider__thumb-container--is-active': isActive,
  285. 'v-slider__thumb-container--show-label': this.showThumbLabel
  286. },
  287. style: {
  288. transition: this.trackTransition,
  289. left: (this.$vuetify.rtl ? 100 - valueWidth : valueWidth) + '%'
  290. },
  291. on: {
  292. touchstart: onDrag,
  293. mousedown: onDrag
  294. }
  295. }), children);
  296. },
  297. genThumbLabel: function genThumbLabel(content) {
  298. var size = (0, _helpers.convertToUnit)(this.thumbSize);
  299. return this.$createElement(_transitions.VScaleTransition, {
  300. props: { origin: 'bottom center' }
  301. }, [this.$createElement('div', {
  302. staticClass: 'v-slider__thumb-label__container',
  303. directives: [{
  304. name: 'show',
  305. value: this.isFocused || this.isActive || this.thumbLabel === 'always'
  306. }]
  307. }, [this.$createElement('div', this.setBackgroundColor(this.computedThumbColor, {
  308. staticClass: 'v-slider__thumb-label',
  309. style: {
  310. height: size,
  311. width: size
  312. }
  313. }), [content])])]);
  314. },
  315. genTrackContainer: function genTrackContainer() {
  316. var children = [this.$createElement('div', this.setBackgroundColor(this.computedTrackColor, {
  317. staticClass: 'v-slider__track',
  318. style: this.trackStyles
  319. })), this.$createElement('div', this.setBackgroundColor(this.computedColor, {
  320. staticClass: 'v-slider__track-fill',
  321. style: this.trackFillStyles
  322. }))];
  323. return this.$createElement('div', {
  324. staticClass: 'v-slider__track__container',
  325. ref: 'track'
  326. }, children);
  327. },
  328. getLabel: function getLabel(value) {
  329. return this.$scopedSlots['thumb-label'] ? this.$scopedSlots['thumb-label']({ value: value }) : this.$createElement('span', value);
  330. },
  331. onBlur: function onBlur(e) {
  332. if (this.keyPressed === 2) return;
  333. this.isActive = false;
  334. this.isFocused = false;
  335. this.$emit('blur', e);
  336. },
  337. onFocus: function onFocus(e) {
  338. this.isFocused = true;
  339. this.$emit('focus', e);
  340. },
  341. onThumbMouseDown: function onThumbMouseDown(e) {
  342. this.oldValue = this.internalValue;
  343. this.keyPressed = 2;
  344. var options = { passive: true };
  345. this.isActive = true;
  346. this.isFocused = false;
  347. if ('touches' in e) {
  348. this.app.addEventListener('touchmove', this.onMouseMove, options);
  349. (0, _helpers.addOnceEventListener)(this.app, 'touchend', this.onSliderMouseUp);
  350. } else {
  351. this.app.addEventListener('mousemove', this.onMouseMove, options);
  352. (0, _helpers.addOnceEventListener)(this.app, 'mouseup', this.onSliderMouseUp);
  353. }
  354. this.$emit('start', this.internalValue);
  355. },
  356. onSliderMouseUp: function onSliderMouseUp() {
  357. this.keyPressed = 0;
  358. var options = { passive: true };
  359. this.isActive = false;
  360. this.isFocused = false;
  361. this.app.removeEventListener('touchmove', this.onMouseMove, options);
  362. this.app.removeEventListener('mousemove', this.onMouseMove, options);
  363. this.$emit('end', this.internalValue);
  364. if (!(0, _helpers.deepEqual)(this.oldValue, this.internalValue)) {
  365. this.$emit('change', this.internalValue);
  366. }
  367. },
  368. onMouseMove: function onMouseMove(e) {
  369. var _parseMouseMove = this.parseMouseMove(e),
  370. value = _parseMouseMove.value,
  371. isInsideTrack = _parseMouseMove.isInsideTrack;
  372. if (isInsideTrack) {
  373. this.setInternalValue(value);
  374. }
  375. },
  376. onKeyDown: function onKeyDown(e) {
  377. if (this.disabled || this.readonly) return;
  378. var value = this.parseKeyDown(e);
  379. if (value == null) return;
  380. this.setInternalValue(value);
  381. this.$emit('change', value);
  382. },
  383. onKeyUp: function onKeyUp() {
  384. this.keyPressed = 0;
  385. },
  386. onSliderClick: function onSliderClick(e) {
  387. this.isFocused = true;
  388. this.onMouseMove(e);
  389. this.$emit('change', this.internalValue);
  390. },
  391. parseMouseMove: function parseMouseMove(e) {
  392. var _$refs$track$getBound = this.$refs.track.getBoundingClientRect(),
  393. offsetLeft = _$refs$track$getBound.left,
  394. trackWidth = _$refs$track$getBound.width;
  395. var clientX = 'touches' in e ? e.touches[0].clientX : e.clientX;
  396. // It is possible for left to be NaN, force to number
  397. var left = Math.min(Math.max((clientX - offsetLeft) / trackWidth, 0), 1) || 0;
  398. if (this.$vuetify.rtl) left = 1 - left;
  399. var isInsideTrack = clientX >= offsetLeft - 8 && clientX <= offsetLeft + trackWidth + 8;
  400. var value = parseFloat(this.min) + left * (this.max - this.min);
  401. return { value: value, isInsideTrack: isInsideTrack };
  402. },
  403. parseKeyDown: function parseKeyDown(e) {
  404. var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.internalValue;
  405. if (this.disabled) return;
  406. var pageup = _helpers.keyCodes.pageup,
  407. pagedown = _helpers.keyCodes.pagedown,
  408. end = _helpers.keyCodes.end,
  409. home = _helpers.keyCodes.home,
  410. left = _helpers.keyCodes.left,
  411. right = _helpers.keyCodes.right,
  412. down = _helpers.keyCodes.down,
  413. up = _helpers.keyCodes.up;
  414. if (![pageup, pagedown, end, home, left, right, down, up].includes(e.keyCode)) return;
  415. e.preventDefault();
  416. var step = this.stepNumeric || 1;
  417. var steps = (this.max - this.min) / step;
  418. if ([left, right, down, up].includes(e.keyCode)) {
  419. this.keyPressed += 1;
  420. var increase = this.$vuetify.rtl ? [left, up] : [right, up];
  421. var direction = increase.includes(e.keyCode) ? 1 : -1;
  422. var multiplier = e.shiftKey ? 3 : e.ctrlKey ? 2 : 1;
  423. value = value + direction * step * multiplier;
  424. } else if (e.keyCode === home) {
  425. value = parseFloat(this.min);
  426. } else if (e.keyCode === end) {
  427. value = parseFloat(this.max);
  428. } else /* if (e.keyCode === keyCodes.pageup || e.keyCode === pagedown) */{
  429. // Page up/down
  430. var _direction = e.keyCode === pagedown ? 1 : -1;
  431. value = value - _direction * step * (steps > 100 ? steps / 10 : 10);
  432. }
  433. return value;
  434. },
  435. roundValue: function roundValue(value) {
  436. if (!this.stepNumeric) return value;
  437. // Format input value using the same number
  438. // of decimals places as in the step prop
  439. var trimmedStep = this.step.toString().trim();
  440. var decimals = trimmedStep.indexOf('.') > -1 ? trimmedStep.length - trimmedStep.indexOf('.') - 1 : 0;
  441. var offset = this.min % this.stepNumeric;
  442. var newValue = Math.round((value - offset) / this.stepNumeric) * this.stepNumeric + offset;
  443. return parseFloat(Math.max(Math.min(newValue, this.max), this.min).toFixed(decimals));
  444. },
  445. setInternalValue: function setInternalValue(value) {
  446. this.internalValue = value;
  447. }
  448. }
  449. });
  450. //# sourceMappingURL=VSlider.js.map