dragResize.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. const stickSize = 18;
  2. const styleMapping = {
  3. y: {
  4. t: 'top',
  5. m: 'marginTop',
  6. b: 'bottom',
  7. },
  8. x: {
  9. l: 'left',
  10. m: 'marginLeft',
  11. r: 'right',
  12. }
  13. };
  14. export default {
  15. name: 'vue-drag-resize',
  16. props: {
  17. parentW: {
  18. type: Number,
  19. default: 0,
  20. validator: function (val) {
  21. return val >= 0
  22. }
  23. },
  24. parentH: {
  25. type: Number,
  26. default: 0,
  27. validator: function (val) {
  28. return val >= 0
  29. }
  30. },
  31. w: {
  32. type: [String, Number],
  33. default: '100px',
  34. validator: function (val) {
  35. return typeof val === 'string' || typeof val === 'number'
  36. }
  37. },
  38. h: {
  39. type: Number,
  40. default: 100,
  41. validator: function (val) {
  42. return val > 0
  43. }
  44. },
  45. pos: {
  46. type: String,
  47. default: 'relative',
  48. },
  49. mLeft: {
  50. },
  51. mTop: {
  52. },
  53. mRight: {
  54. },
  55. mBottom: {
  56. },
  57. x: {
  58. type: [String, Number],
  59. default: '0px',
  60. validator: function (val) {
  61. return typeof val === 'string' || typeof val === 'number'
  62. }
  63. },
  64. y: {
  65. type: Number,
  66. default: 0,
  67. validator: function (val) {
  68. return typeof val === 'number'
  69. }
  70. },
  71. z: {
  72. type: [String, Number],
  73. default: 0,
  74. },
  75. hAuto: {
  76. type: Boolean,
  77. default: true,
  78. },
  79. sticks: {
  80. type: Array,
  81. default: function () {
  82. // return ['tl', 'tm', 'tr', 'mr', 'br', 'bm', 'bl', 'ml']
  83. return ['bl', 'br']
  84. }
  85. }
  86. },
  87. data() {
  88. return {
  89. parentWidth: null,
  90. parentHeight: null,
  91. rawWidth: null,
  92. rawHeight: this.h,
  93. position: this.pos,
  94. marginLeft: this.mLeft,
  95. marginTop: this.mTop,
  96. marginRight: this.mRight,
  97. marginBottom: this.mBottom,
  98. rawLeft: null,
  99. rawTop: this.y,
  100. rawRight: null,
  101. rawBottom: null,
  102. left: null,
  103. top: this.y,
  104. right: null,
  105. bottom: null,
  106. zIndex: this.z,
  107. heightAuto: this.hAuto,
  108. minWidth: 10,
  109. minHeight: 10,
  110. active: false,
  111. positionType: null,
  112. widthType: null,
  113. verPosType: 'top',
  114. perWidth: null,
  115. middlePosStr: '',
  116. rightPosStr: '',
  117. showSettingPanel: false
  118. }
  119. },
  120. provide() {
  121. return {
  122. heightAuto: this
  123. }
  124. },
  125. created() {
  126. this.stickDrag = false;
  127. this.bodyDrag = false;
  128. this.stickAxis = null;
  129. this.stickStartPos = { mouseX: 0, mouseY: 0, x: 0, y: 0, w: 0, h: 0 };
  130. this.limits = {
  131. minLeft: null,
  132. maxLeft: null,
  133. minRight: null,
  134. maxRight: null,
  135. minTop: null,
  136. maxTop: null,
  137. minBottom: null,
  138. maxBottom: null
  139. };
  140. this.currentStick = [];
  141. },
  142. mounted() {
  143. this.parentElement = this.$el.parentNode;
  144. this.parentWidth = this.parentW ? this.parentW : this.parentElement.clientWidth;
  145. this.parentHeight = this.parentH ? this.parentH : this.parentElement.clientHeight;
  146. if (typeof this.w === 'number' || this.w.endsWith('px')) {
  147. this.widthType = 'num';
  148. this.rawWidth = typeof this.w === 'string' ? parseInt(this.w.replace('px', '')) : this.w;
  149. this.perWidth = Math.round(this.rawWidth * 100 / this.parentWidth);
  150. } else if (this.w.endsWith('%')) {
  151. this.widthType = 'percent';
  152. this.perWidth = parseInt(this.w.replace('%', ''));
  153. this.rawWidth = Math.round(this.perWidth * this.parentWidth / 100);
  154. }
  155. if (typeof this.x === 'number' || this.x.endsWith('px')) {
  156. this.positionType = 'left';
  157. this.rawLeft = typeof this.x === 'string' ? parseInt(this.x.replace('px', '')) : this.x;
  158. } else if (this.x.indexOf('100%') != -1) {
  159. this.positionType = 'right';
  160. this.rightPosStr = this.x;
  161. //提取字符串calc(100% - npx)中的n
  162. this.rawLeft = this.parentWidth - this.rawWidth - parseInt(this.x.match(/\d+(.\d+)?/g)[2]);
  163. } else if (this.x.indexOf('50%') != -1) {
  164. this.positionType = 'middle';
  165. this.middlePosStr = this.x;
  166. this.rawLeft = Math.round((this.parentWidth - this.rawWidth) / 2);
  167. }
  168. this.rawBottom = this.parentHeight - this.rawHeight - this.rawTop;
  169. this.rawRight = this.parentWidth - this.rawLeft - this.rawWidth;
  170. document.documentElement.addEventListener('mousemove', this.move);
  171. document.documentElement.addEventListener('mouseup', this.up);
  172. document.documentElement.addEventListener('mouseleave', this.up);
  173. document.documentElement.addEventListener('mousedown', this.deselect);
  174. document.documentElement.addEventListener('touchmove', this.move, true);
  175. document.documentElement.addEventListener('touchend touchcancel', this.up, true);
  176. document.documentElement.addEventListener('touchstart', this.up, true);
  177. },
  178. beforeDestroy() {
  179. document.documentElement.removeEventListener('mousemove', this.move);
  180. document.documentElement.removeEventListener('mouseup', this.up);
  181. document.documentElement.removeEventListener('mouseleave', this.up);
  182. document.documentElement.removeEventListener('mousedown', this.deselect);
  183. document.documentElement.removeEventListener('touchmove', this.move, true);
  184. document.documentElement.removeEventListener('touchend touchcancel', this.up, true);
  185. document.documentElement.removeEventListener('touchstart', this.up, true);
  186. },
  187. methods: {
  188. deselect() {
  189. this.active = false;
  190. },
  191. move(ev) {
  192. if (this.position == 'relative') return;
  193. if (!this.stickDrag && !this.bodyDrag) {
  194. return
  195. }
  196. ev.stopPropagation();
  197. if (this.stickDrag) {
  198. this.stickMove(ev);
  199. }
  200. if (this.bodyDrag) {
  201. this.bodyMove(ev)
  202. }
  203. },
  204. up(ev) {
  205. if (this.position == 'relative') return;
  206. if (this.stickDrag) {
  207. this.stickUp(ev);
  208. }
  209. if (this.bodyDrag) {
  210. this.bodyUp(ev)
  211. // this.moveStickUp(ev);
  212. }
  213. },
  214. bodyDown(ev) {
  215. if (this.position == 'relative') return;
  216. this.$store.dispatch('event/setActiveFlag');
  217. if (ev.button && ev.button !== 0) {
  218. return
  219. }
  220. ev.stopPropagation();
  221. ev.preventDefault();
  222. setTimeout(() => { this.active = true; }, 0);
  223. this.bodyDrag = true;
  224. this.$store.dispatch('event/changeMouseDown', this.bodyDrag);
  225. this.$emit('saveOrDeleteId', this.bodyDrag);
  226. this.stickStartPos.mouseX = ev.pageX || ev.touches[0].pageX;
  227. this.stickStartPos.mouseY = ev.pageY || ev.touches[0].pageY;
  228. this.stickStartPos.left = this.left;
  229. this.stickStartPos.right = this.right;
  230. this.stickStartPos.top = this.top;
  231. this.stickStartPos.bottom = this.bottom;
  232. this.limits = this.calcDragLimitation();
  233. },
  234. //高度改变时limit没有重新计算
  235. calcDragLimitation() {
  236. const parentWidth = this.parentWidth;
  237. const parentHeight = this.parentHeight;
  238. return {
  239. minLeft: 0,
  240. maxLeft: parentWidth - this.width,
  241. minRight: 0,
  242. maxRight: parentWidth - this.width,
  243. minTop: 0,
  244. maxTop: parentHeight - this.height,
  245. minBottom: 0,
  246. maxBottom: parentHeight - this.height
  247. }
  248. },
  249. bodyMove(ev) {
  250. if (this.position == 'relative') return;
  251. const stickStartPos = this.stickStartPos;
  252. let delta = {
  253. x: stickStartPos.mouseX - (ev.pageX || ev.touches[0].pageX),
  254. y: stickStartPos.mouseY - (ev.pageY || ev.touches[0].pageY)
  255. };
  256. this.rawTop = stickStartPos.top - delta.y;
  257. this.rawBottom = stickStartPos.bottom + delta.y;
  258. if (this.positionType != 'middle') {
  259. this.rawLeft = stickStartPos.left - delta.x;
  260. this.rawRight = stickStartPos.right + delta.x;
  261. }
  262. this.$emit('changeRect', this.rect);
  263. let tempX = ev.pageX || ev.touches[0].pageX;
  264. let tempY = ev.pageY || ev.touches[0].pageY;
  265. //开始MOVE,且位移不为0
  266. if (Math.sqrt((tempX - this.stickStartPos.mouseX) * (tempX - this.stickStartPos.mouseX) + (tempY - this.stickStartPos.mouseY) * (tempY - this.stickStartPos.mouseY)) != 0) {
  267. this.$store.dispatch('event/changeIsMove', true);
  268. }
  269. },
  270. bodyUp(ev) {
  271. if (this.position == 'relative') return;
  272. this.bodyDrag = false;
  273. this.$store.dispatch('event/changeMouseDown', this.bodyDrag);
  274. this.checkLimit();
  275. this.$emit('changeRect', this.rect);
  276. const vm = this;
  277. setTimeout(() => {
  278. vm.$store.dispatch('event/changeIsMove', false);
  279. }, 0);
  280. this.$emit('saveOrDeleteId', this.bodyDrag);
  281. this.stickStartPos = { mouseX: 0, mouseY: 0, x: 0, y: 0, w: 0, h: 0 };
  282. this.limits = {
  283. minLeft: null,
  284. maxLeft: null,
  285. minRight: null,
  286. maxRight: null,
  287. minTop: null,
  288. maxTop: null,
  289. minBottom: null,
  290. maxBottom: null
  291. };
  292. },
  293. stickDown(stick, ev) {
  294. if (!this.active || this.position == 'relative') {
  295. return
  296. }
  297. this.stickDrag = true;
  298. this.stickStartPos.mouseX = ev.pageX || ev.touches[0].pageX;
  299. this.stickStartPos.mouseY = ev.pageY || ev.touches[0].pageY;
  300. this.stickStartPos.left = this.left;
  301. this.stickStartPos.right = this.right;
  302. this.stickStartPos.top = this.top;
  303. this.stickStartPos.bottom = this.bottom;
  304. this.currentStick = stick.split('');
  305. this.stickAxis = null;
  306. switch (this.currentStick[0]) {
  307. case 'b':
  308. this.stickAxis = 'y';
  309. break;
  310. case 't':
  311. this.stickAxis = 'y';
  312. break;
  313. }
  314. switch (this.currentStick[1]) {
  315. case 'r':
  316. this.stickAxis = this.stickAxis === 'y' ? 'xy' : 'x';
  317. break;
  318. case 'l':
  319. this.stickAxis = this.stickAxis === 'y' ? 'xy' : 'x';
  320. break;
  321. }
  322. this.limits = this.calcResizeLimitation();
  323. },
  324. calcResizeLimitation() {
  325. const width = this.width;
  326. const height = this.height;
  327. const bottom = this.bottom;
  328. const top = this.top;
  329. const left = this.left;
  330. const right = this.right;
  331. const parentLim = 0;
  332. let limits = {
  333. minLeft: parentLim,
  334. maxLeft: left + (width - this.minWidth),
  335. minRight: parentLim,
  336. maxRight: right + (width - this.minWidth),
  337. minTop: parentLim,
  338. maxTop: top + (height - this.minHeight),
  339. minBottom: parentLim,
  340. maxBottom: bottom + (height - this.minHeight)
  341. };
  342. return limits;
  343. },
  344. stickMove(ev) {
  345. if (this.position == 'relative') return;
  346. const stickStartPos = this.stickStartPos;
  347. const delta = {
  348. x: stickStartPos.mouseX - (ev.pageX || ev.touches[0].pageX),
  349. y: stickStartPos.mouseY - (ev.pageY || ev.touches[0].pageY)
  350. };
  351. switch (this.currentStick[0]) {
  352. case 'b':
  353. this.rawBottom = stickStartPos.bottom + delta.y;
  354. break;
  355. case 't':
  356. this.rawTop = stickStartPos.top - delta.y;
  357. break;
  358. }
  359. if (this.widthType == 'num') {
  360. switch (this.currentStick[1]) {
  361. case 'r':
  362. if (this.positionType == "middle") {
  363. if (stickStartPos.left + 2 * delta.x + stickStartPos.right < this.parentWidth) {
  364. this.rawRight = stickStartPos.right + delta.x;
  365. this.rawLeft = stickStartPos.left + delta.x;
  366. this.middlePosStr = 'calc(50% - ' + Math.round(this.rawWidth / 2) + 'px)';
  367. }
  368. }
  369. else {
  370. this.rawRight = stickStartPos.right + delta.x;
  371. }
  372. break;
  373. case 'l':
  374. if (this.positionType == "middle") {
  375. if (stickStartPos.left - 2 * delta.x + stickStartPos.right < this.parentWidth) {
  376. this.rawLeft = stickStartPos.left - delta.x;
  377. this.rawRight = stickStartPos.right - delta.x;
  378. this.middlePosStr = 'calc(50% - ' + Math.round(this.rawWidth / 2) + 'px)'
  379. }
  380. }
  381. else {
  382. this.rawLeft = stickStartPos.left - delta.x;
  383. }
  384. break;
  385. }
  386. }
  387. this.$emit('changeRect', this.rect);
  388. },
  389. stickUp() {
  390. if (this.position == 'relative') return;
  391. this.stickDrag = false;
  392. this.stickStartPos = {
  393. mouseX: 0,
  394. mouseY: 0,
  395. x: 0,
  396. y: 0,
  397. w: 0,
  398. h: 0
  399. };
  400. this.limits = {
  401. minLeft: null,
  402. maxLeft: null,
  403. minRight: null,
  404. maxRight: null,
  405. minTop: null,
  406. maxTop: null,
  407. minBottom: null,
  408. maxBottom: null
  409. };
  410. this.rawTop = this.top;
  411. this.rawBottom = this.bottom;
  412. this.rawLeft = this.left;
  413. this.rawRight = this.right;
  414. this.stickAxis = null;
  415. this.$emit('changeRect', this.rect);
  416. this.$store.dispatch('event/changeStick');
  417. },
  418. checkLimit() {
  419. const limits = this.limits;
  420. if (limits.minLeft !== null && this.rawLeft < limits.minLeft) {
  421. this.rawLeft = limits.minLeft;
  422. } else if (limits.maxLeft !== null && limits.maxLeft < this.rawLeft) {
  423. this.rawLeft = limits.maxLeft;
  424. }
  425. if (limits.minRight !== null && this.rawRight < limits.minRight) {
  426. this.rawRight = limits.minRight;
  427. } else if (limits.maxRight !== null && limits.maxRight < this.rawRight) {
  428. this.rawRight = limits.maxRight;
  429. }
  430. if (limits.minTop !== null && this.rawTop < limits.minTop) {
  431. this.rawTop = limits.minTop;
  432. } else if (limits.maxTop !== null && limits.maxTop < this.rawTop) {
  433. this.rawTop = limits.maxTop;
  434. }
  435. if (limits.minBottom !== null && this.rawBottom < limits.minBottom) {
  436. this.rawBottom = limits.minBottom;
  437. } else if (limits.maxBottom !== null && limits.maxBottom < this.rawBottom) {
  438. this.rawBottom = limits.maxBottom;
  439. }
  440. this.left = this.rawLeft;
  441. this.right = this.rawRight;
  442. this.top = this.rawTop;
  443. this.bottom = this.rawBottom;
  444. },
  445. changePositionType() {
  446. if (this.positionType == 'middle') {
  447. let halfWidthStr = this.widthType == 'num' ? Math.round(this.rawWidth / 2) + 'px' : Math.round(this.perWidth / 2) + '%';
  448. this.middlePosStr = 'calc(50% - ' + halfWidthStr + ')';
  449. this.rawLeft = Math.round(this.parentWidth / 2) - this.widthType == 'num' ? Math.round(this.rawWidth / 2) : Math.round(this.parentWidth * this.perWidth / 200);
  450. this.rawRight = this.parentWidth - this.rawLeft - this.rawWidth;
  451. }
  452. else if (this.positionType == 'right') {
  453. this.rightPosStr = 'calc(100% - ' + (this.widthType == 'num' ? this.rawWidth + 'px - ' : this.perWidth + '% - ') + this.rawRight + 'px)';
  454. }
  455. this.$emit('changeRect', this.rect);
  456. },
  457. changeWidth() {
  458. if (this.widthType == "percent") {
  459. this.perWidth = Math.round(this.rawWidth * 100 / this.parentWidth);
  460. }
  461. if (this.widthType == "num") {
  462. this.rawWidth = Math.round(this.perWidth * this.parentWidth / 100);
  463. }
  464. this.$emit('changeRect', this.rect);
  465. },
  466. deleteElement() {
  467. this.$emit('deleteElement');
  468. },
  469. copy() {
  470. this.$emit('copy');
  471. },
  472. paste() {
  473. this.$emit('paste');
  474. }
  475. },
  476. computed: {
  477. style() {
  478. let tempStyle = {
  479. position: this.position,
  480. zIndex: this.zIndex
  481. }
  482. if (!this.heightAuto) {
  483. tempStyle.width = this.widthType == 'num' ? this.width + 'px' : this.perWidth + '%',
  484. tempStyle.height = this.height + 'px';
  485. }
  486. if (this.position == 'relative') {
  487. tempStyle.marginLeft = this.marginLeft + 'px';
  488. tempStyle.marginTop = this.marginTop + 'px';
  489. tempStyle.marginRight = this.marginRight + 'px';
  490. tempStyle.marginBottom = this.marginBottom + 'px';
  491. }
  492. else {
  493. tempStyle.top = this.top + 'px';
  494. switch (this.positionType) {
  495. case 'left':
  496. tempStyle.left = this.left + 'px';
  497. break;
  498. case 'middle':
  499. tempStyle.left = this.middlePosStr;
  500. break;
  501. case 'right':
  502. tempStyle.left = this.rightPosStr;
  503. break;
  504. }
  505. }
  506. return tempStyle;
  507. },
  508. vdrStick() {
  509. return (stick) => {
  510. const stickStyle = {
  511. width: `${stickSize}px`,
  512. height: `${stickSize}px`,
  513. };
  514. stickStyle[styleMapping.y[stick[0]]] = '0px';
  515. stickStyle[styleMapping.x[stick[1]]] = '0px';
  516. return stickStyle;
  517. }
  518. },
  519. width() {
  520. if (this.position == 'relative') {
  521. return this.rawWidth;
  522. }
  523. else {
  524. return this.parentWidth - this.left - this.right;
  525. }
  526. },
  527. height() {
  528. if (this.position == 'relative') {
  529. return this.rawHeight;
  530. }
  531. else {
  532. // return this.parentHeight - this.top - this.bottom;
  533. return this.rawHeight;
  534. }
  535. },
  536. rect() {
  537. let tempRect = {
  538. top: Math.round(this.top),
  539. width: this.widthType == 'num' ? Math.round(this.width) : this.perWidth + '%',
  540. height: Math.round(this.height),
  541. zIndex: this.zIndex,
  542. position: this.position,
  543. marginLeft: this.marginLeft,
  544. marginTop: this.marginTop,
  545. marginRight: this.marginRight,
  546. marginBottom: this.marginBottom,
  547. heightAuto: this.heightAuto,
  548. }
  549. switch (this.positionType) {
  550. case 'left':
  551. tempRect.left = this.left;
  552. break;
  553. case 'middle':
  554. tempRect.left = this.middlePosStr;
  555. break;
  556. case 'right':
  557. tempRect.left = this.rightPosStr;
  558. break;
  559. }
  560. return tempRect;
  561. }
  562. },
  563. watch: {
  564. bottom(val) {
  565. this.calcDragLimitation();
  566. },
  567. rawLeft(newLeft) {
  568. if (this.stickDrag) {
  569. const limits = this.limits;
  570. if (limits.minLeft !== null && newLeft < limits.minLeft) {
  571. newLeft = limits.minLeft;
  572. } else if (limits.maxLeft !== null && limits.maxLeft < newLeft) {
  573. newLeft = limits.maxLeft;
  574. }
  575. }
  576. this.left = newLeft;
  577. this.rawWidth = this.parentWidth - newLeft - this.rawRight;
  578. if (this.positionType == 'right') {
  579. this.rightPosStr = 'calc(100% - ' + (this.widthType == 'num' ? this.rawWidth + 'px - ' : this.perWidth + '% - ') + this.rawRight + 'px)';
  580. }
  581. },
  582. rawRight(newRight) {
  583. if (this.stickDrag) {
  584. const limits = this.limits;
  585. if (limits.minRight !== null && newRight < limits.minRight) {
  586. newRight = limits.minRight;
  587. } else if (limits.maxRight !== null && limits.maxRight < newRight) {
  588. newRight = limits.maxRight;
  589. }
  590. }
  591. this.right = newRight;
  592. this.rawWidth = this.parentWidth - newRight - this.rawLeft;
  593. if (this.positionType == 'right') {
  594. this.rightPosStr = 'calc(100% - ' + (this.widthType == 'num' ? this.rawWidth + 'px - ' : this.perWidth + '% - ') + this.rawRight + 'px)';
  595. }
  596. },
  597. rawTop(newTop) {
  598. if (this.stickDrag) {
  599. const limits = this.limits;
  600. if (limits.minTop !== null && newTop < limits.minTop) {
  601. newTop = limits.minTop;
  602. } else if (limits.maxTop !== null && limits.maxTop < newTop) {
  603. newTop = limits.maxTop;
  604. }
  605. }
  606. this.top = newTop;
  607. },
  608. rawBottom(newBottom) {
  609. if (this.stickDrag) {
  610. const limits = this.limits;
  611. if (limits.minBottom !== null && newBottom < limits.minBottom) {
  612. newBottom = limits.minBottom;
  613. } else if (limits.maxBottom !== null && limits.maxBottom < newBottom) {
  614. newBottom = limits.maxBottom;
  615. }
  616. }
  617. this.bottom = newBottom;
  618. },
  619. rawHeight(val) {
  620. if (this.position == 'relative') {
  621. this.$emit('changeRect', this.rect);
  622. this.$emit('elementChangeSize');
  623. this.$store.dispatch('event/changeStick');
  624. }
  625. },
  626. rawWidth() {
  627. if (this.position == 'relative') {
  628. this.$emit('changeRect', this.rect);
  629. this.$store.dispatch('event/changeStick');
  630. }
  631. },
  632. perWidth(newVal) {
  633. if (this.widthType != 'percent') return;
  634. if (this.position == 'relative') {
  635. this.rawWidth = Math.round(newVal * this.parentWidth / 100);
  636. }
  637. else {
  638. switch (this.positionType) {
  639. case 'left':
  640. this.rawRight = this.parentWidth - this.rawLeft - Math.round(newVal * this.parentWidth / 100);
  641. break;
  642. case 'middle':
  643. this.rawLeft = Math.round(this.parentWidth * (50 - Math.round(newVal / 2)) / 100);
  644. this.rawRight = this.parentWidth - this.rawLeft - Math.round(newVal * this.parentWidth / 100);
  645. this.middlePosStr = 'calc(50% - ' + Math.round(newVal / 2) + '%)'
  646. break;
  647. case 'right':
  648. this.rawRight = this.parentWidth - this.rawLeft - Math.round(newVal * this.parentWidth / 100);
  649. this.rightPosStr = 'calc(100% - ' + (this.widthType == 'num' ? this.rawWidth + 'px - ' : newVal + '% - ') + this.rawRight + 'px)';
  650. break;
  651. }
  652. this.$emit('changeRect', this.rect);
  653. this.$store.dispatch('event/changeStick');
  654. }
  655. },
  656. zIndex(val) {
  657. this.$emit('changeRect', this.rect);
  658. },
  659. z(val) {
  660. if (val >= 0 || val === 'auto') {
  661. this.zIndex = val
  662. }
  663. },
  664. position(val) {
  665. if (this.heightAuto && val == 'absolute') {
  666. this.heightAuto = false;
  667. }
  668. this.$emit('changeRect', this.rect);
  669. this.$emit('elementChangeSize');
  670. },
  671. heightAuto() {
  672. this.$emit('changeRect', this.rect);
  673. },
  674. // x() {
  675. // if (this.stickDrag || this.bodyDrag) {
  676. // return
  677. // }
  678. // this.limits = this.calcDragLimitation();
  679. // let delta = this.x - this.left;
  680. // this.rawLeft = this.x;
  681. // this.rawRight = this.right - delta;
  682. // },
  683. y() {
  684. if (this.stickDrag || this.bodyDrag) {
  685. return
  686. }
  687. this.limits = this.calcDragLimitation();
  688. let delta = this.y - this.top;
  689. this.rawTop = this.y;
  690. this.rawBottom = this.bottom - delta;
  691. },
  692. // w() {
  693. // if (this.stickDrag || this.bodyDrag) {
  694. // return
  695. // }
  696. // this.currentStick = ['m', 'r'];
  697. // this.stickAxis = 'x';
  698. // this.limits = this.calcResizeLimitation();
  699. // let delta = this.width - this.w;
  700. // this.rawRight = this.right + delta;
  701. // },
  702. h() {
  703. if (this.stickDrag || this.bodyDrag) {
  704. return
  705. }
  706. this.currentStick = ['b', 'm'];
  707. this.stickAxis = 'y';
  708. this.limits = this.calcResizeLimitation();
  709. let delta = this.height - this.h;
  710. this.rawBottom = this.bottom + delta;
  711. },
  712. marginLeft() {
  713. this.$emit('changeRect', this.rect);
  714. this.$emit('elementChangeSize');
  715. },
  716. marginTop() {
  717. this.$emit('changeRect', this.rect);
  718. this.$emit('elementChangeSize');
  719. },
  720. marginRight() {
  721. this.$emit('changeRect', this.rect);
  722. this.$emit('elementChangeSize');
  723. },
  724. marginBottom() {
  725. this.$emit('changeRect', this.rect);
  726. this.$emit('elementChangeSize');
  727. },
  728. '$store.state.event.activeFlag'() {
  729. this.active = false;
  730. },
  731. parentW(val) {
  732. this.parentWidth = val;
  733. if (this.widthType == 'percent') {
  734. this.rawWidth = Math.round(this.perWidth * val / 100);
  735. }
  736. if (this.positionType == 'middle') {
  737. this.rawLeft = Math.round((this.parentWidth - this.rawWidth) / 2);
  738. this.rawRight = this.parentWidth - this.rawLeft - this.rawWidth;
  739. }
  740. else if (this.positionType == 'right') {
  741. this.rawLeft = val - this.rawWidth - this.rawRight;
  742. }
  743. else {
  744. this.rawRight = val - this.rawWidth - this.rawLeft;
  745. }
  746. },
  747. parentH(val) {
  748. this.parentHeight = val;
  749. this.rawBottom = val - this.rawHeight - this.rawTop;
  750. },
  751. }
  752. }