Window.vue 4.8 KB

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