Window.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <div ref="main" class="main xyfit absolute" @click="close" @mouseup="onMouseUp" @mousemove="onMouseMove">
  3. <div ref="windowBox" class="xyfit absolute flex no-wrap" @click.stop>
  4. <div class="window flexfit column no-wrap">
  5. <div ref="header" class="header row justify-end" @mousedown.prevent.stop="onMouseDown"
  6. @touchstart.stop="onTouchStart" @touchend.stop="onTouchEnd" @touchmove.stop="onTouchMove">
  7. <span class="header-text col"><slot name="header"></slot></span>
  8. <span class="close-button row justify-center items-center" @mousedown.stop @click="close"><q-icon name="o_close" size="18px"/></span>
  9. </div>
  10. <slot></slot>
  11. </div>
  12. </div>
  13. </div>
  14. </template>
  15. <script>
  16. //-----------------------------------------------------------------------------
  17. import Vue from 'vue';
  18. import Component from 'vue-class-component';
  19. export default @Component({
  20. props: {
  21. height: { type: String, default: '100%' },
  22. width: { type: String, default: '100%' },
  23. maxWidth: { type: String, default: '' },
  24. topShift: { type: Number, default: 0 },
  25. }
  26. })
  27. class Window extends Vue {
  28. init() {
  29. this.$nextTick(() => {
  30. this.$refs.windowBox.style.height = this.height;
  31. this.$refs.windowBox.style.width = this.width;
  32. if (this.maxWidth)
  33. this.$refs.windowBox.style.maxWidth = this.maxWidth;
  34. const left = (this.$refs.main.offsetWidth - this.$refs.windowBox.offsetWidth)/2;
  35. const top = (this.$refs.main.offsetHeight - this.$refs.windowBox.offsetHeight)/2 + this.topShift;
  36. this.$refs.windowBox.style.left = (left > 0 ? left : 0) + 'px';
  37. this.$refs.windowBox.style.top = (top > 0 ? top : 0) + 'px';
  38. });
  39. }
  40. onMouseDown(event) {
  41. if (this.$isMobileDevice)
  42. return;
  43. if (event.button == 0) {
  44. this.$refs.header.style.cursor = 'move';
  45. this.startX = event.screenX;
  46. this.startY = event.screenY;
  47. this.moving = true;
  48. }
  49. }
  50. onMouseUp(event) {
  51. if (event.button == 0) {
  52. this.$refs.header.style.cursor = 'default';
  53. this.moving = false;
  54. }
  55. }
  56. onMouseMove(event) {
  57. if (this.moving) {
  58. const deltaX = event.screenX - this.startX;
  59. const deltaY = event.screenY - this.startY;
  60. this.startX = event.screenX;
  61. this.startY = event.screenY;
  62. this.$refs.windowBox.style.left = (this.$refs.windowBox.offsetLeft + deltaX) + 'px';
  63. this.$refs.windowBox.style.top = (this.$refs.windowBox.offsetTop + deltaY) + 'px';
  64. }
  65. }
  66. onTouchStart(event) {
  67. if (!this.$isMobileDevice)
  68. return;
  69. if (event.touches.length == 1) {
  70. const touch = event.touches[0];
  71. this.$refs.header.style.cursor = 'move';
  72. this.startX = touch.screenX;
  73. this.startY = touch.screenY;
  74. this.moving = true;
  75. }
  76. }
  77. onTouchMove(event) {
  78. if (!this.$isMobileDevice)
  79. return;
  80. if (event.touches.length == 1 && this.moving) {
  81. const touch = event.touches[0];
  82. const deltaX = touch.screenX - this.startX;
  83. const deltaY = touch.screenY - this.startY;
  84. this.startX = touch.screenX;
  85. this.startY = touch.screenY;
  86. this.$refs.windowBox.style.left = (this.$refs.windowBox.offsetLeft + deltaX) + 'px';
  87. this.$refs.windowBox.style.top = (this.$refs.windowBox.offsetTop + deltaY) + 'px';
  88. }
  89. }
  90. onTouchEnd() {
  91. if (!this.$isMobileDevice)
  92. return;
  93. this.$refs.header.style.cursor = 'default';
  94. this.moving = false;
  95. }
  96. close() {
  97. if (!this.moving)
  98. this.$emit('close');
  99. }
  100. }
  101. //-----------------------------------------------------------------------------
  102. </script>
  103. <style scoped>
  104. .main {
  105. z-index: 50;
  106. }
  107. .xyfit {
  108. height: 100%;
  109. width: 100%;
  110. }
  111. .flexfit {
  112. flex: 1;
  113. }
  114. .window {
  115. margin: 10px;
  116. background-color: #ffffff;
  117. border: 3px double black;
  118. border-radius: 4px;
  119. box-shadow: 3px 3px 5px black;
  120. }
  121. .header {
  122. background: linear-gradient(to bottom right, green, #59B04F);
  123. align-items: center;
  124. height: 30px;
  125. }
  126. .header-text {
  127. margin-left: 10px;
  128. margin-right: 10px;
  129. color: #E8E8E8;
  130. text-shadow: 2px 1px 5px black, 2px 2px 5px black;
  131. }
  132. .close-button {
  133. width: 30px;
  134. height: 30px;
  135. cursor: pointer;
  136. }
  137. .close-button:hover {
  138. background-color: #69C05F;
  139. }
  140. </style>