Window.vue 5.0 KB

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