slider.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. `${prefix}-class`,
  29. `${prefix}-class-bar`,
  30. `${prefix}-class-bar-active`,
  31. `${prefix}-class-bar-disabled`,
  32. `${prefix}-class-cursor`,
  33. ];
  34. this.options = {
  35. pureDataPattern: /^__/,
  36. };
  37. this.properties = props;
  38. this.controlledProps = [
  39. {
  40. key: 'value',
  41. event: 'change',
  42. },
  43. ];
  44. this.data = {
  45. sliderStyles: '',
  46. classPrefix: name,
  47. initialLeft: null,
  48. initialRight: null,
  49. activeLeft: 0,
  50. activeRight: 0,
  51. maxRange: 0,
  52. lineLeft: 0,
  53. lineRight: 0,
  54. dotTopValue: [0, 0],
  55. _value: 0,
  56. blockSize: 20,
  57. isScale: false,
  58. scaleArray: [],
  59. scaleTextArray: [],
  60. prefix,
  61. isVisibleToScreenReader: false,
  62. identifier: [-1, -1],
  63. __inited: false,
  64. };
  65. this.observers = {
  66. value(newValue) {
  67. this.handlePropsChange(newValue);
  68. },
  69. _value(newValue) {
  70. this.bus.on('initial', () => this.renderLine(newValue));
  71. this.toggleA11yTips();
  72. },
  73. marks(val) {
  74. this.bus.on('initial', () => this.handleMark(val));
  75. },
  76. };
  77. this.lifetimes = {
  78. created() {
  79. this.bus = new Bus();
  80. },
  81. attached() {
  82. const { value } = this.properties;
  83. if (!value)
  84. this.handlePropsChange(0);
  85. this.init();
  86. this.injectPageScroll();
  87. },
  88. };
  89. }
  90. injectPageScroll() {
  91. const { range, vertical } = this.properties;
  92. if (!range || !vertical)
  93. return;
  94. const pages = getCurrentPages() || [];
  95. let curPage = null;
  96. if (pages && pages.length - 1 >= 0) {
  97. curPage = pages[pages.length - 1];
  98. }
  99. if (!curPage)
  100. return;
  101. const originPageScroll = curPage === null || curPage === void 0 ? void 0 : curPage.onPageScroll;
  102. curPage.onPageScroll = (rest) => {
  103. originPageScroll === null || originPageScroll === void 0 ? void 0 : originPageScroll.call(this, rest);
  104. this.observerScrollTop(rest);
  105. };
  106. }
  107. observerScrollTop(rest) {
  108. const { scrollTop } = rest || {};
  109. this.pageScrollTop = scrollTop;
  110. }
  111. toggleA11yTips() {
  112. this.setData({
  113. isVisibleToScreenReader: true,
  114. });
  115. setTimeout(() => {
  116. this.setData({
  117. isVisibleToScreenReader: false,
  118. });
  119. }, 2000);
  120. }
  121. renderLine(val) {
  122. const { min, max, range } = this.properties;
  123. const { maxRange } = this.data;
  124. if (range) {
  125. const left = (maxRange * (val[0] - Number(min))) / (Number(max) - Number(min));
  126. const right = (maxRange * (Number(max) - val[1])) / (Number(max) - Number(min));
  127. this.setLineStyle(left, right);
  128. }
  129. else {
  130. this.setSingleBarWidth(val);
  131. }
  132. }
  133. triggerValue(value) {
  134. if (this.preval === value)
  135. return;
  136. this.preval = value;
  137. this._trigger('change', {
  138. value: trimValue(value, this.properties),
  139. });
  140. }
  141. handlePropsChange(newValue) {
  142. const value = trimValue(newValue, this.properties);
  143. const setValueAndTrigger = () => {
  144. this.setData({
  145. _value: value,
  146. });
  147. };
  148. if (this.data.maxRange === 0) {
  149. this.init().then(setValueAndTrigger);
  150. return;
  151. }
  152. setValueAndTrigger();
  153. }
  154. handleMark(marks) {
  155. const calcPos = (arr) => {
  156. const { max, theme } = this.properties;
  157. const { blockSize, maxRange } = this.data;
  158. const margin = theme === 'capsule' ? blockSize / 2 : 0;
  159. return arr.map((item) => ({
  160. val: item,
  161. left: Math.round((item / Number(max)) * maxRange) + margin,
  162. }));
  163. };
  164. if ((marks === null || marks === void 0 ? void 0 : marks.length) && Array.isArray(marks)) {
  165. this.setData({
  166. isScale: true,
  167. scaleArray: calcPos(marks),
  168. scaleTextArray: [],
  169. });
  170. }
  171. if (Object.prototype.toString.call(marks) === '[object Object]') {
  172. const scaleArray = Object.keys(marks).map((item) => Number(item));
  173. const scaleTextArray = scaleArray.map((item) => marks[item]);
  174. this.setData({
  175. isScale: scaleArray.length > 0,
  176. scaleArray: calcPos(scaleArray),
  177. scaleTextArray,
  178. });
  179. }
  180. }
  181. setSingleBarWidth(value) {
  182. const { max, min, theme } = this.properties;
  183. const { maxRange, blockSize } = this.data;
  184. const halfBlock = theme === 'capsule' ? Number(blockSize) / 2 : 0;
  185. const percentage = (Number(value) - Number(min)) / (Number(max) - Number(min));
  186. const width = percentage * maxRange + halfBlock;
  187. this.setData({
  188. lineBarWidth: `${width}px`,
  189. });
  190. }
  191. init() {
  192. return __awaiter(this, void 0, void 0, function* () {
  193. if (this.data.__inited)
  194. return;
  195. const line = yield getRect(this, '#sliderLine');
  196. const { blockSize } = this.data;
  197. const { theme, vertical } = this.properties;
  198. const halfBlock = Number(blockSize) / 2;
  199. const { top, bottom, right, left } = line;
  200. let maxRange = vertical ? bottom - top : right - left;
  201. let initialLeft = vertical ? top : left;
  202. let initialRight = vertical ? bottom : right;
  203. if (initialLeft === 0 && initialRight === 0)
  204. return;
  205. if (theme === 'capsule') {
  206. maxRange = maxRange - Number(blockSize) - 6;
  207. initialLeft -= halfBlock;
  208. initialRight -= halfBlock;
  209. }
  210. this.setData({
  211. maxRange,
  212. initialLeft,
  213. initialRight,
  214. __inited: true,
  215. });
  216. this.bus.emit('initial');
  217. });
  218. }
  219. stepValue(value) {
  220. const { step, min, max } = this.properties;
  221. const decimal = String(step).indexOf('.') > -1 ? String(step).length - String(step).indexOf('.') - 1 : 0;
  222. const closestStep = trimSingleValue(Number((Math.round(value / Number(step)) * Number(step)).toFixed(decimal)), Number(min), Number(max));
  223. return closestStep;
  224. }
  225. onSingleLineTap(e) {
  226. const { disabled } = this.properties;
  227. if (disabled)
  228. return;
  229. const isSingleLineTap = this.data.identifier[0] === -1;
  230. if (isSingleLineTap) {
  231. const [touch] = e.changedTouches;
  232. this.data.identifier[0] = touch.identifier;
  233. }
  234. const value = this.getSingleChangeValue(e);
  235. if (isSingleLineTap) {
  236. this.data.identifier[0] = -1;
  237. }
  238. this.triggerValue(value);
  239. }
  240. getSingleChangeValue(e) {
  241. const { min, max, theme, vertical } = this.properties;
  242. const { initialLeft, maxRange, blockSize } = this.data;
  243. const touch = e.changedTouches.find((item) => item.identifier === this.data.identifier[0]);
  244. const pagePosition = this.getPagePosition(touch);
  245. let offset = 0;
  246. if (theme === 'capsule') {
  247. offset = Number(blockSize);
  248. if (vertical) {
  249. offset *= 2;
  250. }
  251. offset += 6;
  252. }
  253. else if (vertical) {
  254. offset = Number(blockSize);
  255. }
  256. const currentLeft = pagePosition - initialLeft - offset;
  257. let value = 0;
  258. if (currentLeft <= 0) {
  259. value = Number(min);
  260. }
  261. else if (currentLeft >= maxRange) {
  262. value = Number(max);
  263. }
  264. else {
  265. value = (currentLeft / maxRange) * (Number(max) - Number(min)) + Number(min);
  266. }
  267. return this.stepValue(value);
  268. }
  269. convertPosToValue(posValue, dir) {
  270. const { maxRange } = this.data;
  271. const { max, min } = this.properties;
  272. return dir === 0
  273. ? (posValue / maxRange) * (Number(max) - Number(min)) + Number(min)
  274. : Number(max) - (posValue / maxRange) * (Number(max) - Number(min));
  275. }
  276. onLineTap(e) {
  277. const { disabled, theme, vertical } = this.properties;
  278. const { initialLeft, initialRight, maxRange, blockSize } = this.data;
  279. if (disabled)
  280. return;
  281. const [touch] = e.changedTouches;
  282. const pagePosition = this.getPagePosition(touch);
  283. const halfBlock = theme === 'capsule' ? Number(blockSize) / 2 : 0;
  284. const currentLeft = pagePosition - initialLeft;
  285. const currentRight = -(pagePosition - initialRight);
  286. if (currentLeft < 0 || currentRight > maxRange + Number(blockSize))
  287. return;
  288. Promise.all([getRect(this, '#leftDot'), getRect(this, '#rightDot')]).then(([leftDot, rightDot]) => {
  289. const pageScrollTop = this.pageScrollTop || 0;
  290. const leftDotPosition = vertical ? leftDot.top + pageScrollTop : leftDot.left;
  291. const distanceLeft = Math.abs(pagePosition - leftDotPosition - halfBlock);
  292. const rightDotPosition = vertical ? rightDot.top + pageScrollTop : rightDot.left;
  293. const distanceRight = Math.abs(rightDotPosition - pagePosition + halfBlock);
  294. const isMoveLeft = distanceLeft < distanceRight;
  295. let offset = 0;
  296. if (theme === 'capsule') {
  297. offset = Number(blockSize);
  298. if (vertical) {
  299. offset *= 2;
  300. }
  301. offset += 6;
  302. }
  303. else if (vertical) {
  304. offset = Number(blockSize);
  305. }
  306. if (isMoveLeft) {
  307. const left = pagePosition - initialLeft - offset;
  308. const leftValue = this.convertPosToValue(left, 0);
  309. this.triggerValue([this.stepValue(leftValue), this.data._value[1]]);
  310. }
  311. else {
  312. let right = -(pagePosition - initialRight);
  313. if (vertical) {
  314. right += offset / 2;
  315. }
  316. const rightValue = this.convertPosToValue(right, 1);
  317. this.triggerValue([this.data._value[0], this.stepValue(rightValue)]);
  318. }
  319. });
  320. }
  321. onTouchStart(e) {
  322. this.triggerEvent('dragstart', { e });
  323. const [touch] = e.changedTouches;
  324. if (e.currentTarget.id === 'rightDot') {
  325. this.data.identifier[1] = touch.identifier;
  326. }
  327. else {
  328. this.data.identifier[0] = touch.identifier;
  329. }
  330. }
  331. onTouchMoveLeft(e) {
  332. const { disabled, theme, vertical } = this.properties;
  333. const { initialLeft, _value, blockSize } = this.data;
  334. if (disabled)
  335. return;
  336. const touch = e.changedTouches.find((item) => item.identifier === this.data.identifier[0]);
  337. const pagePosition = this.getPagePosition(touch);
  338. let offset = 0;
  339. if (theme === 'capsule') {
  340. offset += Number(blockSize);
  341. }
  342. if (vertical) {
  343. offset += Number(blockSize) + 6;
  344. }
  345. const currentLeft = pagePosition - initialLeft - offset;
  346. const newData = [..._value];
  347. const leftValue = this.convertPosToValue(currentLeft, 0);
  348. newData[0] = this.stepValue(leftValue);
  349. this.triggerValue(newData);
  350. }
  351. onTouchMoveRight(e) {
  352. const { disabled, vertical } = this.properties;
  353. const { initialRight, _value, blockSize } = this.data;
  354. if (disabled)
  355. return;
  356. const touch = e.changedTouches.find((item) => item.identifier === this.data.identifier[1]);
  357. const pagePosition = this.getPagePosition(touch);
  358. let offset = 0;
  359. if (vertical) {
  360. offset += Number(blockSize) / 2 + 6;
  361. }
  362. const currentRight = -(pagePosition - initialRight - offset);
  363. const newData = [..._value];
  364. const rightValue = this.convertPosToValue(currentRight, 1);
  365. newData[1] = this.stepValue(rightValue);
  366. this.triggerValue(newData);
  367. }
  368. setLineStyle(left, right) {
  369. const { theme } = this.properties;
  370. const { blockSize, maxRange } = this.data;
  371. const halfBlock = theme === 'capsule' ? Number(blockSize) / 2 : 0;
  372. const [a, b] = this.data._value;
  373. const cut = (v) => parseInt(v, 10);
  374. this.setData({
  375. dotTopValue: [a, b],
  376. });
  377. if (left + right <= maxRange) {
  378. this.setData({
  379. lineLeft: cut(left + halfBlock),
  380. lineRight: cut(right + halfBlock),
  381. });
  382. }
  383. else {
  384. this.setData({
  385. lineLeft: cut(maxRange + halfBlock - right),
  386. lineRight: cut(maxRange - left + halfBlock * 1.5),
  387. });
  388. }
  389. }
  390. onTouchEnd(e) {
  391. this.triggerEvent('dragend', { e, value: this.data._value });
  392. if (e.currentTarget.id === 'rightDot') {
  393. this.data.identifier[1] = -1;
  394. }
  395. else {
  396. this.data.identifier[0] = -1;
  397. }
  398. }
  399. getPagePosition(touch) {
  400. const { pageX, pageY } = touch;
  401. const { vertical } = this.properties;
  402. return vertical ? pageY : pageX;
  403. }
  404. };
  405. Slider = __decorate([
  406. wxComponent()
  407. ], Slider);
  408. export default Slider;