index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <template>
  2. <movable-area :style="[getAreaStyle]">
  3. <movable-view v-for="(item, index) in list" :animation="animation" :direction="direction" :key="item.key"
  4. :damping="damping" :x="item.x" :y="item.y" :disabled="longpress ? disabled : false"
  5. @longpress="handleLongpress" @touchstart="handleDragStart(index)" @change="handleMoving"
  6. @touchend="handleDragEnd" :style="[getViewStyle]" class="base-drag-wrapper"
  7. :class="{ active: activeIndex === index }">
  8. <view class="drag-item">
  9. <view @longpress="toggleEdit" :class="['tab-item']">
  10. <image v-if="item.isLock" :src="icons.lock" class="right-icon" />
  11. <image v-if="!item.isLock && isActiveEdit" @click="deleteTab(item)" :src="icons.edit"
  12. class="right-icon" />
  13. <image @click="clickTap(item)" :src="isOverdueShow?item.disableUrl:item.iconUrl" class="icon" />
  14. <view class="name">{{ item.name }}</view>
  15. </view>
  16. </view>
  17. </movable-view>
  18. </movable-area>
  19. </template>
  20. <script>
  21. import {
  22. QINIU_URL
  23. } from '@/common/constant'
  24. const ICONS = {
  25. lock: `${QINIU_URL}FgMCTZCySvEgLYgkzU65BUR4f4Ls`,
  26. edit: `${QINIU_URL}Fq3V1Zi5tKH7ibTwG1nO7N96CU8m`,
  27. add: `${QINIU_URL}FqLEWbMe8DcnQtNz6GdDDD87ObZK`,
  28. placeholder: `${QINIU_URL}FpKvfFNSDbx0d9RDwyGAQdFb7Kt6`
  29. }
  30. export default {
  31. props: {
  32. column: {
  33. type: Number,
  34. default: 3
  35. },
  36. value: {
  37. type: Array,
  38. default: () => []
  39. },
  40. width: {
  41. type: Number,
  42. default: 375
  43. },
  44. height: {
  45. type: String,
  46. default: '100px'
  47. },
  48. itemKey: {
  49. type: String,
  50. required: true
  51. },
  52. itemHeight: {
  53. type: String,
  54. default: '100px'
  55. },
  56. direction: {
  57. type: String,
  58. default: 'all',
  59. validator: value => {
  60. return ['all', 'vertical', 'horizontal', 'none'].includes(value);
  61. }
  62. },
  63. animation: {
  64. type: Boolean,
  65. default: true
  66. },
  67. damping: {
  68. type: Number,
  69. default: 20
  70. },
  71. longpress: {
  72. type: Boolean,
  73. default: true
  74. },
  75. isOverdueShow: {
  76. type: Boolean,
  77. default: false
  78. },
  79. },
  80. data() {
  81. const MAX_TAB_LEN = 4
  82. return {
  83. QINIU_URL,
  84. MAX_TAB_LEN,
  85. icons: ICONS,
  86. isActiveEdit: false,
  87. placeholderTab: {
  88. name: '',
  89. icon: ICONS.placeholder,
  90. isLock: false,
  91. isPlaceholder: true
  92. },
  93. list: [],
  94. disabled: true,
  95. activeIndex: -1,
  96. moveToIndex: -1,
  97. oldIndex: -1,
  98. tempDragInfo: {
  99. x: '',
  100. y: ''
  101. },
  102. cloneList: []
  103. };
  104. },
  105. computed: {
  106. getAreaStyle() {
  107. const width = this.getRealWidth(this.width);
  108. return {
  109. width: width + 'px',
  110. height: this.height !== 'auto' ? this.height : Math.round(this.list.length / this.column) * this
  111. .getItemHeight + 'px'
  112. };
  113. },
  114. getViewStyle() {
  115. const {
  116. itemHeight,
  117. getItemWidth
  118. } = this;
  119. return {
  120. width: getItemWidth + 'px',
  121. height: itemHeight
  122. };
  123. },
  124. getItemHeight() {
  125. return parseFloat(this.itemHeight);
  126. },
  127. getItemWidth() {
  128. if (this.column === 0) return;
  129. const width = this.getRealWidth(this.width);
  130. return (parseFloat(width) / this.column).toFixed(2);
  131. }
  132. },
  133. methods: {
  134. clickTap(item){
  135. if(this.isOverdueShow) return
  136. this.$emit('changClick',item)
  137. },
  138. getMovableItem(tab) {
  139. return this.isActiveEdit && !tab.isLock && !tab.isPlaceholder
  140. },
  141. deleteTab(tab) {
  142. if (tab.isLock) return
  143. const index = this.list.findIndex(t => t.name === tab.name)
  144. if (index !== -1) {
  145. const removedTab = this.list.splice(index, 1)[0]
  146. this.cloneList.splice(index, 1)
  147. this.initList(this.cloneList);
  148. const endList = this.list.map(item => this.omit(item, ['x', 'y', 'key']));
  149. this.$emit('end', this.list, removedTab);
  150. }
  151. },
  152. addPlaceholders() {
  153. while (this.list.length < this.MAX_TAB_LEN) {
  154. this.list.push({
  155. ...this.placeholderTab
  156. })
  157. }
  158. },
  159. toggleEdit(e) {
  160. this.isActiveEdit = !this.isActiveEdit
  161. this.$emit('toggleEdit', this.isActiveEdit);
  162. },
  163. //获取实际的宽度
  164. getRealWidth(width) {
  165. let pxValue = uni.upx2px(width);
  166. // console.log(pxValue)
  167. // if (width.includes('%')) {
  168. // const windowWidth = uni.getSystemInfoSync().windowWidth;
  169. // width = windowWidth * (parseFloat(width) / 100);
  170. // }
  171. return pxValue;
  172. },
  173. initList(list = []) {
  174. const newList = this.deepCopy(list);
  175. this.list = newList.map((item, index) => {
  176. const [x, y] = this.getPosition(index);
  177. return {
  178. ...item,
  179. x,
  180. y,
  181. key: Math.random() + index
  182. };
  183. });
  184. this.cloneList = this.deepCopy(this.list);
  185. },
  186. //长按
  187. handleLongpress(index) {
  188. this.disabled = false;
  189. },
  190. //拖拽开始
  191. handleDragStart(index) {
  192. this.activeIndex = index;
  193. this.oldIndex = index;
  194. },
  195. //拖拽中
  196. handleMoving(e) {
  197. if (e.detail.source !== 'touch') return;
  198. const {
  199. x,
  200. y
  201. } = e.detail;
  202. Object.assign(this.tempDragInfo, {
  203. x,
  204. y
  205. });
  206. const currentX = Math.floor((x + this.getItemWidth / 2) / this.getItemWidth);
  207. const currentY = Math.floor((y + this.getItemHeight / 2) / this.getItemHeight);
  208. this.moveToIndex = Math.min(currentY * this.column + currentX, this.list.length - 1);
  209. if (this.oldIndex !== this.moveToIndex && this.oldIndex !== -1 && this.moveToIndex !== -1) {
  210. const newList = this.deepCopy(this.cloneList);
  211. newList.splice(this.moveToIndex, 0, ...newList.splice(this.activeIndex, 1));
  212. this.list.forEach((item, index) => {
  213. if (index !== this.activeIndex) {
  214. const itemIndex = newList.findIndex(val => val[this.itemKey] === item[this.itemKey]);
  215. [item.x, item.y] = this.getPosition(itemIndex);
  216. }
  217. });
  218. this.oldIndex = this.moveToIndex;
  219. }
  220. },
  221. //获取当前的位置
  222. getPosition(index) {
  223. const x = (index % this.column) * this.getItemWidth;
  224. const y = Math.floor(index / this.column) * this.getItemHeight;
  225. return [x, y];
  226. },
  227. //拖拽结束
  228. handleDragEnd(e) {
  229. if (this.disabled) return;
  230. if (this.moveToIndex !== -1 && this.activeIndex !== -1 && this.moveToIndex !== this.activeIndex) {
  231. this.cloneList.splice(this.moveToIndex, 0, ...this.cloneList.splice(this.activeIndex, 1));
  232. } else {
  233. this.$set(this.list[this.activeIndex], 'x', this.tempDragInfo.x);
  234. this.$set(this.list[this.activeIndex], 'y', this.tempDragInfo.y);
  235. }
  236. this.initList(this.cloneList);
  237. const endList = this.list.map(item => this.omit(item, ['x', 'y', 'key']));
  238. this.$emit('input', endList);
  239. this.$emit('end', endList);
  240. this.activeIndex = -1;
  241. this.oldIndex = -1;
  242. this.moveToIndex = -1;
  243. this.disabled = true;
  244. },
  245. deepCopy(source) {
  246. return JSON.parse(JSON.stringify(source));
  247. },
  248. /**
  249. * 排除掉obj里面的key值
  250. * @param {object} obj
  251. * @param {Array|string} args
  252. * @returns {object}
  253. */
  254. omit(obj, args) {
  255. if (!args) return obj;
  256. const newObj = {};
  257. const isString = typeof args === 'string';
  258. const keys = Object.keys(obj).filter(item => {
  259. if (isString) {
  260. return item !== args;
  261. }
  262. return !args.includes(item);
  263. });
  264. keys.forEach(key => {
  265. if (obj[key] !== undefined) newObj[key] = obj[key];
  266. });
  267. return newObj;
  268. }
  269. },
  270. watch: {
  271. value: {
  272. handler() {
  273. this.initList(this.value);
  274. },
  275. immediate: true,
  276. deep: true
  277. }
  278. }
  279. };
  280. </script>
  281. <style lang="scss" scoped>
  282. .drag-item {
  283. height: 100%;
  284. display: flex;
  285. align-items: center;
  286. justify-content: center;
  287. .tab-item {
  288. text-align: center;
  289. width: 100%;
  290. position: relative;
  291. margin-top: 24rpx;
  292. height: 100px;
  293. .name{
  294. font-size: 26rpx;
  295. }
  296. .right-icon {
  297. position: absolute;
  298. right: 20rpx;
  299. top: 0;
  300. width: 40rpx;
  301. height: 40rpx;
  302. z-index: 9;
  303. }
  304. .icon {
  305. width: 112rpx;
  306. height: 112rpx;
  307. margin-bottom: 20rpx;
  308. }
  309. .name {
  310. font-size: 26rpx;
  311. color: #060809;
  312. &.shake {
  313. animation: shake 0.2s ease-in-out infinite;
  314. }
  315. }
  316. }
  317. }
  318. .base-drag-wrapper {
  319. opacity: 1;
  320. z-index: 1;
  321. &.active {
  322. opacity: 0.7;
  323. transform: scale(1.3);
  324. z-index: 99;
  325. }
  326. }
  327. </style>
  328. <style lang="scss" scoped>
  329. @import "@/libs/css/layout.scss";
  330. .title {
  331. font-family: PingFangSC, PingFang SC;
  332. font-weight: 400;
  333. font-size: 36rpx;
  334. color: #060809;
  335. margin-bottom: 16rpx;
  336. padding-left: 40rpx;
  337. .sort-num {
  338. margin-left: 12rpx;
  339. width: 36rpx;
  340. font-family: Futura, Futura;
  341. font-size: 30rpx;
  342. color: #060809;
  343. }
  344. }
  345. .tab-item-container {
  346. display: flex;
  347. &.is-to-be-select {
  348. flex-wrap: wrap;
  349. }
  350. }
  351. .tips {
  352. font-size: 24rpx;
  353. color: #060809;
  354. text-align: center;
  355. margin-top: 32rpx;
  356. }
  357. .un-bind-btn {
  358. position: absolute;
  359. bottom: 56rpx;
  360. left: 50%;
  361. transform: translateX(-50%);
  362. width: 93%;
  363. padding: 24rpx;
  364. text-align: center;
  365. background: #E4E7EC;
  366. border-radius: 40rpx;
  367. font-family: PingFangSC, PingFang SC;
  368. font-weight: bold;
  369. font-size: 32rpx;
  370. color: #060809;
  371. &:active {
  372. opacity: .7;
  373. }
  374. }
  375. </style>