-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaribou-year.html
286 lines (249 loc) · 9.18 KB
/
caribou-year.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../paper-button/paper-button.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
<dom-module id="caribou-year">
<template>
<style>
:host {
display: block;
overflow-y: auto;
height: 342px;
/*Padding added to hide the scrollbar*/
padding-right: 20px;
width: 100%;
}
:host([hidden]) {
display: none;
}
.years {
@apply --layout-vertical;
}
.years paper-button {
font-size: 15px;
}
.years paper-button .current {
padding: 5px;
color: var(--current-selected-year-color, var(--primary-color));
font-size: 25px;
}
.iconBtnNav {
margin: auto;
}
</style>
<div id="years" class="years">
<template is="dom-repeat" items="{{years}}">
<paper-button data-year$="{{item.value}}" on-tap="_yearSelected">
<span class$="{{_getClassYear(item.isCurrentYear)}}">{{item.value}}</span>
</paper-button>
</template>
</div>
</template>
<script>
/**
* `caribou-year`
*
* This element is used to select the year.
*
*
* ### Styling The following custom properties and mixins are available for styling:
*
* Custom property | Description | Default
* ----------------|-------------|----------
* `--current-selected-year-color`| Color of the current year selected | `--primary-color`
*
* @memberof Caribouflex
* @customElement
* @polymer
* @element caribou-year
*/
class YearElement extends Polymer.Element {
/**
* Fired when the stepper is finished and all the steps are saved.
*
* @event year-selected
* @param {Object} detail {year:2015}
*/
static get is() {
return 'caribou-year';
}
static get properties() {
return {
/**
* Indicate if the element is hidden or not
*/
hidden: {
type: Boolean,
value: false
},
/**
* This is the current year or year selected.
* This year will appear selecte in the view.
*/
year: {
type: Number,
observer: "setYear"
},
/**
* May be deprecated... TODO check
*
*/
currentYear: {
type: Number
},
/**
* This is the years array used to display the list
*/
years: {
type: Array,
value: function () {
return []
}
},
/**
* This is a flag used during the creation of the array
*/
_init: {
type: Array,
value: false
},
/**
* This is the last year added to the array.
* This is used when we add more values.
*/
lastYear: {
type: Number
},
/**
* This is the first year of the array
* Used when we get the previous years
*/
firstYear: {
type: Number
}
};
}
ready() {
super.ready();
this.$.years.addEventListener("mousewheel", this._mouseYearNav.bind(this), {
passive: true
});
}
/**
* This function is used to initialize the array when the year change
* @param {Number} year The year value
*/
setYear(year) {
this.year = Number(year);
this.currentYear = Number(year);;
this.year = Number(year);;
this.lastYear = Number(year);;
this.initYearArray();
}
/**
* clear years array and re-complete calling the functions.
*/
initYearArray() {
this.splice("years", 0, this.years.length);
this._init = false;
this._createYears();
}
/**
* This function is used to intialize the array.
* We will check the init property, if it's false that means the array has been already initialized.
* Otherwise we add 4 year before the current one.
* @private
*/
_createYears() {
if (this.lastYear === undefined)
this.lastYear = this.year;
if (!this._init) {
this.push('years', this._getYearObject(Number(this.lastYear - 3)));
this.push('years', this._getYearObject(Number(this.lastYear - 2)));
this.push('years', this._getYearObject(Number(this.lastYear - 1)));
this.push('years', this._getYearObject(Number(this.lastYear)));
this._init = true;
}
for (var i = 0; i < 10; i++) {
this.lastYear = this.lastYear + 1;
this.push('years', this._getYearObject(Number(this.lastYear)));
}
}
/**
* Complete the array with a couple of years added to the begining of the array.
* @private
*/
_getPreviousYears() {
if (this.firstYear === undefined)
this.firstYear = this.year - 3;
for (var i = 0; i < 10; i++) {
this.firstYear = this.firstYear - 1;
this.splice("years", 0, 0, this._getYearObject(Number(this.firstYear)));
}
this.scrollTop = 600;
}
/**
* This is used to init the style
* @param {Number} year
* @private
*/
_getYearObject(year) {
let isCurrentYear = false;
if (this.currentYear === year)
isCurrentYear = true;
return {
value: year,
isCurrentYear: isCurrentYear
};
}
/**
* This is used to the the class for the style
* @param {Boolean} isCurrentYear
* @private
*/
_getClassYear(isCurrentYear) {
if (isCurrentYear) return "current";
}
/**
* This is a callback function used when the scroll from the mouse is executed.
* this will get the previous or the next years.
* @param {Object} e the event
* @private
*/
_mouseYearNav(e) {
//e.wheelDelta >= 0 go to top otherwise go to bottom
if (e.wheelDelta >= 0 && this.scrollTop === 0) {
this._getPreviousYears();
} else if (this.scrollTop + this.clientHeight >= this.scrollHeight) {
this._createYears();
}
}
/**
* This is used when a year is selected.
* An event will be dispatch.
* @param {Object} e the event
* @private
*/
_yearSelected(e) {
//set the last year as the year selected before redraw the array
this.setYear(Number(e.currentTarget.dataset.year));
//The scroll year view has to be set to the top
this.scrollTop = 0;
// The event has to be sent after the scrollTop otherwise the scroll will never happened
// because the view would have switched.
this.dispatchEvent(new CustomEvent('year-selected', {
bubbles: true,
composed: true,
detail: {
year: e.currentTarget.dataset.year
}
}));
}
}
window.customElements.define(YearElement.is, YearElement);
/**
* @namespace Caribouflex
*/
window.Caribouflex = window.Caribouflex || {};
window.Caribouflex.YearElement = YearElement;
</script>
</dom-module>