annotation-box.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <template>
  2. <div class="annotation-box" :style="style" @mousedown.stop.prevent="moveSelf" @touchstart.stop.prevent="moveSelf">
  3. <div class="nw" @mousedown.stop.prevent="resizeSelf('nw')" @touchstart.stop.prevent="resizeSelf('nw')"></div>
  4. <div class="nn" @mousedown.stop.prevent="resizeSelf('nn')" @touchstart.stop.prevent="resizeSelf('nn')"></div>
  5. <div class="ne" @mousedown.stop.prevent="resizeSelf('ne')" @touchstart.stop.prevent="resizeSelf('ne')"></div>
  6. <div class="ww" @mousedown.stop.prevent="resizeSelf('ww')" @touchstart.stop.prevent="resizeSelf('ww')"></div>
  7. <div class="buttons">
  8. <template v-if="!immutable">
  9. <div v-if="position.label" class="label-text">{{ currentLabel }}</div>
  10. <img v-if="confirmationButton"
  11. alt="confirm" src="@/assets/icons/check.svg"
  12. @click.stop.prevent="confirmSelf"
  13. @touchstart.stop.prevent="confirmSelf">
  14. <img v-if="labelButton"
  15. alt="label" src="@/assets/icons/tag.svg"
  16. @click.stop.prevent="showLabelSelection = true"
  17. @touchstart.stop.prevent="showLabelSelection = true">
  18. <img alt="delete" src="@/assets/icons/trash.svg"
  19. @click.stop.prevent="deleteSelf"
  20. @touchstart.stop.prevent="deleteSelf">
  21. </template>
  22. </div>
  23. <select v-if="!immutable && showLabelSelection"
  24. @touchstart.stop @mousedown.stop
  25. @change="labelSelf"
  26. @focusout="showLabelSelection = false">
  27. <option value="">None</option>
  28. <option v-for="label in labelList"
  29. :key="label.id"
  30. :value="label.id">
  31. {{ label.name }}
  32. </option>
  33. </select>
  34. <div class="ee" @mousedown.stop.prevent="resizeSelf('ee')" @touchstart.stop.prevent="resizeSelf('ee')"></div>
  35. <div class="sw" @mousedown.stop.prevent="resizeSelf('sw')" @touchstart.stop.prevent="resizeSelf('sw')"></div>
  36. <div class="ss" @mousedown.stop.prevent="resizeSelf('ss')" @touchstart.stop.prevent="resizeSelf('ss')"></div>
  37. <div class="se" @mousedown.stop.prevent="resizeSelf('se')" @touchstart.stop.prevent="resizeSelf('se')"></div>
  38. </div>
  39. </template>
  40. <script>
  41. export default {
  42. name: "annotation-box",
  43. props: ['type', 'image', 'position', 'socket', 'boxUrl', 'labels'],
  44. data: function () {
  45. return {
  46. showLabelSelection: false
  47. }
  48. },
  49. computed: {
  50. immutable: function () {
  51. return !this.type;
  52. },
  53. backgroundColor: function () {
  54. switch (this.type) {
  55. case 'user':
  56. return 'rgba(255, 0, 0, 0.3)';
  57. case 'pipeline':
  58. return 'rgba(0, 0, 255, 0.3)';
  59. default:
  60. return 'rgba(255, 255, 255, 0.3)';
  61. }
  62. },
  63. confirmationButton: function () {
  64. return this.type === 'pipeline';
  65. },
  66. labelButton: function () {
  67. return this.type !== 'pipeline';
  68. },
  69. labelList: function () {
  70. return Object.keys(this.labels).map(key => this.labels[key]);
  71. },
  72. currentLabel: function () {
  73. return this.labels[this.position.label].name;
  74. },
  75. style: function () {
  76. const left = this.image.left;
  77. const top = this.image.top;
  78. const width = this.image.width;
  79. const height = this.image.height;
  80. return {
  81. left: (left + this.position.x * width) + 'px',
  82. top: (top + this.position.y * height) + 'px',
  83. width: (this.position.w * width) + 'px',
  84. height: (this.position.h * height) + 'px',
  85. backgroundColor: this.backgroundColor
  86. }
  87. }
  88. },
  89. methods: {
  90. confirmSelf: function () {
  91. this.updateSelf(this.position);
  92. },
  93. labelSelf: function (event) {
  94. const options = event.target.options;
  95. const option = options[options.selectedIndex];
  96. const value = option.value;
  97. if (value)
  98. this.position.label = value;
  99. else
  100. delete this.position.label;
  101. this.updateSelf(this.position);
  102. this.showLabelSelection = false;
  103. },
  104. deleteSelf: function () {
  105. this.socket.post(this.boxUrl, {delete: true});
  106. },
  107. moveSelf: function (event) {
  108. this.$emit('move', event, this.position, this.updateSelf);
  109. },
  110. resizeSelf: function (mode) {
  111. this.$emit('resize', this.position, mode, this.updateSelf);
  112. },
  113. updateSelf: function (value) {
  114. if ('label' in this.position && !('label' in value))
  115. value.label = this.position.label;
  116. this.socket.post(this.boxUrl, value);
  117. }
  118. }
  119. }
  120. </script>
  121. <style scoped>
  122. .annotation-box {
  123. position: absolute;
  124. display: grid;
  125. grid-template-rows: 10px auto 10px;
  126. grid-template-columns: 10px auto 10px;
  127. }
  128. .buttons {
  129. display: flex;
  130. justify-content: center;
  131. align-items: center;
  132. position: relative;
  133. }
  134. img {
  135. width: 2rem;
  136. filter: invert(1);
  137. opacity: 0.9;
  138. }
  139. .nn, .ss {
  140. cursor: ns-resize;
  141. }
  142. .ww, .ee {
  143. cursor: ew-resize;
  144. }
  145. .ne, .sw {
  146. cursor: nesw-resize;
  147. }
  148. .nw, .se {
  149. cursor: nwse-resize;
  150. }
  151. select {
  152. position: absolute;
  153. left: 0;
  154. top: 0;
  155. width: 100%;
  156. height: 100%;
  157. }
  158. .label-text {
  159. position: absolute;
  160. bottom: 0;
  161. color: whitesmoke;
  162. font-size: 80%;
  163. }
  164. </style>