-
Notifications
You must be signed in to change notification settings - Fork 0
/
tp-indicator.html
124 lines (106 loc) · 3.11 KB
/
tp-indicator.html
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
<link rel="import" href="../polymer/polymer.html">
<!--
`tp-indicator`
Line indicator view
@demo demo/index.html
-->
<dom-module id="tp-indicator">
<template>
<style>
:host {
display: block;
}
</style>
<div id="region" style="position: relative;"></div>
</template>
<script>
'use strict';
Polymer({
is: 'tp-indicator',
properties: {
/* View width */
width: {
type: Number,
value: 0,
},
/* View height */
height: {
type: Number,
value: 120,
}
},
/* type {Array<Object>} */
_indicators: [],
/* type {Number} */
_width: 0,
/* type {Number} */
_height: 0,
_adjust: function (indicator) {
const v = indicator.direction === 'vertical';
const style = indicator._element.style;
style.width = (v ? 1 : this._width) + 'px';
style.height = (v ? this._height : 1) + 'px';
style.top = (v ? 0 : this._height * indicator.position) + 'px';
style.left = (v ? this._width * indicator.position : 0) + 'px';
},
ready: function () {
window.addEventListener('resize', e => {
this.resize();
}, false);
},
attached: function () {
this.resize();
},
/**
* Resizes the view.
*/
resize: function () {
this._width = this.get('width');
this._height = this.get('height');
if (!this._width)
this._width = this.clientWidth;
if (!this._height)
this._height = this.clientHeight;
const region = this.$.region;
region.style.width = this._width + 'px';
region.style.height = this._height + 'px';
for (let indicator of this._indicators)
this._adjust(indicator);
},
/**
* Adds an indicator.
* @param {Object} param object containing direction, position, color
* {String} direction 'vertical' or 'horizontal'
* {Number} position [0:1]
* {String} color indicator's color e.g., rgb(0,0,0)
* @return {Number} indicator index
*/
add: function (param) {
const index = this._indicators.length;
this._indicators.push(param);
const element = document.createElement('div');
element.style.position = 'absolute';
element.style.backgroundColor = param.color;
param._element = element;
this._adjust(param);
this.$.region.appendChild(element);
return index;
},
/**
* Updates an indicator.
* @param {Number} index indicator index
* @param {Number} position indicator position
*/
update: function(index, position) {
const indicator = this._indicators[index];
if (!indicator)
return;
indicator.position = position;
if (indicator.direction === 'vertical')
indicator._element.style.left = this._width * position + 'px';
else
indicator._element.style.top = this._height * position + 'px';
}
});
</script>
</dom-module>