-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpx-view.html
More file actions
300 lines (271 loc) · 8.77 KB
/
Copy pathpx-view.html
File metadata and controls
300 lines (271 loc) · 8.77 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<!--
Relative paths assume component is being run from inside an app or another component, where dependencies are flat
siblings. When this component is run from its own repo (e.g. ui tests, examples), we assume the server is started with
'grunt depserve' (or similar server setup) to enable correct finding of bower dependencies for local runs.
-->
<link rel="import" href="../polymer/polymer.html"/>
<dom-module id="px-view">
<template>
<!-- insert element here -->
</template>
<script>
'use strict';
Polymer({
is: 'px-view',
properties: {
/**
*
* Set the 'active' attribute on the <px-view> element to true and the components will be
* loaded and displayed
*
* @attribute preload
* @type Boolean
* @default false
*/
active: {
type: Boolean,
value: false
},
/**
*
* Set the 'preload' attribute on the <px-view> element and the components will be
* loaded immediately or
* lazy-loaded on demand. Lazy-loading is on by default to avoid unnecessary HTTP requests.
*
* @attribute preload
* @type Boolean
* @default false
*/
preload: {
type: Boolean,
value: false
},
/**
*
* Set to true to load this view asynchronously.
*
* @attribute loadAsync
* @type Boolean
* @default false
*/
loadAsync: {
type: Boolean,
value: false
},
/**
*
* Status tracks a px-view component over it's lifecycle
* Steps: 'unloaded' -> 'loading' -> 'loaded' || 'failed' -> 'attached' -> 'hidden' || 'shown'
*
* @attribute status
* @type String
* @readonly
* @access public
*/
status: {
type: String,
value: 'unloaded',
notify: true,
reflectToAttribute: true,
observer: '_statusChanged'
},
/**
*
* elementTagName sets the name of the tag to attach to the DOM which must match component name
*
* @attribute elementTagName
* @type String
* @access private
*/
_elementTagName: {
type: String,
computed: '_computeElementTagName(elementHref)',
observer: '_elementTagNameUpdated'
},
/**
*
* elementHref defines relative URL path of web component html file
*
* @attribute elementHref
* @type String
* @access public
*/
elementHref: {
type: String
},
/**
*
* The display property defines the value of the component's display property when view is shown
*
* @attribute elementHref
* @type String
* @access public
* @default 'block'
*/
display: {
type: String,
value: "block"
},
/**
*
* Data to be passd through to view when it is attached.
* Must be passed in as a stringified object using JSON.stringify()
*
* @attribute elementData
* @type Object
* @access public
* @default {}
*/
elementData: {
type: Object,
value: function() { return {}; }
},
_importLink: {
type: Object,
value: function() { return {}; }
}
},
observers: [
'_checkStatus(status, active, preload, display, _elementTagName)',
],
_element: null, // Reference to the loaded view DOM element
// when state is 'unloaded', if active or preload are true, start loading
_onUnloaded: function() {
if (this.active || this.preload) {
this._loadElement();
}
},
_isElementRegistered: function(elementId) {
var list = Polymer.telemetry.registrations;
// Mimics Array#find
for (var i = 0, length = list.length; i < length; i++) {
var element = list[i];
if (element.is === elementId) {
return true;
}
}
return false;
},
_onLoading: function() {
// do nothing
},
// when state is 'loaded', if active, attach element to DOM
_onLoaded: function() {
if (this.active) {
this._attachElement();
}
},
// when state is 'attached', if active, set status to 'shown', otherwise 'hidden'
_onAttached: function() {
var newStatus = this.active ? 'shown' : 'hidden';
this.set('status', newStatus);
},
// when state is 'shown', if active, set element display property
_onShown: function() {
if (this.active) {
this._element.style.display = this.display;
} else {
this.set('status', 'hidden');
}
},
// when state is 'hidden', if active, set element display property to 'none'
_onHidden: function() {
if (!this.active) {
this._element.style.display = 'none';
} else {
this.set('status', 'shown');
}
},
// _stateFunctions is an object literal with references to functions
// this maps state string values to state functions
_getStateFunction: function(status) {
var stateFunctions = {
'unloaded': this._onUnloaded,
'loading': this._onLoading,
'loaded': this._onLoaded,
'attached': this._onAttached,
'shown': this._onShown,
'hidden': this._onHidden
};
return stateFunctions[status];
},
// 'unloaded' -> 'loading' -> 'loaded' || 'failed' -> 'attached' -> 'hidden' || 'shown'
_checkStatus: function() {
// lookup state function and call it using fn.apply to maintain scope
this._getStateFunction(this.status).apply(this);
},
_attachElement: function() {
// confirm element tag name matches loaded element
this._element = document.createElement(this._elementTagName);
this._element.id = this._elementTagName + '-' + new Date().getTime();
this._element.setAttribute('element-data', JSON.stringify(this.elementData));
Polymer.dom(this.root).appendChild(this._element);
this.set('status', 'attached');
},
_loadElement: function() {
this.set('status', 'loading');
var that = this;
// check if this element is already registered, e.g. from a vulcanized bundle
if(this._isElementRegistered(this._elementTagName)){
that.set('status', 'loaded');
} else {
this._importLink = this.importHref(this.elementHref, function(e) {
if (Object.getOwnPropertyNames(that._importLink).length == 0) {
that.set('status', 'loaded');
} else {
var modulesLoaded = Polymer.dom(that._importLink.import).querySelectorAll('dom-module');
if (modulesLoaded.some(function(ele) {return ele.id === that._elementTagName;})) {
that.set('status', 'loaded');
} else {
console.log('Error: dom-module of imported element does not match element tag name');
that.set('status', 'failed');
}
}
}, function(error) {
this.set('status', 'failed');
}, this.loadAsync);
}
},
_computeElementTagName: function() {
var filenameRegex = /(?=([\w-]+)\.\w{3,4}$).+/;
var match;
if ((match = filenameRegex.exec(this.elementHref)) !== null) {
if (match.index === filenameRegex.lastIndex) {
filenameRegex.lastIndex++;
}
}
return match[1];
},
_elementTagNameUpdated: function(current, previous) {
if(typeof previous !== 'undefined') {
// polymer has changed the tagname, time to reset!
this._resetView();
}
},
_resetView: function() {
this._removeAllElements();
this.set('_importLink', {});
this.set('status', 'unloaded');
},
_removeAllElements: function() {
var nodes = Polymer.dom(this.root).children;
var that = this;
nodes.forEach(
function(node) {
Polymer.dom(that.root).removeChild(node);
}
);
},
/**
* Fired whenever the view status is changed.
* @event view-status-changed
* @param {String} {The view status}
*/
_statusChanged: function() {
if (this._element) {
this.fire('px-view-status-changed', { status: this.status }, { node: this._element, bubbles: false });
}
}
});
</script>
</dom-module>