forked from TimvdLippe/iron-lazy-pages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiron-lazy-pages-behavior.html
121 lines (109 loc) · 3.35 KB
/
iron-lazy-pages-behavior.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
<!--
@license
Copyright (C) 2016, Tim van der Lippe
All rights reserved.
This software may be modified and distributed under the terms
of the BSD license. See the LICENSE file for details.
-->
<link rel="import" href="../iron-selector/iron-selectable.html">
<script>
/** @polymerBehavior Polymer.IronLazyPagesBehavior */
Polymer.IronLazyPagesBehaviorImpl = {
properties: {
// as the selected page is the only one visible, activateEvent
// is both non-sensical and problematic; e.g. in cases where a user
// handler attempts to change the page and the activateEvent
// handler immediately changes it back
// Disabled for this element.
activateEvent: {
type: String,
value: null
},
/**
* Indicates if the page is currently lazy-loading.
*/
loading: {
type: Boolean,
value: false,
notify: true,
readOnly: true
},
/**
* If set to true, the previous item will be removed immediately upon
* switching. By default the item is hidden when the importHref resolves.
*/
hideImmediately: {
type: Boolean,
value: false
},
/**
* If set to true, lazy-loading will not block rendering the page.
* This is recommended to speed up rendering when navigating between routes.
*/
loadAsync: {
type: Boolean,
value: false
},
/**
* When true, elements will be removed from DOM and discarded when
* the page path does not match anymore and re-created and added back
* to the DOM when the page path matches again.
* By default, stamped elements will be hidden but left in the DOM
* when a page path does not match anymore, which is generally results
* in better performance.
*/
restamp: {
type: Boolean,
value: false
}
},
listeners: {
'iron-deselect': '_itemDeselected',
'iron-select': '_itemSelected'
},
_itemDeselected: function(event) {
// Do not listen to possible sub-selectors if these fired and iron-deselect
if (Polymer.dom(event).rootTarget !== this) {
return;
}
if (this.hideImmediately) {
event.detail.item._hide(this.restamp, this.selectedAttribute, this.selectedClass);
} else {
this._lastSelected = event.detail.item;
}
},
_itemSelected: function(event) {
// Do not listen to possible sub-selectors if these fired and iron-select
if (Polymer.dom(event).rootTarget !== this) {
return;
}
this._setLoading(true);
event.detail.item._loadAndShow(function(cb) {
var protectedCb = this.selectedItem === event.detail.item ? cb : function(){};
this._setLoading(false);
if (this._lastSelected) {
this._callback = cb;
this._hide(this._lastSelected, protectedCb)
} else {
protectedCb();
}
if (this.selectedAttribute) {
event.detail.item._toggleAttribute(this.selectedAttribute, true);
}
if (this.selectedClass) {
event.detail.item._toggleClass(this.selectedClass, true);
}
}.bind(this), this);
},
_filterItem: function(node) {
return !this._excludedLocalNames[node.localName] || node.getAttribute('is') === 'iron-lazy-page';
},
_hide: function() {
throw Error('Did not implement "_hide"!');
}
};
Polymer.IronLazyPagesBehavior = [
Polymer.IronSelectableBehavior,
Polymer.IronLazyPagesBehaviorImpl
];
</script>