FloatingBox.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <div
  3. ref="box"
  4. class="box"
  5. :id="id"
  6. v-if="shown"
  7. :style="{
  8. width: width + 'px',
  9. height: height + 'px',
  10. top: top + 'px',
  11. left: left + 'px'
  12. }"
  13. @mousedown="onResizeBox"
  14. >
  15. <div class="box-header" @mousedown="onDragBox">
  16. <slot name="header"></slot>
  17. </div>
  18. <div class="box-body">
  19. <slot name="body"></slot>
  20. </div>
  21. </div>
  22. </template>
  23. <script>
  24. export default {
  25. props: {
  26. id: { type: String, default: null }
  27. },
  28. data() {
  29. return {
  30. width: 200,
  31. height: 200,
  32. top: 0,
  33. left: 0,
  34. shown: false,
  35. pos1: 0,
  36. pos2: 0,
  37. pos3: 0,
  38. pos4: 0
  39. };
  40. },
  41. mounted() {
  42. if (this.id !== null && localStorage[`box:${this.id}`]) {
  43. const json = JSON.parse(localStorage.getItem(`box:${this.id}`));
  44. this.height = json.height;
  45. this.width = json.width;
  46. this.top = json.top;
  47. this.left = json.left;
  48. this.shown = json.shown;
  49. }
  50. },
  51. methods: {
  52. onDragBox(e) {
  53. const e1 = e || window.event;
  54. e1.preventDefault();
  55. this.pos3 = e1.clientX;
  56. this.pos4 = e1.clientY;
  57. document.onmousemove = e => {
  58. const e2 = e || window.event;
  59. e2.preventDefault();
  60. // calculate the new cursor position:
  61. this.pos1 = this.pos3 - e.clientX;
  62. this.pos2 = this.pos4 - e.clientY;
  63. this.pos3 = e.clientX;
  64. this.pos4 = e.clientY;
  65. // set the element's new position:
  66. this.top -= this.pos2;
  67. this.left -= this.pos1;
  68. };
  69. document.onmouseup = () => {
  70. document.onmouseup = null;
  71. document.onmousemove = null;
  72. this.saveBox();
  73. };
  74. },
  75. onResizeBox(e) {
  76. if (e.target !== this.$refs.box) return;
  77. document.onmouseup = () => {
  78. document.onmouseup = null;
  79. const { height, width } = e.target.style;
  80. this.height = Number(
  81. height
  82. .split("")
  83. .splice(0, height.length - 2)
  84. .join("")
  85. );
  86. this.width = Number(
  87. width
  88. .split("")
  89. .splice(0, width.length - 2)
  90. .join("")
  91. );
  92. this.saveBox();
  93. };
  94. },
  95. toggleBox() {
  96. this.shown = !this.shown;
  97. this.saveBox();
  98. },
  99. resetBox() {
  100. this.top = 0;
  101. this.left = 0;
  102. this.width = 200;
  103. this.height = 200;
  104. this.saveBox();
  105. },
  106. saveBox() {
  107. if (this.id === null) return;
  108. localStorage.setItem(
  109. `box:${this.id}`,
  110. JSON.stringify({
  111. height: this.height,
  112. width: this.width,
  113. top: this.top,
  114. left: this.left,
  115. shown: this.shown
  116. })
  117. );
  118. }
  119. }
  120. };
  121. </script>
  122. <style lang="scss" scoped>
  123. @import "../../styles/global.scss";
  124. .box {
  125. background-color: white;
  126. position: fixed;
  127. z-index: 10000000;
  128. resize: both;
  129. overflow: auto;
  130. border: 1px solid #d3d3d3;
  131. min-height: 50px !important;
  132. min-width: 50px !important;
  133. .box-header {
  134. cursor: move;
  135. z-index: 100000001;
  136. background-color: $musare-blue;
  137. padding: 10px;
  138. display: block;
  139. height: 10px;
  140. width: 100%;
  141. }
  142. .box-body {
  143. display: flex;
  144. flex-wrap: wrap;
  145. justify-content: space-evenly;
  146. span {
  147. padding: 3px 6px;
  148. }
  149. }
  150. }
  151. </style>