-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic-page-dots.html
226 lines (193 loc) · 5.68 KB
/
basic-page-dots.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<!--
Aspect which adds a set of small dots to a collective tracking a list of items.
There will be one dot for each item, and the dot for the currently selected item
will be shown selected.
Clicking a dot will select the corresponding list item.
@element basic-page-dots
-->
<link rel="import" href="../basic-aspect/basic-aspect.html">
<dom-module id="basic-page-dots">
<template>
<style>
:host {
display: -webkit-inline-flex;
display: inline-flex;
position: relative;
}
#dots {
bottom: 0;
display: -webkit-flex;
display: flex;
-webkit-justify-content: center;
justify-content: center;
position: absolute;
width: 100%;
z-index: 1;
}
.dot {
background: rgba(255, 255, 255, 0.4);
border-radius: 7px;
box-shadow: 0 0 1px 1px rgba(0, 0, 0, 0.5);
box-sizing: border-box;
height: 8px;
margin: 7px 5px;
padding: 0;
transition: background 0.2s box-shadow 0.2s;
width: 8px;
}
.dot:hover {
background: rgba(0, 0, 0, 0.75);
box-shadow: 0 0 1px 3px rgba(255, 255, 255, 0.5);
}
.dot.selected {
background: rgba(255, 255, 255, 0.95);
}
@media (min-width: 768px) {
.dot {
height: 12px;
width: 12px;
}
}
</style>
<!--
REVIEW: These dots aren't buttons, because they're never meant to be used
on their own. There should always be some other, more accessible, way to
navigate the content.
-->
<!--
<template id="dotTemplate">
<div class="dot" on-click="dotClick"></div>
</template>
-->
<!-- TODO: Replace with something that's basically a list box -->
<div id="dots"></div>
<content></content>
</template>
</dom-module>
<script>
Polymer({
behaviors: [Basic.Aspect],
contribute: {
applySelection: function(item, selected) {
var index = this.collective.indexOfItem(item);
// See if the corresponding dot has already been created.
// If not, the correct dot will be selected when it gets created.
var dots = this.dots;
if (dots && dots.length > index) {
var dot = this.dots[index];
if (dot) {
dot.classList.toggle('selected', selected);
}
}
},
collectiveChanged: function() {
// Apply any selection made before assimilation.
if (this._prematureSelectedIndex
&& 'selectedIndex' in this.collective
&& this.collective.selectedIndex === -1) {
this.collective.selectedIndex = this._prematureSelectedIndex;
this._prematureSelectedIndex = null;
}
// TODO: Move the requirement for a selction to basic-item-selection. This
// should ideally pick the nearest item to the previously-selected item.
// if (this.collective.selectedItem === null && this.collective.items != null && this.collective.selectFirst) {
// this.collective.selectFirst();
// }
},
// TODO: Seems like this ultimately belongs in Collective.
itemsChanged: function() {
// Raise a property change notification so binding can update.
// if (Object.getNotifier) {
// Object.getNotifier(this).notify({
// type: "update",
// name: "items"
// });
// }
// TODO: This reports an incorrect number of items if an item is removed.
this._createDots(this.collective.items.length);
}
},
dotClick: function(event) {
var dot = event.target;
var dotIndex = this.dots.indexOf(dot);
if (dotIndex >= 0) {
this.collective.selectedIndex = dotIndex;
}
},
get dots() {
return [].slice.call(this.$.dots.querySelectorAll('.dot'));
},
is: 'basic-page-dots',
properties: {
selectedIndex: {
type: Number
},
selectedItem: {
type: Object
}
},
ready: function() {
this.$.dots.addEventListener('click', function(event) {
this.dotClick(event);
}.bind(this));
},
/**
* The index of the item which is currently selected, or -1 if there is no
* selection.
*
* @property selectedIndex
* @type Number
*/
get selectedIndex() {
// See note at basic-item-selection's selectedIndex getter.
if (this._readied) {
return this.collective.selectedIndex;
}
},
set selectedIndex(index) {
if ('selectedIndex' in this.collective) {
this.collective.selectedIndex = index;
} else {
// Selection is being made before the collective supports it.
this._prematureSelectedIndex = index;
}
},
/**
* The currently selected item, or null if there is no selection.
*
* @property selectedItem
* @type Object
*/
get selectedItem() {
return this.collective.selectedItem;
},
set selectedItem(item) {
this.collective.selectedItem = item;
},
_createDots: function(count) {
var selectedIndex = this.selectedIndex;
var dotContainer = this.$.dots;
var existingDotCount = dotContainer.children.length;
if (count === existingDotCount) {
return;
} else if (existingDotCount > count) {
// Remove extra dots.
while (dotContainer.children.length > count) {
Polymer.dom(dotContainer).removeChild(dotContainer.children[0]);
}
} else {
// Create needed dots.
for (var i = existingDotCount; i < count; i++) {
var newDot = document.createElement('div');
newDot.classList.add('dot');
newDot.classList.add('style-scope');
newDot.classList.add('basic-page-dots');
newDot.classList.toggle('selected', i === selectedIndex);
dotContainer.appendChild(newDot);
}
}
},
// Tracks selection before the collective supports it.
_prematureSelectedIndex: null
});
</script>