储能智慧云小程序
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

slider.js 15KB

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