1<!-- Copyright (C) 2017 The Android Open Source Project
2
3     Licensed under the Apache License, Version 2.0 (the "License");
4     you may not use this file except in compliance with the License.
5     You may obtain a copy of the License at
6
7          http://www.apache.org/licenses/LICENSE-2.0
8
9     Unless required by applicable law or agreed to in writing, software
10     distributed under the License is distributed on an "AS IS" BASIS,
11     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12     See the License for the specific language governing permissions and
13     limitations under the License.
14-->
15<template>
16  <div class="bounds" :style="boundsStyle">
17    <div class="rect" v-for="r in rects" :style="rectToStyle(r)"
18        @click="onClick(r)">
19      <span class="label">{{r.label}}</span>
20    </div>
21    <div class="highlight" v-if="highlight" :style="rectToStyle(highlight)" />
22  </div>
23</template>
24
25<script>
26
27export default {
28  name: 'rects',
29  props: ['bounds', 'rects', 'highlight'],
30  data () {
31    return {
32      desiredWidth: 400,
33    };
34  },
35  computed: {
36    boundsC() {
37      if (this.bounds) {
38        return this.bounds;
39      }
40      var width = Math.max(...this.rects.map((r) => r.right));
41      var height = Math.max(...this.rects.map((r) => r.bottom));
42      return {width, height};
43    },
44    boundsStyle() {
45      return this.rectToStyle({top: 0, left: 0, right: this.boundsC.width,
46          bottom: this.boundsC.height});
47    },
48  },
49  methods: {
50    s(sourceCoordinate) {  // translate source into target coordinates
51      return sourceCoordinate / this.boundsC.width * this.desiredWidth;
52    },
53    rectToStyle(r) {
54      var x = this.s(r.left);
55      var y = this.s(r.top);
56      var w = this.s(r.right) - this.s(r.left);
57      var h = this.s(r.bottom) - this.s(r.top);
58      var t = r.transform;
59      var tr = t ? `matrix(${t.dsdx}, ${t.dtdx}, ${t.dsdy}, ${t.dtdy}, ${this.s(t.tx)}, ${this.s(t.ty)})` : '';
60      return `top: ${y}px; left: ${x}px; height: ${h}px; width: ${w}px;` +
61             `transform: ${tr}; transform-origin: 0 0;`
62    },
63    onClick(r) {
64      this.$emit('rect-click', r.ref);
65    },
66  }
67}
68</script>
69
70<style scoped>
71.bounds {
72  position: relative;
73  overflow: hidden;
74}
75.highlight, .rect {
76  position: absolute;
77  box-sizing: border-box;
78  display: flex;
79  justify-content: flex-end;
80}
81.rect {
82  border: 1px solid black;
83  background-color: rgba(100, 100, 100, 0.8);
84}
85.highlight {
86  border: 2px solid red;
87  pointer-events: none;
88}
89.label {
90  align-self: center;
91}
92</style>
93