forked from fluidnext/paper-grid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebug-grid.js
194 lines (168 loc) · 7.67 KB
/
debug-grid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
class DebugGrid extends PolymerElement {
static get template() {
return html`
<style>
:host {
position: absolute;
-webkit-touch-callout: none; /* iOS Safari */
-webkit-user-select: none; /* Safari */
-khtml-user-select: none; /* Konqueror HTML */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* Internet Explorer/Edge */
user-select: none; /* Non-prefixed version, currently supported by Chrome and Opera */
}
#container {
position: relative;
}
.legend {
position: absolute;
color: #bfbfbf;
font-family: Arial;
}
.vertical-legend {
padding-top: 5px;
margin-left: -7px;
}
.horizontal-legend {
margin-top: -2px;
padding-left: 5px;
}
.axis {
position: absolute;
/*z-index: -1;*/
}
.vertical-axis {
border-left: 1px solid #bfbfbf;
}
.horizontal-axis {
border-top: 1px solid #bfbfbf;
}
.vertical-axis.center-axis {
border-left-style: dashed;
}
.horizontal-axis.center-axis {
border-top-style: dashed;
}
.margin-axis, .center-axis {
/*z-index: -2;*/
border-color: #eaeaea;
}
</style>
<div id="container">
<dom-repeat items="[[verticalAxes]]">
<template>
<div class="axis vertical-axis margin-axis" style$="[[_computeAxisStyle('vertical', index, negativeOffset, cellWidth, cellHeight, cellMargin, rowCount, colCount)]]"></div>
<div class="axis vertical-axis" style$="[[_computeAxisStyle('vertical', index, 0, cellWidth, cellHeight, cellMargin, rowCount, colCount)]]"></div>
<div class="axis vertical-axis margin-axis" style$="[[_computeAxisStyle('vertical', index, positiveOffset, cellWidth, cellHeight, cellMargin, rowCount, colCount)]]"></div>
<div class="axis vertical-axis center-axis" style$="[[_computeAxisStyle('vertical', index, verticalCenterOffset, cellWidth, cellHeight, cellMargin, rowCount, colCount)]]"></div>
<span class="legend vertical-legend" style$="[[_computeLegendStyle('vertical', index, cellWidth, cellHeight, cellMargin, rowCount, colCount)]]">[[_computeLegendValue(index, cellWidth, cellMargin)]]</span>
</template>
</dom-repeat>
<dom-repeat items="[[horizontalAxes]]">
<template>
<div class="axis horizontal-axis margin-axis" style$="[[_computeAxisStyle('horizontal', index, negativeOffset, cellWidth, cellHeight, cellMargin, rowCount, colCount)]]"></div>
<div class="axis horizontal-axis" style$="[[_computeAxisStyle('horizontal', index, 0, cellWidth, cellHeight, cellMargin, rowCount, colCount)]]"></div>
<div class="axis horizontal-axis margin-axis" style$="[[_computeAxisStyle('horizontal', index, positiveOffset, cellWidth, cellHeight, cellMargin, rowCount, colCount)]]"></div>
<div class="axis horizontal-axis center-axis" style$="[[_computeAxisStyle('horizontal', index, horizontalCenterOffset, cellWidth, cellHeight, cellMargin, rowCount, colCount)]]"></div>
<span class="legend horizontal-legend" style$="[[_computeLegendStyle('horizontal', index, cellWidth, cellHeight, cellMargin, rowCount, colCount)]]">[[_computeLegendValue(index, cellHeight, cellMargin)]]</span>
</template>
</dom-repeat>
</div>
`
}
static get properties() {
return {
cellHeight: {
type: Number,
value: 100,
reflectToAttribute: true,
observer: 'computeOffsets'
},
cellWidth: {
type: Number,
value: 100,
reflectToAttribute: true,
observer: 'computeOffsets'
},
cellMargin: {
type: Number,
value: 0,
reflectToAttribute: true,
observer: 'computeOffsets'
},
rowCount: {
type: Number,
value: 10,
reflectToAttribute: true,
observer: 'rowCountChanged'
},
colCount: {
type: Number,
value: 10,
reflectToAttribute: true,
observer: 'colCountChanged'
}
}
}
_computeAxisStyle(direction, index, offset, width, height, margin, rowCount, colCount) {
let positionValue, positionProp,
size, heightValue, widthValue,
lastIndex;
if (direction === 'vertical') {
positionProp = 'left';
size = width;
heightValue = height * rowCount + (rowCount - 1) * margin;
widthValue = 1;
lastIndex = colCount;
} else {
positionProp = 'top';
size = height;
heightValue = 1;
widthValue = width * colCount + (colCount - 1) * margin;
lastIndex = rowCount;
}
// First axis (but center axis)
if (index === 0 && this.horizontalCenterOffset !== offset && this.verticalCenterOffset !== offset) {
positionValue = 0;
// Last axis
} else if (index === lastIndex) {
positionValue = index * size + (index - 1) * margin;
} else {
positionValue = index * size + (index - 1) * margin + margin / 2 + offset;
}
return `${positionProp}:${positionValue}px; width:${widthValue}px; height:${heightValue}px`;
}
_computeLegendStyle(direction, index, width, height, margin, rowCount, colCount) {
let leftValue, topValue;
if (direction === 'vertical') {
leftValue = index * width + (index - 1) * margin;
topValue = rowCount * height + (rowCount - 1) * margin;
} else {
leftValue = colCount * width + (colCount - 1) * margin;
topValue = index * height + (index - 1) * margin;
}
return `top:${topValue}px; left:${leftValue}px;`;
}
_computeLegendValue(index, size, margin) {
return index * size + (index > 0 ? (index - 1) * margin + margin / 2 : 0);
}
computeOffsets() {
this.positiveOffset = this.cellMargin / 2;
this.negativeOffset = -this.positiveOffset;
this.verticalCenterOffset = this.positiveOffset + this.cellWidth / 2;
this.horizontalCenterOffset = this.positiveOffset + this.cellHeight / 2;
}
rowCountChanged(count) {
let row = typeof count === 'string' ? parseInt(count) : count;
this.horizontalAxes = Array.from({ length: row + 1 });
}
colCountChanged(count) {
let col = typeof count === 'string' ? parseInt(count) : count;
this.verticalAxes = Array.from({ length: col + 1 });
}
constructor() {
super();
}
}
window.customElements.define('debug-grid', DebugGrid);