HM-dragSorts.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. <template>
  2. <view class="HM-drag-sort" :style="{'height': ListHeight+'px'}">
  3. <view class="rowBox-shadow" :style="{'height': rowHeight+'px'}" id="shadowRowBox">
  4. <view class="row" id="shadowRow">
  5. <view class="modules">
  6. <!-- 内容 -->
  7. <!-- <slot name="rowContent" :row="shadowRow"></slot> -->
  8. <view :class="['chart-classify-item',selectId==shadowRow.myChartClassifyId?'chart-classify-item-active':'']">
  9. {{shadowRow.myChartClassifyName}}
  10. </view>
  11. <!-- 拖拽图标 -->
  12. <view class="drag" :style="{'height': rowHeight+'px'}"><text class="iconfont icon-drag"></text></view>
  13. </view>
  14. </view>
  15. </view>
  16. <scroll-view id="scrollView" :scroll-y="true" :style="{'height': ListHeight+'px'}" :scroll-top="scrollViewTop"
  17. @scroll="drag.scroll" :scroll-with-animation="false">
  18. <!-- 两个list列表 拖拽完直接切换list 避免闪烁 -->
  19. <block v-for="(tmplist,listType) in dragList" :key="listType">
  20. <view class="list color" :class="[listType=='A'?(listSwitch?'show':'hide'):(listSwitch?'hide':'show')]">
  21. <block v-for="(row,index) in tmplist" :key="index">
  22. <view class="rowBox" :style="{'height': rowHeight+'px'}" :id="'rowBox'+listType+index">
  23. <view :class="'row row'+listType" :style="{'height': rowHeight+'px'}" :id="'row'+listType+index">
  24. <view class="modules" @tap="triggerClick(index,row)">
  25. <!-- 内容 -->
  26. <!-- <slot name="rowContent" :row="row"></slot> -->
  27. <view :class="['chart-classify-item',selectId==row.myChartClassifyId?'chart-classify-item-active':'']">
  28. {{row.myChartClassifyName}}
  29. </view>
  30. <!-- 拖拽图标 -->
  31. <view class="drag" :style="{'height': rowHeight+'px'}"
  32. :data-index="index" :data-type="listType"
  33. @touchstart="drag.touchstart" @touchmove="drag.touchmove" @touchend="drag.touchend"
  34. <!-- #ifdef MP-WEIXIN -->
  35. @longpress="drag.longpress"
  36. <!-- #endif -->
  37. >
  38. <text class="iconfont icon-drag"></text>
  39. </view>
  40. </view>
  41. </view>
  42. </view>
  43. </block>
  44. </view>
  45. </block>
  46. </scroll-view>
  47. <view :data-isapph5="isAppH5" :data-islongtouch="isLongTouch" :data-longtouchtime="longTouchTime" :data-listheight="ListHeight"
  48. :data-rownum="list.length" id="dataView" style="display: none !important;">存放数据给wxs读取</view>
  49. <view style="display: none !important;" :prop="scrollCommand" :change:prop="renderjs.runCommand">触发renderjs跳板,请勿删除</view>
  50. </view>
  51. </template>
  52. <script src="./drag.wxs" module="drag" lang="wxs"></script>
  53. <script module="renderjs" lang="renderjs">
  54. // APP or H5端renderjs
  55. export default {
  56. data() {
  57. return {
  58. e: null,
  59. ScrollView: null,
  60. scrollTimer: null
  61. }
  62. },
  63. methods: {
  64. runCommand(e, oldValue, ownerInstance, instance) {
  65. this.e = e;
  66. this.getScrollView(document.getElementById('scrollView'))
  67. window.cancelAnimationFrame(this.AnimationFrameID);
  68. this.AnimationFrameID = window.requestAnimationFrame(this.Animation);
  69. if (e.command == "stop") {
  70. window.cancelAnimationFrame(this.AnimationFrameID);
  71. return;
  72. }
  73. },
  74. Animation() {
  75. if (this.e.command == "stop") {
  76. window.cancelAnimationFrame(this.AnimationFrameID);
  77. return;
  78. }
  79. let maxScrollTop = this.e.rowLength * this.e.rowHeight - this.e.ListHeight;
  80. if (this.e.command == "up") {
  81. this.ScrollView.scrollTop -= 3
  82. } else if (this.e.command == "down") {
  83. this.ScrollView.scrollTop += 3;
  84. }
  85. if (this.ScrollView.scrollTop < 0) {
  86. this.ScrollView.scrollTop = 0;
  87. window.cancelAnimationFrame(this.AnimationFrameID);
  88. }
  89. if (this.ScrollView.scrollTop > maxScrollTop) {
  90. this.ScrollView.scrollTop = maxScrollTop;
  91. window.cancelAnimationFrame(this.AnimationFrameID);
  92. }
  93. this.AnimationFrameID = window.requestAnimationFrame(this.Animation);
  94. },
  95. getScrollView(DOM) {
  96. if (this.ScrollView != null) {
  97. return this.ScrollView;
  98. }
  99. var styleStr = DOM.getAttribute('style');
  100. if (DOM.className == 'uni-scroll-view' && styleStr != null && styleStr.indexOf('overflow') > -1 && styleStr.indexOf(
  101. 'auto') > -1) {
  102. this.ScrollView = DOM;
  103. return DOM;
  104. } else {
  105. this.getScrollView(DOM.firstChild);
  106. }
  107. }
  108. }
  109. }
  110. </script>
  111. <script>
  112. /**
  113. * 拖拽排序组件 HM-dragSort
  114. * @description 拖拽排序组件 HM-dragSort
  115. * @property {ObjectArray} list = [] 列表数据,自定义数据,会传递到name="rowContent"插槽
  116. * @property {Boolean} isLongTouch = [true|false] 是否开启长按拖动
  117. * @property {Number} longTouchTime = [] 选填,触发长按时长,单位:ms,默认350ms,不支持微信小程序
  118. * @property {Number} listHeight = [] 选填,可拖动列表整体的高度,单位:px,默认等于窗口高度
  119. * @property {Number} rowHeight = [] 必填,行高,单位:px,默认44px
  120. * @event {Function} change 行位置发生改变时触发事件 返回值:{index:'原始下标',moveTo:'被拖动到的下标',moveRow:'拖动行数据'}
  121. * @event {Function} confirm 拖拽结束且行位置发生了改变触发事件 返回值:{index:'原始下标',moveTo:'被拖动到的下标',moveRow:'拖动行数据',list:'整个列表拖动后的数据'}
  122. * @event {Function} onclick 点击行触发事件 返回值:{index:'被点击行下标',value:'被点击行数据'}
  123. */
  124. export default {
  125. name: 'HM-dragSort',
  126. data() {
  127. return {
  128. // #ifdef APP-PLUS || H5
  129. isAppH5: true,
  130. // #endif
  131. // #ifdef MP-WEIXIN
  132. isAppH5: false,
  133. // #endif
  134. shadowRow: {}, // 存放被拖拽行数据
  135. // 列表数据 分A和B两个列表 互相切换 避免闪烁
  136. dragList: {
  137. "A": [],
  138. "B": []
  139. },
  140. ListHeight: this.listHeight,
  141. listSwitch: true, // 控制切换列表
  142. // 控制滑动
  143. scrollViewTop: 0, // 滚动条位置
  144. scrollCommand: 1, //传递renderjs数据
  145. isHoldTouch: false, //是否正在拖拽
  146. isScrolling: false, //是否正在滚动视图
  147. scrollTimer: null, //定时器-控制滚动 微信小程序端使用 实现类似requestAnimationFrame效果
  148. }
  149. },
  150. props: {
  151. // 是否开启长按拖动
  152. isLongTouch: {
  153. value: Boolean,
  154. default: false
  155. },
  156. longTouchTime: {
  157. value: Number,
  158. default: 300
  159. },
  160. // 列表数据
  161. list: {
  162. value: Array,
  163. default: []
  164. },
  165. // 行高度 默认44行高
  166. rowHeight: {
  167. value: Number,
  168. default: 44
  169. },
  170. // 组件高度 默认windowHeight满屏
  171. listHeight: {
  172. value: Number,
  173. default: 0
  174. },
  175. selectId:0,//选择的id
  176. },
  177. watch: {
  178. list: {
  179. handler(val) {
  180. this.initList(); //数据变化重新初始化list
  181. },
  182. immediate: true
  183. }
  184. },
  185. mounted() {
  186. if (this.listHeight == 0) {
  187. this.ListHeight = uni.getSystemInfoSync().windowHeight;
  188. }
  189. },
  190. methods: {
  191. loadShadowRow(e) {
  192. this.shadowRow = JSON.parse(JSON.stringify(this.dragList[this.listSwitch ? "A" : "B"][e.rowIndex]));
  193. },
  194. initList() {
  195. if (this.dragList.A.length > 0) {
  196. setTimeout(() => {
  197. this.dragList.A = JSON.parse(JSON.stringify(this.list));
  198. this.dragList.B = JSON.parse(JSON.stringify(this.list));
  199. }, 50)
  200. } else {
  201. this.dragList.A = JSON.parse(JSON.stringify(this.list));
  202. this.dragList.B = JSON.parse(JSON.stringify(this.list));
  203. }
  204. },
  205. triggerClick(index, row) {
  206. this.$emit('onclick', {
  207. index: index,
  208. value: JSON.parse(JSON.stringify(row))
  209. });
  210. },
  211. //兼容微信小程序震动
  212. vibrate() {
  213. uni.vibrateShort()
  214. },
  215. // 控制自动滚动视图
  216. pageScroll(e) {
  217. // 滚动
  218. if (e.command == "up" || e.command == "down") {
  219. if (!this.isHoldTouch) {
  220. this.isHoldTouch = true;
  221. this.scrollViewTop = e.scrollTop;
  222. }
  223. if (this.isScrolling) {
  224. return;
  225. };
  226. this.isScrolling = true;
  227. // APP端和H5端 执行renderjs的滚动
  228. // #ifdef APP-PLUS || H5
  229. e.ListHeight = this.ListHeight;
  230. e.rowHeight = this.rowHeight;
  231. e.rowLength = this.list.length;
  232. this.scrollCommand = e;
  233. return;
  234. // #endif
  235. // 微信小程序执行以下逻辑
  236. this.scrollTimer != null && clearInterval(this.scrollTimer);
  237. let maxheight = this.rowHeight * this.list.length + 1 - this.ListHeight;
  238. // 16.6ms毫秒执行一次,接近60HZ
  239. this.scrollTimer = setInterval(() => {
  240. if (e.command == "up") {
  241. this.scrollViewTop -= 3
  242. }
  243. if (e.command == "down") {
  244. this.scrollViewTop += 3;
  245. }
  246. if (this.scrollViewTop < 0) {
  247. this.scrollViewTop = 0;
  248. clearInterval(this.scrollTimer);
  249. }
  250. if (this.scrollViewTop > maxheight) {
  251. this.scrollViewTop = maxheight;
  252. clearInterval(this.scrollTimer);
  253. }
  254. }, 16.6)
  255. }
  256. // 停止滚动
  257. if (e.command == "stop") {
  258. // #ifdef APP-PLUS || H5
  259. this.scrollCommand = e;
  260. // #endif
  261. this.isScrolling && this.stopScroll();
  262. }
  263. },
  264. stopScroll() {
  265. this.scrollTimer != null && clearInterval(this.scrollTimer);
  266. this.isScrolling = false;
  267. this.scrollingtop = 0;
  268. },
  269. change(e) {
  270. e.moveRow = JSON.parse(JSON.stringify(this.dragList.A[e.index]))
  271. this.$emit('change', e);
  272. },
  273. sort(e) {
  274. this.stopScroll();
  275. this.isHoldTouch = false;
  276. let tmpList = JSON.parse(JSON.stringify(this.dragList.A));
  277. tmpList.splice(e.offset, 0, tmpList.splice(e.index, 1)[0]);
  278. let listType = "A"
  279. if (this.listSwitch) {
  280. this.dragList.B = [];
  281. this.dragList.B = tmpList;
  282. } else {
  283. this.dragList.A = [];
  284. this.dragList.A = tmpList;
  285. listType = "B";
  286. }
  287. setTimeout(() => {
  288. this.resetList(listType, tmpList)
  289. }, 50)
  290. this.$emit('confirm', {
  291. list: tmpList,
  292. index: e.index,
  293. moveTo: e.offset,
  294. moveRow: JSON.parse(JSON.stringify(this.dragList.A[e.index]))
  295. });
  296. },
  297. resetList(listType, tmpList) {
  298. this.listSwitch = !this.listSwitch;
  299. this.$nextTick(() => {
  300. this.dragList[listType] = [];
  301. this.dragList[listType] = tmpList;
  302. })
  303. }
  304. }
  305. }
  306. </script>
  307. <style lang="scss">
  308. .chart-classify-item{
  309. font-size: 14px;
  310. color: #1F243A !important;
  311. }
  312. .chart-classify-item-active{
  313. color: #E3B377 !important;
  314. }
  315. //默认
  316. $text-color : #000000;
  317. $border-color :#E5E5E5;
  318. $background-color :rgba(255, 255, 255, 1);
  319. $background-color-moveing :rgba(255, 255, 255, 0.8);
  320. $shadow-background-color-moveing :rgba(0, 0, 0, 0.5);
  321. $icon-drag-color:#c7c7cb;
  322. //Dark模式
  323. $Dark-text-color : #ffffff;
  324. $Dark-border-color :#E5E5E5;
  325. $Dark-background-color :rgba(28, 28, 29, 1);
  326. $Dark-background-color-moveing :rgba(28, 28, 29, 0.8);
  327. $Dark-shadow-background-color-moveing :rgba(0, 0, 0, 0.5);
  328. $Dark-icon-drag-color:#5a5a5e;
  329. // 定义颜色 start
  330. //默认颜色
  331. .color,
  332. .rowBox-shadow {
  333. &.list{
  334. border-bottom: 1rpx $border-color solid;
  335. border-top: 1rpx $border-color solid;
  336. }
  337. .row {
  338. background-color: $background-color;
  339. &.move {
  340. background-color: $background-color-moveing;
  341. box-shadow: 0px 0px 12rpx 0px rgba(154,141,123,0.16);
  342. }
  343. .modules {
  344. border-bottom: 1rpx $border-color solid;
  345. .content {
  346. color: $text-color;
  347. }
  348. .iconfont {
  349. color: $icon-drag-color;
  350. }
  351. }
  352. }
  353. }
  354. // 暗黑模式
  355. @media (prefers-color-scheme: dark) {
  356. //Dark模式
  357. .color .rowBox-shadow {
  358. &.list{
  359. border-bottom: 1rpx $Dark-border-color solid;
  360. border-top: 1rpx $Dark-border-color solid;
  361. }
  362. .row {
  363. background-color: $Dark-background-color;
  364. &.move {
  365. background-color: $Dark-background-color-moveing;
  366. box-shadow: 0 1px 5px $Dark-shadow-background-color-moveing;
  367. }
  368. .modules {
  369. border-bottom: 1rpx $Dark-border-color solid;
  370. .content {
  371. color: $Dark-text-color;
  372. }
  373. .iconfont {
  374. color: $Dark-icon-drag-color;
  375. }
  376. }
  377. }
  378. }
  379. }
  380. // 定义颜色 end
  381. .HM-drag-sort {
  382. // touch-action: none;
  383. display: flex;
  384. flex-direction: column;
  385. position: relative;
  386. .rowBox-shadow {
  387. width: 100%;
  388. position: absolute;
  389. z-index: 100;
  390. display: none;
  391. &.show {
  392. display: flex !important;
  393. }
  394. .row {
  395. display: flex;
  396. flex-direction: row;
  397. width: 100%;
  398. &.move {
  399. // position: fixed;
  400. z-index: 100;
  401. .modules {
  402. border-bottom-width: 0;
  403. }
  404. }
  405. .modules {
  406. // margin-left: 12px;
  407. padding-left: 34rpx;
  408. padding-right: 12px;
  409. width: 100%;
  410. display: flex;
  411. flex-direction: row;
  412. align-items: center;
  413. justify-content: space-between;
  414. .drag {
  415. width: 22px;
  416. flex-shrink: 0;
  417. display: flex;
  418. flex-direction: row;
  419. justify-content: center;
  420. align-items: center;
  421. .iconfont {
  422. font-size: 22px;
  423. }
  424. }
  425. }
  426. }
  427. }
  428. .list {
  429. display: flex;
  430. flex-direction: column;
  431. &.show {
  432. display: flex;
  433. }
  434. &.hide {
  435. display: none;
  436. }
  437. .rowBox {
  438. width: 100%;
  439. &:last-child {
  440. .row {
  441. .modules {
  442. border-bottom-width: 0;
  443. }
  444. }
  445. }
  446. .row {
  447. display: flex;
  448. flex-direction: row;
  449. width: 100%;
  450. &.hide {
  451. display: none !important;
  452. }
  453. &.ani {
  454. transition: all 0.2s;
  455. -webkit-transition: all 0.2s;
  456. }
  457. .modules {
  458. // margin-left: 12px;
  459. padding-left: 34rpx;
  460. padding-right: 12px;
  461. width: 100%;
  462. display: flex;
  463. flex-direction: row;
  464. align-items: center;
  465. justify-content: space-between;
  466. .drag {
  467. width: 22px;
  468. flex-shrink: 0;
  469. display: flex;
  470. flex-direction: row;
  471. justify-content: center;
  472. align-items: center;
  473. .iconfont {
  474. font-size: 22px;
  475. }
  476. }
  477. }
  478. }
  479. }
  480. }
  481. }
  482. @font-face {
  483. font-family: "HM-DS-font";
  484. src: url('data:font/truetype;charset=utf-8;base64,AAEAAAANAIAAAwBQRkZUTYqxv5sAAAYsAAAAHEdERUYAKQAKAAAGDAAAAB5PUy8yPVJI1gAAAVgAAABWY21hcAAP6o8AAAHAAAABQmdhc3D//wADAAAGBAAAAAhnbHlmwsmUEgAAAxAAAAA0aGVhZBgr3I8AAADcAAAANmhoZWEH3gOFAAABFAAAACRobXR4DAAAAAAAAbAAAAAQbG9jYQAaAAAAAAMEAAAACm1heHABEQAYAAABOAAAACBuYW1lKeYRVQAAA0QAAAKIcG9zdEdJTj8AAAXMAAAANwABAAAAAQAAXdXjiV8PPPUACwQAAAAAANqGzEkAAAAA2obMSQAAALsEAAJFAAAACAACAAAAAAAAAAEAAAOA/4AAXAQAAAAAAAQAAAEAAAAAAAAAAAAAAAAAAAAEAAEAAAAEAAwAAwAAAAAAAgAAAAoACgAAAP8AAAAAAAAAAQQ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AGcAdQBsAGEAcgAAUmVndWxhcgAAaQBjAG8AbgBmAG8AbgB0AABpY29uZm9udAAAaQBjAG8AbgBmAG8AbgB0AABpY29uZm9udAAAVgBlAHIAcwBpAG8AbgAgADEALgAwAABWZXJzaW9uIDEuMAAAaQBjAG8AbgBmAG8AbgB0AABpY29uZm9udAAARwBlAG4AZQByAGEAdABlAGQAIABiAHkAIABzAHYAZwAyAHQAdABmACAAZgByAG8AbQAgAEYAbwBuAHQAZQBsAGwAbwAgAHAAcgBvAGoAZQBjAHQALgAAR2VuZXJhdGVkIGJ5IHN2ZzJ0dGYgZnJvbSBGb250ZWxsbyBwcm9qZWN0LgAAaAB0AHQAcAA6AC8ALwBmAG8AbgB0AGUAbABsAG8ALgBjAG8AbQAAaHR0cDovL2ZvbnRlbGxvLmNvbQAAAgAAAAAAAAAKAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAEAAAAAQACAQIMZHJhZ3NlcXVlbmNlAAAAAAH//wACAAEAAAAMAAAAFgAAAAIAAQADAAMAAQAEAAAAAgAAAAAAAAABAAAAANWkJwgAAAAA2obMSQAAAADahsxJ') format('truetype');
  485. }
  486. .iconfont {
  487. font-family: "HM-DS-font" !important;
  488. font-style: normal;
  489. &.icon-drag {
  490. &:before {
  491. content: "\e6e4";
  492. }
  493. }
  494. }
  495. </style>