This repository has been archived by the owner on Sep 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathiron-lazy-pages-behavior.js
169 lines (153 loc) · 4.54 KB
/
iron-lazy-pages-behavior.js
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
/**
@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.
*/
import { Base } from '@polymer/polymer/polymer-legacy.js';
import { IronSelectableBehavior } from '@polymer/iron-selector/iron-selectable.js';
import { dom } from '@polymer/polymer/lib/legacy/polymer.dom.js';
import { resolveUrl } from '@polymer/polymer/lib/utils/resolve-url.js';
/** @polymerBehavior */
export const 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
},
/**
* The set of excluded elements where the key is the `localName`
* of the element that will be ignored from the item list.
*
* @default {template: 1}
*/
_excludedLocalNames: {
type: Object,
value: function() {
return {
// 'template': 1,
'dom-bind': 1,
// Explicitly opt-in for `dom-if`
// 'dom-if': 1,
'dom-repeat': 1,
};
}
}
},
attached: function() {
this.addEventListener('dom-change', function(event) {
// Do not listen to possible sub-selectors if these fired and iron-deselect
if (event.target.parentNode !== this) {
return;
}
var target = event.target;
if (target['if']) {
var sibling = target;
while ((sibling = sibling.previousElementSibling) != this.__previousSibling) {
sibling.classList.add('iron-lazy-selected');
}
}
}.bind(this));
},
listeners: {
'iron-deselect': '_itemDeselected',
'iron-select': '_itemSelected'
},
_itemDeselected: function(event) {
// Do not listen to possible sub-selectors if these fired and iron-deselect
if (dom(event).rootTarget !== this) {
return;
}
if (this.hideImmediately) {
event.detail.item['if'] = false;
event.detail.item.classList.remove('iron-lazy-selected');
} else {
this._lastSelected = event.detail.item;
}
},
_itemSelected: function(event) {
// Do not listen to possible sub-selectors if these fired and iron-select
if (dom(event).rootTarget !== this) {
return;
}
var self = this;
this._setLoading(true);
var page = event.detail.item;
var onFinished = function() {
self._setLoading(false);
if (self.selectedItem === page) {
self._show(page);
}
};
if (!page.classList.contains('iron-lazy-loaded') && page.dataset.path) {
this._loadPage(page, onFinished);
} else {
onFinished();
}
},
/**
* Provide extension point for tests, to make the element actually testable.
*/
_loadPage: function(page, onFinished) {
// When not loaded in shadow dom, `this.parentNode.host` is undefined. Resort back to the parentNode
var parentHost = this.parentNode;
while (parentHost && parentHost.nodeName !== '#document-fragment') {
parentHost = parentHost.parentNode;
}
var url;
if (parentHost && parentHost.host && parentHost.host.resolveUrl) {
url = parentHost.host.resolveUrl(page.dataset.path);
} else {
url = page.dataset.path;
}
this._loadUrl(url, ()=> {
page.classList.add('iron-lazy-loaded');
onFinished();
}, (e) => {
onFinished();
throw e;
});
},
_loadUrl: function(url, onSuccess, onFailure) {
import(resolveUrl(url))
.then(onSuccess)
.catch(onFailure);
},
_show: function(page) {
if (this._lastSelected) {
this._lastSelected['if'] = false;
this._lastSelected.classList.remove('iron-lazy-selected');
}
page.classList.add('iron-lazy-selected');
page['if'] = true;
this.__previousSibling = page.previousElementSibling;
}
};
/** @polymerBehavior */
export const IronLazyPagesBehavior = [
IronSelectableBehavior,
IronLazyPagesBehaviorImpl
];