index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const completeUnit = (val = 0, unit = "rpx") => {
  4. if (isNaN(Number(val)))
  5. return val;
  6. return val + unit;
  7. };
  8. const multiplication = (val1, val2) => {
  9. let dotIndex = String(val2).indexOf(".");
  10. if (dotIndex != -1) {
  11. let floatLength = String(val2).length - (dotIndex + 1);
  12. return val1 * (val2 * Math.pow(10, floatLength)) / Math.pow(10, floatLength);
  13. } else {
  14. return val1 * val2;
  15. }
  16. };
  17. const _sfc_main = {
  18. name: "slider-range",
  19. props: {
  20. value: {
  21. type: [Number, Array],
  22. default: () => []
  23. },
  24. valueType: {
  25. type: String,
  26. default: "max"
  27. },
  28. min: {
  29. type: Number,
  30. default: 0
  31. },
  32. max: {
  33. type: Number,
  34. default: 100
  35. },
  36. step: {
  37. type: Number,
  38. default: 1
  39. },
  40. height: {
  41. type: [Number, String],
  42. default: 12
  43. },
  44. disabled: {
  45. type: Boolean,
  46. default: false
  47. },
  48. tips: {
  49. type: Boolean,
  50. default: true
  51. },
  52. scale: {
  53. type: [Boolean, Object],
  54. default: true
  55. },
  56. backgroundStyle: {
  57. type: Object,
  58. default: () => {
  59. return { width: "100%" };
  60. }
  61. },
  62. activeStyle: {
  63. type: Object,
  64. default: () => {
  65. return {};
  66. }
  67. }
  68. },
  69. data() {
  70. return {
  71. // 两个值
  72. firstValue: 0,
  73. secondValue: 0,
  74. // 两个滑点
  75. firstBlock: {
  76. show: true,
  77. zIndex: 1
  78. },
  79. secondBlock: {
  80. show: true,
  81. zIndex: 2
  82. },
  83. // 滑动中
  84. dragging: "",
  85. // 刻度线设置
  86. scaleInfo: {
  87. show: true,
  88. min: true,
  89. max: true,
  90. interval: "auto",
  91. color: "#333",
  92. tickColor: "#999",
  93. fontSize: 22,
  94. format: null
  95. },
  96. scaleLine: []
  97. };
  98. },
  99. computed: {
  100. getStep() {
  101. if (this.max - this.min < this.step)
  102. return this.max - this.min;
  103. return this.step;
  104. },
  105. // 滑点坐标
  106. getFirstBlockPosition() {
  107. return (this.firstValue - this.min) / (this.max - this.min) * 100 + "%";
  108. },
  109. getSecondBlockPosition() {
  110. return (this.secondValue - this.min) / (this.max - this.min) * 100 + "%";
  111. },
  112. // 背景条样式
  113. getBackgroundStyle() {
  114. return {
  115. ...this.backgroundStyle,
  116. height: completeUnit(this.height)
  117. };
  118. },
  119. // 选中条样式
  120. getActiveStyle() {
  121. let min = Math.min(this.firstValue, this.secondValue);
  122. let max = Math.max(this.firstValue, this.secondValue);
  123. return {
  124. ...this.activeStyle,
  125. left: (min - this.min) / (this.max - this.min) * 100 + "%",
  126. width: (max - min) / (this.max - this.min) * 100 + "%"
  127. };
  128. }
  129. },
  130. mounted() {
  131. this.create();
  132. },
  133. methods: {
  134. create() {
  135. if (this.max <= this.min) {
  136. throw "[slider-range] max 属性不应小于或等于 min 属性";
  137. }
  138. if (typeof this.value == "number") {
  139. let value = this.value;
  140. if (this.value > this.max)
  141. value = this.max;
  142. if (this.value < this.min)
  143. value = this.min;
  144. if (this.valueType == "max") {
  145. this.firstBlock.show = false;
  146. this.firstValue = this.min;
  147. this.secondValue = value;
  148. } else {
  149. this.secondBlock.show = false;
  150. this.secondValue = this.max;
  151. this.firstValue = value;
  152. }
  153. } else {
  154. let firstValue = this.value[0] || this.min;
  155. if (firstValue > this.max)
  156. firstValue = this.max;
  157. if (firstValue < this.min)
  158. firstValue = this.min;
  159. let secondValue = this.value[1] || this.max;
  160. if (secondValue > this.max)
  161. secondValue = this.max;
  162. if (secondValue < this.min)
  163. secondValue = this.min;
  164. this.firstValue = firstValue;
  165. this.secondValue = secondValue;
  166. this.firstBlock.show = true;
  167. this.secondBlock.show = true;
  168. }
  169. if (typeof this.scale != "object") {
  170. this.scaleInfo.show = !!this.scale;
  171. } else {
  172. this.scaleInfo = { ...this.scaleInfo, ...this.scale };
  173. }
  174. if (!this.scaleInfo.show)
  175. return;
  176. let interval = this.scaleInfo.interval;
  177. let step = this.getStep > 0 ? this.getStep : 1;
  178. if (typeof interval != "number") {
  179. interval = Math.ceil((this.max - this.min) / step / 10);
  180. } else {
  181. interval = interval + 1;
  182. }
  183. let cumsum = this.min;
  184. let arr = [];
  185. while (cumsum <= this.max) {
  186. arr.push({
  187. value: cumsum,
  188. label: cumsum,
  189. color: this.scaleInfo.color,
  190. fontSize: completeUnit(this.scaleInfo.fontSize),
  191. tickColor: this.scaleInfo.tickColor,
  192. left: (cumsum - this.min) / (this.max - this.min) * 100 + "%"
  193. });
  194. cumsum = cumsum + multiplication(interval, step);
  195. }
  196. if (arr[0].value == this.min && !this.scaleInfo.min)
  197. arr = arr.slice(1);
  198. if (arr[arr.length - 1].value == this.max && !this.scaleInfo.max)
  199. arr = arr.slice(0, arr.length - 1);
  200. if (arr[arr.length - 1].value != this.max && this.scaleInfo.max)
  201. arr.push({
  202. value: this.max,
  203. label: this.max,
  204. color: this.scaleInfo.color,
  205. fontSize: completeUnit(this.scaleInfo.fontSize),
  206. tickColor: this.scaleInfo.tickColor,
  207. left: "100%"
  208. });
  209. let format = this.scaleInfo.format;
  210. if (typeof format == "function") {
  211. arr = arr.map((item, index) => {
  212. return format(item, index);
  213. });
  214. }
  215. this.scaleLine = [...arr];
  216. this.$forceUpdate();
  217. },
  218. onTouchStart(e) {
  219. if (this.disabled)
  220. return;
  221. this.dragging = e.currentTarget.dataset.sort;
  222. e.currentTarget.dataset.sort == "firstBlock" ? this.firstBlock.zIndex = this.secondBlock.zIndex + 1 : this.secondBlock.zIndex = this.firstBlock.zIndex + 1;
  223. this.startDragPostion = e.changedTouches ? e.changedTouches[0].pageX : e.pageX;
  224. this.startValue = e.currentTarget.dataset.sort == "firstBlock" ? this.firstValue : this.secondValue;
  225. this.$emit("start", {
  226. block: e.currentTarget.dataset.sort,
  227. value: this.startValue,
  228. values: [Math.min(this.firstValue, this.secondValue), Math.max(this.firstValue, this.secondValue)]
  229. });
  230. },
  231. onTouchMove(e) {
  232. if (this.disabled)
  233. return;
  234. this.onDrag(e);
  235. },
  236. onTouchEnd(e) {
  237. this.dragging = "";
  238. if (this.disabled)
  239. return;
  240. this.onDrag(e, true);
  241. },
  242. onDrag(e, end = false) {
  243. let pageX = e.changedTouches ? e.changedTouches[0].pageX : e.pageX;
  244. let view = common_vendor.index.createSelectorQuery().in(this).select(".block-bar");
  245. view.boundingClientRect((data) => {
  246. let diff = (pageX - this.startDragPostion) / data.width * (this.max - this.min);
  247. this.onUpdateValue(this.startValue + diff, e.currentTarget.dataset.sort, end);
  248. }).exec();
  249. },
  250. onUpdateValue(value, sort, end = false) {
  251. if (value < this.min)
  252. value = this.min;
  253. if (value > this.max)
  254. value = this.max;
  255. if (this.getStep > 0 && value != this.min && value != this.max) {
  256. value = this.min + multiplication(Math.round((value - this.min) / this.getStep), this.getStep);
  257. } else {
  258. value = Number(value.toFixed(2));
  259. }
  260. if (sort == "firstBlock") {
  261. this.firstValue = value;
  262. } else {
  263. this.secondValue = value;
  264. }
  265. this.$emit("change", {
  266. firstValue: this.firstValue,
  267. secondValue: this.secondValue,
  268. values: [Math.min(this.firstValue, this.secondValue), Math.max(this.firstValue, this.secondValue)]
  269. });
  270. if (end) {
  271. this.$emit("end", {
  272. block: sort,
  273. value,
  274. values: [Math.min(this.firstValue, this.secondValue), Math.max(this.firstValue, this.secondValue)]
  275. });
  276. }
  277. }
  278. }
  279. };
  280. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  281. return common_vendor.e({
  282. a: $data.scaleInfo.show
  283. }, $data.scaleInfo.show ? {
  284. b: common_vendor.f($data.scaleLine, (scaleItem, scaleIndex, i0) => {
  285. return {
  286. a: common_vendor.t(scaleItem.label),
  287. b: scaleItem.tickColor,
  288. c: scaleIndex,
  289. d: scaleItem.left,
  290. e: scaleItem.color,
  291. f: scaleItem.fontSize
  292. };
  293. })
  294. } : {}, {
  295. c: $props.tips
  296. }, $props.tips ? common_vendor.e({
  297. d: $data.dragging == "firstBlock"
  298. }, $data.dragging == "firstBlock" ? {
  299. e: common_vendor.t($data.firstValue),
  300. f: $options.getFirstBlockPosition,
  301. g: $data.firstBlock.zIndex
  302. } : {}, {
  303. h: $data.dragging == "secondBlock"
  304. }, $data.dragging == "secondBlock" ? {
  305. i: common_vendor.t($data.secondValue),
  306. j: $options.getSecondBlockPosition,
  307. k: $data.secondBlock.zIndex
  308. } : {}) : {}, {
  309. l: common_vendor.s($options.getActiveStyle),
  310. m: common_vendor.s($options.getBackgroundStyle),
  311. n: $data.firstBlock.show
  312. }, $data.firstBlock.show ? {
  313. o: $options.getFirstBlockPosition,
  314. p: $data.firstBlock.zIndex,
  315. q: common_vendor.o((...args) => $options.onTouchStart && $options.onTouchStart(...args)),
  316. r: common_vendor.o((...args) => $options.onTouchMove && $options.onTouchMove(...args)),
  317. s: common_vendor.o((...args) => $options.onTouchEnd && $options.onTouchEnd(...args))
  318. } : {}, {
  319. t: $data.secondBlock.show
  320. }, $data.secondBlock.show ? {
  321. v: $options.getSecondBlockPosition,
  322. w: $data.secondBlock.zIndex,
  323. x: common_vendor.o((...args) => $options.onTouchStart && $options.onTouchStart(...args)),
  324. y: common_vendor.o((...args) => $options.onTouchMove && $options.onTouchMove(...args)),
  325. z: common_vendor.o((...args) => $options.onTouchEnd && $options.onTouchEnd(...args))
  326. } : {}, {
  327. A: $props.disabled ? 1 : "",
  328. B: $props.tips || $data.scaleInfo.show ? 1 : ""
  329. });
  330. }
  331. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-d6b6bfbf"], ["__file", "D:/project/赋强公证/notarization_applet/components/slider-range/index.vue"]]);
  332. wx.createComponent(Component);