-
Notifications
You must be signed in to change notification settings - Fork 0
/
x-pages.html
148 lines (119 loc) · 3.38 KB
/
x-pages.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
<link rel="import" href="../polymer/polymer.html">
<!--
`x-pages`
A simple content switcher.
@demo demo/index.html
-->
<dom-module id="x-pages">
<template>
<style>
:host {
display: block;
}
:host ::content > :not([visible]) {
display: none !important;
}
:host([hidden]) {
display: none;
}
</style>
<content></content>
</template>
<script>
Polymer({
/**
* Fired when an element has been selected. Detail object contains
* - `previous` - the previously selected element. Will be `null` if no previous element exists.
* - `current` - the currenlty selected element. Will be `null` if no element is found.
* @event page-change
*/
is: 'x-pages',
properties: {
/*
* The currently selected string to match the element's `selected` attribute or the element's `tagName`.
*/
selected: {
type: String,
observer: `_selectedObserver`
},
/*
* Scrolls the page to the top if true.
*/
autoScroll: {
type: Boolean,
value: false
},
/*
* Previously selected element.
*/
previous: {
type: Object
},
/*
* Currently selected element.
*/
current: {
type: Object
},
},
/*
* Observe the `selected` property to handle visible elements.
*/
_selectedObserver: function(newValue, oldValue) {
this._autoScrollToTop();
let element = this._findElements(newValue, oldValue);
this.previous = element.previous;
this.current = element.current;
this._deactivateChild(this.previous);
this._activateChild(this.current);
const event = this.fire(`page-change`, { current: this.current, previous: this.previous });
},
/*
* Finds the previously selected elements and the currently selected elements.
*/
_findElements(newSelected, oldSelected) {
const childs = Polymer.dom(this).children;
let currentElement = null;
let previousElement = null;
for (let i = 0; i < childs.length; i++) {
const child = childs[i];
const selected = child.getAttribute(`selected`);
let tagName = child.tagName.toLowerCase();
if ((selected === tagName) || (selected === newSelected)) {
currentElement = child;
} else if ((selected === tagName) || (selected === oldSelected)) {
previousElement = child;
}
}
return {
current: currentElement,
previous: previousElement
}
},
/*
* Activate a child by adding the `visible` attribute.
*/
_activateChild: function(child) {
if (child) {
child.setAttribute(`visible`, ``);
}
},
/*
* Deactivate a child by removing the `visible` attribute.
*/
_deactivateChild: function(child) {
if (child) {
child.removeAttribute(`visible`, ``);
}
},
/*
* If `autoScroll` is true, scroll window to top.
*/
_autoScrollToTop: function() {
if (this.autoScroll) {
window.scrollTo(0, 0);
}
},
});
</script>
</dom-module>