slider.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  8. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  9. return new (P || (P = Promise))(function (resolve, reject) {
  10. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  11. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  12. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  13. step((generator = generator.apply(thisArg, _arguments || [])).next());
  14. });
  15. };
  16. import { SuperComponent, wxComponent } from '../common/src/index';
  17. import config from '../common/config';
  18. import { trimSingleValue, trimValue } from './tool';
  19. import props from './props';
  20. import { getRect } from '../common/utils';
  21. import Bus from '../common/bus';
  22. const { prefix } = config;
  23. const name = `${prefix}-slider`;
  24. let Slider = class Slider extends SuperComponent {
  25. constructor() {
  26. super(...arguments);
  27. this.externalClasses = [
  28. 'class',
  29. `${prefix}-class`,
  30. `${prefix}-class-bar`,
  31. `${prefix}-class-bar-active`,
  32. `${prefix}-class-bar-disabled`,
  33. `${prefix}-class-cursor`,
  34. ];
  35. this.properties = props;
  36. this.controlledProps = [
  37. {
  38. key: 'value',
  39. event: 'change',
  40. },
  41. ];
  42. this.data = {
  43. sliderStyles: '',
  44. classPrefix: name,
  45. initialLeft: null,
  46. initialRight: null,
  47. activeLeft: 0,
  48. activeRight: 0,
  49. maxRange: 0,
  50. lineLeft: 0,
  51. lineRight: 0,
  52. dotTopValue: [0, 0],
  53. _value: 0,
  54. blockSize: 20,
  55. isScale: false,
  56. scaleArray: [],
  57. scaleTextArray: [],
  58. prefix,
  59. };
  60. this.observers = {
  61. value(newValue) {
  62. this.handlePropsChange(newValue);
  63. },
  64. _value(newValue) {
  65. const { min, max, range } = this.properties;
  66. const { maxRange } = this.data;
  67. if (range) {
  68. const left = (maxRange * (newValue[0] - Number(min))) / (Number(max) - Number(min));
  69. const right = (maxRange * (Number(max) - newValue[1])) / (Number(max) - Number(min));
  70. this.setLineStyle(left, right);
  71. }
  72. else {
  73. this.setSingleBarWidth(newValue);
  74. }
  75. },
  76. marks(val) {
  77. if (this.data.initialLeft != null) {
  78. this.handleMask(val);
  79. }
  80. else {
  81. this.bus.on('initial', () => this.handleMask(val));
  82. }
  83. },
  84. };
  85. this.lifetimes = {
  86. created() {
  87. this.bus = new Bus();
  88. },
  89. attached() {
  90. const { value } = this.properties;
  91. if (!value)
  92. this.handlePropsChange(0);
  93. this.getInitialStyle();
  94. },
  95. };
  96. }
  97. triggerValue(value) {
  98. this._trigger('change', {
  99. value: trimValue(value, this.properties),
  100. });
  101. }
  102. handlePropsChange(newValue) {
  103. const value = trimValue(newValue, this.properties);
  104. const setValueAndTrigger = () => {
  105. this.setData({
  106. _value: value,
  107. });
  108. };
  109. if (this.data.maxRange === 0) {
  110. this.getInitialStyle().then(setValueAndTrigger);
  111. return;
  112. }
  113. setValueAndTrigger();
  114. }
  115. handleMask(marks) {
  116. const calcPos = (arr) => {
  117. const { theme } = this.properties;
  118. const { blockSize, maxRange } = this.data;
  119. const margin = theme === 'capsule' ? blockSize / 2 : 0;
  120. return arr.map((item) => ({
  121. val: item,
  122. left: Math.round((item / 100) * maxRange) + margin,
  123. }));
  124. };
  125. if ((marks === null || marks === void 0 ? void 0 : marks.length) && Array.isArray(marks)) {
  126. this.setData({
  127. isScale: true,
  128. scaleArray: calcPos(marks),
  129. scaleTextArray: [],
  130. });
  131. }
  132. if (Object.prototype.toString.call(marks) === '[object Object]') {
  133. const scaleArray = Object.keys(marks).map((item) => Number(item));
  134. const scaleTextArray = scaleArray.map((item) => marks[item]);
  135. this.setData({
  136. isScale: scaleArray.length > 0,
  137. scaleArray: calcPos(scaleArray),
  138. scaleTextArray,
  139. });
  140. }
  141. }
  142. setSingleBarWidth(value) {
  143. const { max, min, theme } = this.properties;
  144. const { maxRange, blockSize } = this.data;
  145. const halfBlock = theme === 'capsule' ? Number(blockSize) / 2 : 0;
  146. const percentage = (Number(value) - Number(min)) / (Number(max) - Number(min));
  147. const width = percentage * maxRange + halfBlock;
  148. this.setData({
  149. lineBarWidth: `${width}px`,
  150. });
  151. }
  152. getInitialStyle() {
  153. return __awaiter(this, void 0, void 0, function* () {
  154. const line = yield getRect(this, '#sliderLine');
  155. const { blockSize } = this.data;
  156. const { theme } = this.properties;
  157. const halfBlock = Number(blockSize) / 2;
  158. let maxRange = line.right - line.left;
  159. let initialLeft = line.left;
  160. let initialRight = line.right;
  161. if (theme === 'capsule') {
  162. maxRange = maxRange - Number(blockSize) - 6;
  163. initialLeft -= halfBlock;
  164. initialRight -= halfBlock;
  165. }
  166. this.setData({
  167. maxRange,
  168. initialLeft,
  169. initialRight,
  170. });
  171. this.bus.emit('initial');
  172. });
  173. }
  174. stepValue(value) {
  175. const { step, min, max } = this.properties;
  176. if (Number(step) < 1 || Number(step) > Number(max) - Number(min))
  177. return value;
  178. const closestStep = trimSingleValue(Math.round(value / Number(step)) * Number(step), Number(min), Number(max));
  179. return closestStep;
  180. }
  181. onSingleLineTap(e) {
  182. const { disabled } = this.properties;
  183. if (disabled)
  184. return;
  185. const value = this.getSingleChangeValue(e);
  186. this.triggerValue(value);
  187. }
  188. getSingleChangeValue(e) {
  189. const { min, max } = this.properties;
  190. const { initialLeft, maxRange } = this.data;
  191. const [touch] = e.changedTouches;
  192. const { pageX } = touch;
  193. const currentLeft = pageX - initialLeft;
  194. let value = 0;
  195. if (currentLeft <= 0) {
  196. value = Number(min);
  197. }
  198. else if (currentLeft >= maxRange) {
  199. value = Number(max);
  200. }
  201. else {
  202. value = Math.round((currentLeft / maxRange) * (Number(max) - Number(min)) + Number(min));
  203. }
  204. return this.stepValue(value);
  205. }
  206. convertPosToValue(posValue, dir) {
  207. const { maxRange } = this.data;
  208. const { max, min } = this.properties;
  209. return dir === 0
  210. ? (posValue / maxRange) * (Number(max) - Number(min)) + Number(min)
  211. : Number(max) - (posValue / maxRange) * (Number(max) - Number(min));
  212. }
  213. onLineTap(e) {
  214. const { disabled, theme } = this.properties;
  215. const { initialLeft, initialRight, maxRange, blockSize } = this.data;
  216. if (disabled)
  217. return;
  218. const [touch] = e.changedTouches;
  219. const { pageX } = touch;
  220. const halfBlock = theme === 'capsule' ? Number(blockSize) / 2 : 0;
  221. const currentLeft = pageX - initialLeft;
  222. if (currentLeft < 0 || currentLeft > maxRange + Number(blockSize))
  223. return;
  224. Promise.all([getRect(this, '#leftDot'), getRect(this, '#rightDot')]).then(([leftDot, rightDot]) => {
  225. const distanceLeft = Math.abs(pageX - leftDot.left - halfBlock);
  226. const distanceRight = Math.abs(rightDot.left - pageX + halfBlock);
  227. const isMoveLeft = distanceLeft < distanceRight;
  228. if (isMoveLeft) {
  229. const left = pageX - initialLeft;
  230. const leftValue = this.convertPosToValue(left, 0);
  231. this.triggerValue([this.stepValue(leftValue), this.data._value[1]]);
  232. }
  233. else {
  234. const right = -(pageX - initialRight);
  235. const rightValue = this.convertPosToValue(right, 1);
  236. this.triggerValue([this.data._value[0], this.stepValue(rightValue)]);
  237. }
  238. });
  239. }
  240. onTouchMoveLeft(e) {
  241. const { disabled } = this.properties;
  242. const { initialLeft, _value } = this.data;
  243. if (disabled)
  244. return;
  245. const [touch] = e.changedTouches;
  246. const { pageX } = touch;
  247. const currentLeft = pageX - initialLeft;
  248. const newData = [..._value];
  249. const leftValue = this.convertPosToValue(currentLeft, 0);
  250. newData[0] = this.stepValue(leftValue);
  251. this.triggerValue(newData);
  252. }
  253. onTouchMoveRight(e) {
  254. const { disabled } = this.properties;
  255. const { initialRight, _value } = this.data;
  256. if (disabled)
  257. return;
  258. const [touch] = e.changedTouches;
  259. const { pageX } = touch;
  260. const currentRight = -(pageX - initialRight);
  261. const newData = [..._value];
  262. const rightValue = this.convertPosToValue(currentRight, 1);
  263. newData[1] = this.stepValue(rightValue);
  264. this.triggerValue(newData);
  265. }
  266. setLineStyle(left, right) {
  267. const { theme } = this.properties;
  268. const { blockSize, maxRange } = this.data;
  269. const halfBlock = theme === 'capsule' ? Number(blockSize) / 2 : 0;
  270. const [a, b] = this.data._value;
  271. const cut = (v) => parseInt(v, 10);
  272. this.setData({
  273. dotTopValue: [a, b],
  274. });
  275. if (left + right <= maxRange) {
  276. this.setData({
  277. lineLeft: cut(left + halfBlock),
  278. lineRight: cut(right + halfBlock),
  279. });
  280. }
  281. else {
  282. this.setData({
  283. lineLeft: cut(maxRange + halfBlock - right),
  284. lineRight: cut(maxRange - left + halfBlock * 1.5),
  285. });
  286. }
  287. }
  288. onTouchEnd() { }
  289. };
  290. Slider = __decorate([
  291. wxComponent()
  292. ], Slider);
  293. export default Slider;