Window.vue 4.9 KB

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