main.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <template>
  2. <div
  3. class="el-slider"
  4. :class="{ 'is-vertical': vertical, 'el-slider--with-input': showInput }"
  5. role="slider"
  6. :aria-valuemin="min"
  7. :aria-valuemax="max"
  8. :aria-orientation="vertical ? 'vertical': 'horizontal'"
  9. :aria-disabled="sliderDisabled"
  10. >
  11. <el-input-number
  12. v-model="firstValue"
  13. v-if="showInput && !range"
  14. class="el-slider__input"
  15. ref="input"
  16. @change="emitChange"
  17. :step="step"
  18. :disabled="sliderDisabled"
  19. :controls="showInputControls"
  20. :min="min"
  21. :max="max"
  22. :debounce="debounce"
  23. :size="inputSize">
  24. </el-input-number>
  25. <div
  26. class="el-slider__runway"
  27. :class="{ 'show-input': showInput, 'disabled': sliderDisabled }"
  28. :style="runwayStyle"
  29. @click="onSliderClick"
  30. ref="slider">
  31. <div
  32. class="el-slider__bar"
  33. :style="barStyle">
  34. </div>
  35. <slider-button
  36. :vertical="vertical"
  37. v-model="firstValue"
  38. :tooltip-class="tooltipClass"
  39. ref="button1">
  40. </slider-button>
  41. <slider-button
  42. :vertical="vertical"
  43. v-model="secondValue"
  44. :tooltip-class="tooltipClass"
  45. ref="button2"
  46. v-if="range">
  47. </slider-button>
  48. <div
  49. class="el-slider__stop"
  50. v-for="(item, key) in stops"
  51. :key="key"
  52. :style="getStopStyle(item)"
  53. v-if="showStops">
  54. </div>
  55. <template v-if="markList.length > 0">
  56. <div>
  57. <div
  58. v-for="(item, key) in markList"
  59. :style="getStopStyle(item.position)"
  60. class="el-slider__stop el-slider__marks-stop"
  61. :key="key">
  62. </div>
  63. </div>
  64. <div class="el-slider__marks">
  65. <slider-marker
  66. :mark="item.mark" v-for="(item, key) in markList"
  67. :key="key"
  68. :style="getStopStyle(item.position)">
  69. </slider-marker>
  70. </div>
  71. </template>
  72. </div>
  73. </div>
  74. </template>
  75. <script type="text/babel">
  76. import ElInputNumber from 'element-ui/packages/input-number';
  77. import SliderButton from './button.vue';
  78. import SliderMarker from './marker';
  79. import Emitter from 'element-ui/src/mixins/emitter';
  80. export default {
  81. name: 'ElSlider',
  82. mixins: [Emitter],
  83. inject: {
  84. elForm: {
  85. default: ''
  86. }
  87. },
  88. props: {
  89. min: {
  90. type: Number,
  91. default: 0
  92. },
  93. max: {
  94. type: Number,
  95. default: 100
  96. },
  97. step: {
  98. type: Number,
  99. default: 1
  100. },
  101. value: {
  102. type: [Number, Array],
  103. default: 0
  104. },
  105. showInput: {
  106. type: Boolean,
  107. default: false
  108. },
  109. showInputControls: {
  110. type: Boolean,
  111. default: true
  112. },
  113. inputSize: {
  114. type: String,
  115. default: 'small'
  116. },
  117. showStops: {
  118. type: Boolean,
  119. default: false
  120. },
  121. showTooltip: {
  122. type: Boolean,
  123. default: true
  124. },
  125. formatTooltip: Function,
  126. disabled: {
  127. type: Boolean,
  128. default: false
  129. },
  130. range: {
  131. type: Boolean,
  132. default: false
  133. },
  134. vertical: {
  135. type: Boolean,
  136. default: false
  137. },
  138. height: {
  139. type: String
  140. },
  141. debounce: {
  142. type: Number,
  143. default: 300
  144. },
  145. label: {
  146. type: String
  147. },
  148. tooltipClass: String,
  149. marks: Object
  150. },
  151. components: {
  152. ElInputNumber,
  153. SliderButton,
  154. SliderMarker
  155. },
  156. data() {
  157. return {
  158. firstValue: null,
  159. secondValue: null,
  160. oldValue: null,
  161. dragging: false,
  162. sliderSize: 1
  163. };
  164. },
  165. watch: {
  166. value(val, oldVal) {
  167. if (this.dragging ||
  168. Array.isArray(val) &&
  169. Array.isArray(oldVal) &&
  170. val.every((item, index) => item === oldVal[index])) {
  171. return;
  172. }
  173. this.setValues();
  174. },
  175. dragging(val) {
  176. if (!val) {
  177. this.setValues();
  178. }
  179. },
  180. firstValue(val) {
  181. if (this.range) {
  182. this.$emit('input', [this.minValue, this.maxValue]);
  183. } else {
  184. this.$emit('input', val);
  185. }
  186. },
  187. secondValue() {
  188. if (this.range) {
  189. this.$emit('input', [this.minValue, this.maxValue]);
  190. }
  191. },
  192. min() {
  193. this.setValues();
  194. },
  195. max() {
  196. this.setValues();
  197. }
  198. },
  199. methods: {
  200. valueChanged() {
  201. if (this.range) {
  202. return ![this.minValue, this.maxValue]
  203. .every((item, index) => item === this.oldValue[index]);
  204. } else {
  205. return this.value !== this.oldValue;
  206. }
  207. },
  208. setValues() {
  209. if (this.min > this.max) {
  210. console.error('[Element Error][Slider]min should not be greater than max.');
  211. return;
  212. }
  213. const val = this.value;
  214. if (this.range && Array.isArray(val)) {
  215. if (val[1] < this.min) {
  216. this.$emit('input', [this.min, this.min]);
  217. } else if (val[0] > this.max) {
  218. this.$emit('input', [this.max, this.max]);
  219. } else if (val[0] < this.min) {
  220. this.$emit('input', [this.min, val[1]]);
  221. } else if (val[1] > this.max) {
  222. this.$emit('input', [val[0], this.max]);
  223. } else {
  224. this.firstValue = val[0];
  225. this.secondValue = val[1];
  226. if (this.valueChanged()) {
  227. this.dispatch('ElFormItem', 'el.form.change', [this.minValue, this.maxValue]);
  228. this.oldValue = val.slice();
  229. }
  230. }
  231. } else if (!this.range && typeof val === 'number' && !isNaN(val)) {
  232. if (val < this.min) {
  233. this.$emit('input', this.min);
  234. } else if (val > this.max) {
  235. this.$emit('input', this.max);
  236. } else {
  237. this.firstValue = val;
  238. if (this.valueChanged()) {
  239. this.dispatch('ElFormItem', 'el.form.change', val);
  240. this.oldValue = val;
  241. }
  242. }
  243. }
  244. },
  245. setPosition(percent) {
  246. const targetValue = this.min + percent * (this.max - this.min) / 100;
  247. if (!this.range) {
  248. this.$refs.button1.setPosition(percent);
  249. return;
  250. }
  251. let button;
  252. if (Math.abs(this.minValue - targetValue) < Math.abs(this.maxValue - targetValue)) {
  253. button = this.firstValue < this.secondValue ? 'button1' : 'button2';
  254. } else {
  255. button = this.firstValue > this.secondValue ? 'button1' : 'button2';
  256. }
  257. this.$refs[button].setPosition(percent);
  258. },
  259. onSliderClick(event) {
  260. if (this.sliderDisabled || this.dragging) return;
  261. this.resetSize();
  262. if (this.vertical) {
  263. const sliderOffsetBottom = this.$refs.slider.getBoundingClientRect().bottom;
  264. this.setPosition((sliderOffsetBottom - event.clientY) / this.sliderSize * 100);
  265. } else {
  266. const sliderOffsetLeft = this.$refs.slider.getBoundingClientRect().left;
  267. this.setPosition((event.clientX - sliderOffsetLeft) / this.sliderSize * 100);
  268. }
  269. this.emitChange();
  270. },
  271. resetSize() {
  272. if (this.$refs.slider) {
  273. this.sliderSize = this.$refs.slider[`client${ this.vertical ? 'Height' : 'Width' }`];
  274. }
  275. },
  276. emitChange() {
  277. this.$nextTick(() => {
  278. this.$emit('change', this.range ? [this.minValue, this.maxValue] : this.value);
  279. });
  280. },
  281. getStopStyle(position) {
  282. return this.vertical ? { 'bottom': position + '%' } : { 'left': position + '%' };
  283. }
  284. },
  285. computed: {
  286. stops() {
  287. if (!this.showStops || this.min > this.max) return [];
  288. if (this.step === 0) {
  289. process.env.NODE_ENV !== 'production' &&
  290. console.warn('[Element Warn][Slider]step should not be 0.');
  291. return [];
  292. }
  293. const stopCount = (this.max - this.min) / this.step;
  294. const stepWidth = 100 * this.step / (this.max - this.min);
  295. const result = [];
  296. for (let i = 1; i < stopCount; i++) {
  297. result.push(i * stepWidth);
  298. }
  299. if (this.range) {
  300. return result.filter(step => {
  301. return step < 100 * (this.minValue - this.min) / (this.max - this.min) ||
  302. step > 100 * (this.maxValue - this.min) / (this.max - this.min);
  303. });
  304. } else {
  305. return result.filter(step => step > 100 * (this.firstValue - this.min) / (this.max - this.min));
  306. }
  307. },
  308. markList() {
  309. if (!this.marks) {
  310. return [];
  311. }
  312. const marksKeys = Object.keys(this.marks);
  313. return marksKeys.map(parseFloat)
  314. .sort((a, b) => a - b)
  315. .filter(point => point <= this.max && point >= this.min)
  316. .map(point => ({
  317. point,
  318. position: (point - this.min) * 100 / (this.max - this.min),
  319. mark: this.marks[point]
  320. }));
  321. },
  322. minValue() {
  323. return Math.min(this.firstValue, this.secondValue);
  324. },
  325. maxValue() {
  326. return Math.max(this.firstValue, this.secondValue);
  327. },
  328. barSize() {
  329. return this.range
  330. ? `${ 100 * (this.maxValue - this.minValue) / (this.max - this.min) }%`
  331. : `${ 100 * (this.firstValue - this.min) / (this.max - this.min) }%`;
  332. },
  333. barStart() {
  334. return this.range
  335. ? `${ 100 * (this.minValue - this.min) / (this.max - this.min) }%`
  336. : '0%';
  337. },
  338. precision() {
  339. let precisions = [this.min, this.max, this.step].map(item => {
  340. let decimal = ('' + item).split('.')[1];
  341. return decimal ? decimal.length : 0;
  342. });
  343. return Math.max.apply(null, precisions);
  344. },
  345. runwayStyle() {
  346. return this.vertical ? { height: this.height } : {};
  347. },
  348. barStyle() {
  349. return this.vertical
  350. ? {
  351. height: this.barSize,
  352. bottom: this.barStart
  353. } : {
  354. width: this.barSize,
  355. left: this.barStart
  356. };
  357. },
  358. sliderDisabled() {
  359. return this.disabled || (this.elForm || {}).disabled;
  360. }
  361. },
  362. mounted() {
  363. let valuetext;
  364. if (this.range) {
  365. if (Array.isArray(this.value)) {
  366. this.firstValue = Math.max(this.min, this.value[0]);
  367. this.secondValue = Math.min(this.max, this.value[1]);
  368. } else {
  369. this.firstValue = this.min;
  370. this.secondValue = this.max;
  371. }
  372. this.oldValue = [this.firstValue, this.secondValue];
  373. valuetext = `${this.firstValue}-${this.secondValue}`;
  374. } else {
  375. if (typeof this.value !== 'number' || isNaN(this.value)) {
  376. this.firstValue = this.min;
  377. } else {
  378. this.firstValue = Math.min(this.max, Math.max(this.min, this.value));
  379. }
  380. this.oldValue = this.firstValue;
  381. valuetext = this.firstValue;
  382. }
  383. this.$el.setAttribute('aria-valuetext', valuetext);
  384. // label screen reader
  385. this.$el.setAttribute('aria-label', this.label ? this.label : `slider between ${this.min} and ${this.max}`);
  386. this.resetSize();
  387. window.addEventListener('resize', this.resetSize);
  388. },
  389. beforeDestroy() {
  390. window.removeEventListener('resize', this.resetSize);
  391. }
  392. };
  393. </script>