vueDragResize.blade.php 31 KB

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