slider.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. module.exports =
  2. /******/ (function(modules) { // webpackBootstrap
  3. /******/ // The module cache
  4. /******/ var installedModules = {};
  5. /******/ // The require function
  6. /******/ function __webpack_require__(moduleId) {
  7. /******/ // Check if module is in cache
  8. /******/ if(installedModules[moduleId])
  9. /******/ return installedModules[moduleId].exports;
  10. /******/ // Create a new module (and put it into the cache)
  11. /******/ var module = installedModules[moduleId] = {
  12. /******/ exports: {},
  13. /******/ id: moduleId,
  14. /******/ loaded: false
  15. /******/ };
  16. /******/ // Execute the module function
  17. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  18. /******/ // Flag the module as loaded
  19. /******/ module.loaded = true;
  20. /******/ // Return the exports of the module
  21. /******/ return module.exports;
  22. /******/ }
  23. /******/ // expose the modules object (__webpack_modules__)
  24. /******/ __webpack_require__.m = modules;
  25. /******/ // expose the module cache
  26. /******/ __webpack_require__.c = installedModules;
  27. /******/ // __webpack_public_path__
  28. /******/ __webpack_require__.p = "/dist/";
  29. /******/ // Load entry module and return exports
  30. /******/ return __webpack_require__(0);
  31. /******/ })
  32. /************************************************************************/
  33. /******/ ({
  34. /***/ 0:
  35. /***/ function(module, exports, __webpack_require__) {
  36. module.exports = __webpack_require__(268);
  37. /***/ },
  38. /***/ 3:
  39. /***/ function(module, exports) {
  40. module.exports = function normalizeComponent (
  41. rawScriptExports,
  42. compiledTemplate,
  43. scopeId,
  44. cssModules
  45. ) {
  46. var esModule
  47. var scriptExports = rawScriptExports = rawScriptExports || {}
  48. // ES6 modules interop
  49. var type = typeof rawScriptExports.default
  50. if (type === 'object' || type === 'function') {
  51. esModule = rawScriptExports
  52. scriptExports = rawScriptExports.default
  53. }
  54. // Vue.extend constructor export interop
  55. var options = typeof scriptExports === 'function'
  56. ? scriptExports.options
  57. : scriptExports
  58. // render functions
  59. if (compiledTemplate) {
  60. options.render = compiledTemplate.render
  61. options.staticRenderFns = compiledTemplate.staticRenderFns
  62. }
  63. // scopedId
  64. if (scopeId) {
  65. options._scopeId = scopeId
  66. }
  67. // inject cssModules
  68. if (cssModules) {
  69. var computed = options.computed || (options.computed = {})
  70. Object.keys(cssModules).forEach(function (key) {
  71. var module = cssModules[key]
  72. computed[key] = function () { return module }
  73. })
  74. }
  75. return {
  76. esModule: esModule,
  77. exports: scriptExports,
  78. options: options
  79. }
  80. }
  81. /***/ },
  82. /***/ 14:
  83. /***/ function(module, exports) {
  84. module.exports = require("element-ui/lib/mixins/emitter");
  85. /***/ },
  86. /***/ 117:
  87. /***/ function(module, exports) {
  88. module.exports = require("element-ui/lib/utils/dom");
  89. /***/ },
  90. /***/ 268:
  91. /***/ function(module, exports, __webpack_require__) {
  92. 'use strict';
  93. exports.__esModule = true;
  94. var _main = __webpack_require__(269);
  95. var _main2 = _interopRequireDefault(_main);
  96. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  97. /* istanbul ignore next */
  98. _main2.default.install = function (Vue) {
  99. Vue.component(_main2.default.name, _main2.default);
  100. };
  101. exports.default = _main2.default;
  102. /***/ },
  103. /***/ 269:
  104. /***/ function(module, exports, __webpack_require__) {
  105. var Component = __webpack_require__(3)(
  106. /* script */
  107. __webpack_require__(270),
  108. /* template */
  109. __webpack_require__(276),
  110. /* scopeId */
  111. null,
  112. /* cssModules */
  113. null
  114. )
  115. module.exports = Component.exports
  116. /***/ },
  117. /***/ 270:
  118. /***/ function(module, exports, __webpack_require__) {
  119. 'use strict';
  120. exports.__esModule = true;
  121. var _inputNumber = __webpack_require__(271);
  122. var _inputNumber2 = _interopRequireDefault(_inputNumber);
  123. var _button = __webpack_require__(272);
  124. var _button2 = _interopRequireDefault(_button);
  125. var _dom = __webpack_require__(117);
  126. var _emitter = __webpack_require__(14);
  127. var _emitter2 = _interopRequireDefault(_emitter);
  128. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  129. //
  130. //
  131. //
  132. //
  133. //
  134. //
  135. //
  136. //
  137. //
  138. //
  139. //
  140. //
  141. //
  142. //
  143. //
  144. //
  145. //
  146. //
  147. //
  148. //
  149. //
  150. //
  151. //
  152. //
  153. //
  154. //
  155. //
  156. //
  157. //
  158. //
  159. //
  160. //
  161. //
  162. //
  163. //
  164. //
  165. //
  166. //
  167. //
  168. //
  169. //
  170. //
  171. //
  172. //
  173. exports.default = {
  174. name: 'ElSlider',
  175. mixins: [_emitter2.default],
  176. props: {
  177. min: {
  178. type: Number,
  179. default: 0
  180. },
  181. max: {
  182. type: Number,
  183. default: 100
  184. },
  185. step: {
  186. type: Number,
  187. default: 1
  188. },
  189. value: {
  190. type: [Number, Array],
  191. default: 0
  192. },
  193. showInput: {
  194. type: Boolean,
  195. default: false
  196. },
  197. showInputControls: {
  198. type: Boolean,
  199. default: true
  200. },
  201. showStops: {
  202. type: Boolean,
  203. default: false
  204. },
  205. showTooltip: {
  206. type: Boolean,
  207. default: true
  208. },
  209. formatTooltip: Function,
  210. disabled: {
  211. type: Boolean,
  212. default: false
  213. },
  214. range: {
  215. type: Boolean,
  216. default: false
  217. }
  218. },
  219. components: {
  220. ElInputNumber: _inputNumber2.default,
  221. SliderButton: _button2.default
  222. },
  223. data: function data() {
  224. return {
  225. firstValue: null,
  226. secondValue: null,
  227. oldValue: null,
  228. dragging: false
  229. };
  230. },
  231. watch: {
  232. value: function value(val, oldVal) {
  233. if (this.dragging || Array.isArray(val) && Array.isArray(oldVal) && val.every(function (item, index) {
  234. return item === oldVal[index];
  235. })) {
  236. return;
  237. }
  238. this.setValues();
  239. },
  240. dragging: function dragging(val) {
  241. if (!val) {
  242. this.setValues();
  243. }
  244. },
  245. firstValue: function firstValue(val) {
  246. if (this.range) {
  247. this.$emit('input', [this.minValue, this.maxValue]);
  248. } else {
  249. this.$emit('input', val);
  250. }
  251. },
  252. secondValue: function secondValue() {
  253. if (this.range) {
  254. this.$emit('input', [this.minValue, this.maxValue]);
  255. }
  256. },
  257. min: function min() {
  258. this.setValues();
  259. },
  260. max: function max() {
  261. this.setValues();
  262. }
  263. },
  264. methods: {
  265. valueChanged: function valueChanged() {
  266. var _this = this;
  267. if (this.range) {
  268. return ![this.minValue, this.maxValue].every(function (item, index) {
  269. return item === _this.oldValue[index];
  270. });
  271. } else {
  272. return this.value !== this.oldValue;
  273. }
  274. },
  275. setValues: function setValues() {
  276. var val = this.value;
  277. if (this.range && Array.isArray(val)) {
  278. if (val[1] < this.min) {
  279. this.$emit('input', [this.min, this.min]);
  280. } else if (val[0] > this.max) {
  281. this.$emit('input', [this.max, this.max]);
  282. } else if (val[0] < this.min) {
  283. this.$emit('input', [this.min, val[1]]);
  284. } else if (val[1] > this.max) {
  285. this.$emit('input', [val[0], this.max]);
  286. } else {
  287. this.firstValue = val[0];
  288. this.secondValue = val[1];
  289. if (this.valueChanged()) {
  290. this.$emit('change', [this.minValue, this.maxValue]);
  291. this.dispatch('ElFormItem', 'el.form.change', [this.minValue, this.maxValue]);
  292. this.oldValue = val.slice();
  293. }
  294. }
  295. } else if (!this.range && typeof val === 'number' && !isNaN(val)) {
  296. if (val < this.min) {
  297. this.$emit('input', this.min);
  298. } else if (val > this.max) {
  299. this.$emit('input', this.max);
  300. } else {
  301. this.firstValue = val;
  302. if (this.valueChanged()) {
  303. this.$emit('change', val);
  304. this.dispatch('ElFormItem', 'el.form.change', val);
  305. this.oldValue = val;
  306. }
  307. }
  308. }
  309. },
  310. setPosition: function setPosition(percent) {
  311. var targetValue = this.min + percent * (this.max - this.min) / 100;
  312. if (!this.range) {
  313. this.$refs.button1.setPosition(percent);
  314. return;
  315. }
  316. var button = void 0;
  317. if (Math.abs(this.minValue - targetValue) < Math.abs(this.maxValue - targetValue)) {
  318. button = this.firstValue < this.secondValue ? 'button1' : 'button2';
  319. } else {
  320. button = this.firstValue > this.secondValue ? 'button1' : 'button2';
  321. }
  322. this.$refs[button].setPosition(percent);
  323. },
  324. onSliderClick: function onSliderClick(event) {
  325. if (this.disabled || this.dragging) return;
  326. var sliderOffsetLeft = this.$refs.slider.getBoundingClientRect().left;
  327. this.setPosition((event.clientX - sliderOffsetLeft) / this.$sliderWidth * 100);
  328. }
  329. },
  330. computed: {
  331. $sliderWidth: function $sliderWidth() {
  332. return parseInt((0, _dom.getStyle)(this.$refs.slider, 'width'), 10);
  333. },
  334. stops: function stops() {
  335. var _this2 = this;
  336. var stopCount = (this.max - this.min) / this.step;
  337. var stepWidth = 100 * this.step / (this.max - this.min);
  338. var result = [];
  339. for (var i = 1; i < stopCount; i++) {
  340. result.push(i * stepWidth);
  341. }
  342. if (this.range) {
  343. return result.filter(function (step) {
  344. return step < 100 * (_this2.minValue - _this2.min) / (_this2.max - _this2.min) || step > 100 * (_this2.maxValue - _this2.min) / (_this2.max - _this2.min);
  345. });
  346. } else {
  347. return result.filter(function (step) {
  348. return step > 100 * (_this2.firstValue - _this2.min) / (_this2.max - _this2.min);
  349. });
  350. }
  351. },
  352. minValue: function minValue() {
  353. return Math.min(this.firstValue, this.secondValue);
  354. },
  355. maxValue: function maxValue() {
  356. return Math.max(this.firstValue, this.secondValue);
  357. },
  358. barWidth: function barWidth() {
  359. return this.range ? 100 * (this.maxValue - this.minValue) / (this.max - this.min) + '%' : 100 * (this.firstValue - this.min) / (this.max - this.min) + '%';
  360. },
  361. barLeft: function barLeft() {
  362. return this.range ? 100 * (this.minValue - this.min) / (this.max - this.min) + '%' : '0%';
  363. },
  364. precision: function precision() {
  365. var precisions = [this.min, this.max, this.step].map(function (item) {
  366. var decimal = ('' + item).split('.')[1];
  367. return decimal ? decimal.length : 0;
  368. });
  369. return Math.max.apply(null, precisions);
  370. }
  371. },
  372. mounted: function mounted() {
  373. if (this.range) {
  374. if (Array.isArray(this.value)) {
  375. this.firstValue = Math.max(this.min, this.value[0]);
  376. this.secondValue = Math.min(this.max, this.value[1]);
  377. } else {
  378. this.firstValue = this.min;
  379. this.secondValue = this.max;
  380. }
  381. this.oldValue = [this.firstValue, this.secondValue];
  382. } else {
  383. if (typeof this.value !== 'number' || isNaN(this.value)) {
  384. this.firstValue = this.min;
  385. } else {
  386. this.firstValue = Math.min(this.max, Math.max(this.min, this.value));
  387. }
  388. this.oldValue = this.firstValue;
  389. }
  390. }
  391. };
  392. /***/ },
  393. /***/ 271:
  394. /***/ function(module, exports) {
  395. module.exports = require("element-ui/lib/input-number");
  396. /***/ },
  397. /***/ 272:
  398. /***/ function(module, exports, __webpack_require__) {
  399. var Component = __webpack_require__(3)(
  400. /* script */
  401. __webpack_require__(273),
  402. /* template */
  403. __webpack_require__(275),
  404. /* scopeId */
  405. null,
  406. /* cssModules */
  407. null
  408. )
  409. module.exports = Component.exports
  410. /***/ },
  411. /***/ 273:
  412. /***/ function(module, exports, __webpack_require__) {
  413. 'use strict';
  414. exports.__esModule = true;
  415. var _tooltip = __webpack_require__(274);
  416. var _tooltip2 = _interopRequireDefault(_tooltip);
  417. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  418. exports.default = {
  419. name: 'ElSliderButton',
  420. components: {
  421. ElTooltip: _tooltip2.default
  422. },
  423. props: {
  424. value: {
  425. type: Number,
  426. default: 0
  427. }
  428. },
  429. data: function data() {
  430. return {
  431. hovering: false,
  432. dragging: false,
  433. startX: 0,
  434. currentX: 0,
  435. startPosition: 0,
  436. newPosition: null,
  437. oldValue: this.value
  438. };
  439. },
  440. computed: {
  441. disabled: function disabled() {
  442. return this.$parent.disabled;
  443. },
  444. max: function max() {
  445. return this.$parent.max;
  446. },
  447. min: function min() {
  448. return this.$parent.min;
  449. },
  450. step: function step() {
  451. return this.$parent.step;
  452. },
  453. showTooltip: function showTooltip() {
  454. return this.$parent.showTooltip;
  455. },
  456. precision: function precision() {
  457. return this.$parent.precision;
  458. },
  459. currentPosition: function currentPosition() {
  460. return (this.value - this.min) / (this.max - this.min) * 100 + '%';
  461. },
  462. enableFormat: function enableFormat() {
  463. return this.$parent.formatTooltip instanceof Function;
  464. },
  465. formatValue: function formatValue() {
  466. return this.enableFormat && this.$parent.formatTooltip(this.value) || this.value;
  467. }
  468. },
  469. watch: {
  470. dragging: function dragging(val) {
  471. this.$parent.dragging = val;
  472. }
  473. },
  474. methods: {
  475. displayTooltip: function displayTooltip() {
  476. this.$refs.tooltip && (this.$refs.tooltip.showPopper = true);
  477. },
  478. hideTooltip: function hideTooltip() {
  479. this.$refs.tooltip && (this.$refs.tooltip.showPopper = false);
  480. },
  481. handleMouseEnter: function handleMouseEnter() {
  482. this.hovering = true;
  483. this.displayTooltip();
  484. },
  485. handleMouseLeave: function handleMouseLeave() {
  486. this.hovering = false;
  487. this.hideTooltip();
  488. },
  489. onButtonDown: function onButtonDown(event) {
  490. if (this.disabled) return;
  491. event.preventDefault();
  492. this.onDragStart(event);
  493. window.addEventListener('mousemove', this.onDragging);
  494. window.addEventListener('mouseup', this.onDragEnd);
  495. window.addEventListener('contextmenu', this.onDragEnd);
  496. },
  497. onDragStart: function onDragStart(event) {
  498. this.dragging = true;
  499. this.startX = event.clientX;
  500. this.startPosition = parseFloat(this.currentPosition);
  501. },
  502. onDragging: function onDragging(event) {
  503. if (this.dragging) {
  504. this.displayTooltip();
  505. this.currentX = event.clientX;
  506. var diff = (this.currentX - this.startX) / this.$parent.$sliderWidth * 100;
  507. this.newPosition = this.startPosition + diff;
  508. this.setPosition(this.newPosition);
  509. }
  510. },
  511. onDragEnd: function onDragEnd() {
  512. var _this = this;
  513. if (this.dragging) {
  514. /*
  515. * 防止在 mouseup 后立即触发 click,导致滑块有几率产生一小段位移
  516. * 不使用 preventDefault 是因为 mouseup 和 click 没有注册在同一个 DOM 上
  517. */
  518. setTimeout(function () {
  519. _this.dragging = false;
  520. _this.hideTooltip();
  521. _this.setPosition(_this.newPosition);
  522. }, 0);
  523. window.removeEventListener('mousemove', this.onDragging);
  524. window.removeEventListener('mouseup', this.onDragEnd);
  525. window.removeEventListener('contextmenu', this.onDragEnd);
  526. }
  527. },
  528. setPosition: function setPosition(newPosition) {
  529. if (newPosition < 0) {
  530. newPosition = 0;
  531. } else if (newPosition > 100) {
  532. newPosition = 100;
  533. }
  534. var lengthPerStep = 100 / ((this.max - this.min) / this.step);
  535. var steps = Math.round(newPosition / lengthPerStep);
  536. var value = steps * lengthPerStep * (this.max - this.min) * 0.01 + this.min;
  537. value = parseFloat(value.toFixed(this.precision));
  538. this.$emit('input', value);
  539. this.$refs.tooltip && this.$refs.tooltip.updatePopper();
  540. if (!this.dragging && this.value !== this.oldValue) {
  541. this.oldValue = this.value;
  542. }
  543. }
  544. }
  545. }; //
  546. //
  547. //
  548. //
  549. //
  550. //
  551. //
  552. //
  553. //
  554. //
  555. //
  556. //
  557. //
  558. //
  559. //
  560. //
  561. /***/ },
  562. /***/ 274:
  563. /***/ function(module, exports) {
  564. module.exports = require("element-ui/lib/tooltip");
  565. /***/ },
  566. /***/ 275:
  567. /***/ function(module, exports) {
  568. module.exports={render:function (){var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h;
  569. return _c('div', {
  570. ref: "button",
  571. staticClass: "el-slider__button-wrapper",
  572. class: {
  573. 'hover': _vm.hovering, 'dragging': _vm.dragging
  574. },
  575. style: ({
  576. left: _vm.currentPosition
  577. }),
  578. on: {
  579. "mouseenter": _vm.handleMouseEnter,
  580. "mouseleave": _vm.handleMouseLeave,
  581. "mousedown": _vm.onButtonDown
  582. }
  583. }, [_c('el-tooltip', {
  584. ref: "tooltip",
  585. attrs: {
  586. "placement": "top",
  587. "disabled": !_vm.showTooltip
  588. }
  589. }, [_c('span', {
  590. slot: "content"
  591. }, [_vm._v(_vm._s(_vm.formatValue))]), _c('div', {
  592. staticClass: "el-slider__button",
  593. class: {
  594. 'hover': _vm.hovering, 'dragging': _vm.dragging
  595. }
  596. })])], 1)
  597. },staticRenderFns: []}
  598. /***/ },
  599. /***/ 276:
  600. /***/ function(module, exports) {
  601. module.exports={render:function (){var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h;
  602. return _c('div', {
  603. staticClass: "el-slider"
  604. }, [(_vm.showInput && !_vm.range) ? _c('el-input-number', {
  605. directives: [{
  606. name: "model",
  607. rawName: "v-model",
  608. value: (_vm.firstValue),
  609. expression: "firstValue"
  610. }],
  611. ref: "input",
  612. staticClass: "el-slider__input",
  613. attrs: {
  614. "step": _vm.step,
  615. "disabled": _vm.disabled,
  616. "controls": _vm.showInputControls,
  617. "min": _vm.min,
  618. "max": _vm.max,
  619. "size": "small"
  620. },
  621. domProps: {
  622. "value": (_vm.firstValue)
  623. },
  624. on: {
  625. "input": function($event) {
  626. _vm.firstValue = $event
  627. }
  628. }
  629. }) : _vm._e(), _c('div', {
  630. ref: "slider",
  631. staticClass: "el-slider__runway",
  632. class: {
  633. 'show-input': _vm.showInput, 'disabled': _vm.disabled
  634. },
  635. on: {
  636. "click": _vm.onSliderClick
  637. }
  638. }, [_c('div', {
  639. staticClass: "el-slider__bar",
  640. style: ({
  641. width: _vm.barWidth,
  642. left: _vm.barLeft
  643. })
  644. }), _c('slider-button', {
  645. directives: [{
  646. name: "model",
  647. rawName: "v-model",
  648. value: (_vm.firstValue),
  649. expression: "firstValue"
  650. }],
  651. ref: "button1",
  652. domProps: {
  653. "value": (_vm.firstValue)
  654. },
  655. on: {
  656. "input": function($event) {
  657. _vm.firstValue = $event
  658. }
  659. }
  660. }), (_vm.range) ? _c('slider-button', {
  661. directives: [{
  662. name: "model",
  663. rawName: "v-model",
  664. value: (_vm.secondValue),
  665. expression: "secondValue"
  666. }],
  667. ref: "button2",
  668. domProps: {
  669. "value": (_vm.secondValue)
  670. },
  671. on: {
  672. "input": function($event) {
  673. _vm.secondValue = $event
  674. }
  675. }
  676. }) : _vm._e(), _vm._l((_vm.stops), function(item) {
  677. return (_vm.showStops) ? _c('div', {
  678. staticClass: "el-slider__stop",
  679. style: ({
  680. 'left': item + '%'
  681. })
  682. }) : _vm._e()
  683. })], 2)], 1)
  684. },staticRenderFns: []}
  685. /***/ }
  686. /******/ });